blob: 980ebb51a855bb520a9c8e3f9bb923a27c6c373f [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 Moolenaar171fb922020-10-28 16:54:47 +0100854 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100855 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856
857// Get pointer to item in the stack.
858#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
859
860// Get pointer to item at the bottom of the stack, -1 is the bottom.
861#undef STACK_TV_BOT
862#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
863
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200864// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200865#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 +0100866
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200867// Like STACK_TV_VAR but use the outer scope
868#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
869
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200870 if (ufunc->uf_def_status == UF_NOT_COMPILED
871 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200872 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200873 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100874 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200875 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200876 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200877 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200878 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200879
Bram Moolenaar09689a02020-05-09 22:50:08 +0200880 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200881 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200882 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
883 + ufunc->uf_dfunc_idx;
884 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200885 {
886 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200887 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200888 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200889 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100891 // If depth of calling is getting too high, don't execute the function.
892 orig_funcdepth = funcdepth_get();
893 if (funcdepth_increment() == FAIL)
894 return FAIL;
895
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200896 CLEAR_FIELD(ectx);
897 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
898 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
899 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100900 {
901 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200902 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100903 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200905 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906
907 // Put arguments on the stack.
908 for (idx = 0; idx < argc; ++idx)
909 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200910 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200911 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
912 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200913 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 copy_tv(&argv[idx], STACK_TV_BOT(0));
915 ++ectx.ec_stack.ga_len;
916 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200917
918 // Turn varargs into a list. Empty list if no args.
919 if (ufunc->uf_va_name != NULL)
920 {
921 int vararg_count = argc - ufunc->uf_args.ga_len;
922
923 if (vararg_count < 0)
924 vararg_count = 0;
925 else
926 argc -= vararg_count;
927 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200928 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200929
930 // Check the type of the list items.
931 tv = STACK_TV_BOT(-1);
932 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +0200933 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200934 && ufunc->uf_va_type->tt_member != &t_any
935 && tv->vval.v_list != NULL)
936 {
937 type_T *expected = ufunc->uf_va_type->tt_member;
938 listitem_T *li = tv->vval.v_list->lv_first;
939
940 for (idx = 0; idx < vararg_count; ++idx)
941 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200942 if (check_typval_type(expected, &li->li_tv,
943 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200944 goto failed_early;
945 li = li->li_next;
946 }
947 }
948
Bram Moolenaar23e03252020-04-12 22:22:31 +0200949 if (defcount > 0)
950 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200951 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200952 --ectx.ec_stack.ga_len;
953 }
954
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100955 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200956 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100957 if (defcount > 0)
958 for (idx = 0; idx < defcount; ++idx)
959 {
960 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
961 ++ectx.ec_stack.ga_len;
962 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200963 if (ufunc->uf_va_name != NULL)
964 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100965
966 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200967 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
968 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200970 if (partial != NULL)
971 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200972 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
973 {
974 // TODO: is this always the right way?
975 ectx.ec_outer_stack = &current_ectx->ec_stack;
976 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
977 }
978 else
979 {
980 ectx.ec_outer_stack = partial->pt_ectx_stack;
981 ectx.ec_outer_frame = partial->pt_ectx_frame;
982 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200983 }
984
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 // dummy frame entries
986 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
987 {
988 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
989 ++ectx.ec_stack.ga_len;
990 }
991
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200992 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200993 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200994 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
995 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200997 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200998 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200999 ectx.ec_stack.ga_len += dfunc->df_varcount;
1000 if (dfunc->df_has_closure)
1001 {
1002 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1003 STACK_TV_VAR(idx)->vval.v_number = 0;
1004 ++ectx.ec_stack.ga_len;
1005 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001006
1007 ectx.ec_instr = dfunc->df_instr;
1008 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001009
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001010 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001011 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001012 estack_push_ufunc(ufunc, 1);
1013 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001014 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1015
Bram Moolenaar352134b2020-10-17 22:04:08 +02001016 // Use a specific location for storing error messages to be converted to an
1017 // exception.
1018 saved_msg_list = msg_list;
1019 msg_list = &private_msg_list;
1020
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001021 // Do turn errors into exceptions.
1022 suppress_errthrow = FALSE;
1023
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001024 // Decide where to start execution, handles optional arguments.
1025 init_instr_idx(ufunc, argc, &ectx);
1026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001027 for (;;)
1028 {
1029 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001030
Bram Moolenaar270d0382020-05-15 21:42:53 +02001031 if (++breakcheck_count >= 100)
1032 {
1033 line_breakcheck();
1034 breakcheck_count = 0;
1035 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001036 if (got_int)
1037 {
1038 // Turn CTRL-C into an exception.
1039 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001040 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001041 goto failed;
1042 did_throw = TRUE;
1043 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044
Bram Moolenaara26b9702020-04-18 19:53:28 +02001045 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1046 {
1047 // Turn an error message into an exception.
1048 did_emsg = FALSE;
1049 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1050 goto failed;
1051 did_throw = TRUE;
1052 *msg_list = NULL;
1053 }
1054
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 if (did_throw && !ectx.ec_in_catch)
1056 {
1057 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001058 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059
1060 // An exception jumps to the first catch, finally, or returns from
1061 // the current function.
1062 if (trystack->ga_len > 0)
1063 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001064 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 {
1066 // jump to ":catch" or ":finally"
1067 ectx.ec_in_catch = TRUE;
1068 ectx.ec_iidx = trycmd->tcd_catch_idx;
1069 }
1070 else
1071 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001072 // Not inside try or need to return from current functions.
1073 // Push a dummy return value.
1074 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1075 goto failed;
1076 tv = STACK_TV_BOT(0);
1077 tv->v_type = VAR_NUMBER;
1078 tv->vval.v_number = 0;
1079 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001080 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001082 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001083 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001084 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1085 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086 goto done;
1087 }
1088
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001089 if (func_return(&ectx) == FAIL)
1090 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091 }
1092 continue;
1093 }
1094
1095 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1096 switch (iptr->isn_type)
1097 {
1098 // execute Ex command line
1099 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001100 {
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001101 SOURCING_LNUM = iptr->isn_lnum;
1102 do_cmdline_cmd(iptr->isn_arg.string);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001103 if (did_emsg)
1104 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 break;
1107
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001108 // execute Ex command from pieces on the stack
1109 case ISN_EXECCONCAT:
1110 {
1111 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001112 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001113 int pass;
1114 int i;
1115 char_u *cmd = NULL;
1116 char_u *str;
1117
1118 for (pass = 1; pass <= 2; ++pass)
1119 {
1120 for (i = 0; i < count; ++i)
1121 {
1122 tv = STACK_TV_BOT(i - count);
1123 str = tv->vval.v_string;
1124 if (str != NULL && *str != NUL)
1125 {
1126 if (pass == 2)
1127 STRCPY(cmd + len, str);
1128 len += STRLEN(str);
1129 }
1130 if (pass == 2)
1131 clear_tv(tv);
1132 }
1133 if (pass == 1)
1134 {
1135 cmd = alloc(len + 1);
1136 if (cmd == NULL)
1137 goto failed;
1138 len = 0;
1139 }
1140 }
1141
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001142 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001143 do_cmdline_cmd(cmd);
1144 vim_free(cmd);
1145 }
1146 break;
1147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 // execute :echo {string} ...
1149 case ISN_ECHO:
1150 {
1151 int count = iptr->isn_arg.echo.echo_count;
1152 int atstart = TRUE;
1153 int needclr = TRUE;
1154
1155 for (idx = 0; idx < count; ++idx)
1156 {
1157 tv = STACK_TV_BOT(idx - count);
1158 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1159 &atstart, &needclr);
1160 clear_tv(tv);
1161 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001162 if (needclr)
1163 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001164 ectx.ec_stack.ga_len -= count;
1165 }
1166 break;
1167
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001168 // :execute {string} ...
1169 // :echomsg {string} ...
1170 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001171 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001172 case ISN_ECHOMSG:
1173 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001174 {
1175 int count = iptr->isn_arg.number;
1176 garray_T ga;
1177 char_u buf[NUMBUFLEN];
1178 char_u *p;
1179 int len;
1180 int failed = FALSE;
1181
1182 ga_init2(&ga, 1, 80);
1183 for (idx = 0; idx < count; ++idx)
1184 {
1185 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001186 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001187 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001188 if (tv->v_type == VAR_CHANNEL
1189 || tv->v_type == VAR_JOB)
1190 {
1191 SOURCING_LNUM = iptr->isn_lnum;
1192 emsg(_(e_inval_string));
1193 break;
1194 }
1195 else
1196 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001197 }
1198 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001199 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001200
1201 len = (int)STRLEN(p);
1202 if (ga_grow(&ga, len + 2) == FAIL)
1203 failed = TRUE;
1204 else
1205 {
1206 if (ga.ga_len > 0)
1207 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1208 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1209 ga.ga_len += len;
1210 }
1211 clear_tv(tv);
1212 }
1213 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001214 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001215 {
1216 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001217 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001218 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001219
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001220 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001221 {
1222 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001223 {
1224 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001225 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001226 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001227 {
1228 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001229 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001230 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001231 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001232 else
1233 {
1234 msg_sb_eol();
1235 if (iptr->isn_type == ISN_ECHOMSG)
1236 {
1237 msg_attr(ga.ga_data, echo_attr);
1238 out_flush();
1239 }
1240 else
1241 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001242 SOURCING_LNUM = iptr->isn_lnum;
1243 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001244 }
1245 }
1246 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001247 ga_clear(&ga);
1248 }
1249 break;
1250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 // load local variable or argument
1252 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001253 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254 goto failed;
1255 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1256 ++ectx.ec_stack.ga_len;
1257 break;
1258
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001259 // load variable or argument from outer scope
1260 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001261 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001262 goto failed;
1263 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1264 STACK_TV_BOT(0));
1265 ++ectx.ec_stack.ga_len;
1266 break;
1267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 // load v: variable
1269 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001270 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 goto failed;
1272 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1273 ++ectx.ec_stack.ga_len;
1274 break;
1275
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001276 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 case ISN_LOADSCRIPT:
1278 {
1279 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001280 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 svar_T *sv;
1282
1283 sv = ((svar_T *)si->sn_var_vals.ga_data)
1284 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001285 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001286 goto failed;
1287 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1288 ++ectx.ec_stack.ga_len;
1289 }
1290 break;
1291
1292 // load s: variable in old script
1293 case ISN_LOADS:
1294 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001295 hashtab_T *ht = &SCRIPT_VARS(
1296 iptr->isn_arg.loadstore.ls_sid);
1297 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300 if (di == NULL)
1301 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001302 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001303 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001304 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 }
1306 else
1307 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001308 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 goto failed;
1310 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1311 ++ectx.ec_stack.ga_len;
1312 }
1313 }
1314 break;
1315
Bram Moolenaard3aac292020-04-19 14:32:17 +02001316 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001318 case ISN_LOADB:
1319 case ISN_LOADW:
1320 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001322 dictitem_T *di = NULL;
1323 hashtab_T *ht = NULL;
1324 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001325
Bram Moolenaard3aac292020-04-19 14:32:17 +02001326 switch (iptr->isn_type)
1327 {
1328 case ISN_LOADG:
1329 ht = get_globvar_ht();
1330 namespace = 'g';
1331 break;
1332 case ISN_LOADB:
1333 ht = &curbuf->b_vars->dv_hashtab;
1334 namespace = 'b';
1335 break;
1336 case ISN_LOADW:
1337 ht = &curwin->w_vars->dv_hashtab;
1338 namespace = 'w';
1339 break;
1340 case ISN_LOADT:
1341 ht = &curtab->tp_vars->dv_hashtab;
1342 namespace = 't';
1343 break;
1344 default: // Cannot reach here
1345 goto failed;
1346 }
1347 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001348
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 if (di == NULL)
1350 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001351 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001352 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001353 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001354 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 }
1356 else
1357 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001358 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 goto failed;
1360 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1361 ++ectx.ec_stack.ga_len;
1362 }
1363 }
1364 break;
1365
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001366 // load g:/b:/w:/t: namespace
1367 case ISN_LOADGDICT:
1368 case ISN_LOADBDICT:
1369 case ISN_LOADWDICT:
1370 case ISN_LOADTDICT:
1371 {
1372 dict_T *d = NULL;
1373
1374 switch (iptr->isn_type)
1375 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001376 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1377 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1378 case ISN_LOADWDICT: d = curwin->w_vars; break;
1379 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001380 default: // Cannot reach here
1381 goto failed;
1382 }
1383 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1384 goto failed;
1385 tv = STACK_TV_BOT(0);
1386 tv->v_type = VAR_DICT;
1387 tv->v_lock = 0;
1388 tv->vval.v_dict = d;
1389 ++ectx.ec_stack.ga_len;
1390 }
1391 break;
1392
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 // load &option
1394 case ISN_LOADOPT:
1395 {
1396 typval_T optval;
1397 char_u *name = iptr->isn_arg.string;
1398
Bram Moolenaara8c17702020-04-01 21:17:24 +02001399 // This is not expected to fail, name is checked during
1400 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001401 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001403 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001404 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405 *STACK_TV_BOT(0) = optval;
1406 ++ectx.ec_stack.ga_len;
1407 }
1408 break;
1409
1410 // load $ENV
1411 case ISN_LOADENV:
1412 {
1413 typval_T optval;
1414 char_u *name = iptr->isn_arg.string;
1415
Bram Moolenaar270d0382020-05-15 21:42:53 +02001416 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001418 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001419 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 *STACK_TV_BOT(0) = optval;
1421 ++ectx.ec_stack.ga_len;
1422 }
1423 break;
1424
1425 // load @register
1426 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001427 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001428 goto failed;
1429 tv = STACK_TV_BOT(0);
1430 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001431 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001432 // This may result in NULL, which should be equivalent to an
1433 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 tv->vval.v_string = get_reg_contents(
1435 iptr->isn_arg.number, GREG_EXPR_SRC);
1436 ++ectx.ec_stack.ga_len;
1437 break;
1438
1439 // store local variable
1440 case ISN_STORE:
1441 --ectx.ec_stack.ga_len;
1442 tv = STACK_TV_VAR(iptr->isn_arg.number);
1443 clear_tv(tv);
1444 *tv = *STACK_TV_BOT(0);
1445 break;
1446
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001447 // store variable or argument in outer scope
1448 case ISN_STOREOUTER:
1449 --ectx.ec_stack.ga_len;
1450 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1451 clear_tv(tv);
1452 *tv = *STACK_TV_BOT(0);
1453 break;
1454
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001455 // store s: variable in old script
1456 case ISN_STORES:
1457 {
1458 hashtab_T *ht = &SCRIPT_VARS(
1459 iptr->isn_arg.loadstore.ls_sid);
1460 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001461 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001462
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001463 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001464 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001465 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001466 else
1467 {
1468 clear_tv(&di->di_tv);
1469 di->di_tv = *STACK_TV_BOT(0);
1470 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001471 }
1472 break;
1473
1474 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475 case ISN_STORESCRIPT:
1476 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001477 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 iptr->isn_arg.script.script_sid);
1479 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1480 + iptr->isn_arg.script.script_idx;
1481
1482 --ectx.ec_stack.ga_len;
1483 clear_tv(sv->sv_tv);
1484 *sv->sv_tv = *STACK_TV_BOT(0);
1485 }
1486 break;
1487
1488 // store option
1489 case ISN_STOREOPT:
1490 {
1491 long n = 0;
1492 char_u *s = NULL;
1493 char *msg;
1494
1495 --ectx.ec_stack.ga_len;
1496 tv = STACK_TV_BOT(0);
1497 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001498 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001500 if (s == NULL)
1501 s = (char_u *)"";
1502 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001504 // must be VAR_NUMBER, CHECKTYPE makes sure
1505 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1507 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001508 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 if (msg != NULL)
1510 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001511 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001513 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 }
1516 break;
1517
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001518 // store $ENV
1519 case ISN_STOREENV:
1520 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001521 tv = STACK_TV_BOT(0);
1522 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1523 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001524 break;
1525
1526 // store @r
1527 case ISN_STOREREG:
1528 {
1529 int reg = iptr->isn_arg.number;
1530
1531 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001532 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001533 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001534 tv_get_string(tv), -1, FALSE);
1535 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001536 }
1537 break;
1538
1539 // store v: variable
1540 case ISN_STOREV:
1541 --ectx.ec_stack.ga_len;
1542 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1543 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001544 // should not happen, type is checked when compiling
1545 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001546 break;
1547
Bram Moolenaard3aac292020-04-19 14:32:17 +02001548 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001550 case ISN_STOREB:
1551 case ISN_STOREW:
1552 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 {
1554 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001555 hashtab_T *ht;
1556 switch (iptr->isn_type)
1557 {
1558 case ISN_STOREG:
1559 ht = get_globvar_ht();
1560 break;
1561 case ISN_STOREB:
1562 ht = &curbuf->b_vars->dv_hashtab;
1563 break;
1564 case ISN_STOREW:
1565 ht = &curwin->w_vars->dv_hashtab;
1566 break;
1567 case ISN_STORET:
1568 ht = &curtab->tp_vars->dv_hashtab;
1569 break;
1570 default: // Cannot reach here
1571 goto failed;
1572 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573
1574 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001575 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001577 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578 else
1579 {
1580 clear_tv(&di->di_tv);
1581 di->di_tv = *STACK_TV_BOT(0);
1582 }
1583 }
1584 break;
1585
1586 // store number in local variable
1587 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001588 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589 clear_tv(tv);
1590 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001591 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 break;
1593
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001594 // store value in list variable
1595 case ISN_STORELIST:
1596 {
1597 typval_T *tv_idx = STACK_TV_BOT(-2);
1598 varnumber_T lidx = tv_idx->vval.v_number;
1599 typval_T *tv_list = STACK_TV_BOT(-1);
1600 list_T *list = tv_list->vval.v_list;
1601
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001602 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001603 if (lidx < 0 && list->lv_len + lidx >= 0)
1604 // negative index is relative to the end
1605 lidx = list->lv_len + lidx;
1606 if (lidx < 0 || lidx > list->lv_len)
1607 {
1608 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001609 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001610 }
1611 tv = STACK_TV_BOT(-3);
1612 if (lidx < list->lv_len)
1613 {
1614 listitem_T *li = list_find(list, lidx);
1615
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001616 if (error_if_locked(li->li_tv.v_lock,
1617 e_cannot_change_list_item))
1618 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001619 // overwrite existing list item
1620 clear_tv(&li->li_tv);
1621 li->li_tv = *tv;
1622 }
1623 else
1624 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001625 if (error_if_locked(list->lv_lock,
1626 e_cannot_change_list))
1627 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001628 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001629 if (list_append_tv(list, tv) == FAIL)
1630 goto failed;
1631 clear_tv(tv);
1632 }
1633 clear_tv(tv_idx);
1634 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001635 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001636 }
1637 break;
1638
1639 // store value in dict variable
1640 case ISN_STOREDICT:
1641 {
1642 typval_T *tv_key = STACK_TV_BOT(-2);
1643 char_u *key = tv_key->vval.v_string;
1644 typval_T *tv_dict = STACK_TV_BOT(-1);
1645 dict_T *dict = tv_dict->vval.v_dict;
1646 dictitem_T *di;
1647
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001648 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001649 if (dict == NULL)
1650 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001651 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001652 goto on_error;
1653 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001654 if (key == NULL)
1655 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001656 tv = STACK_TV_BOT(-3);
1657 di = dict_find(dict, key, -1);
1658 if (di != NULL)
1659 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001660 if (error_if_locked(di->di_tv.v_lock,
1661 e_cannot_change_dict_item))
1662 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001663 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001664 clear_tv(&di->di_tv);
1665 di->di_tv = *tv;
1666 }
1667 else
1668 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001669 if (error_if_locked(dict->dv_lock,
1670 e_cannot_change_dict))
1671 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001672 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001673 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1674 goto failed;
1675 clear_tv(tv);
1676 }
1677 clear_tv(tv_key);
1678 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001679 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001680 }
1681 break;
1682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683 // push constant
1684 case ISN_PUSHNR:
1685 case ISN_PUSHBOOL:
1686 case ISN_PUSHSPEC:
1687 case ISN_PUSHF:
1688 case ISN_PUSHS:
1689 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001690 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001691 case ISN_PUSHCHANNEL:
1692 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001693 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 goto failed;
1695 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001696 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 ++ectx.ec_stack.ga_len;
1698 switch (iptr->isn_type)
1699 {
1700 case ISN_PUSHNR:
1701 tv->v_type = VAR_NUMBER;
1702 tv->vval.v_number = iptr->isn_arg.number;
1703 break;
1704 case ISN_PUSHBOOL:
1705 tv->v_type = VAR_BOOL;
1706 tv->vval.v_number = iptr->isn_arg.number;
1707 break;
1708 case ISN_PUSHSPEC:
1709 tv->v_type = VAR_SPECIAL;
1710 tv->vval.v_number = iptr->isn_arg.number;
1711 break;
1712#ifdef FEAT_FLOAT
1713 case ISN_PUSHF:
1714 tv->v_type = VAR_FLOAT;
1715 tv->vval.v_float = iptr->isn_arg.fnumber;
1716 break;
1717#endif
1718 case ISN_PUSHBLOB:
1719 blob_copy(iptr->isn_arg.blob, tv);
1720 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001721 case ISN_PUSHFUNC:
1722 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001723 if (iptr->isn_arg.string == NULL)
1724 tv->vval.v_string = NULL;
1725 else
1726 tv->vval.v_string =
1727 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001728 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001729 case ISN_PUSHCHANNEL:
1730#ifdef FEAT_JOB_CHANNEL
1731 tv->v_type = VAR_CHANNEL;
1732 tv->vval.v_channel = iptr->isn_arg.channel;
1733 if (tv->vval.v_channel != NULL)
1734 ++tv->vval.v_channel->ch_refcount;
1735#endif
1736 break;
1737 case ISN_PUSHJOB:
1738#ifdef FEAT_JOB_CHANNEL
1739 tv->v_type = VAR_JOB;
1740 tv->vval.v_job = iptr->isn_arg.job;
1741 if (tv->vval.v_job != NULL)
1742 ++tv->vval.v_job->jv_refcount;
1743#endif
1744 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 default:
1746 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001747 tv->vval.v_string = vim_strsave(
1748 iptr->isn_arg.string == NULL
1749 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 }
1751 break;
1752
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001753 case ISN_UNLET:
1754 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1755 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001756 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001757 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001758 case ISN_UNLETENV:
1759 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1760 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001761
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001762 case ISN_LOCKCONST:
1763 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1764 break;
1765
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001766 // create a list from items on the stack; uses a single allocation
1767 // for the list header and the items
1768 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001769 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1770 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 break;
1772
1773 // create a dict from items on the stack
1774 case ISN_NEWDICT:
1775 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001776 int count = iptr->isn_arg.number;
1777 dict_T *dict = dict_alloc();
1778 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001779 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780
1781 if (dict == NULL)
1782 goto failed;
1783 for (idx = 0; idx < count; ++idx)
1784 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001785 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001787 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001788 key = tv->vval.v_string == NULL
1789 ? (char_u *)"" : tv->vval.v_string;
1790 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02001791 if (item != NULL)
1792 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001793 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001794 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02001795 dict_unref(dict);
1796 goto on_error;
1797 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001798 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799 clear_tv(tv);
1800 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001801 {
1802 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001804 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1806 item->di_tv.v_lock = 0;
1807 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001808 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001809 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001810 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001812 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813 }
1814
1815 if (count > 0)
1816 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001817 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 goto failed;
1819 else
1820 ++ectx.ec_stack.ga_len;
1821 tv = STACK_TV_BOT(-1);
1822 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001823 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 tv->vval.v_dict = dict;
1825 ++dict->dv_refcount;
1826 }
1827 break;
1828
1829 // call a :def function
1830 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001831 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1833 iptr->isn_arg.dfunc.cdf_argcount,
1834 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001835 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 break;
1837
1838 // call a builtin function
1839 case ISN_BCALL:
1840 SOURCING_LNUM = iptr->isn_lnum;
1841 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1842 iptr->isn_arg.bfunc.cbf_argcount,
1843 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001844 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 break;
1846
1847 // call a funcref or partial
1848 case ISN_PCALL:
1849 {
1850 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1851 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001852 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853
1854 SOURCING_LNUM = iptr->isn_lnum;
1855 if (pfunc->cpf_top)
1856 {
1857 // funcref is above the arguments
1858 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1859 }
1860 else
1861 {
1862 // Get the funcref from the stack.
1863 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001864 partial_tv = *STACK_TV_BOT(0);
1865 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866 }
1867 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001868 if (tv == &partial_tv)
1869 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001871 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 }
1873 break;
1874
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001875 case ISN_PCALL_END:
1876 // PCALL finished, arguments have been consumed and replaced by
1877 // the return value. Now clear the funcref from the stack,
1878 // and move the return value in its place.
1879 --ectx.ec_stack.ga_len;
1880 clear_tv(STACK_TV_BOT(-1));
1881 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1882 break;
1883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 // call a user defined function or funcref/partial
1885 case ISN_UCALL:
1886 {
1887 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1888
1889 SOURCING_LNUM = iptr->isn_lnum;
1890 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001891 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001892 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 }
1894 break;
1895
1896 // return from a :def function call
1897 case ISN_RETURN:
1898 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001899 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001900 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001901
1902 if (trystack->ga_len > 0)
1903 trycmd = ((trycmd_T *)trystack->ga_data)
1904 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001905 if (trycmd != NULL
1906 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 && trycmd->tcd_finally_idx != 0)
1908 {
1909 // jump to ":finally"
1910 ectx.ec_iidx = trycmd->tcd_finally_idx;
1911 trycmd->tcd_return = TRUE;
1912 }
1913 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001914 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001915 }
1916 break;
1917
1918 // push a function reference to a compiled function
1919 case ISN_FUNCREF:
1920 {
1921 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001922 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923
1924 pt = ALLOC_CLEAR_ONE(partial_T);
1925 if (pt == NULL)
1926 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001927 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001928 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001929 vim_free(pt);
1930 goto failed;
1931 }
1932 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1933 + iptr->isn_arg.funcref.fr_func;
1934 pt->pt_func = pt_dfunc->df_ufunc;
1935 pt->pt_refcount = 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001936
1937 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1938 {
1939 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1940 + ectx.ec_dfunc_idx;
1941
1942 // The closure needs to find arguments and local
1943 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001944 pt->pt_ectx_stack = &ectx.ec_stack;
1945 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001946
1947 // If this function returns and the closure is still
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001948 // being used, we need to make a copy of the context
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001949 // (arguments and local variables). Store a reference
1950 // to the partial so we can handle that.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001951 if (ga_grow(&ectx.ec_funcrefs, 1) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001952 {
Bram Moolenaardec07512020-09-18 23:11:10 +02001953 vim_free(pt);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001954 goto failed;
1955 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001956 // Extra variable keeps the count of closures created
1957 // in the current function call.
1958 tv = STACK_TV_VAR(dfunc->df_varcount);
1959 ++tv->vval.v_number;
1960
1961 ((partial_T **)ectx.ec_funcrefs.ga_data)
1962 [ectx.ec_funcrefs.ga_len] = pt;
1963 ++pt->pt_refcount;
1964 ++ectx.ec_funcrefs.ga_len;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001965 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001966 ++pt_dfunc->df_ufunc->uf_refcount;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 tv = STACK_TV_BOT(0);
1969 ++ectx.ec_stack.ga_len;
1970 tv->vval.v_partial = pt;
1971 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001972 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973 }
1974 break;
1975
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001976 // Create a global function from a lambda.
1977 case ISN_NEWFUNC:
1978 {
1979 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1980
1981 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1982 }
1983 break;
1984
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001985 // List functions
1986 case ISN_DEF:
1987 if (iptr->isn_arg.string == NULL)
1988 list_functions(NULL);
1989 else
1990 {
1991 exarg_T ea;
1992
1993 CLEAR_FIELD(ea);
1994 ea.cmd = ea.arg = iptr->isn_arg.string;
1995 define_function(&ea, NULL);
1996 }
1997 break;
1998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 // jump if a condition is met
2000 case ISN_JUMP:
2001 {
2002 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002003 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 int jump = TRUE;
2005
2006 if (when != JUMP_ALWAYS)
2007 {
2008 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002009 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002010 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002011 || when == JUMP_IF_COND_TRUE)
2012 {
2013 SOURCING_LNUM = iptr->isn_lnum;
2014 jump = tv_get_bool_chk(tv, &error);
2015 if (error)
2016 goto on_error;
2017 }
2018 else
2019 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002021 || when == JUMP_AND_KEEP_IF_FALSE
2022 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002024 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 {
2026 // drop the value from the stack
2027 clear_tv(tv);
2028 --ectx.ec_stack.ga_len;
2029 }
2030 }
2031 if (jump)
2032 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2033 }
2034 break;
2035
2036 // top of a for loop
2037 case ISN_FOR:
2038 {
2039 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2040 typval_T *idxtv =
2041 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2042
2043 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002044 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002046 ++idxtv->vval.v_number;
2047 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 // past the end of the list, jump to "endfor"
2049 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2050 else if (list->lv_first == &range_list_item)
2051 {
2052 // non-materialized range() list
2053 tv = STACK_TV_BOT(0);
2054 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002055 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 tv->vval.v_number = list_find_nr(
2057 list, idxtv->vval.v_number, NULL);
2058 ++ectx.ec_stack.ga_len;
2059 }
2060 else
2061 {
2062 listitem_T *li = list_find(list, idxtv->vval.v_number);
2063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2065 ++ectx.ec_stack.ga_len;
2066 }
2067 }
2068 break;
2069
2070 // start of ":try" block
2071 case ISN_TRY:
2072 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002073 trycmd_T *trycmd = NULL;
2074
Bram Moolenaar270d0382020-05-15 21:42:53 +02002075 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 goto failed;
2077 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2078 + ectx.ec_trystack.ga_len;
2079 ++ectx.ec_trystack.ga_len;
2080 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002081 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2083 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002084 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002085 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 }
2087 break;
2088
2089 case ISN_PUSHEXC:
2090 if (current_exception == NULL)
2091 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002092 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093 iemsg("Evaluating catch while current_exception is NULL");
2094 goto failed;
2095 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002096 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 goto failed;
2098 tv = STACK_TV_BOT(0);
2099 ++ectx.ec_stack.ga_len;
2100 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002101 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 tv->vval.v_string = vim_strsave(
2103 (char_u *)current_exception->value);
2104 break;
2105
2106 case ISN_CATCH:
2107 {
2108 garray_T *trystack = &ectx.ec_trystack;
2109
2110 if (trystack->ga_len > 0)
2111 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002112 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 + trystack->ga_len - 1;
2114 trycmd->tcd_caught = TRUE;
2115 }
2116 did_emsg = got_int = did_throw = FALSE;
2117 catch_exception(current_exception);
2118 }
2119 break;
2120
2121 // end of ":try" block
2122 case ISN_ENDTRY:
2123 {
2124 garray_T *trystack = &ectx.ec_trystack;
2125
2126 if (trystack->ga_len > 0)
2127 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002128 trycmd_T *trycmd = NULL;
2129
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002130 --trystack->ga_len;
2131 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002132 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 trycmd = ((trycmd_T *)trystack->ga_data)
2134 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002135 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 {
2137 // discard the exception
2138 if (caught_stack == current_exception)
2139 caught_stack = caught_stack->caught;
2140 discard_current_exception();
2141 }
2142
2143 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002144 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002145 }
2146 }
2147 break;
2148
2149 case ISN_THROW:
2150 --ectx.ec_stack.ga_len;
2151 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002152 if (tv->vval.v_string == NULL
2153 || *skipwhite(tv->vval.v_string) == NUL)
2154 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002155 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002156 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002157 emsg(_(e_throw_with_empty_string));
2158 goto failed;
2159 }
2160
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2162 {
2163 vim_free(tv->vval.v_string);
2164 goto failed;
2165 }
2166 did_throw = TRUE;
2167 break;
2168
2169 // compare with special values
2170 case ISN_COMPAREBOOL:
2171 case ISN_COMPARESPECIAL:
2172 {
2173 typval_T *tv1 = STACK_TV_BOT(-2);
2174 typval_T *tv2 = STACK_TV_BOT(-1);
2175 varnumber_T arg1 = tv1->vval.v_number;
2176 varnumber_T arg2 = tv2->vval.v_number;
2177 int res;
2178
2179 switch (iptr->isn_arg.op.op_type)
2180 {
2181 case EXPR_EQUAL: res = arg1 == arg2; break;
2182 case EXPR_NEQUAL: res = arg1 != arg2; break;
2183 default: res = 0; break;
2184 }
2185
2186 --ectx.ec_stack.ga_len;
2187 tv1->v_type = VAR_BOOL;
2188 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2189 }
2190 break;
2191
2192 // Operation with two number arguments
2193 case ISN_OPNR:
2194 case ISN_COMPARENR:
2195 {
2196 typval_T *tv1 = STACK_TV_BOT(-2);
2197 typval_T *tv2 = STACK_TV_BOT(-1);
2198 varnumber_T arg1 = tv1->vval.v_number;
2199 varnumber_T arg2 = tv2->vval.v_number;
2200 varnumber_T res;
2201
2202 switch (iptr->isn_arg.op.op_type)
2203 {
2204 case EXPR_MULT: res = arg1 * arg2; break;
2205 case EXPR_DIV: res = arg1 / arg2; break;
2206 case EXPR_REM: res = arg1 % arg2; break;
2207 case EXPR_SUB: res = arg1 - arg2; break;
2208 case EXPR_ADD: res = arg1 + arg2; break;
2209
2210 case EXPR_EQUAL: res = arg1 == arg2; break;
2211 case EXPR_NEQUAL: res = arg1 != arg2; break;
2212 case EXPR_GREATER: res = arg1 > arg2; break;
2213 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2214 case EXPR_SMALLER: res = arg1 < arg2; break;
2215 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2216 default: res = 0; break;
2217 }
2218
2219 --ectx.ec_stack.ga_len;
2220 if (iptr->isn_type == ISN_COMPARENR)
2221 {
2222 tv1->v_type = VAR_BOOL;
2223 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2224 }
2225 else
2226 tv1->vval.v_number = res;
2227 }
2228 break;
2229
2230 // Computation with two float arguments
2231 case ISN_OPFLOAT:
2232 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002233#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002234 {
2235 typval_T *tv1 = STACK_TV_BOT(-2);
2236 typval_T *tv2 = STACK_TV_BOT(-1);
2237 float_T arg1 = tv1->vval.v_float;
2238 float_T arg2 = tv2->vval.v_float;
2239 float_T res = 0;
2240 int cmp = FALSE;
2241
2242 switch (iptr->isn_arg.op.op_type)
2243 {
2244 case EXPR_MULT: res = arg1 * arg2; break;
2245 case EXPR_DIV: res = arg1 / arg2; break;
2246 case EXPR_SUB: res = arg1 - arg2; break;
2247 case EXPR_ADD: res = arg1 + arg2; break;
2248
2249 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2250 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2251 case EXPR_GREATER: cmp = arg1 > arg2; break;
2252 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2253 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2254 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2255 default: cmp = 0; break;
2256 }
2257 --ectx.ec_stack.ga_len;
2258 if (iptr->isn_type == ISN_COMPAREFLOAT)
2259 {
2260 tv1->v_type = VAR_BOOL;
2261 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2262 }
2263 else
2264 tv1->vval.v_float = res;
2265 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002266#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 break;
2268
2269 case ISN_COMPARELIST:
2270 {
2271 typval_T *tv1 = STACK_TV_BOT(-2);
2272 typval_T *tv2 = STACK_TV_BOT(-1);
2273 list_T *arg1 = tv1->vval.v_list;
2274 list_T *arg2 = tv2->vval.v_list;
2275 int cmp = FALSE;
2276 int ic = iptr->isn_arg.op.op_ic;
2277
2278 switch (iptr->isn_arg.op.op_type)
2279 {
2280 case EXPR_EQUAL: cmp =
2281 list_equal(arg1, arg2, ic, FALSE); break;
2282 case EXPR_NEQUAL: cmp =
2283 !list_equal(arg1, arg2, ic, FALSE); break;
2284 case EXPR_IS: cmp = arg1 == arg2; break;
2285 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2286 default: cmp = 0; break;
2287 }
2288 --ectx.ec_stack.ga_len;
2289 clear_tv(tv1);
2290 clear_tv(tv2);
2291 tv1->v_type = VAR_BOOL;
2292 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2293 }
2294 break;
2295
2296 case ISN_COMPAREBLOB:
2297 {
2298 typval_T *tv1 = STACK_TV_BOT(-2);
2299 typval_T *tv2 = STACK_TV_BOT(-1);
2300 blob_T *arg1 = tv1->vval.v_blob;
2301 blob_T *arg2 = tv2->vval.v_blob;
2302 int cmp = FALSE;
2303
2304 switch (iptr->isn_arg.op.op_type)
2305 {
2306 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2307 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2308 case EXPR_IS: cmp = arg1 == arg2; break;
2309 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2310 default: cmp = 0; break;
2311 }
2312 --ectx.ec_stack.ga_len;
2313 clear_tv(tv1);
2314 clear_tv(tv2);
2315 tv1->v_type = VAR_BOOL;
2316 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2317 }
2318 break;
2319
2320 // TODO: handle separately
2321 case ISN_COMPARESTRING:
2322 case ISN_COMPAREDICT:
2323 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324 case ISN_COMPAREANY:
2325 {
2326 typval_T *tv1 = STACK_TV_BOT(-2);
2327 typval_T *tv2 = STACK_TV_BOT(-1);
2328 exptype_T exptype = iptr->isn_arg.op.op_type;
2329 int ic = iptr->isn_arg.op.op_ic;
2330
Bram Moolenaareb26f432020-09-14 16:50:05 +02002331 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 typval_compare(tv1, tv2, exptype, ic);
2333 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 --ectx.ec_stack.ga_len;
2335 }
2336 break;
2337
2338 case ISN_ADDLIST:
2339 case ISN_ADDBLOB:
2340 {
2341 typval_T *tv1 = STACK_TV_BOT(-2);
2342 typval_T *tv2 = STACK_TV_BOT(-1);
2343
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002344 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 if (iptr->isn_type == ISN_ADDLIST)
2346 eval_addlist(tv1, tv2);
2347 else
2348 eval_addblob(tv1, tv2);
2349 clear_tv(tv2);
2350 --ectx.ec_stack.ga_len;
2351 }
2352 break;
2353
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002354 case ISN_LISTAPPEND:
2355 {
2356 typval_T *tv1 = STACK_TV_BOT(-2);
2357 typval_T *tv2 = STACK_TV_BOT(-1);
2358 list_T *l = tv1->vval.v_list;
2359
2360 // add an item to a list
2361 if (l == NULL)
2362 {
2363 SOURCING_LNUM = iptr->isn_lnum;
2364 emsg(_(e_cannot_add_to_null_list));
2365 goto on_error;
2366 }
2367 if (list_append_tv(l, tv2) == FAIL)
2368 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002369 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002370 --ectx.ec_stack.ga_len;
2371 }
2372 break;
2373
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002374 case ISN_BLOBAPPEND:
2375 {
2376 typval_T *tv1 = STACK_TV_BOT(-2);
2377 typval_T *tv2 = STACK_TV_BOT(-1);
2378 blob_T *b = tv1->vval.v_blob;
2379 int error = FALSE;
2380 varnumber_T n;
2381
2382 // add a number to a blob
2383 if (b == NULL)
2384 {
2385 SOURCING_LNUM = iptr->isn_lnum;
2386 emsg(_(e_cannot_add_to_null_blob));
2387 goto on_error;
2388 }
2389 n = tv_get_number_chk(tv2, &error);
2390 if (error)
2391 goto on_error;
2392 ga_append(&b->bv_ga, (int)n);
2393 --ectx.ec_stack.ga_len;
2394 }
2395 break;
2396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 // Computation with two arguments of unknown type
2398 case ISN_OPANY:
2399 {
2400 typval_T *tv1 = STACK_TV_BOT(-2);
2401 typval_T *tv2 = STACK_TV_BOT(-1);
2402 varnumber_T n1, n2;
2403#ifdef FEAT_FLOAT
2404 float_T f1 = 0, f2 = 0;
2405#endif
2406 int error = FALSE;
2407
2408 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2409 {
2410 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2411 {
2412 eval_addlist(tv1, tv2);
2413 clear_tv(tv2);
2414 --ectx.ec_stack.ga_len;
2415 break;
2416 }
2417 else if (tv1->v_type == VAR_BLOB
2418 && tv2->v_type == VAR_BLOB)
2419 {
2420 eval_addblob(tv1, tv2);
2421 clear_tv(tv2);
2422 --ectx.ec_stack.ga_len;
2423 break;
2424 }
2425 }
2426#ifdef FEAT_FLOAT
2427 if (tv1->v_type == VAR_FLOAT)
2428 {
2429 f1 = tv1->vval.v_float;
2430 n1 = 0;
2431 }
2432 else
2433#endif
2434 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002435 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 n1 = tv_get_number_chk(tv1, &error);
2437 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002438 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439#ifdef FEAT_FLOAT
2440 if (tv2->v_type == VAR_FLOAT)
2441 f1 = n1;
2442#endif
2443 }
2444#ifdef FEAT_FLOAT
2445 if (tv2->v_type == VAR_FLOAT)
2446 {
2447 f2 = tv2->vval.v_float;
2448 n2 = 0;
2449 }
2450 else
2451#endif
2452 {
2453 n2 = tv_get_number_chk(tv2, &error);
2454 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002455 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456#ifdef FEAT_FLOAT
2457 if (tv1->v_type == VAR_FLOAT)
2458 f2 = n2;
2459#endif
2460 }
2461#ifdef FEAT_FLOAT
2462 // if there is a float on either side the result is a float
2463 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2464 {
2465 switch (iptr->isn_arg.op.op_type)
2466 {
2467 case EXPR_MULT: f1 = f1 * f2; break;
2468 case EXPR_DIV: f1 = f1 / f2; break;
2469 case EXPR_SUB: f1 = f1 - f2; break;
2470 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002471 default: SOURCING_LNUM = iptr->isn_lnum;
2472 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002473 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 }
2475 clear_tv(tv1);
2476 clear_tv(tv2);
2477 tv1->v_type = VAR_FLOAT;
2478 tv1->vval.v_float = f1;
2479 --ectx.ec_stack.ga_len;
2480 }
2481 else
2482#endif
2483 {
2484 switch (iptr->isn_arg.op.op_type)
2485 {
2486 case EXPR_MULT: n1 = n1 * n2; break;
2487 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2488 case EXPR_SUB: n1 = n1 - n2; break;
2489 case EXPR_ADD: n1 = n1 + n2; break;
2490 default: n1 = num_modulus(n1, n2); break;
2491 }
2492 clear_tv(tv1);
2493 clear_tv(tv2);
2494 tv1->v_type = VAR_NUMBER;
2495 tv1->vval.v_number = n1;
2496 --ectx.ec_stack.ga_len;
2497 }
2498 }
2499 break;
2500
2501 case ISN_CONCAT:
2502 {
2503 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2504 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2505 char_u *res;
2506
2507 res = concat_str(str1, str2);
2508 clear_tv(STACK_TV_BOT(-2));
2509 clear_tv(STACK_TV_BOT(-1));
2510 --ectx.ec_stack.ga_len;
2511 STACK_TV_BOT(-1)->vval.v_string = res;
2512 }
2513 break;
2514
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002515 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002516 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002517 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002518 int is_slice = iptr->isn_type == ISN_STRSLICE;
2519 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002520 char_u *res;
2521
2522 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002523 // string slice: string is at stack-3, first index at
2524 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002525 if (is_slice)
2526 {
2527 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002528 n1 = tv->vval.v_number;
2529 }
2530
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002531 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002532 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002533
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002534 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002535 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002536 if (is_slice)
2537 // Slice: Select the characters from the string
2538 res = string_slice(tv->vval.v_string, n1, n2);
2539 else
2540 // Index: The resulting variable is a string of a
2541 // single character. If the index is too big or
2542 // negative the result is empty.
2543 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002544 vim_free(tv->vval.v_string);
2545 tv->vval.v_string = res;
2546 }
2547 break;
2548
2549 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002550 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 {
Bram Moolenaared591872020-08-15 22:14:53 +02002552 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002554 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555
2556 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002557 // list slice: list is at stack-3, indexes at stack-2 and
2558 // stack-1
2559 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 list = tv->vval.v_list;
2561
2562 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002563 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002565
2566 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 {
Bram Moolenaared591872020-08-15 22:14:53 +02002568 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002569 n1 = tv->vval.v_number;
2570 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 }
Bram Moolenaared591872020-08-15 22:14:53 +02002572
2573 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002574 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002575 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002576 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2577 == FAIL)
2578 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 }
2580 break;
2581
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002582 case ISN_ANYINDEX:
2583 case ISN_ANYSLICE:
2584 {
2585 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2586 typval_T *var1, *var2;
2587 int res;
2588
2589 // index: composite is at stack-2, index at stack-1
2590 // slice: composite is at stack-3, indexes at stack-2 and
2591 // stack-1
2592 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002593 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002594 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2595 goto on_error;
2596 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2597 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2598 res = eval_index_inner(tv, is_slice,
2599 var1, var2, NULL, -1, TRUE);
2600 clear_tv(var1);
2601 if (is_slice)
2602 clear_tv(var2);
2603 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2604 if (res == FAIL)
2605 goto on_error;
2606 }
2607 break;
2608
Bram Moolenaar9af78762020-06-16 11:34:42 +02002609 case ISN_SLICE:
2610 {
2611 list_T *list;
2612 int count = iptr->isn_arg.number;
2613
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002614 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002615 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002616 list = tv->vval.v_list;
2617
2618 // no error for short list, expect it to be checked earlier
2619 if (list != NULL && list->lv_len >= count)
2620 {
2621 list_T *newlist = list_slice(list,
2622 count, list->lv_len - 1);
2623
2624 if (newlist != NULL)
2625 {
2626 list_unref(list);
2627 tv->vval.v_list = newlist;
2628 ++newlist->lv_refcount;
2629 }
2630 }
2631 }
2632 break;
2633
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002634 case ISN_GETITEM:
2635 {
2636 listitem_T *li;
2637 int index = iptr->isn_arg.number;
2638
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002639 // Get list item: list is at stack-1, push item.
2640 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002641 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002642 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002643
2644 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2645 goto failed;
2646 ++ectx.ec_stack.ga_len;
2647 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2648 }
2649 break;
2650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 case ISN_MEMBER:
2652 {
2653 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002654 char_u *key;
2655 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002656 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002657
2658 // dict member: dict is at stack-2, key at stack-1
2659 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002660 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002661 dict = tv->vval.v_dict;
2662
2663 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002664 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002665 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01002666 if (key == NULL)
2667 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002668
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002669 if ((di = dict_find(dict, key, -1)) == NULL)
2670 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002671 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002672 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002673
2674 // If :silent! is used we will continue, make sure the
2675 // stack contents makes sense.
2676 clear_tv(tv);
2677 --ectx.ec_stack.ga_len;
2678 tv = STACK_TV_BOT(-1);
2679 clear_tv(tv);
2680 tv->v_type = VAR_NUMBER;
2681 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002682 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002683 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002684 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002685 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002686 // Clear the dict only after getting the item, to avoid
2687 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002688 tv = STACK_TV_BOT(-1);
2689 temp_tv = *tv;
2690 copy_tv(&di->di_tv, tv);
2691 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002692 }
2693 break;
2694
2695 // dict member with string key
2696 case ISN_STRINGMEMBER:
2697 {
2698 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002700 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701
2702 tv = STACK_TV_BOT(-1);
2703 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2704 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002705 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002707 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 }
2709 dict = tv->vval.v_dict;
2710
2711 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2712 == NULL)
2713 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002714 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002716 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002717 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002718 // Clear the dict after getting the item, to avoid that it
2719 // make the item invalid.
2720 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002722 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723 }
2724 break;
2725
2726 case ISN_NEGATENR:
2727 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002728 if (tv->v_type != VAR_NUMBER
2729#ifdef FEAT_FLOAT
2730 && tv->v_type != VAR_FLOAT
2731#endif
2732 )
2733 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002734 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002735 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002736 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002737 }
2738#ifdef FEAT_FLOAT
2739 if (tv->v_type == VAR_FLOAT)
2740 tv->vval.v_float = -tv->vval.v_float;
2741 else
2742#endif
2743 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744 break;
2745
2746 case ISN_CHECKNR:
2747 {
2748 int error = FALSE;
2749
2750 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002751 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002753 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 (void)tv_get_number_chk(tv, &error);
2755 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002756 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 }
2758 break;
2759
2760 case ISN_CHECKTYPE:
2761 {
2762 checktype_T *ct = &iptr->isn_arg.type;
2763
2764 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002765 SOURCING_LNUM = iptr->isn_lnum;
2766 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2767 goto on_error;
2768
2769 // number 0 is FALSE, number 1 is TRUE
2770 if (tv->v_type == VAR_NUMBER
2771 && ct->ct_type->tt_type == VAR_BOOL
2772 && (tv->vval.v_number == 0
2773 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002775 tv->v_type = VAR_BOOL;
2776 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002777 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 }
2779 }
2780 break;
2781
Bram Moolenaar9af78762020-06-16 11:34:42 +02002782 case ISN_CHECKLEN:
2783 {
2784 int min_len = iptr->isn_arg.checklen.cl_min_len;
2785 list_T *list = NULL;
2786
2787 tv = STACK_TV_BOT(-1);
2788 if (tv->v_type == VAR_LIST)
2789 list = tv->vval.v_list;
2790 if (list == NULL || list->lv_len < min_len
2791 || (list->lv_len > min_len
2792 && !iptr->isn_arg.checklen.cl_more_OK))
2793 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002794 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002795 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002796 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002797 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002798 }
2799 }
2800 break;
2801
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002803 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 {
2805 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002806 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807
2808 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002809 if (iptr->isn_type == ISN_2BOOL)
2810 {
2811 n = tv2bool(tv);
2812 if (iptr->isn_arg.number) // invert
2813 n = !n;
2814 }
2815 else
2816 {
2817 SOURCING_LNUM = iptr->isn_lnum;
2818 n = tv_get_bool_chk(tv, &error);
2819 if (error)
2820 goto on_error;
2821 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 clear_tv(tv);
2823 tv->v_type = VAR_BOOL;
2824 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2825 }
2826 break;
2827
2828 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002829 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 {
2831 char_u *str;
2832
2833 tv = STACK_TV_BOT(iptr->isn_arg.number);
2834 if (tv->v_type != VAR_STRING)
2835 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002836 if (iptr->isn_type == ISN_2STRING_ANY)
2837 {
2838 switch (tv->v_type)
2839 {
2840 case VAR_SPECIAL:
2841 case VAR_BOOL:
2842 case VAR_NUMBER:
2843 case VAR_FLOAT:
2844 case VAR_BLOB: break;
2845 default: to_string_error(tv->v_type);
2846 goto on_error;
2847 }
2848 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 str = typval_tostring(tv);
2850 clear_tv(tv);
2851 tv->v_type = VAR_STRING;
2852 tv->vval.v_string = str;
2853 }
2854 }
2855 break;
2856
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002857 case ISN_PUT:
2858 {
2859 int regname = iptr->isn_arg.put.put_regname;
2860 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2861 char_u *expr = NULL;
2862 int dir = FORWARD;
2863
2864 if (regname == '=')
2865 {
2866 tv = STACK_TV_BOT(-1);
2867 if (tv->v_type == VAR_STRING)
2868 expr = tv->vval.v_string;
2869 else
2870 {
2871 expr = typval_tostring(tv); // allocates value
2872 clear_tv(tv);
2873 }
2874 --ectx.ec_stack.ga_len;
2875 }
2876 if (lnum == -2)
2877 // :put! above cursor
2878 dir = BACKWARD;
2879 else if (lnum >= 0)
2880 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2881 check_cursor();
2882 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2883 vim_free(expr);
2884 }
2885 break;
2886
Bram Moolenaar02194d22020-10-24 23:08:38 +02002887 case ISN_CMDMOD:
2888 save_cmdmod = cmdmod;
2889 restore_cmdmod = TRUE;
2890 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
2891 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002892 break;
2893
Bram Moolenaar02194d22020-10-24 23:08:38 +02002894 case ISN_CMDMOD_REV:
2895 // filter regprog is owned by the instruction, don't free it
2896 cmdmod.cmod_filter_regmatch.regprog = NULL;
2897 undo_cmdmod(&cmdmod);
2898 cmdmod = save_cmdmod;
2899 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002900 break;
2901
Bram Moolenaar792f7862020-11-23 08:31:18 +01002902 case ISN_UNPACK:
2903 {
2904 int count = iptr->isn_arg.unpack.unp_count;
2905 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
2906 list_T *l;
2907 listitem_T *li;
2908 int i;
2909
2910 // Check there is a valid list to unpack.
2911 tv = STACK_TV_BOT(-1);
2912 if (tv->v_type != VAR_LIST)
2913 {
2914 SOURCING_LNUM = iptr->isn_lnum;
2915 emsg(_(e_for_argument_must_be_sequence_of_lists));
2916 goto on_error;
2917 }
2918 l = tv->vval.v_list;
2919 if (l == NULL
2920 || l->lv_len < (semicolon ? count - 1 : count))
2921 {
2922 SOURCING_LNUM = iptr->isn_lnum;
2923 emsg(_(e_list_value_does_not_have_enough_items));
2924 goto on_error;
2925 }
2926 else if (!semicolon && l->lv_len > count)
2927 {
2928 SOURCING_LNUM = iptr->isn_lnum;
2929 emsg(_(e_list_value_has_more_items_than_targets));
2930 goto on_error;
2931 }
2932
2933 CHECK_LIST_MATERIALIZE(l);
2934 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
2935 goto failed;
2936 ectx.ec_stack.ga_len += count - 1;
2937
2938 // Variable after semicolon gets a list with the remaining
2939 // items.
2940 if (semicolon)
2941 {
2942 list_T *rem_list =
2943 list_alloc_with_items(l->lv_len - count + 1);
2944
2945 if (rem_list == NULL)
2946 goto failed;
2947 tv = STACK_TV_BOT(-count);
2948 tv->vval.v_list = rem_list;
2949 ++rem_list->lv_refcount;
2950 tv->v_lock = 0;
2951 li = l->lv_first;
2952 for (i = 0; i < count - 1; ++i)
2953 li = li->li_next;
2954 for (i = 0; li != NULL; ++i)
2955 {
2956 list_set_item(rem_list, i, &li->li_tv);
2957 li = li->li_next;
2958 }
2959 --count;
2960 }
2961
2962 // Produce the values in reverse order, first item last.
2963 li = l->lv_first;
2964 for (i = 0; i < count; ++i)
2965 {
2966 tv = STACK_TV_BOT(-i - 1);
2967 copy_tv(&li->li_tv, tv);
2968 li = li->li_next;
2969 }
2970
2971 list_unref(l);
2972 }
2973 break;
2974
Bram Moolenaar389df252020-07-09 21:20:47 +02002975 case ISN_SHUFFLE:
2976 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01002977 typval_T tmp_tv;
2978 int item = iptr->isn_arg.shuffle.shfl_item;
2979 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02002980
2981 tmp_tv = *STACK_TV_BOT(-item);
2982 for ( ; up > 0 && item > 1; --up)
2983 {
2984 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2985 --item;
2986 }
2987 *STACK_TV_BOT(-item) = tmp_tv;
2988 }
2989 break;
2990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 case ISN_DROP:
2992 --ectx.ec_stack.ga_len;
2993 clear_tv(STACK_TV_BOT(0));
2994 break;
2995 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002996 continue;
2997
Bram Moolenaard032f342020-07-18 18:13:02 +02002998func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002999 // Restore previous function. If the frame pointer is where we started
3000 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003001 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003002 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003003
Bram Moolenaard032f342020-07-18 18:13:02 +02003004 if (func_return(&ectx) == FAIL)
3005 // only fails when out of memory
3006 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003007 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003008
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003009on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003010 // Jump here for an error that does not require aborting execution.
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003011 // If "emsg_silent" is set then ignore the error.
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003012 if (did_emsg_cumul + did_emsg == did_emsg_before && emsg_silent)
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003013 continue;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003014on_fatal_error:
3015 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003016 // If we are not inside a try-catch started here, abort execution.
3017 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003018 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 }
3020
3021done:
3022 // function finished, get result from the stack.
3023 tv = STACK_TV_BOT(-1);
3024 *rettv = *tv;
3025 tv->v_type = VAR_UNKNOWN;
3026 ret = OK;
3027
3028failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003029 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003030 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003031 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003032
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003033 // Deal with any remaining closures, they may be in use somewhere.
3034 if (ectx.ec_funcrefs.ga_len > 0)
3035 handle_closure_in_use(&ectx, FALSE);
3036
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003037 estack_pop();
3038 current_sctx = save_current_sctx;
3039
Bram Moolenaar352134b2020-10-17 22:04:08 +02003040 if (*msg_list != NULL && saved_msg_list != NULL)
3041 {
3042 msglist_T **plist = saved_msg_list;
3043
3044 // Append entries from the current msg_list (uncaught exceptions) to
3045 // the saved msg_list.
3046 while (*plist != NULL)
3047 plist = &(*plist)->next;
3048
3049 *plist = *msg_list;
3050 }
3051 msg_list = saved_msg_list;
3052
Bram Moolenaar02194d22020-10-24 23:08:38 +02003053 if (restore_cmdmod)
3054 {
3055 cmdmod.cmod_filter_regmatch.regprog = NULL;
3056 undo_cmdmod(&cmdmod);
3057 cmdmod = save_cmdmod;
3058 }
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003059
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003060failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003061 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3063 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003066 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003067
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003068 // Not sure if this is necessary.
3069 suppress_errthrow = save_suppress_errthrow;
3070
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003071 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003072 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003073 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003074 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 return ret;
3076}
3077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078/*
3079 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003080 * We don't really need this at runtime, but we do have tests that require it,
3081 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 */
3083 void
3084ex_disassemble(exarg_T *eap)
3085{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003086 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003087 char_u *fname;
3088 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 dfunc_T *dfunc;
3090 isn_T *instr;
3091 int current;
3092 int line_idx = 0;
3093 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003094 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003096 if (STRNCMP(arg, "<lambda>", 8) == 0)
3097 {
3098 arg += 8;
3099 (void)getdigits(&arg);
3100 fname = vim_strnsave(eap->arg, arg - eap->arg);
3101 }
3102 else
3103 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003104 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003105 if (fname == NULL)
3106 {
3107 semsg(_(e_invarg2), eap->arg);
3108 return;
3109 }
3110
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003111 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003112 if (ufunc == NULL)
3113 {
3114 char_u *p = untrans_function_name(fname);
3115
3116 if (p != NULL)
3117 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003118 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003119 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003120 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 if (ufunc == NULL)
3122 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003123 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 return;
3125 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003126 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003127 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3128 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003129 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003131 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 return;
3133 }
3134 if (ufunc->uf_name_exp != NULL)
3135 msg((char *)ufunc->uf_name_exp);
3136 else
3137 msg((char *)ufunc->uf_name);
3138
3139 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3140 instr = dfunc->df_instr;
3141 for (current = 0; current < dfunc->df_instr_count; ++current)
3142 {
3143 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003144 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145
3146 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3147 {
3148 if (current > prev_current)
3149 {
3150 msg_puts("\n\n");
3151 prev_current = current;
3152 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003153 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3154 if (line != NULL)
3155 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 }
3157
3158 switch (iptr->isn_type)
3159 {
3160 case ISN_EXEC:
3161 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3162 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003163 case ISN_EXECCONCAT:
3164 smsg("%4d EXECCONCAT %lld", current,
3165 (long long)iptr->isn_arg.number);
3166 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 case ISN_ECHO:
3168 {
3169 echo_T *echo = &iptr->isn_arg.echo;
3170
3171 smsg("%4d %s %d", current,
3172 echo->echo_with_white ? "ECHO" : "ECHON",
3173 echo->echo_count);
3174 }
3175 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003176 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003177 smsg("%4d EXECUTE %lld", current,
3178 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003179 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003180 case ISN_ECHOMSG:
3181 smsg("%4d ECHOMSG %lld", current,
3182 (long long)(iptr->isn_arg.number));
3183 break;
3184 case ISN_ECHOERR:
3185 smsg("%4d ECHOERR %lld", current,
3186 (long long)(iptr->isn_arg.number));
3187 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003189 case ISN_LOADOUTER:
3190 {
3191 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
3192
3193 if (iptr->isn_arg.number < 0)
3194 smsg("%4d LOAD%s arg[%lld]", current, add,
3195 (long long)(iptr->isn_arg.number
3196 + STACK_FRAME_SIZE));
3197 else
3198 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003199 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 break;
3202 case ISN_LOADV:
3203 smsg("%4d LOADV v:%s", current,
3204 get_vim_var_name(iptr->isn_arg.number));
3205 break;
3206 case ISN_LOADSCRIPT:
3207 {
3208 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003209 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3211 + iptr->isn_arg.script.script_idx;
3212
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003213 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3214 sv->sv_name,
3215 iptr->isn_arg.script.script_idx,
3216 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 }
3218 break;
3219 case ISN_LOADS:
3220 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003221 scriptitem_T *si = SCRIPT_ITEM(
3222 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223
3224 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003225 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 }
3227 break;
3228 case ISN_LOADG:
3229 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3230 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003231 case ISN_LOADB:
3232 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3233 break;
3234 case ISN_LOADW:
3235 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3236 break;
3237 case ISN_LOADT:
3238 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3239 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003240 case ISN_LOADGDICT:
3241 smsg("%4d LOAD g:", current);
3242 break;
3243 case ISN_LOADBDICT:
3244 smsg("%4d LOAD b:", current);
3245 break;
3246 case ISN_LOADWDICT:
3247 smsg("%4d LOAD w:", current);
3248 break;
3249 case ISN_LOADTDICT:
3250 smsg("%4d LOAD t:", current);
3251 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 case ISN_LOADOPT:
3253 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3254 break;
3255 case ISN_LOADENV:
3256 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3257 break;
3258 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003259 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 break;
3261
3262 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003263 case ISN_STOREOUTER:
3264 {
3265 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
3266
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003267 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003268 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003269 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003270 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003271 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003272 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003273 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003275 case ISN_STOREV:
3276 smsg("%4d STOREV v:%s", current,
3277 get_vim_var_name(iptr->isn_arg.number));
3278 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003280 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3281 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003282 case ISN_STOREB:
3283 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3284 break;
3285 case ISN_STOREW:
3286 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3287 break;
3288 case ISN_STORET:
3289 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3290 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003291 case ISN_STORES:
3292 {
3293 scriptitem_T *si = SCRIPT_ITEM(
3294 iptr->isn_arg.loadstore.ls_sid);
3295
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003296 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003297 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 break;
3300 case ISN_STORESCRIPT:
3301 {
3302 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003303 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3305 + iptr->isn_arg.script.script_idx;
3306
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003307 smsg("%4d STORESCRIPT %s-%d in %s", current,
3308 sv->sv_name,
3309 iptr->isn_arg.script.script_idx,
3310 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 }
3312 break;
3313 case ISN_STOREOPT:
3314 smsg("%4d STOREOPT &%s", current,
3315 iptr->isn_arg.storeopt.so_name);
3316 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003317 case ISN_STOREENV:
3318 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3319 break;
3320 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003321 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003322 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 case ISN_STORENR:
3324 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003325 iptr->isn_arg.storenr.stnr_val,
3326 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 break;
3328
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003329 case ISN_STORELIST:
3330 smsg("%4d STORELIST", current);
3331 break;
3332
3333 case ISN_STOREDICT:
3334 smsg("%4d STOREDICT", current);
3335 break;
3336
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 // constants
3338 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003339 smsg("%4d PUSHNR %lld", current,
3340 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 break;
3342 case ISN_PUSHBOOL:
3343 case ISN_PUSHSPEC:
3344 smsg("%4d PUSH %s", current,
3345 get_var_special_name(iptr->isn_arg.number));
3346 break;
3347 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003348#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003350#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 break;
3352 case ISN_PUSHS:
3353 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3354 break;
3355 case ISN_PUSHBLOB:
3356 {
3357 char_u *r;
3358 char_u numbuf[NUMBUFLEN];
3359 char_u *tofree;
3360
3361 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003362 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 vim_free(tofree);
3364 }
3365 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003366 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003367 {
3368 char *name = (char *)iptr->isn_arg.string;
3369
3370 smsg("%4d PUSHFUNC \"%s\"", current,
3371 name == NULL ? "[none]" : name);
3372 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003373 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003374 case ISN_PUSHCHANNEL:
3375#ifdef FEAT_JOB_CHANNEL
3376 {
3377 channel_T *channel = iptr->isn_arg.channel;
3378
3379 smsg("%4d PUSHCHANNEL %d", current,
3380 channel == NULL ? 0 : channel->ch_id);
3381 }
3382#endif
3383 break;
3384 case ISN_PUSHJOB:
3385#ifdef FEAT_JOB_CHANNEL
3386 {
3387 typval_T tv;
3388 char_u *name;
3389
3390 tv.v_type = VAR_JOB;
3391 tv.vval.v_job = iptr->isn_arg.job;
3392 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003393 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003394 }
3395#endif
3396 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 case ISN_PUSHEXC:
3398 smsg("%4d PUSH v:exception", current);
3399 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003400 case ISN_UNLET:
3401 smsg("%4d UNLET%s %s", current,
3402 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3403 iptr->isn_arg.unlet.ul_name);
3404 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003405 case ISN_UNLETENV:
3406 smsg("%4d UNLETENV%s $%s", current,
3407 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3408 iptr->isn_arg.unlet.ul_name);
3409 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003410 case ISN_LOCKCONST:
3411 smsg("%4d LOCKCONST", current);
3412 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003414 smsg("%4d NEWLIST size %lld", current,
3415 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 break;
3417 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003418 smsg("%4d NEWDICT size %lld", current,
3419 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 break;
3421
3422 // function call
3423 case ISN_BCALL:
3424 {
3425 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3426
3427 smsg("%4d BCALL %s(argc %d)", current,
3428 internal_func_name(cbfunc->cbf_idx),
3429 cbfunc->cbf_argcount);
3430 }
3431 break;
3432 case ISN_DCALL:
3433 {
3434 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3435 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3436 + cdfunc->cdf_idx;
3437
3438 smsg("%4d DCALL %s(argc %d)", current,
3439 df->df_ufunc->uf_name_exp != NULL
3440 ? df->df_ufunc->uf_name_exp
3441 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3442 }
3443 break;
3444 case ISN_UCALL:
3445 {
3446 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3447
3448 smsg("%4d UCALL %s(argc %d)", current,
3449 cufunc->cuf_name, cufunc->cuf_argcount);
3450 }
3451 break;
3452 case ISN_PCALL:
3453 {
3454 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3455
3456 smsg("%4d PCALL%s (argc %d)", current,
3457 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3458 }
3459 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003460 case ISN_PCALL_END:
3461 smsg("%4d PCALL end", current);
3462 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 case ISN_RETURN:
3464 smsg("%4d RETURN", current);
3465 break;
3466 case ISN_FUNCREF:
3467 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003468 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003470 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003472 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 }
3474 break;
3475
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003476 case ISN_NEWFUNC:
3477 {
3478 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3479
3480 smsg("%4d NEWFUNC %s %s", current,
3481 newfunc->nf_lambda, newfunc->nf_global);
3482 }
3483 break;
3484
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003485 case ISN_DEF:
3486 {
3487 char_u *name = iptr->isn_arg.string;
3488
3489 smsg("%4d DEF %s", current,
3490 name == NULL ? (char_u *)"" : name);
3491 }
3492 break;
3493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 case ISN_JUMP:
3495 {
3496 char *when = "?";
3497
3498 switch (iptr->isn_arg.jump.jump_when)
3499 {
3500 case JUMP_ALWAYS:
3501 when = "JUMP";
3502 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 case JUMP_AND_KEEP_IF_TRUE:
3504 when = "JUMP_AND_KEEP_IF_TRUE";
3505 break;
3506 case JUMP_IF_FALSE:
3507 when = "JUMP_IF_FALSE";
3508 break;
3509 case JUMP_AND_KEEP_IF_FALSE:
3510 when = "JUMP_AND_KEEP_IF_FALSE";
3511 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003512 case JUMP_IF_COND_FALSE:
3513 when = "JUMP_IF_COND_FALSE";
3514 break;
3515 case JUMP_IF_COND_TRUE:
3516 when = "JUMP_IF_COND_TRUE";
3517 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003519 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 iptr->isn_arg.jump.jump_where);
3521 }
3522 break;
3523
3524 case ISN_FOR:
3525 {
3526 forloop_T *forloop = &iptr->isn_arg.forloop;
3527
3528 smsg("%4d FOR $%d -> %d", current,
3529 forloop->for_idx, forloop->for_end);
3530 }
3531 break;
3532
3533 case ISN_TRY:
3534 {
3535 try_T *try = &iptr->isn_arg.try;
3536
3537 smsg("%4d TRY catch -> %d, finally -> %d", current,
3538 try->try_catch, try->try_finally);
3539 }
3540 break;
3541 case ISN_CATCH:
3542 // TODO
3543 smsg("%4d CATCH", current);
3544 break;
3545 case ISN_ENDTRY:
3546 smsg("%4d ENDTRY", current);
3547 break;
3548 case ISN_THROW:
3549 smsg("%4d THROW", current);
3550 break;
3551
3552 // expression operations on number
3553 case ISN_OPNR:
3554 case ISN_OPFLOAT:
3555 case ISN_OPANY:
3556 {
3557 char *what;
3558 char *ins;
3559
3560 switch (iptr->isn_arg.op.op_type)
3561 {
3562 case EXPR_MULT: what = "*"; break;
3563 case EXPR_DIV: what = "/"; break;
3564 case EXPR_REM: what = "%"; break;
3565 case EXPR_SUB: what = "-"; break;
3566 case EXPR_ADD: what = "+"; break;
3567 default: what = "???"; break;
3568 }
3569 switch (iptr->isn_type)
3570 {
3571 case ISN_OPNR: ins = "OPNR"; break;
3572 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3573 case ISN_OPANY: ins = "OPANY"; break;
3574 default: ins = "???"; break;
3575 }
3576 smsg("%4d %s %s", current, ins, what);
3577 }
3578 break;
3579
3580 case ISN_COMPAREBOOL:
3581 case ISN_COMPARESPECIAL:
3582 case ISN_COMPARENR:
3583 case ISN_COMPAREFLOAT:
3584 case ISN_COMPARESTRING:
3585 case ISN_COMPAREBLOB:
3586 case ISN_COMPARELIST:
3587 case ISN_COMPAREDICT:
3588 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 case ISN_COMPAREANY:
3590 {
3591 char *p;
3592 char buf[10];
3593 char *type;
3594
3595 switch (iptr->isn_arg.op.op_type)
3596 {
3597 case EXPR_EQUAL: p = "=="; break;
3598 case EXPR_NEQUAL: p = "!="; break;
3599 case EXPR_GREATER: p = ">"; break;
3600 case EXPR_GEQUAL: p = ">="; break;
3601 case EXPR_SMALLER: p = "<"; break;
3602 case EXPR_SEQUAL: p = "<="; break;
3603 case EXPR_MATCH: p = "=~"; break;
3604 case EXPR_IS: p = "is"; break;
3605 case EXPR_ISNOT: p = "isnot"; break;
3606 case EXPR_NOMATCH: p = "!~"; break;
3607 default: p = "???"; break;
3608 }
3609 STRCPY(buf, p);
3610 if (iptr->isn_arg.op.op_ic == TRUE)
3611 strcat(buf, "?");
3612 switch(iptr->isn_type)
3613 {
3614 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3615 case ISN_COMPARESPECIAL:
3616 type = "COMPARESPECIAL"; break;
3617 case ISN_COMPARENR: type = "COMPARENR"; break;
3618 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3619 case ISN_COMPARESTRING:
3620 type = "COMPARESTRING"; break;
3621 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3622 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3623 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3624 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3626 default: type = "???"; break;
3627 }
3628
3629 smsg("%4d %s %s", current, type, buf);
3630 }
3631 break;
3632
3633 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3634 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3635
3636 // expression operations
3637 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003638 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003639 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003640 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003641 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003642 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003643 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003644 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3645 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003646 case ISN_SLICE: smsg("%4d SLICE %lld",
3647 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003648 case ISN_GETITEM: smsg("%4d ITEM %lld",
3649 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003650 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3651 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 iptr->isn_arg.string); break;
3653 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3654
3655 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003656 case ISN_CHECKTYPE:
3657 {
3658 char *tofree;
3659
3660 smsg("%4d CHECKTYPE %s stack[%d]", current,
3661 type_name(iptr->isn_arg.type.ct_type, &tofree),
3662 iptr->isn_arg.type.ct_off);
3663 vim_free(tofree);
3664 break;
3665 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003666 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3667 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3668 iptr->isn_arg.checklen.cl_min_len);
3669 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003670 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 case ISN_2BOOL: if (iptr->isn_arg.number)
3672 smsg("%4d INVERT (!val)", current);
3673 else
3674 smsg("%4d 2BOOL (!!val)", current);
3675 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003676 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3677 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003678 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003679 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3680 (long long)(iptr->isn_arg.number));
3681 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003682 case ISN_PUT:
3683 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname,
3684 (long)iptr->isn_arg.put.put_lnum);
3685 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686
Bram Moolenaar02194d22020-10-24 23:08:38 +02003687 // TODO: summarize modifiers
3688 case ISN_CMDMOD:
3689 {
3690 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01003691 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02003692 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3693
3694 buf = alloc(len + 1);
3695 if (buf != NULL)
3696 {
3697 (void)produce_cmdmods(
3698 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3699 smsg("%4d CMDMOD %s", current, buf);
3700 vim_free(buf);
3701 }
3702 break;
3703 }
3704 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003705
Bram Moolenaar792f7862020-11-23 08:31:18 +01003706 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
3707 iptr->isn_arg.unpack.unp_count,
3708 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
3709 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02003710 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3711 iptr->isn_arg.shuffle.shfl_item,
3712 iptr->isn_arg.shuffle.shfl_up);
3713 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 case ISN_DROP: smsg("%4d DROP", current); break;
3715 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003716
3717 out_flush(); // output one line at a time
3718 ui_breakcheck();
3719 if (got_int)
3720 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722}
3723
3724/*
Bram Moolenaar13106602020-10-04 16:06:05 +02003725 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 * list, etc. Mostly like what JavaScript does, except that empty list and
3727 * empty dictionary are FALSE.
3728 */
3729 int
3730tv2bool(typval_T *tv)
3731{
3732 switch (tv->v_type)
3733 {
3734 case VAR_NUMBER:
3735 return tv->vval.v_number != 0;
3736 case VAR_FLOAT:
3737#ifdef FEAT_FLOAT
3738 return tv->vval.v_float != 0.0;
3739#else
3740 break;
3741#endif
3742 case VAR_PARTIAL:
3743 return tv->vval.v_partial != NULL;
3744 case VAR_FUNC:
3745 case VAR_STRING:
3746 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3747 case VAR_LIST:
3748 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3749 case VAR_DICT:
3750 return tv->vval.v_dict != NULL
3751 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3752 case VAR_BOOL:
3753 case VAR_SPECIAL:
3754 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3755 case VAR_JOB:
3756#ifdef FEAT_JOB_CHANNEL
3757 return tv->vval.v_job != NULL;
3758#else
3759 break;
3760#endif
3761 case VAR_CHANNEL:
3762#ifdef FEAT_JOB_CHANNEL
3763 return tv->vval.v_channel != NULL;
3764#else
3765 break;
3766#endif
3767 case VAR_BLOB:
3768 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3769 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003770 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 case VAR_VOID:
3772 break;
3773 }
3774 return FALSE;
3775}
3776
Bram Moolenaarea2d4072020-11-12 12:08:51 +01003777 void
3778emsg_using_string_as(typval_T *tv, int as_number)
3779{
3780 semsg(_(as_number ? e_using_string_as_number_str
3781 : e_using_string_as_bool_str),
3782 tv->vval.v_string == NULL
3783 ? (char_u *)"" : tv->vval.v_string);
3784}
3785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786/*
3787 * If "tv" is a string give an error and return FAIL.
3788 */
3789 int
3790check_not_string(typval_T *tv)
3791{
3792 if (tv->v_type == VAR_STRING)
3793 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01003794 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 clear_tv(tv);
3796 return FAIL;
3797 }
3798 return OK;
3799}
3800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801
3802#endif // FEAT_EVAL