blob: 521f1c9c0cd052b55b4e0ce97ac8091592a2e5cc [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;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200352 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
353 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200354 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
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200379 // A partial created for a local function, that is also used as a
380 // local variable, has a reference count for the variable, thus
381 // will never go down to zero. When all these refcounts are one
382 // then the funcstack is unused. We need to count how many we have
383 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200384 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
385 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200386 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 Moolenaar85d5e2b2020-10-10 14:13:01 +0200389 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
390 gap->ga_len - closure_count + i])
391 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200392 }
393
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200394 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200395 tv->v_type = VAR_UNKNOWN;
396 }
397
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200398 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200399 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200400 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
401 - closure_count + idx];
402 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200403 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200404 ++funcstack->fs_refcount;
405 pt->pt_funcstack = funcstack;
406 pt->pt_ectx_stack = &funcstack->fs_ga;
407 pt->pt_ectx_frame = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200408 }
409 }
410 }
411
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200412 for (idx = 0; idx < closure_count; ++idx)
413 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
414 - closure_count + idx]);
415 gap->ga_len -= closure_count;
416 if (gap->ga_len == 0)
417 ga_clear(gap);
418
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200419 return OK;
420}
421
422/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200423 * Called when a partial is freed or its reference count goes down to one. The
424 * funcstack may be the only reference to the partials in the local variables.
425 * Go over all of them, the funcref and can be freed if all partials
426 * referencing the funcstack have a reference count of one.
427 */
428 void
429funcstack_check_refcount(funcstack_T *funcstack)
430{
431 int i;
432 garray_T *gap = &funcstack->fs_ga;
433 int done = 0;
434
435 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
436 return;
437 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
438 {
439 typval_T *tv = ((typval_T *)gap->ga_data) + i;
440
441 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
442 && tv->vval.v_partial->pt_funcstack == funcstack
443 && tv->vval.v_partial->pt_refcount == 1)
444 ++done;
445 }
446 if (done == funcstack->fs_min_refcount)
447 {
448 typval_T *stack = gap->ga_data;
449
450 // All partials referencing the funcstack have a reference count of
451 // one, thus the funcstack is no longer of use.
452 for (i = 0; i < gap->ga_len; ++i)
453 clear_tv(stack + i);
454 vim_free(stack);
455 vim_free(funcstack);
456 }
457}
458
459/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100460 * Return from the current function.
461 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200462 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100463func_return(ectx_T *ectx)
464{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100465 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200466 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
467 + ectx->ec_dfunc_idx;
468 int argcount = ufunc_argcount(dfunc->df_ufunc);
469 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200470 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100471
472 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200473 entry = estack_pop();
474 if (entry != NULL)
475 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200477 if (handle_closure_in_use(ectx, TRUE) == FAIL)
478 return FAIL;
479
480 // Clear the arguments.
481 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
482 clear_tv(STACK_TV(idx));
483
484 // Clear local variables and temp values, but not the return value.
485 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100486 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100488
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100489 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200490 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
491 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200492 ectx->ec_outer_stack =
493 (void *)STACK_TV(ectx->ec_frame_idx + 2)->vval.v_string;
494 ectx->ec_outer_frame = STACK_TV(ectx->ec_frame_idx + 3)->vval.v_number;
495 // restoring ec_frame_idx must be last
496 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 4)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
498 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100499
500 // Reset the stack to the position before the call, move the return value
501 // to the top of the stack.
502 idx = ectx->ec_stack.ga_len - 1;
503 ectx->ec_stack.ga_len = top + 1;
504 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200505
506 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100507}
508
509#undef STACK_TV
510
511/*
512 * Prepare arguments and rettv for calling a builtin or user function.
513 */
514 static int
515call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
516{
517 int idx;
518 typval_T *tv;
519
520 // Move arguments from bottom of the stack to argvars[] and add terminator.
521 for (idx = 0; idx < argcount; ++idx)
522 argvars[idx] = *STACK_TV_BOT(idx - argcount);
523 argvars[argcount].v_type = VAR_UNKNOWN;
524
525 // Result replaces the arguments on the stack.
526 if (argcount > 0)
527 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200528 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 return FAIL;
530 else
531 ++ectx->ec_stack.ga_len;
532
533 // Default return value is zero.
534 tv = STACK_TV_BOT(-1);
535 tv->v_type = VAR_NUMBER;
536 tv->vval.v_number = 0;
537
538 return OK;
539}
540
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200541// Ugly global to avoid passing the execution context around through many
542// layers.
543static ectx_T *current_ectx = NULL;
544
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545/*
546 * Call a builtin function by index.
547 */
548 static int
549call_bfunc(int func_idx, int argcount, ectx_T *ectx)
550{
551 typval_T argvars[MAX_FUNC_ARGS];
552 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200553 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200554 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555
556 if (call_prepare(argcount, argvars, ectx) == FAIL)
557 return FAIL;
558
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200559 // Call the builtin function. Set "current_ectx" so that when it
560 // recursively invokes call_def_function() a closure context can be set.
561 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200563 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100564
565 // Clear the arguments.
566 for (idx = 0; idx < argcount; ++idx)
567 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200568
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200569 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200570 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 return OK;
572}
573
574/*
575 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100576 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 */
578 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100579call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580{
581 typval_T argvars[MAX_FUNC_ARGS];
582 funcexe_T funcexe;
583 int error;
584 int idx;
Bram Moolenaared677f52020-08-12 16:38:10 +0200585 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200587 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200588 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
589 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200590 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100591 {
592 // The function has been compiled, can call it quickly. For a function
593 // that was defined later: we can call it directly next time.
594 if (iptr != NULL)
595 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100596 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100597 iptr->isn_type = ISN_DCALL;
598 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
599 iptr->isn_arg.dfunc.cdf_argcount = argcount;
600 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100602 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603
604 if (call_prepare(argcount, argvars, ectx) == FAIL)
605 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200606 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607 funcexe.evaluate = TRUE;
608
609 // Call the user function. Result goes in last position on the stack.
610 // TODO: add selfdict if there is one
611 error = call_user_func_check(ufunc, argcount, argvars,
612 STACK_TV_BOT(-1), &funcexe, NULL);
613
614 // Clear the arguments.
615 for (idx = 0; idx < argcount; ++idx)
616 clear_tv(&argvars[idx]);
617
618 if (error != FCERR_NONE)
619 {
620 user_func_error(error, ufunc->uf_name);
621 return FAIL;
622 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200623 if (called_emsg > called_emsg_before)
624 // Error other than from calling the function itself.
625 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626 return OK;
627}
628
629/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200630 * Return TRUE if an error was given or CTRL-C was pressed.
631 */
632 static int
633vim9_aborting(int prev_called_emsg)
634{
635 return called_emsg > prev_called_emsg || got_int || did_throw;
636}
637
638/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100639 * Execute a function by "name".
640 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100641 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 * Returns FAIL if not found without an error message.
643 */
644 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100645call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100646{
647 ufunc_T *ufunc;
648
649 if (builtin_function(name, -1))
650 {
651 int func_idx = find_internal_func(name);
652
653 if (func_idx < 0)
654 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200655 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656 return FAIL;
657 return call_bfunc(func_idx, argcount, ectx);
658 }
659
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200660 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200661
662 if (ufunc == NULL)
663 {
664 int called_emsg_before = called_emsg;
665
666 if (script_autoload(name, TRUE))
667 // loaded a package, search for the function again
668 ufunc = find_func(name, FALSE, NULL);
669 if (vim9_aborting(called_emsg_before))
670 return FAIL; // bail out if loading the script caused an error
671 }
672
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100674 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
676 return FAIL;
677}
678
679 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200680call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200682 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200683 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200685 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686
687 if (tv->v_type == VAR_PARTIAL)
688 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200689 partial_T *pt = tv->vval.v_partial;
690 int i;
691
692 if (pt->pt_argc > 0)
693 {
694 // Make space for arguments from the partial, shift the "argcount"
695 // arguments up.
696 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
697 return FAIL;
698 for (i = 1; i <= argcount; ++i)
699 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
700 ectx->ec_stack.ga_len += pt->pt_argc;
701 argcount += pt->pt_argc;
702
703 // copy the arguments from the partial onto the stack
704 for (i = 0; i < pt->pt_argc; ++i)
705 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
706 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707
708 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200709 {
710 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
711
712 // closure may need the function context where it was defined
713 ectx->ec_outer_stack = pt->pt_ectx_stack;
714 ectx->ec_outer_frame = pt->pt_ectx_frame;
715
716 return ret;
717 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 name = pt->pt_name;
719 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200720 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100721 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200722 if (name != NULL)
723 {
724 char_u fname_buf[FLEN_FIXED + 1];
725 char_u *tofree = NULL;
726 int error = FCERR_NONE;
727 char_u *fname;
728
729 // May need to translate <SNR>123_ to K_SNR.
730 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
731 if (error != FCERR_NONE)
732 res = FAIL;
733 else
734 res = call_by_name(fname, argcount, ectx, NULL);
735 vim_free(tofree);
736 }
737
738 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100739 {
740 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200741 semsg(_(e_unknownfunc),
742 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743 return FAIL;
744 }
745 return OK;
746}
747
748/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200749 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
750 * TRUE.
751 */
752 static int
753error_if_locked(int lock, char *error)
754{
755 if (lock & (VAR_LOCKED | VAR_FIXED))
756 {
757 emsg(_(error));
758 return TRUE;
759 }
760 return FALSE;
761}
762
763/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100764 * Store "tv" in variable "name".
765 * This is for s: and g: variables.
766 */
767 static void
768store_var(char_u *name, typval_T *tv)
769{
770 funccal_entry_T entry;
771
772 save_funccal(&entry);
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200773 set_var_const(name, NULL, tv, FALSE, ASSIGN_NO_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100774 restore_funccal();
775}
776
Bram Moolenaard3aac292020-04-19 14:32:17 +0200777
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100778/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 * Execute a function by "name".
780 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100781 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 */
783 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100784call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785{
Bram Moolenaared677f52020-08-12 16:38:10 +0200786 int called_emsg_before = called_emsg;
787 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788
Bram Moolenaared677f52020-08-12 16:38:10 +0200789 res = call_by_name(name, argcount, ectx, iptr);
790 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100791 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200792 dictitem_T *v;
793
794 v = find_var(name, NULL, FALSE);
795 if (v == NULL)
796 {
797 semsg(_(e_unknownfunc), name);
798 return FAIL;
799 }
800 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
801 {
802 semsg(_(e_unknownfunc), name);
803 return FAIL;
804 }
805 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200807 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808}
809
810/*
811 * Call a "def" function from old Vim script.
812 * Return OK or FAIL.
813 */
814 int
815call_def_function(
816 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200817 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200819 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820 typval_T *rettv) // return value
821{
822 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200823 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200824 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825 typval_T *tv;
826 int idx;
827 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100828 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200829 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200830 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200831 int called_emsg_before = called_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200832 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833
834// Get pointer to item in the stack.
835#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
836
837// Get pointer to item at the bottom of the stack, -1 is the bottom.
838#undef STACK_TV_BOT
839#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
840
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200841// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200842#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 +0100843
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200844// Like STACK_TV_VAR but use the outer scope
845#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
846
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200847 if (ufunc->uf_def_status == UF_NOT_COMPILED
848 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200849 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200850 {
851 if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200852 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200853 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200854 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200855 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200856
Bram Moolenaar09689a02020-05-09 22:50:08 +0200857 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200858 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200859 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
860 + ufunc->uf_dfunc_idx;
861 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200862 {
863 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200864 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200865 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200866 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200868 CLEAR_FIELD(ectx);
869 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
870 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
871 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
872 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200874 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875
876 // Put arguments on the stack.
877 for (idx = 0; idx < argc; ++idx)
878 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200879 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200880 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
881 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200882 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 copy_tv(&argv[idx], STACK_TV_BOT(0));
884 ++ectx.ec_stack.ga_len;
885 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200886
887 // Turn varargs into a list. Empty list if no args.
888 if (ufunc->uf_va_name != NULL)
889 {
890 int vararg_count = argc - ufunc->uf_args.ga_len;
891
892 if (vararg_count < 0)
893 vararg_count = 0;
894 else
895 argc -= vararg_count;
896 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200897 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200898
899 // Check the type of the list items.
900 tv = STACK_TV_BOT(-1);
901 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +0200902 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200903 && ufunc->uf_va_type->tt_member != &t_any
904 && tv->vval.v_list != NULL)
905 {
906 type_T *expected = ufunc->uf_va_type->tt_member;
907 listitem_T *li = tv->vval.v_list->lv_first;
908
909 for (idx = 0; idx < vararg_count; ++idx)
910 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200911 if (check_typval_type(expected, &li->li_tv,
912 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200913 goto failed_early;
914 li = li->li_next;
915 }
916 }
917
Bram Moolenaar23e03252020-04-12 22:22:31 +0200918 if (defcount > 0)
919 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200920 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200921 --ectx.ec_stack.ga_len;
922 }
923
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100924 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200925 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100926 if (defcount > 0)
927 for (idx = 0; idx < defcount; ++idx)
928 {
929 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
930 ++ectx.ec_stack.ga_len;
931 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200932 if (ufunc->uf_va_name != NULL)
933 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934
935 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200936 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
937 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200939 if (partial != NULL)
940 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200941 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
942 {
943 // TODO: is this always the right way?
944 ectx.ec_outer_stack = &current_ectx->ec_stack;
945 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
946 }
947 else
948 {
949 ectx.ec_outer_stack = partial->pt_ectx_stack;
950 ectx.ec_outer_frame = partial->pt_ectx_frame;
951 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200952 }
953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954 // dummy frame entries
955 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
956 {
957 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
958 ++ectx.ec_stack.ga_len;
959 }
960
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200961 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200962 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200963 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
964 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100965
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200966 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200967 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200968 ectx.ec_stack.ga_len += dfunc->df_varcount;
969 if (dfunc->df_has_closure)
970 {
971 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
972 STACK_TV_VAR(idx)->vval.v_number = 0;
973 ++ectx.ec_stack.ga_len;
974 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200975
976 ectx.ec_instr = dfunc->df_instr;
977 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100978
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200979 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +0200980 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200981 estack_push_ufunc(ufunc, 1);
982 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200983 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
984
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200985 // Do turn errors into exceptions.
986 suppress_errthrow = FALSE;
987
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100988 // Decide where to start execution, handles optional arguments.
989 init_instr_idx(ufunc, argc, &ectx);
990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991 for (;;)
992 {
993 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100994
Bram Moolenaar270d0382020-05-15 21:42:53 +0200995 if (++breakcheck_count >= 100)
996 {
997 line_breakcheck();
998 breakcheck_count = 0;
999 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001000 if (got_int)
1001 {
1002 // Turn CTRL-C into an exception.
1003 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001004 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001005 goto failed;
1006 did_throw = TRUE;
1007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008
Bram Moolenaara26b9702020-04-18 19:53:28 +02001009 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1010 {
1011 // Turn an error message into an exception.
1012 did_emsg = FALSE;
1013 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1014 goto failed;
1015 did_throw = TRUE;
1016 *msg_list = NULL;
1017 }
1018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 if (did_throw && !ectx.ec_in_catch)
1020 {
1021 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001022 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023
1024 // An exception jumps to the first catch, finally, or returns from
1025 // the current function.
1026 if (trystack->ga_len > 0)
1027 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001028 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 {
1030 // jump to ":catch" or ":finally"
1031 ectx.ec_in_catch = TRUE;
1032 ectx.ec_iidx = trycmd->tcd_catch_idx;
1033 }
1034 else
1035 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001036 // Not inside try or need to return from current functions.
1037 // Push a dummy return value.
1038 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1039 goto failed;
1040 tv = STACK_TV_BOT(0);
1041 tv->v_type = VAR_NUMBER;
1042 tv->vval.v_number = 0;
1043 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001044 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001045 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001046 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001047 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001048 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1049 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 goto done;
1051 }
1052
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001053 if (func_return(&ectx) == FAIL)
1054 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 }
1056 continue;
1057 }
1058
1059 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1060 switch (iptr->isn_type)
1061 {
1062 // execute Ex command line
1063 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001064 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 do_cmdline_cmd(iptr->isn_arg.string);
1066 break;
1067
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001068 // execute Ex command from pieces on the stack
1069 case ISN_EXECCONCAT:
1070 {
1071 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001072 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001073 int pass;
1074 int i;
1075 char_u *cmd = NULL;
1076 char_u *str;
1077
1078 for (pass = 1; pass <= 2; ++pass)
1079 {
1080 for (i = 0; i < count; ++i)
1081 {
1082 tv = STACK_TV_BOT(i - count);
1083 str = tv->vval.v_string;
1084 if (str != NULL && *str != NUL)
1085 {
1086 if (pass == 2)
1087 STRCPY(cmd + len, str);
1088 len += STRLEN(str);
1089 }
1090 if (pass == 2)
1091 clear_tv(tv);
1092 }
1093 if (pass == 1)
1094 {
1095 cmd = alloc(len + 1);
1096 if (cmd == NULL)
1097 goto failed;
1098 len = 0;
1099 }
1100 }
1101
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001102 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001103 do_cmdline_cmd(cmd);
1104 vim_free(cmd);
1105 }
1106 break;
1107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 // execute :echo {string} ...
1109 case ISN_ECHO:
1110 {
1111 int count = iptr->isn_arg.echo.echo_count;
1112 int atstart = TRUE;
1113 int needclr = TRUE;
1114
1115 for (idx = 0; idx < count; ++idx)
1116 {
1117 tv = STACK_TV_BOT(idx - count);
1118 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1119 &atstart, &needclr);
1120 clear_tv(tv);
1121 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001122 if (needclr)
1123 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001124 ectx.ec_stack.ga_len -= count;
1125 }
1126 break;
1127
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001128 // :execute {string} ...
1129 // :echomsg {string} ...
1130 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001131 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001132 case ISN_ECHOMSG:
1133 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001134 {
1135 int count = iptr->isn_arg.number;
1136 garray_T ga;
1137 char_u buf[NUMBUFLEN];
1138 char_u *p;
1139 int len;
1140 int failed = FALSE;
1141
1142 ga_init2(&ga, 1, 80);
1143 for (idx = 0; idx < count; ++idx)
1144 {
1145 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001146 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001147 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001148 if (tv->v_type == VAR_CHANNEL
1149 || tv->v_type == VAR_JOB)
1150 {
1151 SOURCING_LNUM = iptr->isn_lnum;
1152 emsg(_(e_inval_string));
1153 break;
1154 }
1155 else
1156 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001157 }
1158 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001159 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001160
1161 len = (int)STRLEN(p);
1162 if (ga_grow(&ga, len + 2) == FAIL)
1163 failed = TRUE;
1164 else
1165 {
1166 if (ga.ga_len > 0)
1167 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1168 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1169 ga.ga_len += len;
1170 }
1171 clear_tv(tv);
1172 }
1173 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001174 if (failed)
1175 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001176
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001177 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001178 {
1179 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001180 {
1181 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001182 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaar430deb12020-08-23 16:29:11 +02001183 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001184 else
1185 {
1186 msg_sb_eol();
1187 if (iptr->isn_type == ISN_ECHOMSG)
1188 {
1189 msg_attr(ga.ga_data, echo_attr);
1190 out_flush();
1191 }
1192 else
1193 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001194 SOURCING_LNUM = iptr->isn_lnum;
1195 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001196 }
1197 }
1198 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001199 ga_clear(&ga);
1200 }
1201 break;
1202
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 // load local variable or argument
1204 case ISN_LOAD:
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(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1208 ++ectx.ec_stack.ga_len;
1209 break;
1210
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001211 // load variable or argument from outer scope
1212 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001213 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001214 goto failed;
1215 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1216 STACK_TV_BOT(0));
1217 ++ectx.ec_stack.ga_len;
1218 break;
1219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 // load v: variable
1221 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001222 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223 goto failed;
1224 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1225 ++ectx.ec_stack.ga_len;
1226 break;
1227
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001228 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 case ISN_LOADSCRIPT:
1230 {
1231 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001232 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 svar_T *sv;
1234
1235 sv = ((svar_T *)si->sn_var_vals.ga_data)
1236 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001237 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 goto failed;
1239 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1240 ++ectx.ec_stack.ga_len;
1241 }
1242 break;
1243
1244 // load s: variable in old script
1245 case ISN_LOADS:
1246 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001247 hashtab_T *ht = &SCRIPT_VARS(
1248 iptr->isn_arg.loadstore.ls_sid);
1249 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252 if (di == NULL)
1253 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001254 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001255 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001256 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 }
1258 else
1259 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001260 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 goto failed;
1262 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1263 ++ectx.ec_stack.ga_len;
1264 }
1265 }
1266 break;
1267
Bram Moolenaard3aac292020-04-19 14:32:17 +02001268 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001270 case ISN_LOADB:
1271 case ISN_LOADW:
1272 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001274 dictitem_T *di = NULL;
1275 hashtab_T *ht = NULL;
1276 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001277
Bram Moolenaard3aac292020-04-19 14:32:17 +02001278 switch (iptr->isn_type)
1279 {
1280 case ISN_LOADG:
1281 ht = get_globvar_ht();
1282 namespace = 'g';
1283 break;
1284 case ISN_LOADB:
1285 ht = &curbuf->b_vars->dv_hashtab;
1286 namespace = 'b';
1287 break;
1288 case ISN_LOADW:
1289 ht = &curwin->w_vars->dv_hashtab;
1290 namespace = 'w';
1291 break;
1292 case ISN_LOADT:
1293 ht = &curtab->tp_vars->dv_hashtab;
1294 namespace = 't';
1295 break;
1296 default: // Cannot reach here
1297 goto failed;
1298 }
1299 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301 if (di == NULL)
1302 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001303 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001304 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001305 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001306 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307 }
1308 else
1309 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001310 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001311 goto failed;
1312 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1313 ++ectx.ec_stack.ga_len;
1314 }
1315 }
1316 break;
1317
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001318 // load g:/b:/w:/t: namespace
1319 case ISN_LOADGDICT:
1320 case ISN_LOADBDICT:
1321 case ISN_LOADWDICT:
1322 case ISN_LOADTDICT:
1323 {
1324 dict_T *d = NULL;
1325
1326 switch (iptr->isn_type)
1327 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001328 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1329 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1330 case ISN_LOADWDICT: d = curwin->w_vars; break;
1331 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001332 default: // Cannot reach here
1333 goto failed;
1334 }
1335 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1336 goto failed;
1337 tv = STACK_TV_BOT(0);
1338 tv->v_type = VAR_DICT;
1339 tv->v_lock = 0;
1340 tv->vval.v_dict = d;
1341 ++ectx.ec_stack.ga_len;
1342 }
1343 break;
1344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 // load &option
1346 case ISN_LOADOPT:
1347 {
1348 typval_T optval;
1349 char_u *name = iptr->isn_arg.string;
1350
Bram Moolenaara8c17702020-04-01 21:17:24 +02001351 // This is not expected to fail, name is checked during
1352 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001353 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001355 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001356 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 *STACK_TV_BOT(0) = optval;
1358 ++ectx.ec_stack.ga_len;
1359 }
1360 break;
1361
1362 // load $ENV
1363 case ISN_LOADENV:
1364 {
1365 typval_T optval;
1366 char_u *name = iptr->isn_arg.string;
1367
Bram Moolenaar270d0382020-05-15 21:42:53 +02001368 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001370 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001371 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372 *STACK_TV_BOT(0) = optval;
1373 ++ectx.ec_stack.ga_len;
1374 }
1375 break;
1376
1377 // load @register
1378 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001379 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 goto failed;
1381 tv = STACK_TV_BOT(0);
1382 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001383 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001384 // This may result in NULL, which should be equivalent to an
1385 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386 tv->vval.v_string = get_reg_contents(
1387 iptr->isn_arg.number, GREG_EXPR_SRC);
1388 ++ectx.ec_stack.ga_len;
1389 break;
1390
1391 // store local variable
1392 case ISN_STORE:
1393 --ectx.ec_stack.ga_len;
1394 tv = STACK_TV_VAR(iptr->isn_arg.number);
1395 clear_tv(tv);
1396 *tv = *STACK_TV_BOT(0);
1397 break;
1398
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001399 // store variable or argument in outer scope
1400 case ISN_STOREOUTER:
1401 --ectx.ec_stack.ga_len;
1402 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1403 clear_tv(tv);
1404 *tv = *STACK_TV_BOT(0);
1405 break;
1406
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001407 // store s: variable in old script
1408 case ISN_STORES:
1409 {
1410 hashtab_T *ht = &SCRIPT_VARS(
1411 iptr->isn_arg.loadstore.ls_sid);
1412 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001413 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001414
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001415 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001416 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001417 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001418 else
1419 {
1420 clear_tv(&di->di_tv);
1421 di->di_tv = *STACK_TV_BOT(0);
1422 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001423 }
1424 break;
1425
1426 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 case ISN_STORESCRIPT:
1428 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001429 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 iptr->isn_arg.script.script_sid);
1431 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1432 + iptr->isn_arg.script.script_idx;
1433
1434 --ectx.ec_stack.ga_len;
1435 clear_tv(sv->sv_tv);
1436 *sv->sv_tv = *STACK_TV_BOT(0);
1437 }
1438 break;
1439
1440 // store option
1441 case ISN_STOREOPT:
1442 {
1443 long n = 0;
1444 char_u *s = NULL;
1445 char *msg;
1446
1447 --ectx.ec_stack.ga_len;
1448 tv = STACK_TV_BOT(0);
1449 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001450 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001452 if (s == NULL)
1453 s = (char_u *)"";
1454 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001456 // must be VAR_NUMBER, CHECKTYPE makes sure
1457 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1459 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001460 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 if (msg != NULL)
1462 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001463 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001465 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 }
1468 break;
1469
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001470 // store $ENV
1471 case ISN_STOREENV:
1472 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001473 tv = STACK_TV_BOT(0);
1474 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1475 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001476 break;
1477
1478 // store @r
1479 case ISN_STOREREG:
1480 {
1481 int reg = iptr->isn_arg.number;
1482
1483 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001484 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001485 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001486 tv_get_string(tv), -1, FALSE);
1487 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001488 }
1489 break;
1490
1491 // store v: variable
1492 case ISN_STOREV:
1493 --ectx.ec_stack.ga_len;
1494 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1495 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001496 // should not happen, type is checked when compiling
1497 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001498 break;
1499
Bram Moolenaard3aac292020-04-19 14:32:17 +02001500 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001501 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001502 case ISN_STOREB:
1503 case ISN_STOREW:
1504 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505 {
1506 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001507 hashtab_T *ht;
1508 switch (iptr->isn_type)
1509 {
1510 case ISN_STOREG:
1511 ht = get_globvar_ht();
1512 break;
1513 case ISN_STOREB:
1514 ht = &curbuf->b_vars->dv_hashtab;
1515 break;
1516 case ISN_STOREW:
1517 ht = &curwin->w_vars->dv_hashtab;
1518 break;
1519 case ISN_STORET:
1520 ht = &curtab->tp_vars->dv_hashtab;
1521 break;
1522 default: // Cannot reach here
1523 goto failed;
1524 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525
1526 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001527 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001529 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 else
1531 {
1532 clear_tv(&di->di_tv);
1533 di->di_tv = *STACK_TV_BOT(0);
1534 }
1535 }
1536 break;
1537
1538 // store number in local variable
1539 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001540 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 clear_tv(tv);
1542 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001543 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 break;
1545
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001546 // store value in list variable
1547 case ISN_STORELIST:
1548 {
1549 typval_T *tv_idx = STACK_TV_BOT(-2);
1550 varnumber_T lidx = tv_idx->vval.v_number;
1551 typval_T *tv_list = STACK_TV_BOT(-1);
1552 list_T *list = tv_list->vval.v_list;
1553
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001554 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001555 if (lidx < 0 && list->lv_len + lidx >= 0)
1556 // negative index is relative to the end
1557 lidx = list->lv_len + lidx;
1558 if (lidx < 0 || lidx > list->lv_len)
1559 {
1560 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001561 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001562 }
1563 tv = STACK_TV_BOT(-3);
1564 if (lidx < list->lv_len)
1565 {
1566 listitem_T *li = list_find(list, lidx);
1567
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001568 if (error_if_locked(li->li_tv.v_lock,
1569 e_cannot_change_list_item))
1570 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001571 // overwrite existing list item
1572 clear_tv(&li->li_tv);
1573 li->li_tv = *tv;
1574 }
1575 else
1576 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001577 if (error_if_locked(list->lv_lock,
1578 e_cannot_change_list))
1579 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001580 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001581 if (list_append_tv(list, tv) == FAIL)
1582 goto failed;
1583 clear_tv(tv);
1584 }
1585 clear_tv(tv_idx);
1586 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001587 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001588 }
1589 break;
1590
1591 // store value in dict variable
1592 case ISN_STOREDICT:
1593 {
1594 typval_T *tv_key = STACK_TV_BOT(-2);
1595 char_u *key = tv_key->vval.v_string;
1596 typval_T *tv_dict = STACK_TV_BOT(-1);
1597 dict_T *dict = tv_dict->vval.v_dict;
1598 dictitem_T *di;
1599
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001600 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001601 if (dict == NULL)
1602 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001603 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001604 goto on_error;
1605 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001606 if (key == NULL)
1607 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001608 tv = STACK_TV_BOT(-3);
1609 di = dict_find(dict, key, -1);
1610 if (di != NULL)
1611 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001612 if (error_if_locked(di->di_tv.v_lock,
1613 e_cannot_change_dict_item))
1614 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001615 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001616 clear_tv(&di->di_tv);
1617 di->di_tv = *tv;
1618 }
1619 else
1620 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001621 if (error_if_locked(dict->dv_lock,
1622 e_cannot_change_dict))
1623 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001624 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001625 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1626 goto failed;
1627 clear_tv(tv);
1628 }
1629 clear_tv(tv_key);
1630 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001631 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001632 }
1633 break;
1634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 // push constant
1636 case ISN_PUSHNR:
1637 case ISN_PUSHBOOL:
1638 case ISN_PUSHSPEC:
1639 case ISN_PUSHF:
1640 case ISN_PUSHS:
1641 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001642 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001643 case ISN_PUSHCHANNEL:
1644 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001645 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 goto failed;
1647 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001648 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 ++ectx.ec_stack.ga_len;
1650 switch (iptr->isn_type)
1651 {
1652 case ISN_PUSHNR:
1653 tv->v_type = VAR_NUMBER;
1654 tv->vval.v_number = iptr->isn_arg.number;
1655 break;
1656 case ISN_PUSHBOOL:
1657 tv->v_type = VAR_BOOL;
1658 tv->vval.v_number = iptr->isn_arg.number;
1659 break;
1660 case ISN_PUSHSPEC:
1661 tv->v_type = VAR_SPECIAL;
1662 tv->vval.v_number = iptr->isn_arg.number;
1663 break;
1664#ifdef FEAT_FLOAT
1665 case ISN_PUSHF:
1666 tv->v_type = VAR_FLOAT;
1667 tv->vval.v_float = iptr->isn_arg.fnumber;
1668 break;
1669#endif
1670 case ISN_PUSHBLOB:
1671 blob_copy(iptr->isn_arg.blob, tv);
1672 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001673 case ISN_PUSHFUNC:
1674 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001675 if (iptr->isn_arg.string == NULL)
1676 tv->vval.v_string = NULL;
1677 else
1678 tv->vval.v_string =
1679 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001680 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001681 case ISN_PUSHCHANNEL:
1682#ifdef FEAT_JOB_CHANNEL
1683 tv->v_type = VAR_CHANNEL;
1684 tv->vval.v_channel = iptr->isn_arg.channel;
1685 if (tv->vval.v_channel != NULL)
1686 ++tv->vval.v_channel->ch_refcount;
1687#endif
1688 break;
1689 case ISN_PUSHJOB:
1690#ifdef FEAT_JOB_CHANNEL
1691 tv->v_type = VAR_JOB;
1692 tv->vval.v_job = iptr->isn_arg.job;
1693 if (tv->vval.v_job != NULL)
1694 ++tv->vval.v_job->jv_refcount;
1695#endif
1696 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 default:
1698 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001699 tv->vval.v_string = vim_strsave(
1700 iptr->isn_arg.string == NULL
1701 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 }
1703 break;
1704
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001705 case ISN_UNLET:
1706 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1707 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001708 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001709 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001710 case ISN_UNLETENV:
1711 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1712 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001713
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001714 case ISN_LOCKCONST:
1715 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1716 break;
1717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 // create a list from items on the stack; uses a single allocation
1719 // for the list header and the items
1720 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001721 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1722 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 break;
1724
1725 // create a dict from items on the stack
1726 case ISN_NEWDICT:
1727 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001728 int count = iptr->isn_arg.number;
1729 dict_T *dict = dict_alloc();
1730 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731
1732 if (dict == NULL)
1733 goto failed;
1734 for (idx = 0; idx < count; ++idx)
1735 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001736 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001738 // check key is unique
1739 item = dict_find(dict, tv->vval.v_string, -1);
1740 if (item != NULL)
1741 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001742 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001743 semsg(_(e_duplicate_key), tv->vval.v_string);
1744 dict_unref(dict);
1745 goto on_error;
1746 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 item = dictitem_alloc(tv->vval.v_string);
1748 clear_tv(tv);
1749 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001750 {
1751 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001753 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1755 item->di_tv.v_lock = 0;
1756 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001757 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001758 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001759 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001761 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 }
1763
1764 if (count > 0)
1765 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001766 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767 goto failed;
1768 else
1769 ++ectx.ec_stack.ga_len;
1770 tv = STACK_TV_BOT(-1);
1771 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001772 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 tv->vval.v_dict = dict;
1774 ++dict->dv_refcount;
1775 }
1776 break;
1777
1778 // call a :def function
1779 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001780 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1782 iptr->isn_arg.dfunc.cdf_argcount,
1783 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001784 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 break;
1786
1787 // call a builtin function
1788 case ISN_BCALL:
1789 SOURCING_LNUM = iptr->isn_lnum;
1790 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1791 iptr->isn_arg.bfunc.cbf_argcount,
1792 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001793 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794 break;
1795
1796 // call a funcref or partial
1797 case ISN_PCALL:
1798 {
1799 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1800 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001801 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802
1803 SOURCING_LNUM = iptr->isn_lnum;
1804 if (pfunc->cpf_top)
1805 {
1806 // funcref is above the arguments
1807 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1808 }
1809 else
1810 {
1811 // Get the funcref from the stack.
1812 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001813 partial_tv = *STACK_TV_BOT(0);
1814 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 }
1816 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001817 if (tv == &partial_tv)
1818 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001820 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 }
1822 break;
1823
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001824 case ISN_PCALL_END:
1825 // PCALL finished, arguments have been consumed and replaced by
1826 // the return value. Now clear the funcref from the stack,
1827 // and move the return value in its place.
1828 --ectx.ec_stack.ga_len;
1829 clear_tv(STACK_TV_BOT(-1));
1830 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1831 break;
1832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 // call a user defined function or funcref/partial
1834 case ISN_UCALL:
1835 {
1836 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1837
1838 SOURCING_LNUM = iptr->isn_lnum;
1839 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001840 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001841 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 }
1843 break;
1844
1845 // return from a :def function call
1846 case ISN_RETURN:
1847 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001848 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001849 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001850
1851 if (trystack->ga_len > 0)
1852 trycmd = ((trycmd_T *)trystack->ga_data)
1853 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001854 if (trycmd != NULL
1855 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856 && trycmd->tcd_finally_idx != 0)
1857 {
1858 // jump to ":finally"
1859 ectx.ec_iidx = trycmd->tcd_finally_idx;
1860 trycmd->tcd_return = TRUE;
1861 }
1862 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001863 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 }
1865 break;
1866
1867 // push a function reference to a compiled function
1868 case ISN_FUNCREF:
1869 {
1870 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001871 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872
1873 pt = ALLOC_CLEAR_ONE(partial_T);
1874 if (pt == NULL)
1875 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001876 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001877 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001878 vim_free(pt);
1879 goto failed;
1880 }
1881 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1882 + iptr->isn_arg.funcref.fr_func;
1883 pt->pt_func = pt_dfunc->df_ufunc;
1884 pt->pt_refcount = 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001885
1886 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1887 {
1888 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1889 + ectx.ec_dfunc_idx;
1890
1891 // The closure needs to find arguments and local
1892 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001893 pt->pt_ectx_stack = &ectx.ec_stack;
1894 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001895
1896 // If this function returns and the closure is still
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001897 // being used, we need to make a copy of the context
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001898 // (arguments and local variables). Store a reference
1899 // to the partial so we can handle that.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001900 if (ga_grow(&ectx.ec_funcrefs, 1) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001901 {
Bram Moolenaardec07512020-09-18 23:11:10 +02001902 vim_free(pt);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001903 goto failed;
1904 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001905 // Extra variable keeps the count of closures created
1906 // in the current function call.
1907 tv = STACK_TV_VAR(dfunc->df_varcount);
1908 ++tv->vval.v_number;
1909
1910 ((partial_T **)ectx.ec_funcrefs.ga_data)
1911 [ectx.ec_funcrefs.ga_len] = pt;
1912 ++pt->pt_refcount;
1913 ++ectx.ec_funcrefs.ga_len;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001914 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001915 ++pt_dfunc->df_ufunc->uf_refcount;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 tv = STACK_TV_BOT(0);
1918 ++ectx.ec_stack.ga_len;
1919 tv->vval.v_partial = pt;
1920 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001921 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 }
1923 break;
1924
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001925 // Create a global function from a lambda.
1926 case ISN_NEWFUNC:
1927 {
1928 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1929
1930 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1931 }
1932 break;
1933
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 // jump if a condition is met
1935 case ISN_JUMP:
1936 {
1937 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001938 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 int jump = TRUE;
1940
1941 if (when != JUMP_ALWAYS)
1942 {
1943 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001944 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02001945 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001946 || when == JUMP_IF_COND_TRUE)
1947 {
1948 SOURCING_LNUM = iptr->isn_lnum;
1949 jump = tv_get_bool_chk(tv, &error);
1950 if (error)
1951 goto on_error;
1952 }
1953 else
1954 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001956 || when == JUMP_AND_KEEP_IF_FALSE
1957 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001959 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 {
1961 // drop the value from the stack
1962 clear_tv(tv);
1963 --ectx.ec_stack.ga_len;
1964 }
1965 }
1966 if (jump)
1967 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1968 }
1969 break;
1970
1971 // top of a for loop
1972 case ISN_FOR:
1973 {
1974 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1975 typval_T *idxtv =
1976 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1977
1978 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001979 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001981 ++idxtv->vval.v_number;
1982 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 // past the end of the list, jump to "endfor"
1984 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1985 else if (list->lv_first == &range_list_item)
1986 {
1987 // non-materialized range() list
1988 tv = STACK_TV_BOT(0);
1989 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001990 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 tv->vval.v_number = list_find_nr(
1992 list, idxtv->vval.v_number, NULL);
1993 ++ectx.ec_stack.ga_len;
1994 }
1995 else
1996 {
1997 listitem_T *li = list_find(list, idxtv->vval.v_number);
1998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2000 ++ectx.ec_stack.ga_len;
2001 }
2002 }
2003 break;
2004
2005 // start of ":try" block
2006 case ISN_TRY:
2007 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002008 trycmd_T *trycmd = NULL;
2009
Bram Moolenaar270d0382020-05-15 21:42:53 +02002010 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 goto failed;
2012 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2013 + ectx.ec_trystack.ga_len;
2014 ++ectx.ec_trystack.ga_len;
2015 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002016 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2018 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002019 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002020 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 }
2022 break;
2023
2024 case ISN_PUSHEXC:
2025 if (current_exception == NULL)
2026 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002027 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028 iemsg("Evaluating catch while current_exception is NULL");
2029 goto failed;
2030 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002031 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 goto failed;
2033 tv = STACK_TV_BOT(0);
2034 ++ectx.ec_stack.ga_len;
2035 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002036 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037 tv->vval.v_string = vim_strsave(
2038 (char_u *)current_exception->value);
2039 break;
2040
2041 case ISN_CATCH:
2042 {
2043 garray_T *trystack = &ectx.ec_trystack;
2044
2045 if (trystack->ga_len > 0)
2046 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002047 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 + trystack->ga_len - 1;
2049 trycmd->tcd_caught = TRUE;
2050 }
2051 did_emsg = got_int = did_throw = FALSE;
2052 catch_exception(current_exception);
2053 }
2054 break;
2055
2056 // end of ":try" block
2057 case ISN_ENDTRY:
2058 {
2059 garray_T *trystack = &ectx.ec_trystack;
2060
2061 if (trystack->ga_len > 0)
2062 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002063 trycmd_T *trycmd = NULL;
2064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 --trystack->ga_len;
2066 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002067 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 trycmd = ((trycmd_T *)trystack->ga_data)
2069 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002070 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 {
2072 // discard the exception
2073 if (caught_stack == current_exception)
2074 caught_stack = caught_stack->caught;
2075 discard_current_exception();
2076 }
2077
2078 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002079 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080 }
2081 }
2082 break;
2083
2084 case ISN_THROW:
2085 --ectx.ec_stack.ga_len;
2086 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002087 if (tv->vval.v_string == NULL
2088 || *skipwhite(tv->vval.v_string) == NUL)
2089 {
2090 emsg(_(e_throw_with_empty_string));
2091 goto failed;
2092 }
2093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2095 {
2096 vim_free(tv->vval.v_string);
2097 goto failed;
2098 }
2099 did_throw = TRUE;
2100 break;
2101
2102 // compare with special values
2103 case ISN_COMPAREBOOL:
2104 case ISN_COMPARESPECIAL:
2105 {
2106 typval_T *tv1 = STACK_TV_BOT(-2);
2107 typval_T *tv2 = STACK_TV_BOT(-1);
2108 varnumber_T arg1 = tv1->vval.v_number;
2109 varnumber_T arg2 = tv2->vval.v_number;
2110 int res;
2111
2112 switch (iptr->isn_arg.op.op_type)
2113 {
2114 case EXPR_EQUAL: res = arg1 == arg2; break;
2115 case EXPR_NEQUAL: res = arg1 != arg2; break;
2116 default: res = 0; break;
2117 }
2118
2119 --ectx.ec_stack.ga_len;
2120 tv1->v_type = VAR_BOOL;
2121 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2122 }
2123 break;
2124
2125 // Operation with two number arguments
2126 case ISN_OPNR:
2127 case ISN_COMPARENR:
2128 {
2129 typval_T *tv1 = STACK_TV_BOT(-2);
2130 typval_T *tv2 = STACK_TV_BOT(-1);
2131 varnumber_T arg1 = tv1->vval.v_number;
2132 varnumber_T arg2 = tv2->vval.v_number;
2133 varnumber_T res;
2134
2135 switch (iptr->isn_arg.op.op_type)
2136 {
2137 case EXPR_MULT: res = arg1 * arg2; break;
2138 case EXPR_DIV: res = arg1 / arg2; break;
2139 case EXPR_REM: res = arg1 % arg2; break;
2140 case EXPR_SUB: res = arg1 - arg2; break;
2141 case EXPR_ADD: res = arg1 + arg2; break;
2142
2143 case EXPR_EQUAL: res = arg1 == arg2; break;
2144 case EXPR_NEQUAL: res = arg1 != arg2; break;
2145 case EXPR_GREATER: res = arg1 > arg2; break;
2146 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2147 case EXPR_SMALLER: res = arg1 < arg2; break;
2148 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2149 default: res = 0; break;
2150 }
2151
2152 --ectx.ec_stack.ga_len;
2153 if (iptr->isn_type == ISN_COMPARENR)
2154 {
2155 tv1->v_type = VAR_BOOL;
2156 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2157 }
2158 else
2159 tv1->vval.v_number = res;
2160 }
2161 break;
2162
2163 // Computation with two float arguments
2164 case ISN_OPFLOAT:
2165 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002166#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002167 {
2168 typval_T *tv1 = STACK_TV_BOT(-2);
2169 typval_T *tv2 = STACK_TV_BOT(-1);
2170 float_T arg1 = tv1->vval.v_float;
2171 float_T arg2 = tv2->vval.v_float;
2172 float_T res = 0;
2173 int cmp = FALSE;
2174
2175 switch (iptr->isn_arg.op.op_type)
2176 {
2177 case EXPR_MULT: res = arg1 * arg2; break;
2178 case EXPR_DIV: res = arg1 / arg2; break;
2179 case EXPR_SUB: res = arg1 - arg2; break;
2180 case EXPR_ADD: res = arg1 + arg2; break;
2181
2182 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2183 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2184 case EXPR_GREATER: cmp = arg1 > arg2; break;
2185 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2186 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2187 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2188 default: cmp = 0; break;
2189 }
2190 --ectx.ec_stack.ga_len;
2191 if (iptr->isn_type == ISN_COMPAREFLOAT)
2192 {
2193 tv1->v_type = VAR_BOOL;
2194 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2195 }
2196 else
2197 tv1->vval.v_float = res;
2198 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002199#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 break;
2201
2202 case ISN_COMPARELIST:
2203 {
2204 typval_T *tv1 = STACK_TV_BOT(-2);
2205 typval_T *tv2 = STACK_TV_BOT(-1);
2206 list_T *arg1 = tv1->vval.v_list;
2207 list_T *arg2 = tv2->vval.v_list;
2208 int cmp = FALSE;
2209 int ic = iptr->isn_arg.op.op_ic;
2210
2211 switch (iptr->isn_arg.op.op_type)
2212 {
2213 case EXPR_EQUAL: cmp =
2214 list_equal(arg1, arg2, ic, FALSE); break;
2215 case EXPR_NEQUAL: cmp =
2216 !list_equal(arg1, arg2, ic, FALSE); break;
2217 case EXPR_IS: cmp = arg1 == arg2; break;
2218 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2219 default: cmp = 0; break;
2220 }
2221 --ectx.ec_stack.ga_len;
2222 clear_tv(tv1);
2223 clear_tv(tv2);
2224 tv1->v_type = VAR_BOOL;
2225 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2226 }
2227 break;
2228
2229 case ISN_COMPAREBLOB:
2230 {
2231 typval_T *tv1 = STACK_TV_BOT(-2);
2232 typval_T *tv2 = STACK_TV_BOT(-1);
2233 blob_T *arg1 = tv1->vval.v_blob;
2234 blob_T *arg2 = tv2->vval.v_blob;
2235 int cmp = FALSE;
2236
2237 switch (iptr->isn_arg.op.op_type)
2238 {
2239 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2240 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2241 case EXPR_IS: cmp = arg1 == arg2; break;
2242 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2243 default: cmp = 0; break;
2244 }
2245 --ectx.ec_stack.ga_len;
2246 clear_tv(tv1);
2247 clear_tv(tv2);
2248 tv1->v_type = VAR_BOOL;
2249 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2250 }
2251 break;
2252
2253 // TODO: handle separately
2254 case ISN_COMPARESTRING:
2255 case ISN_COMPAREDICT:
2256 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 case ISN_COMPAREANY:
2258 {
2259 typval_T *tv1 = STACK_TV_BOT(-2);
2260 typval_T *tv2 = STACK_TV_BOT(-1);
2261 exptype_T exptype = iptr->isn_arg.op.op_type;
2262 int ic = iptr->isn_arg.op.op_ic;
2263
Bram Moolenaareb26f432020-09-14 16:50:05 +02002264 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265 typval_compare(tv1, tv2, exptype, ic);
2266 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 --ectx.ec_stack.ga_len;
2268 }
2269 break;
2270
2271 case ISN_ADDLIST:
2272 case ISN_ADDBLOB:
2273 {
2274 typval_T *tv1 = STACK_TV_BOT(-2);
2275 typval_T *tv2 = STACK_TV_BOT(-1);
2276
2277 if (iptr->isn_type == ISN_ADDLIST)
2278 eval_addlist(tv1, tv2);
2279 else
2280 eval_addblob(tv1, tv2);
2281 clear_tv(tv2);
2282 --ectx.ec_stack.ga_len;
2283 }
2284 break;
2285
2286 // Computation with two arguments of unknown type
2287 case ISN_OPANY:
2288 {
2289 typval_T *tv1 = STACK_TV_BOT(-2);
2290 typval_T *tv2 = STACK_TV_BOT(-1);
2291 varnumber_T n1, n2;
2292#ifdef FEAT_FLOAT
2293 float_T f1 = 0, f2 = 0;
2294#endif
2295 int error = FALSE;
2296
2297 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2298 {
2299 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2300 {
2301 eval_addlist(tv1, tv2);
2302 clear_tv(tv2);
2303 --ectx.ec_stack.ga_len;
2304 break;
2305 }
2306 else if (tv1->v_type == VAR_BLOB
2307 && tv2->v_type == VAR_BLOB)
2308 {
2309 eval_addblob(tv1, tv2);
2310 clear_tv(tv2);
2311 --ectx.ec_stack.ga_len;
2312 break;
2313 }
2314 }
2315#ifdef FEAT_FLOAT
2316 if (tv1->v_type == VAR_FLOAT)
2317 {
2318 f1 = tv1->vval.v_float;
2319 n1 = 0;
2320 }
2321 else
2322#endif
2323 {
2324 n1 = tv_get_number_chk(tv1, &error);
2325 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002326 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327#ifdef FEAT_FLOAT
2328 if (tv2->v_type == VAR_FLOAT)
2329 f1 = n1;
2330#endif
2331 }
2332#ifdef FEAT_FLOAT
2333 if (tv2->v_type == VAR_FLOAT)
2334 {
2335 f2 = tv2->vval.v_float;
2336 n2 = 0;
2337 }
2338 else
2339#endif
2340 {
2341 n2 = tv_get_number_chk(tv2, &error);
2342 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002343 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344#ifdef FEAT_FLOAT
2345 if (tv1->v_type == VAR_FLOAT)
2346 f2 = n2;
2347#endif
2348 }
2349#ifdef FEAT_FLOAT
2350 // if there is a float on either side the result is a float
2351 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2352 {
2353 switch (iptr->isn_arg.op.op_type)
2354 {
2355 case EXPR_MULT: f1 = f1 * f2; break;
2356 case EXPR_DIV: f1 = f1 / f2; break;
2357 case EXPR_SUB: f1 = f1 - f2; break;
2358 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002359 default: SOURCING_LNUM = iptr->isn_lnum;
2360 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002361 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 }
2363 clear_tv(tv1);
2364 clear_tv(tv2);
2365 tv1->v_type = VAR_FLOAT;
2366 tv1->vval.v_float = f1;
2367 --ectx.ec_stack.ga_len;
2368 }
2369 else
2370#endif
2371 {
2372 switch (iptr->isn_arg.op.op_type)
2373 {
2374 case EXPR_MULT: n1 = n1 * n2; break;
2375 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2376 case EXPR_SUB: n1 = n1 - n2; break;
2377 case EXPR_ADD: n1 = n1 + n2; break;
2378 default: n1 = num_modulus(n1, n2); break;
2379 }
2380 clear_tv(tv1);
2381 clear_tv(tv2);
2382 tv1->v_type = VAR_NUMBER;
2383 tv1->vval.v_number = n1;
2384 --ectx.ec_stack.ga_len;
2385 }
2386 }
2387 break;
2388
2389 case ISN_CONCAT:
2390 {
2391 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2392 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2393 char_u *res;
2394
2395 res = concat_str(str1, str2);
2396 clear_tv(STACK_TV_BOT(-2));
2397 clear_tv(STACK_TV_BOT(-1));
2398 --ectx.ec_stack.ga_len;
2399 STACK_TV_BOT(-1)->vval.v_string = res;
2400 }
2401 break;
2402
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002403 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002404 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002405 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002406 int is_slice = iptr->isn_type == ISN_STRSLICE;
2407 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002408 char_u *res;
2409
2410 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002411 // string slice: string is at stack-3, first index at
2412 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002413 if (is_slice)
2414 {
2415 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002416 n1 = tv->vval.v_number;
2417 }
2418
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002419 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002420 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002421
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002422 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002423 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002424 if (is_slice)
2425 // Slice: Select the characters from the string
2426 res = string_slice(tv->vval.v_string, n1, n2);
2427 else
2428 // Index: The resulting variable is a string of a
2429 // single character. If the index is too big or
2430 // negative the result is empty.
2431 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002432 vim_free(tv->vval.v_string);
2433 tv->vval.v_string = res;
2434 }
2435 break;
2436
2437 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002438 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439 {
Bram Moolenaared591872020-08-15 22:14:53 +02002440 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002442 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443
2444 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002445 // list slice: list is at stack-3, indexes at stack-2 and
2446 // stack-1
2447 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 list = tv->vval.v_list;
2449
2450 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002451 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002452 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002453
2454 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002455 {
Bram Moolenaared591872020-08-15 22:14:53 +02002456 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002457 n1 = tv->vval.v_number;
2458 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459 }
Bram Moolenaared591872020-08-15 22:14:53 +02002460
2461 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002462 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002463 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002464 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2465 == FAIL)
2466 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 }
2468 break;
2469
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002470 case ISN_ANYINDEX:
2471 case ISN_ANYSLICE:
2472 {
2473 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2474 typval_T *var1, *var2;
2475 int res;
2476
2477 // index: composite is at stack-2, index at stack-1
2478 // slice: composite is at stack-3, indexes at stack-2 and
2479 // stack-1
2480 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002481 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002482 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2483 goto on_error;
2484 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2485 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2486 res = eval_index_inner(tv, is_slice,
2487 var1, var2, NULL, -1, TRUE);
2488 clear_tv(var1);
2489 if (is_slice)
2490 clear_tv(var2);
2491 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2492 if (res == FAIL)
2493 goto on_error;
2494 }
2495 break;
2496
Bram Moolenaar9af78762020-06-16 11:34:42 +02002497 case ISN_SLICE:
2498 {
2499 list_T *list;
2500 int count = iptr->isn_arg.number;
2501
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002502 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002503 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002504 list = tv->vval.v_list;
2505
2506 // no error for short list, expect it to be checked earlier
2507 if (list != NULL && list->lv_len >= count)
2508 {
2509 list_T *newlist = list_slice(list,
2510 count, list->lv_len - 1);
2511
2512 if (newlist != NULL)
2513 {
2514 list_unref(list);
2515 tv->vval.v_list = newlist;
2516 ++newlist->lv_refcount;
2517 }
2518 }
2519 }
2520 break;
2521
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002522 case ISN_GETITEM:
2523 {
2524 listitem_T *li;
2525 int index = iptr->isn_arg.number;
2526
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002527 // Get list item: list is at stack-1, push item.
2528 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002529 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002530 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002531
2532 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2533 goto failed;
2534 ++ectx.ec_stack.ga_len;
2535 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2536 }
2537 break;
2538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 case ISN_MEMBER:
2540 {
2541 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002542 char_u *key;
2543 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002544 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002545
2546 // dict member: dict is at stack-2, key at stack-1
2547 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002548 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002549 dict = tv->vval.v_dict;
2550
2551 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002552 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002553 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002554
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002555 if ((di = dict_find(dict, key, -1)) == NULL)
2556 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002557 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002558 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002559 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002560 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002561 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002562 --ectx.ec_stack.ga_len;
2563 // Clear the dict after getting the item, to avoid that it
2564 // make the item invalid.
2565 tv = STACK_TV_BOT(-1);
2566 temp_tv = *tv;
2567 copy_tv(&di->di_tv, tv);
2568 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002569 }
2570 break;
2571
2572 // dict member with string key
2573 case ISN_STRINGMEMBER:
2574 {
2575 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002577 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578
2579 tv = STACK_TV_BOT(-1);
2580 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2581 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002582 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002584 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 }
2586 dict = tv->vval.v_dict;
2587
2588 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2589 == NULL)
2590 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002591 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002593 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002595 // Clear the dict after getting the item, to avoid that it
2596 // make the item invalid.
2597 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002599 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 }
2601 break;
2602
2603 case ISN_NEGATENR:
2604 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002605 if (tv->v_type != VAR_NUMBER
2606#ifdef FEAT_FLOAT
2607 && tv->v_type != VAR_FLOAT
2608#endif
2609 )
2610 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002611 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002612 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002613 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002614 }
2615#ifdef FEAT_FLOAT
2616 if (tv->v_type == VAR_FLOAT)
2617 tv->vval.v_float = -tv->vval.v_float;
2618 else
2619#endif
2620 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 break;
2622
2623 case ISN_CHECKNR:
2624 {
2625 int error = FALSE;
2626
2627 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002628 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002630 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 (void)tv_get_number_chk(tv, &error);
2632 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002633 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 }
2635 break;
2636
2637 case ISN_CHECKTYPE:
2638 {
2639 checktype_T *ct = &iptr->isn_arg.type;
2640
2641 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002642 SOURCING_LNUM = iptr->isn_lnum;
2643 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2644 goto on_error;
2645
2646 // number 0 is FALSE, number 1 is TRUE
2647 if (tv->v_type == VAR_NUMBER
2648 && ct->ct_type->tt_type == VAR_BOOL
2649 && (tv->vval.v_number == 0
2650 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002652 tv->v_type = VAR_BOOL;
2653 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002654 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 }
2656 }
2657 break;
2658
Bram Moolenaar9af78762020-06-16 11:34:42 +02002659 case ISN_CHECKLEN:
2660 {
2661 int min_len = iptr->isn_arg.checklen.cl_min_len;
2662 list_T *list = NULL;
2663
2664 tv = STACK_TV_BOT(-1);
2665 if (tv->v_type == VAR_LIST)
2666 list = tv->vval.v_list;
2667 if (list == NULL || list->lv_len < min_len
2668 || (list->lv_len > min_len
2669 && !iptr->isn_arg.checklen.cl_more_OK))
2670 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002671 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002672 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002673 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002674 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002675 }
2676 }
2677 break;
2678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002680 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 {
2682 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002683 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684
2685 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002686 if (iptr->isn_type == ISN_2BOOL)
2687 {
2688 n = tv2bool(tv);
2689 if (iptr->isn_arg.number) // invert
2690 n = !n;
2691 }
2692 else
2693 {
2694 SOURCING_LNUM = iptr->isn_lnum;
2695 n = tv_get_bool_chk(tv, &error);
2696 if (error)
2697 goto on_error;
2698 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 clear_tv(tv);
2700 tv->v_type = VAR_BOOL;
2701 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2702 }
2703 break;
2704
2705 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002706 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 {
2708 char_u *str;
2709
2710 tv = STACK_TV_BOT(iptr->isn_arg.number);
2711 if (tv->v_type != VAR_STRING)
2712 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002713 if (iptr->isn_type == ISN_2STRING_ANY)
2714 {
2715 switch (tv->v_type)
2716 {
2717 case VAR_SPECIAL:
2718 case VAR_BOOL:
2719 case VAR_NUMBER:
2720 case VAR_FLOAT:
2721 case VAR_BLOB: break;
2722 default: to_string_error(tv->v_type);
2723 goto on_error;
2724 }
2725 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 str = typval_tostring(tv);
2727 clear_tv(tv);
2728 tv->v_type = VAR_STRING;
2729 tv->vval.v_string = str;
2730 }
2731 }
2732 break;
2733
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002734 case ISN_PUT:
2735 {
2736 int regname = iptr->isn_arg.put.put_regname;
2737 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2738 char_u *expr = NULL;
2739 int dir = FORWARD;
2740
2741 if (regname == '=')
2742 {
2743 tv = STACK_TV_BOT(-1);
2744 if (tv->v_type == VAR_STRING)
2745 expr = tv->vval.v_string;
2746 else
2747 {
2748 expr = typval_tostring(tv); // allocates value
2749 clear_tv(tv);
2750 }
2751 --ectx.ec_stack.ga_len;
2752 }
2753 if (lnum == -2)
2754 // :put! above cursor
2755 dir = BACKWARD;
2756 else if (lnum >= 0)
2757 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2758 check_cursor();
2759 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2760 vim_free(expr);
2761 }
2762 break;
2763
Bram Moolenaar389df252020-07-09 21:20:47 +02002764 case ISN_SHUFFLE:
2765 {
2766 typval_T tmp_tv;
2767 int item = iptr->isn_arg.shuffle.shfl_item;
2768 int up = iptr->isn_arg.shuffle.shfl_up;
2769
2770 tmp_tv = *STACK_TV_BOT(-item);
2771 for ( ; up > 0 && item > 1; --up)
2772 {
2773 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2774 --item;
2775 }
2776 *STACK_TV_BOT(-item) = tmp_tv;
2777 }
2778 break;
2779
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 case ISN_DROP:
2781 --ectx.ec_stack.ga_len;
2782 clear_tv(STACK_TV_BOT(0));
2783 break;
2784 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002785 continue;
2786
Bram Moolenaard032f342020-07-18 18:13:02 +02002787func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002788 // Restore previous function. If the frame pointer is where we started
2789 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02002790 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02002791 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002792
Bram Moolenaard032f342020-07-18 18:13:02 +02002793 if (func_return(&ectx) == FAIL)
2794 // only fails when out of memory
2795 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002796 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002797
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002798on_error:
2799 if (trylevel == 0)
2800 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 }
2802
2803done:
2804 // function finished, get result from the stack.
2805 tv = STACK_TV_BOT(-1);
2806 *rettv = *tv;
2807 tv->v_type = VAR_UNKNOWN;
2808 ret = OK;
2809
2810failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002811 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002812 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002813 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002814
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002815 // Deal with any remaining closures, they may be in use somewhere.
2816 if (ectx.ec_funcrefs.ga_len > 0)
2817 handle_closure_in_use(&ectx, FALSE);
2818
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002819 estack_pop();
2820 current_sctx = save_current_sctx;
2821
2822failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002823 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2825 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002828 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002829
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002830 // Not sure if this is necessary.
2831 suppress_errthrow = save_suppress_errthrow;
2832
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002833 if (ret != OK && called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002834 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002835 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 return ret;
2837}
2838
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839/*
2840 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002841 * We don't really need this at runtime, but we do have tests that require it,
2842 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 */
2844 void
2845ex_disassemble(exarg_T *eap)
2846{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002847 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002848 char_u *fname;
2849 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 dfunc_T *dfunc;
2851 isn_T *instr;
2852 int current;
2853 int line_idx = 0;
2854 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002855 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002857 if (STRNCMP(arg, "<lambda>", 8) == 0)
2858 {
2859 arg += 8;
2860 (void)getdigits(&arg);
2861 fname = vim_strnsave(eap->arg, arg - eap->arg);
2862 }
2863 else
2864 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002865 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002866 if (fname == NULL)
2867 {
2868 semsg(_(e_invarg2), eap->arg);
2869 return;
2870 }
2871
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002872 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002873 if (ufunc == NULL)
2874 {
2875 char_u *p = untrans_function_name(fname);
2876
2877 if (p != NULL)
2878 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002879 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002880 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002881 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 if (ufunc == NULL)
2883 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002884 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 return;
2886 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002887 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002888 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2889 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002890 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002892 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 return;
2894 }
2895 if (ufunc->uf_name_exp != NULL)
2896 msg((char *)ufunc->uf_name_exp);
2897 else
2898 msg((char *)ufunc->uf_name);
2899
2900 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2901 instr = dfunc->df_instr;
2902 for (current = 0; current < dfunc->df_instr_count; ++current)
2903 {
2904 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002905 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906
2907 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2908 {
2909 if (current > prev_current)
2910 {
2911 msg_puts("\n\n");
2912 prev_current = current;
2913 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002914 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2915 if (line != NULL)
2916 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 }
2918
2919 switch (iptr->isn_type)
2920 {
2921 case ISN_EXEC:
2922 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2923 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002924 case ISN_EXECCONCAT:
2925 smsg("%4d EXECCONCAT %lld", current,
2926 (long long)iptr->isn_arg.number);
2927 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 case ISN_ECHO:
2929 {
2930 echo_T *echo = &iptr->isn_arg.echo;
2931
2932 smsg("%4d %s %d", current,
2933 echo->echo_with_white ? "ECHO" : "ECHON",
2934 echo->echo_count);
2935 }
2936 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002937 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002938 smsg("%4d EXECUTE %lld", current,
2939 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002940 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002941 case ISN_ECHOMSG:
2942 smsg("%4d ECHOMSG %lld", current,
2943 (long long)(iptr->isn_arg.number));
2944 break;
2945 case ISN_ECHOERR:
2946 smsg("%4d ECHOERR %lld", current,
2947 (long long)(iptr->isn_arg.number));
2948 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002950 case ISN_LOADOUTER:
2951 {
2952 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2953
2954 if (iptr->isn_arg.number < 0)
2955 smsg("%4d LOAD%s arg[%lld]", current, add,
2956 (long long)(iptr->isn_arg.number
2957 + STACK_FRAME_SIZE));
2958 else
2959 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002960 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 break;
2963 case ISN_LOADV:
2964 smsg("%4d LOADV v:%s", current,
2965 get_vim_var_name(iptr->isn_arg.number));
2966 break;
2967 case ISN_LOADSCRIPT:
2968 {
2969 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002970 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2972 + iptr->isn_arg.script.script_idx;
2973
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002974 smsg("%4d LOADSCRIPT %s-%d from %s", current,
2975 sv->sv_name,
2976 iptr->isn_arg.script.script_idx,
2977 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 }
2979 break;
2980 case ISN_LOADS:
2981 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002982 scriptitem_T *si = SCRIPT_ITEM(
2983 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984
2985 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002986 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 }
2988 break;
2989 case ISN_LOADG:
2990 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2991 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002992 case ISN_LOADB:
2993 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2994 break;
2995 case ISN_LOADW:
2996 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2997 break;
2998 case ISN_LOADT:
2999 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3000 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003001 case ISN_LOADGDICT:
3002 smsg("%4d LOAD g:", current);
3003 break;
3004 case ISN_LOADBDICT:
3005 smsg("%4d LOAD b:", current);
3006 break;
3007 case ISN_LOADWDICT:
3008 smsg("%4d LOAD w:", current);
3009 break;
3010 case ISN_LOADTDICT:
3011 smsg("%4d LOAD t:", current);
3012 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 case ISN_LOADOPT:
3014 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3015 break;
3016 case ISN_LOADENV:
3017 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3018 break;
3019 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003020 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 break;
3022
3023 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003024 case ISN_STOREOUTER:
3025 {
3026 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
3027
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003028 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003029 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003030 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003031 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003032 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003033 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003036 case ISN_STOREV:
3037 smsg("%4d STOREV v:%s", current,
3038 get_vim_var_name(iptr->isn_arg.number));
3039 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003041 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3042 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003043 case ISN_STOREB:
3044 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3045 break;
3046 case ISN_STOREW:
3047 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3048 break;
3049 case ISN_STORET:
3050 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3051 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003052 case ISN_STORES:
3053 {
3054 scriptitem_T *si = SCRIPT_ITEM(
3055 iptr->isn_arg.loadstore.ls_sid);
3056
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003057 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003058 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 break;
3061 case ISN_STORESCRIPT:
3062 {
3063 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003064 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3066 + iptr->isn_arg.script.script_idx;
3067
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003068 smsg("%4d STORESCRIPT %s-%d in %s", current,
3069 sv->sv_name,
3070 iptr->isn_arg.script.script_idx,
3071 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 }
3073 break;
3074 case ISN_STOREOPT:
3075 smsg("%4d STOREOPT &%s", current,
3076 iptr->isn_arg.storeopt.so_name);
3077 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003078 case ISN_STOREENV:
3079 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3080 break;
3081 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003082 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003083 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 case ISN_STORENR:
3085 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003086 iptr->isn_arg.storenr.stnr_val,
3087 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 break;
3089
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003090 case ISN_STORELIST:
3091 smsg("%4d STORELIST", current);
3092 break;
3093
3094 case ISN_STOREDICT:
3095 smsg("%4d STOREDICT", current);
3096 break;
3097
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 // constants
3099 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003100 smsg("%4d PUSHNR %lld", current,
3101 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 break;
3103 case ISN_PUSHBOOL:
3104 case ISN_PUSHSPEC:
3105 smsg("%4d PUSH %s", current,
3106 get_var_special_name(iptr->isn_arg.number));
3107 break;
3108 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003109#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003111#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 break;
3113 case ISN_PUSHS:
3114 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3115 break;
3116 case ISN_PUSHBLOB:
3117 {
3118 char_u *r;
3119 char_u numbuf[NUMBUFLEN];
3120 char_u *tofree;
3121
3122 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003123 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 vim_free(tofree);
3125 }
3126 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003127 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003128 {
3129 char *name = (char *)iptr->isn_arg.string;
3130
3131 smsg("%4d PUSHFUNC \"%s\"", current,
3132 name == NULL ? "[none]" : name);
3133 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003134 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003135 case ISN_PUSHCHANNEL:
3136#ifdef FEAT_JOB_CHANNEL
3137 {
3138 channel_T *channel = iptr->isn_arg.channel;
3139
3140 smsg("%4d PUSHCHANNEL %d", current,
3141 channel == NULL ? 0 : channel->ch_id);
3142 }
3143#endif
3144 break;
3145 case ISN_PUSHJOB:
3146#ifdef FEAT_JOB_CHANNEL
3147 {
3148 typval_T tv;
3149 char_u *name;
3150
3151 tv.v_type = VAR_JOB;
3152 tv.vval.v_job = iptr->isn_arg.job;
3153 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003154 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003155 }
3156#endif
3157 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 case ISN_PUSHEXC:
3159 smsg("%4d PUSH v:exception", current);
3160 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003161 case ISN_UNLET:
3162 smsg("%4d UNLET%s %s", current,
3163 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3164 iptr->isn_arg.unlet.ul_name);
3165 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003166 case ISN_UNLETENV:
3167 smsg("%4d UNLETENV%s $%s", current,
3168 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3169 iptr->isn_arg.unlet.ul_name);
3170 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003171 case ISN_LOCKCONST:
3172 smsg("%4d LOCKCONST", current);
3173 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003175 smsg("%4d NEWLIST size %lld", current,
3176 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 break;
3178 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003179 smsg("%4d NEWDICT size %lld", current,
3180 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 break;
3182
3183 // function call
3184 case ISN_BCALL:
3185 {
3186 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3187
3188 smsg("%4d BCALL %s(argc %d)", current,
3189 internal_func_name(cbfunc->cbf_idx),
3190 cbfunc->cbf_argcount);
3191 }
3192 break;
3193 case ISN_DCALL:
3194 {
3195 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3196 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3197 + cdfunc->cdf_idx;
3198
3199 smsg("%4d DCALL %s(argc %d)", current,
3200 df->df_ufunc->uf_name_exp != NULL
3201 ? df->df_ufunc->uf_name_exp
3202 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3203 }
3204 break;
3205 case ISN_UCALL:
3206 {
3207 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3208
3209 smsg("%4d UCALL %s(argc %d)", current,
3210 cufunc->cuf_name, cufunc->cuf_argcount);
3211 }
3212 break;
3213 case ISN_PCALL:
3214 {
3215 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3216
3217 smsg("%4d PCALL%s (argc %d)", current,
3218 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3219 }
3220 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003221 case ISN_PCALL_END:
3222 smsg("%4d PCALL end", current);
3223 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 case ISN_RETURN:
3225 smsg("%4d RETURN", current);
3226 break;
3227 case ISN_FUNCREF:
3228 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003229 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003231 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003233 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 }
3235 break;
3236
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003237 case ISN_NEWFUNC:
3238 {
3239 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3240
3241 smsg("%4d NEWFUNC %s %s", current,
3242 newfunc->nf_lambda, newfunc->nf_global);
3243 }
3244 break;
3245
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 case ISN_JUMP:
3247 {
3248 char *when = "?";
3249
3250 switch (iptr->isn_arg.jump.jump_when)
3251 {
3252 case JUMP_ALWAYS:
3253 when = "JUMP";
3254 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 case JUMP_AND_KEEP_IF_TRUE:
3256 when = "JUMP_AND_KEEP_IF_TRUE";
3257 break;
3258 case JUMP_IF_FALSE:
3259 when = "JUMP_IF_FALSE";
3260 break;
3261 case JUMP_AND_KEEP_IF_FALSE:
3262 when = "JUMP_AND_KEEP_IF_FALSE";
3263 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003264 case JUMP_IF_COND_FALSE:
3265 when = "JUMP_IF_COND_FALSE";
3266 break;
3267 case JUMP_IF_COND_TRUE:
3268 when = "JUMP_IF_COND_TRUE";
3269 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003271 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 iptr->isn_arg.jump.jump_where);
3273 }
3274 break;
3275
3276 case ISN_FOR:
3277 {
3278 forloop_T *forloop = &iptr->isn_arg.forloop;
3279
3280 smsg("%4d FOR $%d -> %d", current,
3281 forloop->for_idx, forloop->for_end);
3282 }
3283 break;
3284
3285 case ISN_TRY:
3286 {
3287 try_T *try = &iptr->isn_arg.try;
3288
3289 smsg("%4d TRY catch -> %d, finally -> %d", current,
3290 try->try_catch, try->try_finally);
3291 }
3292 break;
3293 case ISN_CATCH:
3294 // TODO
3295 smsg("%4d CATCH", current);
3296 break;
3297 case ISN_ENDTRY:
3298 smsg("%4d ENDTRY", current);
3299 break;
3300 case ISN_THROW:
3301 smsg("%4d THROW", current);
3302 break;
3303
3304 // expression operations on number
3305 case ISN_OPNR:
3306 case ISN_OPFLOAT:
3307 case ISN_OPANY:
3308 {
3309 char *what;
3310 char *ins;
3311
3312 switch (iptr->isn_arg.op.op_type)
3313 {
3314 case EXPR_MULT: what = "*"; break;
3315 case EXPR_DIV: what = "/"; break;
3316 case EXPR_REM: what = "%"; break;
3317 case EXPR_SUB: what = "-"; break;
3318 case EXPR_ADD: what = "+"; break;
3319 default: what = "???"; break;
3320 }
3321 switch (iptr->isn_type)
3322 {
3323 case ISN_OPNR: ins = "OPNR"; break;
3324 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3325 case ISN_OPANY: ins = "OPANY"; break;
3326 default: ins = "???"; break;
3327 }
3328 smsg("%4d %s %s", current, ins, what);
3329 }
3330 break;
3331
3332 case ISN_COMPAREBOOL:
3333 case ISN_COMPARESPECIAL:
3334 case ISN_COMPARENR:
3335 case ISN_COMPAREFLOAT:
3336 case ISN_COMPARESTRING:
3337 case ISN_COMPAREBLOB:
3338 case ISN_COMPARELIST:
3339 case ISN_COMPAREDICT:
3340 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 case ISN_COMPAREANY:
3342 {
3343 char *p;
3344 char buf[10];
3345 char *type;
3346
3347 switch (iptr->isn_arg.op.op_type)
3348 {
3349 case EXPR_EQUAL: p = "=="; break;
3350 case EXPR_NEQUAL: p = "!="; break;
3351 case EXPR_GREATER: p = ">"; break;
3352 case EXPR_GEQUAL: p = ">="; break;
3353 case EXPR_SMALLER: p = "<"; break;
3354 case EXPR_SEQUAL: p = "<="; break;
3355 case EXPR_MATCH: p = "=~"; break;
3356 case EXPR_IS: p = "is"; break;
3357 case EXPR_ISNOT: p = "isnot"; break;
3358 case EXPR_NOMATCH: p = "!~"; break;
3359 default: p = "???"; break;
3360 }
3361 STRCPY(buf, p);
3362 if (iptr->isn_arg.op.op_ic == TRUE)
3363 strcat(buf, "?");
3364 switch(iptr->isn_type)
3365 {
3366 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3367 case ISN_COMPARESPECIAL:
3368 type = "COMPARESPECIAL"; break;
3369 case ISN_COMPARENR: type = "COMPARENR"; break;
3370 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3371 case ISN_COMPARESTRING:
3372 type = "COMPARESTRING"; break;
3373 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3374 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3375 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3376 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3378 default: type = "???"; break;
3379 }
3380
3381 smsg("%4d %s %s", current, type, buf);
3382 }
3383 break;
3384
3385 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3386 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3387
3388 // expression operations
3389 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003390 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003391 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003392 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003393 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003394 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3395 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003396 case ISN_SLICE: smsg("%4d SLICE %lld",
3397 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003398 case ISN_GETITEM: smsg("%4d ITEM %lld",
3399 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003400 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3401 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 iptr->isn_arg.string); break;
3403 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3404
3405 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003406 case ISN_CHECKTYPE:
3407 {
3408 char *tofree;
3409
3410 smsg("%4d CHECKTYPE %s stack[%d]", current,
3411 type_name(iptr->isn_arg.type.ct_type, &tofree),
3412 iptr->isn_arg.type.ct_off);
3413 vim_free(tofree);
3414 break;
3415 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003416 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3417 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3418 iptr->isn_arg.checklen.cl_min_len);
3419 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003420 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 case ISN_2BOOL: if (iptr->isn_arg.number)
3422 smsg("%4d INVERT (!val)", current);
3423 else
3424 smsg("%4d 2BOOL (!!val)", current);
3425 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003426 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3427 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003428 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003429 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3430 (long long)(iptr->isn_arg.number));
3431 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003432 case ISN_PUT:
3433 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname,
3434 (long)iptr->isn_arg.put.put_lnum);
3435 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436
Bram Moolenaar389df252020-07-09 21:20:47 +02003437 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3438 iptr->isn_arg.shuffle.shfl_item,
3439 iptr->isn_arg.shuffle.shfl_up);
3440 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 case ISN_DROP: smsg("%4d DROP", current); break;
3442 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003443
3444 out_flush(); // output one line at a time
3445 ui_breakcheck();
3446 if (got_int)
3447 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449}
3450
3451/*
Bram Moolenaar13106602020-10-04 16:06:05 +02003452 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 * list, etc. Mostly like what JavaScript does, except that empty list and
3454 * empty dictionary are FALSE.
3455 */
3456 int
3457tv2bool(typval_T *tv)
3458{
3459 switch (tv->v_type)
3460 {
3461 case VAR_NUMBER:
3462 return tv->vval.v_number != 0;
3463 case VAR_FLOAT:
3464#ifdef FEAT_FLOAT
3465 return tv->vval.v_float != 0.0;
3466#else
3467 break;
3468#endif
3469 case VAR_PARTIAL:
3470 return tv->vval.v_partial != NULL;
3471 case VAR_FUNC:
3472 case VAR_STRING:
3473 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3474 case VAR_LIST:
3475 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3476 case VAR_DICT:
3477 return tv->vval.v_dict != NULL
3478 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3479 case VAR_BOOL:
3480 case VAR_SPECIAL:
3481 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3482 case VAR_JOB:
3483#ifdef FEAT_JOB_CHANNEL
3484 return tv->vval.v_job != NULL;
3485#else
3486 break;
3487#endif
3488 case VAR_CHANNEL:
3489#ifdef FEAT_JOB_CHANNEL
3490 return tv->vval.v_channel != NULL;
3491#else
3492 break;
3493#endif
3494 case VAR_BLOB:
3495 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3496 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003497 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 case VAR_VOID:
3499 break;
3500 }
3501 return FALSE;
3502}
3503
3504/*
3505 * If "tv" is a string give an error and return FAIL.
3506 */
3507 int
3508check_not_string(typval_T *tv)
3509{
3510 if (tv->v_type == VAR_STRING)
3511 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003512 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 clear_tv(tv);
3514 return FAIL;
3515 }
3516 return OK;
3517}
3518
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519
3520#endif // FEAT_EVAL