blob: b1065ca8e8fd1ceb0630df3b170009159a1f6828 [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 Moolenaarbf67ea12020-05-02 17:52:42 +020027 int tcd_frame_idx; // ec_frame_idx when ISN_TRY was encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028 int tcd_catch_idx; // instruction of the first catch
29 int tcd_finally_idx; // instruction of the finally block
30 int tcd_caught; // catch block entered
31 int tcd_return; // when TRUE return from end of :finally
32} trycmd_T;
33
34
35// A stack is used to store:
36// - arguments passed to a :def function
37// - info about the calling function, to use when returning
38// - local variables
39// - temporary values
40//
41// In detail (FP == Frame Pointer):
42// arg1 first argument from caller (if present)
43// arg2 second argument from caller (if present)
44// extra_arg1 any missing optional argument default value
45// FP -> cur_func calling function
46// current previous instruction pointer
47// frame_ptr previous Frame Pointer
48// var1 space for local variable
49// var2 space for local variable
50// .... fixed space for max. number of local variables
51// temp temporary values
52// .... flexible space for temporary values (can grow big)
53
54/*
55 * Execution context.
56 */
57typedef struct {
58 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020059 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020061 garray_T *ec_outer_stack; // stack used for closures
62 int ec_outer_frame; // stack frame in ec_outer_stack
63
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064 garray_T ec_trystack; // stack of trycmd_T values
65 int ec_in_catch; // when TRUE in catch or finally block
66
67 int ec_dfunc_idx; // current function index
68 isn_T *ec_instr; // array with instructions
69 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020070
71 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072} ectx_T;
73
74// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020075#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 +010076
Bram Moolenaar418f1df2020-08-12 21:34:49 +020077 void
78to_string_error(vartype_T vartype)
79{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020080 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020081}
82
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010084 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 */
86 static int
87ufunc_argcount(ufunc_T *ufunc)
88{
89 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
90}
91
92/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010093 * Set the instruction index, depending on omitted arguments, where the default
94 * values are to be computed. If all optional arguments are present, start
95 * with the function body.
96 * The expression evaluation is at the start of the instructions:
97 * 0 -> EVAL default1
98 * STORE arg[-2]
99 * 1 -> EVAL default2
100 * STORE arg[-1]
101 * 2 -> function body
102 */
103 static void
104init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
105{
106 if (ufunc->uf_def_args.ga_len == 0)
107 ectx->ec_iidx = 0;
108 else
109 {
110 int defcount = ufunc->uf_args.ga_len - argcount;
111
112 // If there is a varargs argument defcount can be negative, no defaults
113 // to evaluate then.
114 if (defcount < 0)
115 defcount = 0;
116 ectx->ec_iidx = ufunc->uf_def_arg_idx[
117 ufunc->uf_def_args.ga_len - defcount];
118 }
119}
120
121/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200122 * Create a new list from "count" items at the bottom of the stack.
123 * When "count" is zero an empty list is added to the stack.
124 */
125 static int
126exe_newlist(int count, ectx_T *ectx)
127{
128 list_T *list = list_alloc_with_items(count);
129 int idx;
130 typval_T *tv;
131
132 if (list == NULL)
133 return FAIL;
134 for (idx = 0; idx < count; ++idx)
135 list_set_item(list, idx, STACK_TV_BOT(idx - count));
136
137 if (count > 0)
138 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200139 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200140 return FAIL;
141 else
142 ++ectx->ec_stack.ga_len;
143 tv = STACK_TV_BOT(-1);
144 tv->v_type = VAR_LIST;
145 tv->vval.v_list = list;
146 ++list->lv_refcount;
147 return OK;
148}
149
150/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 * Call compiled function "cdf_idx" from compiled code.
152 *
153 * Stack has:
154 * - current arguments (already there)
155 * - omitted optional argument (default values) added here
156 * - stack frame:
157 * - pointer to calling function
158 * - Index of next instruction in calling function
159 * - previous frame pointer
160 * - reserved space for local variables
161 */
162 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200163call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200165 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
167 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200168 int arg_to_add;
169 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200170 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200172 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173
174 if (dfunc->df_deleted)
175 {
176 emsg_funcname(e_func_deleted, ufunc->uf_name);
177 return FAIL;
178 }
179
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200180 if (ufunc->uf_va_name != NULL)
181 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200182 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200183 // Stack at time of call with 2 varargs:
184 // normal_arg
185 // optional_arg
186 // vararg_1
187 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200188 // After creating the list:
189 // normal_arg
190 // optional_arg
191 // vararg-list
192 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200193 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200194 // After creating the list
195 // normal_arg
196 // (space for optional_arg)
197 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200198 vararg_count = argcount - ufunc->uf_args.ga_len;
199 if (vararg_count < 0)
200 vararg_count = 0;
201 else
202 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200203 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200204 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200205
206 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200207 }
208
Bram Moolenaarfe270812020-04-11 22:31:27 +0200209 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200210 if (arg_to_add < 0)
211 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200212 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200213 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200214 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200215 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200216 return FAIL;
217 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200218
219 // Reserve space for:
220 // - missing arguments
221 // - stack frame
222 // - local variables
223 // - if needed: a counter for number of closures created in
224 // ectx->ec_funcrefs.
225 varcount = dfunc->df_varcount + dfunc->df_has_closure;
226 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
227 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 return FAIL;
229
Bram Moolenaarfe270812020-04-11 22:31:27 +0200230 // Move the vararg-list to below the missing optional arguments.
231 if (vararg_count > 0 && arg_to_add > 0)
232 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100233
234 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200235 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200236 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200237 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238
239 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
241 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200242 STACK_TV_BOT(2)->vval.v_string = (void *)ectx->ec_outer_stack;
243 STACK_TV_BOT(3)->vval.v_number = ectx->ec_outer_frame;
244 STACK_TV_BOT(4)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200245 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
247 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200248 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200250 if (dfunc->df_has_closure)
251 {
252 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
253
254 tv->v_type = VAR_NUMBER;
255 tv->vval.v_number = 0;
256 }
257 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258
259 // Set execution state to the start of the called function.
260 ectx->ec_dfunc_idx = cdf_idx;
261 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200262 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
263 if (entry != NULL)
264 {
265 // Set the script context to the script where the function was defined.
266 // TODO: save more than the SID?
267 entry->es_save_sid = current_sctx.sc_sid;
268 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
269 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100270
271 // Decide where to start execution, handles optional arguments.
272 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100273
274 return OK;
275}
276
277// Get pointer to item in the stack.
278#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
279
280/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200281 * Used when returning from a function: Check if any closure is still
282 * referenced. If so then move the arguments and variables to a separate piece
283 * of stack to be used when the closure is called.
284 * When "free_arguments" is TRUE the arguments are to be freed.
285 * Returns FAIL when out of memory.
286 */
287 static int
288handle_closure_in_use(ectx_T *ectx, int free_arguments)
289{
290 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
291 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200292 int argcount;
293 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200294 int idx;
295 typval_T *tv;
296 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200297 garray_T *gap = &ectx->ec_funcrefs;
298 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200299
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200300 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200301 return OK; // function was freed
302 if (dfunc->df_has_closure == 0)
303 return OK; // no closures
304 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
305 closure_count = tv->vval.v_number;
306 if (closure_count == 0)
307 return OK; // no funcrefs created
308
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200309 argcount = ufunc_argcount(dfunc->df_ufunc);
310 top = ectx->ec_frame_idx - argcount;
311
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200312 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200313 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200314 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200315 partial_T *pt;
316 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200317
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200318 if (off < 0)
319 continue; // count is off or already done
320 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200321 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200322 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200323 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200324 int i;
325
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200326 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200327 // unreferenced on return.
328 for (i = 0; i < dfunc->df_varcount; ++i)
329 {
330 typval_T *stv = STACK_TV(ectx->ec_frame_idx
331 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200332 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200333 --refcount;
334 }
335 if (refcount > 1)
336 {
337 closure_in_use = TRUE;
338 break;
339 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200340 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200341 }
342
343 if (closure_in_use)
344 {
345 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
346 typval_T *stack;
347
348 // A closure is using the arguments and/or local variables.
349 // Move them to the called function.
350 if (funcstack == NULL)
351 return FAIL;
352 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
353 + dfunc->df_varcount;
354 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
355 funcstack->fs_ga.ga_data = stack;
356 if (stack == NULL)
357 {
358 vim_free(funcstack);
359 return FAIL;
360 }
361
362 // Move or copy the arguments.
363 for (idx = 0; idx < argcount; ++idx)
364 {
365 tv = STACK_TV(top + idx);
366 if (free_arguments)
367 {
368 *(stack + idx) = *tv;
369 tv->v_type = VAR_UNKNOWN;
370 }
371 else
372 copy_tv(tv, stack + idx);
373 }
374 // Move the local variables.
375 for (idx = 0; idx < dfunc->df_varcount; ++idx)
376 {
377 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200378
379 // Do not copy a partial created for a local function.
380 // TODO: this won't work if the closure actually uses it. But when
381 // keeping it it gets complicated: it will create a reference cycle
382 // inside the partial, thus needs special handling for garbage
383 // collection.
384 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
385 {
386 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200387
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200388 for (i = 0; i < closure_count; ++i)
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200389 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200390 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
391 - closure_count + i];
392 if (tv->vval.v_partial == pt)
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200393 break;
394 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200395 if (i < closure_count)
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200396 continue;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200397 }
398
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200399 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
400 tv->v_type = VAR_UNKNOWN;
401 }
402
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200403 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200404 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200405 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
406 - closure_count + idx];
407 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200408 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200409 ++funcstack->fs_refcount;
410 pt->pt_funcstack = funcstack;
411 pt->pt_ectx_stack = &funcstack->fs_ga;
412 pt->pt_ectx_frame = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200413 }
414 }
415 }
416
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200417 for (idx = 0; idx < closure_count; ++idx)
418 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
419 - closure_count + idx]);
420 gap->ga_len -= closure_count;
421 if (gap->ga_len == 0)
422 ga_clear(gap);
423
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200424 return OK;
425}
426
427/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100428 * Return from the current function.
429 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200430 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100431func_return(ectx_T *ectx)
432{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200434 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
435 + ectx->ec_dfunc_idx;
436 int argcount = ufunc_argcount(dfunc->df_ufunc);
437 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200438 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439
440 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200441 entry = estack_pop();
442 if (entry != NULL)
443 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100444
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200445 if (handle_closure_in_use(ectx, TRUE) == FAIL)
446 return FAIL;
447
448 // Clear the arguments.
449 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
450 clear_tv(STACK_TV(idx));
451
452 // Clear local variables and temp values, but not the return value.
453 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100454 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100456
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100457 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200458 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
459 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200460 ectx->ec_outer_stack =
461 (void *)STACK_TV(ectx->ec_frame_idx + 2)->vval.v_string;
462 ectx->ec_outer_frame = STACK_TV(ectx->ec_frame_idx + 3)->vval.v_number;
463 // restoring ec_frame_idx must be last
464 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 4)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100465 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
466 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100467
468 // Reset the stack to the position before the call, move the return value
469 // to the top of the stack.
470 idx = ectx->ec_stack.ga_len - 1;
471 ectx->ec_stack.ga_len = top + 1;
472 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200473
474 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475}
476
477#undef STACK_TV
478
479/*
480 * Prepare arguments and rettv for calling a builtin or user function.
481 */
482 static int
483call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
484{
485 int idx;
486 typval_T *tv;
487
488 // Move arguments from bottom of the stack to argvars[] and add terminator.
489 for (idx = 0; idx < argcount; ++idx)
490 argvars[idx] = *STACK_TV_BOT(idx - argcount);
491 argvars[argcount].v_type = VAR_UNKNOWN;
492
493 // Result replaces the arguments on the stack.
494 if (argcount > 0)
495 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200496 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497 return FAIL;
498 else
499 ++ectx->ec_stack.ga_len;
500
501 // Default return value is zero.
502 tv = STACK_TV_BOT(-1);
503 tv->v_type = VAR_NUMBER;
504 tv->vval.v_number = 0;
505
506 return OK;
507}
508
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200509// Ugly global to avoid passing the execution context around through many
510// layers.
511static ectx_T *current_ectx = NULL;
512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100513/*
514 * Call a builtin function by index.
515 */
516 static int
517call_bfunc(int func_idx, int argcount, ectx_T *ectx)
518{
519 typval_T argvars[MAX_FUNC_ARGS];
520 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200521 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200522 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523
524 if (call_prepare(argcount, argvars, ectx) == FAIL)
525 return FAIL;
526
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200527 // Call the builtin function. Set "current_ectx" so that when it
528 // recursively invokes call_def_function() a closure context can be set.
529 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200531 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532
533 // Clear the arguments.
534 for (idx = 0; idx < argcount; ++idx)
535 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200536
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200537 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200538 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100539 return OK;
540}
541
542/*
543 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100544 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545 */
546 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100547call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548{
549 typval_T argvars[MAX_FUNC_ARGS];
550 funcexe_T funcexe;
551 int error;
552 int idx;
Bram Moolenaared677f52020-08-12 16:38:10 +0200553 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200555 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200556 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
557 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200558 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100559 {
560 // The function has been compiled, can call it quickly. For a function
561 // that was defined later: we can call it directly next time.
562 if (iptr != NULL)
563 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100564 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100565 iptr->isn_type = ISN_DCALL;
566 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
567 iptr->isn_arg.dfunc.cdf_argcount = argcount;
568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100570 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571
572 if (call_prepare(argcount, argvars, ectx) == FAIL)
573 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200574 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 funcexe.evaluate = TRUE;
576
577 // Call the user function. Result goes in last position on the stack.
578 // TODO: add selfdict if there is one
579 error = call_user_func_check(ufunc, argcount, argvars,
580 STACK_TV_BOT(-1), &funcexe, NULL);
581
582 // Clear the arguments.
583 for (idx = 0; idx < argcount; ++idx)
584 clear_tv(&argvars[idx]);
585
586 if (error != FCERR_NONE)
587 {
588 user_func_error(error, ufunc->uf_name);
589 return FAIL;
590 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200591 if (called_emsg > called_emsg_before)
592 // Error other than from calling the function itself.
593 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594 return OK;
595}
596
597/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200598 * Return TRUE if an error was given or CTRL-C was pressed.
599 */
600 static int
601vim9_aborting(int prev_called_emsg)
602{
603 return called_emsg > prev_called_emsg || got_int || did_throw;
604}
605
606/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607 * Execute a function by "name".
608 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100609 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610 * Returns FAIL if not found without an error message.
611 */
612 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100613call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614{
615 ufunc_T *ufunc;
616
617 if (builtin_function(name, -1))
618 {
619 int func_idx = find_internal_func(name);
620
621 if (func_idx < 0)
622 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200623 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 return FAIL;
625 return call_bfunc(func_idx, argcount, ectx);
626 }
627
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200628 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200629
630 if (ufunc == NULL)
631 {
632 int called_emsg_before = called_emsg;
633
634 if (script_autoload(name, TRUE))
635 // loaded a package, search for the function again
636 ufunc = find_func(name, FALSE, NULL);
637 if (vim9_aborting(called_emsg_before))
638 return FAIL; // bail out if loading the script caused an error
639 }
640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100642 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643
644 return FAIL;
645}
646
647 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200648call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200650 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200651 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200653 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654
655 if (tv->v_type == VAR_PARTIAL)
656 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200657 partial_T *pt = tv->vval.v_partial;
658 int i;
659
660 if (pt->pt_argc > 0)
661 {
662 // Make space for arguments from the partial, shift the "argcount"
663 // arguments up.
664 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
665 return FAIL;
666 for (i = 1; i <= argcount; ++i)
667 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
668 ectx->ec_stack.ga_len += pt->pt_argc;
669 argcount += pt->pt_argc;
670
671 // copy the arguments from the partial onto the stack
672 for (i = 0; i < pt->pt_argc; ++i)
673 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
674 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
676 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200677 {
678 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
679
680 // closure may need the function context where it was defined
681 ectx->ec_outer_stack = pt->pt_ectx_stack;
682 ectx->ec_outer_frame = pt->pt_ectx_frame;
683
684 return ret;
685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686 name = pt->pt_name;
687 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200688 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200690 if (name != NULL)
691 {
692 char_u fname_buf[FLEN_FIXED + 1];
693 char_u *tofree = NULL;
694 int error = FCERR_NONE;
695 char_u *fname;
696
697 // May need to translate <SNR>123_ to K_SNR.
698 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
699 if (error != FCERR_NONE)
700 res = FAIL;
701 else
702 res = call_by_name(fname, argcount, ectx, NULL);
703 vim_free(tofree);
704 }
705
706 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 {
708 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200709 semsg(_(e_unknownfunc),
710 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 return FAIL;
712 }
713 return OK;
714}
715
716/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200717 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
718 * TRUE.
719 */
720 static int
721error_if_locked(int lock, char *error)
722{
723 if (lock & (VAR_LOCKED | VAR_FIXED))
724 {
725 emsg(_(error));
726 return TRUE;
727 }
728 return FALSE;
729}
730
731/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100732 * Store "tv" in variable "name".
733 * This is for s: and g: variables.
734 */
735 static void
736store_var(char_u *name, typval_T *tv)
737{
738 funccal_entry_T entry;
739
740 save_funccal(&entry);
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200741 set_var_const(name, NULL, tv, FALSE, ASSIGN_NO_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100742 restore_funccal();
743}
744
Bram Moolenaard3aac292020-04-19 14:32:17 +0200745
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100746/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747 * Execute a function by "name".
748 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100749 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 */
751 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100752call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753{
Bram Moolenaared677f52020-08-12 16:38:10 +0200754 int called_emsg_before = called_emsg;
755 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756
Bram Moolenaared677f52020-08-12 16:38:10 +0200757 res = call_by_name(name, argcount, ectx, iptr);
758 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200760 dictitem_T *v;
761
762 v = find_var(name, NULL, FALSE);
763 if (v == NULL)
764 {
765 semsg(_(e_unknownfunc), name);
766 return FAIL;
767 }
768 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
769 {
770 semsg(_(e_unknownfunc), name);
771 return FAIL;
772 }
773 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200775 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776}
777
778/*
779 * Call a "def" function from old Vim script.
780 * Return OK or FAIL.
781 */
782 int
783call_def_function(
784 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200785 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200787 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788 typval_T *rettv) // return value
789{
790 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200791 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200792 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793 typval_T *tv;
794 int idx;
795 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100796 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200797 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200798 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200799 int called_emsg_before = called_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200800 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100801
802// Get pointer to item in the stack.
803#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
804
805// Get pointer to item at the bottom of the stack, -1 is the bottom.
806#undef STACK_TV_BOT
807#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
808
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200809// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200810#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 +0100811
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200812// Like STACK_TV_VAR but use the outer scope
813#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
814
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200815 if (ufunc->uf_def_status == UF_NOT_COMPILED
816 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200817 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200818 {
819 if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200820 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200821 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200822 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200823 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200824
Bram Moolenaar09689a02020-05-09 22:50:08 +0200825 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200826 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200827 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
828 + ufunc->uf_dfunc_idx;
829 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200830 {
831 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200832 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200833 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200834 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200836 CLEAR_FIELD(ectx);
837 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
838 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
839 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
840 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200842 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843
844 // Put arguments on the stack.
845 for (idx = 0; idx < argc; ++idx)
846 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200847 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200848 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
849 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200850 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 copy_tv(&argv[idx], STACK_TV_BOT(0));
852 ++ectx.ec_stack.ga_len;
853 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200854
855 // Turn varargs into a list. Empty list if no args.
856 if (ufunc->uf_va_name != NULL)
857 {
858 int vararg_count = argc - ufunc->uf_args.ga_len;
859
860 if (vararg_count < 0)
861 vararg_count = 0;
862 else
863 argc -= vararg_count;
864 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200865 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200866
867 // Check the type of the list items.
868 tv = STACK_TV_BOT(-1);
869 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +0200870 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200871 && ufunc->uf_va_type->tt_member != &t_any
872 && tv->vval.v_list != NULL)
873 {
874 type_T *expected = ufunc->uf_va_type->tt_member;
875 listitem_T *li = tv->vval.v_list->lv_first;
876
877 for (idx = 0; idx < vararg_count; ++idx)
878 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200879 if (check_typval_type(expected, &li->li_tv,
880 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200881 goto failed_early;
882 li = li->li_next;
883 }
884 }
885
Bram Moolenaar23e03252020-04-12 22:22:31 +0200886 if (defcount > 0)
887 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200888 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200889 --ectx.ec_stack.ga_len;
890 }
891
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100892 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200893 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100894 if (defcount > 0)
895 for (idx = 0; idx < defcount; ++idx)
896 {
897 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
898 ++ectx.ec_stack.ga_len;
899 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200900 if (ufunc->uf_va_name != NULL)
901 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902
903 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200904 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
905 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200907 if (partial != NULL)
908 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200909 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
910 {
911 // TODO: is this always the right way?
912 ectx.ec_outer_stack = &current_ectx->ec_stack;
913 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
914 }
915 else
916 {
917 ectx.ec_outer_stack = partial->pt_ectx_stack;
918 ectx.ec_outer_frame = partial->pt_ectx_frame;
919 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200920 }
921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922 // dummy frame entries
923 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
924 {
925 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
926 ++ectx.ec_stack.ga_len;
927 }
928
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200929 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200930 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200931 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
932 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200934 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200935 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200936 ectx.ec_stack.ga_len += dfunc->df_varcount;
937 if (dfunc->df_has_closure)
938 {
939 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
940 STACK_TV_VAR(idx)->vval.v_number = 0;
941 ++ectx.ec_stack.ga_len;
942 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200943
944 ectx.ec_instr = dfunc->df_instr;
945 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100946
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200947 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +0200948 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200949 estack_push_ufunc(ufunc, 1);
950 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200951 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
952
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200953 // Do turn errors into exceptions.
954 suppress_errthrow = FALSE;
955
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100956 // Decide where to start execution, handles optional arguments.
957 init_instr_idx(ufunc, argc, &ectx);
958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 for (;;)
960 {
961 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100962
Bram Moolenaar270d0382020-05-15 21:42:53 +0200963 if (++breakcheck_count >= 100)
964 {
965 line_breakcheck();
966 breakcheck_count = 0;
967 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100968 if (got_int)
969 {
970 // Turn CTRL-C into an exception.
971 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100972 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100973 goto failed;
974 did_throw = TRUE;
975 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976
Bram Moolenaara26b9702020-04-18 19:53:28 +0200977 if (did_emsg && msg_list != NULL && *msg_list != NULL)
978 {
979 // Turn an error message into an exception.
980 did_emsg = FALSE;
981 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
982 goto failed;
983 did_throw = TRUE;
984 *msg_list = NULL;
985 }
986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987 if (did_throw && !ectx.ec_in_catch)
988 {
989 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100990 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991
992 // An exception jumps to the first catch, finally, or returns from
993 // the current function.
994 if (trystack->ga_len > 0)
995 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200996 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997 {
998 // jump to ":catch" or ":finally"
999 ectx.ec_in_catch = TRUE;
1000 ectx.ec_iidx = trycmd->tcd_catch_idx;
1001 }
1002 else
1003 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001004 // Not inside try or need to return from current functions.
1005 // Push a dummy return value.
1006 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1007 goto failed;
1008 tv = STACK_TV_BOT(0);
1009 tv->v_type = VAR_NUMBER;
1010 tv->vval.v_number = 0;
1011 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001012 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001014 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001015 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001016 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1017 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001018 goto done;
1019 }
1020
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001021 if (func_return(&ectx) == FAIL)
1022 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023 }
1024 continue;
1025 }
1026
1027 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1028 switch (iptr->isn_type)
1029 {
1030 // execute Ex command line
1031 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001032 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 do_cmdline_cmd(iptr->isn_arg.string);
1034 break;
1035
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001036 // execute Ex command from pieces on the stack
1037 case ISN_EXECCONCAT:
1038 {
1039 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001040 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001041 int pass;
1042 int i;
1043 char_u *cmd = NULL;
1044 char_u *str;
1045
1046 for (pass = 1; pass <= 2; ++pass)
1047 {
1048 for (i = 0; i < count; ++i)
1049 {
1050 tv = STACK_TV_BOT(i - count);
1051 str = tv->vval.v_string;
1052 if (str != NULL && *str != NUL)
1053 {
1054 if (pass == 2)
1055 STRCPY(cmd + len, str);
1056 len += STRLEN(str);
1057 }
1058 if (pass == 2)
1059 clear_tv(tv);
1060 }
1061 if (pass == 1)
1062 {
1063 cmd = alloc(len + 1);
1064 if (cmd == NULL)
1065 goto failed;
1066 len = 0;
1067 }
1068 }
1069
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001070 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001071 do_cmdline_cmd(cmd);
1072 vim_free(cmd);
1073 }
1074 break;
1075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076 // execute :echo {string} ...
1077 case ISN_ECHO:
1078 {
1079 int count = iptr->isn_arg.echo.echo_count;
1080 int atstart = TRUE;
1081 int needclr = TRUE;
1082
1083 for (idx = 0; idx < count; ++idx)
1084 {
1085 tv = STACK_TV_BOT(idx - count);
1086 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1087 &atstart, &needclr);
1088 clear_tv(tv);
1089 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001090 if (needclr)
1091 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092 ectx.ec_stack.ga_len -= count;
1093 }
1094 break;
1095
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001096 // :execute {string} ...
1097 // :echomsg {string} ...
1098 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001099 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001100 case ISN_ECHOMSG:
1101 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001102 {
1103 int count = iptr->isn_arg.number;
1104 garray_T ga;
1105 char_u buf[NUMBUFLEN];
1106 char_u *p;
1107 int len;
1108 int failed = FALSE;
1109
1110 ga_init2(&ga, 1, 80);
1111 for (idx = 0; idx < count; ++idx)
1112 {
1113 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001114 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001115 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001116 if (tv->v_type == VAR_CHANNEL
1117 || tv->v_type == VAR_JOB)
1118 {
1119 SOURCING_LNUM = iptr->isn_lnum;
1120 emsg(_(e_inval_string));
1121 break;
1122 }
1123 else
1124 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001125 }
1126 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001127 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001128
1129 len = (int)STRLEN(p);
1130 if (ga_grow(&ga, len + 2) == FAIL)
1131 failed = TRUE;
1132 else
1133 {
1134 if (ga.ga_len > 0)
1135 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1136 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1137 ga.ga_len += len;
1138 }
1139 clear_tv(tv);
1140 }
1141 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001142 if (failed)
1143 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001144
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001145 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001146 {
1147 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001148 {
1149 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001150 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaar430deb12020-08-23 16:29:11 +02001151 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001152 else
1153 {
1154 msg_sb_eol();
1155 if (iptr->isn_type == ISN_ECHOMSG)
1156 {
1157 msg_attr(ga.ga_data, echo_attr);
1158 out_flush();
1159 }
1160 else
1161 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001162 SOURCING_LNUM = iptr->isn_lnum;
1163 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001164 }
1165 }
1166 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001167 ga_clear(&ga);
1168 }
1169 break;
1170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 // load local variable or argument
1172 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001173 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 goto failed;
1175 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1176 ++ectx.ec_stack.ga_len;
1177 break;
1178
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001179 // load variable or argument from outer scope
1180 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001181 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001182 goto failed;
1183 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1184 STACK_TV_BOT(0));
1185 ++ectx.ec_stack.ga_len;
1186 break;
1187
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 // load v: variable
1189 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001190 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191 goto failed;
1192 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1193 ++ectx.ec_stack.ga_len;
1194 break;
1195
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001196 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197 case ISN_LOADSCRIPT:
1198 {
1199 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001200 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 svar_T *sv;
1202
1203 sv = ((svar_T *)si->sn_var_vals.ga_data)
1204 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001205 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 goto failed;
1207 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1208 ++ectx.ec_stack.ga_len;
1209 }
1210 break;
1211
1212 // load s: variable in old script
1213 case ISN_LOADS:
1214 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001215 hashtab_T *ht = &SCRIPT_VARS(
1216 iptr->isn_arg.loadstore.ls_sid);
1217 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 if (di == NULL)
1221 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001222 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001223 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001224 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 }
1226 else
1227 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001228 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 goto failed;
1230 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1231 ++ectx.ec_stack.ga_len;
1232 }
1233 }
1234 break;
1235
Bram Moolenaard3aac292020-04-19 14:32:17 +02001236 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001238 case ISN_LOADB:
1239 case ISN_LOADW:
1240 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001241 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001242 dictitem_T *di = NULL;
1243 hashtab_T *ht = NULL;
1244 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001245
Bram Moolenaard3aac292020-04-19 14:32:17 +02001246 switch (iptr->isn_type)
1247 {
1248 case ISN_LOADG:
1249 ht = get_globvar_ht();
1250 namespace = 'g';
1251 break;
1252 case ISN_LOADB:
1253 ht = &curbuf->b_vars->dv_hashtab;
1254 namespace = 'b';
1255 break;
1256 case ISN_LOADW:
1257 ht = &curwin->w_vars->dv_hashtab;
1258 namespace = 'w';
1259 break;
1260 case ISN_LOADT:
1261 ht = &curtab->tp_vars->dv_hashtab;
1262 namespace = 't';
1263 break;
1264 default: // Cannot reach here
1265 goto failed;
1266 }
1267 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 if (di == NULL)
1270 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001271 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001272 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001273 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001274 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 }
1276 else
1277 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001278 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 goto failed;
1280 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1281 ++ectx.ec_stack.ga_len;
1282 }
1283 }
1284 break;
1285
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001286 // load g:/b:/w:/t: namespace
1287 case ISN_LOADGDICT:
1288 case ISN_LOADBDICT:
1289 case ISN_LOADWDICT:
1290 case ISN_LOADTDICT:
1291 {
1292 dict_T *d = NULL;
1293
1294 switch (iptr->isn_type)
1295 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001296 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1297 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1298 case ISN_LOADWDICT: d = curwin->w_vars; break;
1299 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001300 default: // Cannot reach here
1301 goto failed;
1302 }
1303 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1304 goto failed;
1305 tv = STACK_TV_BOT(0);
1306 tv->v_type = VAR_DICT;
1307 tv->v_lock = 0;
1308 tv->vval.v_dict = d;
1309 ++ectx.ec_stack.ga_len;
1310 }
1311 break;
1312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 // load &option
1314 case ISN_LOADOPT:
1315 {
1316 typval_T optval;
1317 char_u *name = iptr->isn_arg.string;
1318
Bram Moolenaara8c17702020-04-01 21:17:24 +02001319 // This is not expected to fail, name is checked during
1320 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001321 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001323 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001324 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 *STACK_TV_BOT(0) = optval;
1326 ++ectx.ec_stack.ga_len;
1327 }
1328 break;
1329
1330 // load $ENV
1331 case ISN_LOADENV:
1332 {
1333 typval_T optval;
1334 char_u *name = iptr->isn_arg.string;
1335
Bram Moolenaar270d0382020-05-15 21:42:53 +02001336 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001338 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001339 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 *STACK_TV_BOT(0) = optval;
1341 ++ectx.ec_stack.ga_len;
1342 }
1343 break;
1344
1345 // load @register
1346 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001347 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 goto failed;
1349 tv = STACK_TV_BOT(0);
1350 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001351 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 tv->vval.v_string = get_reg_contents(
1353 iptr->isn_arg.number, GREG_EXPR_SRC);
1354 ++ectx.ec_stack.ga_len;
1355 break;
1356
1357 // store local variable
1358 case ISN_STORE:
1359 --ectx.ec_stack.ga_len;
1360 tv = STACK_TV_VAR(iptr->isn_arg.number);
1361 clear_tv(tv);
1362 *tv = *STACK_TV_BOT(0);
1363 break;
1364
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001365 // store variable or argument in outer scope
1366 case ISN_STOREOUTER:
1367 --ectx.ec_stack.ga_len;
1368 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1369 clear_tv(tv);
1370 *tv = *STACK_TV_BOT(0);
1371 break;
1372
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001373 // store s: variable in old script
1374 case ISN_STORES:
1375 {
1376 hashtab_T *ht = &SCRIPT_VARS(
1377 iptr->isn_arg.loadstore.ls_sid);
1378 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001379 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001380
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001381 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001382 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001383 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001384 else
1385 {
1386 clear_tv(&di->di_tv);
1387 di->di_tv = *STACK_TV_BOT(0);
1388 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001389 }
1390 break;
1391
1392 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 case ISN_STORESCRIPT:
1394 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001395 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 iptr->isn_arg.script.script_sid);
1397 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1398 + iptr->isn_arg.script.script_idx;
1399
1400 --ectx.ec_stack.ga_len;
1401 clear_tv(sv->sv_tv);
1402 *sv->sv_tv = *STACK_TV_BOT(0);
1403 }
1404 break;
1405
1406 // store option
1407 case ISN_STOREOPT:
1408 {
1409 long n = 0;
1410 char_u *s = NULL;
1411 char *msg;
1412
1413 --ectx.ec_stack.ga_len;
1414 tv = STACK_TV_BOT(0);
1415 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001416 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001418 if (s == NULL)
1419 s = (char_u *)"";
1420 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001422 // must be VAR_NUMBER, CHECKTYPE makes sure
1423 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1425 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001426 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 if (msg != NULL)
1428 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001429 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001431 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 }
1434 break;
1435
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001436 // store $ENV
1437 case ISN_STOREENV:
1438 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001439 tv = STACK_TV_BOT(0);
1440 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1441 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001442 break;
1443
1444 // store @r
1445 case ISN_STOREREG:
1446 {
1447 int reg = iptr->isn_arg.number;
1448
1449 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001450 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001451 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001452 tv_get_string(tv), -1, FALSE);
1453 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001454 }
1455 break;
1456
1457 // store v: variable
1458 case ISN_STOREV:
1459 --ectx.ec_stack.ga_len;
1460 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1461 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001462 // should not happen, type is checked when compiling
1463 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001464 break;
1465
Bram Moolenaard3aac292020-04-19 14:32:17 +02001466 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001468 case ISN_STOREB:
1469 case ISN_STOREW:
1470 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 {
1472 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001473 hashtab_T *ht;
1474 switch (iptr->isn_type)
1475 {
1476 case ISN_STOREG:
1477 ht = get_globvar_ht();
1478 break;
1479 case ISN_STOREB:
1480 ht = &curbuf->b_vars->dv_hashtab;
1481 break;
1482 case ISN_STOREW:
1483 ht = &curwin->w_vars->dv_hashtab;
1484 break;
1485 case ISN_STORET:
1486 ht = &curtab->tp_vars->dv_hashtab;
1487 break;
1488 default: // Cannot reach here
1489 goto failed;
1490 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491
1492 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001493 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001495 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 else
1497 {
1498 clear_tv(&di->di_tv);
1499 di->di_tv = *STACK_TV_BOT(0);
1500 }
1501 }
1502 break;
1503
1504 // store number in local variable
1505 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001506 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 clear_tv(tv);
1508 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001509 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 break;
1511
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001512 // store value in list variable
1513 case ISN_STORELIST:
1514 {
1515 typval_T *tv_idx = STACK_TV_BOT(-2);
1516 varnumber_T lidx = tv_idx->vval.v_number;
1517 typval_T *tv_list = STACK_TV_BOT(-1);
1518 list_T *list = tv_list->vval.v_list;
1519
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001520 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001521 if (lidx < 0 && list->lv_len + lidx >= 0)
1522 // negative index is relative to the end
1523 lidx = list->lv_len + lidx;
1524 if (lidx < 0 || lidx > list->lv_len)
1525 {
1526 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001527 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001528 }
1529 tv = STACK_TV_BOT(-3);
1530 if (lidx < list->lv_len)
1531 {
1532 listitem_T *li = list_find(list, lidx);
1533
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001534 if (error_if_locked(li->li_tv.v_lock,
1535 e_cannot_change_list_item))
1536 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001537 // overwrite existing list item
1538 clear_tv(&li->li_tv);
1539 li->li_tv = *tv;
1540 }
1541 else
1542 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001543 if (error_if_locked(list->lv_lock,
1544 e_cannot_change_list))
1545 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001546 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001547 if (list_append_tv(list, tv) == FAIL)
1548 goto failed;
1549 clear_tv(tv);
1550 }
1551 clear_tv(tv_idx);
1552 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001553 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001554 }
1555 break;
1556
1557 // store value in dict variable
1558 case ISN_STOREDICT:
1559 {
1560 typval_T *tv_key = STACK_TV_BOT(-2);
1561 char_u *key = tv_key->vval.v_string;
1562 typval_T *tv_dict = STACK_TV_BOT(-1);
1563 dict_T *dict = tv_dict->vval.v_dict;
1564 dictitem_T *di;
1565
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001566 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001567 if (dict == NULL)
1568 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001569 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001570 goto on_error;
1571 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001572 if (key == NULL)
1573 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001574 tv = STACK_TV_BOT(-3);
1575 di = dict_find(dict, key, -1);
1576 if (di != NULL)
1577 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001578 if (error_if_locked(di->di_tv.v_lock,
1579 e_cannot_change_dict_item))
1580 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001581 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001582 clear_tv(&di->di_tv);
1583 di->di_tv = *tv;
1584 }
1585 else
1586 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001587 if (error_if_locked(dict->dv_lock,
1588 e_cannot_change_dict))
1589 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001590 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001591 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1592 goto failed;
1593 clear_tv(tv);
1594 }
1595 clear_tv(tv_key);
1596 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001597 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001598 }
1599 break;
1600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 // push constant
1602 case ISN_PUSHNR:
1603 case ISN_PUSHBOOL:
1604 case ISN_PUSHSPEC:
1605 case ISN_PUSHF:
1606 case ISN_PUSHS:
1607 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001608 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001609 case ISN_PUSHCHANNEL:
1610 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001611 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 goto failed;
1613 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001614 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 ++ectx.ec_stack.ga_len;
1616 switch (iptr->isn_type)
1617 {
1618 case ISN_PUSHNR:
1619 tv->v_type = VAR_NUMBER;
1620 tv->vval.v_number = iptr->isn_arg.number;
1621 break;
1622 case ISN_PUSHBOOL:
1623 tv->v_type = VAR_BOOL;
1624 tv->vval.v_number = iptr->isn_arg.number;
1625 break;
1626 case ISN_PUSHSPEC:
1627 tv->v_type = VAR_SPECIAL;
1628 tv->vval.v_number = iptr->isn_arg.number;
1629 break;
1630#ifdef FEAT_FLOAT
1631 case ISN_PUSHF:
1632 tv->v_type = VAR_FLOAT;
1633 tv->vval.v_float = iptr->isn_arg.fnumber;
1634 break;
1635#endif
1636 case ISN_PUSHBLOB:
1637 blob_copy(iptr->isn_arg.blob, tv);
1638 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001639 case ISN_PUSHFUNC:
1640 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001641 if (iptr->isn_arg.string == NULL)
1642 tv->vval.v_string = NULL;
1643 else
1644 tv->vval.v_string =
1645 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001646 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001647 case ISN_PUSHCHANNEL:
1648#ifdef FEAT_JOB_CHANNEL
1649 tv->v_type = VAR_CHANNEL;
1650 tv->vval.v_channel = iptr->isn_arg.channel;
1651 if (tv->vval.v_channel != NULL)
1652 ++tv->vval.v_channel->ch_refcount;
1653#endif
1654 break;
1655 case ISN_PUSHJOB:
1656#ifdef FEAT_JOB_CHANNEL
1657 tv->v_type = VAR_JOB;
1658 tv->vval.v_job = iptr->isn_arg.job;
1659 if (tv->vval.v_job != NULL)
1660 ++tv->vval.v_job->jv_refcount;
1661#endif
1662 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 default:
1664 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001665 tv->vval.v_string = vim_strsave(
1666 iptr->isn_arg.string == NULL
1667 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668 }
1669 break;
1670
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001671 case ISN_UNLET:
1672 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1673 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001674 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001675 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001676 case ISN_UNLETENV:
1677 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1678 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001679
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001680 case ISN_LOCKCONST:
1681 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1682 break;
1683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 // create a list from items on the stack; uses a single allocation
1685 // for the list header and the items
1686 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001687 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1688 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 break;
1690
1691 // create a dict from items on the stack
1692 case ISN_NEWDICT:
1693 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001694 int count = iptr->isn_arg.number;
1695 dict_T *dict = dict_alloc();
1696 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697
1698 if (dict == NULL)
1699 goto failed;
1700 for (idx = 0; idx < count; ++idx)
1701 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001702 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001704 // check key is unique
1705 item = dict_find(dict, tv->vval.v_string, -1);
1706 if (item != NULL)
1707 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001708 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001709 semsg(_(e_duplicate_key), tv->vval.v_string);
1710 dict_unref(dict);
1711 goto on_error;
1712 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 item = dictitem_alloc(tv->vval.v_string);
1714 clear_tv(tv);
1715 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001716 {
1717 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001719 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1721 item->di_tv.v_lock = 0;
1722 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001723 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001724 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001725 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 }
1729
1730 if (count > 0)
1731 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001732 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 goto failed;
1734 else
1735 ++ectx.ec_stack.ga_len;
1736 tv = STACK_TV_BOT(-1);
1737 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001738 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 tv->vval.v_dict = dict;
1740 ++dict->dv_refcount;
1741 }
1742 break;
1743
1744 // call a :def function
1745 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001746 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1748 iptr->isn_arg.dfunc.cdf_argcount,
1749 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001750 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 break;
1752
1753 // call a builtin function
1754 case ISN_BCALL:
1755 SOURCING_LNUM = iptr->isn_lnum;
1756 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1757 iptr->isn_arg.bfunc.cbf_argcount,
1758 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001759 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760 break;
1761
1762 // call a funcref or partial
1763 case ISN_PCALL:
1764 {
1765 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1766 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001767 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768
1769 SOURCING_LNUM = iptr->isn_lnum;
1770 if (pfunc->cpf_top)
1771 {
1772 // funcref is above the arguments
1773 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1774 }
1775 else
1776 {
1777 // Get the funcref from the stack.
1778 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001779 partial_tv = *STACK_TV_BOT(0);
1780 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 }
1782 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001783 if (tv == &partial_tv)
1784 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001786 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 }
1788 break;
1789
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001790 case ISN_PCALL_END:
1791 // PCALL finished, arguments have been consumed and replaced by
1792 // the return value. Now clear the funcref from the stack,
1793 // and move the return value in its place.
1794 --ectx.ec_stack.ga_len;
1795 clear_tv(STACK_TV_BOT(-1));
1796 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1797 break;
1798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799 // call a user defined function or funcref/partial
1800 case ISN_UCALL:
1801 {
1802 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1803
1804 SOURCING_LNUM = iptr->isn_lnum;
1805 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001806 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001807 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 }
1809 break;
1810
1811 // return from a :def function call
1812 case ISN_RETURN:
1813 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001814 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001815 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001816
1817 if (trystack->ga_len > 0)
1818 trycmd = ((trycmd_T *)trystack->ga_data)
1819 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001820 if (trycmd != NULL
1821 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 && trycmd->tcd_finally_idx != 0)
1823 {
1824 // jump to ":finally"
1825 ectx.ec_iidx = trycmd->tcd_finally_idx;
1826 trycmd->tcd_return = TRUE;
1827 }
1828 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001829 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 }
1831 break;
1832
1833 // push a function reference to a compiled function
1834 case ISN_FUNCREF:
1835 {
1836 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001837 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838
1839 pt = ALLOC_CLEAR_ONE(partial_T);
1840 if (pt == NULL)
1841 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001842 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001843 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001844 vim_free(pt);
1845 goto failed;
1846 }
1847 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1848 + iptr->isn_arg.funcref.fr_func;
1849 pt->pt_func = pt_dfunc->df_ufunc;
1850 pt->pt_refcount = 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001851
1852 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1853 {
1854 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1855 + ectx.ec_dfunc_idx;
1856
1857 // The closure needs to find arguments and local
1858 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001859 pt->pt_ectx_stack = &ectx.ec_stack;
1860 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001861
1862 // If this function returns and the closure is still
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001863 // being used, we need to make a copy of the context
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001864 // (arguments and local variables). Store a reference
1865 // to the partial so we can handle that.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001866 if (ga_grow(&ectx.ec_funcrefs, 1) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001867 {
Bram Moolenaardec07512020-09-18 23:11:10 +02001868 vim_free(pt);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001869 goto failed;
1870 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001871 // Extra variable keeps the count of closures created
1872 // in the current function call.
1873 tv = STACK_TV_VAR(dfunc->df_varcount);
1874 ++tv->vval.v_number;
1875
1876 ((partial_T **)ectx.ec_funcrefs.ga_data)
1877 [ectx.ec_funcrefs.ga_len] = pt;
1878 ++pt->pt_refcount;
1879 ++ectx.ec_funcrefs.ga_len;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001880 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001881 ++pt_dfunc->df_ufunc->uf_refcount;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883 tv = STACK_TV_BOT(0);
1884 ++ectx.ec_stack.ga_len;
1885 tv->vval.v_partial = pt;
1886 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001887 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 }
1889 break;
1890
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001891 // Create a global function from a lambda.
1892 case ISN_NEWFUNC:
1893 {
1894 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1895
1896 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1897 }
1898 break;
1899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900 // jump if a condition is met
1901 case ISN_JUMP:
1902 {
1903 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001904 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 int jump = TRUE;
1906
1907 if (when != JUMP_ALWAYS)
1908 {
1909 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001910 if (when == JUMP_IF_COND_FALSE
1911 || when == JUMP_IF_COND_TRUE)
1912 {
1913 SOURCING_LNUM = iptr->isn_lnum;
1914 jump = tv_get_bool_chk(tv, &error);
1915 if (error)
1916 goto on_error;
1917 }
1918 else
1919 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001921 || when == JUMP_AND_KEEP_IF_FALSE
1922 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001924 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 {
1926 // drop the value from the stack
1927 clear_tv(tv);
1928 --ectx.ec_stack.ga_len;
1929 }
1930 }
1931 if (jump)
1932 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1933 }
1934 break;
1935
1936 // top of a for loop
1937 case ISN_FOR:
1938 {
1939 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1940 typval_T *idxtv =
1941 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1942
1943 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001944 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001946 ++idxtv->vval.v_number;
1947 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 // past the end of the list, jump to "endfor"
1949 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1950 else if (list->lv_first == &range_list_item)
1951 {
1952 // non-materialized range() list
1953 tv = STACK_TV_BOT(0);
1954 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001955 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 tv->vval.v_number = list_find_nr(
1957 list, idxtv->vval.v_number, NULL);
1958 ++ectx.ec_stack.ga_len;
1959 }
1960 else
1961 {
1962 listitem_T *li = list_find(list, idxtv->vval.v_number);
1963
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1965 ++ectx.ec_stack.ga_len;
1966 }
1967 }
1968 break;
1969
1970 // start of ":try" block
1971 case ISN_TRY:
1972 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001973 trycmd_T *trycmd = NULL;
1974
Bram Moolenaar270d0382020-05-15 21:42:53 +02001975 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 goto failed;
1977 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1978 + ectx.ec_trystack.ga_len;
1979 ++ectx.ec_trystack.ga_len;
1980 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001981 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1983 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001984 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02001985 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 }
1987 break;
1988
1989 case ISN_PUSHEXC:
1990 if (current_exception == NULL)
1991 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001992 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 iemsg("Evaluating catch while current_exception is NULL");
1994 goto failed;
1995 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001996 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 goto failed;
1998 tv = STACK_TV_BOT(0);
1999 ++ectx.ec_stack.ga_len;
2000 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002001 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 tv->vval.v_string = vim_strsave(
2003 (char_u *)current_exception->value);
2004 break;
2005
2006 case ISN_CATCH:
2007 {
2008 garray_T *trystack = &ectx.ec_trystack;
2009
2010 if (trystack->ga_len > 0)
2011 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002012 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 + trystack->ga_len - 1;
2014 trycmd->tcd_caught = TRUE;
2015 }
2016 did_emsg = got_int = did_throw = FALSE;
2017 catch_exception(current_exception);
2018 }
2019 break;
2020
2021 // end of ":try" block
2022 case ISN_ENDTRY:
2023 {
2024 garray_T *trystack = &ectx.ec_trystack;
2025
2026 if (trystack->ga_len > 0)
2027 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002028 trycmd_T *trycmd = NULL;
2029
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030 --trystack->ga_len;
2031 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002032 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033 trycmd = ((trycmd_T *)trystack->ga_data)
2034 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002035 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 {
2037 // discard the exception
2038 if (caught_stack == current_exception)
2039 caught_stack = caught_stack->caught;
2040 discard_current_exception();
2041 }
2042
2043 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002044 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 }
2046 }
2047 break;
2048
2049 case ISN_THROW:
2050 --ectx.ec_stack.ga_len;
2051 tv = STACK_TV_BOT(0);
2052 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2053 {
2054 vim_free(tv->vval.v_string);
2055 goto failed;
2056 }
2057 did_throw = TRUE;
2058 break;
2059
2060 // compare with special values
2061 case ISN_COMPAREBOOL:
2062 case ISN_COMPARESPECIAL:
2063 {
2064 typval_T *tv1 = STACK_TV_BOT(-2);
2065 typval_T *tv2 = STACK_TV_BOT(-1);
2066 varnumber_T arg1 = tv1->vval.v_number;
2067 varnumber_T arg2 = tv2->vval.v_number;
2068 int res;
2069
2070 switch (iptr->isn_arg.op.op_type)
2071 {
2072 case EXPR_EQUAL: res = arg1 == arg2; break;
2073 case EXPR_NEQUAL: res = arg1 != arg2; break;
2074 default: res = 0; break;
2075 }
2076
2077 --ectx.ec_stack.ga_len;
2078 tv1->v_type = VAR_BOOL;
2079 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2080 }
2081 break;
2082
2083 // Operation with two number arguments
2084 case ISN_OPNR:
2085 case ISN_COMPARENR:
2086 {
2087 typval_T *tv1 = STACK_TV_BOT(-2);
2088 typval_T *tv2 = STACK_TV_BOT(-1);
2089 varnumber_T arg1 = tv1->vval.v_number;
2090 varnumber_T arg2 = tv2->vval.v_number;
2091 varnumber_T res;
2092
2093 switch (iptr->isn_arg.op.op_type)
2094 {
2095 case EXPR_MULT: res = arg1 * arg2; break;
2096 case EXPR_DIV: res = arg1 / arg2; break;
2097 case EXPR_REM: res = arg1 % arg2; break;
2098 case EXPR_SUB: res = arg1 - arg2; break;
2099 case EXPR_ADD: res = arg1 + arg2; break;
2100
2101 case EXPR_EQUAL: res = arg1 == arg2; break;
2102 case EXPR_NEQUAL: res = arg1 != arg2; break;
2103 case EXPR_GREATER: res = arg1 > arg2; break;
2104 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2105 case EXPR_SMALLER: res = arg1 < arg2; break;
2106 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2107 default: res = 0; break;
2108 }
2109
2110 --ectx.ec_stack.ga_len;
2111 if (iptr->isn_type == ISN_COMPARENR)
2112 {
2113 tv1->v_type = VAR_BOOL;
2114 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2115 }
2116 else
2117 tv1->vval.v_number = res;
2118 }
2119 break;
2120
2121 // Computation with two float arguments
2122 case ISN_OPFLOAT:
2123 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002124#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 {
2126 typval_T *tv1 = STACK_TV_BOT(-2);
2127 typval_T *tv2 = STACK_TV_BOT(-1);
2128 float_T arg1 = tv1->vval.v_float;
2129 float_T arg2 = tv2->vval.v_float;
2130 float_T res = 0;
2131 int cmp = FALSE;
2132
2133 switch (iptr->isn_arg.op.op_type)
2134 {
2135 case EXPR_MULT: res = arg1 * arg2; break;
2136 case EXPR_DIV: res = arg1 / arg2; break;
2137 case EXPR_SUB: res = arg1 - arg2; break;
2138 case EXPR_ADD: res = arg1 + arg2; break;
2139
2140 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2141 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2142 case EXPR_GREATER: cmp = arg1 > arg2; break;
2143 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2144 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2145 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2146 default: cmp = 0; break;
2147 }
2148 --ectx.ec_stack.ga_len;
2149 if (iptr->isn_type == ISN_COMPAREFLOAT)
2150 {
2151 tv1->v_type = VAR_BOOL;
2152 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2153 }
2154 else
2155 tv1->vval.v_float = res;
2156 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002157#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158 break;
2159
2160 case ISN_COMPARELIST:
2161 {
2162 typval_T *tv1 = STACK_TV_BOT(-2);
2163 typval_T *tv2 = STACK_TV_BOT(-1);
2164 list_T *arg1 = tv1->vval.v_list;
2165 list_T *arg2 = tv2->vval.v_list;
2166 int cmp = FALSE;
2167 int ic = iptr->isn_arg.op.op_ic;
2168
2169 switch (iptr->isn_arg.op.op_type)
2170 {
2171 case EXPR_EQUAL: cmp =
2172 list_equal(arg1, arg2, ic, FALSE); break;
2173 case EXPR_NEQUAL: cmp =
2174 !list_equal(arg1, arg2, ic, FALSE); break;
2175 case EXPR_IS: cmp = arg1 == arg2; break;
2176 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2177 default: cmp = 0; break;
2178 }
2179 --ectx.ec_stack.ga_len;
2180 clear_tv(tv1);
2181 clear_tv(tv2);
2182 tv1->v_type = VAR_BOOL;
2183 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2184 }
2185 break;
2186
2187 case ISN_COMPAREBLOB:
2188 {
2189 typval_T *tv1 = STACK_TV_BOT(-2);
2190 typval_T *tv2 = STACK_TV_BOT(-1);
2191 blob_T *arg1 = tv1->vval.v_blob;
2192 blob_T *arg2 = tv2->vval.v_blob;
2193 int cmp = FALSE;
2194
2195 switch (iptr->isn_arg.op.op_type)
2196 {
2197 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2198 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2199 case EXPR_IS: cmp = arg1 == arg2; break;
2200 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2201 default: cmp = 0; break;
2202 }
2203 --ectx.ec_stack.ga_len;
2204 clear_tv(tv1);
2205 clear_tv(tv2);
2206 tv1->v_type = VAR_BOOL;
2207 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2208 }
2209 break;
2210
2211 // TODO: handle separately
2212 case ISN_COMPARESTRING:
2213 case ISN_COMPAREDICT:
2214 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 case ISN_COMPAREANY:
2216 {
2217 typval_T *tv1 = STACK_TV_BOT(-2);
2218 typval_T *tv2 = STACK_TV_BOT(-1);
2219 exptype_T exptype = iptr->isn_arg.op.op_type;
2220 int ic = iptr->isn_arg.op.op_ic;
2221
Bram Moolenaareb26f432020-09-14 16:50:05 +02002222 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223 typval_compare(tv1, tv2, exptype, ic);
2224 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 --ectx.ec_stack.ga_len;
2226 }
2227 break;
2228
2229 case ISN_ADDLIST:
2230 case ISN_ADDBLOB:
2231 {
2232 typval_T *tv1 = STACK_TV_BOT(-2);
2233 typval_T *tv2 = STACK_TV_BOT(-1);
2234
2235 if (iptr->isn_type == ISN_ADDLIST)
2236 eval_addlist(tv1, tv2);
2237 else
2238 eval_addblob(tv1, tv2);
2239 clear_tv(tv2);
2240 --ectx.ec_stack.ga_len;
2241 }
2242 break;
2243
2244 // Computation with two arguments of unknown type
2245 case ISN_OPANY:
2246 {
2247 typval_T *tv1 = STACK_TV_BOT(-2);
2248 typval_T *tv2 = STACK_TV_BOT(-1);
2249 varnumber_T n1, n2;
2250#ifdef FEAT_FLOAT
2251 float_T f1 = 0, f2 = 0;
2252#endif
2253 int error = FALSE;
2254
2255 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2256 {
2257 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2258 {
2259 eval_addlist(tv1, tv2);
2260 clear_tv(tv2);
2261 --ectx.ec_stack.ga_len;
2262 break;
2263 }
2264 else if (tv1->v_type == VAR_BLOB
2265 && tv2->v_type == VAR_BLOB)
2266 {
2267 eval_addblob(tv1, tv2);
2268 clear_tv(tv2);
2269 --ectx.ec_stack.ga_len;
2270 break;
2271 }
2272 }
2273#ifdef FEAT_FLOAT
2274 if (tv1->v_type == VAR_FLOAT)
2275 {
2276 f1 = tv1->vval.v_float;
2277 n1 = 0;
2278 }
2279 else
2280#endif
2281 {
2282 n1 = tv_get_number_chk(tv1, &error);
2283 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002284 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285#ifdef FEAT_FLOAT
2286 if (tv2->v_type == VAR_FLOAT)
2287 f1 = n1;
2288#endif
2289 }
2290#ifdef FEAT_FLOAT
2291 if (tv2->v_type == VAR_FLOAT)
2292 {
2293 f2 = tv2->vval.v_float;
2294 n2 = 0;
2295 }
2296 else
2297#endif
2298 {
2299 n2 = tv_get_number_chk(tv2, &error);
2300 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002301 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302#ifdef FEAT_FLOAT
2303 if (tv1->v_type == VAR_FLOAT)
2304 f2 = n2;
2305#endif
2306 }
2307#ifdef FEAT_FLOAT
2308 // if there is a float on either side the result is a float
2309 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2310 {
2311 switch (iptr->isn_arg.op.op_type)
2312 {
2313 case EXPR_MULT: f1 = f1 * f2; break;
2314 case EXPR_DIV: f1 = f1 / f2; break;
2315 case EXPR_SUB: f1 = f1 - f2; break;
2316 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002317 default: SOURCING_LNUM = iptr->isn_lnum;
2318 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002319 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 }
2321 clear_tv(tv1);
2322 clear_tv(tv2);
2323 tv1->v_type = VAR_FLOAT;
2324 tv1->vval.v_float = f1;
2325 --ectx.ec_stack.ga_len;
2326 }
2327 else
2328#endif
2329 {
2330 switch (iptr->isn_arg.op.op_type)
2331 {
2332 case EXPR_MULT: n1 = n1 * n2; break;
2333 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2334 case EXPR_SUB: n1 = n1 - n2; break;
2335 case EXPR_ADD: n1 = n1 + n2; break;
2336 default: n1 = num_modulus(n1, n2); break;
2337 }
2338 clear_tv(tv1);
2339 clear_tv(tv2);
2340 tv1->v_type = VAR_NUMBER;
2341 tv1->vval.v_number = n1;
2342 --ectx.ec_stack.ga_len;
2343 }
2344 }
2345 break;
2346
2347 case ISN_CONCAT:
2348 {
2349 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2350 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2351 char_u *res;
2352
2353 res = concat_str(str1, str2);
2354 clear_tv(STACK_TV_BOT(-2));
2355 clear_tv(STACK_TV_BOT(-1));
2356 --ectx.ec_stack.ga_len;
2357 STACK_TV_BOT(-1)->vval.v_string = res;
2358 }
2359 break;
2360
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002361 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002362 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002363 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002364 int is_slice = iptr->isn_type == ISN_STRSLICE;
2365 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002366 char_u *res;
2367
2368 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002369 // string slice: string is at stack-3, first index at
2370 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002371 if (is_slice)
2372 {
2373 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002374 n1 = tv->vval.v_number;
2375 }
2376
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002377 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002378 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002379
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002380 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002381 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002382 if (is_slice)
2383 // Slice: Select the characters from the string
2384 res = string_slice(tv->vval.v_string, n1, n2);
2385 else
2386 // Index: The resulting variable is a string of a
2387 // single character. If the index is too big or
2388 // negative the result is empty.
2389 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002390 vim_free(tv->vval.v_string);
2391 tv->vval.v_string = res;
2392 }
2393 break;
2394
2395 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002396 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 {
Bram Moolenaared591872020-08-15 22:14:53 +02002398 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002400 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401
2402 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002403 // list slice: list is at stack-3, indexes at stack-2 and
2404 // stack-1
2405 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406 list = tv->vval.v_list;
2407
2408 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002409 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002411
2412 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 {
Bram Moolenaared591872020-08-15 22:14:53 +02002414 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002415 n1 = tv->vval.v_number;
2416 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 }
Bram Moolenaared591872020-08-15 22:14:53 +02002418
2419 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002420 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002421 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002422 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2423 == FAIL)
2424 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 }
2426 break;
2427
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002428 case ISN_ANYINDEX:
2429 case ISN_ANYSLICE:
2430 {
2431 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2432 typval_T *var1, *var2;
2433 int res;
2434
2435 // index: composite is at stack-2, index at stack-1
2436 // slice: composite is at stack-3, indexes at stack-2 and
2437 // stack-1
2438 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002439 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002440 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2441 goto on_error;
2442 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2443 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2444 res = eval_index_inner(tv, is_slice,
2445 var1, var2, NULL, -1, TRUE);
2446 clear_tv(var1);
2447 if (is_slice)
2448 clear_tv(var2);
2449 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2450 if (res == FAIL)
2451 goto on_error;
2452 }
2453 break;
2454
Bram Moolenaar9af78762020-06-16 11:34:42 +02002455 case ISN_SLICE:
2456 {
2457 list_T *list;
2458 int count = iptr->isn_arg.number;
2459
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002460 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002461 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002462 list = tv->vval.v_list;
2463
2464 // no error for short list, expect it to be checked earlier
2465 if (list != NULL && list->lv_len >= count)
2466 {
2467 list_T *newlist = list_slice(list,
2468 count, list->lv_len - 1);
2469
2470 if (newlist != NULL)
2471 {
2472 list_unref(list);
2473 tv->vval.v_list = newlist;
2474 ++newlist->lv_refcount;
2475 }
2476 }
2477 }
2478 break;
2479
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002480 case ISN_GETITEM:
2481 {
2482 listitem_T *li;
2483 int index = iptr->isn_arg.number;
2484
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002485 // Get list item: list is at stack-1, push item.
2486 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002487 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002488 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002489
2490 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2491 goto failed;
2492 ++ectx.ec_stack.ga_len;
2493 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2494 }
2495 break;
2496
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 case ISN_MEMBER:
2498 {
2499 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002500 char_u *key;
2501 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002502 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002503
2504 // dict member: dict is at stack-2, key at stack-1
2505 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002506 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002507 dict = tv->vval.v_dict;
2508
2509 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002510 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002511 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002512
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002513 if ((di = dict_find(dict, key, -1)) == NULL)
2514 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002515 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002516 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002517 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002518 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002519 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002520 --ectx.ec_stack.ga_len;
2521 // Clear the dict after getting the item, to avoid that it
2522 // make the item invalid.
2523 tv = STACK_TV_BOT(-1);
2524 temp_tv = *tv;
2525 copy_tv(&di->di_tv, tv);
2526 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002527 }
2528 break;
2529
2530 // dict member with string key
2531 case ISN_STRINGMEMBER:
2532 {
2533 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002535 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536
2537 tv = STACK_TV_BOT(-1);
2538 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2539 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002540 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002542 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 }
2544 dict = tv->vval.v_dict;
2545
2546 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2547 == NULL)
2548 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002549 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002551 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002553 // Clear the dict after getting the item, to avoid that it
2554 // make the item invalid.
2555 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002557 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 }
2559 break;
2560
2561 case ISN_NEGATENR:
2562 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002563 if (tv->v_type != VAR_NUMBER
2564#ifdef FEAT_FLOAT
2565 && tv->v_type != VAR_FLOAT
2566#endif
2567 )
2568 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002569 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002570 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002571 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002572 }
2573#ifdef FEAT_FLOAT
2574 if (tv->v_type == VAR_FLOAT)
2575 tv->vval.v_float = -tv->vval.v_float;
2576 else
2577#endif
2578 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 break;
2580
2581 case ISN_CHECKNR:
2582 {
2583 int error = FALSE;
2584
2585 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002586 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002588 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 (void)tv_get_number_chk(tv, &error);
2590 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002591 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 }
2593 break;
2594
2595 case ISN_CHECKTYPE:
2596 {
2597 checktype_T *ct = &iptr->isn_arg.type;
2598
2599 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002600 SOURCING_LNUM = iptr->isn_lnum;
2601 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2602 goto on_error;
2603
2604 // number 0 is FALSE, number 1 is TRUE
2605 if (tv->v_type == VAR_NUMBER
2606 && ct->ct_type->tt_type == VAR_BOOL
2607 && (tv->vval.v_number == 0
2608 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002610 tv->v_type = VAR_BOOL;
2611 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002612 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 }
2614 }
2615 break;
2616
Bram Moolenaar9af78762020-06-16 11:34:42 +02002617 case ISN_CHECKLEN:
2618 {
2619 int min_len = iptr->isn_arg.checklen.cl_min_len;
2620 list_T *list = NULL;
2621
2622 tv = STACK_TV_BOT(-1);
2623 if (tv->v_type == VAR_LIST)
2624 list = tv->vval.v_list;
2625 if (list == NULL || list->lv_len < min_len
2626 || (list->lv_len > min_len
2627 && !iptr->isn_arg.checklen.cl_more_OK))
2628 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002629 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002630 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002631 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002632 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002633 }
2634 }
2635 break;
2636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002638 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 {
2640 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002641 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642
2643 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002644 if (iptr->isn_type == ISN_2BOOL)
2645 {
2646 n = tv2bool(tv);
2647 if (iptr->isn_arg.number) // invert
2648 n = !n;
2649 }
2650 else
2651 {
2652 SOURCING_LNUM = iptr->isn_lnum;
2653 n = tv_get_bool_chk(tv, &error);
2654 if (error)
2655 goto on_error;
2656 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 clear_tv(tv);
2658 tv->v_type = VAR_BOOL;
2659 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2660 }
2661 break;
2662
2663 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002664 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 {
2666 char_u *str;
2667
2668 tv = STACK_TV_BOT(iptr->isn_arg.number);
2669 if (tv->v_type != VAR_STRING)
2670 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002671 if (iptr->isn_type == ISN_2STRING_ANY)
2672 {
2673 switch (tv->v_type)
2674 {
2675 case VAR_SPECIAL:
2676 case VAR_BOOL:
2677 case VAR_NUMBER:
2678 case VAR_FLOAT:
2679 case VAR_BLOB: break;
2680 default: to_string_error(tv->v_type);
2681 goto on_error;
2682 }
2683 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 str = typval_tostring(tv);
2685 clear_tv(tv);
2686 tv->v_type = VAR_STRING;
2687 tv->vval.v_string = str;
2688 }
2689 }
2690 break;
2691
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002692 case ISN_PUT:
2693 {
2694 int regname = iptr->isn_arg.put.put_regname;
2695 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2696 char_u *expr = NULL;
2697 int dir = FORWARD;
2698
2699 if (regname == '=')
2700 {
2701 tv = STACK_TV_BOT(-1);
2702 if (tv->v_type == VAR_STRING)
2703 expr = tv->vval.v_string;
2704 else
2705 {
2706 expr = typval_tostring(tv); // allocates value
2707 clear_tv(tv);
2708 }
2709 --ectx.ec_stack.ga_len;
2710 }
2711 if (lnum == -2)
2712 // :put! above cursor
2713 dir = BACKWARD;
2714 else if (lnum >= 0)
2715 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2716 check_cursor();
2717 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2718 vim_free(expr);
2719 }
2720 break;
2721
Bram Moolenaar389df252020-07-09 21:20:47 +02002722 case ISN_SHUFFLE:
2723 {
2724 typval_T tmp_tv;
2725 int item = iptr->isn_arg.shuffle.shfl_item;
2726 int up = iptr->isn_arg.shuffle.shfl_up;
2727
2728 tmp_tv = *STACK_TV_BOT(-item);
2729 for ( ; up > 0 && item > 1; --up)
2730 {
2731 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2732 --item;
2733 }
2734 *STACK_TV_BOT(-item) = tmp_tv;
2735 }
2736 break;
2737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 case ISN_DROP:
2739 --ectx.ec_stack.ga_len;
2740 clear_tv(STACK_TV_BOT(0));
2741 break;
2742 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002743 continue;
2744
Bram Moolenaard032f342020-07-18 18:13:02 +02002745func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002746 // Restore previous function. If the frame pointer is where we started
2747 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02002748 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02002749 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002750
Bram Moolenaard032f342020-07-18 18:13:02 +02002751 if (func_return(&ectx) == FAIL)
2752 // only fails when out of memory
2753 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002754 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002755
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002756on_error:
2757 if (trylevel == 0)
2758 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 }
2760
2761done:
2762 // function finished, get result from the stack.
2763 tv = STACK_TV_BOT(-1);
2764 *rettv = *tv;
2765 tv->v_type = VAR_UNKNOWN;
2766 ret = OK;
2767
2768failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002769 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002770 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002771 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002772
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002773 // Deal with any remaining closures, they may be in use somewhere.
2774 if (ectx.ec_funcrefs.ga_len > 0)
2775 handle_closure_in_use(&ectx, FALSE);
2776
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002777 estack_pop();
2778 current_sctx = save_current_sctx;
2779
2780failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002781 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2783 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002784
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002786 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002787
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002788 // Not sure if this is necessary.
2789 suppress_errthrow = save_suppress_errthrow;
2790
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002791 if (ret != OK && called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002792 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002793 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 return ret;
2795}
2796
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797/*
2798 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002799 * We don't really need this at runtime, but we do have tests that require it,
2800 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 */
2802 void
2803ex_disassemble(exarg_T *eap)
2804{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002805 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002806 char_u *fname;
2807 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 dfunc_T *dfunc;
2809 isn_T *instr;
2810 int current;
2811 int line_idx = 0;
2812 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002813 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002815 if (STRNCMP(arg, "<lambda>", 8) == 0)
2816 {
2817 arg += 8;
2818 (void)getdigits(&arg);
2819 fname = vim_strnsave(eap->arg, arg - eap->arg);
2820 }
2821 else
2822 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002823 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002824 if (fname == NULL)
2825 {
2826 semsg(_(e_invarg2), eap->arg);
2827 return;
2828 }
2829
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002830 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002831 if (ufunc == NULL)
2832 {
2833 char_u *p = untrans_function_name(fname);
2834
2835 if (p != NULL)
2836 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002837 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002838 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002839 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 if (ufunc == NULL)
2841 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002842 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 return;
2844 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002845 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002846 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2847 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002848 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002850 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 return;
2852 }
2853 if (ufunc->uf_name_exp != NULL)
2854 msg((char *)ufunc->uf_name_exp);
2855 else
2856 msg((char *)ufunc->uf_name);
2857
2858 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2859 instr = dfunc->df_instr;
2860 for (current = 0; current < dfunc->df_instr_count; ++current)
2861 {
2862 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002863 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864
2865 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2866 {
2867 if (current > prev_current)
2868 {
2869 msg_puts("\n\n");
2870 prev_current = current;
2871 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002872 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2873 if (line != NULL)
2874 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 }
2876
2877 switch (iptr->isn_type)
2878 {
2879 case ISN_EXEC:
2880 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2881 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002882 case ISN_EXECCONCAT:
2883 smsg("%4d EXECCONCAT %lld", current,
2884 (long long)iptr->isn_arg.number);
2885 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 case ISN_ECHO:
2887 {
2888 echo_T *echo = &iptr->isn_arg.echo;
2889
2890 smsg("%4d %s %d", current,
2891 echo->echo_with_white ? "ECHO" : "ECHON",
2892 echo->echo_count);
2893 }
2894 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002895 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002896 smsg("%4d EXECUTE %lld", current,
2897 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002898 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002899 case ISN_ECHOMSG:
2900 smsg("%4d ECHOMSG %lld", current,
2901 (long long)(iptr->isn_arg.number));
2902 break;
2903 case ISN_ECHOERR:
2904 smsg("%4d ECHOERR %lld", current,
2905 (long long)(iptr->isn_arg.number));
2906 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002908 case ISN_LOADOUTER:
2909 {
2910 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2911
2912 if (iptr->isn_arg.number < 0)
2913 smsg("%4d LOAD%s arg[%lld]", current, add,
2914 (long long)(iptr->isn_arg.number
2915 + STACK_FRAME_SIZE));
2916 else
2917 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002918 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 break;
2921 case ISN_LOADV:
2922 smsg("%4d LOADV v:%s", current,
2923 get_vim_var_name(iptr->isn_arg.number));
2924 break;
2925 case ISN_LOADSCRIPT:
2926 {
2927 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002928 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2930 + iptr->isn_arg.script.script_idx;
2931
2932 smsg("%4d LOADSCRIPT %s from %s", current,
2933 sv->sv_name, si->sn_name);
2934 }
2935 break;
2936 case ISN_LOADS:
2937 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002938 scriptitem_T *si = SCRIPT_ITEM(
2939 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940
2941 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002942 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 }
2944 break;
2945 case ISN_LOADG:
2946 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2947 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002948 case ISN_LOADB:
2949 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2950 break;
2951 case ISN_LOADW:
2952 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2953 break;
2954 case ISN_LOADT:
2955 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2956 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002957 case ISN_LOADGDICT:
2958 smsg("%4d LOAD g:", current);
2959 break;
2960 case ISN_LOADBDICT:
2961 smsg("%4d LOAD b:", current);
2962 break;
2963 case ISN_LOADWDICT:
2964 smsg("%4d LOAD w:", current);
2965 break;
2966 case ISN_LOADTDICT:
2967 smsg("%4d LOAD t:", current);
2968 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 case ISN_LOADOPT:
2970 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2971 break;
2972 case ISN_LOADENV:
2973 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2974 break;
2975 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002976 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 break;
2978
2979 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002980 case ISN_STOREOUTER:
2981 {
2982 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2983
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002984 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002985 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002986 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002987 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002988 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002989 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002990 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002992 case ISN_STOREV:
2993 smsg("%4d STOREV v:%s", current,
2994 get_vim_var_name(iptr->isn_arg.number));
2995 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002997 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2998 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002999 case ISN_STOREB:
3000 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3001 break;
3002 case ISN_STOREW:
3003 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3004 break;
3005 case ISN_STORET:
3006 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3007 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003008 case ISN_STORES:
3009 {
3010 scriptitem_T *si = SCRIPT_ITEM(
3011 iptr->isn_arg.loadstore.ls_sid);
3012
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003013 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003014 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003015 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 break;
3017 case ISN_STORESCRIPT:
3018 {
3019 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003020 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3022 + iptr->isn_arg.script.script_idx;
3023
3024 smsg("%4d STORESCRIPT %s in %s", current,
3025 sv->sv_name, si->sn_name);
3026 }
3027 break;
3028 case ISN_STOREOPT:
3029 smsg("%4d STOREOPT &%s", current,
3030 iptr->isn_arg.storeopt.so_name);
3031 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003032 case ISN_STOREENV:
3033 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3034 break;
3035 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003036 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003037 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 case ISN_STORENR:
3039 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003040 iptr->isn_arg.storenr.stnr_val,
3041 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 break;
3043
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003044 case ISN_STORELIST:
3045 smsg("%4d STORELIST", current);
3046 break;
3047
3048 case ISN_STOREDICT:
3049 smsg("%4d STOREDICT", current);
3050 break;
3051
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 // constants
3053 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003054 smsg("%4d PUSHNR %lld", current,
3055 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 break;
3057 case ISN_PUSHBOOL:
3058 case ISN_PUSHSPEC:
3059 smsg("%4d PUSH %s", current,
3060 get_var_special_name(iptr->isn_arg.number));
3061 break;
3062 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003063#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003065#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 break;
3067 case ISN_PUSHS:
3068 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3069 break;
3070 case ISN_PUSHBLOB:
3071 {
3072 char_u *r;
3073 char_u numbuf[NUMBUFLEN];
3074 char_u *tofree;
3075
3076 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003077 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 vim_free(tofree);
3079 }
3080 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003081 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003082 {
3083 char *name = (char *)iptr->isn_arg.string;
3084
3085 smsg("%4d PUSHFUNC \"%s\"", current,
3086 name == NULL ? "[none]" : name);
3087 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003088 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003089 case ISN_PUSHCHANNEL:
3090#ifdef FEAT_JOB_CHANNEL
3091 {
3092 channel_T *channel = iptr->isn_arg.channel;
3093
3094 smsg("%4d PUSHCHANNEL %d", current,
3095 channel == NULL ? 0 : channel->ch_id);
3096 }
3097#endif
3098 break;
3099 case ISN_PUSHJOB:
3100#ifdef FEAT_JOB_CHANNEL
3101 {
3102 typval_T tv;
3103 char_u *name;
3104
3105 tv.v_type = VAR_JOB;
3106 tv.vval.v_job = iptr->isn_arg.job;
3107 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003108 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003109 }
3110#endif
3111 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 case ISN_PUSHEXC:
3113 smsg("%4d PUSH v:exception", current);
3114 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003115 case ISN_UNLET:
3116 smsg("%4d UNLET%s %s", current,
3117 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3118 iptr->isn_arg.unlet.ul_name);
3119 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003120 case ISN_UNLETENV:
3121 smsg("%4d UNLETENV%s $%s", current,
3122 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3123 iptr->isn_arg.unlet.ul_name);
3124 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003125 case ISN_LOCKCONST:
3126 smsg("%4d LOCKCONST", current);
3127 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003129 smsg("%4d NEWLIST size %lld", current,
3130 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 break;
3132 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003133 smsg("%4d NEWDICT size %lld", current,
3134 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 break;
3136
3137 // function call
3138 case ISN_BCALL:
3139 {
3140 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3141
3142 smsg("%4d BCALL %s(argc %d)", current,
3143 internal_func_name(cbfunc->cbf_idx),
3144 cbfunc->cbf_argcount);
3145 }
3146 break;
3147 case ISN_DCALL:
3148 {
3149 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3150 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3151 + cdfunc->cdf_idx;
3152
3153 smsg("%4d DCALL %s(argc %d)", current,
3154 df->df_ufunc->uf_name_exp != NULL
3155 ? df->df_ufunc->uf_name_exp
3156 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3157 }
3158 break;
3159 case ISN_UCALL:
3160 {
3161 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3162
3163 smsg("%4d UCALL %s(argc %d)", current,
3164 cufunc->cuf_name, cufunc->cuf_argcount);
3165 }
3166 break;
3167 case ISN_PCALL:
3168 {
3169 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3170
3171 smsg("%4d PCALL%s (argc %d)", current,
3172 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3173 }
3174 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003175 case ISN_PCALL_END:
3176 smsg("%4d PCALL end", current);
3177 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 case ISN_RETURN:
3179 smsg("%4d RETURN", current);
3180 break;
3181 case ISN_FUNCREF:
3182 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003183 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003185 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003187 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 }
3189 break;
3190
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003191 case ISN_NEWFUNC:
3192 {
3193 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3194
3195 smsg("%4d NEWFUNC %s %s", current,
3196 newfunc->nf_lambda, newfunc->nf_global);
3197 }
3198 break;
3199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 case ISN_JUMP:
3201 {
3202 char *when = "?";
3203
3204 switch (iptr->isn_arg.jump.jump_when)
3205 {
3206 case JUMP_ALWAYS:
3207 when = "JUMP";
3208 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 case JUMP_AND_KEEP_IF_TRUE:
3210 when = "JUMP_AND_KEEP_IF_TRUE";
3211 break;
3212 case JUMP_IF_FALSE:
3213 when = "JUMP_IF_FALSE";
3214 break;
3215 case JUMP_AND_KEEP_IF_FALSE:
3216 when = "JUMP_AND_KEEP_IF_FALSE";
3217 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003218 case JUMP_IF_COND_FALSE:
3219 when = "JUMP_IF_COND_FALSE";
3220 break;
3221 case JUMP_IF_COND_TRUE:
3222 when = "JUMP_IF_COND_TRUE";
3223 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003225 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 iptr->isn_arg.jump.jump_where);
3227 }
3228 break;
3229
3230 case ISN_FOR:
3231 {
3232 forloop_T *forloop = &iptr->isn_arg.forloop;
3233
3234 smsg("%4d FOR $%d -> %d", current,
3235 forloop->for_idx, forloop->for_end);
3236 }
3237 break;
3238
3239 case ISN_TRY:
3240 {
3241 try_T *try = &iptr->isn_arg.try;
3242
3243 smsg("%4d TRY catch -> %d, finally -> %d", current,
3244 try->try_catch, try->try_finally);
3245 }
3246 break;
3247 case ISN_CATCH:
3248 // TODO
3249 smsg("%4d CATCH", current);
3250 break;
3251 case ISN_ENDTRY:
3252 smsg("%4d ENDTRY", current);
3253 break;
3254 case ISN_THROW:
3255 smsg("%4d THROW", current);
3256 break;
3257
3258 // expression operations on number
3259 case ISN_OPNR:
3260 case ISN_OPFLOAT:
3261 case ISN_OPANY:
3262 {
3263 char *what;
3264 char *ins;
3265
3266 switch (iptr->isn_arg.op.op_type)
3267 {
3268 case EXPR_MULT: what = "*"; break;
3269 case EXPR_DIV: what = "/"; break;
3270 case EXPR_REM: what = "%"; break;
3271 case EXPR_SUB: what = "-"; break;
3272 case EXPR_ADD: what = "+"; break;
3273 default: what = "???"; break;
3274 }
3275 switch (iptr->isn_type)
3276 {
3277 case ISN_OPNR: ins = "OPNR"; break;
3278 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3279 case ISN_OPANY: ins = "OPANY"; break;
3280 default: ins = "???"; break;
3281 }
3282 smsg("%4d %s %s", current, ins, what);
3283 }
3284 break;
3285
3286 case ISN_COMPAREBOOL:
3287 case ISN_COMPARESPECIAL:
3288 case ISN_COMPARENR:
3289 case ISN_COMPAREFLOAT:
3290 case ISN_COMPARESTRING:
3291 case ISN_COMPAREBLOB:
3292 case ISN_COMPARELIST:
3293 case ISN_COMPAREDICT:
3294 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 case ISN_COMPAREANY:
3296 {
3297 char *p;
3298 char buf[10];
3299 char *type;
3300
3301 switch (iptr->isn_arg.op.op_type)
3302 {
3303 case EXPR_EQUAL: p = "=="; break;
3304 case EXPR_NEQUAL: p = "!="; break;
3305 case EXPR_GREATER: p = ">"; break;
3306 case EXPR_GEQUAL: p = ">="; break;
3307 case EXPR_SMALLER: p = "<"; break;
3308 case EXPR_SEQUAL: p = "<="; break;
3309 case EXPR_MATCH: p = "=~"; break;
3310 case EXPR_IS: p = "is"; break;
3311 case EXPR_ISNOT: p = "isnot"; break;
3312 case EXPR_NOMATCH: p = "!~"; break;
3313 default: p = "???"; break;
3314 }
3315 STRCPY(buf, p);
3316 if (iptr->isn_arg.op.op_ic == TRUE)
3317 strcat(buf, "?");
3318 switch(iptr->isn_type)
3319 {
3320 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3321 case ISN_COMPARESPECIAL:
3322 type = "COMPARESPECIAL"; break;
3323 case ISN_COMPARENR: type = "COMPARENR"; break;
3324 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3325 case ISN_COMPARESTRING:
3326 type = "COMPARESTRING"; break;
3327 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3328 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3329 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3330 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3332 default: type = "???"; break;
3333 }
3334
3335 smsg("%4d %s %s", current, type, buf);
3336 }
3337 break;
3338
3339 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3340 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3341
3342 // expression operations
3343 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003344 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003345 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003346 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003347 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003348 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3349 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003350 case ISN_SLICE: smsg("%4d SLICE %lld",
3351 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003352 case ISN_GETITEM: smsg("%4d ITEM %lld",
3353 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003354 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3355 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 iptr->isn_arg.string); break;
3357 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3358
3359 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003360 case ISN_CHECKTYPE:
3361 {
3362 char *tofree;
3363
3364 smsg("%4d CHECKTYPE %s stack[%d]", current,
3365 type_name(iptr->isn_arg.type.ct_type, &tofree),
3366 iptr->isn_arg.type.ct_off);
3367 vim_free(tofree);
3368 break;
3369 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003370 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3371 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3372 iptr->isn_arg.checklen.cl_min_len);
3373 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003374 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 case ISN_2BOOL: if (iptr->isn_arg.number)
3376 smsg("%4d INVERT (!val)", current);
3377 else
3378 smsg("%4d 2BOOL (!!val)", current);
3379 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003380 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3381 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003382 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003383 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3384 (long long)(iptr->isn_arg.number));
3385 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003386 case ISN_PUT:
3387 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname,
3388 (long)iptr->isn_arg.put.put_lnum);
3389 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390
Bram Moolenaar389df252020-07-09 21:20:47 +02003391 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3392 iptr->isn_arg.shuffle.shfl_item,
3393 iptr->isn_arg.shuffle.shfl_up);
3394 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 case ISN_DROP: smsg("%4d DROP", current); break;
3396 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003397
3398 out_flush(); // output one line at a time
3399 ui_breakcheck();
3400 if (got_int)
3401 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403}
3404
3405/*
3406 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3407 * list, etc. Mostly like what JavaScript does, except that empty list and
3408 * empty dictionary are FALSE.
3409 */
3410 int
3411tv2bool(typval_T *tv)
3412{
3413 switch (tv->v_type)
3414 {
3415 case VAR_NUMBER:
3416 return tv->vval.v_number != 0;
3417 case VAR_FLOAT:
3418#ifdef FEAT_FLOAT
3419 return tv->vval.v_float != 0.0;
3420#else
3421 break;
3422#endif
3423 case VAR_PARTIAL:
3424 return tv->vval.v_partial != NULL;
3425 case VAR_FUNC:
3426 case VAR_STRING:
3427 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3428 case VAR_LIST:
3429 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3430 case VAR_DICT:
3431 return tv->vval.v_dict != NULL
3432 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3433 case VAR_BOOL:
3434 case VAR_SPECIAL:
3435 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3436 case VAR_JOB:
3437#ifdef FEAT_JOB_CHANNEL
3438 return tv->vval.v_job != NULL;
3439#else
3440 break;
3441#endif
3442 case VAR_CHANNEL:
3443#ifdef FEAT_JOB_CHANNEL
3444 return tv->vval.v_channel != NULL;
3445#else
3446 break;
3447#endif
3448 case VAR_BLOB:
3449 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3450 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003451 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 case VAR_VOID:
3453 break;
3454 }
3455 return FALSE;
3456}
3457
3458/*
3459 * If "tv" is a string give an error and return FAIL.
3460 */
3461 int
3462check_not_string(typval_T *tv)
3463{
3464 if (tv->v_type == VAR_STRING)
3465 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003466 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 clear_tv(tv);
3468 return FAIL;
3469 }
3470 return OK;
3471}
3472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473
3474#endif // FEAT_EVAL