blob: 3f61cb4457fe8852b9532c82a503058c1b332eaf [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 Moolenaar34c54eb2020-11-25 19:15:19 +0100470 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200471 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
472 + ectx->ec_dfunc_idx;
473 int argcount = ufunc_argcount(dfunc->df_ufunc);
474 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200475 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476
477 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200478 entry = estack_pop();
479 if (entry != NULL)
480 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200482 if (handle_closure_in_use(ectx, TRUE) == FAIL)
483 return FAIL;
484
485 // Clear the arguments.
486 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
487 clear_tv(STACK_TV(idx));
488
489 // Clear local variables and temp values, but not the return value.
490 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100491 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100492 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100493
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100494 // The return value should be on top of the stack. However, when aborting
495 // it may not be there and ec_frame_idx is the top of the stack.
496 ret_idx = ectx->ec_stack.ga_len - 1;
497 if (ret_idx == ectx->ec_frame_idx + 4)
498 ret_idx = 0;
499
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100500 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200501 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
502 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200503 ectx->ec_outer_stack =
504 (void *)STACK_TV(ectx->ec_frame_idx + 2)->vval.v_string;
505 ectx->ec_outer_frame = STACK_TV(ectx->ec_frame_idx + 3)->vval.v_number;
506 // restoring ec_frame_idx must be last
507 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 4)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
509 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100510
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100511 if (ret_idx > 0)
512 {
513 // Reset the stack to the position before the call, with a spot for the
514 // return value, moved there from above the frame.
515 ectx->ec_stack.ga_len = top + 1;
516 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
517 }
518 else
519 // Reset the stack to the position before the call.
520 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200521
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100522 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200523 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524}
525
526#undef STACK_TV
527
528/*
529 * Prepare arguments and rettv for calling a builtin or user function.
530 */
531 static int
532call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
533{
534 int idx;
535 typval_T *tv;
536
537 // Move arguments from bottom of the stack to argvars[] and add terminator.
538 for (idx = 0; idx < argcount; ++idx)
539 argvars[idx] = *STACK_TV_BOT(idx - argcount);
540 argvars[argcount].v_type = VAR_UNKNOWN;
541
542 // Result replaces the arguments on the stack.
543 if (argcount > 0)
544 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200545 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100546 return FAIL;
547 else
548 ++ectx->ec_stack.ga_len;
549
550 // Default return value is zero.
551 tv = STACK_TV_BOT(-1);
552 tv->v_type = VAR_NUMBER;
553 tv->vval.v_number = 0;
554
555 return OK;
556}
557
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200558// Ugly global to avoid passing the execution context around through many
559// layers.
560static ectx_T *current_ectx = NULL;
561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562/*
563 * Call a builtin function by index.
564 */
565 static int
566call_bfunc(int func_idx, int argcount, ectx_T *ectx)
567{
568 typval_T argvars[MAX_FUNC_ARGS];
569 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100570 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200571 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572
573 if (call_prepare(argcount, argvars, ectx) == FAIL)
574 return FAIL;
575
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200576 // Call the builtin function. Set "current_ectx" so that when it
577 // recursively invokes call_def_function() a closure context can be set.
578 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200580 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581
582 // Clear the arguments.
583 for (idx = 0; idx < argcount; ++idx)
584 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200585
Bram Moolenaar171fb922020-10-28 16:54:47 +0100586 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200587 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588 return OK;
589}
590
591/*
592 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100593 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594 */
595 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100596call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597{
598 typval_T argvars[MAX_FUNC_ARGS];
599 funcexe_T funcexe;
600 int error;
601 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100602 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200604 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200605 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
606 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200607 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100608 {
609 // The function has been compiled, can call it quickly. For a function
610 // that was defined later: we can call it directly next time.
611 if (iptr != NULL)
612 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100613 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100614 iptr->isn_type = ISN_DCALL;
615 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
616 iptr->isn_arg.dfunc.cdf_argcount = argcount;
617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100619 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620
621 if (call_prepare(argcount, argvars, ectx) == FAIL)
622 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200623 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 funcexe.evaluate = TRUE;
625
626 // Call the user function. Result goes in last position on the stack.
627 // TODO: add selfdict if there is one
628 error = call_user_func_check(ufunc, argcount, argvars,
629 STACK_TV_BOT(-1), &funcexe, NULL);
630
631 // Clear the arguments.
632 for (idx = 0; idx < argcount; ++idx)
633 clear_tv(&argvars[idx]);
634
635 if (error != FCERR_NONE)
636 {
637 user_func_error(error, ufunc->uf_name);
638 return FAIL;
639 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100640 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200641 // Error other than from calling the function itself.
642 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 return OK;
644}
645
646/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200647 * Return TRUE if an error was given or CTRL-C was pressed.
648 */
649 static int
650vim9_aborting(int prev_called_emsg)
651{
652 return called_emsg > prev_called_emsg || got_int || did_throw;
653}
654
655/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656 * Execute a function by "name".
657 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100658 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100659 * Returns FAIL if not found without an error message.
660 */
661 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100662call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663{
664 ufunc_T *ufunc;
665
666 if (builtin_function(name, -1))
667 {
668 int func_idx = find_internal_func(name);
669
670 if (func_idx < 0)
671 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200672 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673 return FAIL;
674 return call_bfunc(func_idx, argcount, ectx);
675 }
676
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200677 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200678
679 if (ufunc == NULL)
680 {
681 int called_emsg_before = called_emsg;
682
683 if (script_autoload(name, TRUE))
684 // loaded a package, search for the function again
685 ufunc = find_func(name, FALSE, NULL);
686 if (vim9_aborting(called_emsg_before))
687 return FAIL; // bail out if loading the script caused an error
688 }
689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100690 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100691 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692
693 return FAIL;
694}
695
696 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200697call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200699 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200700 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200702 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703
704 if (tv->v_type == VAR_PARTIAL)
705 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200706 partial_T *pt = tv->vval.v_partial;
707 int i;
708
709 if (pt->pt_argc > 0)
710 {
711 // Make space for arguments from the partial, shift the "argcount"
712 // arguments up.
713 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
714 return FAIL;
715 for (i = 1; i <= argcount; ++i)
716 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
717 ectx->ec_stack.ga_len += pt->pt_argc;
718 argcount += pt->pt_argc;
719
720 // copy the arguments from the partial onto the stack
721 for (i = 0; i < pt->pt_argc; ++i)
722 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
723 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724
725 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200726 {
727 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
728
729 // closure may need the function context where it was defined
730 ectx->ec_outer_stack = pt->pt_ectx_stack;
731 ectx->ec_outer_frame = pt->pt_ectx_frame;
732
733 return ret;
734 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735 name = pt->pt_name;
736 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200737 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200739 if (name != NULL)
740 {
741 char_u fname_buf[FLEN_FIXED + 1];
742 char_u *tofree = NULL;
743 int error = FCERR_NONE;
744 char_u *fname;
745
746 // May need to translate <SNR>123_ to K_SNR.
747 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
748 if (error != FCERR_NONE)
749 res = FAIL;
750 else
751 res = call_by_name(fname, argcount, ectx, NULL);
752 vim_free(tofree);
753 }
754
755 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 {
757 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200758 semsg(_(e_unknownfunc),
759 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 return FAIL;
761 }
762 return OK;
763}
764
765/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200766 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
767 * TRUE.
768 */
769 static int
770error_if_locked(int lock, char *error)
771{
772 if (lock & (VAR_LOCKED | VAR_FIXED))
773 {
774 emsg(_(error));
775 return TRUE;
776 }
777 return FALSE;
778}
779
780/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100781 * Store "tv" in variable "name".
782 * This is for s: and g: variables.
783 */
784 static void
785store_var(char_u *name, typval_T *tv)
786{
787 funccal_entry_T entry;
788
789 save_funccal(&entry);
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200790 set_var_const(name, NULL, tv, FALSE, ASSIGN_NO_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100791 restore_funccal();
792}
793
Bram Moolenaard3aac292020-04-19 14:32:17 +0200794
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100795/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796 * Execute a function by "name".
797 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100798 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 */
800 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100801call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100802{
Bram Moolenaared677f52020-08-12 16:38:10 +0200803 int called_emsg_before = called_emsg;
804 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805
Bram Moolenaared677f52020-08-12 16:38:10 +0200806 res = call_by_name(name, argcount, ectx, iptr);
807 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200809 dictitem_T *v;
810
811 v = find_var(name, NULL, FALSE);
812 if (v == NULL)
813 {
814 semsg(_(e_unknownfunc), name);
815 return FAIL;
816 }
817 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
818 {
819 semsg(_(e_unknownfunc), name);
820 return FAIL;
821 }
822 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200824 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825}
826
827/*
828 * Call a "def" function from old Vim script.
829 * Return OK or FAIL.
830 */
831 int
832call_def_function(
833 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200834 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200836 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 typval_T *rettv) // return value
838{
839 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200840 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200841 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100842 typval_T *tv;
843 int idx;
844 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100845 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200846 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200847 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100848 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200849 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +0200850 msglist_T **saved_msg_list = NULL;
851 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +0200852 cmdmod_T save_cmdmod;
853 int restore_cmdmod = FALSE;
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100854 int save_emsg_silent_def = emsg_silent_def;
855 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100856 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100857 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858
859// Get pointer to item in the stack.
860#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
861
862// Get pointer to item at the bottom of the stack, -1 is the bottom.
863#undef STACK_TV_BOT
864#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
865
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200866// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200867#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 +0100868
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200869// Like STACK_TV_VAR but use the outer scope
870#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
871
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200872 if (ufunc->uf_def_status == UF_NOT_COMPILED
873 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200874 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200875 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100876 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200877 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200878 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200879 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200880 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200881
Bram Moolenaar09689a02020-05-09 22:50:08 +0200882 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200883 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200884 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
885 + ufunc->uf_dfunc_idx;
886 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200887 {
888 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200889 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200890 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100893 // If depth of calling is getting too high, don't execute the function.
894 orig_funcdepth = funcdepth_get();
895 if (funcdepth_increment() == FAIL)
896 return FAIL;
897
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200898 CLEAR_FIELD(ectx);
899 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
900 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
901 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100902 {
903 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200904 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200907 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908
909 // Put arguments on the stack.
910 for (idx = 0; idx < argc; ++idx)
911 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200912 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200913 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
914 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200915 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100916 copy_tv(&argv[idx], STACK_TV_BOT(0));
917 ++ectx.ec_stack.ga_len;
918 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200919
920 // Turn varargs into a list. Empty list if no args.
921 if (ufunc->uf_va_name != NULL)
922 {
923 int vararg_count = argc - ufunc->uf_args.ga_len;
924
925 if (vararg_count < 0)
926 vararg_count = 0;
927 else
928 argc -= vararg_count;
929 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200930 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200931
932 // Check the type of the list items.
933 tv = STACK_TV_BOT(-1);
934 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +0200935 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200936 && ufunc->uf_va_type->tt_member != &t_any
937 && tv->vval.v_list != NULL)
938 {
939 type_T *expected = ufunc->uf_va_type->tt_member;
940 listitem_T *li = tv->vval.v_list->lv_first;
941
942 for (idx = 0; idx < vararg_count; ++idx)
943 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200944 if (check_typval_type(expected, &li->li_tv,
945 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200946 goto failed_early;
947 li = li->li_next;
948 }
949 }
950
Bram Moolenaar23e03252020-04-12 22:22:31 +0200951 if (defcount > 0)
952 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200953 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200954 --ectx.ec_stack.ga_len;
955 }
956
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100957 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200958 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100959 if (defcount > 0)
960 for (idx = 0; idx < defcount; ++idx)
961 {
962 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
963 ++ectx.ec_stack.ga_len;
964 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200965 if (ufunc->uf_va_name != NULL)
966 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967
968 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200969 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
970 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200972 if (partial != NULL)
973 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200974 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
975 {
976 // TODO: is this always the right way?
977 ectx.ec_outer_stack = &current_ectx->ec_stack;
978 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
979 }
980 else
981 {
982 ectx.ec_outer_stack = partial->pt_ectx_stack;
983 ectx.ec_outer_frame = partial->pt_ectx_frame;
984 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200985 }
986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987 // dummy frame entries
988 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
989 {
990 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
991 ++ectx.ec_stack.ga_len;
992 }
993
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200994 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200995 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200996 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
997 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200999 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001000 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001001 ectx.ec_stack.ga_len += dfunc->df_varcount;
1002 if (dfunc->df_has_closure)
1003 {
1004 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1005 STACK_TV_VAR(idx)->vval.v_number = 0;
1006 ++ectx.ec_stack.ga_len;
1007 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001008
1009 ectx.ec_instr = dfunc->df_instr;
1010 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001011
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001012 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001013 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001014 estack_push_ufunc(ufunc, 1);
1015 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001016 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1017
Bram Moolenaar352134b2020-10-17 22:04:08 +02001018 // Use a specific location for storing error messages to be converted to an
1019 // exception.
1020 saved_msg_list = msg_list;
1021 msg_list = &private_msg_list;
1022
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001023 // Do turn errors into exceptions.
1024 suppress_errthrow = FALSE;
1025
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001026 // When ":silent!" was used before calling then we still abort the
1027 // function. If ":silent!" is used in the function then we don't.
1028 emsg_silent_def = emsg_silent;
1029 did_emsg_def = 0;
1030
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001031 // Decide where to start execution, handles optional arguments.
1032 init_instr_idx(ufunc, argc, &ectx);
1033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001034 for (;;)
1035 {
1036 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001037
Bram Moolenaar270d0382020-05-15 21:42:53 +02001038 if (++breakcheck_count >= 100)
1039 {
1040 line_breakcheck();
1041 breakcheck_count = 0;
1042 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001043 if (got_int)
1044 {
1045 // Turn CTRL-C into an exception.
1046 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001047 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001048 goto failed;
1049 did_throw = TRUE;
1050 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051
Bram Moolenaara26b9702020-04-18 19:53:28 +02001052 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1053 {
1054 // Turn an error message into an exception.
1055 did_emsg = FALSE;
1056 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1057 goto failed;
1058 did_throw = TRUE;
1059 *msg_list = NULL;
1060 }
1061
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001062 if (did_throw && !ectx.ec_in_catch)
1063 {
1064 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001065 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066
1067 // An exception jumps to the first catch, finally, or returns from
1068 // the current function.
1069 if (trystack->ga_len > 0)
1070 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001071 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072 {
1073 // jump to ":catch" or ":finally"
1074 ectx.ec_in_catch = TRUE;
1075 ectx.ec_iidx = trycmd->tcd_catch_idx;
1076 }
1077 else
1078 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001079 // Not inside try or need to return from current functions.
1080 // Push a dummy return value.
1081 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1082 goto failed;
1083 tv = STACK_TV_BOT(0);
1084 tv->v_type = VAR_NUMBER;
1085 tv->vval.v_number = 0;
1086 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001087 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001089 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001090 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001091 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1092 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093 goto done;
1094 }
1095
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001096 if (func_return(&ectx) == FAIL)
1097 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001098 }
1099 continue;
1100 }
1101
1102 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1103 switch (iptr->isn_type)
1104 {
1105 // execute Ex command line
1106 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001107 {
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001108 SOURCING_LNUM = iptr->isn_lnum;
1109 do_cmdline_cmd(iptr->isn_arg.string);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001110 if (did_emsg)
1111 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001112 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113 break;
1114
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001115 // execute Ex command from pieces on the stack
1116 case ISN_EXECCONCAT:
1117 {
1118 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001119 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001120 int pass;
1121 int i;
1122 char_u *cmd = NULL;
1123 char_u *str;
1124
1125 for (pass = 1; pass <= 2; ++pass)
1126 {
1127 for (i = 0; i < count; ++i)
1128 {
1129 tv = STACK_TV_BOT(i - count);
1130 str = tv->vval.v_string;
1131 if (str != NULL && *str != NUL)
1132 {
1133 if (pass == 2)
1134 STRCPY(cmd + len, str);
1135 len += STRLEN(str);
1136 }
1137 if (pass == 2)
1138 clear_tv(tv);
1139 }
1140 if (pass == 1)
1141 {
1142 cmd = alloc(len + 1);
1143 if (cmd == NULL)
1144 goto failed;
1145 len = 0;
1146 }
1147 }
1148
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001149 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001150 do_cmdline_cmd(cmd);
1151 vim_free(cmd);
1152 }
1153 break;
1154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 // execute :echo {string} ...
1156 case ISN_ECHO:
1157 {
1158 int count = iptr->isn_arg.echo.echo_count;
1159 int atstart = TRUE;
1160 int needclr = TRUE;
1161
1162 for (idx = 0; idx < count; ++idx)
1163 {
1164 tv = STACK_TV_BOT(idx - count);
1165 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1166 &atstart, &needclr);
1167 clear_tv(tv);
1168 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001169 if (needclr)
1170 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 ectx.ec_stack.ga_len -= count;
1172 }
1173 break;
1174
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001175 // :execute {string} ...
1176 // :echomsg {string} ...
1177 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001178 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001179 case ISN_ECHOMSG:
1180 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001181 {
1182 int count = iptr->isn_arg.number;
1183 garray_T ga;
1184 char_u buf[NUMBUFLEN];
1185 char_u *p;
1186 int len;
1187 int failed = FALSE;
1188
1189 ga_init2(&ga, 1, 80);
1190 for (idx = 0; idx < count; ++idx)
1191 {
1192 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001193 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001194 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001195 if (tv->v_type == VAR_CHANNEL
1196 || tv->v_type == VAR_JOB)
1197 {
1198 SOURCING_LNUM = iptr->isn_lnum;
1199 emsg(_(e_inval_string));
1200 break;
1201 }
1202 else
1203 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001204 }
1205 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001206 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001207
1208 len = (int)STRLEN(p);
1209 if (ga_grow(&ga, len + 2) == FAIL)
1210 failed = TRUE;
1211 else
1212 {
1213 if (ga.ga_len > 0)
1214 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1215 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1216 ga.ga_len += len;
1217 }
1218 clear_tv(tv);
1219 }
1220 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001221 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001222 {
1223 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001224 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001225 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001226
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001227 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001228 {
1229 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001230 {
1231 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001232 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001233 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001234 {
1235 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001236 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001237 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001238 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001239 else
1240 {
1241 msg_sb_eol();
1242 if (iptr->isn_type == ISN_ECHOMSG)
1243 {
1244 msg_attr(ga.ga_data, echo_attr);
1245 out_flush();
1246 }
1247 else
1248 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001249 SOURCING_LNUM = iptr->isn_lnum;
1250 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001251 }
1252 }
1253 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001254 ga_clear(&ga);
1255 }
1256 break;
1257
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 // load local variable or argument
1259 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001260 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 goto failed;
1262 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1263 ++ectx.ec_stack.ga_len;
1264 break;
1265
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001266 // load variable or argument from outer scope
1267 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001268 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001269 goto failed;
1270 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1271 STACK_TV_BOT(0));
1272 ++ectx.ec_stack.ga_len;
1273 break;
1274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 // load v: variable
1276 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001277 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001278 goto failed;
1279 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1280 ++ectx.ec_stack.ga_len;
1281 break;
1282
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001283 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284 case ISN_LOADSCRIPT:
1285 {
1286 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001287 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 svar_T *sv;
1289
1290 sv = ((svar_T *)si->sn_var_vals.ga_data)
1291 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001292 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293 goto failed;
1294 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1295 ++ectx.ec_stack.ga_len;
1296 }
1297 break;
1298
1299 // load s: variable in old script
1300 case ISN_LOADS:
1301 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001302 hashtab_T *ht = &SCRIPT_VARS(
1303 iptr->isn_arg.loadstore.ls_sid);
1304 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001306
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307 if (di == NULL)
1308 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001309 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001310 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001311 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 }
1313 else
1314 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001315 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 goto failed;
1317 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1318 ++ectx.ec_stack.ga_len;
1319 }
1320 }
1321 break;
1322
Bram Moolenaard3aac292020-04-19 14:32:17 +02001323 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001325 case ISN_LOADB:
1326 case ISN_LOADW:
1327 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001329 dictitem_T *di = NULL;
1330 hashtab_T *ht = NULL;
1331 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001332
Bram Moolenaard3aac292020-04-19 14:32:17 +02001333 switch (iptr->isn_type)
1334 {
1335 case ISN_LOADG:
1336 ht = get_globvar_ht();
1337 namespace = 'g';
1338 break;
1339 case ISN_LOADB:
1340 ht = &curbuf->b_vars->dv_hashtab;
1341 namespace = 'b';
1342 break;
1343 case ISN_LOADW:
1344 ht = &curwin->w_vars->dv_hashtab;
1345 namespace = 'w';
1346 break;
1347 case ISN_LOADT:
1348 ht = &curtab->tp_vars->dv_hashtab;
1349 namespace = 't';
1350 break;
1351 default: // Cannot reach here
1352 goto failed;
1353 }
1354 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 if (di == NULL)
1357 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001358 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001359 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001360 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001361 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 }
1363 else
1364 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001365 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 goto failed;
1367 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1368 ++ectx.ec_stack.ga_len;
1369 }
1370 }
1371 break;
1372
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001373 // load g:/b:/w:/t: namespace
1374 case ISN_LOADGDICT:
1375 case ISN_LOADBDICT:
1376 case ISN_LOADWDICT:
1377 case ISN_LOADTDICT:
1378 {
1379 dict_T *d = NULL;
1380
1381 switch (iptr->isn_type)
1382 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001383 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1384 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1385 case ISN_LOADWDICT: d = curwin->w_vars; break;
1386 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001387 default: // Cannot reach here
1388 goto failed;
1389 }
1390 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1391 goto failed;
1392 tv = STACK_TV_BOT(0);
1393 tv->v_type = VAR_DICT;
1394 tv->v_lock = 0;
1395 tv->vval.v_dict = d;
1396 ++ectx.ec_stack.ga_len;
1397 }
1398 break;
1399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 // load &option
1401 case ISN_LOADOPT:
1402 {
1403 typval_T optval;
1404 char_u *name = iptr->isn_arg.string;
1405
Bram Moolenaara8c17702020-04-01 21:17:24 +02001406 // This is not expected to fail, name is checked during
1407 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001408 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001410 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001411 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 *STACK_TV_BOT(0) = optval;
1413 ++ectx.ec_stack.ga_len;
1414 }
1415 break;
1416
1417 // load $ENV
1418 case ISN_LOADENV:
1419 {
1420 typval_T optval;
1421 char_u *name = iptr->isn_arg.string;
1422
Bram Moolenaar270d0382020-05-15 21:42:53 +02001423 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001425 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001426 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 *STACK_TV_BOT(0) = optval;
1428 ++ectx.ec_stack.ga_len;
1429 }
1430 break;
1431
1432 // load @register
1433 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001434 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435 goto failed;
1436 tv = STACK_TV_BOT(0);
1437 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001438 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001439 // This may result in NULL, which should be equivalent to an
1440 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 tv->vval.v_string = get_reg_contents(
1442 iptr->isn_arg.number, GREG_EXPR_SRC);
1443 ++ectx.ec_stack.ga_len;
1444 break;
1445
1446 // store local variable
1447 case ISN_STORE:
1448 --ectx.ec_stack.ga_len;
1449 tv = STACK_TV_VAR(iptr->isn_arg.number);
1450 clear_tv(tv);
1451 *tv = *STACK_TV_BOT(0);
1452 break;
1453
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001454 // store variable or argument in outer scope
1455 case ISN_STOREOUTER:
1456 --ectx.ec_stack.ga_len;
1457 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1458 clear_tv(tv);
1459 *tv = *STACK_TV_BOT(0);
1460 break;
1461
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001462 // store s: variable in old script
1463 case ISN_STORES:
1464 {
1465 hashtab_T *ht = &SCRIPT_VARS(
1466 iptr->isn_arg.loadstore.ls_sid);
1467 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001468 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001469
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001470 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001471 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001472 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001473 else
1474 {
1475 clear_tv(&di->di_tv);
1476 di->di_tv = *STACK_TV_BOT(0);
1477 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001478 }
1479 break;
1480
1481 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 case ISN_STORESCRIPT:
1483 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001484 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 iptr->isn_arg.script.script_sid);
1486 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1487 + iptr->isn_arg.script.script_idx;
1488
1489 --ectx.ec_stack.ga_len;
1490 clear_tv(sv->sv_tv);
1491 *sv->sv_tv = *STACK_TV_BOT(0);
1492 }
1493 break;
1494
1495 // store option
1496 case ISN_STOREOPT:
1497 {
1498 long n = 0;
1499 char_u *s = NULL;
1500 char *msg;
1501
1502 --ectx.ec_stack.ga_len;
1503 tv = STACK_TV_BOT(0);
1504 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001505 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001507 if (s == NULL)
1508 s = (char_u *)"";
1509 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001511 // must be VAR_NUMBER, CHECKTYPE makes sure
1512 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1514 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001515 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001516 if (msg != NULL)
1517 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001518 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001520 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 }
1523 break;
1524
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001525 // store $ENV
1526 case ISN_STOREENV:
1527 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001528 tv = STACK_TV_BOT(0);
1529 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1530 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001531 break;
1532
1533 // store @r
1534 case ISN_STOREREG:
1535 {
1536 int reg = iptr->isn_arg.number;
1537
1538 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001539 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001540 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001541 tv_get_string(tv), -1, FALSE);
1542 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001543 }
1544 break;
1545
1546 // store v: variable
1547 case ISN_STOREV:
1548 --ectx.ec_stack.ga_len;
1549 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1550 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001551 // should not happen, type is checked when compiling
1552 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001553 break;
1554
Bram Moolenaard3aac292020-04-19 14:32:17 +02001555 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001557 case ISN_STOREB:
1558 case ISN_STOREW:
1559 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 {
1561 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001562 hashtab_T *ht;
1563 switch (iptr->isn_type)
1564 {
1565 case ISN_STOREG:
1566 ht = get_globvar_ht();
1567 break;
1568 case ISN_STOREB:
1569 ht = &curbuf->b_vars->dv_hashtab;
1570 break;
1571 case ISN_STOREW:
1572 ht = &curwin->w_vars->dv_hashtab;
1573 break;
1574 case ISN_STORET:
1575 ht = &curtab->tp_vars->dv_hashtab;
1576 break;
1577 default: // Cannot reach here
1578 goto failed;
1579 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580
1581 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001582 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001584 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 else
1586 {
1587 clear_tv(&di->di_tv);
1588 di->di_tv = *STACK_TV_BOT(0);
1589 }
1590 }
1591 break;
1592
1593 // store number in local variable
1594 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001595 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 clear_tv(tv);
1597 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001598 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 break;
1600
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001601 // store value in list variable
1602 case ISN_STORELIST:
1603 {
1604 typval_T *tv_idx = STACK_TV_BOT(-2);
1605 varnumber_T lidx = tv_idx->vval.v_number;
1606 typval_T *tv_list = STACK_TV_BOT(-1);
1607 list_T *list = tv_list->vval.v_list;
1608
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001609 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001610 if (lidx < 0 && list->lv_len + lidx >= 0)
1611 // negative index is relative to the end
1612 lidx = list->lv_len + lidx;
1613 if (lidx < 0 || lidx > list->lv_len)
1614 {
1615 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001616 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001617 }
1618 tv = STACK_TV_BOT(-3);
1619 if (lidx < list->lv_len)
1620 {
1621 listitem_T *li = list_find(list, lidx);
1622
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001623 if (error_if_locked(li->li_tv.v_lock,
1624 e_cannot_change_list_item))
1625 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001626 // overwrite existing list item
1627 clear_tv(&li->li_tv);
1628 li->li_tv = *tv;
1629 }
1630 else
1631 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001632 if (error_if_locked(list->lv_lock,
1633 e_cannot_change_list))
1634 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001635 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001636 if (list_append_tv(list, tv) == FAIL)
1637 goto failed;
1638 clear_tv(tv);
1639 }
1640 clear_tv(tv_idx);
1641 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001642 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001643 }
1644 break;
1645
1646 // store value in dict variable
1647 case ISN_STOREDICT:
1648 {
1649 typval_T *tv_key = STACK_TV_BOT(-2);
1650 char_u *key = tv_key->vval.v_string;
1651 typval_T *tv_dict = STACK_TV_BOT(-1);
1652 dict_T *dict = tv_dict->vval.v_dict;
1653 dictitem_T *di;
1654
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001655 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001656 if (dict == NULL)
1657 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001658 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001659 goto on_error;
1660 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001661 if (key == NULL)
1662 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001663 tv = STACK_TV_BOT(-3);
1664 di = dict_find(dict, key, -1);
1665 if (di != NULL)
1666 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001667 if (error_if_locked(di->di_tv.v_lock,
1668 e_cannot_change_dict_item))
1669 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001670 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001671 clear_tv(&di->di_tv);
1672 di->di_tv = *tv;
1673 }
1674 else
1675 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001676 if (error_if_locked(dict->dv_lock,
1677 e_cannot_change_dict))
1678 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001679 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001680 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1681 goto failed;
1682 clear_tv(tv);
1683 }
1684 clear_tv(tv_key);
1685 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001686 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001687 }
1688 break;
1689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 // push constant
1691 case ISN_PUSHNR:
1692 case ISN_PUSHBOOL:
1693 case ISN_PUSHSPEC:
1694 case ISN_PUSHF:
1695 case ISN_PUSHS:
1696 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001697 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001698 case ISN_PUSHCHANNEL:
1699 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001700 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 goto failed;
1702 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001703 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 ++ectx.ec_stack.ga_len;
1705 switch (iptr->isn_type)
1706 {
1707 case ISN_PUSHNR:
1708 tv->v_type = VAR_NUMBER;
1709 tv->vval.v_number = iptr->isn_arg.number;
1710 break;
1711 case ISN_PUSHBOOL:
1712 tv->v_type = VAR_BOOL;
1713 tv->vval.v_number = iptr->isn_arg.number;
1714 break;
1715 case ISN_PUSHSPEC:
1716 tv->v_type = VAR_SPECIAL;
1717 tv->vval.v_number = iptr->isn_arg.number;
1718 break;
1719#ifdef FEAT_FLOAT
1720 case ISN_PUSHF:
1721 tv->v_type = VAR_FLOAT;
1722 tv->vval.v_float = iptr->isn_arg.fnumber;
1723 break;
1724#endif
1725 case ISN_PUSHBLOB:
1726 blob_copy(iptr->isn_arg.blob, tv);
1727 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001728 case ISN_PUSHFUNC:
1729 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001730 if (iptr->isn_arg.string == NULL)
1731 tv->vval.v_string = NULL;
1732 else
1733 tv->vval.v_string =
1734 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001735 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001736 case ISN_PUSHCHANNEL:
1737#ifdef FEAT_JOB_CHANNEL
1738 tv->v_type = VAR_CHANNEL;
1739 tv->vval.v_channel = iptr->isn_arg.channel;
1740 if (tv->vval.v_channel != NULL)
1741 ++tv->vval.v_channel->ch_refcount;
1742#endif
1743 break;
1744 case ISN_PUSHJOB:
1745#ifdef FEAT_JOB_CHANNEL
1746 tv->v_type = VAR_JOB;
1747 tv->vval.v_job = iptr->isn_arg.job;
1748 if (tv->vval.v_job != NULL)
1749 ++tv->vval.v_job->jv_refcount;
1750#endif
1751 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 default:
1753 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001754 tv->vval.v_string = vim_strsave(
1755 iptr->isn_arg.string == NULL
1756 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 }
1758 break;
1759
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001760 case ISN_UNLET:
1761 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1762 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001763 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001764 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001765 case ISN_UNLETENV:
1766 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1767 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001768
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001769 case ISN_LOCKCONST:
1770 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1771 break;
1772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 // create a list from items on the stack; uses a single allocation
1774 // for the list header and the items
1775 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001776 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1777 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 break;
1779
1780 // create a dict from items on the stack
1781 case ISN_NEWDICT:
1782 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001783 int count = iptr->isn_arg.number;
1784 dict_T *dict = dict_alloc();
1785 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001786 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787
1788 if (dict == NULL)
1789 goto failed;
1790 for (idx = 0; idx < count; ++idx)
1791 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001792 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001794 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001795 key = tv->vval.v_string == NULL
1796 ? (char_u *)"" : tv->vval.v_string;
1797 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02001798 if (item != NULL)
1799 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001800 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001801 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02001802 dict_unref(dict);
1803 goto on_error;
1804 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001805 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 clear_tv(tv);
1807 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001808 {
1809 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001811 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1813 item->di_tv.v_lock = 0;
1814 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001815 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001816 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001817 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001819 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 }
1821
1822 if (count > 0)
1823 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001824 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 goto failed;
1826 else
1827 ++ectx.ec_stack.ga_len;
1828 tv = STACK_TV_BOT(-1);
1829 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001830 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 tv->vval.v_dict = dict;
1832 ++dict->dv_refcount;
1833 }
1834 break;
1835
1836 // call a :def function
1837 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001838 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1840 iptr->isn_arg.dfunc.cdf_argcount,
1841 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001842 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 break;
1844
1845 // call a builtin function
1846 case ISN_BCALL:
1847 SOURCING_LNUM = iptr->isn_lnum;
1848 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1849 iptr->isn_arg.bfunc.cbf_argcount,
1850 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001851 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 break;
1853
1854 // call a funcref or partial
1855 case ISN_PCALL:
1856 {
1857 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1858 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001859 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860
1861 SOURCING_LNUM = iptr->isn_lnum;
1862 if (pfunc->cpf_top)
1863 {
1864 // funcref is above the arguments
1865 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1866 }
1867 else
1868 {
1869 // Get the funcref from the stack.
1870 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001871 partial_tv = *STACK_TV_BOT(0);
1872 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873 }
1874 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001875 if (tv == &partial_tv)
1876 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001878 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879 }
1880 break;
1881
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001882 case ISN_PCALL_END:
1883 // PCALL finished, arguments have been consumed and replaced by
1884 // the return value. Now clear the funcref from the stack,
1885 // and move the return value in its place.
1886 --ectx.ec_stack.ga_len;
1887 clear_tv(STACK_TV_BOT(-1));
1888 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1889 break;
1890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 // call a user defined function or funcref/partial
1892 case ISN_UCALL:
1893 {
1894 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1895
1896 SOURCING_LNUM = iptr->isn_lnum;
1897 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001898 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001899 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900 }
1901 break;
1902
1903 // return from a :def function call
1904 case ISN_RETURN:
1905 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001906 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001907 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001908
1909 if (trystack->ga_len > 0)
1910 trycmd = ((trycmd_T *)trystack->ga_data)
1911 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001912 if (trycmd != NULL
1913 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914 && trycmd->tcd_finally_idx != 0)
1915 {
1916 // jump to ":finally"
1917 ectx.ec_iidx = trycmd->tcd_finally_idx;
1918 trycmd->tcd_return = TRUE;
1919 }
1920 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001921 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 }
1923 break;
1924
1925 // push a function reference to a compiled function
1926 case ISN_FUNCREF:
1927 {
1928 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001929 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930
1931 pt = ALLOC_CLEAR_ONE(partial_T);
1932 if (pt == NULL)
1933 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001934 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001935 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001936 vim_free(pt);
1937 goto failed;
1938 }
1939 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1940 + iptr->isn_arg.funcref.fr_func;
1941 pt->pt_func = pt_dfunc->df_ufunc;
1942 pt->pt_refcount = 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001943
1944 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1945 {
1946 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1947 + ectx.ec_dfunc_idx;
1948
1949 // The closure needs to find arguments and local
1950 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001951 pt->pt_ectx_stack = &ectx.ec_stack;
1952 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001953
1954 // If this function returns and the closure is still
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001955 // being used, we need to make a copy of the context
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001956 // (arguments and local variables). Store a reference
1957 // to the partial so we can handle that.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001958 if (ga_grow(&ectx.ec_funcrefs, 1) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001959 {
Bram Moolenaardec07512020-09-18 23:11:10 +02001960 vim_free(pt);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001961 goto failed;
1962 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001963 // Extra variable keeps the count of closures created
1964 // in the current function call.
1965 tv = STACK_TV_VAR(dfunc->df_varcount);
1966 ++tv->vval.v_number;
1967
1968 ((partial_T **)ectx.ec_funcrefs.ga_data)
1969 [ectx.ec_funcrefs.ga_len] = pt;
1970 ++pt->pt_refcount;
1971 ++ectx.ec_funcrefs.ga_len;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001972 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001973 ++pt_dfunc->df_ufunc->uf_refcount;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001974
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 tv = STACK_TV_BOT(0);
1976 ++ectx.ec_stack.ga_len;
1977 tv->vval.v_partial = pt;
1978 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001979 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 }
1981 break;
1982
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001983 // Create a global function from a lambda.
1984 case ISN_NEWFUNC:
1985 {
1986 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1987
1988 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1989 }
1990 break;
1991
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001992 // List functions
1993 case ISN_DEF:
1994 if (iptr->isn_arg.string == NULL)
1995 list_functions(NULL);
1996 else
1997 {
1998 exarg_T ea;
1999
2000 CLEAR_FIELD(ea);
2001 ea.cmd = ea.arg = iptr->isn_arg.string;
2002 define_function(&ea, NULL);
2003 }
2004 break;
2005
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 // jump if a condition is met
2007 case ISN_JUMP:
2008 {
2009 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002010 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 int jump = TRUE;
2012
2013 if (when != JUMP_ALWAYS)
2014 {
2015 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002016 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002017 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002018 || when == JUMP_IF_COND_TRUE)
2019 {
2020 SOURCING_LNUM = iptr->isn_lnum;
2021 jump = tv_get_bool_chk(tv, &error);
2022 if (error)
2023 goto on_error;
2024 }
2025 else
2026 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002028 || when == JUMP_AND_KEEP_IF_FALSE
2029 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002031 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 {
2033 // drop the value from the stack
2034 clear_tv(tv);
2035 --ectx.ec_stack.ga_len;
2036 }
2037 }
2038 if (jump)
2039 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2040 }
2041 break;
2042
2043 // top of a for loop
2044 case ISN_FOR:
2045 {
2046 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2047 typval_T *idxtv =
2048 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2049
2050 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002051 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002053 ++idxtv->vval.v_number;
2054 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055 // past the end of the list, jump to "endfor"
2056 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2057 else if (list->lv_first == &range_list_item)
2058 {
2059 // non-materialized range() list
2060 tv = STACK_TV_BOT(0);
2061 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002062 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063 tv->vval.v_number = list_find_nr(
2064 list, idxtv->vval.v_number, NULL);
2065 ++ectx.ec_stack.ga_len;
2066 }
2067 else
2068 {
2069 listitem_T *li = list_find(list, idxtv->vval.v_number);
2070
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2072 ++ectx.ec_stack.ga_len;
2073 }
2074 }
2075 break;
2076
2077 // start of ":try" block
2078 case ISN_TRY:
2079 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002080 trycmd_T *trycmd = NULL;
2081
Bram Moolenaar270d0382020-05-15 21:42:53 +02002082 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083 goto failed;
2084 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2085 + ectx.ec_trystack.ga_len;
2086 ++ectx.ec_trystack.ga_len;
2087 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002088 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2090 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002091 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002092 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093 }
2094 break;
2095
2096 case ISN_PUSHEXC:
2097 if (current_exception == NULL)
2098 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002099 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 iemsg("Evaluating catch while current_exception is NULL");
2101 goto failed;
2102 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002103 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 goto failed;
2105 tv = STACK_TV_BOT(0);
2106 ++ectx.ec_stack.ga_len;
2107 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002108 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 tv->vval.v_string = vim_strsave(
2110 (char_u *)current_exception->value);
2111 break;
2112
2113 case ISN_CATCH:
2114 {
2115 garray_T *trystack = &ectx.ec_trystack;
2116
2117 if (trystack->ga_len > 0)
2118 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002119 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120 + trystack->ga_len - 1;
2121 trycmd->tcd_caught = TRUE;
2122 }
2123 did_emsg = got_int = did_throw = FALSE;
2124 catch_exception(current_exception);
2125 }
2126 break;
2127
2128 // end of ":try" block
2129 case ISN_ENDTRY:
2130 {
2131 garray_T *trystack = &ectx.ec_trystack;
2132
2133 if (trystack->ga_len > 0)
2134 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002135 trycmd_T *trycmd = NULL;
2136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 --trystack->ga_len;
2138 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002139 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 trycmd = ((trycmd_T *)trystack->ga_data)
2141 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002142 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 {
2144 // discard the exception
2145 if (caught_stack == current_exception)
2146 caught_stack = caught_stack->caught;
2147 discard_current_exception();
2148 }
2149
2150 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002151 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002152 }
2153 }
2154 break;
2155
2156 case ISN_THROW:
2157 --ectx.ec_stack.ga_len;
2158 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002159 if (tv->vval.v_string == NULL
2160 || *skipwhite(tv->vval.v_string) == NUL)
2161 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002162 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002163 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002164 emsg(_(e_throw_with_empty_string));
2165 goto failed;
2166 }
2167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2169 {
2170 vim_free(tv->vval.v_string);
2171 goto failed;
2172 }
2173 did_throw = TRUE;
2174 break;
2175
2176 // compare with special values
2177 case ISN_COMPAREBOOL:
2178 case ISN_COMPARESPECIAL:
2179 {
2180 typval_T *tv1 = STACK_TV_BOT(-2);
2181 typval_T *tv2 = STACK_TV_BOT(-1);
2182 varnumber_T arg1 = tv1->vval.v_number;
2183 varnumber_T arg2 = tv2->vval.v_number;
2184 int res;
2185
2186 switch (iptr->isn_arg.op.op_type)
2187 {
2188 case EXPR_EQUAL: res = arg1 == arg2; break;
2189 case EXPR_NEQUAL: res = arg1 != arg2; break;
2190 default: res = 0; break;
2191 }
2192
2193 --ectx.ec_stack.ga_len;
2194 tv1->v_type = VAR_BOOL;
2195 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2196 }
2197 break;
2198
2199 // Operation with two number arguments
2200 case ISN_OPNR:
2201 case ISN_COMPARENR:
2202 {
2203 typval_T *tv1 = STACK_TV_BOT(-2);
2204 typval_T *tv2 = STACK_TV_BOT(-1);
2205 varnumber_T arg1 = tv1->vval.v_number;
2206 varnumber_T arg2 = tv2->vval.v_number;
2207 varnumber_T res;
2208
2209 switch (iptr->isn_arg.op.op_type)
2210 {
2211 case EXPR_MULT: res = arg1 * arg2; break;
2212 case EXPR_DIV: res = arg1 / arg2; break;
2213 case EXPR_REM: 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: res = arg1 == arg2; break;
2218 case EXPR_NEQUAL: res = arg1 != arg2; break;
2219 case EXPR_GREATER: res = arg1 > arg2; break;
2220 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2221 case EXPR_SMALLER: res = arg1 < arg2; break;
2222 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2223 default: res = 0; break;
2224 }
2225
2226 --ectx.ec_stack.ga_len;
2227 if (iptr->isn_type == ISN_COMPARENR)
2228 {
2229 tv1->v_type = VAR_BOOL;
2230 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2231 }
2232 else
2233 tv1->vval.v_number = res;
2234 }
2235 break;
2236
2237 // Computation with two float arguments
2238 case ISN_OPFLOAT:
2239 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002240#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 {
2242 typval_T *tv1 = STACK_TV_BOT(-2);
2243 typval_T *tv2 = STACK_TV_BOT(-1);
2244 float_T arg1 = tv1->vval.v_float;
2245 float_T arg2 = tv2->vval.v_float;
2246 float_T res = 0;
2247 int cmp = FALSE;
2248
2249 switch (iptr->isn_arg.op.op_type)
2250 {
2251 case EXPR_MULT: res = arg1 * arg2; break;
2252 case EXPR_DIV: res = arg1 / arg2; break;
2253 case EXPR_SUB: res = arg1 - arg2; break;
2254 case EXPR_ADD: res = arg1 + arg2; break;
2255
2256 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2257 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2258 case EXPR_GREATER: cmp = arg1 > arg2; break;
2259 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2260 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2261 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2262 default: cmp = 0; break;
2263 }
2264 --ectx.ec_stack.ga_len;
2265 if (iptr->isn_type == ISN_COMPAREFLOAT)
2266 {
2267 tv1->v_type = VAR_BOOL;
2268 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2269 }
2270 else
2271 tv1->vval.v_float = res;
2272 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002273#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 break;
2275
2276 case ISN_COMPARELIST:
2277 {
2278 typval_T *tv1 = STACK_TV_BOT(-2);
2279 typval_T *tv2 = STACK_TV_BOT(-1);
2280 list_T *arg1 = tv1->vval.v_list;
2281 list_T *arg2 = tv2->vval.v_list;
2282 int cmp = FALSE;
2283 int ic = iptr->isn_arg.op.op_ic;
2284
2285 switch (iptr->isn_arg.op.op_type)
2286 {
2287 case EXPR_EQUAL: cmp =
2288 list_equal(arg1, arg2, ic, FALSE); break;
2289 case EXPR_NEQUAL: cmp =
2290 !list_equal(arg1, arg2, ic, FALSE); break;
2291 case EXPR_IS: cmp = arg1 == arg2; break;
2292 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2293 default: cmp = 0; break;
2294 }
2295 --ectx.ec_stack.ga_len;
2296 clear_tv(tv1);
2297 clear_tv(tv2);
2298 tv1->v_type = VAR_BOOL;
2299 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2300 }
2301 break;
2302
2303 case ISN_COMPAREBLOB:
2304 {
2305 typval_T *tv1 = STACK_TV_BOT(-2);
2306 typval_T *tv2 = STACK_TV_BOT(-1);
2307 blob_T *arg1 = tv1->vval.v_blob;
2308 blob_T *arg2 = tv2->vval.v_blob;
2309 int cmp = FALSE;
2310
2311 switch (iptr->isn_arg.op.op_type)
2312 {
2313 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2314 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2315 case EXPR_IS: cmp = arg1 == arg2; break;
2316 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2317 default: cmp = 0; break;
2318 }
2319 --ectx.ec_stack.ga_len;
2320 clear_tv(tv1);
2321 clear_tv(tv2);
2322 tv1->v_type = VAR_BOOL;
2323 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2324 }
2325 break;
2326
2327 // TODO: handle separately
2328 case ISN_COMPARESTRING:
2329 case ISN_COMPAREDICT:
2330 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002331 case ISN_COMPAREANY:
2332 {
2333 typval_T *tv1 = STACK_TV_BOT(-2);
2334 typval_T *tv2 = STACK_TV_BOT(-1);
2335 exptype_T exptype = iptr->isn_arg.op.op_type;
2336 int ic = iptr->isn_arg.op.op_ic;
2337
Bram Moolenaareb26f432020-09-14 16:50:05 +02002338 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339 typval_compare(tv1, tv2, exptype, ic);
2340 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341 --ectx.ec_stack.ga_len;
2342 }
2343 break;
2344
2345 case ISN_ADDLIST:
2346 case ISN_ADDBLOB:
2347 {
2348 typval_T *tv1 = STACK_TV_BOT(-2);
2349 typval_T *tv2 = STACK_TV_BOT(-1);
2350
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002351 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352 if (iptr->isn_type == ISN_ADDLIST)
2353 eval_addlist(tv1, tv2);
2354 else
2355 eval_addblob(tv1, tv2);
2356 clear_tv(tv2);
2357 --ectx.ec_stack.ga_len;
2358 }
2359 break;
2360
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002361 case ISN_LISTAPPEND:
2362 {
2363 typval_T *tv1 = STACK_TV_BOT(-2);
2364 typval_T *tv2 = STACK_TV_BOT(-1);
2365 list_T *l = tv1->vval.v_list;
2366
2367 // add an item to a list
2368 if (l == NULL)
2369 {
2370 SOURCING_LNUM = iptr->isn_lnum;
2371 emsg(_(e_cannot_add_to_null_list));
2372 goto on_error;
2373 }
2374 if (list_append_tv(l, tv2) == FAIL)
2375 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002376 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002377 --ectx.ec_stack.ga_len;
2378 }
2379 break;
2380
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002381 case ISN_BLOBAPPEND:
2382 {
2383 typval_T *tv1 = STACK_TV_BOT(-2);
2384 typval_T *tv2 = STACK_TV_BOT(-1);
2385 blob_T *b = tv1->vval.v_blob;
2386 int error = FALSE;
2387 varnumber_T n;
2388
2389 // add a number to a blob
2390 if (b == NULL)
2391 {
2392 SOURCING_LNUM = iptr->isn_lnum;
2393 emsg(_(e_cannot_add_to_null_blob));
2394 goto on_error;
2395 }
2396 n = tv_get_number_chk(tv2, &error);
2397 if (error)
2398 goto on_error;
2399 ga_append(&b->bv_ga, (int)n);
2400 --ectx.ec_stack.ga_len;
2401 }
2402 break;
2403
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404 // Computation with two arguments of unknown type
2405 case ISN_OPANY:
2406 {
2407 typval_T *tv1 = STACK_TV_BOT(-2);
2408 typval_T *tv2 = STACK_TV_BOT(-1);
2409 varnumber_T n1, n2;
2410#ifdef FEAT_FLOAT
2411 float_T f1 = 0, f2 = 0;
2412#endif
2413 int error = FALSE;
2414
2415 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2416 {
2417 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2418 {
2419 eval_addlist(tv1, tv2);
2420 clear_tv(tv2);
2421 --ectx.ec_stack.ga_len;
2422 break;
2423 }
2424 else if (tv1->v_type == VAR_BLOB
2425 && tv2->v_type == VAR_BLOB)
2426 {
2427 eval_addblob(tv1, tv2);
2428 clear_tv(tv2);
2429 --ectx.ec_stack.ga_len;
2430 break;
2431 }
2432 }
2433#ifdef FEAT_FLOAT
2434 if (tv1->v_type == VAR_FLOAT)
2435 {
2436 f1 = tv1->vval.v_float;
2437 n1 = 0;
2438 }
2439 else
2440#endif
2441 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002442 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 n1 = tv_get_number_chk(tv1, &error);
2444 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002445 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446#ifdef FEAT_FLOAT
2447 if (tv2->v_type == VAR_FLOAT)
2448 f1 = n1;
2449#endif
2450 }
2451#ifdef FEAT_FLOAT
2452 if (tv2->v_type == VAR_FLOAT)
2453 {
2454 f2 = tv2->vval.v_float;
2455 n2 = 0;
2456 }
2457 else
2458#endif
2459 {
2460 n2 = tv_get_number_chk(tv2, &error);
2461 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002462 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463#ifdef FEAT_FLOAT
2464 if (tv1->v_type == VAR_FLOAT)
2465 f2 = n2;
2466#endif
2467 }
2468#ifdef FEAT_FLOAT
2469 // if there is a float on either side the result is a float
2470 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2471 {
2472 switch (iptr->isn_arg.op.op_type)
2473 {
2474 case EXPR_MULT: f1 = f1 * f2; break;
2475 case EXPR_DIV: f1 = f1 / f2; break;
2476 case EXPR_SUB: f1 = f1 - f2; break;
2477 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002478 default: SOURCING_LNUM = iptr->isn_lnum;
2479 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002480 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 }
2482 clear_tv(tv1);
2483 clear_tv(tv2);
2484 tv1->v_type = VAR_FLOAT;
2485 tv1->vval.v_float = f1;
2486 --ectx.ec_stack.ga_len;
2487 }
2488 else
2489#endif
2490 {
2491 switch (iptr->isn_arg.op.op_type)
2492 {
2493 case EXPR_MULT: n1 = n1 * n2; break;
2494 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2495 case EXPR_SUB: n1 = n1 - n2; break;
2496 case EXPR_ADD: n1 = n1 + n2; break;
2497 default: n1 = num_modulus(n1, n2); break;
2498 }
2499 clear_tv(tv1);
2500 clear_tv(tv2);
2501 tv1->v_type = VAR_NUMBER;
2502 tv1->vval.v_number = n1;
2503 --ectx.ec_stack.ga_len;
2504 }
2505 }
2506 break;
2507
2508 case ISN_CONCAT:
2509 {
2510 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2511 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2512 char_u *res;
2513
2514 res = concat_str(str1, str2);
2515 clear_tv(STACK_TV_BOT(-2));
2516 clear_tv(STACK_TV_BOT(-1));
2517 --ectx.ec_stack.ga_len;
2518 STACK_TV_BOT(-1)->vval.v_string = res;
2519 }
2520 break;
2521
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002522 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002523 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002524 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002525 int is_slice = iptr->isn_type == ISN_STRSLICE;
2526 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002527 char_u *res;
2528
2529 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002530 // string slice: string is at stack-3, first index at
2531 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002532 if (is_slice)
2533 {
2534 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002535 n1 = tv->vval.v_number;
2536 }
2537
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002538 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002539 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002540
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002541 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002542 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002543 if (is_slice)
2544 // Slice: Select the characters from the string
2545 res = string_slice(tv->vval.v_string, n1, n2);
2546 else
2547 // Index: The resulting variable is a string of a
2548 // single character. If the index is too big or
2549 // negative the result is empty.
2550 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002551 vim_free(tv->vval.v_string);
2552 tv->vval.v_string = res;
2553 }
2554 break;
2555
2556 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002557 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 {
Bram Moolenaared591872020-08-15 22:14:53 +02002559 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002561 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562
2563 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002564 // list slice: list is at stack-3, indexes at stack-2 and
2565 // stack-1
2566 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 list = tv->vval.v_list;
2568
2569 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002570 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002572
2573 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 {
Bram Moolenaared591872020-08-15 22:14:53 +02002575 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002576 n1 = tv->vval.v_number;
2577 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 }
Bram Moolenaared591872020-08-15 22:14:53 +02002579
2580 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002581 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002582 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002583 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2584 == FAIL)
2585 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 }
2587 break;
2588
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002589 case ISN_ANYINDEX:
2590 case ISN_ANYSLICE:
2591 {
2592 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2593 typval_T *var1, *var2;
2594 int res;
2595
2596 // index: composite is at stack-2, index at stack-1
2597 // slice: composite is at stack-3, indexes at stack-2 and
2598 // stack-1
2599 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002600 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002601 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2602 goto on_error;
2603 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2604 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2605 res = eval_index_inner(tv, is_slice,
2606 var1, var2, NULL, -1, TRUE);
2607 clear_tv(var1);
2608 if (is_slice)
2609 clear_tv(var2);
2610 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2611 if (res == FAIL)
2612 goto on_error;
2613 }
2614 break;
2615
Bram Moolenaar9af78762020-06-16 11:34:42 +02002616 case ISN_SLICE:
2617 {
2618 list_T *list;
2619 int count = iptr->isn_arg.number;
2620
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002621 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002622 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002623 list = tv->vval.v_list;
2624
2625 // no error for short list, expect it to be checked earlier
2626 if (list != NULL && list->lv_len >= count)
2627 {
2628 list_T *newlist = list_slice(list,
2629 count, list->lv_len - 1);
2630
2631 if (newlist != NULL)
2632 {
2633 list_unref(list);
2634 tv->vval.v_list = newlist;
2635 ++newlist->lv_refcount;
2636 }
2637 }
2638 }
2639 break;
2640
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002641 case ISN_GETITEM:
2642 {
2643 listitem_T *li;
2644 int index = iptr->isn_arg.number;
2645
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002646 // Get list item: list is at stack-1, push item.
2647 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002648 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002649 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002650
2651 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2652 goto failed;
2653 ++ectx.ec_stack.ga_len;
2654 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2655 }
2656 break;
2657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 case ISN_MEMBER:
2659 {
2660 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002661 char_u *key;
2662 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002663 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002664
2665 // dict member: dict is at stack-2, key at stack-1
2666 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002667 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002668 dict = tv->vval.v_dict;
2669
2670 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002671 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002672 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01002673 if (key == NULL)
2674 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002675
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002676 if ((di = dict_find(dict, key, -1)) == NULL)
2677 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002678 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002679 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002680
2681 // If :silent! is used we will continue, make sure the
2682 // stack contents makes sense.
2683 clear_tv(tv);
2684 --ectx.ec_stack.ga_len;
2685 tv = STACK_TV_BOT(-1);
2686 clear_tv(tv);
2687 tv->v_type = VAR_NUMBER;
2688 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002689 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002690 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002691 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002692 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002693 // Clear the dict only after getting the item, to avoid
2694 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002695 tv = STACK_TV_BOT(-1);
2696 temp_tv = *tv;
2697 copy_tv(&di->di_tv, tv);
2698 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002699 }
2700 break;
2701
2702 // dict member with string key
2703 case ISN_STRINGMEMBER:
2704 {
2705 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002707 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708
2709 tv = STACK_TV_BOT(-1);
2710 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2711 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002712 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002714 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 }
2716 dict = tv->vval.v_dict;
2717
2718 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2719 == NULL)
2720 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002721 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002723 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002725 // Clear the dict after getting the item, to avoid that it
2726 // make the item invalid.
2727 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002729 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 }
2731 break;
2732
2733 case ISN_NEGATENR:
2734 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002735 if (tv->v_type != VAR_NUMBER
2736#ifdef FEAT_FLOAT
2737 && tv->v_type != VAR_FLOAT
2738#endif
2739 )
2740 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002741 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002742 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002743 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002744 }
2745#ifdef FEAT_FLOAT
2746 if (tv->v_type == VAR_FLOAT)
2747 tv->vval.v_float = -tv->vval.v_float;
2748 else
2749#endif
2750 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 break;
2752
2753 case ISN_CHECKNR:
2754 {
2755 int error = FALSE;
2756
2757 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002758 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002760 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 (void)tv_get_number_chk(tv, &error);
2762 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002763 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 }
2765 break;
2766
2767 case ISN_CHECKTYPE:
2768 {
2769 checktype_T *ct = &iptr->isn_arg.type;
2770
2771 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002772 SOURCING_LNUM = iptr->isn_lnum;
2773 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2774 goto on_error;
2775
2776 // number 0 is FALSE, number 1 is TRUE
2777 if (tv->v_type == VAR_NUMBER
2778 && ct->ct_type->tt_type == VAR_BOOL
2779 && (tv->vval.v_number == 0
2780 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002782 tv->v_type = VAR_BOOL;
2783 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002784 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 }
2786 }
2787 break;
2788
Bram Moolenaar9af78762020-06-16 11:34:42 +02002789 case ISN_CHECKLEN:
2790 {
2791 int min_len = iptr->isn_arg.checklen.cl_min_len;
2792 list_T *list = NULL;
2793
2794 tv = STACK_TV_BOT(-1);
2795 if (tv->v_type == VAR_LIST)
2796 list = tv->vval.v_list;
2797 if (list == NULL || list->lv_len < min_len
2798 || (list->lv_len > min_len
2799 && !iptr->isn_arg.checklen.cl_more_OK))
2800 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002801 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002802 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002803 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002804 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002805 }
2806 }
2807 break;
2808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002810 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 {
2812 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002813 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814
2815 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002816 if (iptr->isn_type == ISN_2BOOL)
2817 {
2818 n = tv2bool(tv);
2819 if (iptr->isn_arg.number) // invert
2820 n = !n;
2821 }
2822 else
2823 {
2824 SOURCING_LNUM = iptr->isn_lnum;
2825 n = tv_get_bool_chk(tv, &error);
2826 if (error)
2827 goto on_error;
2828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 clear_tv(tv);
2830 tv->v_type = VAR_BOOL;
2831 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2832 }
2833 break;
2834
2835 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002836 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 {
2838 char_u *str;
2839
2840 tv = STACK_TV_BOT(iptr->isn_arg.number);
2841 if (tv->v_type != VAR_STRING)
2842 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002843 if (iptr->isn_type == ISN_2STRING_ANY)
2844 {
2845 switch (tv->v_type)
2846 {
2847 case VAR_SPECIAL:
2848 case VAR_BOOL:
2849 case VAR_NUMBER:
2850 case VAR_FLOAT:
2851 case VAR_BLOB: break;
2852 default: to_string_error(tv->v_type);
2853 goto on_error;
2854 }
2855 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 str = typval_tostring(tv);
2857 clear_tv(tv);
2858 tv->v_type = VAR_STRING;
2859 tv->vval.v_string = str;
2860 }
2861 }
2862 break;
2863
Bram Moolenaar08597872020-12-10 19:43:40 +01002864 case ISN_RANGE:
2865 {
2866 exarg_T ea;
2867 char *errormsg;
2868
2869 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2870 goto failed;
2871 ++ectx.ec_stack.ga_len;
2872 tv = STACK_TV_BOT(-1);
2873 ea.addr_type = ADDR_LINES;
2874 ea.cmd = iptr->isn_arg.string;
2875 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
2876 goto failed;
2877 if (ea.addr_count == 0)
2878 tv->vval.v_number = curwin->w_cursor.lnum;
2879 else
2880 tv->vval.v_number = ea.line2;
2881 }
2882 break;
2883
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002884 case ISN_PUT:
2885 {
2886 int regname = iptr->isn_arg.put.put_regname;
2887 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2888 char_u *expr = NULL;
2889 int dir = FORWARD;
2890
2891 if (regname == '=')
2892 {
2893 tv = STACK_TV_BOT(-1);
2894 if (tv->v_type == VAR_STRING)
2895 expr = tv->vval.v_string;
2896 else
2897 {
2898 expr = typval_tostring(tv); // allocates value
2899 clear_tv(tv);
2900 }
2901 --ectx.ec_stack.ga_len;
2902 }
Bram Moolenaar08597872020-12-10 19:43:40 +01002903 if (lnum < -2)
2904 {
2905 // line number was put on the stack by ISN_RANGE
2906 tv = STACK_TV_BOT(-1);
2907 curwin->w_cursor.lnum = tv->vval.v_number;
2908 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
2909 dir = BACKWARD;
2910 --ectx.ec_stack.ga_len;
2911 }
2912 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002913 // :put! above cursor
2914 dir = BACKWARD;
2915 else if (lnum >= 0)
2916 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2917 check_cursor();
2918 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2919 vim_free(expr);
2920 }
2921 break;
2922
Bram Moolenaar02194d22020-10-24 23:08:38 +02002923 case ISN_CMDMOD:
2924 save_cmdmod = cmdmod;
2925 restore_cmdmod = TRUE;
2926 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
2927 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002928 break;
2929
Bram Moolenaar02194d22020-10-24 23:08:38 +02002930 case ISN_CMDMOD_REV:
2931 // filter regprog is owned by the instruction, don't free it
2932 cmdmod.cmod_filter_regmatch.regprog = NULL;
2933 undo_cmdmod(&cmdmod);
2934 cmdmod = save_cmdmod;
2935 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002936 break;
2937
Bram Moolenaar792f7862020-11-23 08:31:18 +01002938 case ISN_UNPACK:
2939 {
2940 int count = iptr->isn_arg.unpack.unp_count;
2941 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
2942 list_T *l;
2943 listitem_T *li;
2944 int i;
2945
2946 // Check there is a valid list to unpack.
2947 tv = STACK_TV_BOT(-1);
2948 if (tv->v_type != VAR_LIST)
2949 {
2950 SOURCING_LNUM = iptr->isn_lnum;
2951 emsg(_(e_for_argument_must_be_sequence_of_lists));
2952 goto on_error;
2953 }
2954 l = tv->vval.v_list;
2955 if (l == NULL
2956 || l->lv_len < (semicolon ? count - 1 : count))
2957 {
2958 SOURCING_LNUM = iptr->isn_lnum;
2959 emsg(_(e_list_value_does_not_have_enough_items));
2960 goto on_error;
2961 }
2962 else if (!semicolon && l->lv_len > count)
2963 {
2964 SOURCING_LNUM = iptr->isn_lnum;
2965 emsg(_(e_list_value_has_more_items_than_targets));
2966 goto on_error;
2967 }
2968
2969 CHECK_LIST_MATERIALIZE(l);
2970 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
2971 goto failed;
2972 ectx.ec_stack.ga_len += count - 1;
2973
2974 // Variable after semicolon gets a list with the remaining
2975 // items.
2976 if (semicolon)
2977 {
2978 list_T *rem_list =
2979 list_alloc_with_items(l->lv_len - count + 1);
2980
2981 if (rem_list == NULL)
2982 goto failed;
2983 tv = STACK_TV_BOT(-count);
2984 tv->vval.v_list = rem_list;
2985 ++rem_list->lv_refcount;
2986 tv->v_lock = 0;
2987 li = l->lv_first;
2988 for (i = 0; i < count - 1; ++i)
2989 li = li->li_next;
2990 for (i = 0; li != NULL; ++i)
2991 {
2992 list_set_item(rem_list, i, &li->li_tv);
2993 li = li->li_next;
2994 }
2995 --count;
2996 }
2997
2998 // Produce the values in reverse order, first item last.
2999 li = l->lv_first;
3000 for (i = 0; i < count; ++i)
3001 {
3002 tv = STACK_TV_BOT(-i - 1);
3003 copy_tv(&li->li_tv, tv);
3004 li = li->li_next;
3005 }
3006
3007 list_unref(l);
3008 }
3009 break;
3010
Bram Moolenaar389df252020-07-09 21:20:47 +02003011 case ISN_SHUFFLE:
3012 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003013 typval_T tmp_tv;
3014 int item = iptr->isn_arg.shuffle.shfl_item;
3015 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003016
3017 tmp_tv = *STACK_TV_BOT(-item);
3018 for ( ; up > 0 && item > 1; --up)
3019 {
3020 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3021 --item;
3022 }
3023 *STACK_TV_BOT(-item) = tmp_tv;
3024 }
3025 break;
3026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 case ISN_DROP:
3028 --ectx.ec_stack.ga_len;
3029 clear_tv(STACK_TV_BOT(0));
3030 break;
3031 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003032 continue;
3033
Bram Moolenaard032f342020-07-18 18:13:02 +02003034func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003035 // Restore previous function. If the frame pointer is where we started
3036 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003037 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003038 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003039
Bram Moolenaard032f342020-07-18 18:13:02 +02003040 if (func_return(&ectx) == FAIL)
3041 // only fails when out of memory
3042 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003043 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003044
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003045on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003046 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003047 // If "emsg_silent" is set then ignore the error, unless it was set
3048 // when calling the function.
3049 if (did_emsg_cumul + did_emsg == did_emsg_before
3050 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003051 continue;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003052on_fatal_error:
3053 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003054 // If we are not inside a try-catch started here, abort execution.
3055 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003056 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 }
3058
3059done:
3060 // function finished, get result from the stack.
3061 tv = STACK_TV_BOT(-1);
3062 *rettv = *tv;
3063 tv->v_type = VAR_UNKNOWN;
3064 ret = OK;
3065
3066failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003067 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003068 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003069 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003070
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003071 // Deal with any remaining closures, they may be in use somewhere.
3072 if (ectx.ec_funcrefs.ga_len > 0)
3073 handle_closure_in_use(&ectx, FALSE);
3074
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003075 estack_pop();
3076 current_sctx = save_current_sctx;
3077
Bram Moolenaar352134b2020-10-17 22:04:08 +02003078 if (*msg_list != NULL && saved_msg_list != NULL)
3079 {
3080 msglist_T **plist = saved_msg_list;
3081
3082 // Append entries from the current msg_list (uncaught exceptions) to
3083 // the saved msg_list.
3084 while (*plist != NULL)
3085 plist = &(*plist)->next;
3086
3087 *plist = *msg_list;
3088 }
3089 msg_list = saved_msg_list;
3090
Bram Moolenaar02194d22020-10-24 23:08:38 +02003091 if (restore_cmdmod)
3092 {
3093 cmdmod.cmod_filter_regmatch.regprog = NULL;
3094 undo_cmdmod(&cmdmod);
3095 cmdmod = save_cmdmod;
3096 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003097 emsg_silent_def = save_emsg_silent_def;
3098 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003099
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003100failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003101 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3103 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003106 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003107
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003108 // Not sure if this is necessary.
3109 suppress_errthrow = save_suppress_errthrow;
3110
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003111 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003112 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003113 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003114 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 return ret;
3116}
3117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118/*
3119 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003120 * We don't really need this at runtime, but we do have tests that require it,
3121 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 */
3123 void
3124ex_disassemble(exarg_T *eap)
3125{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003126 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003127 char_u *fname;
3128 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 dfunc_T *dfunc;
3130 isn_T *instr;
3131 int current;
3132 int line_idx = 0;
3133 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003134 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003136 if (STRNCMP(arg, "<lambda>", 8) == 0)
3137 {
3138 arg += 8;
3139 (void)getdigits(&arg);
3140 fname = vim_strnsave(eap->arg, arg - eap->arg);
3141 }
3142 else
3143 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003144 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003145 if (fname == NULL)
3146 {
3147 semsg(_(e_invarg2), eap->arg);
3148 return;
3149 }
3150
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003151 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003152 if (ufunc == NULL)
3153 {
3154 char_u *p = untrans_function_name(fname);
3155
3156 if (p != NULL)
3157 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003158 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003159 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003160 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 if (ufunc == NULL)
3162 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003163 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 return;
3165 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003166 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003167 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3168 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003169 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003171 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 return;
3173 }
3174 if (ufunc->uf_name_exp != NULL)
3175 msg((char *)ufunc->uf_name_exp);
3176 else
3177 msg((char *)ufunc->uf_name);
3178
3179 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3180 instr = dfunc->df_instr;
3181 for (current = 0; current < dfunc->df_instr_count; ++current)
3182 {
3183 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003184 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185
3186 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3187 {
3188 if (current > prev_current)
3189 {
3190 msg_puts("\n\n");
3191 prev_current = current;
3192 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003193 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3194 if (line != NULL)
3195 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 }
3197
3198 switch (iptr->isn_type)
3199 {
3200 case ISN_EXEC:
3201 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3202 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003203 case ISN_EXECCONCAT:
3204 smsg("%4d EXECCONCAT %lld", current,
3205 (long long)iptr->isn_arg.number);
3206 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 case ISN_ECHO:
3208 {
3209 echo_T *echo = &iptr->isn_arg.echo;
3210
3211 smsg("%4d %s %d", current,
3212 echo->echo_with_white ? "ECHO" : "ECHON",
3213 echo->echo_count);
3214 }
3215 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003216 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003217 smsg("%4d EXECUTE %lld", current,
3218 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003219 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003220 case ISN_ECHOMSG:
3221 smsg("%4d ECHOMSG %lld", current,
3222 (long long)(iptr->isn_arg.number));
3223 break;
3224 case ISN_ECHOERR:
3225 smsg("%4d ECHOERR %lld", current,
3226 (long long)(iptr->isn_arg.number));
3227 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003229 case ISN_LOADOUTER:
3230 {
3231 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
3232
3233 if (iptr->isn_arg.number < 0)
3234 smsg("%4d LOAD%s arg[%lld]", current, add,
3235 (long long)(iptr->isn_arg.number
3236 + STACK_FRAME_SIZE));
3237 else
3238 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003239 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 break;
3242 case ISN_LOADV:
3243 smsg("%4d LOADV v:%s", current,
3244 get_vim_var_name(iptr->isn_arg.number));
3245 break;
3246 case ISN_LOADSCRIPT:
3247 {
3248 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003249 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3251 + iptr->isn_arg.script.script_idx;
3252
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003253 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3254 sv->sv_name,
3255 iptr->isn_arg.script.script_idx,
3256 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 }
3258 break;
3259 case ISN_LOADS:
3260 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003261 scriptitem_T *si = SCRIPT_ITEM(
3262 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263
3264 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003265 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 }
3267 break;
3268 case ISN_LOADG:
3269 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3270 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003271 case ISN_LOADB:
3272 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3273 break;
3274 case ISN_LOADW:
3275 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3276 break;
3277 case ISN_LOADT:
3278 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3279 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003280 case ISN_LOADGDICT:
3281 smsg("%4d LOAD g:", current);
3282 break;
3283 case ISN_LOADBDICT:
3284 smsg("%4d LOAD b:", current);
3285 break;
3286 case ISN_LOADWDICT:
3287 smsg("%4d LOAD w:", current);
3288 break;
3289 case ISN_LOADTDICT:
3290 smsg("%4d LOAD t:", current);
3291 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 case ISN_LOADOPT:
3293 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3294 break;
3295 case ISN_LOADENV:
3296 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3297 break;
3298 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003299 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 break;
3301
3302 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003303 case ISN_STOREOUTER:
3304 {
3305 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
3306
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003307 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003308 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003309 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003310 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003311 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003312 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003313 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003315 case ISN_STOREV:
3316 smsg("%4d STOREV v:%s", current,
3317 get_vim_var_name(iptr->isn_arg.number));
3318 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003320 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3321 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003322 case ISN_STOREB:
3323 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3324 break;
3325 case ISN_STOREW:
3326 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3327 break;
3328 case ISN_STORET:
3329 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3330 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003331 case ISN_STORES:
3332 {
3333 scriptitem_T *si = SCRIPT_ITEM(
3334 iptr->isn_arg.loadstore.ls_sid);
3335
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003336 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003337 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003338 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 break;
3340 case ISN_STORESCRIPT:
3341 {
3342 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003343 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3345 + iptr->isn_arg.script.script_idx;
3346
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003347 smsg("%4d STORESCRIPT %s-%d in %s", current,
3348 sv->sv_name,
3349 iptr->isn_arg.script.script_idx,
3350 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 }
3352 break;
3353 case ISN_STOREOPT:
3354 smsg("%4d STOREOPT &%s", current,
3355 iptr->isn_arg.storeopt.so_name);
3356 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003357 case ISN_STOREENV:
3358 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3359 break;
3360 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003361 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003362 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 case ISN_STORENR:
3364 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003365 iptr->isn_arg.storenr.stnr_val,
3366 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 break;
3368
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003369 case ISN_STORELIST:
3370 smsg("%4d STORELIST", current);
3371 break;
3372
3373 case ISN_STOREDICT:
3374 smsg("%4d STOREDICT", current);
3375 break;
3376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 // constants
3378 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003379 smsg("%4d PUSHNR %lld", current,
3380 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 break;
3382 case ISN_PUSHBOOL:
3383 case ISN_PUSHSPEC:
3384 smsg("%4d PUSH %s", current,
3385 get_var_special_name(iptr->isn_arg.number));
3386 break;
3387 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003388#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003390#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 break;
3392 case ISN_PUSHS:
3393 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3394 break;
3395 case ISN_PUSHBLOB:
3396 {
3397 char_u *r;
3398 char_u numbuf[NUMBUFLEN];
3399 char_u *tofree;
3400
3401 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003402 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 vim_free(tofree);
3404 }
3405 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003406 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003407 {
3408 char *name = (char *)iptr->isn_arg.string;
3409
3410 smsg("%4d PUSHFUNC \"%s\"", current,
3411 name == NULL ? "[none]" : name);
3412 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003413 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003414 case ISN_PUSHCHANNEL:
3415#ifdef FEAT_JOB_CHANNEL
3416 {
3417 channel_T *channel = iptr->isn_arg.channel;
3418
3419 smsg("%4d PUSHCHANNEL %d", current,
3420 channel == NULL ? 0 : channel->ch_id);
3421 }
3422#endif
3423 break;
3424 case ISN_PUSHJOB:
3425#ifdef FEAT_JOB_CHANNEL
3426 {
3427 typval_T tv;
3428 char_u *name;
3429
3430 tv.v_type = VAR_JOB;
3431 tv.vval.v_job = iptr->isn_arg.job;
3432 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003433 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003434 }
3435#endif
3436 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 case ISN_PUSHEXC:
3438 smsg("%4d PUSH v:exception", current);
3439 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003440 case ISN_UNLET:
3441 smsg("%4d UNLET%s %s", current,
3442 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3443 iptr->isn_arg.unlet.ul_name);
3444 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003445 case ISN_UNLETENV:
3446 smsg("%4d UNLETENV%s $%s", current,
3447 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3448 iptr->isn_arg.unlet.ul_name);
3449 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003450 case ISN_LOCKCONST:
3451 smsg("%4d LOCKCONST", current);
3452 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003454 smsg("%4d NEWLIST size %lld", current,
3455 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 break;
3457 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003458 smsg("%4d NEWDICT size %lld", current,
3459 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 break;
3461
3462 // function call
3463 case ISN_BCALL:
3464 {
3465 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3466
3467 smsg("%4d BCALL %s(argc %d)", current,
3468 internal_func_name(cbfunc->cbf_idx),
3469 cbfunc->cbf_argcount);
3470 }
3471 break;
3472 case ISN_DCALL:
3473 {
3474 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3475 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3476 + cdfunc->cdf_idx;
3477
3478 smsg("%4d DCALL %s(argc %d)", current,
3479 df->df_ufunc->uf_name_exp != NULL
3480 ? df->df_ufunc->uf_name_exp
3481 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3482 }
3483 break;
3484 case ISN_UCALL:
3485 {
3486 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3487
3488 smsg("%4d UCALL %s(argc %d)", current,
3489 cufunc->cuf_name, cufunc->cuf_argcount);
3490 }
3491 break;
3492 case ISN_PCALL:
3493 {
3494 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3495
3496 smsg("%4d PCALL%s (argc %d)", current,
3497 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3498 }
3499 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003500 case ISN_PCALL_END:
3501 smsg("%4d PCALL end", current);
3502 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 case ISN_RETURN:
3504 smsg("%4d RETURN", current);
3505 break;
3506 case ISN_FUNCREF:
3507 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003508 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003510 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003512 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 }
3514 break;
3515
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003516 case ISN_NEWFUNC:
3517 {
3518 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3519
3520 smsg("%4d NEWFUNC %s %s", current,
3521 newfunc->nf_lambda, newfunc->nf_global);
3522 }
3523 break;
3524
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003525 case ISN_DEF:
3526 {
3527 char_u *name = iptr->isn_arg.string;
3528
3529 smsg("%4d DEF %s", current,
3530 name == NULL ? (char_u *)"" : name);
3531 }
3532 break;
3533
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 case ISN_JUMP:
3535 {
3536 char *when = "?";
3537
3538 switch (iptr->isn_arg.jump.jump_when)
3539 {
3540 case JUMP_ALWAYS:
3541 when = "JUMP";
3542 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 case JUMP_AND_KEEP_IF_TRUE:
3544 when = "JUMP_AND_KEEP_IF_TRUE";
3545 break;
3546 case JUMP_IF_FALSE:
3547 when = "JUMP_IF_FALSE";
3548 break;
3549 case JUMP_AND_KEEP_IF_FALSE:
3550 when = "JUMP_AND_KEEP_IF_FALSE";
3551 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003552 case JUMP_IF_COND_FALSE:
3553 when = "JUMP_IF_COND_FALSE";
3554 break;
3555 case JUMP_IF_COND_TRUE:
3556 when = "JUMP_IF_COND_TRUE";
3557 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003559 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 iptr->isn_arg.jump.jump_where);
3561 }
3562 break;
3563
3564 case ISN_FOR:
3565 {
3566 forloop_T *forloop = &iptr->isn_arg.forloop;
3567
3568 smsg("%4d FOR $%d -> %d", current,
3569 forloop->for_idx, forloop->for_end);
3570 }
3571 break;
3572
3573 case ISN_TRY:
3574 {
3575 try_T *try = &iptr->isn_arg.try;
3576
3577 smsg("%4d TRY catch -> %d, finally -> %d", current,
3578 try->try_catch, try->try_finally);
3579 }
3580 break;
3581 case ISN_CATCH:
3582 // TODO
3583 smsg("%4d CATCH", current);
3584 break;
3585 case ISN_ENDTRY:
3586 smsg("%4d ENDTRY", current);
3587 break;
3588 case ISN_THROW:
3589 smsg("%4d THROW", current);
3590 break;
3591
3592 // expression operations on number
3593 case ISN_OPNR:
3594 case ISN_OPFLOAT:
3595 case ISN_OPANY:
3596 {
3597 char *what;
3598 char *ins;
3599
3600 switch (iptr->isn_arg.op.op_type)
3601 {
3602 case EXPR_MULT: what = "*"; break;
3603 case EXPR_DIV: what = "/"; break;
3604 case EXPR_REM: what = "%"; break;
3605 case EXPR_SUB: what = "-"; break;
3606 case EXPR_ADD: what = "+"; break;
3607 default: what = "???"; break;
3608 }
3609 switch (iptr->isn_type)
3610 {
3611 case ISN_OPNR: ins = "OPNR"; break;
3612 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3613 case ISN_OPANY: ins = "OPANY"; break;
3614 default: ins = "???"; break;
3615 }
3616 smsg("%4d %s %s", current, ins, what);
3617 }
3618 break;
3619
3620 case ISN_COMPAREBOOL:
3621 case ISN_COMPARESPECIAL:
3622 case ISN_COMPARENR:
3623 case ISN_COMPAREFLOAT:
3624 case ISN_COMPARESTRING:
3625 case ISN_COMPAREBLOB:
3626 case ISN_COMPARELIST:
3627 case ISN_COMPAREDICT:
3628 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 case ISN_COMPAREANY:
3630 {
3631 char *p;
3632 char buf[10];
3633 char *type;
3634
3635 switch (iptr->isn_arg.op.op_type)
3636 {
3637 case EXPR_EQUAL: p = "=="; break;
3638 case EXPR_NEQUAL: p = "!="; break;
3639 case EXPR_GREATER: p = ">"; break;
3640 case EXPR_GEQUAL: p = ">="; break;
3641 case EXPR_SMALLER: p = "<"; break;
3642 case EXPR_SEQUAL: p = "<="; break;
3643 case EXPR_MATCH: p = "=~"; break;
3644 case EXPR_IS: p = "is"; break;
3645 case EXPR_ISNOT: p = "isnot"; break;
3646 case EXPR_NOMATCH: p = "!~"; break;
3647 default: p = "???"; break;
3648 }
3649 STRCPY(buf, p);
3650 if (iptr->isn_arg.op.op_ic == TRUE)
3651 strcat(buf, "?");
3652 switch(iptr->isn_type)
3653 {
3654 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3655 case ISN_COMPARESPECIAL:
3656 type = "COMPARESPECIAL"; break;
3657 case ISN_COMPARENR: type = "COMPARENR"; break;
3658 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3659 case ISN_COMPARESTRING:
3660 type = "COMPARESTRING"; break;
3661 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3662 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3663 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3664 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3666 default: type = "???"; break;
3667 }
3668
3669 smsg("%4d %s %s", current, type, buf);
3670 }
3671 break;
3672
3673 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3674 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3675
3676 // expression operations
3677 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003678 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003679 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003680 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003681 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003682 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003683 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003684 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3685 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003686 case ISN_SLICE: smsg("%4d SLICE %lld",
3687 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003688 case ISN_GETITEM: smsg("%4d ITEM %lld",
3689 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003690 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3691 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 iptr->isn_arg.string); break;
3693 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3694
3695 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003696 case ISN_CHECKTYPE:
3697 {
3698 char *tofree;
3699
3700 smsg("%4d CHECKTYPE %s stack[%d]", current,
3701 type_name(iptr->isn_arg.type.ct_type, &tofree),
3702 iptr->isn_arg.type.ct_off);
3703 vim_free(tofree);
3704 break;
3705 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003706 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3707 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3708 iptr->isn_arg.checklen.cl_min_len);
3709 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003710 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 case ISN_2BOOL: if (iptr->isn_arg.number)
3712 smsg("%4d INVERT (!val)", current);
3713 else
3714 smsg("%4d 2BOOL (!!val)", current);
3715 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003716 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3717 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003718 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003719 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3720 (long long)(iptr->isn_arg.number));
3721 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01003722 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
3723 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003724 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01003725 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
3726 smsg("%4d PUT %c above range",
3727 current, iptr->isn_arg.put.put_regname);
3728 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
3729 smsg("%4d PUT %c range",
3730 current, iptr->isn_arg.put.put_regname);
3731 else
3732 smsg("%4d PUT %c %ld", current,
3733 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003734 (long)iptr->isn_arg.put.put_lnum);
3735 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736
Bram Moolenaar02194d22020-10-24 23:08:38 +02003737 // TODO: summarize modifiers
3738 case ISN_CMDMOD:
3739 {
3740 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01003741 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02003742 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3743
3744 buf = alloc(len + 1);
3745 if (buf != NULL)
3746 {
3747 (void)produce_cmdmods(
3748 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3749 smsg("%4d CMDMOD %s", current, buf);
3750 vim_free(buf);
3751 }
3752 break;
3753 }
3754 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003755
Bram Moolenaar792f7862020-11-23 08:31:18 +01003756 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
3757 iptr->isn_arg.unpack.unp_count,
3758 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
3759 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02003760 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3761 iptr->isn_arg.shuffle.shfl_item,
3762 iptr->isn_arg.shuffle.shfl_up);
3763 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 case ISN_DROP: smsg("%4d DROP", current); break;
3765 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003766
3767 out_flush(); // output one line at a time
3768 ui_breakcheck();
3769 if (got_int)
3770 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772}
3773
3774/*
Bram Moolenaar13106602020-10-04 16:06:05 +02003775 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 * list, etc. Mostly like what JavaScript does, except that empty list and
3777 * empty dictionary are FALSE.
3778 */
3779 int
3780tv2bool(typval_T *tv)
3781{
3782 switch (tv->v_type)
3783 {
3784 case VAR_NUMBER:
3785 return tv->vval.v_number != 0;
3786 case VAR_FLOAT:
3787#ifdef FEAT_FLOAT
3788 return tv->vval.v_float != 0.0;
3789#else
3790 break;
3791#endif
3792 case VAR_PARTIAL:
3793 return tv->vval.v_partial != NULL;
3794 case VAR_FUNC:
3795 case VAR_STRING:
3796 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3797 case VAR_LIST:
3798 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3799 case VAR_DICT:
3800 return tv->vval.v_dict != NULL
3801 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3802 case VAR_BOOL:
3803 case VAR_SPECIAL:
3804 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3805 case VAR_JOB:
3806#ifdef FEAT_JOB_CHANNEL
3807 return tv->vval.v_job != NULL;
3808#else
3809 break;
3810#endif
3811 case VAR_CHANNEL:
3812#ifdef FEAT_JOB_CHANNEL
3813 return tv->vval.v_channel != NULL;
3814#else
3815 break;
3816#endif
3817 case VAR_BLOB:
3818 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3819 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003820 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 case VAR_VOID:
3822 break;
3823 }
3824 return FALSE;
3825}
3826
Bram Moolenaarea2d4072020-11-12 12:08:51 +01003827 void
3828emsg_using_string_as(typval_T *tv, int as_number)
3829{
3830 semsg(_(as_number ? e_using_string_as_number_str
3831 : e_using_string_as_bool_str),
3832 tv->vval.v_string == NULL
3833 ? (char_u *)"" : tv->vval.v_string);
3834}
3835
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836/*
3837 * If "tv" is a string give an error and return FAIL.
3838 */
3839 int
3840check_not_string(typval_T *tv)
3841{
3842 if (tv->v_type == VAR_STRING)
3843 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01003844 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 clear_tv(tv);
3846 return FAIL;
3847 }
3848 return OK;
3849}
3850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851
3852#endif // FEAT_EVAL