blob: 2eb0bec13de88a05df99933683127b60a1dc4f86 [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 {
2744 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2745 typval_T *idxtv =
2746 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2747
2748 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002749 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002751 ++idxtv->vval.v_number;
2752 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002753 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 // past the end of the list, jump to "endfor"
2755 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002756 may_restore_cmdmod(&funclocal);
2757 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 else if (list->lv_first == &range_list_item)
2759 {
2760 // non-materialized range() list
2761 tv = STACK_TV_BOT(0);
2762 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002763 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 tv->vval.v_number = list_find_nr(
2765 list, idxtv->vval.v_number, NULL);
2766 ++ectx.ec_stack.ga_len;
2767 }
2768 else
2769 {
2770 listitem_T *li = list_find(list, idxtv->vval.v_number);
2771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2773 ++ectx.ec_stack.ga_len;
2774 }
2775 }
2776 break;
2777
2778 // start of ":try" block
2779 case ISN_TRY:
2780 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002781 trycmd_T *trycmd = NULL;
2782
Bram Moolenaar270d0382020-05-15 21:42:53 +02002783 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 goto failed;
2785 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2786 + ectx.ec_trystack.ga_len;
2787 ++ectx.ec_trystack.ga_len;
2788 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002789 CLEAR_POINTER(trycmd);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002790 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002791 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002792 trycmd->tcd_catch_idx =
2793 iptr->isn_arg.try.try_ref->try_catch;
2794 trycmd->tcd_finally_idx =
2795 iptr->isn_arg.try.try_ref->try_finally;
2796 trycmd->tcd_endtry_idx =
2797 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 }
2799 break;
2800
2801 case ISN_PUSHEXC:
2802 if (current_exception == NULL)
2803 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002804 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 iemsg("Evaluating catch while current_exception is NULL");
2806 goto failed;
2807 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002808 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 goto failed;
2810 tv = STACK_TV_BOT(0);
2811 ++ectx.ec_stack.ga_len;
2812 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002813 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814 tv->vval.v_string = vim_strsave(
2815 (char_u *)current_exception->value);
2816 break;
2817
2818 case ISN_CATCH:
2819 {
2820 garray_T *trystack = &ectx.ec_trystack;
2821
Bram Moolenaara91a7132021-03-25 21:12:15 +01002822 may_restore_cmdmod(&funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002823 if (trystack->ga_len > 0)
2824 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002825 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 + trystack->ga_len - 1;
2827 trycmd->tcd_caught = TRUE;
2828 }
2829 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002830 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 catch_exception(current_exception);
2832 }
2833 break;
2834
Bram Moolenaarc150c092021-02-13 15:02:46 +01002835 case ISN_TRYCONT:
2836 {
2837 garray_T *trystack = &ectx.ec_trystack;
2838 trycont_T *trycont = &iptr->isn_arg.trycont;
2839 int i;
2840 trycmd_T *trycmd;
2841 int iidx = trycont->tct_where;
2842
2843 if (trystack->ga_len < trycont->tct_levels)
2844 {
2845 siemsg("TRYCONT: expected %d levels, found %d",
2846 trycont->tct_levels, trystack->ga_len);
2847 goto failed;
2848 }
2849 // Make :endtry jump to any outer try block and the last
2850 // :endtry inside the loop to the loop start.
2851 for (i = trycont->tct_levels; i > 0; --i)
2852 {
2853 trycmd = ((trycmd_T *)trystack->ga_data)
2854 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002855 // Add one to tcd_cont to be able to jump to
2856 // instruction with index zero.
2857 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002858 iidx = trycmd->tcd_finally_idx == 0
2859 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002860 }
2861 // jump to :finally or :endtry of current try statement
2862 ectx.ec_iidx = iidx;
2863 }
2864 break;
2865
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002866 case ISN_FINALLY:
2867 {
2868 garray_T *trystack = &ectx.ec_trystack;
2869 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2870 + trystack->ga_len - 1;
2871
2872 // Reset the index to avoid a return statement jumps here
2873 // again.
2874 trycmd->tcd_finally_idx = 0;
2875 break;
2876 }
2877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 // end of ":try" block
2879 case ISN_ENDTRY:
2880 {
2881 garray_T *trystack = &ectx.ec_trystack;
2882
2883 if (trystack->ga_len > 0)
2884 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002885 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 --trystack->ga_len;
2888 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002889 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 trycmd = ((trycmd_T *)trystack->ga_data)
2891 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002892 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 {
2894 // discard the exception
2895 if (caught_stack == current_exception)
2896 caught_stack = caught_stack->caught;
2897 discard_current_exception();
2898 }
2899
2900 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002901 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002902
2903 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2904 {
2905 --ectx.ec_stack.ga_len;
2906 clear_tv(STACK_TV_BOT(0));
2907 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002908 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002909 // handling :continue: jump to outer try block or
2910 // start of the loop
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002911 ectx.ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 }
2913 }
2914 break;
2915
2916 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002917 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002918 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002919
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002920 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2921 {
2922 // throwing an exception while using "silent!" causes
2923 // the function to abort but not display an error.
2924 tv = STACK_TV_BOT(-1);
2925 clear_tv(tv);
2926 tv->v_type = VAR_NUMBER;
2927 tv->vval.v_number = 0;
2928 goto done;
2929 }
2930 --ectx.ec_stack.ga_len;
2931 tv = STACK_TV_BOT(0);
2932 if (tv->vval.v_string == NULL
2933 || *skipwhite(tv->vval.v_string) == NUL)
2934 {
2935 vim_free(tv->vval.v_string);
2936 SOURCING_LNUM = iptr->isn_lnum;
2937 emsg(_(e_throw_with_empty_string));
2938 goto failed;
2939 }
2940
2941 // Inside a "catch" we need to first discard the caught
2942 // exception.
2943 if (trystack->ga_len > 0)
2944 {
2945 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2946 + trystack->ga_len - 1;
2947 if (trycmd->tcd_caught && current_exception != NULL)
2948 {
2949 // discard the exception
2950 if (caught_stack == current_exception)
2951 caught_stack = caught_stack->caught;
2952 discard_current_exception();
2953 trycmd->tcd_caught = FALSE;
2954 }
2955 }
2956
2957 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2958 == FAIL)
2959 {
2960 vim_free(tv->vval.v_string);
2961 goto failed;
2962 }
2963 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 break;
2966
2967 // compare with special values
2968 case ISN_COMPAREBOOL:
2969 case ISN_COMPARESPECIAL:
2970 {
2971 typval_T *tv1 = STACK_TV_BOT(-2);
2972 typval_T *tv2 = STACK_TV_BOT(-1);
2973 varnumber_T arg1 = tv1->vval.v_number;
2974 varnumber_T arg2 = tv2->vval.v_number;
2975 int res;
2976
2977 switch (iptr->isn_arg.op.op_type)
2978 {
2979 case EXPR_EQUAL: res = arg1 == arg2; break;
2980 case EXPR_NEQUAL: res = arg1 != arg2; break;
2981 default: res = 0; break;
2982 }
2983
2984 --ectx.ec_stack.ga_len;
2985 tv1->v_type = VAR_BOOL;
2986 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2987 }
2988 break;
2989
2990 // Operation with two number arguments
2991 case ISN_OPNR:
2992 case ISN_COMPARENR:
2993 {
2994 typval_T *tv1 = STACK_TV_BOT(-2);
2995 typval_T *tv2 = STACK_TV_BOT(-1);
2996 varnumber_T arg1 = tv1->vval.v_number;
2997 varnumber_T arg2 = tv2->vval.v_number;
2998 varnumber_T res;
2999
3000 switch (iptr->isn_arg.op.op_type)
3001 {
3002 case EXPR_MULT: res = arg1 * arg2; break;
3003 case EXPR_DIV: res = arg1 / arg2; break;
3004 case EXPR_REM: res = arg1 % arg2; break;
3005 case EXPR_SUB: res = arg1 - arg2; break;
3006 case EXPR_ADD: res = arg1 + arg2; break;
3007
3008 case EXPR_EQUAL: res = arg1 == arg2; break;
3009 case EXPR_NEQUAL: res = arg1 != arg2; break;
3010 case EXPR_GREATER: res = arg1 > arg2; break;
3011 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3012 case EXPR_SMALLER: res = arg1 < arg2; break;
3013 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3014 default: res = 0; break;
3015 }
3016
3017 --ectx.ec_stack.ga_len;
3018 if (iptr->isn_type == ISN_COMPARENR)
3019 {
3020 tv1->v_type = VAR_BOOL;
3021 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3022 }
3023 else
3024 tv1->vval.v_number = res;
3025 }
3026 break;
3027
3028 // Computation with two float arguments
3029 case ISN_OPFLOAT:
3030 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003031#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 {
3033 typval_T *tv1 = STACK_TV_BOT(-2);
3034 typval_T *tv2 = STACK_TV_BOT(-1);
3035 float_T arg1 = tv1->vval.v_float;
3036 float_T arg2 = tv2->vval.v_float;
3037 float_T res = 0;
3038 int cmp = FALSE;
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_SUB: res = arg1 - arg2; break;
3045 case EXPR_ADD: res = arg1 + arg2; break;
3046
3047 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3048 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3049 case EXPR_GREATER: cmp = arg1 > arg2; break;
3050 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3051 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3052 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3053 default: cmp = 0; break;
3054 }
3055 --ectx.ec_stack.ga_len;
3056 if (iptr->isn_type == ISN_COMPAREFLOAT)
3057 {
3058 tv1->v_type = VAR_BOOL;
3059 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3060 }
3061 else
3062 tv1->vval.v_float = res;
3063 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003064#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 break;
3066
3067 case ISN_COMPARELIST:
3068 {
3069 typval_T *tv1 = STACK_TV_BOT(-2);
3070 typval_T *tv2 = STACK_TV_BOT(-1);
3071 list_T *arg1 = tv1->vval.v_list;
3072 list_T *arg2 = tv2->vval.v_list;
3073 int cmp = FALSE;
3074 int ic = iptr->isn_arg.op.op_ic;
3075
3076 switch (iptr->isn_arg.op.op_type)
3077 {
3078 case EXPR_EQUAL: cmp =
3079 list_equal(arg1, arg2, ic, FALSE); break;
3080 case EXPR_NEQUAL: cmp =
3081 !list_equal(arg1, arg2, ic, FALSE); break;
3082 case EXPR_IS: cmp = arg1 == arg2; break;
3083 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3084 default: cmp = 0; break;
3085 }
3086 --ectx.ec_stack.ga_len;
3087 clear_tv(tv1);
3088 clear_tv(tv2);
3089 tv1->v_type = VAR_BOOL;
3090 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3091 }
3092 break;
3093
3094 case ISN_COMPAREBLOB:
3095 {
3096 typval_T *tv1 = STACK_TV_BOT(-2);
3097 typval_T *tv2 = STACK_TV_BOT(-1);
3098 blob_T *arg1 = tv1->vval.v_blob;
3099 blob_T *arg2 = tv2->vval.v_blob;
3100 int cmp = FALSE;
3101
3102 switch (iptr->isn_arg.op.op_type)
3103 {
3104 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3105 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3106 case EXPR_IS: cmp = arg1 == arg2; break;
3107 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3108 default: cmp = 0; break;
3109 }
3110 --ectx.ec_stack.ga_len;
3111 clear_tv(tv1);
3112 clear_tv(tv2);
3113 tv1->v_type = VAR_BOOL;
3114 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3115 }
3116 break;
3117
3118 // TODO: handle separately
3119 case ISN_COMPARESTRING:
3120 case ISN_COMPAREDICT:
3121 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 case ISN_COMPAREANY:
3123 {
3124 typval_T *tv1 = STACK_TV_BOT(-2);
3125 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003126 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 int ic = iptr->isn_arg.op.op_ic;
3128
Bram Moolenaareb26f432020-09-14 16:50:05 +02003129 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003130 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 --ectx.ec_stack.ga_len;
3133 }
3134 break;
3135
3136 case ISN_ADDLIST:
3137 case ISN_ADDBLOB:
3138 {
3139 typval_T *tv1 = STACK_TV_BOT(-2);
3140 typval_T *tv2 = STACK_TV_BOT(-1);
3141
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003142 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 if (iptr->isn_type == ISN_ADDLIST)
3144 eval_addlist(tv1, tv2);
3145 else
3146 eval_addblob(tv1, tv2);
3147 clear_tv(tv2);
3148 --ectx.ec_stack.ga_len;
3149 }
3150 break;
3151
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003152 case ISN_LISTAPPEND:
3153 {
3154 typval_T *tv1 = STACK_TV_BOT(-2);
3155 typval_T *tv2 = STACK_TV_BOT(-1);
3156 list_T *l = tv1->vval.v_list;
3157
3158 // add an item to a list
3159 if (l == NULL)
3160 {
3161 SOURCING_LNUM = iptr->isn_lnum;
3162 emsg(_(e_cannot_add_to_null_list));
3163 goto on_error;
3164 }
3165 if (list_append_tv(l, tv2) == FAIL)
3166 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003167 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003168 --ectx.ec_stack.ga_len;
3169 }
3170 break;
3171
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003172 case ISN_BLOBAPPEND:
3173 {
3174 typval_T *tv1 = STACK_TV_BOT(-2);
3175 typval_T *tv2 = STACK_TV_BOT(-1);
3176 blob_T *b = tv1->vval.v_blob;
3177 int error = FALSE;
3178 varnumber_T n;
3179
3180 // add a number to a blob
3181 if (b == NULL)
3182 {
3183 SOURCING_LNUM = iptr->isn_lnum;
3184 emsg(_(e_cannot_add_to_null_blob));
3185 goto on_error;
3186 }
3187 n = tv_get_number_chk(tv2, &error);
3188 if (error)
3189 goto on_error;
3190 ga_append(&b->bv_ga, (int)n);
3191 --ectx.ec_stack.ga_len;
3192 }
3193 break;
3194
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 // Computation with two arguments of unknown type
3196 case ISN_OPANY:
3197 {
3198 typval_T *tv1 = STACK_TV_BOT(-2);
3199 typval_T *tv2 = STACK_TV_BOT(-1);
3200 varnumber_T n1, n2;
3201#ifdef FEAT_FLOAT
3202 float_T f1 = 0, f2 = 0;
3203#endif
3204 int error = FALSE;
3205
3206 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3207 {
3208 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3209 {
3210 eval_addlist(tv1, tv2);
3211 clear_tv(tv2);
3212 --ectx.ec_stack.ga_len;
3213 break;
3214 }
3215 else if (tv1->v_type == VAR_BLOB
3216 && tv2->v_type == VAR_BLOB)
3217 {
3218 eval_addblob(tv1, tv2);
3219 clear_tv(tv2);
3220 --ectx.ec_stack.ga_len;
3221 break;
3222 }
3223 }
3224#ifdef FEAT_FLOAT
3225 if (tv1->v_type == VAR_FLOAT)
3226 {
3227 f1 = tv1->vval.v_float;
3228 n1 = 0;
3229 }
3230 else
3231#endif
3232 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003233 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 n1 = tv_get_number_chk(tv1, &error);
3235 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003236 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237#ifdef FEAT_FLOAT
3238 if (tv2->v_type == VAR_FLOAT)
3239 f1 = n1;
3240#endif
3241 }
3242#ifdef FEAT_FLOAT
3243 if (tv2->v_type == VAR_FLOAT)
3244 {
3245 f2 = tv2->vval.v_float;
3246 n2 = 0;
3247 }
3248 else
3249#endif
3250 {
3251 n2 = tv_get_number_chk(tv2, &error);
3252 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003253 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254#ifdef FEAT_FLOAT
3255 if (tv1->v_type == VAR_FLOAT)
3256 f2 = n2;
3257#endif
3258 }
3259#ifdef FEAT_FLOAT
3260 // if there is a float on either side the result is a float
3261 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3262 {
3263 switch (iptr->isn_arg.op.op_type)
3264 {
3265 case EXPR_MULT: f1 = f1 * f2; break;
3266 case EXPR_DIV: f1 = f1 / f2; break;
3267 case EXPR_SUB: f1 = f1 - f2; break;
3268 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003269 default: SOURCING_LNUM = iptr->isn_lnum;
3270 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003271 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 }
3273 clear_tv(tv1);
3274 clear_tv(tv2);
3275 tv1->v_type = VAR_FLOAT;
3276 tv1->vval.v_float = f1;
3277 --ectx.ec_stack.ga_len;
3278 }
3279 else
3280#endif
3281 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003282 int failed = FALSE;
3283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 switch (iptr->isn_arg.op.op_type)
3285 {
3286 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003287 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3288 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003289 goto on_error;
3290 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 case EXPR_SUB: n1 = n1 - n2; break;
3292 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003293 default: n1 = num_modulus(n1, n2, &failed);
3294 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003295 goto on_error;
3296 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 }
3298 clear_tv(tv1);
3299 clear_tv(tv2);
3300 tv1->v_type = VAR_NUMBER;
3301 tv1->vval.v_number = n1;
3302 --ectx.ec_stack.ga_len;
3303 }
3304 }
3305 break;
3306
3307 case ISN_CONCAT:
3308 {
3309 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3310 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3311 char_u *res;
3312
3313 res = concat_str(str1, str2);
3314 clear_tv(STACK_TV_BOT(-2));
3315 clear_tv(STACK_TV_BOT(-1));
3316 --ectx.ec_stack.ga_len;
3317 STACK_TV_BOT(-1)->vval.v_string = res;
3318 }
3319 break;
3320
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003321 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003322 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003323 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003324 int is_slice = iptr->isn_type == ISN_STRSLICE;
3325 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003326 char_u *res;
3327
3328 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003329 // string slice: string is at stack-3, first index at
3330 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003331 if (is_slice)
3332 {
3333 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003334 n1 = tv->vval.v_number;
3335 }
3336
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003337 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003338 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003339
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003340 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003341 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003342 if (is_slice)
3343 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003344 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003345 else
3346 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003347 // single character (including composing characters).
3348 // If the index is too big or negative the result is
3349 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003350 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003351 vim_free(tv->vval.v_string);
3352 tv->vval.v_string = res;
3353 }
3354 break;
3355
3356 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003357 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 {
Bram Moolenaared591872020-08-15 22:14:53 +02003359 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003361 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362
3363 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003364 // list slice: list is at stack-3, indexes at stack-2 and
3365 // stack-1
3366 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 list = tv->vval.v_list;
3368
3369 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003370 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003372
3373 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 {
Bram Moolenaared591872020-08-15 22:14:53 +02003375 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003376 n1 = tv->vval.v_number;
3377 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 }
Bram Moolenaared591872020-08-15 22:14:53 +02003379
3380 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003381 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003382 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003383 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3384 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003385 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 }
3387 break;
3388
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003389 case ISN_ANYINDEX:
3390 case ISN_ANYSLICE:
3391 {
3392 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3393 typval_T *var1, *var2;
3394 int res;
3395
3396 // index: composite is at stack-2, index at stack-1
3397 // slice: composite is at stack-3, indexes at stack-2 and
3398 // stack-1
3399 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003400 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003401 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3402 goto on_error;
3403 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3404 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003405 res = eval_index_inner(tv, is_slice, var1, var2,
3406 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003407 clear_tv(var1);
3408 if (is_slice)
3409 clear_tv(var2);
3410 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3411 if (res == FAIL)
3412 goto on_error;
3413 }
3414 break;
3415
Bram Moolenaar9af78762020-06-16 11:34:42 +02003416 case ISN_SLICE:
3417 {
3418 list_T *list;
3419 int count = iptr->isn_arg.number;
3420
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003421 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003422 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003423 list = tv->vval.v_list;
3424
3425 // no error for short list, expect it to be checked earlier
3426 if (list != NULL && list->lv_len >= count)
3427 {
3428 list_T *newlist = list_slice(list,
3429 count, list->lv_len - 1);
3430
3431 if (newlist != NULL)
3432 {
3433 list_unref(list);
3434 tv->vval.v_list = newlist;
3435 ++newlist->lv_refcount;
3436 }
3437 }
3438 }
3439 break;
3440
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003441 case ISN_GETITEM:
3442 {
3443 listitem_T *li;
3444 int index = iptr->isn_arg.number;
3445
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003446 // Get list item: list is at stack-1, push item.
3447 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003448 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003449 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003450
3451 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3452 goto failed;
3453 ++ectx.ec_stack.ga_len;
3454 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003455
3456 // Useful when used in unpack assignment. Reset at
3457 // ISN_DROP.
3458 where.wt_index = index + 1;
3459 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003460 }
3461 break;
3462
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 case ISN_MEMBER:
3464 {
3465 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003466 char_u *key;
3467 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003468 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003469
3470 // dict member: dict is at stack-2, key at stack-1
3471 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003472 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003473 dict = tv->vval.v_dict;
3474
3475 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003476 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003477 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003478 if (key == NULL)
3479 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003480
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003481 if ((di = dict_find(dict, key, -1)) == NULL)
3482 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003483 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003484 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003485
3486 // If :silent! is used we will continue, make sure the
3487 // stack contents makes sense.
3488 clear_tv(tv);
3489 --ectx.ec_stack.ga_len;
3490 tv = STACK_TV_BOT(-1);
3491 clear_tv(tv);
3492 tv->v_type = VAR_NUMBER;
3493 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003494 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003495 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003496 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003497 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003498 // Clear the dict only after getting the item, to avoid
3499 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003500 tv = STACK_TV_BOT(-1);
3501 temp_tv = *tv;
3502 copy_tv(&di->di_tv, tv);
3503 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003504 }
3505 break;
3506
3507 // dict member with string key
3508 case ISN_STRINGMEMBER:
3509 {
3510 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003512 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513
3514 tv = STACK_TV_BOT(-1);
3515 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3516 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003517 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003519 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 }
3521 dict = tv->vval.v_dict;
3522
3523 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3524 == NULL)
3525 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003526 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003528 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003530 // Clear the dict after getting the item, to avoid that it
3531 // make the item invalid.
3532 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003534 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 }
3536 break;
3537
3538 case ISN_NEGATENR:
3539 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003540 if (tv->v_type != VAR_NUMBER
3541#ifdef FEAT_FLOAT
3542 && tv->v_type != VAR_FLOAT
3543#endif
3544 )
3545 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003546 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003547 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003548 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003549 }
3550#ifdef FEAT_FLOAT
3551 if (tv->v_type == VAR_FLOAT)
3552 tv->vval.v_float = -tv->vval.v_float;
3553 else
3554#endif
3555 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 break;
3557
3558 case ISN_CHECKNR:
3559 {
3560 int error = FALSE;
3561
3562 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003563 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003565 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 (void)tv_get_number_chk(tv, &error);
3567 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003568 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 }
3570 break;
3571
3572 case ISN_CHECKTYPE:
3573 {
3574 checktype_T *ct = &iptr->isn_arg.type;
3575
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003576 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003577 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003578 if (!where.wt_variable)
3579 where.wt_index = ct->ct_arg_idx;
3580 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003581 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003582 if (!where.wt_variable)
3583 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003584
3585 // number 0 is FALSE, number 1 is TRUE
3586 if (tv->v_type == VAR_NUMBER
3587 && ct->ct_type->tt_type == VAR_BOOL
3588 && (tv->vval.v_number == 0
3589 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003591 tv->v_type = VAR_BOOL;
3592 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003593 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 }
3595 }
3596 break;
3597
Bram Moolenaar9af78762020-06-16 11:34:42 +02003598 case ISN_CHECKLEN:
3599 {
3600 int min_len = iptr->isn_arg.checklen.cl_min_len;
3601 list_T *list = NULL;
3602
3603 tv = STACK_TV_BOT(-1);
3604 if (tv->v_type == VAR_LIST)
3605 list = tv->vval.v_list;
3606 if (list == NULL || list->lv_len < min_len
3607 || (list->lv_len > min_len
3608 && !iptr->isn_arg.checklen.cl_more_OK))
3609 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003610 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003611 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003612 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003613 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003614 }
3615 }
3616 break;
3617
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003618 case ISN_SETTYPE:
3619 {
3620 checktype_T *ct = &iptr->isn_arg.type;
3621
3622 tv = STACK_TV_BOT(-1);
3623 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3624 {
3625 free_type(tv->vval.v_dict->dv_type);
3626 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3627 }
3628 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3629 {
3630 free_type(tv->vval.v_list->lv_type);
3631 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3632 }
3633 }
3634 break;
3635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003637 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 {
3639 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003640 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641
3642 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003643 if (iptr->isn_type == ISN_2BOOL)
3644 {
3645 n = tv2bool(tv);
3646 if (iptr->isn_arg.number) // invert
3647 n = !n;
3648 }
3649 else
3650 {
3651 SOURCING_LNUM = iptr->isn_lnum;
3652 n = tv_get_bool_chk(tv, &error);
3653 if (error)
3654 goto on_error;
3655 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 clear_tv(tv);
3657 tv->v_type = VAR_BOOL;
3658 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3659 }
3660 break;
3661
3662 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003663 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003664 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003665 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3666 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3667 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 break;
3669
Bram Moolenaar08597872020-12-10 19:43:40 +01003670 case ISN_RANGE:
3671 {
3672 exarg_T ea;
3673 char *errormsg;
3674
Bram Moolenaarece0b872021-01-08 20:40:45 +01003675 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003676 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003677 ea.addr_type = ADDR_LINES;
3678 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003679 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003680 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003681 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003682
3683 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3684 goto failed;
3685 ++ectx.ec_stack.ga_len;
3686 tv = STACK_TV_BOT(-1);
3687 tv->v_type = VAR_NUMBER;
3688 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003689 if (ea.addr_count == 0)
3690 tv->vval.v_number = curwin->w_cursor.lnum;
3691 else
3692 tv->vval.v_number = ea.line2;
3693 }
3694 break;
3695
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003696 case ISN_PUT:
3697 {
3698 int regname = iptr->isn_arg.put.put_regname;
3699 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3700 char_u *expr = NULL;
3701 int dir = FORWARD;
3702
Bram Moolenaar08597872020-12-10 19:43:40 +01003703 if (lnum < -2)
3704 {
3705 // line number was put on the stack by ISN_RANGE
3706 tv = STACK_TV_BOT(-1);
3707 curwin->w_cursor.lnum = tv->vval.v_number;
3708 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3709 dir = BACKWARD;
3710 --ectx.ec_stack.ga_len;
3711 }
3712 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003713 // :put! above cursor
3714 dir = BACKWARD;
3715 else if (lnum >= 0)
3716 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003717
3718 if (regname == '=')
3719 {
3720 tv = STACK_TV_BOT(-1);
3721 if (tv->v_type == VAR_STRING)
3722 expr = tv->vval.v_string;
3723 else
3724 {
3725 expr = typval2string(tv, TRUE); // allocates value
3726 clear_tv(tv);
3727 }
3728 --ectx.ec_stack.ga_len;
3729 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003730 check_cursor();
3731 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3732 vim_free(expr);
3733 }
3734 break;
3735
Bram Moolenaar02194d22020-10-24 23:08:38 +02003736 case ISN_CMDMOD:
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003737 funclocal.floc_save_cmdmod = cmdmod;
3738 funclocal.floc_restore_cmdmod = TRUE;
3739 funclocal.floc_restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003740 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3741 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003742 break;
3743
Bram Moolenaar02194d22020-10-24 23:08:38 +02003744 case ISN_CMDMOD_REV:
3745 // filter regprog is owned by the instruction, don't free it
3746 cmdmod.cmod_filter_regmatch.regprog = NULL;
3747 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003748 cmdmod = funclocal.floc_save_cmdmod;
3749 funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003750 break;
3751
Bram Moolenaar792f7862020-11-23 08:31:18 +01003752 case ISN_UNPACK:
3753 {
3754 int count = iptr->isn_arg.unpack.unp_count;
3755 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3756 list_T *l;
3757 listitem_T *li;
3758 int i;
3759
3760 // Check there is a valid list to unpack.
3761 tv = STACK_TV_BOT(-1);
3762 if (tv->v_type != VAR_LIST)
3763 {
3764 SOURCING_LNUM = iptr->isn_lnum;
3765 emsg(_(e_for_argument_must_be_sequence_of_lists));
3766 goto on_error;
3767 }
3768 l = tv->vval.v_list;
3769 if (l == NULL
3770 || l->lv_len < (semicolon ? count - 1 : count))
3771 {
3772 SOURCING_LNUM = iptr->isn_lnum;
3773 emsg(_(e_list_value_does_not_have_enough_items));
3774 goto on_error;
3775 }
3776 else if (!semicolon && l->lv_len > count)
3777 {
3778 SOURCING_LNUM = iptr->isn_lnum;
3779 emsg(_(e_list_value_has_more_items_than_targets));
3780 goto on_error;
3781 }
3782
3783 CHECK_LIST_MATERIALIZE(l);
3784 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3785 goto failed;
3786 ectx.ec_stack.ga_len += count - 1;
3787
3788 // Variable after semicolon gets a list with the remaining
3789 // items.
3790 if (semicolon)
3791 {
3792 list_T *rem_list =
3793 list_alloc_with_items(l->lv_len - count + 1);
3794
3795 if (rem_list == NULL)
3796 goto failed;
3797 tv = STACK_TV_BOT(-count);
3798 tv->vval.v_list = rem_list;
3799 ++rem_list->lv_refcount;
3800 tv->v_lock = 0;
3801 li = l->lv_first;
3802 for (i = 0; i < count - 1; ++i)
3803 li = li->li_next;
3804 for (i = 0; li != NULL; ++i)
3805 {
3806 list_set_item(rem_list, i, &li->li_tv);
3807 li = li->li_next;
3808 }
3809 --count;
3810 }
3811
3812 // Produce the values in reverse order, first item last.
3813 li = l->lv_first;
3814 for (i = 0; i < count; ++i)
3815 {
3816 tv = STACK_TV_BOT(-i - 1);
3817 copy_tv(&li->li_tv, tv);
3818 li = li->li_next;
3819 }
3820
3821 list_unref(l);
3822 }
3823 break;
3824
Bram Moolenaarb2049902021-01-24 12:53:53 +01003825 case ISN_PROF_START:
3826 case ISN_PROF_END:
3827 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003828#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003829 funccall_T cookie;
3830 ufunc_T *cur_ufunc =
3831 (((dfunc_T *)def_functions.ga_data)
3832 + ectx.ec_dfunc_idx)->df_ufunc;
3833
3834 cookie.func = cur_ufunc;
3835 if (iptr->isn_type == ISN_PROF_START)
3836 {
3837 func_line_start(&cookie, iptr->isn_lnum);
3838 // if we get here the instruction is executed
3839 func_line_exec(&cookie);
3840 }
3841 else
3842 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003843#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003844 }
3845 break;
3846
Bram Moolenaar389df252020-07-09 21:20:47 +02003847 case ISN_SHUFFLE:
3848 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003849 typval_T tmp_tv;
3850 int item = iptr->isn_arg.shuffle.shfl_item;
3851 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003852
3853 tmp_tv = *STACK_TV_BOT(-item);
3854 for ( ; up > 0 && item > 1; --up)
3855 {
3856 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3857 --item;
3858 }
3859 *STACK_TV_BOT(-item) = tmp_tv;
3860 }
3861 break;
3862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 case ISN_DROP:
3864 --ectx.ec_stack.ga_len;
3865 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003866 where.wt_index = 0;
3867 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 break;
3869 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003870 continue;
3871
Bram Moolenaard032f342020-07-18 18:13:02 +02003872func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003873 // Restore previous function. If the frame pointer is where we started
3874 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003875 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003876 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003877
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003878 if (func_return(&funclocal, &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003879 // only fails when out of memory
3880 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003881 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003882
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003883on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003884 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003885 // If "emsg_silent" is set then ignore the error, unless it was set
3886 // when calling the function.
3887 if (did_emsg_cumul + did_emsg == did_emsg_before
3888 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003889 {
3890 // If a sequence of instructions causes an error while ":silent!"
3891 // was used, restore the stack length and jump ahead to restoring
3892 // the cmdmod.
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003893 if (funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003894 {
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003895 while (ectx.ec_stack.ga_len
3896 > funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003897 {
3898 --ectx.ec_stack.ga_len;
3899 clear_tv(STACK_TV_BOT(0));
3900 }
3901 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3902 ++ectx.ec_iidx;
3903 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003904 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003905 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003906on_fatal_error:
3907 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003908 // If we are not inside a try-catch started here, abort execution.
3909 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003910 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 }
3912
3913done:
3914 // function finished, get result from the stack.
3915 tv = STACK_TV_BOT(-1);
3916 *rettv = *tv;
3917 tv->v_type = VAR_UNKNOWN;
3918 ret = OK;
3919
3920failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003921 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003922 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003923 func_return(&funclocal, &ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003924
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003925 // Deal with any remaining closures, they may be in use somewhere.
3926 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003927 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003928 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003929 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3930 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003931
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003932 estack_pop();
3933 current_sctx = save_current_sctx;
3934
Bram Moolenaarc970e422021-03-17 15:03:04 +01003935 // TODO: when is it safe to delete the function if it is no longer used?
3936 --ufunc->uf_calls;
3937
Bram Moolenaar352134b2020-10-17 22:04:08 +02003938 if (*msg_list != NULL && saved_msg_list != NULL)
3939 {
3940 msglist_T **plist = saved_msg_list;
3941
3942 // Append entries from the current msg_list (uncaught exceptions) to
3943 // the saved msg_list.
3944 while (*plist != NULL)
3945 plist = &(*plist)->next;
3946
3947 *plist = *msg_list;
3948 }
3949 msg_list = saved_msg_list;
3950
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003951 if (funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02003952 {
3953 cmdmod.cmod_filter_regmatch.regprog = NULL;
3954 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003955 cmdmod = funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003956 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003957 emsg_silent_def = save_emsg_silent_def;
3958 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003959
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003960failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003961 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3963 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003966 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003967
Bram Moolenaar0186e582021-01-10 18:33:11 +01003968 while (ectx.ec_outer != NULL)
3969 {
3970 outer_T *up = ectx.ec_outer->out_up_is_copy
3971 ? NULL : ectx.ec_outer->out_up;
3972
3973 vim_free(ectx.ec_outer);
3974 ectx.ec_outer = up;
3975 }
3976
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003977 // Not sure if this is necessary.
3978 suppress_errthrow = save_suppress_errthrow;
3979
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003980 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003981 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003982 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003983 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 return ret;
3985}
3986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003988 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003989 * We don't really need this at runtime, but we do have tests that require it,
3990 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 */
3992 void
3993ex_disassemble(exarg_T *eap)
3994{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003995 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003996 char_u *fname;
3997 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 dfunc_T *dfunc;
3999 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004000 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 int current;
4002 int line_idx = 0;
4003 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004004 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005
Bram Moolenaarbfd65582020-07-13 18:18:00 +02004006 if (STRNCMP(arg, "<lambda>", 8) == 0)
4007 {
4008 arg += 8;
4009 (void)getdigits(&arg);
4010 fname = vim_strnsave(eap->arg, arg - eap->arg);
4011 }
4012 else
4013 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004014 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01004015 if (fname == NULL)
4016 {
4017 semsg(_(e_invarg2), eap->arg);
4018 return;
4019 }
4020
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004021 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004022 if (ufunc == NULL)
4023 {
4024 char_u *p = untrans_function_name(fname);
4025
4026 if (p != NULL)
4027 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004028 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004029 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01004030 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 if (ufunc == NULL)
4032 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004033 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 return;
4035 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01004036 if (func_needs_compiling(ufunc, eap->forceit)
4037 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02004038 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004039 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004041 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 return;
4043 }
4044 if (ufunc->uf_name_exp != NULL)
4045 msg((char *)ufunc->uf_name_exp);
4046 else
4047 msg((char *)ufunc->uf_name);
4048
4049 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01004050#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004051 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
4052 instr_count = eap->forceit ? dfunc->df_instr_prof_count
4053 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01004054#else
4055 instr = dfunc->df_instr;
4056 instr_count = dfunc->df_instr_count;
4057#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004058 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 {
4060 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004061 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062
4063 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
4064 {
4065 if (current > prev_current)
4066 {
4067 msg_puts("\n\n");
4068 prev_current = current;
4069 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004070 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4071 if (line != NULL)
4072 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 }
4074
4075 switch (iptr->isn_type)
4076 {
4077 case ISN_EXEC:
4078 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
4079 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004080 case ISN_EXECCONCAT:
4081 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004082 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004083 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 case ISN_ECHO:
4085 {
4086 echo_T *echo = &iptr->isn_arg.echo;
4087
4088 smsg("%4d %s %d", current,
4089 echo->echo_with_white ? "ECHO" : "ECHON",
4090 echo->echo_count);
4091 }
4092 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01004093 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01004094 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004095 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01004096 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004097 case ISN_ECHOMSG:
4098 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004099 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004100 break;
4101 case ISN_ECHOERR:
4102 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004103 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004104 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004106 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004107 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004108 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004109 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004110 + STACK_FRAME_SIZE));
4111 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004112 smsg("%4d LOAD $%lld", current,
4113 (varnumber_T)(iptr->isn_arg.number));
4114 }
4115 break;
4116 case ISN_LOADOUTER:
4117 {
4118 if (iptr->isn_arg.number < 0)
4119 smsg("%4d LOADOUTER level %d arg[%d]", current,
4120 iptr->isn_arg.outer.outer_depth,
4121 iptr->isn_arg.outer.outer_idx
4122 + STACK_FRAME_SIZE);
4123 else
4124 smsg("%4d LOADOUTER level %d $%d", current,
4125 iptr->isn_arg.outer.outer_depth,
4126 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004127 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 break;
4129 case ISN_LOADV:
4130 smsg("%4d LOADV v:%s", current,
4131 get_vim_var_name(iptr->isn_arg.number));
4132 break;
4133 case ISN_LOADSCRIPT:
4134 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004135 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4136 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004138 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004140 smsg("%4d LOADSCRIPT %s-%d from %s", current,
4141 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004142 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004143 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 }
4145 break;
4146 case ISN_LOADS:
4147 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004148 scriptitem_T *si = SCRIPT_ITEM(
4149 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150
4151 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004152 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 }
4154 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004155 case ISN_LOADAUTO:
4156 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
4157 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 case ISN_LOADG:
4159 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
4160 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004161 case ISN_LOADB:
4162 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
4163 break;
4164 case ISN_LOADW:
4165 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
4166 break;
4167 case ISN_LOADT:
4168 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
4169 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004170 case ISN_LOADGDICT:
4171 smsg("%4d LOAD g:", current);
4172 break;
4173 case ISN_LOADBDICT:
4174 smsg("%4d LOAD b:", current);
4175 break;
4176 case ISN_LOADWDICT:
4177 smsg("%4d LOAD w:", current);
4178 break;
4179 case ISN_LOADTDICT:
4180 smsg("%4d LOAD t:", current);
4181 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 case ISN_LOADOPT:
4183 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
4184 break;
4185 case ISN_LOADENV:
4186 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
4187 break;
4188 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004189 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 break;
4191
4192 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01004193 if (iptr->isn_arg.number < 0)
4194 smsg("%4d STORE arg[%lld]", current,
4195 iptr->isn_arg.number + STACK_FRAME_SIZE);
4196 else
4197 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
4198 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004199 case ISN_STOREOUTER:
4200 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004201 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004202 smsg("%4d STOREOUTEr level %d arg[%d]", current,
4203 iptr->isn_arg.outer.outer_depth,
4204 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004205 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004206 smsg("%4d STOREOUTER level %d $%d", current,
4207 iptr->isn_arg.outer.outer_depth,
4208 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004209 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004211 case ISN_STOREV:
4212 smsg("%4d STOREV v:%s", current,
4213 get_vim_var_name(iptr->isn_arg.number));
4214 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004215 case ISN_STOREAUTO:
4216 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
4217 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004219 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
4220 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004221 case ISN_STOREB:
4222 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
4223 break;
4224 case ISN_STOREW:
4225 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
4226 break;
4227 case ISN_STORET:
4228 smsg("%4d STORET %s", current, iptr->isn_arg.string);
4229 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004230 case ISN_STORES:
4231 {
4232 scriptitem_T *si = SCRIPT_ITEM(
4233 iptr->isn_arg.loadstore.ls_sid);
4234
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004235 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004236 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 break;
4239 case ISN_STORESCRIPT:
4240 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004241 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4242 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004244 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004246 smsg("%4d STORESCRIPT %s-%d in %s", current,
4247 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004248 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004249 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 }
4251 break;
4252 case ISN_STOREOPT:
4253 smsg("%4d STOREOPT &%s", current,
4254 iptr->isn_arg.storeopt.so_name);
4255 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004256 case ISN_STOREENV:
4257 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4258 break;
4259 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004260 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004261 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 case ISN_STORENR:
4263 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004264 iptr->isn_arg.storenr.stnr_val,
4265 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 break;
4267
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004268 case ISN_STOREINDEX:
4269 switch (iptr->isn_arg.vartype)
4270 {
4271 case VAR_LIST:
4272 smsg("%4d STORELIST", current);
4273 break;
4274 case VAR_DICT:
4275 smsg("%4d STOREDICT", current);
4276 break;
4277 case VAR_ANY:
4278 smsg("%4d STOREINDEX", current);
4279 break;
4280 default: break;
4281 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004282 break;
4283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 // constants
4285 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004286 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004287 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 break;
4289 case ISN_PUSHBOOL:
4290 case ISN_PUSHSPEC:
4291 smsg("%4d PUSH %s", current,
4292 get_var_special_name(iptr->isn_arg.number));
4293 break;
4294 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004295#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004297#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 break;
4299 case ISN_PUSHS:
4300 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4301 break;
4302 case ISN_PUSHBLOB:
4303 {
4304 char_u *r;
4305 char_u numbuf[NUMBUFLEN];
4306 char_u *tofree;
4307
4308 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004309 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 vim_free(tofree);
4311 }
4312 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004313 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004314 {
4315 char *name = (char *)iptr->isn_arg.string;
4316
4317 smsg("%4d PUSHFUNC \"%s\"", current,
4318 name == NULL ? "[none]" : name);
4319 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004320 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004321 case ISN_PUSHCHANNEL:
4322#ifdef FEAT_JOB_CHANNEL
4323 {
4324 channel_T *channel = iptr->isn_arg.channel;
4325
4326 smsg("%4d PUSHCHANNEL %d", current,
4327 channel == NULL ? 0 : channel->ch_id);
4328 }
4329#endif
4330 break;
4331 case ISN_PUSHJOB:
4332#ifdef FEAT_JOB_CHANNEL
4333 {
4334 typval_T tv;
4335 char_u *name;
4336
4337 tv.v_type = VAR_JOB;
4338 tv.vval.v_job = iptr->isn_arg.job;
4339 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004340 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004341 }
4342#endif
4343 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 case ISN_PUSHEXC:
4345 smsg("%4d PUSH v:exception", current);
4346 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004347 case ISN_UNLET:
4348 smsg("%4d UNLET%s %s", current,
4349 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4350 iptr->isn_arg.unlet.ul_name);
4351 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004352 case ISN_UNLETENV:
4353 smsg("%4d UNLETENV%s $%s", current,
4354 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4355 iptr->isn_arg.unlet.ul_name);
4356 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004357 case ISN_UNLETINDEX:
4358 smsg("%4d UNLETINDEX", current);
4359 break;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004360 case ISN_UNLETRANGE:
4361 smsg("%4d UNLETRANGE", current);
4362 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004363 case ISN_LOCKCONST:
4364 smsg("%4d LOCKCONST", current);
4365 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004367 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004368 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 break;
4370 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004371 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004372 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 break;
4374
4375 // function call
4376 case ISN_BCALL:
4377 {
4378 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4379
4380 smsg("%4d BCALL %s(argc %d)", current,
4381 internal_func_name(cbfunc->cbf_idx),
4382 cbfunc->cbf_argcount);
4383 }
4384 break;
4385 case ISN_DCALL:
4386 {
4387 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4388 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4389 + cdfunc->cdf_idx;
4390
4391 smsg("%4d DCALL %s(argc %d)", current,
4392 df->df_ufunc->uf_name_exp != NULL
4393 ? df->df_ufunc->uf_name_exp
4394 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4395 }
4396 break;
4397 case ISN_UCALL:
4398 {
4399 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4400
4401 smsg("%4d UCALL %s(argc %d)", current,
4402 cufunc->cuf_name, cufunc->cuf_argcount);
4403 }
4404 break;
4405 case ISN_PCALL:
4406 {
4407 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4408
4409 smsg("%4d PCALL%s (argc %d)", current,
4410 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4411 }
4412 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004413 case ISN_PCALL_END:
4414 smsg("%4d PCALL end", current);
4415 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 case ISN_RETURN:
4417 smsg("%4d RETURN", current);
4418 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004419 case ISN_RETURN_ZERO:
4420 smsg("%4d RETURN 0", current);
4421 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 case ISN_FUNCREF:
4423 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004424 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004426 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004428 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 }
4430 break;
4431
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004432 case ISN_NEWFUNC:
4433 {
4434 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4435
4436 smsg("%4d NEWFUNC %s %s", current,
4437 newfunc->nf_lambda, newfunc->nf_global);
4438 }
4439 break;
4440
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004441 case ISN_DEF:
4442 {
4443 char_u *name = iptr->isn_arg.string;
4444
4445 smsg("%4d DEF %s", current,
4446 name == NULL ? (char_u *)"" : name);
4447 }
4448 break;
4449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 case ISN_JUMP:
4451 {
4452 char *when = "?";
4453
4454 switch (iptr->isn_arg.jump.jump_when)
4455 {
4456 case JUMP_ALWAYS:
4457 when = "JUMP";
4458 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 case JUMP_AND_KEEP_IF_TRUE:
4460 when = "JUMP_AND_KEEP_IF_TRUE";
4461 break;
4462 case JUMP_IF_FALSE:
4463 when = "JUMP_IF_FALSE";
4464 break;
4465 case JUMP_AND_KEEP_IF_FALSE:
4466 when = "JUMP_AND_KEEP_IF_FALSE";
4467 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004468 case JUMP_IF_COND_FALSE:
4469 when = "JUMP_IF_COND_FALSE";
4470 break;
4471 case JUMP_IF_COND_TRUE:
4472 when = "JUMP_IF_COND_TRUE";
4473 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004475 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 iptr->isn_arg.jump.jump_where);
4477 }
4478 break;
4479
4480 case ISN_FOR:
4481 {
4482 forloop_T *forloop = &iptr->isn_arg.forloop;
4483
4484 smsg("%4d FOR $%d -> %d", current,
4485 forloop->for_idx, forloop->for_end);
4486 }
4487 break;
4488
4489 case ISN_TRY:
4490 {
4491 try_T *try = &iptr->isn_arg.try;
4492
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004493 if (try->try_ref->try_finally == 0)
4494 smsg("%4d TRY catch -> %d, endtry -> %d",
4495 current,
4496 try->try_ref->try_catch,
4497 try->try_ref->try_endtry);
4498 else
4499 smsg("%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4500 current,
4501 try->try_ref->try_catch,
4502 try->try_ref->try_finally,
4503 try->try_ref->try_endtry);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504 }
4505 break;
4506 case ISN_CATCH:
4507 // TODO
4508 smsg("%4d CATCH", current);
4509 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004510 case ISN_TRYCONT:
4511 {
4512 trycont_T *trycont = &iptr->isn_arg.trycont;
4513
4514 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4515 trycont->tct_levels,
4516 trycont->tct_levels == 1 ? "" : "s",
4517 trycont->tct_where);
4518 }
4519 break;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004520 case ISN_FINALLY:
4521 smsg("%4d FINALLY", current);
4522 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 case ISN_ENDTRY:
4524 smsg("%4d ENDTRY", current);
4525 break;
4526 case ISN_THROW:
4527 smsg("%4d THROW", current);
4528 break;
4529
4530 // expression operations on number
4531 case ISN_OPNR:
4532 case ISN_OPFLOAT:
4533 case ISN_OPANY:
4534 {
4535 char *what;
4536 char *ins;
4537
4538 switch (iptr->isn_arg.op.op_type)
4539 {
4540 case EXPR_MULT: what = "*"; break;
4541 case EXPR_DIV: what = "/"; break;
4542 case EXPR_REM: what = "%"; break;
4543 case EXPR_SUB: what = "-"; break;
4544 case EXPR_ADD: what = "+"; break;
4545 default: what = "???"; break;
4546 }
4547 switch (iptr->isn_type)
4548 {
4549 case ISN_OPNR: ins = "OPNR"; break;
4550 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4551 case ISN_OPANY: ins = "OPANY"; break;
4552 default: ins = "???"; break;
4553 }
4554 smsg("%4d %s %s", current, ins, what);
4555 }
4556 break;
4557
4558 case ISN_COMPAREBOOL:
4559 case ISN_COMPARESPECIAL:
4560 case ISN_COMPARENR:
4561 case ISN_COMPAREFLOAT:
4562 case ISN_COMPARESTRING:
4563 case ISN_COMPAREBLOB:
4564 case ISN_COMPARELIST:
4565 case ISN_COMPAREDICT:
4566 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567 case ISN_COMPAREANY:
4568 {
4569 char *p;
4570 char buf[10];
4571 char *type;
4572
4573 switch (iptr->isn_arg.op.op_type)
4574 {
4575 case EXPR_EQUAL: p = "=="; break;
4576 case EXPR_NEQUAL: p = "!="; break;
4577 case EXPR_GREATER: p = ">"; break;
4578 case EXPR_GEQUAL: p = ">="; break;
4579 case EXPR_SMALLER: p = "<"; break;
4580 case EXPR_SEQUAL: p = "<="; break;
4581 case EXPR_MATCH: p = "=~"; break;
4582 case EXPR_IS: p = "is"; break;
4583 case EXPR_ISNOT: p = "isnot"; break;
4584 case EXPR_NOMATCH: p = "!~"; break;
4585 default: p = "???"; break;
4586 }
4587 STRCPY(buf, p);
4588 if (iptr->isn_arg.op.op_ic == TRUE)
4589 strcat(buf, "?");
4590 switch(iptr->isn_type)
4591 {
4592 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4593 case ISN_COMPARESPECIAL:
4594 type = "COMPARESPECIAL"; break;
4595 case ISN_COMPARENR: type = "COMPARENR"; break;
4596 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4597 case ISN_COMPARESTRING:
4598 type = "COMPARESTRING"; break;
4599 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4600 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4601 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4602 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4604 default: type = "???"; break;
4605 }
4606
4607 smsg("%4d %s %s", current, type, buf);
4608 }
4609 break;
4610
4611 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4612 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4613
4614 // expression operations
4615 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004616 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004617 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004618 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004619 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004620 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004621 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004622 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4623 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004624 case ISN_SLICE: smsg("%4d SLICE %lld",
4625 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004626 case ISN_GETITEM: smsg("%4d ITEM %lld",
4627 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004628 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4629 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 iptr->isn_arg.string); break;
4631 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4632
4633 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004634 case ISN_CHECKTYPE:
4635 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004636 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004637 char *tofree;
4638
Bram Moolenaare32e5162021-01-21 20:21:29 +01004639 if (ct->ct_arg_idx == 0)
4640 smsg("%4d CHECKTYPE %s stack[%d]", current,
4641 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004642 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004643 else
4644 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4645 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004646 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004647 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004648 vim_free(tofree);
4649 break;
4650 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004651 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4652 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4653 iptr->isn_arg.checklen.cl_min_len);
4654 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004655 case ISN_SETTYPE:
4656 {
4657 char *tofree;
4658
4659 smsg("%4d SETTYPE %s", current,
4660 type_name(iptr->isn_arg.type.ct_type, &tofree));
4661 vim_free(tofree);
4662 break;
4663 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004664 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 case ISN_2BOOL: if (iptr->isn_arg.number)
4666 smsg("%4d INVERT (!val)", current);
4667 else
4668 smsg("%4d 2BOOL (!!val)", current);
4669 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004670 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004671 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004672 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004673 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004674 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004675 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004676 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4677 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004678 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004679 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4680 smsg("%4d PUT %c above range",
4681 current, iptr->isn_arg.put.put_regname);
4682 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4683 smsg("%4d PUT %c range",
4684 current, iptr->isn_arg.put.put_regname);
4685 else
4686 smsg("%4d PUT %c %ld", current,
4687 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004688 (long)iptr->isn_arg.put.put_lnum);
4689 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690
Bram Moolenaar02194d22020-10-24 23:08:38 +02004691 // TODO: summarize modifiers
4692 case ISN_CMDMOD:
4693 {
4694 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004695 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004696 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4697
4698 buf = alloc(len + 1);
4699 if (buf != NULL)
4700 {
4701 (void)produce_cmdmods(
4702 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4703 smsg("%4d CMDMOD %s", current, buf);
4704 vim_free(buf);
4705 }
4706 break;
4707 }
4708 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004709
Bram Moolenaarb2049902021-01-24 12:53:53 +01004710 case ISN_PROF_START:
4711 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4712 break;
4713
4714 case ISN_PROF_END:
4715 smsg("%4d PROFILE END", current);
4716 break;
4717
Bram Moolenaar792f7862020-11-23 08:31:18 +01004718 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4719 iptr->isn_arg.unpack.unp_count,
4720 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4721 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004722 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4723 iptr->isn_arg.shuffle.shfl_item,
4724 iptr->isn_arg.shuffle.shfl_up);
4725 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 case ISN_DROP: smsg("%4d DROP", current); break;
4727 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004728
4729 out_flush(); // output one line at a time
4730 ui_breakcheck();
4731 if (got_int)
4732 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734}
4735
4736/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004737 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 * list, etc. Mostly like what JavaScript does, except that empty list and
4739 * empty dictionary are FALSE.
4740 */
4741 int
4742tv2bool(typval_T *tv)
4743{
4744 switch (tv->v_type)
4745 {
4746 case VAR_NUMBER:
4747 return tv->vval.v_number != 0;
4748 case VAR_FLOAT:
4749#ifdef FEAT_FLOAT
4750 return tv->vval.v_float != 0.0;
4751#else
4752 break;
4753#endif
4754 case VAR_PARTIAL:
4755 return tv->vval.v_partial != NULL;
4756 case VAR_FUNC:
4757 case VAR_STRING:
4758 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4759 case VAR_LIST:
4760 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4761 case VAR_DICT:
4762 return tv->vval.v_dict != NULL
4763 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4764 case VAR_BOOL:
4765 case VAR_SPECIAL:
4766 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4767 case VAR_JOB:
4768#ifdef FEAT_JOB_CHANNEL
4769 return tv->vval.v_job != NULL;
4770#else
4771 break;
4772#endif
4773 case VAR_CHANNEL:
4774#ifdef FEAT_JOB_CHANNEL
4775 return tv->vval.v_channel != NULL;
4776#else
4777 break;
4778#endif
4779 case VAR_BLOB:
4780 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4781 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004782 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783 case VAR_VOID:
4784 break;
4785 }
4786 return FALSE;
4787}
4788
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004789 void
4790emsg_using_string_as(typval_T *tv, int as_number)
4791{
4792 semsg(_(as_number ? e_using_string_as_number_str
4793 : e_using_string_as_bool_str),
4794 tv->vval.v_string == NULL
4795 ? (char_u *)"" : tv->vval.v_string);
4796}
4797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798/*
4799 * If "tv" is a string give an error and return FAIL.
4800 */
4801 int
4802check_not_string(typval_T *tv)
4803{
4804 if (tv->v_type == VAR_STRING)
4805 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004806 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004807 clear_tv(tv);
4808 return FAIL;
4809 }
4810 return OK;
4811}
4812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004813
4814#endif // FEAT_EVAL