blob: eca99f3aa2078be85d67d982ac5187b4df0124bc [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 Moolenaar0ba48e82020-11-17 18:23:19 +0100230 // If depth of calling is getting too high, don't execute the function.
231 if (funcdepth_increment() == FAIL)
232 return FAIL;
233
Bram Moolenaarfe270812020-04-11 22:31:27 +0200234 // Move the vararg-list to below the missing optional arguments.
235 if (vararg_count > 0 && arg_to_add > 0)
236 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100237
238 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200239 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200240 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200241 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242
243 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
245 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200246 STACK_TV_BOT(2)->vval.v_string = (void *)ectx->ec_outer_stack;
247 STACK_TV_BOT(3)->vval.v_number = ectx->ec_outer_frame;
248 STACK_TV_BOT(4)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200249 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100250
251 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200252 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200254 if (dfunc->df_has_closure)
255 {
256 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
257
258 tv->v_type = VAR_NUMBER;
259 tv->vval.v_number = 0;
260 }
261 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262
263 // Set execution state to the start of the called function.
264 ectx->ec_dfunc_idx = cdf_idx;
265 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200266 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
267 if (entry != NULL)
268 {
269 // Set the script context to the script where the function was defined.
270 // TODO: save more than the SID?
271 entry->es_save_sid = current_sctx.sc_sid;
272 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
273 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100274
275 // Decide where to start execution, handles optional arguments.
276 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277
278 return OK;
279}
280
281// Get pointer to item in the stack.
282#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
283
284/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200285 * Used when returning from a function: Check if any closure is still
286 * referenced. If so then move the arguments and variables to a separate piece
287 * of stack to be used when the closure is called.
288 * When "free_arguments" is TRUE the arguments are to be freed.
289 * Returns FAIL when out of memory.
290 */
291 static int
292handle_closure_in_use(ectx_T *ectx, int free_arguments)
293{
294 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
295 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200296 int argcount;
297 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200298 int idx;
299 typval_T *tv;
300 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200301 garray_T *gap = &ectx->ec_funcrefs;
302 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200303
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200304 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200305 return OK; // function was freed
306 if (dfunc->df_has_closure == 0)
307 return OK; // no closures
308 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
309 closure_count = tv->vval.v_number;
310 if (closure_count == 0)
311 return OK; // no funcrefs created
312
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200313 argcount = ufunc_argcount(dfunc->df_ufunc);
314 top = ectx->ec_frame_idx - argcount;
315
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200316 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200317 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200318 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200319 partial_T *pt;
320 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200321
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200322 if (off < 0)
323 continue; // count is off or already done
324 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200325 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200326 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200327 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200328 int i;
329
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200330 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200331 // unreferenced on return.
332 for (i = 0; i < dfunc->df_varcount; ++i)
333 {
334 typval_T *stv = STACK_TV(ectx->ec_frame_idx
335 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200336 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200337 --refcount;
338 }
339 if (refcount > 1)
340 {
341 closure_in_use = TRUE;
342 break;
343 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200344 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200345 }
346
347 if (closure_in_use)
348 {
349 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
350 typval_T *stack;
351
352 // A closure is using the arguments and/or local variables.
353 // Move them to the called function.
354 if (funcstack == NULL)
355 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200356 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
357 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200358 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
359 funcstack->fs_ga.ga_data = stack;
360 if (stack == NULL)
361 {
362 vim_free(funcstack);
363 return FAIL;
364 }
365
366 // Move or copy the arguments.
367 for (idx = 0; idx < argcount; ++idx)
368 {
369 tv = STACK_TV(top + idx);
370 if (free_arguments)
371 {
372 *(stack + idx) = *tv;
373 tv->v_type = VAR_UNKNOWN;
374 }
375 else
376 copy_tv(tv, stack + idx);
377 }
378 // Move the local variables.
379 for (idx = 0; idx < dfunc->df_varcount; ++idx)
380 {
381 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200382
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200383 // A partial created for a local function, that is also used as a
384 // local variable, has a reference count for the variable, thus
385 // will never go down to zero. When all these refcounts are one
386 // then the funcstack is unused. We need to count how many we have
387 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200388 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
389 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200390 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200391
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200392 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200393 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
394 gap->ga_len - closure_count + i])
395 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200396 }
397
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200398 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200399 tv->v_type = VAR_UNKNOWN;
400 }
401
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200402 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200403 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200404 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
405 - closure_count + idx];
406 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200407 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200408 ++funcstack->fs_refcount;
409 pt->pt_funcstack = funcstack;
410 pt->pt_ectx_stack = &funcstack->fs_ga;
411 pt->pt_ectx_frame = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200412 }
413 }
414 }
415
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200416 for (idx = 0; idx < closure_count; ++idx)
417 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
418 - closure_count + idx]);
419 gap->ga_len -= closure_count;
420 if (gap->ga_len == 0)
421 ga_clear(gap);
422
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200423 return OK;
424}
425
426/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200427 * Called when a partial is freed or its reference count goes down to one. The
428 * funcstack may be the only reference to the partials in the local variables.
429 * Go over all of them, the funcref and can be freed if all partials
430 * referencing the funcstack have a reference count of one.
431 */
432 void
433funcstack_check_refcount(funcstack_T *funcstack)
434{
435 int i;
436 garray_T *gap = &funcstack->fs_ga;
437 int done = 0;
438
439 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
440 return;
441 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
442 {
443 typval_T *tv = ((typval_T *)gap->ga_data) + i;
444
445 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
446 && tv->vval.v_partial->pt_funcstack == funcstack
447 && tv->vval.v_partial->pt_refcount == 1)
448 ++done;
449 }
450 if (done == funcstack->fs_min_refcount)
451 {
452 typval_T *stack = gap->ga_data;
453
454 // All partials referencing the funcstack have a reference count of
455 // one, thus the funcstack is no longer of use.
456 for (i = 0; i < gap->ga_len; ++i)
457 clear_tv(stack + i);
458 vim_free(stack);
459 vim_free(funcstack);
460 }
461}
462
463/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 * Return from the current function.
465 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200466 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100467func_return(ectx_T *ectx)
468{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100469 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200470 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
471 + ectx->ec_dfunc_idx;
472 int argcount = ufunc_argcount(dfunc->df_ufunc);
473 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200474 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475
476 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200477 entry = estack_pop();
478 if (entry != NULL)
479 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100480
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200481 if (handle_closure_in_use(ectx, TRUE) == FAIL)
482 return FAIL;
483
484 // Clear the arguments.
485 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
486 clear_tv(STACK_TV(idx));
487
488 // Clear local variables and temp values, but not the return value.
489 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100490 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100491 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100492
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100493 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200494 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
495 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200496 ectx->ec_outer_stack =
497 (void *)STACK_TV(ectx->ec_frame_idx + 2)->vval.v_string;
498 ectx->ec_outer_frame = STACK_TV(ectx->ec_frame_idx + 3)->vval.v_number;
499 // restoring ec_frame_idx must be last
500 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 4)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100501 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
502 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100503
504 // Reset the stack to the position before the call, move the return value
505 // to the top of the stack.
506 idx = ectx->ec_stack.ga_len - 1;
507 ectx->ec_stack.ga_len = top + 1;
508 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200509
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100510 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200511 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512}
513
514#undef STACK_TV
515
516/*
517 * Prepare arguments and rettv for calling a builtin or user function.
518 */
519 static int
520call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
521{
522 int idx;
523 typval_T *tv;
524
525 // Move arguments from bottom of the stack to argvars[] and add terminator.
526 for (idx = 0; idx < argcount; ++idx)
527 argvars[idx] = *STACK_TV_BOT(idx - argcount);
528 argvars[argcount].v_type = VAR_UNKNOWN;
529
530 // Result replaces the arguments on the stack.
531 if (argcount > 0)
532 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200533 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534 return FAIL;
535 else
536 ++ectx->ec_stack.ga_len;
537
538 // Default return value is zero.
539 tv = STACK_TV_BOT(-1);
540 tv->v_type = VAR_NUMBER;
541 tv->vval.v_number = 0;
542
543 return OK;
544}
545
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200546// Ugly global to avoid passing the execution context around through many
547// layers.
548static ectx_T *current_ectx = NULL;
549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550/*
551 * Call a builtin function by index.
552 */
553 static int
554call_bfunc(int func_idx, int argcount, ectx_T *ectx)
555{
556 typval_T argvars[MAX_FUNC_ARGS];
557 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100558 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200559 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560
561 if (call_prepare(argcount, argvars, ectx) == FAIL)
562 return FAIL;
563
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200564 // Call the builtin function. Set "current_ectx" so that when it
565 // recursively invokes call_def_function() a closure context can be set.
566 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200568 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569
570 // Clear the arguments.
571 for (idx = 0; idx < argcount; ++idx)
572 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200573
Bram Moolenaar171fb922020-10-28 16:54:47 +0100574 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200575 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100576 return OK;
577}
578
579/*
580 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100581 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 */
583 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100584call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585{
586 typval_T argvars[MAX_FUNC_ARGS];
587 funcexe_T funcexe;
588 int error;
589 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100590 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200592 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200593 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
594 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200595 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100596 {
597 // The function has been compiled, can call it quickly. For a function
598 // that was defined later: we can call it directly next time.
599 if (iptr != NULL)
600 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100601 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100602 iptr->isn_type = ISN_DCALL;
603 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
604 iptr->isn_arg.dfunc.cdf_argcount = argcount;
605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100607 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608
609 if (call_prepare(argcount, argvars, ectx) == FAIL)
610 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200611 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100612 funcexe.evaluate = TRUE;
613
614 // Call the user function. Result goes in last position on the stack.
615 // TODO: add selfdict if there is one
616 error = call_user_func_check(ufunc, argcount, argvars,
617 STACK_TV_BOT(-1), &funcexe, NULL);
618
619 // Clear the arguments.
620 for (idx = 0; idx < argcount; ++idx)
621 clear_tv(&argvars[idx]);
622
623 if (error != FCERR_NONE)
624 {
625 user_func_error(error, ufunc->uf_name);
626 return FAIL;
627 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100628 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200629 // Error other than from calling the function itself.
630 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100631 return OK;
632}
633
634/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200635 * Return TRUE if an error was given or CTRL-C was pressed.
636 */
637 static int
638vim9_aborting(int prev_called_emsg)
639{
640 return called_emsg > prev_called_emsg || got_int || did_throw;
641}
642
643/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644 * Execute a function by "name".
645 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100646 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647 * Returns FAIL if not found without an error message.
648 */
649 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100650call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100651{
652 ufunc_T *ufunc;
653
654 if (builtin_function(name, -1))
655 {
656 int func_idx = find_internal_func(name);
657
658 if (func_idx < 0)
659 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200660 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100661 return FAIL;
662 return call_bfunc(func_idx, argcount, ectx);
663 }
664
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200665 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200666
667 if (ufunc == NULL)
668 {
669 int called_emsg_before = called_emsg;
670
671 if (script_autoload(name, TRUE))
672 // loaded a package, search for the function again
673 ufunc = find_func(name, FALSE, NULL);
674 if (vim9_aborting(called_emsg_before))
675 return FAIL; // bail out if loading the script caused an error
676 }
677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100678 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100679 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100680
681 return FAIL;
682}
683
684 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200685call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200687 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200688 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200690 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691
692 if (tv->v_type == VAR_PARTIAL)
693 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200694 partial_T *pt = tv->vval.v_partial;
695 int i;
696
697 if (pt->pt_argc > 0)
698 {
699 // Make space for arguments from the partial, shift the "argcount"
700 // arguments up.
701 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
702 return FAIL;
703 for (i = 1; i <= argcount; ++i)
704 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
705 ectx->ec_stack.ga_len += pt->pt_argc;
706 argcount += pt->pt_argc;
707
708 // copy the arguments from the partial onto the stack
709 for (i = 0; i < pt->pt_argc; ++i)
710 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
711 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100712
713 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200714 {
715 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
716
717 // closure may need the function context where it was defined
718 ectx->ec_outer_stack = pt->pt_ectx_stack;
719 ectx->ec_outer_frame = pt->pt_ectx_frame;
720
721 return ret;
722 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723 name = pt->pt_name;
724 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200725 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100726 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200727 if (name != NULL)
728 {
729 char_u fname_buf[FLEN_FIXED + 1];
730 char_u *tofree = NULL;
731 int error = FCERR_NONE;
732 char_u *fname;
733
734 // May need to translate <SNR>123_ to K_SNR.
735 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
736 if (error != FCERR_NONE)
737 res = FAIL;
738 else
739 res = call_by_name(fname, argcount, ectx, NULL);
740 vim_free(tofree);
741 }
742
743 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744 {
745 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200746 semsg(_(e_unknownfunc),
747 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748 return FAIL;
749 }
750 return OK;
751}
752
753/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200754 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
755 * TRUE.
756 */
757 static int
758error_if_locked(int lock, char *error)
759{
760 if (lock & (VAR_LOCKED | VAR_FIXED))
761 {
762 emsg(_(error));
763 return TRUE;
764 }
765 return FALSE;
766}
767
768/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100769 * Store "tv" in variable "name".
770 * This is for s: and g: variables.
771 */
772 static void
773store_var(char_u *name, typval_T *tv)
774{
775 funccal_entry_T entry;
776
777 save_funccal(&entry);
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200778 set_var_const(name, NULL, tv, FALSE, ASSIGN_NO_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100779 restore_funccal();
780}
781
Bram Moolenaard3aac292020-04-19 14:32:17 +0200782
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100783/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 * Execute a function by "name".
785 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100786 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787 */
788 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100789call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100790{
Bram Moolenaared677f52020-08-12 16:38:10 +0200791 int called_emsg_before = called_emsg;
792 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793
Bram Moolenaared677f52020-08-12 16:38:10 +0200794 res = call_by_name(name, argcount, ectx, iptr);
795 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200797 dictitem_T *v;
798
799 v = find_var(name, NULL, FALSE);
800 if (v == NULL)
801 {
802 semsg(_(e_unknownfunc), name);
803 return FAIL;
804 }
805 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
806 {
807 semsg(_(e_unknownfunc), name);
808 return FAIL;
809 }
810 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200812 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813}
814
815/*
816 * Call a "def" function from old Vim script.
817 * Return OK or FAIL.
818 */
819 int
820call_def_function(
821 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200822 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200824 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825 typval_T *rettv) // return value
826{
827 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200828 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200829 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830 typval_T *tv;
831 int idx;
832 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100833 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200834 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200835 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100836 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200837 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +0200838 msglist_T **saved_msg_list = NULL;
839 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +0200840 cmdmod_T save_cmdmod;
841 int restore_cmdmod = FALSE;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100842 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100843 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100844
845// Get pointer to item in the stack.
846#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
847
848// Get pointer to item at the bottom of the stack, -1 is the bottom.
849#undef STACK_TV_BOT
850#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
851
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200852// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200853#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 +0100854
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200855// Like STACK_TV_VAR but use the outer scope
856#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
857
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200858 if (ufunc->uf_def_status == UF_NOT_COMPILED
859 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200860 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200861 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100862 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200863 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200864 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200865 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200866 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200867
Bram Moolenaar09689a02020-05-09 22:50:08 +0200868 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200869 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200870 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
871 + ufunc->uf_dfunc_idx;
872 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200873 {
874 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200875 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200876 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200877 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100879 // If depth of calling is getting too high, don't execute the function.
880 orig_funcdepth = funcdepth_get();
881 if (funcdepth_increment() == FAIL)
882 return FAIL;
883
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200884 CLEAR_FIELD(ectx);
885 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
886 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
887 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100888 {
889 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200890 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200893 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894
895 // Put arguments on the stack.
896 for (idx = 0; idx < argc; ++idx)
897 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200898 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200899 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
900 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200901 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902 copy_tv(&argv[idx], STACK_TV_BOT(0));
903 ++ectx.ec_stack.ga_len;
904 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200905
906 // Turn varargs into a list. Empty list if no args.
907 if (ufunc->uf_va_name != NULL)
908 {
909 int vararg_count = argc - ufunc->uf_args.ga_len;
910
911 if (vararg_count < 0)
912 vararg_count = 0;
913 else
914 argc -= vararg_count;
915 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200916 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200917
918 // Check the type of the list items.
919 tv = STACK_TV_BOT(-1);
920 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +0200921 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200922 && ufunc->uf_va_type->tt_member != &t_any
923 && tv->vval.v_list != NULL)
924 {
925 type_T *expected = ufunc->uf_va_type->tt_member;
926 listitem_T *li = tv->vval.v_list->lv_first;
927
928 for (idx = 0; idx < vararg_count; ++idx)
929 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200930 if (check_typval_type(expected, &li->li_tv,
931 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200932 goto failed_early;
933 li = li->li_next;
934 }
935 }
936
Bram Moolenaar23e03252020-04-12 22:22:31 +0200937 if (defcount > 0)
938 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200939 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200940 --ectx.ec_stack.ga_len;
941 }
942
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100943 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200944 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100945 if (defcount > 0)
946 for (idx = 0; idx < defcount; ++idx)
947 {
948 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
949 ++ectx.ec_stack.ga_len;
950 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200951 if (ufunc->uf_va_name != NULL)
952 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953
954 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200955 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
956 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200958 if (partial != NULL)
959 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200960 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
961 {
962 // TODO: is this always the right way?
963 ectx.ec_outer_stack = &current_ectx->ec_stack;
964 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
965 }
966 else
967 {
968 ectx.ec_outer_stack = partial->pt_ectx_stack;
969 ectx.ec_outer_frame = partial->pt_ectx_frame;
970 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200971 }
972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973 // dummy frame entries
974 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
975 {
976 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
977 ++ectx.ec_stack.ga_len;
978 }
979
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200980 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200981 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200982 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
983 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100984
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200985 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200986 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200987 ectx.ec_stack.ga_len += dfunc->df_varcount;
988 if (dfunc->df_has_closure)
989 {
990 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
991 STACK_TV_VAR(idx)->vval.v_number = 0;
992 ++ectx.ec_stack.ga_len;
993 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200994
995 ectx.ec_instr = dfunc->df_instr;
996 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100997
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200998 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +0200999 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001000 estack_push_ufunc(ufunc, 1);
1001 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001002 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1003
Bram Moolenaar352134b2020-10-17 22:04:08 +02001004 // Use a specific location for storing error messages to be converted to an
1005 // exception.
1006 saved_msg_list = msg_list;
1007 msg_list = &private_msg_list;
1008
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001009 // Do turn errors into exceptions.
1010 suppress_errthrow = FALSE;
1011
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001012 // Decide where to start execution, handles optional arguments.
1013 init_instr_idx(ufunc, argc, &ectx);
1014
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001015 for (;;)
1016 {
1017 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001018
Bram Moolenaar270d0382020-05-15 21:42:53 +02001019 if (++breakcheck_count >= 100)
1020 {
1021 line_breakcheck();
1022 breakcheck_count = 0;
1023 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001024 if (got_int)
1025 {
1026 // Turn CTRL-C into an exception.
1027 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001028 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001029 goto failed;
1030 did_throw = TRUE;
1031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032
Bram Moolenaara26b9702020-04-18 19:53:28 +02001033 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1034 {
1035 // Turn an error message into an exception.
1036 did_emsg = FALSE;
1037 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1038 goto failed;
1039 did_throw = TRUE;
1040 *msg_list = NULL;
1041 }
1042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043 if (did_throw && !ectx.ec_in_catch)
1044 {
1045 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001046 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047
1048 // An exception jumps to the first catch, finally, or returns from
1049 // the current function.
1050 if (trystack->ga_len > 0)
1051 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001052 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 {
1054 // jump to ":catch" or ":finally"
1055 ectx.ec_in_catch = TRUE;
1056 ectx.ec_iidx = trycmd->tcd_catch_idx;
1057 }
1058 else
1059 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001060 // Not inside try or need to return from current functions.
1061 // Push a dummy return value.
1062 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1063 goto failed;
1064 tv = STACK_TV_BOT(0);
1065 tv->v_type = VAR_NUMBER;
1066 tv->vval.v_number = 0;
1067 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001068 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001070 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001071 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001072 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1073 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074 goto done;
1075 }
1076
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001077 if (func_return(&ectx) == FAIL)
1078 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001079 }
1080 continue;
1081 }
1082
1083 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1084 switch (iptr->isn_type)
1085 {
1086 // execute Ex command line
1087 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001088 {
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001089 SOURCING_LNUM = iptr->isn_lnum;
1090 do_cmdline_cmd(iptr->isn_arg.string);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001091 if (did_emsg)
1092 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 break;
1095
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001096 // execute Ex command from pieces on the stack
1097 case ISN_EXECCONCAT:
1098 {
1099 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001100 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001101 int pass;
1102 int i;
1103 char_u *cmd = NULL;
1104 char_u *str;
1105
1106 for (pass = 1; pass <= 2; ++pass)
1107 {
1108 for (i = 0; i < count; ++i)
1109 {
1110 tv = STACK_TV_BOT(i - count);
1111 str = tv->vval.v_string;
1112 if (str != NULL && *str != NUL)
1113 {
1114 if (pass == 2)
1115 STRCPY(cmd + len, str);
1116 len += STRLEN(str);
1117 }
1118 if (pass == 2)
1119 clear_tv(tv);
1120 }
1121 if (pass == 1)
1122 {
1123 cmd = alloc(len + 1);
1124 if (cmd == NULL)
1125 goto failed;
1126 len = 0;
1127 }
1128 }
1129
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001130 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001131 do_cmdline_cmd(cmd);
1132 vim_free(cmd);
1133 }
1134 break;
1135
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001136 // execute :echo {string} ...
1137 case ISN_ECHO:
1138 {
1139 int count = iptr->isn_arg.echo.echo_count;
1140 int atstart = TRUE;
1141 int needclr = TRUE;
1142
1143 for (idx = 0; idx < count; ++idx)
1144 {
1145 tv = STACK_TV_BOT(idx - count);
1146 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1147 &atstart, &needclr);
1148 clear_tv(tv);
1149 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001150 if (needclr)
1151 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 ectx.ec_stack.ga_len -= count;
1153 }
1154 break;
1155
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001156 // :execute {string} ...
1157 // :echomsg {string} ...
1158 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001159 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001160 case ISN_ECHOMSG:
1161 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001162 {
1163 int count = iptr->isn_arg.number;
1164 garray_T ga;
1165 char_u buf[NUMBUFLEN];
1166 char_u *p;
1167 int len;
1168 int failed = FALSE;
1169
1170 ga_init2(&ga, 1, 80);
1171 for (idx = 0; idx < count; ++idx)
1172 {
1173 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001174 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001175 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001176 if (tv->v_type == VAR_CHANNEL
1177 || tv->v_type == VAR_JOB)
1178 {
1179 SOURCING_LNUM = iptr->isn_lnum;
1180 emsg(_(e_inval_string));
1181 break;
1182 }
1183 else
1184 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001185 }
1186 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001187 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001188
1189 len = (int)STRLEN(p);
1190 if (ga_grow(&ga, len + 2) == FAIL)
1191 failed = TRUE;
1192 else
1193 {
1194 if (ga.ga_len > 0)
1195 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1196 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1197 ga.ga_len += len;
1198 }
1199 clear_tv(tv);
1200 }
1201 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001202 if (failed)
1203 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001204
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001205 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001206 {
1207 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001208 {
1209 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001210 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001211 if (did_emsg)
1212 goto on_error;
Bram Moolenaar430deb12020-08-23 16:29:11 +02001213 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001214 else
1215 {
1216 msg_sb_eol();
1217 if (iptr->isn_type == ISN_ECHOMSG)
1218 {
1219 msg_attr(ga.ga_data, echo_attr);
1220 out_flush();
1221 }
1222 else
1223 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001224 SOURCING_LNUM = iptr->isn_lnum;
1225 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001226 }
1227 }
1228 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001229 ga_clear(&ga);
1230 }
1231 break;
1232
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 // load local variable or argument
1234 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001235 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001236 goto failed;
1237 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1238 ++ectx.ec_stack.ga_len;
1239 break;
1240
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001241 // load variable or argument from outer scope
1242 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001243 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001244 goto failed;
1245 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1246 STACK_TV_BOT(0));
1247 ++ectx.ec_stack.ga_len;
1248 break;
1249
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250 // load v: variable
1251 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001252 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253 goto failed;
1254 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1255 ++ectx.ec_stack.ga_len;
1256 break;
1257
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001258 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259 case ISN_LOADSCRIPT:
1260 {
1261 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001262 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 svar_T *sv;
1264
1265 sv = ((svar_T *)si->sn_var_vals.ga_data)
1266 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001267 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 goto failed;
1269 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1270 ++ectx.ec_stack.ga_len;
1271 }
1272 break;
1273
1274 // load s: variable in old script
1275 case ISN_LOADS:
1276 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001277 hashtab_T *ht = &SCRIPT_VARS(
1278 iptr->isn_arg.loadstore.ls_sid);
1279 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 if (di == NULL)
1283 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001284 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001285 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001286 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 }
1288 else
1289 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001290 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 goto failed;
1292 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1293 ++ectx.ec_stack.ga_len;
1294 }
1295 }
1296 break;
1297
Bram Moolenaard3aac292020-04-19 14:32:17 +02001298 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001300 case ISN_LOADB:
1301 case ISN_LOADW:
1302 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001304 dictitem_T *di = NULL;
1305 hashtab_T *ht = NULL;
1306 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001307
Bram Moolenaard3aac292020-04-19 14:32:17 +02001308 switch (iptr->isn_type)
1309 {
1310 case ISN_LOADG:
1311 ht = get_globvar_ht();
1312 namespace = 'g';
1313 break;
1314 case ISN_LOADB:
1315 ht = &curbuf->b_vars->dv_hashtab;
1316 namespace = 'b';
1317 break;
1318 case ISN_LOADW:
1319 ht = &curwin->w_vars->dv_hashtab;
1320 namespace = 'w';
1321 break;
1322 case ISN_LOADT:
1323 ht = &curtab->tp_vars->dv_hashtab;
1324 namespace = 't';
1325 break;
1326 default: // Cannot reach here
1327 goto failed;
1328 }
1329 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001330
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 if (di == NULL)
1332 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001333 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001334 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001335 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001336 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 }
1338 else
1339 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001340 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 goto failed;
1342 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1343 ++ectx.ec_stack.ga_len;
1344 }
1345 }
1346 break;
1347
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001348 // load g:/b:/w:/t: namespace
1349 case ISN_LOADGDICT:
1350 case ISN_LOADBDICT:
1351 case ISN_LOADWDICT:
1352 case ISN_LOADTDICT:
1353 {
1354 dict_T *d = NULL;
1355
1356 switch (iptr->isn_type)
1357 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001358 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1359 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1360 case ISN_LOADWDICT: d = curwin->w_vars; break;
1361 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001362 default: // Cannot reach here
1363 goto failed;
1364 }
1365 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1366 goto failed;
1367 tv = STACK_TV_BOT(0);
1368 tv->v_type = VAR_DICT;
1369 tv->v_lock = 0;
1370 tv->vval.v_dict = d;
1371 ++ectx.ec_stack.ga_len;
1372 }
1373 break;
1374
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 // load &option
1376 case ISN_LOADOPT:
1377 {
1378 typval_T optval;
1379 char_u *name = iptr->isn_arg.string;
1380
Bram Moolenaara8c17702020-04-01 21:17:24 +02001381 // This is not expected to fail, name is checked during
1382 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001383 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001385 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001386 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 *STACK_TV_BOT(0) = optval;
1388 ++ectx.ec_stack.ga_len;
1389 }
1390 break;
1391
1392 // load $ENV
1393 case ISN_LOADENV:
1394 {
1395 typval_T optval;
1396 char_u *name = iptr->isn_arg.string;
1397
Bram Moolenaar270d0382020-05-15 21:42:53 +02001398 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001400 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001401 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 *STACK_TV_BOT(0) = optval;
1403 ++ectx.ec_stack.ga_len;
1404 }
1405 break;
1406
1407 // load @register
1408 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001409 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 goto failed;
1411 tv = STACK_TV_BOT(0);
1412 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001413 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001414 // This may result in NULL, which should be equivalent to an
1415 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 tv->vval.v_string = get_reg_contents(
1417 iptr->isn_arg.number, GREG_EXPR_SRC);
1418 ++ectx.ec_stack.ga_len;
1419 break;
1420
1421 // store local variable
1422 case ISN_STORE:
1423 --ectx.ec_stack.ga_len;
1424 tv = STACK_TV_VAR(iptr->isn_arg.number);
1425 clear_tv(tv);
1426 *tv = *STACK_TV_BOT(0);
1427 break;
1428
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001429 // store variable or argument in outer scope
1430 case ISN_STOREOUTER:
1431 --ectx.ec_stack.ga_len;
1432 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1433 clear_tv(tv);
1434 *tv = *STACK_TV_BOT(0);
1435 break;
1436
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001437 // store s: variable in old script
1438 case ISN_STORES:
1439 {
1440 hashtab_T *ht = &SCRIPT_VARS(
1441 iptr->isn_arg.loadstore.ls_sid);
1442 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001443 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001444
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001445 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001446 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001447 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001448 else
1449 {
1450 clear_tv(&di->di_tv);
1451 di->di_tv = *STACK_TV_BOT(0);
1452 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001453 }
1454 break;
1455
1456 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 case ISN_STORESCRIPT:
1458 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001459 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460 iptr->isn_arg.script.script_sid);
1461 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1462 + iptr->isn_arg.script.script_idx;
1463
1464 --ectx.ec_stack.ga_len;
1465 clear_tv(sv->sv_tv);
1466 *sv->sv_tv = *STACK_TV_BOT(0);
1467 }
1468 break;
1469
1470 // store option
1471 case ISN_STOREOPT:
1472 {
1473 long n = 0;
1474 char_u *s = NULL;
1475 char *msg;
1476
1477 --ectx.ec_stack.ga_len;
1478 tv = STACK_TV_BOT(0);
1479 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001480 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001482 if (s == NULL)
1483 s = (char_u *)"";
1484 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001486 // must be VAR_NUMBER, CHECKTYPE makes sure
1487 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1489 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001490 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 if (msg != NULL)
1492 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001493 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001495 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497 }
1498 break;
1499
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001500 // store $ENV
1501 case ISN_STOREENV:
1502 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001503 tv = STACK_TV_BOT(0);
1504 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1505 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001506 break;
1507
1508 // store @r
1509 case ISN_STOREREG:
1510 {
1511 int reg = iptr->isn_arg.number;
1512
1513 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001514 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001515 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001516 tv_get_string(tv), -1, FALSE);
1517 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001518 }
1519 break;
1520
1521 // store v: variable
1522 case ISN_STOREV:
1523 --ectx.ec_stack.ga_len;
1524 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1525 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001526 // should not happen, type is checked when compiling
1527 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001528 break;
1529
Bram Moolenaard3aac292020-04-19 14:32:17 +02001530 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001531 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001532 case ISN_STOREB:
1533 case ISN_STOREW:
1534 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 {
1536 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001537 hashtab_T *ht;
1538 switch (iptr->isn_type)
1539 {
1540 case ISN_STOREG:
1541 ht = get_globvar_ht();
1542 break;
1543 case ISN_STOREB:
1544 ht = &curbuf->b_vars->dv_hashtab;
1545 break;
1546 case ISN_STOREW:
1547 ht = &curwin->w_vars->dv_hashtab;
1548 break;
1549 case ISN_STORET:
1550 ht = &curtab->tp_vars->dv_hashtab;
1551 break;
1552 default: // Cannot reach here
1553 goto failed;
1554 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555
1556 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001557 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001559 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 else
1561 {
1562 clear_tv(&di->di_tv);
1563 di->di_tv = *STACK_TV_BOT(0);
1564 }
1565 }
1566 break;
1567
1568 // store number in local variable
1569 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001570 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571 clear_tv(tv);
1572 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001573 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574 break;
1575
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001576 // store value in list variable
1577 case ISN_STORELIST:
1578 {
1579 typval_T *tv_idx = STACK_TV_BOT(-2);
1580 varnumber_T lidx = tv_idx->vval.v_number;
1581 typval_T *tv_list = STACK_TV_BOT(-1);
1582 list_T *list = tv_list->vval.v_list;
1583
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001584 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001585 if (lidx < 0 && list->lv_len + lidx >= 0)
1586 // negative index is relative to the end
1587 lidx = list->lv_len + lidx;
1588 if (lidx < 0 || lidx > list->lv_len)
1589 {
1590 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001591 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001592 }
1593 tv = STACK_TV_BOT(-3);
1594 if (lidx < list->lv_len)
1595 {
1596 listitem_T *li = list_find(list, lidx);
1597
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001598 if (error_if_locked(li->li_tv.v_lock,
1599 e_cannot_change_list_item))
1600 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001601 // overwrite existing list item
1602 clear_tv(&li->li_tv);
1603 li->li_tv = *tv;
1604 }
1605 else
1606 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001607 if (error_if_locked(list->lv_lock,
1608 e_cannot_change_list))
1609 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001610 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001611 if (list_append_tv(list, tv) == FAIL)
1612 goto failed;
1613 clear_tv(tv);
1614 }
1615 clear_tv(tv_idx);
1616 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001617 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001618 }
1619 break;
1620
1621 // store value in dict variable
1622 case ISN_STOREDICT:
1623 {
1624 typval_T *tv_key = STACK_TV_BOT(-2);
1625 char_u *key = tv_key->vval.v_string;
1626 typval_T *tv_dict = STACK_TV_BOT(-1);
1627 dict_T *dict = tv_dict->vval.v_dict;
1628 dictitem_T *di;
1629
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001630 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001631 if (dict == NULL)
1632 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001633 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001634 goto on_error;
1635 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001636 if (key == NULL)
1637 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001638 tv = STACK_TV_BOT(-3);
1639 di = dict_find(dict, key, -1);
1640 if (di != NULL)
1641 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001642 if (error_if_locked(di->di_tv.v_lock,
1643 e_cannot_change_dict_item))
1644 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001645 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001646 clear_tv(&di->di_tv);
1647 di->di_tv = *tv;
1648 }
1649 else
1650 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001651 if (error_if_locked(dict->dv_lock,
1652 e_cannot_change_dict))
1653 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001654 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001655 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1656 goto failed;
1657 clear_tv(tv);
1658 }
1659 clear_tv(tv_key);
1660 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001661 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001662 }
1663 break;
1664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 // push constant
1666 case ISN_PUSHNR:
1667 case ISN_PUSHBOOL:
1668 case ISN_PUSHSPEC:
1669 case ISN_PUSHF:
1670 case ISN_PUSHS:
1671 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001672 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001673 case ISN_PUSHCHANNEL:
1674 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001675 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 goto failed;
1677 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001678 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 ++ectx.ec_stack.ga_len;
1680 switch (iptr->isn_type)
1681 {
1682 case ISN_PUSHNR:
1683 tv->v_type = VAR_NUMBER;
1684 tv->vval.v_number = iptr->isn_arg.number;
1685 break;
1686 case ISN_PUSHBOOL:
1687 tv->v_type = VAR_BOOL;
1688 tv->vval.v_number = iptr->isn_arg.number;
1689 break;
1690 case ISN_PUSHSPEC:
1691 tv->v_type = VAR_SPECIAL;
1692 tv->vval.v_number = iptr->isn_arg.number;
1693 break;
1694#ifdef FEAT_FLOAT
1695 case ISN_PUSHF:
1696 tv->v_type = VAR_FLOAT;
1697 tv->vval.v_float = iptr->isn_arg.fnumber;
1698 break;
1699#endif
1700 case ISN_PUSHBLOB:
1701 blob_copy(iptr->isn_arg.blob, tv);
1702 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001703 case ISN_PUSHFUNC:
1704 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001705 if (iptr->isn_arg.string == NULL)
1706 tv->vval.v_string = NULL;
1707 else
1708 tv->vval.v_string =
1709 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001710 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001711 case ISN_PUSHCHANNEL:
1712#ifdef FEAT_JOB_CHANNEL
1713 tv->v_type = VAR_CHANNEL;
1714 tv->vval.v_channel = iptr->isn_arg.channel;
1715 if (tv->vval.v_channel != NULL)
1716 ++tv->vval.v_channel->ch_refcount;
1717#endif
1718 break;
1719 case ISN_PUSHJOB:
1720#ifdef FEAT_JOB_CHANNEL
1721 tv->v_type = VAR_JOB;
1722 tv->vval.v_job = iptr->isn_arg.job;
1723 if (tv->vval.v_job != NULL)
1724 ++tv->vval.v_job->jv_refcount;
1725#endif
1726 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 default:
1728 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001729 tv->vval.v_string = vim_strsave(
1730 iptr->isn_arg.string == NULL
1731 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732 }
1733 break;
1734
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001735 case ISN_UNLET:
1736 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1737 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001738 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001739 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001740 case ISN_UNLETENV:
1741 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1742 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001743
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001744 case ISN_LOCKCONST:
1745 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1746 break;
1747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 // create a list from items on the stack; uses a single allocation
1749 // for the list header and the items
1750 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001751 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1752 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 break;
1754
1755 // create a dict from items on the stack
1756 case ISN_NEWDICT:
1757 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001758 int count = iptr->isn_arg.number;
1759 dict_T *dict = dict_alloc();
1760 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001761 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762
1763 if (dict == NULL)
1764 goto failed;
1765 for (idx = 0; idx < count; ++idx)
1766 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001767 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001769 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001770 key = tv->vval.v_string == NULL
1771 ? (char_u *)"" : tv->vval.v_string;
1772 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02001773 if (item != NULL)
1774 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001775 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001776 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02001777 dict_unref(dict);
1778 goto on_error;
1779 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001780 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 clear_tv(tv);
1782 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001783 {
1784 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1788 item->di_tv.v_lock = 0;
1789 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001790 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001791 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001792 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001794 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 }
1796
1797 if (count > 0)
1798 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001799 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800 goto failed;
1801 else
1802 ++ectx.ec_stack.ga_len;
1803 tv = STACK_TV_BOT(-1);
1804 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001805 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 tv->vval.v_dict = dict;
1807 ++dict->dv_refcount;
1808 }
1809 break;
1810
1811 // call a :def function
1812 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001813 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1815 iptr->isn_arg.dfunc.cdf_argcount,
1816 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001817 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 break;
1819
1820 // call a builtin function
1821 case ISN_BCALL:
1822 SOURCING_LNUM = iptr->isn_lnum;
1823 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1824 iptr->isn_arg.bfunc.cbf_argcount,
1825 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001826 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827 break;
1828
1829 // call a funcref or partial
1830 case ISN_PCALL:
1831 {
1832 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1833 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001834 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835
1836 SOURCING_LNUM = iptr->isn_lnum;
1837 if (pfunc->cpf_top)
1838 {
1839 // funcref is above the arguments
1840 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1841 }
1842 else
1843 {
1844 // Get the funcref from the stack.
1845 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001846 partial_tv = *STACK_TV_BOT(0);
1847 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 }
1849 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001850 if (tv == &partial_tv)
1851 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001853 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 }
1855 break;
1856
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001857 case ISN_PCALL_END:
1858 // PCALL finished, arguments have been consumed and replaced by
1859 // the return value. Now clear the funcref from the stack,
1860 // and move the return value in its place.
1861 --ectx.ec_stack.ga_len;
1862 clear_tv(STACK_TV_BOT(-1));
1863 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1864 break;
1865
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866 // call a user defined function or funcref/partial
1867 case ISN_UCALL:
1868 {
1869 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1870
1871 SOURCING_LNUM = iptr->isn_lnum;
1872 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001873 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001874 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 }
1876 break;
1877
1878 // return from a :def function call
1879 case ISN_RETURN:
1880 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001881 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001882 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001883
1884 if (trystack->ga_len > 0)
1885 trycmd = ((trycmd_T *)trystack->ga_data)
1886 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001887 if (trycmd != NULL
1888 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 && trycmd->tcd_finally_idx != 0)
1890 {
1891 // jump to ":finally"
1892 ectx.ec_iidx = trycmd->tcd_finally_idx;
1893 trycmd->tcd_return = TRUE;
1894 }
1895 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001896 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001897 }
1898 break;
1899
1900 // push a function reference to a compiled function
1901 case ISN_FUNCREF:
1902 {
1903 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001904 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905
1906 pt = ALLOC_CLEAR_ONE(partial_T);
1907 if (pt == NULL)
1908 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001909 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001910 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001911 vim_free(pt);
1912 goto failed;
1913 }
1914 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1915 + iptr->isn_arg.funcref.fr_func;
1916 pt->pt_func = pt_dfunc->df_ufunc;
1917 pt->pt_refcount = 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001918
1919 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1920 {
1921 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1922 + ectx.ec_dfunc_idx;
1923
1924 // The closure needs to find arguments and local
1925 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001926 pt->pt_ectx_stack = &ectx.ec_stack;
1927 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001928
1929 // If this function returns and the closure is still
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001930 // being used, we need to make a copy of the context
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001931 // (arguments and local variables). Store a reference
1932 // to the partial so we can handle that.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001933 if (ga_grow(&ectx.ec_funcrefs, 1) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001934 {
Bram Moolenaardec07512020-09-18 23:11:10 +02001935 vim_free(pt);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001936 goto failed;
1937 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001938 // Extra variable keeps the count of closures created
1939 // in the current function call.
1940 tv = STACK_TV_VAR(dfunc->df_varcount);
1941 ++tv->vval.v_number;
1942
1943 ((partial_T **)ectx.ec_funcrefs.ga_data)
1944 [ectx.ec_funcrefs.ga_len] = pt;
1945 ++pt->pt_refcount;
1946 ++ectx.ec_funcrefs.ga_len;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001947 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001948 ++pt_dfunc->df_ufunc->uf_refcount;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950 tv = STACK_TV_BOT(0);
1951 ++ectx.ec_stack.ga_len;
1952 tv->vval.v_partial = pt;
1953 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001954 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 }
1956 break;
1957
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001958 // Create a global function from a lambda.
1959 case ISN_NEWFUNC:
1960 {
1961 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1962
1963 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1964 }
1965 break;
1966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 // jump if a condition is met
1968 case ISN_JUMP:
1969 {
1970 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001971 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 int jump = TRUE;
1973
1974 if (when != JUMP_ALWAYS)
1975 {
1976 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001977 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02001978 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001979 || when == JUMP_IF_COND_TRUE)
1980 {
1981 SOURCING_LNUM = iptr->isn_lnum;
1982 jump = tv_get_bool_chk(tv, &error);
1983 if (error)
1984 goto on_error;
1985 }
1986 else
1987 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001989 || when == JUMP_AND_KEEP_IF_FALSE
1990 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001992 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 {
1994 // drop the value from the stack
1995 clear_tv(tv);
1996 --ectx.ec_stack.ga_len;
1997 }
1998 }
1999 if (jump)
2000 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2001 }
2002 break;
2003
2004 // top of a for loop
2005 case ISN_FOR:
2006 {
2007 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2008 typval_T *idxtv =
2009 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2010
2011 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002012 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002014 ++idxtv->vval.v_number;
2015 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 // past the end of the list, jump to "endfor"
2017 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2018 else if (list->lv_first == &range_list_item)
2019 {
2020 // non-materialized range() list
2021 tv = STACK_TV_BOT(0);
2022 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002023 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 tv->vval.v_number = list_find_nr(
2025 list, idxtv->vval.v_number, NULL);
2026 ++ectx.ec_stack.ga_len;
2027 }
2028 else
2029 {
2030 listitem_T *li = list_find(list, idxtv->vval.v_number);
2031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2033 ++ectx.ec_stack.ga_len;
2034 }
2035 }
2036 break;
2037
2038 // start of ":try" block
2039 case ISN_TRY:
2040 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002041 trycmd_T *trycmd = NULL;
2042
Bram Moolenaar270d0382020-05-15 21:42:53 +02002043 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 goto failed;
2045 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2046 + ectx.ec_trystack.ga_len;
2047 ++ectx.ec_trystack.ga_len;
2048 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002049 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2051 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002052 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002053 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054 }
2055 break;
2056
2057 case ISN_PUSHEXC:
2058 if (current_exception == NULL)
2059 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002060 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002061 iemsg("Evaluating catch while current_exception is NULL");
2062 goto failed;
2063 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002064 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 goto failed;
2066 tv = STACK_TV_BOT(0);
2067 ++ectx.ec_stack.ga_len;
2068 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002069 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 tv->vval.v_string = vim_strsave(
2071 (char_u *)current_exception->value);
2072 break;
2073
2074 case ISN_CATCH:
2075 {
2076 garray_T *trystack = &ectx.ec_trystack;
2077
2078 if (trystack->ga_len > 0)
2079 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002080 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081 + trystack->ga_len - 1;
2082 trycmd->tcd_caught = TRUE;
2083 }
2084 did_emsg = got_int = did_throw = FALSE;
2085 catch_exception(current_exception);
2086 }
2087 break;
2088
2089 // end of ":try" block
2090 case ISN_ENDTRY:
2091 {
2092 garray_T *trystack = &ectx.ec_trystack;
2093
2094 if (trystack->ga_len > 0)
2095 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002096 trycmd_T *trycmd = NULL;
2097
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 --trystack->ga_len;
2099 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002100 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101 trycmd = ((trycmd_T *)trystack->ga_data)
2102 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002103 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 {
2105 // discard the exception
2106 if (caught_stack == current_exception)
2107 caught_stack = caught_stack->caught;
2108 discard_current_exception();
2109 }
2110
2111 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002112 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 }
2114 }
2115 break;
2116
2117 case ISN_THROW:
2118 --ectx.ec_stack.ga_len;
2119 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002120 if (tv->vval.v_string == NULL
2121 || *skipwhite(tv->vval.v_string) == NUL)
2122 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002123 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002124 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002125 emsg(_(e_throw_with_empty_string));
2126 goto failed;
2127 }
2128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2130 {
2131 vim_free(tv->vval.v_string);
2132 goto failed;
2133 }
2134 did_throw = TRUE;
2135 break;
2136
2137 // compare with special values
2138 case ISN_COMPAREBOOL:
2139 case ISN_COMPARESPECIAL:
2140 {
2141 typval_T *tv1 = STACK_TV_BOT(-2);
2142 typval_T *tv2 = STACK_TV_BOT(-1);
2143 varnumber_T arg1 = tv1->vval.v_number;
2144 varnumber_T arg2 = tv2->vval.v_number;
2145 int res;
2146
2147 switch (iptr->isn_arg.op.op_type)
2148 {
2149 case EXPR_EQUAL: res = arg1 == arg2; break;
2150 case EXPR_NEQUAL: res = arg1 != arg2; break;
2151 default: res = 0; break;
2152 }
2153
2154 --ectx.ec_stack.ga_len;
2155 tv1->v_type = VAR_BOOL;
2156 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2157 }
2158 break;
2159
2160 // Operation with two number arguments
2161 case ISN_OPNR:
2162 case ISN_COMPARENR:
2163 {
2164 typval_T *tv1 = STACK_TV_BOT(-2);
2165 typval_T *tv2 = STACK_TV_BOT(-1);
2166 varnumber_T arg1 = tv1->vval.v_number;
2167 varnumber_T arg2 = tv2->vval.v_number;
2168 varnumber_T res;
2169
2170 switch (iptr->isn_arg.op.op_type)
2171 {
2172 case EXPR_MULT: res = arg1 * arg2; break;
2173 case EXPR_DIV: res = arg1 / arg2; break;
2174 case EXPR_REM: res = arg1 % arg2; break;
2175 case EXPR_SUB: res = arg1 - arg2; break;
2176 case EXPR_ADD: res = arg1 + arg2; break;
2177
2178 case EXPR_EQUAL: res = arg1 == arg2; break;
2179 case EXPR_NEQUAL: res = arg1 != arg2; break;
2180 case EXPR_GREATER: res = arg1 > arg2; break;
2181 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2182 case EXPR_SMALLER: res = arg1 < arg2; break;
2183 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2184 default: res = 0; break;
2185 }
2186
2187 --ectx.ec_stack.ga_len;
2188 if (iptr->isn_type == ISN_COMPARENR)
2189 {
2190 tv1->v_type = VAR_BOOL;
2191 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2192 }
2193 else
2194 tv1->vval.v_number = res;
2195 }
2196 break;
2197
2198 // Computation with two float arguments
2199 case ISN_OPFLOAT:
2200 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002201#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 {
2203 typval_T *tv1 = STACK_TV_BOT(-2);
2204 typval_T *tv2 = STACK_TV_BOT(-1);
2205 float_T arg1 = tv1->vval.v_float;
2206 float_T arg2 = tv2->vval.v_float;
2207 float_T res = 0;
2208 int cmp = FALSE;
2209
2210 switch (iptr->isn_arg.op.op_type)
2211 {
2212 case EXPR_MULT: res = arg1 * arg2; break;
2213 case EXPR_DIV: res = arg1 / arg2; break;
2214 case EXPR_SUB: res = arg1 - arg2; break;
2215 case EXPR_ADD: res = arg1 + arg2; break;
2216
2217 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2218 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2219 case EXPR_GREATER: cmp = arg1 > arg2; break;
2220 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2221 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2222 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2223 default: cmp = 0; break;
2224 }
2225 --ectx.ec_stack.ga_len;
2226 if (iptr->isn_type == ISN_COMPAREFLOAT)
2227 {
2228 tv1->v_type = VAR_BOOL;
2229 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2230 }
2231 else
2232 tv1->vval.v_float = res;
2233 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002234#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 break;
2236
2237 case ISN_COMPARELIST:
2238 {
2239 typval_T *tv1 = STACK_TV_BOT(-2);
2240 typval_T *tv2 = STACK_TV_BOT(-1);
2241 list_T *arg1 = tv1->vval.v_list;
2242 list_T *arg2 = tv2->vval.v_list;
2243 int cmp = FALSE;
2244 int ic = iptr->isn_arg.op.op_ic;
2245
2246 switch (iptr->isn_arg.op.op_type)
2247 {
2248 case EXPR_EQUAL: cmp =
2249 list_equal(arg1, arg2, ic, FALSE); break;
2250 case EXPR_NEQUAL: cmp =
2251 !list_equal(arg1, arg2, ic, FALSE); break;
2252 case EXPR_IS: cmp = arg1 == arg2; break;
2253 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2254 default: cmp = 0; break;
2255 }
2256 --ectx.ec_stack.ga_len;
2257 clear_tv(tv1);
2258 clear_tv(tv2);
2259 tv1->v_type = VAR_BOOL;
2260 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2261 }
2262 break;
2263
2264 case ISN_COMPAREBLOB:
2265 {
2266 typval_T *tv1 = STACK_TV_BOT(-2);
2267 typval_T *tv2 = STACK_TV_BOT(-1);
2268 blob_T *arg1 = tv1->vval.v_blob;
2269 blob_T *arg2 = tv2->vval.v_blob;
2270 int cmp = FALSE;
2271
2272 switch (iptr->isn_arg.op.op_type)
2273 {
2274 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2275 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2276 case EXPR_IS: cmp = arg1 == arg2; break;
2277 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2278 default: cmp = 0; break;
2279 }
2280 --ectx.ec_stack.ga_len;
2281 clear_tv(tv1);
2282 clear_tv(tv2);
2283 tv1->v_type = VAR_BOOL;
2284 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2285 }
2286 break;
2287
2288 // TODO: handle separately
2289 case ISN_COMPARESTRING:
2290 case ISN_COMPAREDICT:
2291 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 case ISN_COMPAREANY:
2293 {
2294 typval_T *tv1 = STACK_TV_BOT(-2);
2295 typval_T *tv2 = STACK_TV_BOT(-1);
2296 exptype_T exptype = iptr->isn_arg.op.op_type;
2297 int ic = iptr->isn_arg.op.op_ic;
2298
Bram Moolenaareb26f432020-09-14 16:50:05 +02002299 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 typval_compare(tv1, tv2, exptype, ic);
2301 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302 --ectx.ec_stack.ga_len;
2303 }
2304 break;
2305
2306 case ISN_ADDLIST:
2307 case ISN_ADDBLOB:
2308 {
2309 typval_T *tv1 = STACK_TV_BOT(-2);
2310 typval_T *tv2 = STACK_TV_BOT(-1);
2311
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002312 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 if (iptr->isn_type == ISN_ADDLIST)
2314 eval_addlist(tv1, tv2);
2315 else
2316 eval_addblob(tv1, tv2);
2317 clear_tv(tv2);
2318 --ectx.ec_stack.ga_len;
2319 }
2320 break;
2321
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002322 case ISN_LISTAPPEND:
2323 {
2324 typval_T *tv1 = STACK_TV_BOT(-2);
2325 typval_T *tv2 = STACK_TV_BOT(-1);
2326 list_T *l = tv1->vval.v_list;
2327
2328 // add an item to a list
2329 if (l == NULL)
2330 {
2331 SOURCING_LNUM = iptr->isn_lnum;
2332 emsg(_(e_cannot_add_to_null_list));
2333 goto on_error;
2334 }
2335 if (list_append_tv(l, tv2) == FAIL)
2336 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002337 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002338 --ectx.ec_stack.ga_len;
2339 }
2340 break;
2341
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002342 case ISN_BLOBAPPEND:
2343 {
2344 typval_T *tv1 = STACK_TV_BOT(-2);
2345 typval_T *tv2 = STACK_TV_BOT(-1);
2346 blob_T *b = tv1->vval.v_blob;
2347 int error = FALSE;
2348 varnumber_T n;
2349
2350 // add a number to a blob
2351 if (b == NULL)
2352 {
2353 SOURCING_LNUM = iptr->isn_lnum;
2354 emsg(_(e_cannot_add_to_null_blob));
2355 goto on_error;
2356 }
2357 n = tv_get_number_chk(tv2, &error);
2358 if (error)
2359 goto on_error;
2360 ga_append(&b->bv_ga, (int)n);
2361 --ectx.ec_stack.ga_len;
2362 }
2363 break;
2364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 // Computation with two arguments of unknown type
2366 case ISN_OPANY:
2367 {
2368 typval_T *tv1 = STACK_TV_BOT(-2);
2369 typval_T *tv2 = STACK_TV_BOT(-1);
2370 varnumber_T n1, n2;
2371#ifdef FEAT_FLOAT
2372 float_T f1 = 0, f2 = 0;
2373#endif
2374 int error = FALSE;
2375
2376 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2377 {
2378 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2379 {
2380 eval_addlist(tv1, tv2);
2381 clear_tv(tv2);
2382 --ectx.ec_stack.ga_len;
2383 break;
2384 }
2385 else if (tv1->v_type == VAR_BLOB
2386 && tv2->v_type == VAR_BLOB)
2387 {
2388 eval_addblob(tv1, tv2);
2389 clear_tv(tv2);
2390 --ectx.ec_stack.ga_len;
2391 break;
2392 }
2393 }
2394#ifdef FEAT_FLOAT
2395 if (tv1->v_type == VAR_FLOAT)
2396 {
2397 f1 = tv1->vval.v_float;
2398 n1 = 0;
2399 }
2400 else
2401#endif
2402 {
2403 n1 = tv_get_number_chk(tv1, &error);
2404 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002405 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406#ifdef FEAT_FLOAT
2407 if (tv2->v_type == VAR_FLOAT)
2408 f1 = n1;
2409#endif
2410 }
2411#ifdef FEAT_FLOAT
2412 if (tv2->v_type == VAR_FLOAT)
2413 {
2414 f2 = tv2->vval.v_float;
2415 n2 = 0;
2416 }
2417 else
2418#endif
2419 {
2420 n2 = tv_get_number_chk(tv2, &error);
2421 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002422 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423#ifdef FEAT_FLOAT
2424 if (tv1->v_type == VAR_FLOAT)
2425 f2 = n2;
2426#endif
2427 }
2428#ifdef FEAT_FLOAT
2429 // if there is a float on either side the result is a float
2430 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2431 {
2432 switch (iptr->isn_arg.op.op_type)
2433 {
2434 case EXPR_MULT: f1 = f1 * f2; break;
2435 case EXPR_DIV: f1 = f1 / f2; break;
2436 case EXPR_SUB: f1 = f1 - f2; break;
2437 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002438 default: SOURCING_LNUM = iptr->isn_lnum;
2439 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002440 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441 }
2442 clear_tv(tv1);
2443 clear_tv(tv2);
2444 tv1->v_type = VAR_FLOAT;
2445 tv1->vval.v_float = f1;
2446 --ectx.ec_stack.ga_len;
2447 }
2448 else
2449#endif
2450 {
2451 switch (iptr->isn_arg.op.op_type)
2452 {
2453 case EXPR_MULT: n1 = n1 * n2; break;
2454 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2455 case EXPR_SUB: n1 = n1 - n2; break;
2456 case EXPR_ADD: n1 = n1 + n2; break;
2457 default: n1 = num_modulus(n1, n2); break;
2458 }
2459 clear_tv(tv1);
2460 clear_tv(tv2);
2461 tv1->v_type = VAR_NUMBER;
2462 tv1->vval.v_number = n1;
2463 --ectx.ec_stack.ga_len;
2464 }
2465 }
2466 break;
2467
2468 case ISN_CONCAT:
2469 {
2470 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2471 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2472 char_u *res;
2473
2474 res = concat_str(str1, str2);
2475 clear_tv(STACK_TV_BOT(-2));
2476 clear_tv(STACK_TV_BOT(-1));
2477 --ectx.ec_stack.ga_len;
2478 STACK_TV_BOT(-1)->vval.v_string = res;
2479 }
2480 break;
2481
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002482 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002483 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002484 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002485 int is_slice = iptr->isn_type == ISN_STRSLICE;
2486 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002487 char_u *res;
2488
2489 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002490 // string slice: string is at stack-3, first index at
2491 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002492 if (is_slice)
2493 {
2494 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002495 n1 = tv->vval.v_number;
2496 }
2497
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002498 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002499 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002500
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002501 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002502 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002503 if (is_slice)
2504 // Slice: Select the characters from the string
2505 res = string_slice(tv->vval.v_string, n1, n2);
2506 else
2507 // Index: The resulting variable is a string of a
2508 // single character. If the index is too big or
2509 // negative the result is empty.
2510 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002511 vim_free(tv->vval.v_string);
2512 tv->vval.v_string = res;
2513 }
2514 break;
2515
2516 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002517 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 {
Bram Moolenaared591872020-08-15 22:14:53 +02002519 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002521 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522
2523 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002524 // list slice: list is at stack-3, indexes at stack-2 and
2525 // stack-1
2526 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 list = tv->vval.v_list;
2528
2529 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002530 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002532
2533 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 {
Bram Moolenaared591872020-08-15 22:14:53 +02002535 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002536 n1 = tv->vval.v_number;
2537 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 }
Bram Moolenaared591872020-08-15 22:14:53 +02002539
2540 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002541 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002542 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002543 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2544 == FAIL)
2545 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 }
2547 break;
2548
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002549 case ISN_ANYINDEX:
2550 case ISN_ANYSLICE:
2551 {
2552 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2553 typval_T *var1, *var2;
2554 int res;
2555
2556 // index: composite is at stack-2, index at stack-1
2557 // slice: composite is at stack-3, indexes at stack-2 and
2558 // stack-1
2559 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002560 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002561 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2562 goto on_error;
2563 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2564 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2565 res = eval_index_inner(tv, is_slice,
2566 var1, var2, NULL, -1, TRUE);
2567 clear_tv(var1);
2568 if (is_slice)
2569 clear_tv(var2);
2570 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2571 if (res == FAIL)
2572 goto on_error;
2573 }
2574 break;
2575
Bram Moolenaar9af78762020-06-16 11:34:42 +02002576 case ISN_SLICE:
2577 {
2578 list_T *list;
2579 int count = iptr->isn_arg.number;
2580
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002581 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002582 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002583 list = tv->vval.v_list;
2584
2585 // no error for short list, expect it to be checked earlier
2586 if (list != NULL && list->lv_len >= count)
2587 {
2588 list_T *newlist = list_slice(list,
2589 count, list->lv_len - 1);
2590
2591 if (newlist != NULL)
2592 {
2593 list_unref(list);
2594 tv->vval.v_list = newlist;
2595 ++newlist->lv_refcount;
2596 }
2597 }
2598 }
2599 break;
2600
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002601 case ISN_GETITEM:
2602 {
2603 listitem_T *li;
2604 int index = iptr->isn_arg.number;
2605
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002606 // Get list item: list is at stack-1, push item.
2607 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002608 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002609 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002610
2611 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2612 goto failed;
2613 ++ectx.ec_stack.ga_len;
2614 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2615 }
2616 break;
2617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 case ISN_MEMBER:
2619 {
2620 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002621 char_u *key;
2622 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002623 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002624
2625 // dict member: dict is at stack-2, key at stack-1
2626 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002627 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002628 dict = tv->vval.v_dict;
2629
2630 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002631 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002632 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01002633 if (key == NULL)
2634 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002635
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002636 if ((di = dict_find(dict, key, -1)) == NULL)
2637 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002638 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002639 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002640 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002641 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002642 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002643 --ectx.ec_stack.ga_len;
2644 // Clear the dict after getting the item, to avoid that it
2645 // make the item invalid.
2646 tv = STACK_TV_BOT(-1);
2647 temp_tv = *tv;
2648 copy_tv(&di->di_tv, tv);
2649 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002650 }
2651 break;
2652
2653 // dict member with string key
2654 case ISN_STRINGMEMBER:
2655 {
2656 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002658 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659
2660 tv = STACK_TV_BOT(-1);
2661 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2662 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002663 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002665 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 }
2667 dict = tv->vval.v_dict;
2668
2669 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2670 == NULL)
2671 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002672 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002674 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002676 // Clear the dict after getting the item, to avoid that it
2677 // make the item invalid.
2678 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002680 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 }
2682 break;
2683
2684 case ISN_NEGATENR:
2685 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002686 if (tv->v_type != VAR_NUMBER
2687#ifdef FEAT_FLOAT
2688 && tv->v_type != VAR_FLOAT
2689#endif
2690 )
2691 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002692 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002693 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002694 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002695 }
2696#ifdef FEAT_FLOAT
2697 if (tv->v_type == VAR_FLOAT)
2698 tv->vval.v_float = -tv->vval.v_float;
2699 else
2700#endif
2701 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 break;
2703
2704 case ISN_CHECKNR:
2705 {
2706 int error = FALSE;
2707
2708 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002709 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002711 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712 (void)tv_get_number_chk(tv, &error);
2713 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002714 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 }
2716 break;
2717
2718 case ISN_CHECKTYPE:
2719 {
2720 checktype_T *ct = &iptr->isn_arg.type;
2721
2722 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002723 SOURCING_LNUM = iptr->isn_lnum;
2724 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2725 goto on_error;
2726
2727 // number 0 is FALSE, number 1 is TRUE
2728 if (tv->v_type == VAR_NUMBER
2729 && ct->ct_type->tt_type == VAR_BOOL
2730 && (tv->vval.v_number == 0
2731 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002733 tv->v_type = VAR_BOOL;
2734 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002735 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 }
2737 }
2738 break;
2739
Bram Moolenaar9af78762020-06-16 11:34:42 +02002740 case ISN_CHECKLEN:
2741 {
2742 int min_len = iptr->isn_arg.checklen.cl_min_len;
2743 list_T *list = NULL;
2744
2745 tv = STACK_TV_BOT(-1);
2746 if (tv->v_type == VAR_LIST)
2747 list = tv->vval.v_list;
2748 if (list == NULL || list->lv_len < min_len
2749 || (list->lv_len > min_len
2750 && !iptr->isn_arg.checklen.cl_more_OK))
2751 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002752 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002753 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002754 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002755 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002756 }
2757 }
2758 break;
2759
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002761 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 {
2763 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002764 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765
2766 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002767 if (iptr->isn_type == ISN_2BOOL)
2768 {
2769 n = tv2bool(tv);
2770 if (iptr->isn_arg.number) // invert
2771 n = !n;
2772 }
2773 else
2774 {
2775 SOURCING_LNUM = iptr->isn_lnum;
2776 n = tv_get_bool_chk(tv, &error);
2777 if (error)
2778 goto on_error;
2779 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 clear_tv(tv);
2781 tv->v_type = VAR_BOOL;
2782 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2783 }
2784 break;
2785
2786 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002787 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 {
2789 char_u *str;
2790
2791 tv = STACK_TV_BOT(iptr->isn_arg.number);
2792 if (tv->v_type != VAR_STRING)
2793 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002794 if (iptr->isn_type == ISN_2STRING_ANY)
2795 {
2796 switch (tv->v_type)
2797 {
2798 case VAR_SPECIAL:
2799 case VAR_BOOL:
2800 case VAR_NUMBER:
2801 case VAR_FLOAT:
2802 case VAR_BLOB: break;
2803 default: to_string_error(tv->v_type);
2804 goto on_error;
2805 }
2806 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 str = typval_tostring(tv);
2808 clear_tv(tv);
2809 tv->v_type = VAR_STRING;
2810 tv->vval.v_string = str;
2811 }
2812 }
2813 break;
2814
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002815 case ISN_PUT:
2816 {
2817 int regname = iptr->isn_arg.put.put_regname;
2818 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2819 char_u *expr = NULL;
2820 int dir = FORWARD;
2821
2822 if (regname == '=')
2823 {
2824 tv = STACK_TV_BOT(-1);
2825 if (tv->v_type == VAR_STRING)
2826 expr = tv->vval.v_string;
2827 else
2828 {
2829 expr = typval_tostring(tv); // allocates value
2830 clear_tv(tv);
2831 }
2832 --ectx.ec_stack.ga_len;
2833 }
2834 if (lnum == -2)
2835 // :put! above cursor
2836 dir = BACKWARD;
2837 else if (lnum >= 0)
2838 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2839 check_cursor();
2840 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2841 vim_free(expr);
2842 }
2843 break;
2844
Bram Moolenaar02194d22020-10-24 23:08:38 +02002845 case ISN_CMDMOD:
2846 save_cmdmod = cmdmod;
2847 restore_cmdmod = TRUE;
2848 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
2849 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002850 break;
2851
Bram Moolenaar02194d22020-10-24 23:08:38 +02002852 case ISN_CMDMOD_REV:
2853 // filter regprog is owned by the instruction, don't free it
2854 cmdmod.cmod_filter_regmatch.regprog = NULL;
2855 undo_cmdmod(&cmdmod);
2856 cmdmod = save_cmdmod;
2857 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002858 break;
2859
Bram Moolenaar389df252020-07-09 21:20:47 +02002860 case ISN_SHUFFLE:
2861 {
2862 typval_T tmp_tv;
2863 int item = iptr->isn_arg.shuffle.shfl_item;
2864 int up = iptr->isn_arg.shuffle.shfl_up;
2865
2866 tmp_tv = *STACK_TV_BOT(-item);
2867 for ( ; up > 0 && item > 1; --up)
2868 {
2869 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2870 --item;
2871 }
2872 *STACK_TV_BOT(-item) = tmp_tv;
2873 }
2874 break;
2875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 case ISN_DROP:
2877 --ectx.ec_stack.ga_len;
2878 clear_tv(STACK_TV_BOT(0));
2879 break;
2880 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002881 continue;
2882
Bram Moolenaard032f342020-07-18 18:13:02 +02002883func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002884 // Restore previous function. If the frame pointer is where we started
2885 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02002886 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02002887 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002888
Bram Moolenaard032f342020-07-18 18:13:02 +02002889 if (func_return(&ectx) == FAIL)
2890 // only fails when out of memory
2891 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002892 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002893
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002894on_error:
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002895 // If "emsg_silent" is set then ignore the error.
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002896 if (did_emsg_cumul + did_emsg == did_emsg_before && emsg_silent)
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002897 continue;
2898
Bram Moolenaar171fb922020-10-28 16:54:47 +01002899 // If we are not inside a try-catch started here, abort execution.
2900 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002901 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 }
2903
2904done:
2905 // function finished, get result from the stack.
2906 tv = STACK_TV_BOT(-1);
2907 *rettv = *tv;
2908 tv->v_type = VAR_UNKNOWN;
2909 ret = OK;
2910
2911failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002912 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002913 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002914 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002915
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002916 // Deal with any remaining closures, they may be in use somewhere.
2917 if (ectx.ec_funcrefs.ga_len > 0)
2918 handle_closure_in_use(&ectx, FALSE);
2919
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002920 estack_pop();
2921 current_sctx = save_current_sctx;
2922
Bram Moolenaar352134b2020-10-17 22:04:08 +02002923 if (*msg_list != NULL && saved_msg_list != NULL)
2924 {
2925 msglist_T **plist = saved_msg_list;
2926
2927 // Append entries from the current msg_list (uncaught exceptions) to
2928 // the saved msg_list.
2929 while (*plist != NULL)
2930 plist = &(*plist)->next;
2931
2932 *plist = *msg_list;
2933 }
2934 msg_list = saved_msg_list;
2935
Bram Moolenaar02194d22020-10-24 23:08:38 +02002936 if (restore_cmdmod)
2937 {
2938 cmdmod.cmod_filter_regmatch.regprog = NULL;
2939 undo_cmdmod(&cmdmod);
2940 cmdmod = save_cmdmod;
2941 }
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002942
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002943failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002944 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2946 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002949 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002950
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002951 // Not sure if this is necessary.
2952 suppress_errthrow = save_suppress_errthrow;
2953
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002954 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002955 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002956 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002957 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 return ret;
2959}
2960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961/*
2962 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002963 * We don't really need this at runtime, but we do have tests that require it,
2964 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 */
2966 void
2967ex_disassemble(exarg_T *eap)
2968{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002969 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002970 char_u *fname;
2971 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 dfunc_T *dfunc;
2973 isn_T *instr;
2974 int current;
2975 int line_idx = 0;
2976 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002977 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002979 if (STRNCMP(arg, "<lambda>", 8) == 0)
2980 {
2981 arg += 8;
2982 (void)getdigits(&arg);
2983 fname = vim_strnsave(eap->arg, arg - eap->arg);
2984 }
2985 else
2986 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002987 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002988 if (fname == NULL)
2989 {
2990 semsg(_(e_invarg2), eap->arg);
2991 return;
2992 }
2993
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002994 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002995 if (ufunc == NULL)
2996 {
2997 char_u *p = untrans_function_name(fname);
2998
2999 if (p != NULL)
3000 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003001 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003002 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003003 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 if (ufunc == NULL)
3005 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003006 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 return;
3008 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003009 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003010 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3011 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003012 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003014 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 return;
3016 }
3017 if (ufunc->uf_name_exp != NULL)
3018 msg((char *)ufunc->uf_name_exp);
3019 else
3020 msg((char *)ufunc->uf_name);
3021
3022 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3023 instr = dfunc->df_instr;
3024 for (current = 0; current < dfunc->df_instr_count; ++current)
3025 {
3026 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003027 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028
3029 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3030 {
3031 if (current > prev_current)
3032 {
3033 msg_puts("\n\n");
3034 prev_current = current;
3035 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003036 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3037 if (line != NULL)
3038 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 }
3040
3041 switch (iptr->isn_type)
3042 {
3043 case ISN_EXEC:
3044 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3045 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003046 case ISN_EXECCONCAT:
3047 smsg("%4d EXECCONCAT %lld", current,
3048 (long long)iptr->isn_arg.number);
3049 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 case ISN_ECHO:
3051 {
3052 echo_T *echo = &iptr->isn_arg.echo;
3053
3054 smsg("%4d %s %d", current,
3055 echo->echo_with_white ? "ECHO" : "ECHON",
3056 echo->echo_count);
3057 }
3058 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003059 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003060 smsg("%4d EXECUTE %lld", current,
3061 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003062 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003063 case ISN_ECHOMSG:
3064 smsg("%4d ECHOMSG %lld", current,
3065 (long long)(iptr->isn_arg.number));
3066 break;
3067 case ISN_ECHOERR:
3068 smsg("%4d ECHOERR %lld", current,
3069 (long long)(iptr->isn_arg.number));
3070 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003072 case ISN_LOADOUTER:
3073 {
3074 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
3075
3076 if (iptr->isn_arg.number < 0)
3077 smsg("%4d LOAD%s arg[%lld]", current, add,
3078 (long long)(iptr->isn_arg.number
3079 + STACK_FRAME_SIZE));
3080 else
3081 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003082 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003083 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 break;
3085 case ISN_LOADV:
3086 smsg("%4d LOADV v:%s", current,
3087 get_vim_var_name(iptr->isn_arg.number));
3088 break;
3089 case ISN_LOADSCRIPT:
3090 {
3091 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003092 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3094 + iptr->isn_arg.script.script_idx;
3095
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003096 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3097 sv->sv_name,
3098 iptr->isn_arg.script.script_idx,
3099 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 }
3101 break;
3102 case ISN_LOADS:
3103 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003104 scriptitem_T *si = SCRIPT_ITEM(
3105 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106
3107 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003108 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 }
3110 break;
3111 case ISN_LOADG:
3112 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3113 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003114 case ISN_LOADB:
3115 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3116 break;
3117 case ISN_LOADW:
3118 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3119 break;
3120 case ISN_LOADT:
3121 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3122 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003123 case ISN_LOADGDICT:
3124 smsg("%4d LOAD g:", current);
3125 break;
3126 case ISN_LOADBDICT:
3127 smsg("%4d LOAD b:", current);
3128 break;
3129 case ISN_LOADWDICT:
3130 smsg("%4d LOAD w:", current);
3131 break;
3132 case ISN_LOADTDICT:
3133 smsg("%4d LOAD t:", current);
3134 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 case ISN_LOADOPT:
3136 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3137 break;
3138 case ISN_LOADENV:
3139 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3140 break;
3141 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003142 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 break;
3144
3145 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003146 case ISN_STOREOUTER:
3147 {
3148 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
3149
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003150 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003151 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003152 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003153 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003154 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003155 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003156 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003158 case ISN_STOREV:
3159 smsg("%4d STOREV v:%s", current,
3160 get_vim_var_name(iptr->isn_arg.number));
3161 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003163 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3164 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003165 case ISN_STOREB:
3166 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3167 break;
3168 case ISN_STOREW:
3169 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3170 break;
3171 case ISN_STORET:
3172 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3173 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003174 case ISN_STORES:
3175 {
3176 scriptitem_T *si = SCRIPT_ITEM(
3177 iptr->isn_arg.loadstore.ls_sid);
3178
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003179 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003180 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 break;
3183 case ISN_STORESCRIPT:
3184 {
3185 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003186 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3188 + iptr->isn_arg.script.script_idx;
3189
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003190 smsg("%4d STORESCRIPT %s-%d in %s", current,
3191 sv->sv_name,
3192 iptr->isn_arg.script.script_idx,
3193 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 }
3195 break;
3196 case ISN_STOREOPT:
3197 smsg("%4d STOREOPT &%s", current,
3198 iptr->isn_arg.storeopt.so_name);
3199 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003200 case ISN_STOREENV:
3201 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3202 break;
3203 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003204 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003205 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 case ISN_STORENR:
3207 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003208 iptr->isn_arg.storenr.stnr_val,
3209 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 break;
3211
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003212 case ISN_STORELIST:
3213 smsg("%4d STORELIST", current);
3214 break;
3215
3216 case ISN_STOREDICT:
3217 smsg("%4d STOREDICT", current);
3218 break;
3219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 // constants
3221 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003222 smsg("%4d PUSHNR %lld", current,
3223 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 break;
3225 case ISN_PUSHBOOL:
3226 case ISN_PUSHSPEC:
3227 smsg("%4d PUSH %s", current,
3228 get_var_special_name(iptr->isn_arg.number));
3229 break;
3230 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003231#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003233#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 break;
3235 case ISN_PUSHS:
3236 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3237 break;
3238 case ISN_PUSHBLOB:
3239 {
3240 char_u *r;
3241 char_u numbuf[NUMBUFLEN];
3242 char_u *tofree;
3243
3244 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003245 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 vim_free(tofree);
3247 }
3248 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003249 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003250 {
3251 char *name = (char *)iptr->isn_arg.string;
3252
3253 smsg("%4d PUSHFUNC \"%s\"", current,
3254 name == NULL ? "[none]" : name);
3255 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003256 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003257 case ISN_PUSHCHANNEL:
3258#ifdef FEAT_JOB_CHANNEL
3259 {
3260 channel_T *channel = iptr->isn_arg.channel;
3261
3262 smsg("%4d PUSHCHANNEL %d", current,
3263 channel == NULL ? 0 : channel->ch_id);
3264 }
3265#endif
3266 break;
3267 case ISN_PUSHJOB:
3268#ifdef FEAT_JOB_CHANNEL
3269 {
3270 typval_T tv;
3271 char_u *name;
3272
3273 tv.v_type = VAR_JOB;
3274 tv.vval.v_job = iptr->isn_arg.job;
3275 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003276 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003277 }
3278#endif
3279 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 case ISN_PUSHEXC:
3281 smsg("%4d PUSH v:exception", current);
3282 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003283 case ISN_UNLET:
3284 smsg("%4d UNLET%s %s", current,
3285 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3286 iptr->isn_arg.unlet.ul_name);
3287 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003288 case ISN_UNLETENV:
3289 smsg("%4d UNLETENV%s $%s", current,
3290 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3291 iptr->isn_arg.unlet.ul_name);
3292 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003293 case ISN_LOCKCONST:
3294 smsg("%4d LOCKCONST", current);
3295 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003297 smsg("%4d NEWLIST size %lld", current,
3298 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 break;
3300 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003301 smsg("%4d NEWDICT size %lld", current,
3302 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 break;
3304
3305 // function call
3306 case ISN_BCALL:
3307 {
3308 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3309
3310 smsg("%4d BCALL %s(argc %d)", current,
3311 internal_func_name(cbfunc->cbf_idx),
3312 cbfunc->cbf_argcount);
3313 }
3314 break;
3315 case ISN_DCALL:
3316 {
3317 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3318 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3319 + cdfunc->cdf_idx;
3320
3321 smsg("%4d DCALL %s(argc %d)", current,
3322 df->df_ufunc->uf_name_exp != NULL
3323 ? df->df_ufunc->uf_name_exp
3324 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3325 }
3326 break;
3327 case ISN_UCALL:
3328 {
3329 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3330
3331 smsg("%4d UCALL %s(argc %d)", current,
3332 cufunc->cuf_name, cufunc->cuf_argcount);
3333 }
3334 break;
3335 case ISN_PCALL:
3336 {
3337 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3338
3339 smsg("%4d PCALL%s (argc %d)", current,
3340 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3341 }
3342 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003343 case ISN_PCALL_END:
3344 smsg("%4d PCALL end", current);
3345 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 case ISN_RETURN:
3347 smsg("%4d RETURN", current);
3348 break;
3349 case ISN_FUNCREF:
3350 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003351 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003353 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003355 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 }
3357 break;
3358
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003359 case ISN_NEWFUNC:
3360 {
3361 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3362
3363 smsg("%4d NEWFUNC %s %s", current,
3364 newfunc->nf_lambda, newfunc->nf_global);
3365 }
3366 break;
3367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 case ISN_JUMP:
3369 {
3370 char *when = "?";
3371
3372 switch (iptr->isn_arg.jump.jump_when)
3373 {
3374 case JUMP_ALWAYS:
3375 when = "JUMP";
3376 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 case JUMP_AND_KEEP_IF_TRUE:
3378 when = "JUMP_AND_KEEP_IF_TRUE";
3379 break;
3380 case JUMP_IF_FALSE:
3381 when = "JUMP_IF_FALSE";
3382 break;
3383 case JUMP_AND_KEEP_IF_FALSE:
3384 when = "JUMP_AND_KEEP_IF_FALSE";
3385 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003386 case JUMP_IF_COND_FALSE:
3387 when = "JUMP_IF_COND_FALSE";
3388 break;
3389 case JUMP_IF_COND_TRUE:
3390 when = "JUMP_IF_COND_TRUE";
3391 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003393 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 iptr->isn_arg.jump.jump_where);
3395 }
3396 break;
3397
3398 case ISN_FOR:
3399 {
3400 forloop_T *forloop = &iptr->isn_arg.forloop;
3401
3402 smsg("%4d FOR $%d -> %d", current,
3403 forloop->for_idx, forloop->for_end);
3404 }
3405 break;
3406
3407 case ISN_TRY:
3408 {
3409 try_T *try = &iptr->isn_arg.try;
3410
3411 smsg("%4d TRY catch -> %d, finally -> %d", current,
3412 try->try_catch, try->try_finally);
3413 }
3414 break;
3415 case ISN_CATCH:
3416 // TODO
3417 smsg("%4d CATCH", current);
3418 break;
3419 case ISN_ENDTRY:
3420 smsg("%4d ENDTRY", current);
3421 break;
3422 case ISN_THROW:
3423 smsg("%4d THROW", current);
3424 break;
3425
3426 // expression operations on number
3427 case ISN_OPNR:
3428 case ISN_OPFLOAT:
3429 case ISN_OPANY:
3430 {
3431 char *what;
3432 char *ins;
3433
3434 switch (iptr->isn_arg.op.op_type)
3435 {
3436 case EXPR_MULT: what = "*"; break;
3437 case EXPR_DIV: what = "/"; break;
3438 case EXPR_REM: what = "%"; break;
3439 case EXPR_SUB: what = "-"; break;
3440 case EXPR_ADD: what = "+"; break;
3441 default: what = "???"; break;
3442 }
3443 switch (iptr->isn_type)
3444 {
3445 case ISN_OPNR: ins = "OPNR"; break;
3446 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3447 case ISN_OPANY: ins = "OPANY"; break;
3448 default: ins = "???"; break;
3449 }
3450 smsg("%4d %s %s", current, ins, what);
3451 }
3452 break;
3453
3454 case ISN_COMPAREBOOL:
3455 case ISN_COMPARESPECIAL:
3456 case ISN_COMPARENR:
3457 case ISN_COMPAREFLOAT:
3458 case ISN_COMPARESTRING:
3459 case ISN_COMPAREBLOB:
3460 case ISN_COMPARELIST:
3461 case ISN_COMPAREDICT:
3462 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 case ISN_COMPAREANY:
3464 {
3465 char *p;
3466 char buf[10];
3467 char *type;
3468
3469 switch (iptr->isn_arg.op.op_type)
3470 {
3471 case EXPR_EQUAL: p = "=="; break;
3472 case EXPR_NEQUAL: p = "!="; break;
3473 case EXPR_GREATER: p = ">"; break;
3474 case EXPR_GEQUAL: p = ">="; break;
3475 case EXPR_SMALLER: p = "<"; break;
3476 case EXPR_SEQUAL: p = "<="; break;
3477 case EXPR_MATCH: p = "=~"; break;
3478 case EXPR_IS: p = "is"; break;
3479 case EXPR_ISNOT: p = "isnot"; break;
3480 case EXPR_NOMATCH: p = "!~"; break;
3481 default: p = "???"; break;
3482 }
3483 STRCPY(buf, p);
3484 if (iptr->isn_arg.op.op_ic == TRUE)
3485 strcat(buf, "?");
3486 switch(iptr->isn_type)
3487 {
3488 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3489 case ISN_COMPARESPECIAL:
3490 type = "COMPARESPECIAL"; break;
3491 case ISN_COMPARENR: type = "COMPARENR"; break;
3492 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3493 case ISN_COMPARESTRING:
3494 type = "COMPARESTRING"; break;
3495 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3496 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3497 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3498 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3500 default: type = "???"; break;
3501 }
3502
3503 smsg("%4d %s %s", current, type, buf);
3504 }
3505 break;
3506
3507 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3508 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3509
3510 // expression operations
3511 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003512 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003513 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003514 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003515 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003516 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003517 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003518 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3519 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003520 case ISN_SLICE: smsg("%4d SLICE %lld",
3521 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003522 case ISN_GETITEM: smsg("%4d ITEM %lld",
3523 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003524 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3525 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 iptr->isn_arg.string); break;
3527 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3528
3529 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003530 case ISN_CHECKTYPE:
3531 {
3532 char *tofree;
3533
3534 smsg("%4d CHECKTYPE %s stack[%d]", current,
3535 type_name(iptr->isn_arg.type.ct_type, &tofree),
3536 iptr->isn_arg.type.ct_off);
3537 vim_free(tofree);
3538 break;
3539 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003540 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3541 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3542 iptr->isn_arg.checklen.cl_min_len);
3543 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003544 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 case ISN_2BOOL: if (iptr->isn_arg.number)
3546 smsg("%4d INVERT (!val)", current);
3547 else
3548 smsg("%4d 2BOOL (!!val)", current);
3549 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003550 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3551 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003552 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003553 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3554 (long long)(iptr->isn_arg.number));
3555 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003556 case ISN_PUT:
3557 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname,
3558 (long)iptr->isn_arg.put.put_lnum);
3559 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560
Bram Moolenaar02194d22020-10-24 23:08:38 +02003561 // TODO: summarize modifiers
3562 case ISN_CMDMOD:
3563 {
3564 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01003565 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02003566 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3567
3568 buf = alloc(len + 1);
3569 if (buf != NULL)
3570 {
3571 (void)produce_cmdmods(
3572 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3573 smsg("%4d CMDMOD %s", current, buf);
3574 vim_free(buf);
3575 }
3576 break;
3577 }
3578 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003579
Bram Moolenaar389df252020-07-09 21:20:47 +02003580 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3581 iptr->isn_arg.shuffle.shfl_item,
3582 iptr->isn_arg.shuffle.shfl_up);
3583 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 case ISN_DROP: smsg("%4d DROP", current); break;
3585 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003586
3587 out_flush(); // output one line at a time
3588 ui_breakcheck();
3589 if (got_int)
3590 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592}
3593
3594/*
Bram Moolenaar13106602020-10-04 16:06:05 +02003595 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 * list, etc. Mostly like what JavaScript does, except that empty list and
3597 * empty dictionary are FALSE.
3598 */
3599 int
3600tv2bool(typval_T *tv)
3601{
3602 switch (tv->v_type)
3603 {
3604 case VAR_NUMBER:
3605 return tv->vval.v_number != 0;
3606 case VAR_FLOAT:
3607#ifdef FEAT_FLOAT
3608 return tv->vval.v_float != 0.0;
3609#else
3610 break;
3611#endif
3612 case VAR_PARTIAL:
3613 return tv->vval.v_partial != NULL;
3614 case VAR_FUNC:
3615 case VAR_STRING:
3616 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3617 case VAR_LIST:
3618 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3619 case VAR_DICT:
3620 return tv->vval.v_dict != NULL
3621 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3622 case VAR_BOOL:
3623 case VAR_SPECIAL:
3624 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3625 case VAR_JOB:
3626#ifdef FEAT_JOB_CHANNEL
3627 return tv->vval.v_job != NULL;
3628#else
3629 break;
3630#endif
3631 case VAR_CHANNEL:
3632#ifdef FEAT_JOB_CHANNEL
3633 return tv->vval.v_channel != NULL;
3634#else
3635 break;
3636#endif
3637 case VAR_BLOB:
3638 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3639 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003640 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 case VAR_VOID:
3642 break;
3643 }
3644 return FALSE;
3645}
3646
Bram Moolenaarea2d4072020-11-12 12:08:51 +01003647 void
3648emsg_using_string_as(typval_T *tv, int as_number)
3649{
3650 semsg(_(as_number ? e_using_string_as_number_str
3651 : e_using_string_as_bool_str),
3652 tv->vval.v_string == NULL
3653 ? (char_u *)"" : tv->vval.v_string);
3654}
3655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656/*
3657 * If "tv" is a string give an error and return FAIL.
3658 */
3659 int
3660check_not_string(typval_T *tv)
3661{
3662 if (tv->v_type == VAR_STRING)
3663 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01003664 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 clear_tv(tv);
3666 return FAIL;
3667 }
3668 return OK;
3669}
3670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671
3672#endif // FEAT_EVAL