blob: b80d533c910d1f307c1ea41fba3a67e67a44c090 [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
Bram Moolenaarff871402021-03-26 13:34:05 +01001070 // Do the same as for a list: a negative index counts from the end.
1071 // Optimization to check the first byte to be below 0x80 (and no composing
1072 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001073 if (index < 0)
1074 {
1075 int clen = 0;
1076
1077 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001078 {
1079 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1080 ++nbyte;
1081 else if (enc_utf8)
1082 nbyte += utfc_ptr2len(str + nbyte);
1083 else
1084 nbyte += mb_ptr2len(str + nbyte);
1085 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001086 nchar = clen + index;
1087 if (nchar < 0)
1088 // unlike list: index out of range results in empty string
1089 return NULL;
1090 }
1091
1092 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001093 {
1094 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1095 ++nbyte;
1096 else if (enc_utf8)
1097 nbyte += utfc_ptr2len(str + nbyte);
1098 else
1099 nbyte += mb_ptr2len(str + nbyte);
1100 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001101 if (nbyte >= slen)
1102 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001103 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001104}
1105
1106/*
1107 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001108 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001109 * If going over the end return "str_len".
1110 * If "idx" is negative count from the end, -1 is the last character.
1111 * When going over the start return -1.
1112 */
1113 static long
1114char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1115{
1116 varnumber_T nchar = idx;
1117 size_t nbyte = 0;
1118
1119 if (nchar >= 0)
1120 {
1121 while (nchar > 0 && nbyte < str_len)
1122 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001123 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001124 --nchar;
1125 }
1126 }
1127 else
1128 {
1129 nbyte = str_len;
1130 while (nchar < 0 && nbyte > 0)
1131 {
1132 --nbyte;
1133 nbyte -= mb_head_off(str, str + nbyte);
1134 ++nchar;
1135 }
1136 if (nchar < 0)
1137 return -1;
1138 }
1139 return (long)nbyte;
1140}
1141
1142/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001143 * Return the slice "str[first : last]" using character indexes. Composing
1144 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001145 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001146 * Return NULL when the result is empty.
1147 */
1148 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001149string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001150{
1151 long start_byte, end_byte;
1152 size_t slen;
1153
1154 if (str == NULL)
1155 return NULL;
1156 slen = STRLEN(str);
1157 start_byte = char_idx2byte(str, slen, first);
1158 if (start_byte < 0)
1159 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001160 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001161 end_byte = (long)slen;
1162 else
1163 {
1164 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001165 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001166 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001167 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001168 }
1169
1170 if (start_byte >= (long)slen || end_byte <= start_byte)
1171 return NULL;
1172 return vim_strnsave(str + start_byte, end_byte - start_byte);
1173}
1174
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001175 static svar_T *
1176get_script_svar(scriptref_T *sref, ectx_T *ectx)
1177{
1178 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1179 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1180 + ectx->ec_dfunc_idx;
1181 svar_T *sv;
1182
1183 if (sref->sref_seq != si->sn_script_seq)
1184 {
1185 // The script was reloaded after the function was
1186 // compiled, the script_idx may not be valid.
1187 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1188 dfunc->df_ufunc->uf_name_exp);
1189 return NULL;
1190 }
1191 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1192 if (!equal_type(sv->sv_type, sref->sref_type))
1193 {
1194 emsg(_(e_script_variable_type_changed));
1195 return NULL;
1196 }
1197 return sv;
1198}
1199
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001200/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 * Execute a function by "name".
1202 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001203 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 */
1205 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001206call_eval_func(
1207 char_u *name,
1208 int argcount,
1209 funclocal_T *funclocal,
1210 ectx_T *ectx,
1211 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212{
Bram Moolenaared677f52020-08-12 16:38:10 +02001213 int called_emsg_before = called_emsg;
1214 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001216 res = call_by_name(name, argcount, funclocal, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001217 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001219 dictitem_T *v;
1220
1221 v = find_var(name, NULL, FALSE);
1222 if (v == NULL)
1223 {
1224 semsg(_(e_unknownfunc), name);
1225 return FAIL;
1226 }
1227 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1228 {
1229 semsg(_(e_unknownfunc), name);
1230 return FAIL;
1231 }
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001232 return call_partial(&v->di_tv, argcount, funclocal, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001234 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235}
1236
1237/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001238 * When a function reference is used, fill a partial with the information
1239 * needed, especially when it is used as a closure.
1240 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001241 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001242fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1243{
1244 pt->pt_func = ufunc;
1245 pt->pt_refcount = 1;
1246
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001247 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001248 {
1249 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1250 + ectx->ec_dfunc_idx;
1251
1252 // The closure needs to find arguments and local
1253 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001254 pt->pt_outer.out_stack = &ectx->ec_stack;
1255 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1256 pt->pt_outer.out_up = ectx->ec_outer;
1257 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001258
1259 // If this function returns and the closure is still
1260 // being used, we need to make a copy of the context
1261 // (arguments and local variables). Store a reference
1262 // to the partial so we can handle that.
1263 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1264 {
1265 vim_free(pt);
1266 return FAIL;
1267 }
1268 // Extra variable keeps the count of closures created
1269 // in the current function call.
1270 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1271 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1272
1273 ((partial_T **)ectx->ec_funcrefs.ga_data)
1274 [ectx->ec_funcrefs.ga_len] = pt;
1275 ++pt->pt_refcount;
1276 ++ectx->ec_funcrefs.ga_len;
1277 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001278 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001279 return OK;
1280}
1281
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001282
Bram Moolenaarf112f302020-12-20 17:47:52 +01001283/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284 * Call a "def" function from old Vim script.
1285 * Return OK or FAIL.
1286 */
1287 int
1288call_def_function(
1289 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001290 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001292 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293 typval_T *rettv) // return value
1294{
1295 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001296 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001297 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298 typval_T *tv;
1299 int idx;
1300 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001301 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001302 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001303 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001304 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001305 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001306 msglist_T **saved_msg_list = NULL;
1307 msglist_T *private_msg_list = NULL;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001308 funclocal_T funclocal;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001309 int save_emsg_silent_def = emsg_silent_def;
1310 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001311 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001312 int orig_funcdepth;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001313 where_T where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314
1315// Get pointer to item in the stack.
1316#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1317
1318// Get pointer to item at the bottom of the stack, -1 is the bottom.
1319#undef STACK_TV_BOT
1320#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1321
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001322// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001323#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 +01001324
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001325 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001326 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1327 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001328 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001329 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001330 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001331 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001332 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001333 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001334 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001335
Bram Moolenaar09689a02020-05-09 22:50:08 +02001336 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001337 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001338 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1339 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001340 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001341 {
1342 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001343 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001344 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001345 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001347 // If depth of calling is getting too high, don't execute the function.
1348 orig_funcdepth = funcdepth_get();
1349 if (funcdepth_increment() == FAIL)
1350 return FAIL;
1351
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001352 CLEAR_FIELD(funclocal);
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001353 CLEAR_FIELD(ectx);
1354 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1355 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1356 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001357 {
1358 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001359 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001360 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001362 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001364 // Put arguments on the stack, but no more than what the function expects.
1365 // A lambda can be called with more arguments than it uses.
1366 for (idx = 0; idx < argc
1367 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1368 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001370 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001371 && check_typval_arg_type(ufunc->uf_arg_types[idx], &argv[idx],
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001372 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001373 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 copy_tv(&argv[idx], STACK_TV_BOT(0));
1375 ++ectx.ec_stack.ga_len;
1376 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001377
1378 // Turn varargs into a list. Empty list if no args.
1379 if (ufunc->uf_va_name != NULL)
1380 {
1381 int vararg_count = argc - ufunc->uf_args.ga_len;
1382
1383 if (vararg_count < 0)
1384 vararg_count = 0;
1385 else
1386 argc -= vararg_count;
1387 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001388 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001389
1390 // Check the type of the list items.
1391 tv = STACK_TV_BOT(-1);
1392 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001393 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001394 && ufunc->uf_va_type->tt_member != &t_any
1395 && tv->vval.v_list != NULL)
1396 {
1397 type_T *expected = ufunc->uf_va_type->tt_member;
1398 listitem_T *li = tv->vval.v_list->lv_first;
1399
1400 for (idx = 0; idx < vararg_count; ++idx)
1401 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001402 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001403 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001404 goto failed_early;
1405 li = li->li_next;
1406 }
1407 }
1408
Bram Moolenaar23e03252020-04-12 22:22:31 +02001409 if (defcount > 0)
1410 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001411 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001412 --ectx.ec_stack.ga_len;
1413 }
1414
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001415 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001416 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001417 if (defcount > 0)
1418 for (idx = 0; idx < defcount; ++idx)
1419 {
1420 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1421 ++ectx.ec_stack.ga_len;
1422 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001423 if (ufunc->uf_va_name != NULL)
Bram Moolenaarc970e422021-03-17 15:03:04 +01001424 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425
1426 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001427 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1428 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001430 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001431 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1432 + ufunc->uf_dfunc_idx;
1433 ufunc_T *base_ufunc = dfunc->df_ufunc;
1434
1435 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1436 // by copy_func().
1437 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001438 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001439 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1440 if (ectx.ec_outer == NULL)
1441 goto failed_early;
1442 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001443 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001444 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1445 {
1446 if (current_ectx->ec_outer != NULL)
1447 *ectx.ec_outer = *current_ectx->ec_outer;
1448 }
1449 else
1450 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001451 }
1452 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001453 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1454 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001455 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001456 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 // dummy frame entries
1459 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1460 {
1461 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1462 ++ectx.ec_stack.ga_len;
1463 }
1464
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001465 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001466 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001467 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1468 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001470 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001471 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001472 ectx.ec_stack.ga_len += dfunc->df_varcount;
1473 if (dfunc->df_has_closure)
1474 {
1475 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1476 STACK_TV_VAR(idx)->vval.v_number = 0;
1477 ++ectx.ec_stack.ga_len;
1478 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001479
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001480 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001481 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001482
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001483 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001484 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001485 estack_push_ufunc(ufunc, 1);
1486 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001487 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1488
Bram Moolenaar352134b2020-10-17 22:04:08 +02001489 // Use a specific location for storing error messages to be converted to an
1490 // exception.
1491 saved_msg_list = msg_list;
1492 msg_list = &private_msg_list;
1493
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001494 // Do turn errors into exceptions.
1495 suppress_errthrow = FALSE;
1496
Bram Moolenaarc970e422021-03-17 15:03:04 +01001497 // Do not delete the function while executing it.
1498 ++ufunc->uf_calls;
1499
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001500 // When ":silent!" was used before calling then we still abort the
1501 // function. If ":silent!" is used in the function then we don't.
1502 emsg_silent_def = emsg_silent;
1503 did_emsg_def = 0;
1504
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001505 where.wt_index = 0;
1506 where.wt_variable = FALSE;
1507
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001508 // Decide where to start execution, handles optional arguments.
1509 init_instr_idx(ufunc, argc, &ectx);
1510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 for (;;)
1512 {
1513 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001514
Bram Moolenaar270d0382020-05-15 21:42:53 +02001515 if (++breakcheck_count >= 100)
1516 {
1517 line_breakcheck();
1518 breakcheck_count = 0;
1519 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001520 if (got_int)
1521 {
1522 // Turn CTRL-C into an exception.
1523 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001524 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001525 goto failed;
1526 did_throw = TRUE;
1527 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528
Bram Moolenaara26b9702020-04-18 19:53:28 +02001529 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1530 {
1531 // Turn an error message into an exception.
1532 did_emsg = FALSE;
1533 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1534 goto failed;
1535 did_throw = TRUE;
1536 *msg_list = NULL;
1537 }
1538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539 if (did_throw && !ectx.ec_in_catch)
1540 {
1541 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001542 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543
1544 // An exception jumps to the first catch, finally, or returns from
1545 // the current function.
1546 if (trystack->ga_len > 0)
1547 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001548 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 {
1550 // jump to ":catch" or ":finally"
1551 ectx.ec_in_catch = TRUE;
1552 ectx.ec_iidx = trycmd->tcd_catch_idx;
1553 }
1554 else
1555 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001556 // Not inside try or need to return from current functions.
1557 // Push a dummy return value.
1558 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1559 goto failed;
1560 tv = STACK_TV_BOT(0);
1561 tv->v_type = VAR_NUMBER;
1562 tv->vval.v_number = 0;
1563 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001564 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001566 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001567 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001568 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1569 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 goto done;
1571 }
1572
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001573 if (func_return(&funclocal, &ectx) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001574 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 }
1576 continue;
1577 }
1578
1579 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1580 switch (iptr->isn_type)
1581 {
1582 // execute Ex command line
1583 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001584 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001585 source_cookie_T cookie;
1586
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001587 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001588 // Pass getsourceline to get an error for a missing ":end"
1589 // command.
1590 CLEAR_FIELD(cookie);
1591 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1592 if (do_cmdline(iptr->isn_arg.string,
1593 getsourceline, &cookie,
1594 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1595 == FAIL
1596 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001597 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001598 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 break;
1600
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001601 // execute Ex command from pieces on the stack
1602 case ISN_EXECCONCAT:
1603 {
1604 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001605 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001606 int pass;
1607 int i;
1608 char_u *cmd = NULL;
1609 char_u *str;
1610
1611 for (pass = 1; pass <= 2; ++pass)
1612 {
1613 for (i = 0; i < count; ++i)
1614 {
1615 tv = STACK_TV_BOT(i - count);
1616 str = tv->vval.v_string;
1617 if (str != NULL && *str != NUL)
1618 {
1619 if (pass == 2)
1620 STRCPY(cmd + len, str);
1621 len += STRLEN(str);
1622 }
1623 if (pass == 2)
1624 clear_tv(tv);
1625 }
1626 if (pass == 1)
1627 {
1628 cmd = alloc(len + 1);
1629 if (cmd == NULL)
1630 goto failed;
1631 len = 0;
1632 }
1633 }
1634
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001635 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001636 do_cmdline_cmd(cmd);
1637 vim_free(cmd);
1638 }
1639 break;
1640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 // execute :echo {string} ...
1642 case ISN_ECHO:
1643 {
1644 int count = iptr->isn_arg.echo.echo_count;
1645 int atstart = TRUE;
1646 int needclr = TRUE;
1647
1648 for (idx = 0; idx < count; ++idx)
1649 {
1650 tv = STACK_TV_BOT(idx - count);
1651 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1652 &atstart, &needclr);
1653 clear_tv(tv);
1654 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001655 if (needclr)
1656 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 ectx.ec_stack.ga_len -= count;
1658 }
1659 break;
1660
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001661 // :execute {string} ...
1662 // :echomsg {string} ...
1663 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001664 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001665 case ISN_ECHOMSG:
1666 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001667 {
1668 int count = iptr->isn_arg.number;
1669 garray_T ga;
1670 char_u buf[NUMBUFLEN];
1671 char_u *p;
1672 int len;
1673 int failed = FALSE;
1674
1675 ga_init2(&ga, 1, 80);
1676 for (idx = 0; idx < count; ++idx)
1677 {
1678 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001679 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001680 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001681 if (tv->v_type == VAR_CHANNEL
1682 || tv->v_type == VAR_JOB)
1683 {
1684 SOURCING_LNUM = iptr->isn_lnum;
1685 emsg(_(e_inval_string));
1686 break;
1687 }
1688 else
1689 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001690 }
1691 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001692 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001693
1694 len = (int)STRLEN(p);
1695 if (ga_grow(&ga, len + 2) == FAIL)
1696 failed = TRUE;
1697 else
1698 {
1699 if (ga.ga_len > 0)
1700 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1701 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1702 ga.ga_len += len;
1703 }
1704 clear_tv(tv);
1705 }
1706 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001707 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001708 {
1709 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001710 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001711 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001712
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001713 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001714 {
1715 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001716 {
1717 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001718 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001719 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001720 {
1721 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001722 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001723 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001724 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001725 else
1726 {
1727 msg_sb_eol();
1728 if (iptr->isn_type == ISN_ECHOMSG)
1729 {
1730 msg_attr(ga.ga_data, echo_attr);
1731 out_flush();
1732 }
1733 else
1734 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001735 SOURCING_LNUM = iptr->isn_lnum;
1736 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001737 }
1738 }
1739 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001740 ga_clear(&ga);
1741 }
1742 break;
1743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 // load local variable or argument
1745 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001746 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 goto failed;
1748 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1749 ++ectx.ec_stack.ga_len;
1750 break;
1751
1752 // load v: variable
1753 case ISN_LOADV:
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(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1757 ++ectx.ec_stack.ga_len;
1758 break;
1759
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001760 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 case ISN_LOADSCRIPT:
1762 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001763 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764 svar_T *sv;
1765
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001766 sv = get_script_svar(sref, &ectx);
1767 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001768 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001769 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001770 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 goto failed;
1772 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1773 ++ectx.ec_stack.ga_len;
1774 }
1775 break;
1776
1777 // load s: variable in old script
1778 case ISN_LOADS:
1779 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001780 hashtab_T *ht = &SCRIPT_VARS(
1781 iptr->isn_arg.loadstore.ls_sid);
1782 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001784
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 if (di == NULL)
1786 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001787 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001788 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001789 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790 }
1791 else
1792 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001793 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794 goto failed;
1795 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1796 ++ectx.ec_stack.ga_len;
1797 }
1798 }
1799 break;
1800
Bram Moolenaard3aac292020-04-19 14:32:17 +02001801 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001803 case ISN_LOADB:
1804 case ISN_LOADW:
1805 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001807 dictitem_T *di = NULL;
1808 hashtab_T *ht = NULL;
1809 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001810
Bram Moolenaard3aac292020-04-19 14:32:17 +02001811 switch (iptr->isn_type)
1812 {
1813 case ISN_LOADG:
1814 ht = get_globvar_ht();
1815 namespace = 'g';
1816 break;
1817 case ISN_LOADB:
1818 ht = &curbuf->b_vars->dv_hashtab;
1819 namespace = 'b';
1820 break;
1821 case ISN_LOADW:
1822 ht = &curwin->w_vars->dv_hashtab;
1823 namespace = 'w';
1824 break;
1825 case ISN_LOADT:
1826 ht = &curtab->tp_vars->dv_hashtab;
1827 namespace = 't';
1828 break;
1829 default: // Cannot reach here
1830 goto failed;
1831 }
1832 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001833
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 if (di == NULL)
1835 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001836 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001837 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001838 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001839 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 }
1841 else
1842 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001843 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844 goto failed;
1845 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1846 ++ectx.ec_stack.ga_len;
1847 }
1848 }
1849 break;
1850
Bram Moolenaar03290b82020-12-19 16:30:44 +01001851 // load autoload variable
1852 case ISN_LOADAUTO:
1853 {
1854 char_u *name = iptr->isn_arg.string;
1855
1856 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1857 goto failed;
1858 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001859 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001860 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001861 goto on_error;
1862 ++ectx.ec_stack.ga_len;
1863 }
1864 break;
1865
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001866 // load g:/b:/w:/t: namespace
1867 case ISN_LOADGDICT:
1868 case ISN_LOADBDICT:
1869 case ISN_LOADWDICT:
1870 case ISN_LOADTDICT:
1871 {
1872 dict_T *d = NULL;
1873
1874 switch (iptr->isn_type)
1875 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001876 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1877 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1878 case ISN_LOADWDICT: d = curwin->w_vars; break;
1879 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001880 default: // Cannot reach here
1881 goto failed;
1882 }
1883 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1884 goto failed;
1885 tv = STACK_TV_BOT(0);
1886 tv->v_type = VAR_DICT;
1887 tv->v_lock = 0;
1888 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001889 ++d->dv_refcount;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001890 ++ectx.ec_stack.ga_len;
1891 }
1892 break;
1893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 // load &option
1895 case ISN_LOADOPT:
1896 {
1897 typval_T optval;
1898 char_u *name = iptr->isn_arg.string;
1899
Bram Moolenaara8c17702020-04-01 21:17:24 +02001900 // This is not expected to fail, name is checked during
1901 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001902 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001904 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001905 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 *STACK_TV_BOT(0) = optval;
1907 ++ectx.ec_stack.ga_len;
1908 }
1909 break;
1910
1911 // load $ENV
1912 case ISN_LOADENV:
1913 {
1914 typval_T optval;
1915 char_u *name = iptr->isn_arg.string;
1916
Bram Moolenaar270d0382020-05-15 21:42:53 +02001917 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001919 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001920 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 *STACK_TV_BOT(0) = optval;
1922 ++ectx.ec_stack.ga_len;
1923 }
1924 break;
1925
1926 // load @register
1927 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001928 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 goto failed;
1930 tv = STACK_TV_BOT(0);
1931 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001932 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001933 // This may result in NULL, which should be equivalent to an
1934 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 tv->vval.v_string = get_reg_contents(
1936 iptr->isn_arg.number, GREG_EXPR_SRC);
1937 ++ectx.ec_stack.ga_len;
1938 break;
1939
1940 // store local variable
1941 case ISN_STORE:
1942 --ectx.ec_stack.ga_len;
1943 tv = STACK_TV_VAR(iptr->isn_arg.number);
1944 clear_tv(tv);
1945 *tv = *STACK_TV_BOT(0);
1946 break;
1947
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001948 // store s: variable in old script
1949 case ISN_STORES:
1950 {
1951 hashtab_T *ht = &SCRIPT_VARS(
1952 iptr->isn_arg.loadstore.ls_sid);
1953 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001954 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001955
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001956 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001957 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001958 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001959 else
1960 {
1961 clear_tv(&di->di_tv);
1962 di->di_tv = *STACK_TV_BOT(0);
1963 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001964 }
1965 break;
1966
1967 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 case ISN_STORESCRIPT:
1969 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001970 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1971 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001973 sv = get_script_svar(sref, &ectx);
1974 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001975 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 --ectx.ec_stack.ga_len;
1977 clear_tv(sv->sv_tv);
1978 *sv->sv_tv = *STACK_TV_BOT(0);
1979 }
1980 break;
1981
1982 // store option
1983 case ISN_STOREOPT:
1984 {
1985 long n = 0;
1986 char_u *s = NULL;
1987 char *msg;
1988
1989 --ectx.ec_stack.ga_len;
1990 tv = STACK_TV_BOT(0);
1991 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001992 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001994 if (s == NULL)
1995 s = (char_u *)"";
1996 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001998 // must be VAR_NUMBER, CHECKTYPE makes sure
1999 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2001 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002002 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 if (msg != NULL)
2004 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002005 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002007 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 }
2010 break;
2011
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002012 // store $ENV
2013 case ISN_STOREENV:
2014 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002015 tv = STACK_TV_BOT(0);
2016 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2017 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002018 break;
2019
2020 // store @r
2021 case ISN_STOREREG:
2022 {
2023 int reg = iptr->isn_arg.number;
2024
2025 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002026 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002027 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002028 tv_get_string(tv), -1, FALSE);
2029 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002030 }
2031 break;
2032
2033 // store v: variable
2034 case ISN_STOREV:
2035 --ectx.ec_stack.ga_len;
2036 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2037 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002038 // should not happen, type is checked when compiling
2039 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002040 break;
2041
Bram Moolenaard3aac292020-04-19 14:32:17 +02002042 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002044 case ISN_STOREB:
2045 case ISN_STOREW:
2046 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002048 dictitem_T *di;
2049 hashtab_T *ht;
2050 char_u *name = iptr->isn_arg.string + 2;
2051
Bram Moolenaard3aac292020-04-19 14:32:17 +02002052 switch (iptr->isn_type)
2053 {
2054 case ISN_STOREG:
2055 ht = get_globvar_ht();
2056 break;
2057 case ISN_STOREB:
2058 ht = &curbuf->b_vars->dv_hashtab;
2059 break;
2060 case ISN_STOREW:
2061 ht = &curwin->w_vars->dv_hashtab;
2062 break;
2063 case ISN_STORET:
2064 ht = &curtab->tp_vars->dv_hashtab;
2065 break;
2066 default: // Cannot reach here
2067 goto failed;
2068 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069
2070 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002071 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002073 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074 else
2075 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002076 SOURCING_LNUM = iptr->isn_lnum;
2077 if (var_check_permission(di, name) == FAIL)
2078 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 clear_tv(&di->di_tv);
2080 di->di_tv = *STACK_TV_BOT(0);
2081 }
2082 }
2083 break;
2084
Bram Moolenaar03290b82020-12-19 16:30:44 +01002085 // store an autoload variable
2086 case ISN_STOREAUTO:
2087 SOURCING_LNUM = iptr->isn_lnum;
2088 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2089 clear_tv(STACK_TV_BOT(-1));
2090 --ectx.ec_stack.ga_len;
2091 break;
2092
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093 // store number in local variable
2094 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002095 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096 clear_tv(tv);
2097 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002098 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 break;
2100
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002101 // store value in list or dict variable
2102 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002103 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002104 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002105 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002106 typval_T *tv_dest = STACK_TV_BOT(-1);
2107 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002108
Bram Moolenaar752fc692021-01-04 21:57:11 +01002109 // Stack contains:
2110 // -3 value to be stored
2111 // -2 index
2112 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002113 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002114 SOURCING_LNUM = iptr->isn_lnum;
2115 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002116 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002117 dest_type = tv_dest->v_type;
2118 if (dest_type == VAR_DICT)
2119 status = do_2string(tv_idx, TRUE);
2120 else if (dest_type == VAR_LIST
2121 && tv_idx->v_type != VAR_NUMBER)
2122 {
2123 emsg(_(e_number_exp));
2124 status = FAIL;
2125 }
2126 }
2127 else if (dest_type != tv_dest->v_type)
2128 {
2129 // just in case, should be OK
2130 semsg(_(e_expected_str_but_got_str),
2131 vartype_name(dest_type),
2132 vartype_name(tv_dest->v_type));
2133 status = FAIL;
2134 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002135
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002136 if (status == OK && dest_type == VAR_LIST)
2137 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002138 long lidx = (long)tv_idx->vval.v_number;
2139 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002140
2141 if (list == NULL)
2142 {
2143 emsg(_(e_list_not_set));
2144 goto on_error;
2145 }
2146 if (lidx < 0 && list->lv_len + lidx >= 0)
2147 // negative index is relative to the end
2148 lidx = list->lv_len + lidx;
2149 if (lidx < 0 || lidx > list->lv_len)
2150 {
2151 semsg(_(e_listidx), lidx);
2152 goto on_error;
2153 }
2154 if (lidx < list->lv_len)
2155 {
2156 listitem_T *li = list_find(list, lidx);
2157
2158 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002159 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002160 goto on_error;
2161 // overwrite existing list item
2162 clear_tv(&li->li_tv);
2163 li->li_tv = *tv;
2164 }
2165 else
2166 {
2167 if (error_if_locked(list->lv_lock,
2168 e_cannot_change_list))
2169 goto on_error;
2170 // append to list, only fails when out of memory
2171 if (list_append_tv(list, tv) == FAIL)
2172 goto failed;
2173 clear_tv(tv);
2174 }
2175 }
2176 else if (status == OK && dest_type == VAR_DICT)
2177 {
2178 char_u *key = tv_idx->vval.v_string;
2179 dict_T *dict = tv_dest->vval.v_dict;
2180 dictitem_T *di;
2181
2182 SOURCING_LNUM = iptr->isn_lnum;
2183 if (dict == NULL)
2184 {
2185 emsg(_(e_dictionary_not_set));
2186 goto on_error;
2187 }
2188 if (key == NULL)
2189 key = (char_u *)"";
2190 di = dict_find(dict, key, -1);
2191 if (di != NULL)
2192 {
2193 if (error_if_locked(di->di_tv.v_lock,
2194 e_cannot_change_dict_item))
2195 goto on_error;
2196 // overwrite existing value
2197 clear_tv(&di->di_tv);
2198 di->di_tv = *tv;
2199 }
2200 else
2201 {
2202 if (error_if_locked(dict->dv_lock,
2203 e_cannot_change_dict))
2204 goto on_error;
2205 // add to dict, only fails when out of memory
2206 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2207 goto failed;
2208 clear_tv(tv);
2209 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002210 }
2211 else
2212 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002213 status = FAIL;
2214 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002215 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002216
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002217 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002218 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002219 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002220 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002221 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002222 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002223 goto on_error;
2224 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002225 }
2226 break;
2227
Bram Moolenaar0186e582021-01-10 18:33:11 +01002228 // load or store variable or argument from outer scope
2229 case ISN_LOADOUTER:
2230 case ISN_STOREOUTER:
2231 {
2232 int depth = iptr->isn_arg.outer.outer_depth;
2233 outer_T *outer = ectx.ec_outer;
2234
2235 while (depth > 1 && outer != NULL)
2236 {
2237 outer = outer->out_up;
2238 --depth;
2239 }
2240 if (outer == NULL)
2241 {
2242 SOURCING_LNUM = iptr->isn_lnum;
2243 iemsg("LOADOUTER depth more than scope levels");
2244 goto failed;
2245 }
2246 tv = ((typval_T *)outer->out_stack->ga_data)
2247 + outer->out_frame_idx + STACK_FRAME_SIZE
2248 + iptr->isn_arg.outer.outer_idx;
2249 if (iptr->isn_type == ISN_LOADOUTER)
2250 {
2251 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2252 goto failed;
2253 copy_tv(tv, STACK_TV_BOT(0));
2254 ++ectx.ec_stack.ga_len;
2255 }
2256 else
2257 {
2258 --ectx.ec_stack.ga_len;
2259 clear_tv(tv);
2260 *tv = *STACK_TV_BOT(0);
2261 }
2262 }
2263 break;
2264
Bram Moolenaar752fc692021-01-04 21:57:11 +01002265 // unlet item in list or dict variable
2266 case ISN_UNLETINDEX:
2267 {
2268 typval_T *tv_idx = STACK_TV_BOT(-2);
2269 typval_T *tv_dest = STACK_TV_BOT(-1);
2270 int status = OK;
2271
2272 // Stack contains:
2273 // -2 index
2274 // -1 dict or list
2275 if (tv_dest->v_type == VAR_DICT)
2276 {
2277 // unlet a dict item, index must be a string
2278 if (tv_idx->v_type != VAR_STRING)
2279 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002280 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002281 semsg(_(e_expected_str_but_got_str),
2282 vartype_name(VAR_STRING),
2283 vartype_name(tv_idx->v_type));
2284 status = FAIL;
2285 }
2286 else
2287 {
2288 dict_T *d = tv_dest->vval.v_dict;
2289 char_u *key = tv_idx->vval.v_string;
2290 dictitem_T *di = NULL;
2291
2292 if (key == NULL)
2293 key = (char_u *)"";
2294 if (d != NULL)
2295 di = dict_find(d, key, (int)STRLEN(key));
2296 if (di == NULL)
2297 {
2298 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002299 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002300 semsg(_(e_dictkey), key);
2301 status = FAIL;
2302 }
2303 else
2304 {
2305 // TODO: check for dict or item locked
2306 dictitem_remove(d, di);
2307 }
2308 }
2309 }
2310 else if (tv_dest->v_type == VAR_LIST)
2311 {
2312 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002313 SOURCING_LNUM = iptr->isn_lnum;
2314 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002315 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002316 status = FAIL;
2317 }
2318 else
2319 {
2320 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002321 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002322 listitem_T *li = NULL;
2323
2324 li = list_find(l, n);
2325 if (li == NULL)
2326 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002327 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002328 semsg(_(e_listidx), n);
2329 status = FAIL;
2330 }
2331 else
2332 // TODO: check for list or item locked
2333 listitem_remove(l, li);
2334 }
2335 }
2336 else
2337 {
2338 status = FAIL;
2339 semsg(_(e_cannot_index_str),
2340 vartype_name(tv_dest->v_type));
2341 }
2342
2343 clear_tv(tv_idx);
2344 clear_tv(tv_dest);
2345 ectx.ec_stack.ga_len -= 2;
2346 if (status == FAIL)
2347 goto on_error;
2348 }
2349 break;
2350
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002351 // unlet range of items in list variable
2352 case ISN_UNLETRANGE:
2353 {
2354 // Stack contains:
2355 // -3 index1
2356 // -2 index2
2357 // -1 dict or list
2358 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2359 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2360 typval_T *tv_dest = STACK_TV_BOT(-1);
2361 int status = OK;
2362
2363 if (tv_dest->v_type == VAR_LIST)
2364 {
2365 // indexes must be a number
2366 SOURCING_LNUM = iptr->isn_lnum;
2367 if (check_for_number(tv_idx1) == FAIL
2368 || check_for_number(tv_idx2) == FAIL)
2369 {
2370 status = FAIL;
2371 }
2372 else
2373 {
2374 list_T *l = tv_dest->vval.v_list;
2375 long n1 = (long)tv_idx1->vval.v_number;
2376 long n2 = (long)tv_idx2->vval.v_number;
2377 listitem_T *li;
2378
2379 li = list_find_index(l, &n1);
2380 if (li == NULL
2381 || list_unlet_range(l, li, NULL, n1,
2382 TRUE, n2) == FAIL)
2383 status = FAIL;
2384 }
2385 }
2386 else
2387 {
2388 status = FAIL;
2389 SOURCING_LNUM = iptr->isn_lnum;
2390 semsg(_(e_cannot_index_str),
2391 vartype_name(tv_dest->v_type));
2392 }
2393
2394 clear_tv(tv_idx1);
2395 clear_tv(tv_idx2);
2396 clear_tv(tv_dest);
2397 ectx.ec_stack.ga_len -= 3;
2398 if (status == FAIL)
2399 goto on_error;
2400 }
2401 break;
2402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 // push constant
2404 case ISN_PUSHNR:
2405 case ISN_PUSHBOOL:
2406 case ISN_PUSHSPEC:
2407 case ISN_PUSHF:
2408 case ISN_PUSHS:
2409 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002410 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002411 case ISN_PUSHCHANNEL:
2412 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002413 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 goto failed;
2415 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002416 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 ++ectx.ec_stack.ga_len;
2418 switch (iptr->isn_type)
2419 {
2420 case ISN_PUSHNR:
2421 tv->v_type = VAR_NUMBER;
2422 tv->vval.v_number = iptr->isn_arg.number;
2423 break;
2424 case ISN_PUSHBOOL:
2425 tv->v_type = VAR_BOOL;
2426 tv->vval.v_number = iptr->isn_arg.number;
2427 break;
2428 case ISN_PUSHSPEC:
2429 tv->v_type = VAR_SPECIAL;
2430 tv->vval.v_number = iptr->isn_arg.number;
2431 break;
2432#ifdef FEAT_FLOAT
2433 case ISN_PUSHF:
2434 tv->v_type = VAR_FLOAT;
2435 tv->vval.v_float = iptr->isn_arg.fnumber;
2436 break;
2437#endif
2438 case ISN_PUSHBLOB:
2439 blob_copy(iptr->isn_arg.blob, tv);
2440 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002441 case ISN_PUSHFUNC:
2442 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002443 if (iptr->isn_arg.string == NULL)
2444 tv->vval.v_string = NULL;
2445 else
2446 tv->vval.v_string =
2447 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002448 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002449 case ISN_PUSHCHANNEL:
2450#ifdef FEAT_JOB_CHANNEL
2451 tv->v_type = VAR_CHANNEL;
2452 tv->vval.v_channel = iptr->isn_arg.channel;
2453 if (tv->vval.v_channel != NULL)
2454 ++tv->vval.v_channel->ch_refcount;
2455#endif
2456 break;
2457 case ISN_PUSHJOB:
2458#ifdef FEAT_JOB_CHANNEL
2459 tv->v_type = VAR_JOB;
2460 tv->vval.v_job = iptr->isn_arg.job;
2461 if (tv->vval.v_job != NULL)
2462 ++tv->vval.v_job->jv_refcount;
2463#endif
2464 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465 default:
2466 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002467 tv->vval.v_string = vim_strsave(
2468 iptr->isn_arg.string == NULL
2469 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 }
2471 break;
2472
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002473 case ISN_UNLET:
2474 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2475 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002476 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002477 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002478 case ISN_UNLETENV:
2479 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2480 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002481
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002482 case ISN_LOCKCONST:
2483 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2484 break;
2485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 // create a list from items on the stack; uses a single allocation
2487 // for the list header and the items
2488 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002489 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2490 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491 break;
2492
2493 // create a dict from items on the stack
2494 case ISN_NEWDICT:
2495 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002496 int count = iptr->isn_arg.number;
2497 dict_T *dict = dict_alloc();
2498 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002499 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500
2501 if (dict == NULL)
2502 goto failed;
2503 for (idx = 0; idx < count; ++idx)
2504 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002505 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002507 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002508 key = tv->vval.v_string == NULL
2509 ? (char_u *)"" : tv->vval.v_string;
2510 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002511 if (item != NULL)
2512 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002513 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002514 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002515 dict_unref(dict);
2516 goto on_error;
2517 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002518 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 clear_tv(tv);
2520 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002521 {
2522 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002524 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2526 item->di_tv.v_lock = 0;
2527 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002528 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002529 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002530 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 }
2534
2535 if (count > 0)
2536 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002537 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 goto failed;
2539 else
2540 ++ectx.ec_stack.ga_len;
2541 tv = STACK_TV_BOT(-1);
2542 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002543 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 tv->vval.v_dict = dict;
2545 ++dict->dv_refcount;
2546 }
2547 break;
2548
2549 // call a :def function
2550 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002551 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002552 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2553 NULL,
2554 iptr->isn_arg.dfunc.cdf_argcount,
2555 &funclocal,
2556 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002557 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 break;
2559
2560 // call a builtin function
2561 case ISN_BCALL:
2562 SOURCING_LNUM = iptr->isn_lnum;
2563 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2564 iptr->isn_arg.bfunc.cbf_argcount,
2565 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002566 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 break;
2568
2569 // call a funcref or partial
2570 case ISN_PCALL:
2571 {
2572 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2573 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002574 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575
2576 SOURCING_LNUM = iptr->isn_lnum;
2577 if (pfunc->cpf_top)
2578 {
2579 // funcref is above the arguments
2580 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2581 }
2582 else
2583 {
2584 // Get the funcref from the stack.
2585 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002586 partial_tv = *STACK_TV_BOT(0);
2587 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 }
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002589 r = call_partial(tv, pfunc->cpf_argcount,
2590 &funclocal, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002591 if (tv == &partial_tv)
2592 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002594 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 }
2596 break;
2597
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002598 case ISN_PCALL_END:
2599 // PCALL finished, arguments have been consumed and replaced by
2600 // the return value. Now clear the funcref from the stack,
2601 // and move the return value in its place.
2602 --ectx.ec_stack.ga_len;
2603 clear_tv(STACK_TV_BOT(-1));
2604 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2605 break;
2606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 // call a user defined function or funcref/partial
2608 case ISN_UCALL:
2609 {
2610 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2611
2612 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002613 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
2614 &funclocal, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002615 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 }
2617 break;
2618
2619 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002620 case ISN_RETURN_ZERO:
2621 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2622 goto failed;
2623 tv = STACK_TV_BOT(0);
2624 ++ectx.ec_stack.ga_len;
2625 tv->v_type = VAR_NUMBER;
2626 tv->vval.v_number = 0;
2627 tv->v_lock = 0;
2628 // FALLTHROUGH
2629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 case ISN_RETURN:
2631 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002632 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002633 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002634
2635 if (trystack->ga_len > 0)
2636 trycmd = ((trycmd_T *)trystack->ga_data)
2637 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002638 if (trycmd != NULL
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002639 && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002641 // jump to ":finally" or ":endtry"
2642 if (trycmd->tcd_finally_idx != 0)
2643 ectx.ec_iidx = trycmd->tcd_finally_idx;
2644 else
2645 ectx.ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 trycmd->tcd_return = TRUE;
2647 }
2648 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002649 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650 }
2651 break;
2652
2653 // push a function reference to a compiled function
2654 case ISN_FUNCREF:
2655 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002656 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2657 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2658 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 if (pt == NULL)
2661 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002662 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002663 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002664 vim_free(pt);
2665 goto failed;
2666 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002667 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2668 &ectx) == FAIL)
2669 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 tv = STACK_TV_BOT(0);
2672 ++ectx.ec_stack.ga_len;
2673 tv->vval.v_partial = pt;
2674 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002675 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 }
2677 break;
2678
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002679 // Create a global function from a lambda.
2680 case ISN_NEWFUNC:
2681 {
2682 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2683
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002684 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002685 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002686 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002687 }
2688 break;
2689
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002690 // List functions
2691 case ISN_DEF:
2692 if (iptr->isn_arg.string == NULL)
2693 list_functions(NULL);
2694 else
2695 {
2696 exarg_T ea;
2697
2698 CLEAR_FIELD(ea);
2699 ea.cmd = ea.arg = iptr->isn_arg.string;
2700 define_function(&ea, NULL);
2701 }
2702 break;
2703
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 // jump if a condition is met
2705 case ISN_JUMP:
2706 {
2707 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002708 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 int jump = TRUE;
2710
2711 if (when != JUMP_ALWAYS)
2712 {
2713 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002714 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002715 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002716 || when == JUMP_IF_COND_TRUE)
2717 {
2718 SOURCING_LNUM = iptr->isn_lnum;
2719 jump = tv_get_bool_chk(tv, &error);
2720 if (error)
2721 goto on_error;
2722 }
2723 else
2724 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002726 || when == JUMP_AND_KEEP_IF_FALSE
2727 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002729 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 {
2731 // drop the value from the stack
2732 clear_tv(tv);
2733 --ectx.ec_stack.ga_len;
2734 }
2735 }
2736 if (jump)
2737 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2738 }
2739 break;
2740
2741 // top of a for loop
2742 case ISN_FOR:
2743 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002744 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 typval_T *idxtv =
2746 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2747
Bram Moolenaar270d0382020-05-15 21:42:53 +02002748 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 goto failed;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002750 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002751 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002752 list_T *list = ltv->vval.v_list;
2753
2754 // push the next item from the list
2755 ++idxtv->vval.v_number;
2756 if (list == NULL
2757 || idxtv->vval.v_number >= list->lv_len)
2758 {
2759 // past the end of the list, jump to "endfor"
2760 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2761 may_restore_cmdmod(&funclocal);
2762 }
2763 else if (list->lv_first == &range_list_item)
2764 {
2765 // non-materialized range() list
2766 tv = STACK_TV_BOT(0);
2767 tv->v_type = VAR_NUMBER;
2768 tv->v_lock = 0;
2769 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770 list, idxtv->vval.v_number, NULL);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002771 ++ectx.ec_stack.ga_len;
2772 }
2773 else
2774 {
2775 listitem_T *li = list_find(list,
2776 idxtv->vval.v_number);
2777
2778 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2779 ++ectx.ec_stack.ga_len;
2780 }
2781 }
2782 else if (ltv->v_type == VAR_STRING)
2783 {
2784 char_u *str = ltv->vval.v_string;
2785 int len = str == NULL ? 0 : (int)STRLEN(str);
2786
2787 // Push the next character from the string. The index
2788 // is for the last byte of the previous character.
2789 ++idxtv->vval.v_number;
2790 if (idxtv->vval.v_number >= len)
2791 {
2792 // past the end of the string, jump to "endfor"
2793 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2794 may_restore_cmdmod(&funclocal);
2795 }
2796 else
2797 {
2798 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2799
2800 tv = STACK_TV_BOT(0);
2801 tv->v_type = VAR_STRING;
2802 tv->vval.v_string = vim_strnsave(
2803 str + idxtv->vval.v_number, clen);
2804 ++ectx.ec_stack.ga_len;
2805 idxtv->vval.v_number += clen - 1;
2806 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 }
2808 else
2809 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002810 // TODO: support Blob
2811 semsg(_(e_for_loop_on_str_not_supported),
2812 vartype_name(ltv->v_type));
2813 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814 }
2815 }
2816 break;
2817
2818 // start of ":try" block
2819 case ISN_TRY:
2820 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002821 trycmd_T *trycmd = NULL;
2822
Bram Moolenaar270d0382020-05-15 21:42:53 +02002823 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 goto failed;
2825 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2826 + ectx.ec_trystack.ga_len;
2827 ++ectx.ec_trystack.ga_len;
2828 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002829 CLEAR_POINTER(trycmd);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002830 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002831 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002832 trycmd->tcd_catch_idx =
2833 iptr->isn_arg.try.try_ref->try_catch;
2834 trycmd->tcd_finally_idx =
2835 iptr->isn_arg.try.try_ref->try_finally;
2836 trycmd->tcd_endtry_idx =
2837 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 }
2839 break;
2840
2841 case ISN_PUSHEXC:
2842 if (current_exception == NULL)
2843 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002844 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 iemsg("Evaluating catch while current_exception is NULL");
2846 goto failed;
2847 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002848 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 goto failed;
2850 tv = STACK_TV_BOT(0);
2851 ++ectx.ec_stack.ga_len;
2852 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002853 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 tv->vval.v_string = vim_strsave(
2855 (char_u *)current_exception->value);
2856 break;
2857
2858 case ISN_CATCH:
2859 {
2860 garray_T *trystack = &ectx.ec_trystack;
2861
Bram Moolenaara91a7132021-03-25 21:12:15 +01002862 may_restore_cmdmod(&funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 if (trystack->ga_len > 0)
2864 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002865 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 + trystack->ga_len - 1;
2867 trycmd->tcd_caught = TRUE;
2868 }
2869 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002870 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 catch_exception(current_exception);
2872 }
2873 break;
2874
Bram Moolenaarc150c092021-02-13 15:02:46 +01002875 case ISN_TRYCONT:
2876 {
2877 garray_T *trystack = &ectx.ec_trystack;
2878 trycont_T *trycont = &iptr->isn_arg.trycont;
2879 int i;
2880 trycmd_T *trycmd;
2881 int iidx = trycont->tct_where;
2882
2883 if (trystack->ga_len < trycont->tct_levels)
2884 {
2885 siemsg("TRYCONT: expected %d levels, found %d",
2886 trycont->tct_levels, trystack->ga_len);
2887 goto failed;
2888 }
2889 // Make :endtry jump to any outer try block and the last
2890 // :endtry inside the loop to the loop start.
2891 for (i = trycont->tct_levels; i > 0; --i)
2892 {
2893 trycmd = ((trycmd_T *)trystack->ga_data)
2894 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002895 // Add one to tcd_cont to be able to jump to
2896 // instruction with index zero.
2897 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002898 iidx = trycmd->tcd_finally_idx == 0
2899 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002900 }
2901 // jump to :finally or :endtry of current try statement
2902 ectx.ec_iidx = iidx;
2903 }
2904 break;
2905
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002906 case ISN_FINALLY:
2907 {
2908 garray_T *trystack = &ectx.ec_trystack;
2909 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2910 + trystack->ga_len - 1;
2911
2912 // Reset the index to avoid a return statement jumps here
2913 // again.
2914 trycmd->tcd_finally_idx = 0;
2915 break;
2916 }
2917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 // end of ":try" block
2919 case ISN_ENDTRY:
2920 {
2921 garray_T *trystack = &ectx.ec_trystack;
2922
2923 if (trystack->ga_len > 0)
2924 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002925 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 --trystack->ga_len;
2928 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002929 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 trycmd = ((trycmd_T *)trystack->ga_data)
2931 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002932 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 {
2934 // discard the exception
2935 if (caught_stack == current_exception)
2936 caught_stack = caught_stack->caught;
2937 discard_current_exception();
2938 }
2939
2940 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002941 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002942
2943 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2944 {
2945 --ectx.ec_stack.ga_len;
2946 clear_tv(STACK_TV_BOT(0));
2947 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002948 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002949 // handling :continue: jump to outer try block or
2950 // start of the loop
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002951 ectx.ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 }
2953 }
2954 break;
2955
2956 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002957 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002958 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002959
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002960 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2961 {
2962 // throwing an exception while using "silent!" causes
2963 // the function to abort but not display an error.
2964 tv = STACK_TV_BOT(-1);
2965 clear_tv(tv);
2966 tv->v_type = VAR_NUMBER;
2967 tv->vval.v_number = 0;
2968 goto done;
2969 }
2970 --ectx.ec_stack.ga_len;
2971 tv = STACK_TV_BOT(0);
2972 if (tv->vval.v_string == NULL
2973 || *skipwhite(tv->vval.v_string) == NUL)
2974 {
2975 vim_free(tv->vval.v_string);
2976 SOURCING_LNUM = iptr->isn_lnum;
2977 emsg(_(e_throw_with_empty_string));
2978 goto failed;
2979 }
2980
2981 // Inside a "catch" we need to first discard the caught
2982 // exception.
2983 if (trystack->ga_len > 0)
2984 {
2985 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2986 + trystack->ga_len - 1;
2987 if (trycmd->tcd_caught && current_exception != NULL)
2988 {
2989 // discard the exception
2990 if (caught_stack == current_exception)
2991 caught_stack = caught_stack->caught;
2992 discard_current_exception();
2993 trycmd->tcd_caught = FALSE;
2994 }
2995 }
2996
2997 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2998 == FAIL)
2999 {
3000 vim_free(tv->vval.v_string);
3001 goto failed;
3002 }
3003 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 break;
3006
3007 // compare with special values
3008 case ISN_COMPAREBOOL:
3009 case ISN_COMPARESPECIAL:
3010 {
3011 typval_T *tv1 = STACK_TV_BOT(-2);
3012 typval_T *tv2 = STACK_TV_BOT(-1);
3013 varnumber_T arg1 = tv1->vval.v_number;
3014 varnumber_T arg2 = tv2->vval.v_number;
3015 int res;
3016
3017 switch (iptr->isn_arg.op.op_type)
3018 {
3019 case EXPR_EQUAL: res = arg1 == arg2; break;
3020 case EXPR_NEQUAL: res = arg1 != arg2; break;
3021 default: res = 0; break;
3022 }
3023
3024 --ectx.ec_stack.ga_len;
3025 tv1->v_type = VAR_BOOL;
3026 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3027 }
3028 break;
3029
3030 // Operation with two number arguments
3031 case ISN_OPNR:
3032 case ISN_COMPARENR:
3033 {
3034 typval_T *tv1 = STACK_TV_BOT(-2);
3035 typval_T *tv2 = STACK_TV_BOT(-1);
3036 varnumber_T arg1 = tv1->vval.v_number;
3037 varnumber_T arg2 = tv2->vval.v_number;
3038 varnumber_T res;
3039
3040 switch (iptr->isn_arg.op.op_type)
3041 {
3042 case EXPR_MULT: res = arg1 * arg2; break;
3043 case EXPR_DIV: res = arg1 / arg2; break;
3044 case EXPR_REM: res = arg1 % arg2; break;
3045 case EXPR_SUB: res = arg1 - arg2; break;
3046 case EXPR_ADD: res = arg1 + arg2; break;
3047
3048 case EXPR_EQUAL: res = arg1 == arg2; break;
3049 case EXPR_NEQUAL: res = arg1 != arg2; break;
3050 case EXPR_GREATER: res = arg1 > arg2; break;
3051 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3052 case EXPR_SMALLER: res = arg1 < arg2; break;
3053 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3054 default: res = 0; break;
3055 }
3056
3057 --ectx.ec_stack.ga_len;
3058 if (iptr->isn_type == ISN_COMPARENR)
3059 {
3060 tv1->v_type = VAR_BOOL;
3061 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3062 }
3063 else
3064 tv1->vval.v_number = res;
3065 }
3066 break;
3067
3068 // Computation with two float arguments
3069 case ISN_OPFLOAT:
3070 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003071#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 {
3073 typval_T *tv1 = STACK_TV_BOT(-2);
3074 typval_T *tv2 = STACK_TV_BOT(-1);
3075 float_T arg1 = tv1->vval.v_float;
3076 float_T arg2 = tv2->vval.v_float;
3077 float_T res = 0;
3078 int cmp = FALSE;
3079
3080 switch (iptr->isn_arg.op.op_type)
3081 {
3082 case EXPR_MULT: res = arg1 * arg2; break;
3083 case EXPR_DIV: res = arg1 / arg2; break;
3084 case EXPR_SUB: res = arg1 - arg2; break;
3085 case EXPR_ADD: res = arg1 + arg2; break;
3086
3087 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3088 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3089 case EXPR_GREATER: cmp = arg1 > arg2; break;
3090 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3091 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3092 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3093 default: cmp = 0; break;
3094 }
3095 --ectx.ec_stack.ga_len;
3096 if (iptr->isn_type == ISN_COMPAREFLOAT)
3097 {
3098 tv1->v_type = VAR_BOOL;
3099 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3100 }
3101 else
3102 tv1->vval.v_float = res;
3103 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003104#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 break;
3106
3107 case ISN_COMPARELIST:
3108 {
3109 typval_T *tv1 = STACK_TV_BOT(-2);
3110 typval_T *tv2 = STACK_TV_BOT(-1);
3111 list_T *arg1 = tv1->vval.v_list;
3112 list_T *arg2 = tv2->vval.v_list;
3113 int cmp = FALSE;
3114 int ic = iptr->isn_arg.op.op_ic;
3115
3116 switch (iptr->isn_arg.op.op_type)
3117 {
3118 case EXPR_EQUAL: cmp =
3119 list_equal(arg1, arg2, ic, FALSE); break;
3120 case EXPR_NEQUAL: cmp =
3121 !list_equal(arg1, arg2, ic, FALSE); break;
3122 case EXPR_IS: cmp = arg1 == arg2; break;
3123 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3124 default: cmp = 0; break;
3125 }
3126 --ectx.ec_stack.ga_len;
3127 clear_tv(tv1);
3128 clear_tv(tv2);
3129 tv1->v_type = VAR_BOOL;
3130 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3131 }
3132 break;
3133
3134 case ISN_COMPAREBLOB:
3135 {
3136 typval_T *tv1 = STACK_TV_BOT(-2);
3137 typval_T *tv2 = STACK_TV_BOT(-1);
3138 blob_T *arg1 = tv1->vval.v_blob;
3139 blob_T *arg2 = tv2->vval.v_blob;
3140 int cmp = FALSE;
3141
3142 switch (iptr->isn_arg.op.op_type)
3143 {
3144 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3145 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3146 case EXPR_IS: cmp = arg1 == arg2; break;
3147 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3148 default: cmp = 0; break;
3149 }
3150 --ectx.ec_stack.ga_len;
3151 clear_tv(tv1);
3152 clear_tv(tv2);
3153 tv1->v_type = VAR_BOOL;
3154 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3155 }
3156 break;
3157
3158 // TODO: handle separately
3159 case ISN_COMPARESTRING:
3160 case ISN_COMPAREDICT:
3161 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 case ISN_COMPAREANY:
3163 {
3164 typval_T *tv1 = STACK_TV_BOT(-2);
3165 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003166 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 int ic = iptr->isn_arg.op.op_ic;
3168
Bram Moolenaareb26f432020-09-14 16:50:05 +02003169 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003170 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 --ectx.ec_stack.ga_len;
3173 }
3174 break;
3175
3176 case ISN_ADDLIST:
3177 case ISN_ADDBLOB:
3178 {
3179 typval_T *tv1 = STACK_TV_BOT(-2);
3180 typval_T *tv2 = STACK_TV_BOT(-1);
3181
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003182 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 if (iptr->isn_type == ISN_ADDLIST)
3184 eval_addlist(tv1, tv2);
3185 else
3186 eval_addblob(tv1, tv2);
3187 clear_tv(tv2);
3188 --ectx.ec_stack.ga_len;
3189 }
3190 break;
3191
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003192 case ISN_LISTAPPEND:
3193 {
3194 typval_T *tv1 = STACK_TV_BOT(-2);
3195 typval_T *tv2 = STACK_TV_BOT(-1);
3196 list_T *l = tv1->vval.v_list;
3197
3198 // add an item to a list
3199 if (l == NULL)
3200 {
3201 SOURCING_LNUM = iptr->isn_lnum;
3202 emsg(_(e_cannot_add_to_null_list));
3203 goto on_error;
3204 }
3205 if (list_append_tv(l, tv2) == FAIL)
3206 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003207 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003208 --ectx.ec_stack.ga_len;
3209 }
3210 break;
3211
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003212 case ISN_BLOBAPPEND:
3213 {
3214 typval_T *tv1 = STACK_TV_BOT(-2);
3215 typval_T *tv2 = STACK_TV_BOT(-1);
3216 blob_T *b = tv1->vval.v_blob;
3217 int error = FALSE;
3218 varnumber_T n;
3219
3220 // add a number to a blob
3221 if (b == NULL)
3222 {
3223 SOURCING_LNUM = iptr->isn_lnum;
3224 emsg(_(e_cannot_add_to_null_blob));
3225 goto on_error;
3226 }
3227 n = tv_get_number_chk(tv2, &error);
3228 if (error)
3229 goto on_error;
3230 ga_append(&b->bv_ga, (int)n);
3231 --ectx.ec_stack.ga_len;
3232 }
3233 break;
3234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 // Computation with two arguments of unknown type
3236 case ISN_OPANY:
3237 {
3238 typval_T *tv1 = STACK_TV_BOT(-2);
3239 typval_T *tv2 = STACK_TV_BOT(-1);
3240 varnumber_T n1, n2;
3241#ifdef FEAT_FLOAT
3242 float_T f1 = 0, f2 = 0;
3243#endif
3244 int error = FALSE;
3245
3246 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3247 {
3248 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3249 {
3250 eval_addlist(tv1, tv2);
3251 clear_tv(tv2);
3252 --ectx.ec_stack.ga_len;
3253 break;
3254 }
3255 else if (tv1->v_type == VAR_BLOB
3256 && tv2->v_type == VAR_BLOB)
3257 {
3258 eval_addblob(tv1, tv2);
3259 clear_tv(tv2);
3260 --ectx.ec_stack.ga_len;
3261 break;
3262 }
3263 }
3264#ifdef FEAT_FLOAT
3265 if (tv1->v_type == VAR_FLOAT)
3266 {
3267 f1 = tv1->vval.v_float;
3268 n1 = 0;
3269 }
3270 else
3271#endif
3272 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003273 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 n1 = tv_get_number_chk(tv1, &error);
3275 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003276 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277#ifdef FEAT_FLOAT
3278 if (tv2->v_type == VAR_FLOAT)
3279 f1 = n1;
3280#endif
3281 }
3282#ifdef FEAT_FLOAT
3283 if (tv2->v_type == VAR_FLOAT)
3284 {
3285 f2 = tv2->vval.v_float;
3286 n2 = 0;
3287 }
3288 else
3289#endif
3290 {
3291 n2 = tv_get_number_chk(tv2, &error);
3292 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003293 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294#ifdef FEAT_FLOAT
3295 if (tv1->v_type == VAR_FLOAT)
3296 f2 = n2;
3297#endif
3298 }
3299#ifdef FEAT_FLOAT
3300 // if there is a float on either side the result is a float
3301 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3302 {
3303 switch (iptr->isn_arg.op.op_type)
3304 {
3305 case EXPR_MULT: f1 = f1 * f2; break;
3306 case EXPR_DIV: f1 = f1 / f2; break;
3307 case EXPR_SUB: f1 = f1 - f2; break;
3308 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003309 default: SOURCING_LNUM = iptr->isn_lnum;
3310 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003311 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 }
3313 clear_tv(tv1);
3314 clear_tv(tv2);
3315 tv1->v_type = VAR_FLOAT;
3316 tv1->vval.v_float = f1;
3317 --ectx.ec_stack.ga_len;
3318 }
3319 else
3320#endif
3321 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003322 int failed = FALSE;
3323
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 switch (iptr->isn_arg.op.op_type)
3325 {
3326 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003327 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3328 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003329 goto on_error;
3330 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 case EXPR_SUB: n1 = n1 - n2; break;
3332 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003333 default: n1 = num_modulus(n1, n2, &failed);
3334 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003335 goto on_error;
3336 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 }
3338 clear_tv(tv1);
3339 clear_tv(tv2);
3340 tv1->v_type = VAR_NUMBER;
3341 tv1->vval.v_number = n1;
3342 --ectx.ec_stack.ga_len;
3343 }
3344 }
3345 break;
3346
3347 case ISN_CONCAT:
3348 {
3349 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3350 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3351 char_u *res;
3352
3353 res = concat_str(str1, str2);
3354 clear_tv(STACK_TV_BOT(-2));
3355 clear_tv(STACK_TV_BOT(-1));
3356 --ectx.ec_stack.ga_len;
3357 STACK_TV_BOT(-1)->vval.v_string = res;
3358 }
3359 break;
3360
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003361 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003362 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003363 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003364 int is_slice = iptr->isn_type == ISN_STRSLICE;
3365 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003366 char_u *res;
3367
3368 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003369 // string slice: string is at stack-3, first index at
3370 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003371 if (is_slice)
3372 {
3373 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003374 n1 = tv->vval.v_number;
3375 }
3376
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003377 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003378 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003379
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003380 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003381 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003382 if (is_slice)
3383 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003384 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003385 else
3386 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003387 // single character (including composing characters).
3388 // If the index is too big or negative the result is
3389 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003390 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003391 vim_free(tv->vval.v_string);
3392 tv->vval.v_string = res;
3393 }
3394 break;
3395
3396 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003397 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 {
Bram Moolenaared591872020-08-15 22:14:53 +02003399 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003401 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402
3403 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003404 // list slice: list is at stack-3, indexes at stack-2 and
3405 // stack-1
3406 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 list = tv->vval.v_list;
3408
3409 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003410 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003412
3413 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 {
Bram Moolenaared591872020-08-15 22:14:53 +02003415 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003416 n1 = tv->vval.v_number;
3417 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 }
Bram Moolenaared591872020-08-15 22:14:53 +02003419
3420 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003421 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003422 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003423 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3424 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003425 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 }
3427 break;
3428
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003429 case ISN_ANYINDEX:
3430 case ISN_ANYSLICE:
3431 {
3432 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3433 typval_T *var1, *var2;
3434 int res;
3435
3436 // index: composite is at stack-2, index at stack-1
3437 // slice: composite is at stack-3, indexes at stack-2 and
3438 // stack-1
3439 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003440 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003441 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3442 goto on_error;
3443 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3444 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003445 res = eval_index_inner(tv, is_slice, var1, var2,
3446 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003447 clear_tv(var1);
3448 if (is_slice)
3449 clear_tv(var2);
3450 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3451 if (res == FAIL)
3452 goto on_error;
3453 }
3454 break;
3455
Bram Moolenaar9af78762020-06-16 11:34:42 +02003456 case ISN_SLICE:
3457 {
3458 list_T *list;
3459 int count = iptr->isn_arg.number;
3460
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003461 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003462 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003463 list = tv->vval.v_list;
3464
3465 // no error for short list, expect it to be checked earlier
3466 if (list != NULL && list->lv_len >= count)
3467 {
3468 list_T *newlist = list_slice(list,
3469 count, list->lv_len - 1);
3470
3471 if (newlist != NULL)
3472 {
3473 list_unref(list);
3474 tv->vval.v_list = newlist;
3475 ++newlist->lv_refcount;
3476 }
3477 }
3478 }
3479 break;
3480
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003481 case ISN_GETITEM:
3482 {
3483 listitem_T *li;
3484 int index = iptr->isn_arg.number;
3485
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003486 // Get list item: list is at stack-1, push item.
3487 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003488 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003489 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003490
3491 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3492 goto failed;
3493 ++ectx.ec_stack.ga_len;
3494 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003495
3496 // Useful when used in unpack assignment. Reset at
3497 // ISN_DROP.
3498 where.wt_index = index + 1;
3499 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003500 }
3501 break;
3502
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 case ISN_MEMBER:
3504 {
3505 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003506 char_u *key;
3507 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003508 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003509
3510 // dict member: dict is at stack-2, key at stack-1
3511 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003512 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003513 dict = tv->vval.v_dict;
3514
3515 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003516 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003517 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003518 if (key == NULL)
3519 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003520
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003521 if ((di = dict_find(dict, key, -1)) == NULL)
3522 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003523 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003524 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003525
3526 // If :silent! is used we will continue, make sure the
3527 // stack contents makes sense.
3528 clear_tv(tv);
3529 --ectx.ec_stack.ga_len;
3530 tv = STACK_TV_BOT(-1);
3531 clear_tv(tv);
3532 tv->v_type = VAR_NUMBER;
3533 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003534 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003535 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003536 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003537 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003538 // Clear the dict only after getting the item, to avoid
3539 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003540 tv = STACK_TV_BOT(-1);
3541 temp_tv = *tv;
3542 copy_tv(&di->di_tv, tv);
3543 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003544 }
3545 break;
3546
3547 // dict member with string key
3548 case ISN_STRINGMEMBER:
3549 {
3550 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003552 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553
3554 tv = STACK_TV_BOT(-1);
3555 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3556 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003557 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003559 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 }
3561 dict = tv->vval.v_dict;
3562
3563 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3564 == NULL)
3565 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003566 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003568 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003570 // Clear the dict after getting the item, to avoid that it
3571 // make the item invalid.
3572 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003574 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 }
3576 break;
3577
3578 case ISN_NEGATENR:
3579 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003580 if (tv->v_type != VAR_NUMBER
3581#ifdef FEAT_FLOAT
3582 && tv->v_type != VAR_FLOAT
3583#endif
3584 )
3585 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003586 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003587 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003588 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003589 }
3590#ifdef FEAT_FLOAT
3591 if (tv->v_type == VAR_FLOAT)
3592 tv->vval.v_float = -tv->vval.v_float;
3593 else
3594#endif
3595 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 break;
3597
3598 case ISN_CHECKNR:
3599 {
3600 int error = FALSE;
3601
3602 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003603 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003605 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 (void)tv_get_number_chk(tv, &error);
3607 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003608 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 }
3610 break;
3611
3612 case ISN_CHECKTYPE:
3613 {
3614 checktype_T *ct = &iptr->isn_arg.type;
3615
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003616 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003617 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003618 if (!where.wt_variable)
3619 where.wt_index = ct->ct_arg_idx;
3620 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003621 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003622 if (!where.wt_variable)
3623 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003624
3625 // number 0 is FALSE, number 1 is TRUE
3626 if (tv->v_type == VAR_NUMBER
3627 && ct->ct_type->tt_type == VAR_BOOL
3628 && (tv->vval.v_number == 0
3629 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003631 tv->v_type = VAR_BOOL;
3632 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003633 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 }
3635 }
3636 break;
3637
Bram Moolenaar9af78762020-06-16 11:34:42 +02003638 case ISN_CHECKLEN:
3639 {
3640 int min_len = iptr->isn_arg.checklen.cl_min_len;
3641 list_T *list = NULL;
3642
3643 tv = STACK_TV_BOT(-1);
3644 if (tv->v_type == VAR_LIST)
3645 list = tv->vval.v_list;
3646 if (list == NULL || list->lv_len < min_len
3647 || (list->lv_len > min_len
3648 && !iptr->isn_arg.checklen.cl_more_OK))
3649 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003650 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003651 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003652 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003653 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003654 }
3655 }
3656 break;
3657
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003658 case ISN_SETTYPE:
3659 {
3660 checktype_T *ct = &iptr->isn_arg.type;
3661
3662 tv = STACK_TV_BOT(-1);
3663 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3664 {
3665 free_type(tv->vval.v_dict->dv_type);
3666 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3667 }
3668 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3669 {
3670 free_type(tv->vval.v_list->lv_type);
3671 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3672 }
3673 }
3674 break;
3675
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003677 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 {
3679 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003680 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681
3682 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003683 if (iptr->isn_type == ISN_2BOOL)
3684 {
3685 n = tv2bool(tv);
3686 if (iptr->isn_arg.number) // invert
3687 n = !n;
3688 }
3689 else
3690 {
3691 SOURCING_LNUM = iptr->isn_lnum;
3692 n = tv_get_bool_chk(tv, &error);
3693 if (error)
3694 goto on_error;
3695 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 clear_tv(tv);
3697 tv->v_type = VAR_BOOL;
3698 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3699 }
3700 break;
3701
3702 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003703 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003704 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003705 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3706 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3707 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 break;
3709
Bram Moolenaar08597872020-12-10 19:43:40 +01003710 case ISN_RANGE:
3711 {
3712 exarg_T ea;
3713 char *errormsg;
3714
Bram Moolenaarece0b872021-01-08 20:40:45 +01003715 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003716 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003717 ea.addr_type = ADDR_LINES;
3718 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003719 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003720 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003721 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003722
3723 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3724 goto failed;
3725 ++ectx.ec_stack.ga_len;
3726 tv = STACK_TV_BOT(-1);
3727 tv->v_type = VAR_NUMBER;
3728 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003729 if (ea.addr_count == 0)
3730 tv->vval.v_number = curwin->w_cursor.lnum;
3731 else
3732 tv->vval.v_number = ea.line2;
3733 }
3734 break;
3735
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003736 case ISN_PUT:
3737 {
3738 int regname = iptr->isn_arg.put.put_regname;
3739 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3740 char_u *expr = NULL;
3741 int dir = FORWARD;
3742
Bram Moolenaar08597872020-12-10 19:43:40 +01003743 if (lnum < -2)
3744 {
3745 // line number was put on the stack by ISN_RANGE
3746 tv = STACK_TV_BOT(-1);
3747 curwin->w_cursor.lnum = tv->vval.v_number;
3748 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3749 dir = BACKWARD;
3750 --ectx.ec_stack.ga_len;
3751 }
3752 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003753 // :put! above cursor
3754 dir = BACKWARD;
3755 else if (lnum >= 0)
3756 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003757
3758 if (regname == '=')
3759 {
3760 tv = STACK_TV_BOT(-1);
3761 if (tv->v_type == VAR_STRING)
3762 expr = tv->vval.v_string;
3763 else
3764 {
3765 expr = typval2string(tv, TRUE); // allocates value
3766 clear_tv(tv);
3767 }
3768 --ectx.ec_stack.ga_len;
3769 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003770 check_cursor();
3771 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3772 vim_free(expr);
3773 }
3774 break;
3775
Bram Moolenaar02194d22020-10-24 23:08:38 +02003776 case ISN_CMDMOD:
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003777 funclocal.floc_save_cmdmod = cmdmod;
3778 funclocal.floc_restore_cmdmod = TRUE;
3779 funclocal.floc_restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003780 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3781 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003782 break;
3783
Bram Moolenaar02194d22020-10-24 23:08:38 +02003784 case ISN_CMDMOD_REV:
3785 // filter regprog is owned by the instruction, don't free it
3786 cmdmod.cmod_filter_regmatch.regprog = NULL;
3787 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003788 cmdmod = funclocal.floc_save_cmdmod;
3789 funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003790 break;
3791
Bram Moolenaar792f7862020-11-23 08:31:18 +01003792 case ISN_UNPACK:
3793 {
3794 int count = iptr->isn_arg.unpack.unp_count;
3795 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3796 list_T *l;
3797 listitem_T *li;
3798 int i;
3799
3800 // Check there is a valid list to unpack.
3801 tv = STACK_TV_BOT(-1);
3802 if (tv->v_type != VAR_LIST)
3803 {
3804 SOURCING_LNUM = iptr->isn_lnum;
3805 emsg(_(e_for_argument_must_be_sequence_of_lists));
3806 goto on_error;
3807 }
3808 l = tv->vval.v_list;
3809 if (l == NULL
3810 || l->lv_len < (semicolon ? count - 1 : count))
3811 {
3812 SOURCING_LNUM = iptr->isn_lnum;
3813 emsg(_(e_list_value_does_not_have_enough_items));
3814 goto on_error;
3815 }
3816 else if (!semicolon && l->lv_len > count)
3817 {
3818 SOURCING_LNUM = iptr->isn_lnum;
3819 emsg(_(e_list_value_has_more_items_than_targets));
3820 goto on_error;
3821 }
3822
3823 CHECK_LIST_MATERIALIZE(l);
3824 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3825 goto failed;
3826 ectx.ec_stack.ga_len += count - 1;
3827
3828 // Variable after semicolon gets a list with the remaining
3829 // items.
3830 if (semicolon)
3831 {
3832 list_T *rem_list =
3833 list_alloc_with_items(l->lv_len - count + 1);
3834
3835 if (rem_list == NULL)
3836 goto failed;
3837 tv = STACK_TV_BOT(-count);
3838 tv->vval.v_list = rem_list;
3839 ++rem_list->lv_refcount;
3840 tv->v_lock = 0;
3841 li = l->lv_first;
3842 for (i = 0; i < count - 1; ++i)
3843 li = li->li_next;
3844 for (i = 0; li != NULL; ++i)
3845 {
3846 list_set_item(rem_list, i, &li->li_tv);
3847 li = li->li_next;
3848 }
3849 --count;
3850 }
3851
3852 // Produce the values in reverse order, first item last.
3853 li = l->lv_first;
3854 for (i = 0; i < count; ++i)
3855 {
3856 tv = STACK_TV_BOT(-i - 1);
3857 copy_tv(&li->li_tv, tv);
3858 li = li->li_next;
3859 }
3860
3861 list_unref(l);
3862 }
3863 break;
3864
Bram Moolenaarb2049902021-01-24 12:53:53 +01003865 case ISN_PROF_START:
3866 case ISN_PROF_END:
3867 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003868#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003869 funccall_T cookie;
3870 ufunc_T *cur_ufunc =
3871 (((dfunc_T *)def_functions.ga_data)
3872 + ectx.ec_dfunc_idx)->df_ufunc;
3873
3874 cookie.func = cur_ufunc;
3875 if (iptr->isn_type == ISN_PROF_START)
3876 {
3877 func_line_start(&cookie, iptr->isn_lnum);
3878 // if we get here the instruction is executed
3879 func_line_exec(&cookie);
3880 }
3881 else
3882 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003883#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003884 }
3885 break;
3886
Bram Moolenaar389df252020-07-09 21:20:47 +02003887 case ISN_SHUFFLE:
3888 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003889 typval_T tmp_tv;
3890 int item = iptr->isn_arg.shuffle.shfl_item;
3891 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003892
3893 tmp_tv = *STACK_TV_BOT(-item);
3894 for ( ; up > 0 && item > 1; --up)
3895 {
3896 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3897 --item;
3898 }
3899 *STACK_TV_BOT(-item) = tmp_tv;
3900 }
3901 break;
3902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 case ISN_DROP:
3904 --ectx.ec_stack.ga_len;
3905 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003906 where.wt_index = 0;
3907 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 break;
3909 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003910 continue;
3911
Bram Moolenaard032f342020-07-18 18:13:02 +02003912func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003913 // Restore previous function. If the frame pointer is where we started
3914 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003915 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003916 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003917
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003918 if (func_return(&funclocal, &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003919 // only fails when out of memory
3920 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003921 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003922
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003923on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003924 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003925 // If "emsg_silent" is set then ignore the error, unless it was set
3926 // when calling the function.
3927 if (did_emsg_cumul + did_emsg == did_emsg_before
3928 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003929 {
3930 // If a sequence of instructions causes an error while ":silent!"
3931 // was used, restore the stack length and jump ahead to restoring
3932 // the cmdmod.
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003933 if (funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003934 {
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003935 while (ectx.ec_stack.ga_len
3936 > funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003937 {
3938 --ectx.ec_stack.ga_len;
3939 clear_tv(STACK_TV_BOT(0));
3940 }
3941 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3942 ++ectx.ec_iidx;
3943 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003944 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003945 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003946on_fatal_error:
3947 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003948 // If we are not inside a try-catch started here, abort execution.
3949 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003950 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 }
3952
3953done:
3954 // function finished, get result from the stack.
3955 tv = STACK_TV_BOT(-1);
3956 *rettv = *tv;
3957 tv->v_type = VAR_UNKNOWN;
3958 ret = OK;
3959
3960failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003961 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003962 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003963 func_return(&funclocal, &ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003964
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003965 // Deal with any remaining closures, they may be in use somewhere.
3966 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003967 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003968 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003969 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3970 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003971
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003972 estack_pop();
3973 current_sctx = save_current_sctx;
3974
Bram Moolenaarc970e422021-03-17 15:03:04 +01003975 // TODO: when is it safe to delete the function if it is no longer used?
3976 --ufunc->uf_calls;
3977
Bram Moolenaar352134b2020-10-17 22:04:08 +02003978 if (*msg_list != NULL && saved_msg_list != NULL)
3979 {
3980 msglist_T **plist = saved_msg_list;
3981
3982 // Append entries from the current msg_list (uncaught exceptions) to
3983 // the saved msg_list.
3984 while (*plist != NULL)
3985 plist = &(*plist)->next;
3986
3987 *plist = *msg_list;
3988 }
3989 msg_list = saved_msg_list;
3990
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003991 if (funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02003992 {
3993 cmdmod.cmod_filter_regmatch.regprog = NULL;
3994 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003995 cmdmod = funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003996 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003997 emsg_silent_def = save_emsg_silent_def;
3998 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003999
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004000failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004001 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4003 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004006 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004007
Bram Moolenaar0186e582021-01-10 18:33:11 +01004008 while (ectx.ec_outer != NULL)
4009 {
4010 outer_T *up = ectx.ec_outer->out_up_is_copy
4011 ? NULL : ectx.ec_outer->out_up;
4012
4013 vim_free(ectx.ec_outer);
4014 ectx.ec_outer = up;
4015 }
4016
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004017 // Not sure if this is necessary.
4018 suppress_errthrow = save_suppress_errthrow;
4019
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004020 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004021 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004022 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004023 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 return ret;
4025}
4026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004028 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01004029 * We don't really need this at runtime, but we do have tests that require it,
4030 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 */
4032 void
4033ex_disassemble(exarg_T *eap)
4034{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01004035 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01004036 char_u *fname;
4037 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 dfunc_T *dfunc;
4039 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004040 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 int current;
4042 int line_idx = 0;
4043 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004044 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045
Bram Moolenaarbfd65582020-07-13 18:18:00 +02004046 if (STRNCMP(arg, "<lambda>", 8) == 0)
4047 {
4048 arg += 8;
4049 (void)getdigits(&arg);
4050 fname = vim_strnsave(eap->arg, arg - eap->arg);
4051 }
4052 else
4053 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004054 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01004055 if (fname == NULL)
4056 {
4057 semsg(_(e_invarg2), eap->arg);
4058 return;
4059 }
4060
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004061 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004062 if (ufunc == NULL)
4063 {
4064 char_u *p = untrans_function_name(fname);
4065
4066 if (p != NULL)
4067 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004068 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004069 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01004070 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 if (ufunc == NULL)
4072 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004073 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 return;
4075 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01004076 if (func_needs_compiling(ufunc, eap->forceit)
4077 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02004078 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004079 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004081 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 return;
4083 }
4084 if (ufunc->uf_name_exp != NULL)
4085 msg((char *)ufunc->uf_name_exp);
4086 else
4087 msg((char *)ufunc->uf_name);
4088
4089 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01004090#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004091 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
4092 instr_count = eap->forceit ? dfunc->df_instr_prof_count
4093 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01004094#else
4095 instr = dfunc->df_instr;
4096 instr_count = dfunc->df_instr_count;
4097#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004098 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 {
4100 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004101 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102
4103 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
4104 {
4105 if (current > prev_current)
4106 {
4107 msg_puts("\n\n");
4108 prev_current = current;
4109 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004110 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4111 if (line != NULL)
4112 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 }
4114
4115 switch (iptr->isn_type)
4116 {
4117 case ISN_EXEC:
4118 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
4119 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004120 case ISN_EXECCONCAT:
4121 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004122 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004123 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 case ISN_ECHO:
4125 {
4126 echo_T *echo = &iptr->isn_arg.echo;
4127
4128 smsg("%4d %s %d", current,
4129 echo->echo_with_white ? "ECHO" : "ECHON",
4130 echo->echo_count);
4131 }
4132 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01004133 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01004134 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004135 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01004136 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004137 case ISN_ECHOMSG:
4138 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004139 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004140 break;
4141 case ISN_ECHOERR:
4142 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004143 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004144 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004146 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004147 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004148 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004149 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004150 + STACK_FRAME_SIZE));
4151 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004152 smsg("%4d LOAD $%lld", current,
4153 (varnumber_T)(iptr->isn_arg.number));
4154 }
4155 break;
4156 case ISN_LOADOUTER:
4157 {
4158 if (iptr->isn_arg.number < 0)
4159 smsg("%4d LOADOUTER level %d arg[%d]", current,
4160 iptr->isn_arg.outer.outer_depth,
4161 iptr->isn_arg.outer.outer_idx
4162 + STACK_FRAME_SIZE);
4163 else
4164 smsg("%4d LOADOUTER level %d $%d", current,
4165 iptr->isn_arg.outer.outer_depth,
4166 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004167 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 break;
4169 case ISN_LOADV:
4170 smsg("%4d LOADV v:%s", current,
4171 get_vim_var_name(iptr->isn_arg.number));
4172 break;
4173 case ISN_LOADSCRIPT:
4174 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004175 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4176 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004178 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004180 smsg("%4d LOADSCRIPT %s-%d from %s", current,
4181 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004182 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004183 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 }
4185 break;
4186 case ISN_LOADS:
4187 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004188 scriptitem_T *si = SCRIPT_ITEM(
4189 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190
4191 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004192 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 }
4194 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004195 case ISN_LOADAUTO:
4196 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
4197 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 case ISN_LOADG:
4199 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
4200 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004201 case ISN_LOADB:
4202 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
4203 break;
4204 case ISN_LOADW:
4205 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
4206 break;
4207 case ISN_LOADT:
4208 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
4209 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004210 case ISN_LOADGDICT:
4211 smsg("%4d LOAD g:", current);
4212 break;
4213 case ISN_LOADBDICT:
4214 smsg("%4d LOAD b:", current);
4215 break;
4216 case ISN_LOADWDICT:
4217 smsg("%4d LOAD w:", current);
4218 break;
4219 case ISN_LOADTDICT:
4220 smsg("%4d LOAD t:", current);
4221 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 case ISN_LOADOPT:
4223 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
4224 break;
4225 case ISN_LOADENV:
4226 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
4227 break;
4228 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004229 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230 break;
4231
4232 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01004233 if (iptr->isn_arg.number < 0)
4234 smsg("%4d STORE arg[%lld]", current,
4235 iptr->isn_arg.number + STACK_FRAME_SIZE);
4236 else
4237 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
4238 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004239 case ISN_STOREOUTER:
4240 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004241 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004242 smsg("%4d STOREOUTEr level %d arg[%d]", current,
4243 iptr->isn_arg.outer.outer_depth,
4244 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004245 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004246 smsg("%4d STOREOUTER level %d $%d", current,
4247 iptr->isn_arg.outer.outer_depth,
4248 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004249 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004251 case ISN_STOREV:
4252 smsg("%4d STOREV v:%s", current,
4253 get_vim_var_name(iptr->isn_arg.number));
4254 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004255 case ISN_STOREAUTO:
4256 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
4257 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004259 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
4260 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004261 case ISN_STOREB:
4262 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
4263 break;
4264 case ISN_STOREW:
4265 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
4266 break;
4267 case ISN_STORET:
4268 smsg("%4d STORET %s", current, iptr->isn_arg.string);
4269 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004270 case ISN_STORES:
4271 {
4272 scriptitem_T *si = SCRIPT_ITEM(
4273 iptr->isn_arg.loadstore.ls_sid);
4274
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004275 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004276 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004277 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 break;
4279 case ISN_STORESCRIPT:
4280 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004281 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4282 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004284 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004286 smsg("%4d STORESCRIPT %s-%d in %s", current,
4287 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004288 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004289 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 }
4291 break;
4292 case ISN_STOREOPT:
4293 smsg("%4d STOREOPT &%s", current,
4294 iptr->isn_arg.storeopt.so_name);
4295 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004296 case ISN_STOREENV:
4297 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4298 break;
4299 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004300 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004301 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 case ISN_STORENR:
4303 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004304 iptr->isn_arg.storenr.stnr_val,
4305 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 break;
4307
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004308 case ISN_STOREINDEX:
4309 switch (iptr->isn_arg.vartype)
4310 {
4311 case VAR_LIST:
4312 smsg("%4d STORELIST", current);
4313 break;
4314 case VAR_DICT:
4315 smsg("%4d STOREDICT", current);
4316 break;
4317 case VAR_ANY:
4318 smsg("%4d STOREINDEX", current);
4319 break;
4320 default: break;
4321 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004322 break;
4323
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 // constants
4325 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004326 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004327 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 break;
4329 case ISN_PUSHBOOL:
4330 case ISN_PUSHSPEC:
4331 smsg("%4d PUSH %s", current,
4332 get_var_special_name(iptr->isn_arg.number));
4333 break;
4334 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004335#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004337#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 break;
4339 case ISN_PUSHS:
4340 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4341 break;
4342 case ISN_PUSHBLOB:
4343 {
4344 char_u *r;
4345 char_u numbuf[NUMBUFLEN];
4346 char_u *tofree;
4347
4348 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004349 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 vim_free(tofree);
4351 }
4352 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004353 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004354 {
4355 char *name = (char *)iptr->isn_arg.string;
4356
4357 smsg("%4d PUSHFUNC \"%s\"", current,
4358 name == NULL ? "[none]" : name);
4359 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004360 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004361 case ISN_PUSHCHANNEL:
4362#ifdef FEAT_JOB_CHANNEL
4363 {
4364 channel_T *channel = iptr->isn_arg.channel;
4365
4366 smsg("%4d PUSHCHANNEL %d", current,
4367 channel == NULL ? 0 : channel->ch_id);
4368 }
4369#endif
4370 break;
4371 case ISN_PUSHJOB:
4372#ifdef FEAT_JOB_CHANNEL
4373 {
4374 typval_T tv;
4375 char_u *name;
4376
4377 tv.v_type = VAR_JOB;
4378 tv.vval.v_job = iptr->isn_arg.job;
4379 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004380 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004381 }
4382#endif
4383 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 case ISN_PUSHEXC:
4385 smsg("%4d PUSH v:exception", current);
4386 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004387 case ISN_UNLET:
4388 smsg("%4d UNLET%s %s", current,
4389 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4390 iptr->isn_arg.unlet.ul_name);
4391 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004392 case ISN_UNLETENV:
4393 smsg("%4d UNLETENV%s $%s", current,
4394 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4395 iptr->isn_arg.unlet.ul_name);
4396 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004397 case ISN_UNLETINDEX:
4398 smsg("%4d UNLETINDEX", current);
4399 break;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004400 case ISN_UNLETRANGE:
4401 smsg("%4d UNLETRANGE", current);
4402 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004403 case ISN_LOCKCONST:
4404 smsg("%4d LOCKCONST", current);
4405 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004407 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004408 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 break;
4410 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004411 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004412 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 break;
4414
4415 // function call
4416 case ISN_BCALL:
4417 {
4418 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4419
4420 smsg("%4d BCALL %s(argc %d)", current,
4421 internal_func_name(cbfunc->cbf_idx),
4422 cbfunc->cbf_argcount);
4423 }
4424 break;
4425 case ISN_DCALL:
4426 {
4427 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4428 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4429 + cdfunc->cdf_idx;
4430
4431 smsg("%4d DCALL %s(argc %d)", current,
4432 df->df_ufunc->uf_name_exp != NULL
4433 ? df->df_ufunc->uf_name_exp
4434 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4435 }
4436 break;
4437 case ISN_UCALL:
4438 {
4439 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4440
4441 smsg("%4d UCALL %s(argc %d)", current,
4442 cufunc->cuf_name, cufunc->cuf_argcount);
4443 }
4444 break;
4445 case ISN_PCALL:
4446 {
4447 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4448
4449 smsg("%4d PCALL%s (argc %d)", current,
4450 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4451 }
4452 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004453 case ISN_PCALL_END:
4454 smsg("%4d PCALL end", current);
4455 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 case ISN_RETURN:
4457 smsg("%4d RETURN", current);
4458 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004459 case ISN_RETURN_ZERO:
4460 smsg("%4d RETURN 0", current);
4461 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 case ISN_FUNCREF:
4463 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004464 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004466 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004468 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 }
4470 break;
4471
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004472 case ISN_NEWFUNC:
4473 {
4474 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4475
4476 smsg("%4d NEWFUNC %s %s", current,
4477 newfunc->nf_lambda, newfunc->nf_global);
4478 }
4479 break;
4480
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004481 case ISN_DEF:
4482 {
4483 char_u *name = iptr->isn_arg.string;
4484
4485 smsg("%4d DEF %s", current,
4486 name == NULL ? (char_u *)"" : name);
4487 }
4488 break;
4489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 case ISN_JUMP:
4491 {
4492 char *when = "?";
4493
4494 switch (iptr->isn_arg.jump.jump_when)
4495 {
4496 case JUMP_ALWAYS:
4497 when = "JUMP";
4498 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 case JUMP_AND_KEEP_IF_TRUE:
4500 when = "JUMP_AND_KEEP_IF_TRUE";
4501 break;
4502 case JUMP_IF_FALSE:
4503 when = "JUMP_IF_FALSE";
4504 break;
4505 case JUMP_AND_KEEP_IF_FALSE:
4506 when = "JUMP_AND_KEEP_IF_FALSE";
4507 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004508 case JUMP_IF_COND_FALSE:
4509 when = "JUMP_IF_COND_FALSE";
4510 break;
4511 case JUMP_IF_COND_TRUE:
4512 when = "JUMP_IF_COND_TRUE";
4513 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004515 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 iptr->isn_arg.jump.jump_where);
4517 }
4518 break;
4519
4520 case ISN_FOR:
4521 {
4522 forloop_T *forloop = &iptr->isn_arg.forloop;
4523
4524 smsg("%4d FOR $%d -> %d", current,
4525 forloop->for_idx, forloop->for_end);
4526 }
4527 break;
4528
4529 case ISN_TRY:
4530 {
4531 try_T *try = &iptr->isn_arg.try;
4532
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004533 if (try->try_ref->try_finally == 0)
4534 smsg("%4d TRY catch -> %d, endtry -> %d",
4535 current,
4536 try->try_ref->try_catch,
4537 try->try_ref->try_endtry);
4538 else
4539 smsg("%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4540 current,
4541 try->try_ref->try_catch,
4542 try->try_ref->try_finally,
4543 try->try_ref->try_endtry);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004544 }
4545 break;
4546 case ISN_CATCH:
4547 // TODO
4548 smsg("%4d CATCH", current);
4549 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004550 case ISN_TRYCONT:
4551 {
4552 trycont_T *trycont = &iptr->isn_arg.trycont;
4553
4554 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4555 trycont->tct_levels,
4556 trycont->tct_levels == 1 ? "" : "s",
4557 trycont->tct_where);
4558 }
4559 break;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004560 case ISN_FINALLY:
4561 smsg("%4d FINALLY", current);
4562 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 case ISN_ENDTRY:
4564 smsg("%4d ENDTRY", current);
4565 break;
4566 case ISN_THROW:
4567 smsg("%4d THROW", current);
4568 break;
4569
4570 // expression operations on number
4571 case ISN_OPNR:
4572 case ISN_OPFLOAT:
4573 case ISN_OPANY:
4574 {
4575 char *what;
4576 char *ins;
4577
4578 switch (iptr->isn_arg.op.op_type)
4579 {
4580 case EXPR_MULT: what = "*"; break;
4581 case EXPR_DIV: what = "/"; break;
4582 case EXPR_REM: what = "%"; break;
4583 case EXPR_SUB: what = "-"; break;
4584 case EXPR_ADD: what = "+"; break;
4585 default: what = "???"; break;
4586 }
4587 switch (iptr->isn_type)
4588 {
4589 case ISN_OPNR: ins = "OPNR"; break;
4590 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4591 case ISN_OPANY: ins = "OPANY"; break;
4592 default: ins = "???"; break;
4593 }
4594 smsg("%4d %s %s", current, ins, what);
4595 }
4596 break;
4597
4598 case ISN_COMPAREBOOL:
4599 case ISN_COMPARESPECIAL:
4600 case ISN_COMPARENR:
4601 case ISN_COMPAREFLOAT:
4602 case ISN_COMPARESTRING:
4603 case ISN_COMPAREBLOB:
4604 case ISN_COMPARELIST:
4605 case ISN_COMPAREDICT:
4606 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 case ISN_COMPAREANY:
4608 {
4609 char *p;
4610 char buf[10];
4611 char *type;
4612
4613 switch (iptr->isn_arg.op.op_type)
4614 {
4615 case EXPR_EQUAL: p = "=="; break;
4616 case EXPR_NEQUAL: p = "!="; break;
4617 case EXPR_GREATER: p = ">"; break;
4618 case EXPR_GEQUAL: p = ">="; break;
4619 case EXPR_SMALLER: p = "<"; break;
4620 case EXPR_SEQUAL: p = "<="; break;
4621 case EXPR_MATCH: p = "=~"; break;
4622 case EXPR_IS: p = "is"; break;
4623 case EXPR_ISNOT: p = "isnot"; break;
4624 case EXPR_NOMATCH: p = "!~"; break;
4625 default: p = "???"; break;
4626 }
4627 STRCPY(buf, p);
4628 if (iptr->isn_arg.op.op_ic == TRUE)
4629 strcat(buf, "?");
4630 switch(iptr->isn_type)
4631 {
4632 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4633 case ISN_COMPARESPECIAL:
4634 type = "COMPARESPECIAL"; break;
4635 case ISN_COMPARENR: type = "COMPARENR"; break;
4636 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4637 case ISN_COMPARESTRING:
4638 type = "COMPARESTRING"; break;
4639 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4640 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4641 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4642 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4644 default: type = "???"; break;
4645 }
4646
4647 smsg("%4d %s %s", current, type, buf);
4648 }
4649 break;
4650
4651 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4652 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4653
4654 // expression operations
4655 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004656 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004657 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004658 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004659 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004660 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004661 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004662 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4663 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004664 case ISN_SLICE: smsg("%4d SLICE %lld",
4665 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004666 case ISN_GETITEM: smsg("%4d ITEM %lld",
4667 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004668 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4669 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 iptr->isn_arg.string); break;
4671 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4672
4673 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004674 case ISN_CHECKTYPE:
4675 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004676 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004677 char *tofree;
4678
Bram Moolenaare32e5162021-01-21 20:21:29 +01004679 if (ct->ct_arg_idx == 0)
4680 smsg("%4d CHECKTYPE %s stack[%d]", current,
4681 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004682 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004683 else
4684 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4685 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004686 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004687 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004688 vim_free(tofree);
4689 break;
4690 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004691 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4692 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4693 iptr->isn_arg.checklen.cl_min_len);
4694 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004695 case ISN_SETTYPE:
4696 {
4697 char *tofree;
4698
4699 smsg("%4d SETTYPE %s", current,
4700 type_name(iptr->isn_arg.type.ct_type, &tofree));
4701 vim_free(tofree);
4702 break;
4703 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004704 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 case ISN_2BOOL: if (iptr->isn_arg.number)
4706 smsg("%4d INVERT (!val)", current);
4707 else
4708 smsg("%4d 2BOOL (!!val)", current);
4709 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004710 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004711 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004712 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004713 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004714 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004715 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004716 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4717 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004718 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004719 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4720 smsg("%4d PUT %c above range",
4721 current, iptr->isn_arg.put.put_regname);
4722 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4723 smsg("%4d PUT %c range",
4724 current, iptr->isn_arg.put.put_regname);
4725 else
4726 smsg("%4d PUT %c %ld", current,
4727 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004728 (long)iptr->isn_arg.put.put_lnum);
4729 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730
Bram Moolenaar02194d22020-10-24 23:08:38 +02004731 // TODO: summarize modifiers
4732 case ISN_CMDMOD:
4733 {
4734 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004735 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004736 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4737
4738 buf = alloc(len + 1);
4739 if (buf != NULL)
4740 {
4741 (void)produce_cmdmods(
4742 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4743 smsg("%4d CMDMOD %s", current, buf);
4744 vim_free(buf);
4745 }
4746 break;
4747 }
4748 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004749
Bram Moolenaarb2049902021-01-24 12:53:53 +01004750 case ISN_PROF_START:
4751 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4752 break;
4753
4754 case ISN_PROF_END:
4755 smsg("%4d PROFILE END", current);
4756 break;
4757
Bram Moolenaar792f7862020-11-23 08:31:18 +01004758 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4759 iptr->isn_arg.unpack.unp_count,
4760 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4761 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004762 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4763 iptr->isn_arg.shuffle.shfl_item,
4764 iptr->isn_arg.shuffle.shfl_up);
4765 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 case ISN_DROP: smsg("%4d DROP", current); break;
4767 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004768
4769 out_flush(); // output one line at a time
4770 ui_breakcheck();
4771 if (got_int)
4772 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774}
4775
4776/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004777 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 * list, etc. Mostly like what JavaScript does, except that empty list and
4779 * empty dictionary are FALSE.
4780 */
4781 int
4782tv2bool(typval_T *tv)
4783{
4784 switch (tv->v_type)
4785 {
4786 case VAR_NUMBER:
4787 return tv->vval.v_number != 0;
4788 case VAR_FLOAT:
4789#ifdef FEAT_FLOAT
4790 return tv->vval.v_float != 0.0;
4791#else
4792 break;
4793#endif
4794 case VAR_PARTIAL:
4795 return tv->vval.v_partial != NULL;
4796 case VAR_FUNC:
4797 case VAR_STRING:
4798 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4799 case VAR_LIST:
4800 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4801 case VAR_DICT:
4802 return tv->vval.v_dict != NULL
4803 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4804 case VAR_BOOL:
4805 case VAR_SPECIAL:
4806 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4807 case VAR_JOB:
4808#ifdef FEAT_JOB_CHANNEL
4809 return tv->vval.v_job != NULL;
4810#else
4811 break;
4812#endif
4813 case VAR_CHANNEL:
4814#ifdef FEAT_JOB_CHANNEL
4815 return tv->vval.v_channel != NULL;
4816#else
4817 break;
4818#endif
4819 case VAR_BLOB:
4820 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4821 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004822 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823 case VAR_VOID:
4824 break;
4825 }
4826 return FALSE;
4827}
4828
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004829 void
4830emsg_using_string_as(typval_T *tv, int as_number)
4831{
4832 semsg(_(as_number ? e_using_string_as_number_str
4833 : e_using_string_as_bool_str),
4834 tv->vval.v_string == NULL
4835 ? (char_u *)"" : tv->vval.v_string);
4836}
4837
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838/*
4839 * If "tv" is a string give an error and return FAIL.
4840 */
4841 int
4842check_not_string(typval_T *tv)
4843{
4844 if (tv->v_type == VAR_STRING)
4845 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004846 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847 clear_tv(tv);
4848 return FAIL;
4849 }
4850 return OK;
4851}
4852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853
4854#endif // FEAT_EVAL