blob: 1ae17d97bfc4d3f8ae4d8a8ed5aa76523a687de8 [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 Moolenaara91a7132021-03-25 21:12:15 +0100799 * If command modifiers were applied restore them.
800 */
801 static void
802may_restore_cmdmod(funclocal_T *funclocal)
803{
804 if (funclocal->floc_restore_cmdmod)
805 {
806 cmdmod.cmod_filter_regmatch.regprog = NULL;
807 undo_cmdmod(&cmdmod);
808 cmdmod = funclocal->floc_save_cmdmod;
809 funclocal->floc_restore_cmdmod = FALSE;
810 }
811}
812
813/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200814 * Return TRUE if an error was given or CTRL-C was pressed.
815 */
816 static int
817vim9_aborting(int prev_called_emsg)
818{
819 return called_emsg > prev_called_emsg || got_int || did_throw;
820}
821
822/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 * Execute a function by "name".
824 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100825 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 * Returns FAIL if not found without an error message.
827 */
828 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100829call_by_name(
830 char_u *name,
831 int argcount,
832 funclocal_T *funclocal,
833 ectx_T *ectx,
834 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835{
836 ufunc_T *ufunc;
837
838 if (builtin_function(name, -1))
839 {
840 int func_idx = find_internal_func(name);
841
842 if (func_idx < 0)
843 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200844 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 return FAIL;
846 return call_bfunc(func_idx, argcount, ectx);
847 }
848
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200849 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200850
851 if (ufunc == NULL)
852 {
853 int called_emsg_before = called_emsg;
854
855 if (script_autoload(name, TRUE))
856 // loaded a package, search for the function again
857 ufunc = find_func(name, FALSE, NULL);
858 if (vim9_aborting(called_emsg_before))
859 return FAIL; // bail out if loading the script caused an error
860 }
861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100863 {
864 if (ufunc->uf_arg_types != NULL)
865 {
866 int i;
867 typval_T *argv = STACK_TV_BOT(0) - argcount;
868
869 // The function can change at runtime, check that the argument
870 // types are correct.
871 for (i = 0; i < argcount; ++i)
872 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100873 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100874
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100875 if (i < ufunc->uf_args.ga_len)
876 type = ufunc->uf_arg_types[i];
877 else if (ufunc->uf_va_type != NULL)
878 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100879 if (type != NULL && check_typval_arg_type(type,
880 &argv[i], i + 1) == FAIL)
881 return FAIL;
882 }
883 }
884
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100885 return call_ufunc(ufunc, NULL, argcount, funclocal, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887
888 return FAIL;
889}
890
891 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100892call_partial(
893 typval_T *tv,
894 int argcount_arg,
895 funclocal_T *funclocal,
896 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100897{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200898 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200899 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100901 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902
903 if (tv->v_type == VAR_PARTIAL)
904 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200905 partial_T *pt = tv->vval.v_partial;
906 int i;
907
908 if (pt->pt_argc > 0)
909 {
910 // Make space for arguments from the partial, shift the "argcount"
911 // arguments up.
912 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
913 return FAIL;
914 for (i = 1; i <= argcount; ++i)
915 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
916 ectx->ec_stack.ga_len += pt->pt_argc;
917 argcount += pt->pt_argc;
918
919 // copy the arguments from the partial onto the stack
920 for (i = 0; i < pt->pt_argc; ++i)
921 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
922 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923
924 if (pt->pt_func != NULL)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100925 return call_ufunc(pt->pt_func, pt, argcount, funclocal, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927 name = pt->pt_name;
928 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200929 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200931 if (name != NULL)
932 {
933 char_u fname_buf[FLEN_FIXED + 1];
934 char_u *tofree = NULL;
935 int error = FCERR_NONE;
936 char_u *fname;
937
938 // May need to translate <SNR>123_ to K_SNR.
939 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
940 if (error != FCERR_NONE)
941 res = FAIL;
942 else
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100943 res = call_by_name(fname, argcount, funclocal, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200944 vim_free(tofree);
945 }
946
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100947 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948 {
949 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200950 semsg(_(e_unknownfunc),
951 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952 return FAIL;
953 }
954 return OK;
955}
956
957/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200958 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
959 * TRUE.
960 */
961 static int
962error_if_locked(int lock, char *error)
963{
964 if (lock & (VAR_LOCKED | VAR_FIXED))
965 {
966 emsg(_(error));
967 return TRUE;
968 }
969 return FALSE;
970}
971
972/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100973 * Give an error if "tv" is not a number and return FAIL.
974 */
975 static int
976check_for_number(typval_T *tv)
977{
978 if (tv->v_type != VAR_NUMBER)
979 {
980 semsg(_(e_expected_str_but_got_str),
981 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
982 return FAIL;
983 }
984 return OK;
985}
986
987/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100988 * Store "tv" in variable "name".
989 * This is for s: and g: variables.
990 */
991 static void
992store_var(char_u *name, typval_T *tv)
993{
994 funccal_entry_T entry;
995
996 save_funccal(&entry);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100997 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100998 restore_funccal();
999}
1000
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001001/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001002 * Convert "tv" to a string.
1003 * Return FAIL if not allowed.
1004 */
1005 static int
1006do_2string(typval_T *tv, int is_2string_any)
1007{
1008 if (tv->v_type != VAR_STRING)
1009 {
1010 char_u *str;
1011
1012 if (is_2string_any)
1013 {
1014 switch (tv->v_type)
1015 {
1016 case VAR_SPECIAL:
1017 case VAR_BOOL:
1018 case VAR_NUMBER:
1019 case VAR_FLOAT:
1020 case VAR_BLOB: break;
1021 default: to_string_error(tv->v_type);
1022 return FAIL;
1023 }
1024 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001025 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001026 clear_tv(tv);
1027 tv->v_type = VAR_STRING;
1028 tv->vval.v_string = str;
1029 }
1030 return OK;
1031}
1032
1033/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001034 * When the value of "sv" is a null list of dict, allocate it.
1035 */
1036 static void
1037allocate_if_null(typval_T *tv)
1038{
1039 switch (tv->v_type)
1040 {
1041 case VAR_LIST:
1042 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001043 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001044 break;
1045 case VAR_DICT:
1046 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001047 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001048 break;
1049 default:
1050 break;
1051 }
1052}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001053
Bram Moolenaare7525c52021-01-09 13:20:37 +01001054/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001055 * Return the character "str[index]" where "index" is the character index,
1056 * including composing characters.
1057 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001058 */
1059 char_u *
1060char_from_string(char_u *str, varnumber_T index)
1061{
1062 size_t nbyte = 0;
1063 varnumber_T nchar = index;
1064 size_t slen;
1065
1066 if (str == NULL)
1067 return NULL;
1068 slen = STRLEN(str);
1069
1070 // do the same as for a list: a negative index counts from the end
1071 if (index < 0)
1072 {
1073 int clen = 0;
1074
1075 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaar0289a092021-03-14 18:40:19 +01001076 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001077 nchar = clen + index;
1078 if (nchar < 0)
1079 // unlike list: index out of range results in empty string
1080 return NULL;
1081 }
1082
1083 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaar0289a092021-03-14 18:40:19 +01001084 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001085 if (nbyte >= slen)
1086 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001087 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001088}
1089
1090/*
1091 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001092 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001093 * If going over the end return "str_len".
1094 * If "idx" is negative count from the end, -1 is the last character.
1095 * When going over the start return -1.
1096 */
1097 static long
1098char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1099{
1100 varnumber_T nchar = idx;
1101 size_t nbyte = 0;
1102
1103 if (nchar >= 0)
1104 {
1105 while (nchar > 0 && nbyte < str_len)
1106 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001107 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001108 --nchar;
1109 }
1110 }
1111 else
1112 {
1113 nbyte = str_len;
1114 while (nchar < 0 && nbyte > 0)
1115 {
1116 --nbyte;
1117 nbyte -= mb_head_off(str, str + nbyte);
1118 ++nchar;
1119 }
1120 if (nchar < 0)
1121 return -1;
1122 }
1123 return (long)nbyte;
1124}
1125
1126/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001127 * Return the slice "str[first : last]" using character indexes. Composing
1128 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001129 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001130 * Return NULL when the result is empty.
1131 */
1132 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001133string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001134{
1135 long start_byte, end_byte;
1136 size_t slen;
1137
1138 if (str == NULL)
1139 return NULL;
1140 slen = STRLEN(str);
1141 start_byte = char_idx2byte(str, slen, first);
1142 if (start_byte < 0)
1143 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001144 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001145 end_byte = (long)slen;
1146 else
1147 {
1148 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001149 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001150 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001151 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001152 }
1153
1154 if (start_byte >= (long)slen || end_byte <= start_byte)
1155 return NULL;
1156 return vim_strnsave(str + start_byte, end_byte - start_byte);
1157}
1158
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001159 static svar_T *
1160get_script_svar(scriptref_T *sref, ectx_T *ectx)
1161{
1162 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1163 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1164 + ectx->ec_dfunc_idx;
1165 svar_T *sv;
1166
1167 if (sref->sref_seq != si->sn_script_seq)
1168 {
1169 // The script was reloaded after the function was
1170 // compiled, the script_idx may not be valid.
1171 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1172 dfunc->df_ufunc->uf_name_exp);
1173 return NULL;
1174 }
1175 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1176 if (!equal_type(sv->sv_type, sref->sref_type))
1177 {
1178 emsg(_(e_script_variable_type_changed));
1179 return NULL;
1180 }
1181 return sv;
1182}
1183
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001184/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 * Execute a function by "name".
1186 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001187 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 */
1189 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001190call_eval_func(
1191 char_u *name,
1192 int argcount,
1193 funclocal_T *funclocal,
1194 ectx_T *ectx,
1195 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196{
Bram Moolenaared677f52020-08-12 16:38:10 +02001197 int called_emsg_before = called_emsg;
1198 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001200 res = call_by_name(name, argcount, funclocal, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001201 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001203 dictitem_T *v;
1204
1205 v = find_var(name, NULL, FALSE);
1206 if (v == NULL)
1207 {
1208 semsg(_(e_unknownfunc), name);
1209 return FAIL;
1210 }
1211 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1212 {
1213 semsg(_(e_unknownfunc), name);
1214 return FAIL;
1215 }
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001216 return call_partial(&v->di_tv, argcount, funclocal, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001218 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219}
1220
1221/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001222 * When a function reference is used, fill a partial with the information
1223 * needed, especially when it is used as a closure.
1224 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001225 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001226fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1227{
1228 pt->pt_func = ufunc;
1229 pt->pt_refcount = 1;
1230
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001231 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001232 {
1233 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1234 + ectx->ec_dfunc_idx;
1235
1236 // The closure needs to find arguments and local
1237 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001238 pt->pt_outer.out_stack = &ectx->ec_stack;
1239 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1240 pt->pt_outer.out_up = ectx->ec_outer;
1241 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001242
1243 // If this function returns and the closure is still
1244 // being used, we need to make a copy of the context
1245 // (arguments and local variables). Store a reference
1246 // to the partial so we can handle that.
1247 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1248 {
1249 vim_free(pt);
1250 return FAIL;
1251 }
1252 // Extra variable keeps the count of closures created
1253 // in the current function call.
1254 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1255 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1256
1257 ((partial_T **)ectx->ec_funcrefs.ga_data)
1258 [ectx->ec_funcrefs.ga_len] = pt;
1259 ++pt->pt_refcount;
1260 ++ectx->ec_funcrefs.ga_len;
1261 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001262 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001263 return OK;
1264}
1265
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001266
Bram Moolenaarf112f302020-12-20 17:47:52 +01001267/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 * Call a "def" function from old Vim script.
1269 * Return OK or FAIL.
1270 */
1271 int
1272call_def_function(
1273 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001274 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001276 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 typval_T *rettv) // return value
1278{
1279 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001280 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001281 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 typval_T *tv;
1283 int idx;
1284 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001285 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001286 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001287 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001288 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001289 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001290 msglist_T **saved_msg_list = NULL;
1291 msglist_T *private_msg_list = NULL;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001292 funclocal_T funclocal;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001293 int save_emsg_silent_def = emsg_silent_def;
1294 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001295 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001296 int orig_funcdepth;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001297 where_T where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298
1299// Get pointer to item in the stack.
1300#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1301
1302// Get pointer to item at the bottom of the stack, -1 is the bottom.
1303#undef STACK_TV_BOT
1304#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1305
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001306// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001307#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 +01001308
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001309 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001310 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1311 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001312 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001313 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001314 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001315 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001316 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001317 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001318 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001319
Bram Moolenaar09689a02020-05-09 22:50:08 +02001320 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001321 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001322 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1323 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001324 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001325 {
1326 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001327 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001328 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001329 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001331 // If depth of calling is getting too high, don't execute the function.
1332 orig_funcdepth = funcdepth_get();
1333 if (funcdepth_increment() == FAIL)
1334 return FAIL;
1335
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001336 CLEAR_FIELD(funclocal);
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001337 CLEAR_FIELD(ectx);
1338 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1339 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1340 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001341 {
1342 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001343 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001344 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001346 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001348 // Put arguments on the stack, but no more than what the function expects.
1349 // A lambda can be called with more arguments than it uses.
1350 for (idx = 0; idx < argc
1351 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1352 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001353 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001354 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001355 && check_typval_arg_type(ufunc->uf_arg_types[idx], &argv[idx],
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001356 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001357 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 copy_tv(&argv[idx], STACK_TV_BOT(0));
1359 ++ectx.ec_stack.ga_len;
1360 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001361
1362 // Turn varargs into a list. Empty list if no args.
1363 if (ufunc->uf_va_name != NULL)
1364 {
1365 int vararg_count = argc - ufunc->uf_args.ga_len;
1366
1367 if (vararg_count < 0)
1368 vararg_count = 0;
1369 else
1370 argc -= vararg_count;
1371 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001372 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001373
1374 // Check the type of the list items.
1375 tv = STACK_TV_BOT(-1);
1376 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001377 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001378 && ufunc->uf_va_type->tt_member != &t_any
1379 && tv->vval.v_list != NULL)
1380 {
1381 type_T *expected = ufunc->uf_va_type->tt_member;
1382 listitem_T *li = tv->vval.v_list->lv_first;
1383
1384 for (idx = 0; idx < vararg_count; ++idx)
1385 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001386 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001387 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001388 goto failed_early;
1389 li = li->li_next;
1390 }
1391 }
1392
Bram Moolenaar23e03252020-04-12 22:22:31 +02001393 if (defcount > 0)
1394 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001395 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001396 --ectx.ec_stack.ga_len;
1397 }
1398
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001399 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001400 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001401 if (defcount > 0)
1402 for (idx = 0; idx < defcount; ++idx)
1403 {
1404 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1405 ++ectx.ec_stack.ga_len;
1406 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001407 if (ufunc->uf_va_name != NULL)
Bram Moolenaarc970e422021-03-17 15:03:04 +01001408 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409
1410 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001411 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1412 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001414 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001415 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1416 + ufunc->uf_dfunc_idx;
1417 ufunc_T *base_ufunc = dfunc->df_ufunc;
1418
1419 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1420 // by copy_func().
1421 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001422 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001423 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1424 if (ectx.ec_outer == NULL)
1425 goto failed_early;
1426 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001427 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001428 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1429 {
1430 if (current_ectx->ec_outer != NULL)
1431 *ectx.ec_outer = *current_ectx->ec_outer;
1432 }
1433 else
1434 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001435 }
1436 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001437 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1438 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001439 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001440 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 // dummy frame entries
1443 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1444 {
1445 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1446 ++ectx.ec_stack.ga_len;
1447 }
1448
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001449 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001450 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001451 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1452 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001454 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001455 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001456 ectx.ec_stack.ga_len += dfunc->df_varcount;
1457 if (dfunc->df_has_closure)
1458 {
1459 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1460 STACK_TV_VAR(idx)->vval.v_number = 0;
1461 ++ectx.ec_stack.ga_len;
1462 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001463
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001464 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001465 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001466
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001467 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001468 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001469 estack_push_ufunc(ufunc, 1);
1470 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001471 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1472
Bram Moolenaar352134b2020-10-17 22:04:08 +02001473 // Use a specific location for storing error messages to be converted to an
1474 // exception.
1475 saved_msg_list = msg_list;
1476 msg_list = &private_msg_list;
1477
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001478 // Do turn errors into exceptions.
1479 suppress_errthrow = FALSE;
1480
Bram Moolenaarc970e422021-03-17 15:03:04 +01001481 // Do not delete the function while executing it.
1482 ++ufunc->uf_calls;
1483
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001484 // When ":silent!" was used before calling then we still abort the
1485 // function. If ":silent!" is used in the function then we don't.
1486 emsg_silent_def = emsg_silent;
1487 did_emsg_def = 0;
1488
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001489 where.wt_index = 0;
1490 where.wt_variable = FALSE;
1491
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001492 // Decide where to start execution, handles optional arguments.
1493 init_instr_idx(ufunc, argc, &ectx);
1494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495 for (;;)
1496 {
1497 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001498
Bram Moolenaar270d0382020-05-15 21:42:53 +02001499 if (++breakcheck_count >= 100)
1500 {
1501 line_breakcheck();
1502 breakcheck_count = 0;
1503 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001504 if (got_int)
1505 {
1506 // Turn CTRL-C into an exception.
1507 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001508 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001509 goto failed;
1510 did_throw = TRUE;
1511 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512
Bram Moolenaara26b9702020-04-18 19:53:28 +02001513 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1514 {
1515 // Turn an error message into an exception.
1516 did_emsg = FALSE;
1517 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1518 goto failed;
1519 did_throw = TRUE;
1520 *msg_list = NULL;
1521 }
1522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523 if (did_throw && !ectx.ec_in_catch)
1524 {
1525 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001526 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527
1528 // An exception jumps to the first catch, finally, or returns from
1529 // the current function.
1530 if (trystack->ga_len > 0)
1531 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001532 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 {
1534 // jump to ":catch" or ":finally"
1535 ectx.ec_in_catch = TRUE;
1536 ectx.ec_iidx = trycmd->tcd_catch_idx;
1537 }
1538 else
1539 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001540 // Not inside try or need to return from current functions.
1541 // Push a dummy return value.
1542 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1543 goto failed;
1544 tv = STACK_TV_BOT(0);
1545 tv->v_type = VAR_NUMBER;
1546 tv->vval.v_number = 0;
1547 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001548 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001550 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001551 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001552 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1553 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 goto done;
1555 }
1556
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001557 if (func_return(&funclocal, &ectx) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001558 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 }
1560 continue;
1561 }
1562
1563 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1564 switch (iptr->isn_type)
1565 {
1566 // execute Ex command line
1567 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001568 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001569 source_cookie_T cookie;
1570
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001571 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001572 // Pass getsourceline to get an error for a missing ":end"
1573 // command.
1574 CLEAR_FIELD(cookie);
1575 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1576 if (do_cmdline(iptr->isn_arg.string,
1577 getsourceline, &cookie,
1578 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1579 == FAIL
1580 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001581 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001582 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583 break;
1584
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001585 // execute Ex command from pieces on the stack
1586 case ISN_EXECCONCAT:
1587 {
1588 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001589 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001590 int pass;
1591 int i;
1592 char_u *cmd = NULL;
1593 char_u *str;
1594
1595 for (pass = 1; pass <= 2; ++pass)
1596 {
1597 for (i = 0; i < count; ++i)
1598 {
1599 tv = STACK_TV_BOT(i - count);
1600 str = tv->vval.v_string;
1601 if (str != NULL && *str != NUL)
1602 {
1603 if (pass == 2)
1604 STRCPY(cmd + len, str);
1605 len += STRLEN(str);
1606 }
1607 if (pass == 2)
1608 clear_tv(tv);
1609 }
1610 if (pass == 1)
1611 {
1612 cmd = alloc(len + 1);
1613 if (cmd == NULL)
1614 goto failed;
1615 len = 0;
1616 }
1617 }
1618
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001619 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001620 do_cmdline_cmd(cmd);
1621 vim_free(cmd);
1622 }
1623 break;
1624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 // execute :echo {string} ...
1626 case ISN_ECHO:
1627 {
1628 int count = iptr->isn_arg.echo.echo_count;
1629 int atstart = TRUE;
1630 int needclr = TRUE;
1631
1632 for (idx = 0; idx < count; ++idx)
1633 {
1634 tv = STACK_TV_BOT(idx - count);
1635 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1636 &atstart, &needclr);
1637 clear_tv(tv);
1638 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001639 if (needclr)
1640 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 ectx.ec_stack.ga_len -= count;
1642 }
1643 break;
1644
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001645 // :execute {string} ...
1646 // :echomsg {string} ...
1647 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001648 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001649 case ISN_ECHOMSG:
1650 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001651 {
1652 int count = iptr->isn_arg.number;
1653 garray_T ga;
1654 char_u buf[NUMBUFLEN];
1655 char_u *p;
1656 int len;
1657 int failed = FALSE;
1658
1659 ga_init2(&ga, 1, 80);
1660 for (idx = 0; idx < count; ++idx)
1661 {
1662 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001663 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001664 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001665 if (tv->v_type == VAR_CHANNEL
1666 || tv->v_type == VAR_JOB)
1667 {
1668 SOURCING_LNUM = iptr->isn_lnum;
1669 emsg(_(e_inval_string));
1670 break;
1671 }
1672 else
1673 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001674 }
1675 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001676 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001677
1678 len = (int)STRLEN(p);
1679 if (ga_grow(&ga, len + 2) == FAIL)
1680 failed = TRUE;
1681 else
1682 {
1683 if (ga.ga_len > 0)
1684 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1685 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1686 ga.ga_len += len;
1687 }
1688 clear_tv(tv);
1689 }
1690 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001691 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001692 {
1693 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001694 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001695 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001696
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001697 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001698 {
1699 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001700 {
1701 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001702 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001703 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001704 {
1705 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001706 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001707 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001708 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001709 else
1710 {
1711 msg_sb_eol();
1712 if (iptr->isn_type == ISN_ECHOMSG)
1713 {
1714 msg_attr(ga.ga_data, echo_attr);
1715 out_flush();
1716 }
1717 else
1718 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001719 SOURCING_LNUM = iptr->isn_lnum;
1720 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001721 }
1722 }
1723 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001724 ga_clear(&ga);
1725 }
1726 break;
1727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 // load local variable or argument
1729 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001730 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 goto failed;
1732 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1733 ++ectx.ec_stack.ga_len;
1734 break;
1735
1736 // load v: variable
1737 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001738 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 goto failed;
1740 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1741 ++ectx.ec_stack.ga_len;
1742 break;
1743
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001744 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 case ISN_LOADSCRIPT:
1746 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001747 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 svar_T *sv;
1749
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001750 sv = get_script_svar(sref, &ectx);
1751 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001752 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001753 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001754 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755 goto failed;
1756 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1757 ++ectx.ec_stack.ga_len;
1758 }
1759 break;
1760
1761 // load s: variable in old script
1762 case ISN_LOADS:
1763 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001764 hashtab_T *ht = &SCRIPT_VARS(
1765 iptr->isn_arg.loadstore.ls_sid);
1766 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 if (di == NULL)
1770 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001771 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001772 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001773 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 }
1775 else
1776 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001777 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 goto failed;
1779 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1780 ++ectx.ec_stack.ga_len;
1781 }
1782 }
1783 break;
1784
Bram Moolenaard3aac292020-04-19 14:32:17 +02001785 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001787 case ISN_LOADB:
1788 case ISN_LOADW:
1789 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001791 dictitem_T *di = NULL;
1792 hashtab_T *ht = NULL;
1793 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001794
Bram Moolenaard3aac292020-04-19 14:32:17 +02001795 switch (iptr->isn_type)
1796 {
1797 case ISN_LOADG:
1798 ht = get_globvar_ht();
1799 namespace = 'g';
1800 break;
1801 case ISN_LOADB:
1802 ht = &curbuf->b_vars->dv_hashtab;
1803 namespace = 'b';
1804 break;
1805 case ISN_LOADW:
1806 ht = &curwin->w_vars->dv_hashtab;
1807 namespace = 'w';
1808 break;
1809 case ISN_LOADT:
1810 ht = &curtab->tp_vars->dv_hashtab;
1811 namespace = 't';
1812 break;
1813 default: // Cannot reach here
1814 goto failed;
1815 }
1816 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001817
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 if (di == NULL)
1819 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001820 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001821 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001822 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001823 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 }
1825 else
1826 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001827 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 goto failed;
1829 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1830 ++ectx.ec_stack.ga_len;
1831 }
1832 }
1833 break;
1834
Bram Moolenaar03290b82020-12-19 16:30:44 +01001835 // load autoload variable
1836 case ISN_LOADAUTO:
1837 {
1838 char_u *name = iptr->isn_arg.string;
1839
1840 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1841 goto failed;
1842 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001843 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001844 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001845 goto on_error;
1846 ++ectx.ec_stack.ga_len;
1847 }
1848 break;
1849
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001850 // load g:/b:/w:/t: namespace
1851 case ISN_LOADGDICT:
1852 case ISN_LOADBDICT:
1853 case ISN_LOADWDICT:
1854 case ISN_LOADTDICT:
1855 {
1856 dict_T *d = NULL;
1857
1858 switch (iptr->isn_type)
1859 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001860 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1861 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1862 case ISN_LOADWDICT: d = curwin->w_vars; break;
1863 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001864 default: // Cannot reach here
1865 goto failed;
1866 }
1867 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1868 goto failed;
1869 tv = STACK_TV_BOT(0);
1870 tv->v_type = VAR_DICT;
1871 tv->v_lock = 0;
1872 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001873 ++d->dv_refcount;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001874 ++ectx.ec_stack.ga_len;
1875 }
1876 break;
1877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878 // load &option
1879 case ISN_LOADOPT:
1880 {
1881 typval_T optval;
1882 char_u *name = iptr->isn_arg.string;
1883
Bram Moolenaara8c17702020-04-01 21:17:24 +02001884 // This is not expected to fail, name is checked during
1885 // compilation: don't set SOURCING_LNUM.
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 Moolenaar9a78e6d2020-07-01 18:29:55 +02001888 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001889 goto failed;
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 $ENV
1896 case ISN_LOADENV:
1897 {
1898 typval_T optval;
1899 char_u *name = iptr->isn_arg.string;
1900
Bram Moolenaar270d0382020-05-15 21:42:53 +02001901 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001903 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001904 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 *STACK_TV_BOT(0) = optval;
1906 ++ectx.ec_stack.ga_len;
1907 }
1908 break;
1909
1910 // load @register
1911 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001912 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001913 goto failed;
1914 tv = STACK_TV_BOT(0);
1915 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001916 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001917 // This may result in NULL, which should be equivalent to an
1918 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919 tv->vval.v_string = get_reg_contents(
1920 iptr->isn_arg.number, GREG_EXPR_SRC);
1921 ++ectx.ec_stack.ga_len;
1922 break;
1923
1924 // store local variable
1925 case ISN_STORE:
1926 --ectx.ec_stack.ga_len;
1927 tv = STACK_TV_VAR(iptr->isn_arg.number);
1928 clear_tv(tv);
1929 *tv = *STACK_TV_BOT(0);
1930 break;
1931
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001932 // store s: variable in old script
1933 case ISN_STORES:
1934 {
1935 hashtab_T *ht = &SCRIPT_VARS(
1936 iptr->isn_arg.loadstore.ls_sid);
1937 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001938 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001939
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001940 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001941 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001942 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001943 else
1944 {
1945 clear_tv(&di->di_tv);
1946 di->di_tv = *STACK_TV_BOT(0);
1947 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001948 }
1949 break;
1950
1951 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 case ISN_STORESCRIPT:
1953 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001954 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1955 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001957 sv = get_script_svar(sref, &ectx);
1958 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001959 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 --ectx.ec_stack.ga_len;
1961 clear_tv(sv->sv_tv);
1962 *sv->sv_tv = *STACK_TV_BOT(0);
1963 }
1964 break;
1965
1966 // store option
1967 case ISN_STOREOPT:
1968 {
1969 long n = 0;
1970 char_u *s = NULL;
1971 char *msg;
1972
1973 --ectx.ec_stack.ga_len;
1974 tv = STACK_TV_BOT(0);
1975 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001976 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001978 if (s == NULL)
1979 s = (char_u *)"";
1980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001982 // must be VAR_NUMBER, CHECKTYPE makes sure
1983 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1985 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001986 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 if (msg != NULL)
1988 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001989 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001991 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 }
1994 break;
1995
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001996 // store $ENV
1997 case ISN_STOREENV:
1998 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001999 tv = STACK_TV_BOT(0);
2000 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2001 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002002 break;
2003
2004 // store @r
2005 case ISN_STOREREG:
2006 {
2007 int reg = iptr->isn_arg.number;
2008
2009 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002010 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002011 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002012 tv_get_string(tv), -1, FALSE);
2013 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002014 }
2015 break;
2016
2017 // store v: variable
2018 case ISN_STOREV:
2019 --ectx.ec_stack.ga_len;
2020 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2021 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002022 // should not happen, type is checked when compiling
2023 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002024 break;
2025
Bram Moolenaard3aac292020-04-19 14:32:17 +02002026 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002028 case ISN_STOREB:
2029 case ISN_STOREW:
2030 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002032 dictitem_T *di;
2033 hashtab_T *ht;
2034 char_u *name = iptr->isn_arg.string + 2;
2035
Bram Moolenaard3aac292020-04-19 14:32:17 +02002036 switch (iptr->isn_type)
2037 {
2038 case ISN_STOREG:
2039 ht = get_globvar_ht();
2040 break;
2041 case ISN_STOREB:
2042 ht = &curbuf->b_vars->dv_hashtab;
2043 break;
2044 case ISN_STOREW:
2045 ht = &curwin->w_vars->dv_hashtab;
2046 break;
2047 case ISN_STORET:
2048 ht = &curtab->tp_vars->dv_hashtab;
2049 break;
2050 default: // Cannot reach here
2051 goto failed;
2052 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053
2054 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002055 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002057 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 else
2059 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002060 SOURCING_LNUM = iptr->isn_lnum;
2061 if (var_check_permission(di, name) == FAIL)
2062 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063 clear_tv(&di->di_tv);
2064 di->di_tv = *STACK_TV_BOT(0);
2065 }
2066 }
2067 break;
2068
Bram Moolenaar03290b82020-12-19 16:30:44 +01002069 // store an autoload variable
2070 case ISN_STOREAUTO:
2071 SOURCING_LNUM = iptr->isn_lnum;
2072 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2073 clear_tv(STACK_TV_BOT(-1));
2074 --ectx.ec_stack.ga_len;
2075 break;
2076
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002077 // store number in local variable
2078 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002079 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080 clear_tv(tv);
2081 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002082 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083 break;
2084
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002085 // store value in list or dict variable
2086 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002087 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002088 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002089 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002090 typval_T *tv_dest = STACK_TV_BOT(-1);
2091 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002092
Bram Moolenaar752fc692021-01-04 21:57:11 +01002093 // Stack contains:
2094 // -3 value to be stored
2095 // -2 index
2096 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002097 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002098 SOURCING_LNUM = iptr->isn_lnum;
2099 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002100 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002101 dest_type = tv_dest->v_type;
2102 if (dest_type == VAR_DICT)
2103 status = do_2string(tv_idx, TRUE);
2104 else if (dest_type == VAR_LIST
2105 && tv_idx->v_type != VAR_NUMBER)
2106 {
2107 emsg(_(e_number_exp));
2108 status = FAIL;
2109 }
2110 }
2111 else if (dest_type != tv_dest->v_type)
2112 {
2113 // just in case, should be OK
2114 semsg(_(e_expected_str_but_got_str),
2115 vartype_name(dest_type),
2116 vartype_name(tv_dest->v_type));
2117 status = FAIL;
2118 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002119
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002120 if (status == OK && dest_type == VAR_LIST)
2121 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002122 long lidx = (long)tv_idx->vval.v_number;
2123 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002124
2125 if (list == NULL)
2126 {
2127 emsg(_(e_list_not_set));
2128 goto on_error;
2129 }
2130 if (lidx < 0 && list->lv_len + lidx >= 0)
2131 // negative index is relative to the end
2132 lidx = list->lv_len + lidx;
2133 if (lidx < 0 || lidx > list->lv_len)
2134 {
2135 semsg(_(e_listidx), lidx);
2136 goto on_error;
2137 }
2138 if (lidx < list->lv_len)
2139 {
2140 listitem_T *li = list_find(list, lidx);
2141
2142 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002143 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002144 goto on_error;
2145 // overwrite existing list item
2146 clear_tv(&li->li_tv);
2147 li->li_tv = *tv;
2148 }
2149 else
2150 {
2151 if (error_if_locked(list->lv_lock,
2152 e_cannot_change_list))
2153 goto on_error;
2154 // append to list, only fails when out of memory
2155 if (list_append_tv(list, tv) == FAIL)
2156 goto failed;
2157 clear_tv(tv);
2158 }
2159 }
2160 else if (status == OK && dest_type == VAR_DICT)
2161 {
2162 char_u *key = tv_idx->vval.v_string;
2163 dict_T *dict = tv_dest->vval.v_dict;
2164 dictitem_T *di;
2165
2166 SOURCING_LNUM = iptr->isn_lnum;
2167 if (dict == NULL)
2168 {
2169 emsg(_(e_dictionary_not_set));
2170 goto on_error;
2171 }
2172 if (key == NULL)
2173 key = (char_u *)"";
2174 di = dict_find(dict, key, -1);
2175 if (di != NULL)
2176 {
2177 if (error_if_locked(di->di_tv.v_lock,
2178 e_cannot_change_dict_item))
2179 goto on_error;
2180 // overwrite existing value
2181 clear_tv(&di->di_tv);
2182 di->di_tv = *tv;
2183 }
2184 else
2185 {
2186 if (error_if_locked(dict->dv_lock,
2187 e_cannot_change_dict))
2188 goto on_error;
2189 // add to dict, only fails when out of memory
2190 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2191 goto failed;
2192 clear_tv(tv);
2193 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002194 }
2195 else
2196 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002197 status = FAIL;
2198 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002199 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002200
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002201 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002202 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002203 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002204 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002205 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002206 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002207 goto on_error;
2208 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002209 }
2210 break;
2211
Bram Moolenaar0186e582021-01-10 18:33:11 +01002212 // load or store variable or argument from outer scope
2213 case ISN_LOADOUTER:
2214 case ISN_STOREOUTER:
2215 {
2216 int depth = iptr->isn_arg.outer.outer_depth;
2217 outer_T *outer = ectx.ec_outer;
2218
2219 while (depth > 1 && outer != NULL)
2220 {
2221 outer = outer->out_up;
2222 --depth;
2223 }
2224 if (outer == NULL)
2225 {
2226 SOURCING_LNUM = iptr->isn_lnum;
2227 iemsg("LOADOUTER depth more than scope levels");
2228 goto failed;
2229 }
2230 tv = ((typval_T *)outer->out_stack->ga_data)
2231 + outer->out_frame_idx + STACK_FRAME_SIZE
2232 + iptr->isn_arg.outer.outer_idx;
2233 if (iptr->isn_type == ISN_LOADOUTER)
2234 {
2235 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2236 goto failed;
2237 copy_tv(tv, STACK_TV_BOT(0));
2238 ++ectx.ec_stack.ga_len;
2239 }
2240 else
2241 {
2242 --ectx.ec_stack.ga_len;
2243 clear_tv(tv);
2244 *tv = *STACK_TV_BOT(0);
2245 }
2246 }
2247 break;
2248
Bram Moolenaar752fc692021-01-04 21:57:11 +01002249 // unlet item in list or dict variable
2250 case ISN_UNLETINDEX:
2251 {
2252 typval_T *tv_idx = STACK_TV_BOT(-2);
2253 typval_T *tv_dest = STACK_TV_BOT(-1);
2254 int status = OK;
2255
2256 // Stack contains:
2257 // -2 index
2258 // -1 dict or list
2259 if (tv_dest->v_type == VAR_DICT)
2260 {
2261 // unlet a dict item, index must be a string
2262 if (tv_idx->v_type != VAR_STRING)
2263 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002264 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002265 semsg(_(e_expected_str_but_got_str),
2266 vartype_name(VAR_STRING),
2267 vartype_name(tv_idx->v_type));
2268 status = FAIL;
2269 }
2270 else
2271 {
2272 dict_T *d = tv_dest->vval.v_dict;
2273 char_u *key = tv_idx->vval.v_string;
2274 dictitem_T *di = NULL;
2275
2276 if (key == NULL)
2277 key = (char_u *)"";
2278 if (d != NULL)
2279 di = dict_find(d, key, (int)STRLEN(key));
2280 if (di == NULL)
2281 {
2282 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002283 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002284 semsg(_(e_dictkey), key);
2285 status = FAIL;
2286 }
2287 else
2288 {
2289 // TODO: check for dict or item locked
2290 dictitem_remove(d, di);
2291 }
2292 }
2293 }
2294 else if (tv_dest->v_type == VAR_LIST)
2295 {
2296 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002297 SOURCING_LNUM = iptr->isn_lnum;
2298 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002299 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002300 status = FAIL;
2301 }
2302 else
2303 {
2304 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002305 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002306 listitem_T *li = NULL;
2307
2308 li = list_find(l, n);
2309 if (li == NULL)
2310 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002311 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002312 semsg(_(e_listidx), n);
2313 status = FAIL;
2314 }
2315 else
2316 // TODO: check for list or item locked
2317 listitem_remove(l, li);
2318 }
2319 }
2320 else
2321 {
2322 status = FAIL;
2323 semsg(_(e_cannot_index_str),
2324 vartype_name(tv_dest->v_type));
2325 }
2326
2327 clear_tv(tv_idx);
2328 clear_tv(tv_dest);
2329 ectx.ec_stack.ga_len -= 2;
2330 if (status == FAIL)
2331 goto on_error;
2332 }
2333 break;
2334
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002335 // unlet range of items in list variable
2336 case ISN_UNLETRANGE:
2337 {
2338 // Stack contains:
2339 // -3 index1
2340 // -2 index2
2341 // -1 dict or list
2342 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2343 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2344 typval_T *tv_dest = STACK_TV_BOT(-1);
2345 int status = OK;
2346
2347 if (tv_dest->v_type == VAR_LIST)
2348 {
2349 // indexes must be a number
2350 SOURCING_LNUM = iptr->isn_lnum;
2351 if (check_for_number(tv_idx1) == FAIL
2352 || check_for_number(tv_idx2) == FAIL)
2353 {
2354 status = FAIL;
2355 }
2356 else
2357 {
2358 list_T *l = tv_dest->vval.v_list;
2359 long n1 = (long)tv_idx1->vval.v_number;
2360 long n2 = (long)tv_idx2->vval.v_number;
2361 listitem_T *li;
2362
2363 li = list_find_index(l, &n1);
2364 if (li == NULL
2365 || list_unlet_range(l, li, NULL, n1,
2366 TRUE, n2) == FAIL)
2367 status = FAIL;
2368 }
2369 }
2370 else
2371 {
2372 status = FAIL;
2373 SOURCING_LNUM = iptr->isn_lnum;
2374 semsg(_(e_cannot_index_str),
2375 vartype_name(tv_dest->v_type));
2376 }
2377
2378 clear_tv(tv_idx1);
2379 clear_tv(tv_idx2);
2380 clear_tv(tv_dest);
2381 ectx.ec_stack.ga_len -= 3;
2382 if (status == FAIL)
2383 goto on_error;
2384 }
2385 break;
2386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387 // push constant
2388 case ISN_PUSHNR:
2389 case ISN_PUSHBOOL:
2390 case ISN_PUSHSPEC:
2391 case ISN_PUSHF:
2392 case ISN_PUSHS:
2393 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002394 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002395 case ISN_PUSHCHANNEL:
2396 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002397 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 goto failed;
2399 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002400 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 ++ectx.ec_stack.ga_len;
2402 switch (iptr->isn_type)
2403 {
2404 case ISN_PUSHNR:
2405 tv->v_type = VAR_NUMBER;
2406 tv->vval.v_number = iptr->isn_arg.number;
2407 break;
2408 case ISN_PUSHBOOL:
2409 tv->v_type = VAR_BOOL;
2410 tv->vval.v_number = iptr->isn_arg.number;
2411 break;
2412 case ISN_PUSHSPEC:
2413 tv->v_type = VAR_SPECIAL;
2414 tv->vval.v_number = iptr->isn_arg.number;
2415 break;
2416#ifdef FEAT_FLOAT
2417 case ISN_PUSHF:
2418 tv->v_type = VAR_FLOAT;
2419 tv->vval.v_float = iptr->isn_arg.fnumber;
2420 break;
2421#endif
2422 case ISN_PUSHBLOB:
2423 blob_copy(iptr->isn_arg.blob, tv);
2424 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002425 case ISN_PUSHFUNC:
2426 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002427 if (iptr->isn_arg.string == NULL)
2428 tv->vval.v_string = NULL;
2429 else
2430 tv->vval.v_string =
2431 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002432 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002433 case ISN_PUSHCHANNEL:
2434#ifdef FEAT_JOB_CHANNEL
2435 tv->v_type = VAR_CHANNEL;
2436 tv->vval.v_channel = iptr->isn_arg.channel;
2437 if (tv->vval.v_channel != NULL)
2438 ++tv->vval.v_channel->ch_refcount;
2439#endif
2440 break;
2441 case ISN_PUSHJOB:
2442#ifdef FEAT_JOB_CHANNEL
2443 tv->v_type = VAR_JOB;
2444 tv->vval.v_job = iptr->isn_arg.job;
2445 if (tv->vval.v_job != NULL)
2446 ++tv->vval.v_job->jv_refcount;
2447#endif
2448 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 default:
2450 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002451 tv->vval.v_string = vim_strsave(
2452 iptr->isn_arg.string == NULL
2453 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 }
2455 break;
2456
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002457 case ISN_UNLET:
2458 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2459 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002460 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002461 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002462 case ISN_UNLETENV:
2463 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2464 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002465
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002466 case ISN_LOCKCONST:
2467 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2468 break;
2469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 // create a list from items on the stack; uses a single allocation
2471 // for the list header and the items
2472 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002473 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2474 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 break;
2476
2477 // create a dict from items on the stack
2478 case ISN_NEWDICT:
2479 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002480 int count = iptr->isn_arg.number;
2481 dict_T *dict = dict_alloc();
2482 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002483 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484
2485 if (dict == NULL)
2486 goto failed;
2487 for (idx = 0; idx < count; ++idx)
2488 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002489 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002491 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002492 key = tv->vval.v_string == NULL
2493 ? (char_u *)"" : tv->vval.v_string;
2494 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002495 if (item != NULL)
2496 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002497 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002498 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002499 dict_unref(dict);
2500 goto on_error;
2501 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002502 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 clear_tv(tv);
2504 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002505 {
2506 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2510 item->di_tv.v_lock = 0;
2511 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002512 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002513 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002514 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 }
2518
2519 if (count > 0)
2520 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002521 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 goto failed;
2523 else
2524 ++ectx.ec_stack.ga_len;
2525 tv = STACK_TV_BOT(-1);
2526 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002527 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 tv->vval.v_dict = dict;
2529 ++dict->dv_refcount;
2530 }
2531 break;
2532
2533 // call a :def function
2534 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002535 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002536 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2537 NULL,
2538 iptr->isn_arg.dfunc.cdf_argcount,
2539 &funclocal,
2540 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002541 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 break;
2543
2544 // call a builtin function
2545 case ISN_BCALL:
2546 SOURCING_LNUM = iptr->isn_lnum;
2547 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2548 iptr->isn_arg.bfunc.cbf_argcount,
2549 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002550 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 break;
2552
2553 // call a funcref or partial
2554 case ISN_PCALL:
2555 {
2556 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2557 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002558 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559
2560 SOURCING_LNUM = iptr->isn_lnum;
2561 if (pfunc->cpf_top)
2562 {
2563 // funcref is above the arguments
2564 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2565 }
2566 else
2567 {
2568 // Get the funcref from the stack.
2569 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002570 partial_tv = *STACK_TV_BOT(0);
2571 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 }
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002573 r = call_partial(tv, pfunc->cpf_argcount,
2574 &funclocal, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002575 if (tv == &partial_tv)
2576 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002578 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 }
2580 break;
2581
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002582 case ISN_PCALL_END:
2583 // PCALL finished, arguments have been consumed and replaced by
2584 // the return value. Now clear the funcref from the stack,
2585 // and move the return value in its place.
2586 --ectx.ec_stack.ga_len;
2587 clear_tv(STACK_TV_BOT(-1));
2588 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2589 break;
2590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 // call a user defined function or funcref/partial
2592 case ISN_UCALL:
2593 {
2594 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2595
2596 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002597 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
2598 &funclocal, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002599 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 }
2601 break;
2602
2603 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002604 case ISN_RETURN_ZERO:
2605 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2606 goto failed;
2607 tv = STACK_TV_BOT(0);
2608 ++ectx.ec_stack.ga_len;
2609 tv->v_type = VAR_NUMBER;
2610 tv->vval.v_number = 0;
2611 tv->v_lock = 0;
2612 // FALLTHROUGH
2613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 case ISN_RETURN:
2615 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002616 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002617 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002618
2619 if (trystack->ga_len > 0)
2620 trycmd = ((trycmd_T *)trystack->ga_data)
2621 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002622 if (trycmd != NULL
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002623 && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002625 // jump to ":finally" or ":endtry"
2626 if (trycmd->tcd_finally_idx != 0)
2627 ectx.ec_iidx = trycmd->tcd_finally_idx;
2628 else
2629 ectx.ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 trycmd->tcd_return = TRUE;
2631 }
2632 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002633 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 }
2635 break;
2636
2637 // push a function reference to a compiled function
2638 case ISN_FUNCREF:
2639 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002640 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2641 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2642 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 if (pt == NULL)
2645 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002646 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002647 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002648 vim_free(pt);
2649 goto failed;
2650 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002651 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2652 &ectx) == FAIL)
2653 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 tv = STACK_TV_BOT(0);
2656 ++ectx.ec_stack.ga_len;
2657 tv->vval.v_partial = pt;
2658 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002659 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 }
2661 break;
2662
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002663 // Create a global function from a lambda.
2664 case ISN_NEWFUNC:
2665 {
2666 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2667
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002668 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002669 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002670 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002671 }
2672 break;
2673
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002674 // List functions
2675 case ISN_DEF:
2676 if (iptr->isn_arg.string == NULL)
2677 list_functions(NULL);
2678 else
2679 {
2680 exarg_T ea;
2681
2682 CLEAR_FIELD(ea);
2683 ea.cmd = ea.arg = iptr->isn_arg.string;
2684 define_function(&ea, NULL);
2685 }
2686 break;
2687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 // jump if a condition is met
2689 case ISN_JUMP:
2690 {
2691 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002692 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 int jump = TRUE;
2694
2695 if (when != JUMP_ALWAYS)
2696 {
2697 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002698 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002699 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002700 || when == JUMP_IF_COND_TRUE)
2701 {
2702 SOURCING_LNUM = iptr->isn_lnum;
2703 jump = tv_get_bool_chk(tv, &error);
2704 if (error)
2705 goto on_error;
2706 }
2707 else
2708 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002710 || when == JUMP_AND_KEEP_IF_FALSE
2711 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002713 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714 {
2715 // drop the value from the stack
2716 clear_tv(tv);
2717 --ectx.ec_stack.ga_len;
2718 }
2719 }
2720 if (jump)
2721 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2722 }
2723 break;
2724
2725 // top of a for loop
2726 case ISN_FOR:
2727 {
2728 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2729 typval_T *idxtv =
2730 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2731
2732 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002733 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002735 ++idxtv->vval.v_number;
2736 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002737 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 // past the end of the list, jump to "endfor"
2739 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002740 may_restore_cmdmod(&funclocal);
2741 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 else if (list->lv_first == &range_list_item)
2743 {
2744 // non-materialized range() list
2745 tv = STACK_TV_BOT(0);
2746 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002747 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 tv->vval.v_number = list_find_nr(
2749 list, idxtv->vval.v_number, NULL);
2750 ++ectx.ec_stack.ga_len;
2751 }
2752 else
2753 {
2754 listitem_T *li = list_find(list, idxtv->vval.v_number);
2755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2757 ++ectx.ec_stack.ga_len;
2758 }
2759 }
2760 break;
2761
2762 // start of ":try" block
2763 case ISN_TRY:
2764 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002765 trycmd_T *trycmd = NULL;
2766
Bram Moolenaar270d0382020-05-15 21:42:53 +02002767 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 goto failed;
2769 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2770 + ectx.ec_trystack.ga_len;
2771 ++ectx.ec_trystack.ga_len;
2772 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002773 CLEAR_POINTER(trycmd);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002774 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002775 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002776 trycmd->tcd_catch_idx =
2777 iptr->isn_arg.try.try_ref->try_catch;
2778 trycmd->tcd_finally_idx =
2779 iptr->isn_arg.try.try_ref->try_finally;
2780 trycmd->tcd_endtry_idx =
2781 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 }
2783 break;
2784
2785 case ISN_PUSHEXC:
2786 if (current_exception == NULL)
2787 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002788 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 iemsg("Evaluating catch while current_exception is NULL");
2790 goto failed;
2791 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002792 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 goto failed;
2794 tv = STACK_TV_BOT(0);
2795 ++ectx.ec_stack.ga_len;
2796 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002797 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 tv->vval.v_string = vim_strsave(
2799 (char_u *)current_exception->value);
2800 break;
2801
2802 case ISN_CATCH:
2803 {
2804 garray_T *trystack = &ectx.ec_trystack;
2805
Bram Moolenaara91a7132021-03-25 21:12:15 +01002806 may_restore_cmdmod(&funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 if (trystack->ga_len > 0)
2808 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002809 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 + trystack->ga_len - 1;
2811 trycmd->tcd_caught = TRUE;
2812 }
2813 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002814 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 catch_exception(current_exception);
2816 }
2817 break;
2818
Bram Moolenaarc150c092021-02-13 15:02:46 +01002819 case ISN_TRYCONT:
2820 {
2821 garray_T *trystack = &ectx.ec_trystack;
2822 trycont_T *trycont = &iptr->isn_arg.trycont;
2823 int i;
2824 trycmd_T *trycmd;
2825 int iidx = trycont->tct_where;
2826
2827 if (trystack->ga_len < trycont->tct_levels)
2828 {
2829 siemsg("TRYCONT: expected %d levels, found %d",
2830 trycont->tct_levels, trystack->ga_len);
2831 goto failed;
2832 }
2833 // Make :endtry jump to any outer try block and the last
2834 // :endtry inside the loop to the loop start.
2835 for (i = trycont->tct_levels; i > 0; --i)
2836 {
2837 trycmd = ((trycmd_T *)trystack->ga_data)
2838 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002839 // Add one to tcd_cont to be able to jump to
2840 // instruction with index zero.
2841 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002842 iidx = trycmd->tcd_finally_idx == 0
2843 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002844 }
2845 // jump to :finally or :endtry of current try statement
2846 ectx.ec_iidx = iidx;
2847 }
2848 break;
2849
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002850 case ISN_FINALLY:
2851 {
2852 garray_T *trystack = &ectx.ec_trystack;
2853 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2854 + trystack->ga_len - 1;
2855
2856 // Reset the index to avoid a return statement jumps here
2857 // again.
2858 trycmd->tcd_finally_idx = 0;
2859 break;
2860 }
2861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 // end of ":try" block
2863 case ISN_ENDTRY:
2864 {
2865 garray_T *trystack = &ectx.ec_trystack;
2866
2867 if (trystack->ga_len > 0)
2868 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002869 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002870
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 --trystack->ga_len;
2872 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002873 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 trycmd = ((trycmd_T *)trystack->ga_data)
2875 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002876 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 {
2878 // discard the exception
2879 if (caught_stack == current_exception)
2880 caught_stack = caught_stack->caught;
2881 discard_current_exception();
2882 }
2883
2884 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002885 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002886
2887 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2888 {
2889 --ectx.ec_stack.ga_len;
2890 clear_tv(STACK_TV_BOT(0));
2891 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002892 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002893 // handling :continue: jump to outer try block or
2894 // start of the loop
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002895 ectx.ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 }
2897 }
2898 break;
2899
2900 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002901 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002902 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002903
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002904 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2905 {
2906 // throwing an exception while using "silent!" causes
2907 // the function to abort but not display an error.
2908 tv = STACK_TV_BOT(-1);
2909 clear_tv(tv);
2910 tv->v_type = VAR_NUMBER;
2911 tv->vval.v_number = 0;
2912 goto done;
2913 }
2914 --ectx.ec_stack.ga_len;
2915 tv = STACK_TV_BOT(0);
2916 if (tv->vval.v_string == NULL
2917 || *skipwhite(tv->vval.v_string) == NUL)
2918 {
2919 vim_free(tv->vval.v_string);
2920 SOURCING_LNUM = iptr->isn_lnum;
2921 emsg(_(e_throw_with_empty_string));
2922 goto failed;
2923 }
2924
2925 // Inside a "catch" we need to first discard the caught
2926 // exception.
2927 if (trystack->ga_len > 0)
2928 {
2929 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2930 + trystack->ga_len - 1;
2931 if (trycmd->tcd_caught && current_exception != NULL)
2932 {
2933 // discard the exception
2934 if (caught_stack == current_exception)
2935 caught_stack = caught_stack->caught;
2936 discard_current_exception();
2937 trycmd->tcd_caught = FALSE;
2938 }
2939 }
2940
2941 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2942 == FAIL)
2943 {
2944 vim_free(tv->vval.v_string);
2945 goto failed;
2946 }
2947 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 break;
2950
2951 // compare with special values
2952 case ISN_COMPAREBOOL:
2953 case ISN_COMPARESPECIAL:
2954 {
2955 typval_T *tv1 = STACK_TV_BOT(-2);
2956 typval_T *tv2 = STACK_TV_BOT(-1);
2957 varnumber_T arg1 = tv1->vval.v_number;
2958 varnumber_T arg2 = tv2->vval.v_number;
2959 int res;
2960
2961 switch (iptr->isn_arg.op.op_type)
2962 {
2963 case EXPR_EQUAL: res = arg1 == arg2; break;
2964 case EXPR_NEQUAL: res = arg1 != arg2; break;
2965 default: res = 0; break;
2966 }
2967
2968 --ectx.ec_stack.ga_len;
2969 tv1->v_type = VAR_BOOL;
2970 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2971 }
2972 break;
2973
2974 // Operation with two number arguments
2975 case ISN_OPNR:
2976 case ISN_COMPARENR:
2977 {
2978 typval_T *tv1 = STACK_TV_BOT(-2);
2979 typval_T *tv2 = STACK_TV_BOT(-1);
2980 varnumber_T arg1 = tv1->vval.v_number;
2981 varnumber_T arg2 = tv2->vval.v_number;
2982 varnumber_T res;
2983
2984 switch (iptr->isn_arg.op.op_type)
2985 {
2986 case EXPR_MULT: res = arg1 * arg2; break;
2987 case EXPR_DIV: res = arg1 / arg2; break;
2988 case EXPR_REM: res = arg1 % arg2; break;
2989 case EXPR_SUB: res = arg1 - arg2; break;
2990 case EXPR_ADD: res = arg1 + arg2; break;
2991
2992 case EXPR_EQUAL: res = arg1 == arg2; break;
2993 case EXPR_NEQUAL: res = arg1 != arg2; break;
2994 case EXPR_GREATER: res = arg1 > arg2; break;
2995 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2996 case EXPR_SMALLER: res = arg1 < arg2; break;
2997 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2998 default: res = 0; break;
2999 }
3000
3001 --ectx.ec_stack.ga_len;
3002 if (iptr->isn_type == ISN_COMPARENR)
3003 {
3004 tv1->v_type = VAR_BOOL;
3005 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3006 }
3007 else
3008 tv1->vval.v_number = res;
3009 }
3010 break;
3011
3012 // Computation with two float arguments
3013 case ISN_OPFLOAT:
3014 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003015#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 {
3017 typval_T *tv1 = STACK_TV_BOT(-2);
3018 typval_T *tv2 = STACK_TV_BOT(-1);
3019 float_T arg1 = tv1->vval.v_float;
3020 float_T arg2 = tv2->vval.v_float;
3021 float_T res = 0;
3022 int cmp = FALSE;
3023
3024 switch (iptr->isn_arg.op.op_type)
3025 {
3026 case EXPR_MULT: res = arg1 * arg2; break;
3027 case EXPR_DIV: res = arg1 / arg2; break;
3028 case EXPR_SUB: res = arg1 - arg2; break;
3029 case EXPR_ADD: res = arg1 + arg2; break;
3030
3031 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3032 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3033 case EXPR_GREATER: cmp = arg1 > arg2; break;
3034 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3035 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3036 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3037 default: cmp = 0; break;
3038 }
3039 --ectx.ec_stack.ga_len;
3040 if (iptr->isn_type == ISN_COMPAREFLOAT)
3041 {
3042 tv1->v_type = VAR_BOOL;
3043 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3044 }
3045 else
3046 tv1->vval.v_float = res;
3047 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003048#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 break;
3050
3051 case ISN_COMPARELIST:
3052 {
3053 typval_T *tv1 = STACK_TV_BOT(-2);
3054 typval_T *tv2 = STACK_TV_BOT(-1);
3055 list_T *arg1 = tv1->vval.v_list;
3056 list_T *arg2 = tv2->vval.v_list;
3057 int cmp = FALSE;
3058 int ic = iptr->isn_arg.op.op_ic;
3059
3060 switch (iptr->isn_arg.op.op_type)
3061 {
3062 case EXPR_EQUAL: cmp =
3063 list_equal(arg1, arg2, ic, FALSE); break;
3064 case EXPR_NEQUAL: cmp =
3065 !list_equal(arg1, arg2, ic, FALSE); break;
3066 case EXPR_IS: cmp = arg1 == arg2; break;
3067 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3068 default: cmp = 0; break;
3069 }
3070 --ectx.ec_stack.ga_len;
3071 clear_tv(tv1);
3072 clear_tv(tv2);
3073 tv1->v_type = VAR_BOOL;
3074 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3075 }
3076 break;
3077
3078 case ISN_COMPAREBLOB:
3079 {
3080 typval_T *tv1 = STACK_TV_BOT(-2);
3081 typval_T *tv2 = STACK_TV_BOT(-1);
3082 blob_T *arg1 = tv1->vval.v_blob;
3083 blob_T *arg2 = tv2->vval.v_blob;
3084 int cmp = FALSE;
3085
3086 switch (iptr->isn_arg.op.op_type)
3087 {
3088 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3089 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3090 case EXPR_IS: cmp = arg1 == arg2; break;
3091 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3092 default: cmp = 0; break;
3093 }
3094 --ectx.ec_stack.ga_len;
3095 clear_tv(tv1);
3096 clear_tv(tv2);
3097 tv1->v_type = VAR_BOOL;
3098 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3099 }
3100 break;
3101
3102 // TODO: handle separately
3103 case ISN_COMPARESTRING:
3104 case ISN_COMPAREDICT:
3105 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 case ISN_COMPAREANY:
3107 {
3108 typval_T *tv1 = STACK_TV_BOT(-2);
3109 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003110 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 int ic = iptr->isn_arg.op.op_ic;
3112
Bram Moolenaareb26f432020-09-14 16:50:05 +02003113 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003114 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 --ectx.ec_stack.ga_len;
3117 }
3118 break;
3119
3120 case ISN_ADDLIST:
3121 case ISN_ADDBLOB:
3122 {
3123 typval_T *tv1 = STACK_TV_BOT(-2);
3124 typval_T *tv2 = STACK_TV_BOT(-1);
3125
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003126 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 if (iptr->isn_type == ISN_ADDLIST)
3128 eval_addlist(tv1, tv2);
3129 else
3130 eval_addblob(tv1, tv2);
3131 clear_tv(tv2);
3132 --ectx.ec_stack.ga_len;
3133 }
3134 break;
3135
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003136 case ISN_LISTAPPEND:
3137 {
3138 typval_T *tv1 = STACK_TV_BOT(-2);
3139 typval_T *tv2 = STACK_TV_BOT(-1);
3140 list_T *l = tv1->vval.v_list;
3141
3142 // add an item to a list
3143 if (l == NULL)
3144 {
3145 SOURCING_LNUM = iptr->isn_lnum;
3146 emsg(_(e_cannot_add_to_null_list));
3147 goto on_error;
3148 }
3149 if (list_append_tv(l, tv2) == FAIL)
3150 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003151 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003152 --ectx.ec_stack.ga_len;
3153 }
3154 break;
3155
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003156 case ISN_BLOBAPPEND:
3157 {
3158 typval_T *tv1 = STACK_TV_BOT(-2);
3159 typval_T *tv2 = STACK_TV_BOT(-1);
3160 blob_T *b = tv1->vval.v_blob;
3161 int error = FALSE;
3162 varnumber_T n;
3163
3164 // add a number to a blob
3165 if (b == NULL)
3166 {
3167 SOURCING_LNUM = iptr->isn_lnum;
3168 emsg(_(e_cannot_add_to_null_blob));
3169 goto on_error;
3170 }
3171 n = tv_get_number_chk(tv2, &error);
3172 if (error)
3173 goto on_error;
3174 ga_append(&b->bv_ga, (int)n);
3175 --ectx.ec_stack.ga_len;
3176 }
3177 break;
3178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 // Computation with two arguments of unknown type
3180 case ISN_OPANY:
3181 {
3182 typval_T *tv1 = STACK_TV_BOT(-2);
3183 typval_T *tv2 = STACK_TV_BOT(-1);
3184 varnumber_T n1, n2;
3185#ifdef FEAT_FLOAT
3186 float_T f1 = 0, f2 = 0;
3187#endif
3188 int error = FALSE;
3189
3190 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3191 {
3192 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3193 {
3194 eval_addlist(tv1, tv2);
3195 clear_tv(tv2);
3196 --ectx.ec_stack.ga_len;
3197 break;
3198 }
3199 else if (tv1->v_type == VAR_BLOB
3200 && tv2->v_type == VAR_BLOB)
3201 {
3202 eval_addblob(tv1, tv2);
3203 clear_tv(tv2);
3204 --ectx.ec_stack.ga_len;
3205 break;
3206 }
3207 }
3208#ifdef FEAT_FLOAT
3209 if (tv1->v_type == VAR_FLOAT)
3210 {
3211 f1 = tv1->vval.v_float;
3212 n1 = 0;
3213 }
3214 else
3215#endif
3216 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003217 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 n1 = tv_get_number_chk(tv1, &error);
3219 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003220 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221#ifdef FEAT_FLOAT
3222 if (tv2->v_type == VAR_FLOAT)
3223 f1 = n1;
3224#endif
3225 }
3226#ifdef FEAT_FLOAT
3227 if (tv2->v_type == VAR_FLOAT)
3228 {
3229 f2 = tv2->vval.v_float;
3230 n2 = 0;
3231 }
3232 else
3233#endif
3234 {
3235 n2 = tv_get_number_chk(tv2, &error);
3236 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003237 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238#ifdef FEAT_FLOAT
3239 if (tv1->v_type == VAR_FLOAT)
3240 f2 = n2;
3241#endif
3242 }
3243#ifdef FEAT_FLOAT
3244 // if there is a float on either side the result is a float
3245 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3246 {
3247 switch (iptr->isn_arg.op.op_type)
3248 {
3249 case EXPR_MULT: f1 = f1 * f2; break;
3250 case EXPR_DIV: f1 = f1 / f2; break;
3251 case EXPR_SUB: f1 = f1 - f2; break;
3252 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003253 default: SOURCING_LNUM = iptr->isn_lnum;
3254 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003255 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 }
3257 clear_tv(tv1);
3258 clear_tv(tv2);
3259 tv1->v_type = VAR_FLOAT;
3260 tv1->vval.v_float = f1;
3261 --ectx.ec_stack.ga_len;
3262 }
3263 else
3264#endif
3265 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003266 int failed = FALSE;
3267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 switch (iptr->isn_arg.op.op_type)
3269 {
3270 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003271 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3272 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003273 goto on_error;
3274 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 case EXPR_SUB: n1 = n1 - n2; break;
3276 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003277 default: n1 = num_modulus(n1, n2, &failed);
3278 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003279 goto on_error;
3280 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 }
3282 clear_tv(tv1);
3283 clear_tv(tv2);
3284 tv1->v_type = VAR_NUMBER;
3285 tv1->vval.v_number = n1;
3286 --ectx.ec_stack.ga_len;
3287 }
3288 }
3289 break;
3290
3291 case ISN_CONCAT:
3292 {
3293 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3294 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3295 char_u *res;
3296
3297 res = concat_str(str1, str2);
3298 clear_tv(STACK_TV_BOT(-2));
3299 clear_tv(STACK_TV_BOT(-1));
3300 --ectx.ec_stack.ga_len;
3301 STACK_TV_BOT(-1)->vval.v_string = res;
3302 }
3303 break;
3304
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003305 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003306 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003307 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003308 int is_slice = iptr->isn_type == ISN_STRSLICE;
3309 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003310 char_u *res;
3311
3312 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003313 // string slice: string is at stack-3, first index at
3314 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003315 if (is_slice)
3316 {
3317 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003318 n1 = tv->vval.v_number;
3319 }
3320
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003321 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003322 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003323
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003324 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003325 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003326 if (is_slice)
3327 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003328 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003329 else
3330 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003331 // single character (including composing characters).
3332 // If the index is too big or negative the result is
3333 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003334 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003335 vim_free(tv->vval.v_string);
3336 tv->vval.v_string = res;
3337 }
3338 break;
3339
3340 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003341 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342 {
Bram Moolenaared591872020-08-15 22:14:53 +02003343 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003345 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346
3347 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003348 // list slice: list is at stack-3, indexes at stack-2 and
3349 // stack-1
3350 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 list = tv->vval.v_list;
3352
3353 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003354 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003356
3357 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 {
Bram Moolenaared591872020-08-15 22:14:53 +02003359 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003360 n1 = tv->vval.v_number;
3361 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 }
Bram Moolenaared591872020-08-15 22:14:53 +02003363
3364 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003365 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003366 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003367 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3368 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003369 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 }
3371 break;
3372
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003373 case ISN_ANYINDEX:
3374 case ISN_ANYSLICE:
3375 {
3376 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3377 typval_T *var1, *var2;
3378 int res;
3379
3380 // index: composite is at stack-2, index at stack-1
3381 // slice: composite is at stack-3, indexes at stack-2 and
3382 // stack-1
3383 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003384 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003385 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3386 goto on_error;
3387 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3388 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003389 res = eval_index_inner(tv, is_slice, var1, var2,
3390 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003391 clear_tv(var1);
3392 if (is_slice)
3393 clear_tv(var2);
3394 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3395 if (res == FAIL)
3396 goto on_error;
3397 }
3398 break;
3399
Bram Moolenaar9af78762020-06-16 11:34:42 +02003400 case ISN_SLICE:
3401 {
3402 list_T *list;
3403 int count = iptr->isn_arg.number;
3404
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003405 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003406 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003407 list = tv->vval.v_list;
3408
3409 // no error for short list, expect it to be checked earlier
3410 if (list != NULL && list->lv_len >= count)
3411 {
3412 list_T *newlist = list_slice(list,
3413 count, list->lv_len - 1);
3414
3415 if (newlist != NULL)
3416 {
3417 list_unref(list);
3418 tv->vval.v_list = newlist;
3419 ++newlist->lv_refcount;
3420 }
3421 }
3422 }
3423 break;
3424
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003425 case ISN_GETITEM:
3426 {
3427 listitem_T *li;
3428 int index = iptr->isn_arg.number;
3429
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003430 // Get list item: list is at stack-1, push item.
3431 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003432 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003433 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003434
3435 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3436 goto failed;
3437 ++ectx.ec_stack.ga_len;
3438 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003439
3440 // Useful when used in unpack assignment. Reset at
3441 // ISN_DROP.
3442 where.wt_index = index + 1;
3443 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003444 }
3445 break;
3446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 case ISN_MEMBER:
3448 {
3449 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003450 char_u *key;
3451 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003452 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003453
3454 // dict member: dict is at stack-2, key at stack-1
3455 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003456 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003457 dict = tv->vval.v_dict;
3458
3459 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003460 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003461 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003462 if (key == NULL)
3463 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003464
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003465 if ((di = dict_find(dict, key, -1)) == NULL)
3466 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003467 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003468 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003469
3470 // If :silent! is used we will continue, make sure the
3471 // stack contents makes sense.
3472 clear_tv(tv);
3473 --ectx.ec_stack.ga_len;
3474 tv = STACK_TV_BOT(-1);
3475 clear_tv(tv);
3476 tv->v_type = VAR_NUMBER;
3477 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003478 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003479 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003480 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003481 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003482 // Clear the dict only after getting the item, to avoid
3483 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003484 tv = STACK_TV_BOT(-1);
3485 temp_tv = *tv;
3486 copy_tv(&di->di_tv, tv);
3487 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003488 }
3489 break;
3490
3491 // dict member with string key
3492 case ISN_STRINGMEMBER:
3493 {
3494 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003496 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497
3498 tv = STACK_TV_BOT(-1);
3499 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3500 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003501 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003503 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 }
3505 dict = tv->vval.v_dict;
3506
3507 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3508 == NULL)
3509 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003510 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003512 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003514 // Clear the dict after getting the item, to avoid that it
3515 // make the item invalid.
3516 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003518 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 }
3520 break;
3521
3522 case ISN_NEGATENR:
3523 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003524 if (tv->v_type != VAR_NUMBER
3525#ifdef FEAT_FLOAT
3526 && tv->v_type != VAR_FLOAT
3527#endif
3528 )
3529 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003530 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003531 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003532 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003533 }
3534#ifdef FEAT_FLOAT
3535 if (tv->v_type == VAR_FLOAT)
3536 tv->vval.v_float = -tv->vval.v_float;
3537 else
3538#endif
3539 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 break;
3541
3542 case ISN_CHECKNR:
3543 {
3544 int error = FALSE;
3545
3546 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003547 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003549 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 (void)tv_get_number_chk(tv, &error);
3551 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003552 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 }
3554 break;
3555
3556 case ISN_CHECKTYPE:
3557 {
3558 checktype_T *ct = &iptr->isn_arg.type;
3559
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003560 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003561 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003562 if (!where.wt_variable)
3563 where.wt_index = ct->ct_arg_idx;
3564 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003565 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003566 if (!where.wt_variable)
3567 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003568
3569 // number 0 is FALSE, number 1 is TRUE
3570 if (tv->v_type == VAR_NUMBER
3571 && ct->ct_type->tt_type == VAR_BOOL
3572 && (tv->vval.v_number == 0
3573 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003575 tv->v_type = VAR_BOOL;
3576 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003577 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 }
3579 }
3580 break;
3581
Bram Moolenaar9af78762020-06-16 11:34:42 +02003582 case ISN_CHECKLEN:
3583 {
3584 int min_len = iptr->isn_arg.checklen.cl_min_len;
3585 list_T *list = NULL;
3586
3587 tv = STACK_TV_BOT(-1);
3588 if (tv->v_type == VAR_LIST)
3589 list = tv->vval.v_list;
3590 if (list == NULL || list->lv_len < min_len
3591 || (list->lv_len > min_len
3592 && !iptr->isn_arg.checklen.cl_more_OK))
3593 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003594 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003595 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003596 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003597 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003598 }
3599 }
3600 break;
3601
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003602 case ISN_SETTYPE:
3603 {
3604 checktype_T *ct = &iptr->isn_arg.type;
3605
3606 tv = STACK_TV_BOT(-1);
3607 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3608 {
3609 free_type(tv->vval.v_dict->dv_type);
3610 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3611 }
3612 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3613 {
3614 free_type(tv->vval.v_list->lv_type);
3615 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3616 }
3617 }
3618 break;
3619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003621 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 {
3623 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003624 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625
3626 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003627 if (iptr->isn_type == ISN_2BOOL)
3628 {
3629 n = tv2bool(tv);
3630 if (iptr->isn_arg.number) // invert
3631 n = !n;
3632 }
3633 else
3634 {
3635 SOURCING_LNUM = iptr->isn_lnum;
3636 n = tv_get_bool_chk(tv, &error);
3637 if (error)
3638 goto on_error;
3639 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 clear_tv(tv);
3641 tv->v_type = VAR_BOOL;
3642 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3643 }
3644 break;
3645
3646 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003647 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003648 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003649 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3650 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3651 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 break;
3653
Bram Moolenaar08597872020-12-10 19:43:40 +01003654 case ISN_RANGE:
3655 {
3656 exarg_T ea;
3657 char *errormsg;
3658
Bram Moolenaarece0b872021-01-08 20:40:45 +01003659 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003660 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003661 ea.addr_type = ADDR_LINES;
3662 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003663 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003664 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003665 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003666
3667 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3668 goto failed;
3669 ++ectx.ec_stack.ga_len;
3670 tv = STACK_TV_BOT(-1);
3671 tv->v_type = VAR_NUMBER;
3672 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003673 if (ea.addr_count == 0)
3674 tv->vval.v_number = curwin->w_cursor.lnum;
3675 else
3676 tv->vval.v_number = ea.line2;
3677 }
3678 break;
3679
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003680 case ISN_PUT:
3681 {
3682 int regname = iptr->isn_arg.put.put_regname;
3683 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3684 char_u *expr = NULL;
3685 int dir = FORWARD;
3686
Bram Moolenaar08597872020-12-10 19:43:40 +01003687 if (lnum < -2)
3688 {
3689 // line number was put on the stack by ISN_RANGE
3690 tv = STACK_TV_BOT(-1);
3691 curwin->w_cursor.lnum = tv->vval.v_number;
3692 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3693 dir = BACKWARD;
3694 --ectx.ec_stack.ga_len;
3695 }
3696 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003697 // :put! above cursor
3698 dir = BACKWARD;
3699 else if (lnum >= 0)
3700 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003701
3702 if (regname == '=')
3703 {
3704 tv = STACK_TV_BOT(-1);
3705 if (tv->v_type == VAR_STRING)
3706 expr = tv->vval.v_string;
3707 else
3708 {
3709 expr = typval2string(tv, TRUE); // allocates value
3710 clear_tv(tv);
3711 }
3712 --ectx.ec_stack.ga_len;
3713 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003714 check_cursor();
3715 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3716 vim_free(expr);
3717 }
3718 break;
3719
Bram Moolenaar02194d22020-10-24 23:08:38 +02003720 case ISN_CMDMOD:
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003721 funclocal.floc_save_cmdmod = cmdmod;
3722 funclocal.floc_restore_cmdmod = TRUE;
3723 funclocal.floc_restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003724 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3725 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003726 break;
3727
Bram Moolenaar02194d22020-10-24 23:08:38 +02003728 case ISN_CMDMOD_REV:
3729 // filter regprog is owned by the instruction, don't free it
3730 cmdmod.cmod_filter_regmatch.regprog = NULL;
3731 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003732 cmdmod = funclocal.floc_save_cmdmod;
3733 funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003734 break;
3735
Bram Moolenaar792f7862020-11-23 08:31:18 +01003736 case ISN_UNPACK:
3737 {
3738 int count = iptr->isn_arg.unpack.unp_count;
3739 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3740 list_T *l;
3741 listitem_T *li;
3742 int i;
3743
3744 // Check there is a valid list to unpack.
3745 tv = STACK_TV_BOT(-1);
3746 if (tv->v_type != VAR_LIST)
3747 {
3748 SOURCING_LNUM = iptr->isn_lnum;
3749 emsg(_(e_for_argument_must_be_sequence_of_lists));
3750 goto on_error;
3751 }
3752 l = tv->vval.v_list;
3753 if (l == NULL
3754 || l->lv_len < (semicolon ? count - 1 : count))
3755 {
3756 SOURCING_LNUM = iptr->isn_lnum;
3757 emsg(_(e_list_value_does_not_have_enough_items));
3758 goto on_error;
3759 }
3760 else if (!semicolon && l->lv_len > count)
3761 {
3762 SOURCING_LNUM = iptr->isn_lnum;
3763 emsg(_(e_list_value_has_more_items_than_targets));
3764 goto on_error;
3765 }
3766
3767 CHECK_LIST_MATERIALIZE(l);
3768 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3769 goto failed;
3770 ectx.ec_stack.ga_len += count - 1;
3771
3772 // Variable after semicolon gets a list with the remaining
3773 // items.
3774 if (semicolon)
3775 {
3776 list_T *rem_list =
3777 list_alloc_with_items(l->lv_len - count + 1);
3778
3779 if (rem_list == NULL)
3780 goto failed;
3781 tv = STACK_TV_BOT(-count);
3782 tv->vval.v_list = rem_list;
3783 ++rem_list->lv_refcount;
3784 tv->v_lock = 0;
3785 li = l->lv_first;
3786 for (i = 0; i < count - 1; ++i)
3787 li = li->li_next;
3788 for (i = 0; li != NULL; ++i)
3789 {
3790 list_set_item(rem_list, i, &li->li_tv);
3791 li = li->li_next;
3792 }
3793 --count;
3794 }
3795
3796 // Produce the values in reverse order, first item last.
3797 li = l->lv_first;
3798 for (i = 0; i < count; ++i)
3799 {
3800 tv = STACK_TV_BOT(-i - 1);
3801 copy_tv(&li->li_tv, tv);
3802 li = li->li_next;
3803 }
3804
3805 list_unref(l);
3806 }
3807 break;
3808
Bram Moolenaarb2049902021-01-24 12:53:53 +01003809 case ISN_PROF_START:
3810 case ISN_PROF_END:
3811 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003812#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003813 funccall_T cookie;
3814 ufunc_T *cur_ufunc =
3815 (((dfunc_T *)def_functions.ga_data)
3816 + ectx.ec_dfunc_idx)->df_ufunc;
3817
3818 cookie.func = cur_ufunc;
3819 if (iptr->isn_type == ISN_PROF_START)
3820 {
3821 func_line_start(&cookie, iptr->isn_lnum);
3822 // if we get here the instruction is executed
3823 func_line_exec(&cookie);
3824 }
3825 else
3826 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003827#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003828 }
3829 break;
3830
Bram Moolenaar389df252020-07-09 21:20:47 +02003831 case ISN_SHUFFLE:
3832 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003833 typval_T tmp_tv;
3834 int item = iptr->isn_arg.shuffle.shfl_item;
3835 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003836
3837 tmp_tv = *STACK_TV_BOT(-item);
3838 for ( ; up > 0 && item > 1; --up)
3839 {
3840 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3841 --item;
3842 }
3843 *STACK_TV_BOT(-item) = tmp_tv;
3844 }
3845 break;
3846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 case ISN_DROP:
3848 --ectx.ec_stack.ga_len;
3849 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003850 where.wt_index = 0;
3851 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 break;
3853 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003854 continue;
3855
Bram Moolenaard032f342020-07-18 18:13:02 +02003856func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003857 // Restore previous function. If the frame pointer is where we started
3858 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003859 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003860 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003861
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003862 if (func_return(&funclocal, &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003863 // only fails when out of memory
3864 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003865 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003866
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003867on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003868 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003869 // If "emsg_silent" is set then ignore the error, unless it was set
3870 // when calling the function.
3871 if (did_emsg_cumul + did_emsg == did_emsg_before
3872 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003873 {
3874 // If a sequence of instructions causes an error while ":silent!"
3875 // was used, restore the stack length and jump ahead to restoring
3876 // the cmdmod.
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003877 if (funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003878 {
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003879 while (ectx.ec_stack.ga_len
3880 > funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003881 {
3882 --ectx.ec_stack.ga_len;
3883 clear_tv(STACK_TV_BOT(0));
3884 }
3885 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3886 ++ectx.ec_iidx;
3887 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003888 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003889 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003890on_fatal_error:
3891 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003892 // If we are not inside a try-catch started here, abort execution.
3893 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003894 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 }
3896
3897done:
3898 // function finished, get result from the stack.
3899 tv = STACK_TV_BOT(-1);
3900 *rettv = *tv;
3901 tv->v_type = VAR_UNKNOWN;
3902 ret = OK;
3903
3904failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003905 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003906 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003907 func_return(&funclocal, &ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003908
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003909 // Deal with any remaining closures, they may be in use somewhere.
3910 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003911 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003912 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003913 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3914 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003915
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003916 estack_pop();
3917 current_sctx = save_current_sctx;
3918
Bram Moolenaarc970e422021-03-17 15:03:04 +01003919 // TODO: when is it safe to delete the function if it is no longer used?
3920 --ufunc->uf_calls;
3921
Bram Moolenaar352134b2020-10-17 22:04:08 +02003922 if (*msg_list != NULL && saved_msg_list != NULL)
3923 {
3924 msglist_T **plist = saved_msg_list;
3925
3926 // Append entries from the current msg_list (uncaught exceptions) to
3927 // the saved msg_list.
3928 while (*plist != NULL)
3929 plist = &(*plist)->next;
3930
3931 *plist = *msg_list;
3932 }
3933 msg_list = saved_msg_list;
3934
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003935 if (funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02003936 {
3937 cmdmod.cmod_filter_regmatch.regprog = NULL;
3938 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003939 cmdmod = funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003940 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003941 emsg_silent_def = save_emsg_silent_def;
3942 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003943
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003944failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003945 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3947 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003950 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003951
Bram Moolenaar0186e582021-01-10 18:33:11 +01003952 while (ectx.ec_outer != NULL)
3953 {
3954 outer_T *up = ectx.ec_outer->out_up_is_copy
3955 ? NULL : ectx.ec_outer->out_up;
3956
3957 vim_free(ectx.ec_outer);
3958 ectx.ec_outer = up;
3959 }
3960
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003961 // Not sure if this is necessary.
3962 suppress_errthrow = save_suppress_errthrow;
3963
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003964 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003965 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003966 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003967 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 return ret;
3969}
3970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003972 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003973 * We don't really need this at runtime, but we do have tests that require it,
3974 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 */
3976 void
3977ex_disassemble(exarg_T *eap)
3978{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003979 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003980 char_u *fname;
3981 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 dfunc_T *dfunc;
3983 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003984 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 int current;
3986 int line_idx = 0;
3987 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003988 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003990 if (STRNCMP(arg, "<lambda>", 8) == 0)
3991 {
3992 arg += 8;
3993 (void)getdigits(&arg);
3994 fname = vim_strnsave(eap->arg, arg - eap->arg);
3995 }
3996 else
3997 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003998 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003999 if (fname == NULL)
4000 {
4001 semsg(_(e_invarg2), eap->arg);
4002 return;
4003 }
4004
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004005 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004006 if (ufunc == NULL)
4007 {
4008 char_u *p = untrans_function_name(fname);
4009
4010 if (p != NULL)
4011 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004012 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004013 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01004014 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 if (ufunc == NULL)
4016 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004017 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 return;
4019 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01004020 if (func_needs_compiling(ufunc, eap->forceit)
4021 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02004022 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004023 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004025 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 return;
4027 }
4028 if (ufunc->uf_name_exp != NULL)
4029 msg((char *)ufunc->uf_name_exp);
4030 else
4031 msg((char *)ufunc->uf_name);
4032
4033 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01004034#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004035 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
4036 instr_count = eap->forceit ? dfunc->df_instr_prof_count
4037 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01004038#else
4039 instr = dfunc->df_instr;
4040 instr_count = dfunc->df_instr_count;
4041#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004042 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 {
4044 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004045 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046
4047 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
4048 {
4049 if (current > prev_current)
4050 {
4051 msg_puts("\n\n");
4052 prev_current = current;
4053 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004054 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4055 if (line != NULL)
4056 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 }
4058
4059 switch (iptr->isn_type)
4060 {
4061 case ISN_EXEC:
4062 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
4063 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004064 case ISN_EXECCONCAT:
4065 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004066 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004067 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 case ISN_ECHO:
4069 {
4070 echo_T *echo = &iptr->isn_arg.echo;
4071
4072 smsg("%4d %s %d", current,
4073 echo->echo_with_white ? "ECHO" : "ECHON",
4074 echo->echo_count);
4075 }
4076 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01004077 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01004078 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004079 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01004080 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004081 case ISN_ECHOMSG:
4082 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004083 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004084 break;
4085 case ISN_ECHOERR:
4086 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004087 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004088 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004090 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004091 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004092 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004093 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004094 + STACK_FRAME_SIZE));
4095 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004096 smsg("%4d LOAD $%lld", current,
4097 (varnumber_T)(iptr->isn_arg.number));
4098 }
4099 break;
4100 case ISN_LOADOUTER:
4101 {
4102 if (iptr->isn_arg.number < 0)
4103 smsg("%4d LOADOUTER level %d arg[%d]", current,
4104 iptr->isn_arg.outer.outer_depth,
4105 iptr->isn_arg.outer.outer_idx
4106 + STACK_FRAME_SIZE);
4107 else
4108 smsg("%4d LOADOUTER level %d $%d", current,
4109 iptr->isn_arg.outer.outer_depth,
4110 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004111 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 break;
4113 case ISN_LOADV:
4114 smsg("%4d LOADV v:%s", current,
4115 get_vim_var_name(iptr->isn_arg.number));
4116 break;
4117 case ISN_LOADSCRIPT:
4118 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004119 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4120 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004122 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004124 smsg("%4d LOADSCRIPT %s-%d from %s", current,
4125 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004126 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004127 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 }
4129 break;
4130 case ISN_LOADS:
4131 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004132 scriptitem_T *si = SCRIPT_ITEM(
4133 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134
4135 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004136 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 }
4138 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004139 case ISN_LOADAUTO:
4140 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
4141 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 case ISN_LOADG:
4143 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
4144 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004145 case ISN_LOADB:
4146 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
4147 break;
4148 case ISN_LOADW:
4149 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
4150 break;
4151 case ISN_LOADT:
4152 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
4153 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004154 case ISN_LOADGDICT:
4155 smsg("%4d LOAD g:", current);
4156 break;
4157 case ISN_LOADBDICT:
4158 smsg("%4d LOAD b:", current);
4159 break;
4160 case ISN_LOADWDICT:
4161 smsg("%4d LOAD w:", current);
4162 break;
4163 case ISN_LOADTDICT:
4164 smsg("%4d LOAD t:", current);
4165 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 case ISN_LOADOPT:
4167 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
4168 break;
4169 case ISN_LOADENV:
4170 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
4171 break;
4172 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004173 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 break;
4175
4176 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01004177 if (iptr->isn_arg.number < 0)
4178 smsg("%4d STORE arg[%lld]", current,
4179 iptr->isn_arg.number + STACK_FRAME_SIZE);
4180 else
4181 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
4182 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004183 case ISN_STOREOUTER:
4184 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004185 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004186 smsg("%4d STOREOUTEr level %d arg[%d]", current,
4187 iptr->isn_arg.outer.outer_depth,
4188 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004189 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004190 smsg("%4d STOREOUTER level %d $%d", current,
4191 iptr->isn_arg.outer.outer_depth,
4192 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004193 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004195 case ISN_STOREV:
4196 smsg("%4d STOREV v:%s", current,
4197 get_vim_var_name(iptr->isn_arg.number));
4198 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004199 case ISN_STOREAUTO:
4200 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
4201 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004203 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
4204 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004205 case ISN_STOREB:
4206 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
4207 break;
4208 case ISN_STOREW:
4209 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
4210 break;
4211 case ISN_STORET:
4212 smsg("%4d STORET %s", current, iptr->isn_arg.string);
4213 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004214 case ISN_STORES:
4215 {
4216 scriptitem_T *si = SCRIPT_ITEM(
4217 iptr->isn_arg.loadstore.ls_sid);
4218
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004219 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004220 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 break;
4223 case ISN_STORESCRIPT:
4224 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004225 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4226 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004228 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004230 smsg("%4d STORESCRIPT %s-%d in %s", current,
4231 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004232 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004233 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 }
4235 break;
4236 case ISN_STOREOPT:
4237 smsg("%4d STOREOPT &%s", current,
4238 iptr->isn_arg.storeopt.so_name);
4239 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004240 case ISN_STOREENV:
4241 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4242 break;
4243 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004244 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004245 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 case ISN_STORENR:
4247 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004248 iptr->isn_arg.storenr.stnr_val,
4249 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 break;
4251
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004252 case ISN_STOREINDEX:
4253 switch (iptr->isn_arg.vartype)
4254 {
4255 case VAR_LIST:
4256 smsg("%4d STORELIST", current);
4257 break;
4258 case VAR_DICT:
4259 smsg("%4d STOREDICT", current);
4260 break;
4261 case VAR_ANY:
4262 smsg("%4d STOREINDEX", current);
4263 break;
4264 default: break;
4265 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004266 break;
4267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 // constants
4269 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004270 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004271 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 break;
4273 case ISN_PUSHBOOL:
4274 case ISN_PUSHSPEC:
4275 smsg("%4d PUSH %s", current,
4276 get_var_special_name(iptr->isn_arg.number));
4277 break;
4278 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004279#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004281#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 break;
4283 case ISN_PUSHS:
4284 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4285 break;
4286 case ISN_PUSHBLOB:
4287 {
4288 char_u *r;
4289 char_u numbuf[NUMBUFLEN];
4290 char_u *tofree;
4291
4292 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004293 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 vim_free(tofree);
4295 }
4296 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004297 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004298 {
4299 char *name = (char *)iptr->isn_arg.string;
4300
4301 smsg("%4d PUSHFUNC \"%s\"", current,
4302 name == NULL ? "[none]" : name);
4303 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004304 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004305 case ISN_PUSHCHANNEL:
4306#ifdef FEAT_JOB_CHANNEL
4307 {
4308 channel_T *channel = iptr->isn_arg.channel;
4309
4310 smsg("%4d PUSHCHANNEL %d", current,
4311 channel == NULL ? 0 : channel->ch_id);
4312 }
4313#endif
4314 break;
4315 case ISN_PUSHJOB:
4316#ifdef FEAT_JOB_CHANNEL
4317 {
4318 typval_T tv;
4319 char_u *name;
4320
4321 tv.v_type = VAR_JOB;
4322 tv.vval.v_job = iptr->isn_arg.job;
4323 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004324 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004325 }
4326#endif
4327 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 case ISN_PUSHEXC:
4329 smsg("%4d PUSH v:exception", current);
4330 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004331 case ISN_UNLET:
4332 smsg("%4d UNLET%s %s", current,
4333 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4334 iptr->isn_arg.unlet.ul_name);
4335 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004336 case ISN_UNLETENV:
4337 smsg("%4d UNLETENV%s $%s", current,
4338 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4339 iptr->isn_arg.unlet.ul_name);
4340 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004341 case ISN_UNLETINDEX:
4342 smsg("%4d UNLETINDEX", current);
4343 break;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004344 case ISN_UNLETRANGE:
4345 smsg("%4d UNLETRANGE", current);
4346 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004347 case ISN_LOCKCONST:
4348 smsg("%4d LOCKCONST", current);
4349 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004351 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004352 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 break;
4354 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004355 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004356 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 break;
4358
4359 // function call
4360 case ISN_BCALL:
4361 {
4362 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4363
4364 smsg("%4d BCALL %s(argc %d)", current,
4365 internal_func_name(cbfunc->cbf_idx),
4366 cbfunc->cbf_argcount);
4367 }
4368 break;
4369 case ISN_DCALL:
4370 {
4371 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4372 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4373 + cdfunc->cdf_idx;
4374
4375 smsg("%4d DCALL %s(argc %d)", current,
4376 df->df_ufunc->uf_name_exp != NULL
4377 ? df->df_ufunc->uf_name_exp
4378 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4379 }
4380 break;
4381 case ISN_UCALL:
4382 {
4383 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4384
4385 smsg("%4d UCALL %s(argc %d)", current,
4386 cufunc->cuf_name, cufunc->cuf_argcount);
4387 }
4388 break;
4389 case ISN_PCALL:
4390 {
4391 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4392
4393 smsg("%4d PCALL%s (argc %d)", current,
4394 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4395 }
4396 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004397 case ISN_PCALL_END:
4398 smsg("%4d PCALL end", current);
4399 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 case ISN_RETURN:
4401 smsg("%4d RETURN", current);
4402 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004403 case ISN_RETURN_ZERO:
4404 smsg("%4d RETURN 0", current);
4405 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 case ISN_FUNCREF:
4407 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004408 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004410 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004412 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 }
4414 break;
4415
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004416 case ISN_NEWFUNC:
4417 {
4418 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4419
4420 smsg("%4d NEWFUNC %s %s", current,
4421 newfunc->nf_lambda, newfunc->nf_global);
4422 }
4423 break;
4424
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004425 case ISN_DEF:
4426 {
4427 char_u *name = iptr->isn_arg.string;
4428
4429 smsg("%4d DEF %s", current,
4430 name == NULL ? (char_u *)"" : name);
4431 }
4432 break;
4433
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 case ISN_JUMP:
4435 {
4436 char *when = "?";
4437
4438 switch (iptr->isn_arg.jump.jump_when)
4439 {
4440 case JUMP_ALWAYS:
4441 when = "JUMP";
4442 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 case JUMP_AND_KEEP_IF_TRUE:
4444 when = "JUMP_AND_KEEP_IF_TRUE";
4445 break;
4446 case JUMP_IF_FALSE:
4447 when = "JUMP_IF_FALSE";
4448 break;
4449 case JUMP_AND_KEEP_IF_FALSE:
4450 when = "JUMP_AND_KEEP_IF_FALSE";
4451 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004452 case JUMP_IF_COND_FALSE:
4453 when = "JUMP_IF_COND_FALSE";
4454 break;
4455 case JUMP_IF_COND_TRUE:
4456 when = "JUMP_IF_COND_TRUE";
4457 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004459 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 iptr->isn_arg.jump.jump_where);
4461 }
4462 break;
4463
4464 case ISN_FOR:
4465 {
4466 forloop_T *forloop = &iptr->isn_arg.forloop;
4467
4468 smsg("%4d FOR $%d -> %d", current,
4469 forloop->for_idx, forloop->for_end);
4470 }
4471 break;
4472
4473 case ISN_TRY:
4474 {
4475 try_T *try = &iptr->isn_arg.try;
4476
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004477 if (try->try_ref->try_finally == 0)
4478 smsg("%4d TRY catch -> %d, endtry -> %d",
4479 current,
4480 try->try_ref->try_catch,
4481 try->try_ref->try_endtry);
4482 else
4483 smsg("%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4484 current,
4485 try->try_ref->try_catch,
4486 try->try_ref->try_finally,
4487 try->try_ref->try_endtry);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 }
4489 break;
4490 case ISN_CATCH:
4491 // TODO
4492 smsg("%4d CATCH", current);
4493 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004494 case ISN_TRYCONT:
4495 {
4496 trycont_T *trycont = &iptr->isn_arg.trycont;
4497
4498 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4499 trycont->tct_levels,
4500 trycont->tct_levels == 1 ? "" : "s",
4501 trycont->tct_where);
4502 }
4503 break;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004504 case ISN_FINALLY:
4505 smsg("%4d FINALLY", current);
4506 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 case ISN_ENDTRY:
4508 smsg("%4d ENDTRY", current);
4509 break;
4510 case ISN_THROW:
4511 smsg("%4d THROW", current);
4512 break;
4513
4514 // expression operations on number
4515 case ISN_OPNR:
4516 case ISN_OPFLOAT:
4517 case ISN_OPANY:
4518 {
4519 char *what;
4520 char *ins;
4521
4522 switch (iptr->isn_arg.op.op_type)
4523 {
4524 case EXPR_MULT: what = "*"; break;
4525 case EXPR_DIV: what = "/"; break;
4526 case EXPR_REM: what = "%"; break;
4527 case EXPR_SUB: what = "-"; break;
4528 case EXPR_ADD: what = "+"; break;
4529 default: what = "???"; break;
4530 }
4531 switch (iptr->isn_type)
4532 {
4533 case ISN_OPNR: ins = "OPNR"; break;
4534 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4535 case ISN_OPANY: ins = "OPANY"; break;
4536 default: ins = "???"; break;
4537 }
4538 smsg("%4d %s %s", current, ins, what);
4539 }
4540 break;
4541
4542 case ISN_COMPAREBOOL:
4543 case ISN_COMPARESPECIAL:
4544 case ISN_COMPARENR:
4545 case ISN_COMPAREFLOAT:
4546 case ISN_COMPARESTRING:
4547 case ISN_COMPAREBLOB:
4548 case ISN_COMPARELIST:
4549 case ISN_COMPAREDICT:
4550 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 case ISN_COMPAREANY:
4552 {
4553 char *p;
4554 char buf[10];
4555 char *type;
4556
4557 switch (iptr->isn_arg.op.op_type)
4558 {
4559 case EXPR_EQUAL: p = "=="; break;
4560 case EXPR_NEQUAL: p = "!="; break;
4561 case EXPR_GREATER: p = ">"; break;
4562 case EXPR_GEQUAL: p = ">="; break;
4563 case EXPR_SMALLER: p = "<"; break;
4564 case EXPR_SEQUAL: p = "<="; break;
4565 case EXPR_MATCH: p = "=~"; break;
4566 case EXPR_IS: p = "is"; break;
4567 case EXPR_ISNOT: p = "isnot"; break;
4568 case EXPR_NOMATCH: p = "!~"; break;
4569 default: p = "???"; break;
4570 }
4571 STRCPY(buf, p);
4572 if (iptr->isn_arg.op.op_ic == TRUE)
4573 strcat(buf, "?");
4574 switch(iptr->isn_type)
4575 {
4576 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4577 case ISN_COMPARESPECIAL:
4578 type = "COMPARESPECIAL"; break;
4579 case ISN_COMPARENR: type = "COMPARENR"; break;
4580 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4581 case ISN_COMPARESTRING:
4582 type = "COMPARESTRING"; break;
4583 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4584 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4585 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4586 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4588 default: type = "???"; break;
4589 }
4590
4591 smsg("%4d %s %s", current, type, buf);
4592 }
4593 break;
4594
4595 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4596 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4597
4598 // expression operations
4599 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004600 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004601 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004602 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004603 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004604 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004605 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004606 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4607 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004608 case ISN_SLICE: smsg("%4d SLICE %lld",
4609 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004610 case ISN_GETITEM: smsg("%4d ITEM %lld",
4611 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004612 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4613 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 iptr->isn_arg.string); break;
4615 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4616
4617 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004618 case ISN_CHECKTYPE:
4619 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004620 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004621 char *tofree;
4622
Bram Moolenaare32e5162021-01-21 20:21:29 +01004623 if (ct->ct_arg_idx == 0)
4624 smsg("%4d CHECKTYPE %s stack[%d]", current,
4625 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004626 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004627 else
4628 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4629 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004630 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004631 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004632 vim_free(tofree);
4633 break;
4634 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004635 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4636 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4637 iptr->isn_arg.checklen.cl_min_len);
4638 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004639 case ISN_SETTYPE:
4640 {
4641 char *tofree;
4642
4643 smsg("%4d SETTYPE %s", current,
4644 type_name(iptr->isn_arg.type.ct_type, &tofree));
4645 vim_free(tofree);
4646 break;
4647 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004648 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649 case ISN_2BOOL: if (iptr->isn_arg.number)
4650 smsg("%4d INVERT (!val)", current);
4651 else
4652 smsg("%4d 2BOOL (!!val)", current);
4653 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004654 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004655 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004656 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004657 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004658 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004659 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004660 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4661 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004662 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004663 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4664 smsg("%4d PUT %c above range",
4665 current, iptr->isn_arg.put.put_regname);
4666 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4667 smsg("%4d PUT %c range",
4668 current, iptr->isn_arg.put.put_regname);
4669 else
4670 smsg("%4d PUT %c %ld", current,
4671 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004672 (long)iptr->isn_arg.put.put_lnum);
4673 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674
Bram Moolenaar02194d22020-10-24 23:08:38 +02004675 // TODO: summarize modifiers
4676 case ISN_CMDMOD:
4677 {
4678 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004679 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004680 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4681
4682 buf = alloc(len + 1);
4683 if (buf != NULL)
4684 {
4685 (void)produce_cmdmods(
4686 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4687 smsg("%4d CMDMOD %s", current, buf);
4688 vim_free(buf);
4689 }
4690 break;
4691 }
4692 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004693
Bram Moolenaarb2049902021-01-24 12:53:53 +01004694 case ISN_PROF_START:
4695 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4696 break;
4697
4698 case ISN_PROF_END:
4699 smsg("%4d PROFILE END", current);
4700 break;
4701
Bram Moolenaar792f7862020-11-23 08:31:18 +01004702 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4703 iptr->isn_arg.unpack.unp_count,
4704 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4705 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004706 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4707 iptr->isn_arg.shuffle.shfl_item,
4708 iptr->isn_arg.shuffle.shfl_up);
4709 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 case ISN_DROP: smsg("%4d DROP", current); break;
4711 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004712
4713 out_flush(); // output one line at a time
4714 ui_breakcheck();
4715 if (got_int)
4716 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718}
4719
4720/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004721 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004722 * list, etc. Mostly like what JavaScript does, except that empty list and
4723 * empty dictionary are FALSE.
4724 */
4725 int
4726tv2bool(typval_T *tv)
4727{
4728 switch (tv->v_type)
4729 {
4730 case VAR_NUMBER:
4731 return tv->vval.v_number != 0;
4732 case VAR_FLOAT:
4733#ifdef FEAT_FLOAT
4734 return tv->vval.v_float != 0.0;
4735#else
4736 break;
4737#endif
4738 case VAR_PARTIAL:
4739 return tv->vval.v_partial != NULL;
4740 case VAR_FUNC:
4741 case VAR_STRING:
4742 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4743 case VAR_LIST:
4744 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4745 case VAR_DICT:
4746 return tv->vval.v_dict != NULL
4747 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4748 case VAR_BOOL:
4749 case VAR_SPECIAL:
4750 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4751 case VAR_JOB:
4752#ifdef FEAT_JOB_CHANNEL
4753 return tv->vval.v_job != NULL;
4754#else
4755 break;
4756#endif
4757 case VAR_CHANNEL:
4758#ifdef FEAT_JOB_CHANNEL
4759 return tv->vval.v_channel != NULL;
4760#else
4761 break;
4762#endif
4763 case VAR_BLOB:
4764 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4765 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004766 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767 case VAR_VOID:
4768 break;
4769 }
4770 return FALSE;
4771}
4772
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004773 void
4774emsg_using_string_as(typval_T *tv, int as_number)
4775{
4776 semsg(_(as_number ? e_using_string_as_number_str
4777 : e_using_string_as_bool_str),
4778 tv->vval.v_string == NULL
4779 ? (char_u *)"" : tv->vval.v_string);
4780}
4781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782/*
4783 * If "tv" is a string give an error and return FAIL.
4784 */
4785 int
4786check_not_string(typval_T *tv)
4787{
4788 if (tv->v_type == VAR_STRING)
4789 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004790 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 clear_tv(tv);
4792 return FAIL;
4793 }
4794 return OK;
4795}
4796
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797
4798#endif // FEAT_EVAL