blob: 5a943542950ba152a9432707cdd2c1acbea473a0 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020027 int tcd_frame_idx; // ec_frame_idx when ISN_TRY was encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028 int tcd_catch_idx; // instruction of the first catch
29 int tcd_finally_idx; // instruction of the finally block
30 int tcd_caught; // catch block entered
31 int tcd_return; // when TRUE return from end of :finally
32} trycmd_T;
33
34
35// A stack is used to store:
36// - arguments passed to a :def function
37// - info about the calling function, to use when returning
38// - local variables
39// - temporary values
40//
41// In detail (FP == Frame Pointer):
42// arg1 first argument from caller (if present)
43// arg2 second argument from caller (if present)
44// extra_arg1 any missing optional argument default value
45// FP -> cur_func calling function
46// current previous instruction pointer
47// frame_ptr previous Frame Pointer
48// var1 space for local variable
49// var2 space for local variable
50// .... fixed space for max. number of local variables
51// temp temporary values
52// .... flexible space for temporary values (can grow big)
53
54/*
55 * Execution context.
56 */
57typedef struct {
58 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020059 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020061 garray_T *ec_outer_stack; // stack used for closures
62 int ec_outer_frame; // stack frame in ec_outer_stack
63
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064 garray_T ec_trystack; // stack of trycmd_T values
65 int ec_in_catch; // when TRUE in catch or finally block
66
67 int ec_dfunc_idx; // current function index
68 isn_T *ec_instr; // array with instructions
69 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020070
71 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072} ectx_T;
73
74// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020075#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010076
Bram Moolenaar418f1df2020-08-12 21:34:49 +020077 void
78to_string_error(vartype_T vartype)
79{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020080 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020081}
82
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010084 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 */
86 static int
87ufunc_argcount(ufunc_T *ufunc)
88{
89 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
90}
91
92/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010093 * Set the instruction index, depending on omitted arguments, where the default
94 * values are to be computed. If all optional arguments are present, start
95 * with the function body.
96 * The expression evaluation is at the start of the instructions:
97 * 0 -> EVAL default1
98 * STORE arg[-2]
99 * 1 -> EVAL default2
100 * STORE arg[-1]
101 * 2 -> function body
102 */
103 static void
104init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
105{
106 if (ufunc->uf_def_args.ga_len == 0)
107 ectx->ec_iidx = 0;
108 else
109 {
110 int defcount = ufunc->uf_args.ga_len - argcount;
111
112 // If there is a varargs argument defcount can be negative, no defaults
113 // to evaluate then.
114 if (defcount < 0)
115 defcount = 0;
116 ectx->ec_iidx = ufunc->uf_def_arg_idx[
117 ufunc->uf_def_args.ga_len - defcount];
118 }
119}
120
121/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200122 * Create a new list from "count" items at the bottom of the stack.
123 * When "count" is zero an empty list is added to the stack.
124 */
125 static int
126exe_newlist(int count, ectx_T *ectx)
127{
128 list_T *list = list_alloc_with_items(count);
129 int idx;
130 typval_T *tv;
131
132 if (list == NULL)
133 return FAIL;
134 for (idx = 0; idx < count; ++idx)
135 list_set_item(list, idx, STACK_TV_BOT(idx - count));
136
137 if (count > 0)
138 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200139 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200140 return FAIL;
141 else
142 ++ectx->ec_stack.ga_len;
143 tv = STACK_TV_BOT(-1);
144 tv->v_type = VAR_LIST;
145 tv->vval.v_list = list;
146 ++list->lv_refcount;
147 return OK;
148}
149
150/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 * Call compiled function "cdf_idx" from compiled code.
152 *
153 * Stack has:
154 * - current arguments (already there)
155 * - omitted optional argument (default values) added here
156 * - stack frame:
157 * - pointer to calling function
158 * - Index of next instruction in calling function
159 * - previous frame pointer
160 * - reserved space for local variables
161 */
162 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200163call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200165 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
167 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200168 int arg_to_add;
169 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200170 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200172 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173
174 if (dfunc->df_deleted)
175 {
176 emsg_funcname(e_func_deleted, ufunc->uf_name);
177 return FAIL;
178 }
179
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200180 if (ufunc->uf_va_name != NULL)
181 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200182 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200183 // Stack at time of call with 2 varargs:
184 // normal_arg
185 // optional_arg
186 // vararg_1
187 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200188 // After creating the list:
189 // normal_arg
190 // optional_arg
191 // vararg-list
192 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200193 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200194 // After creating the list
195 // normal_arg
196 // (space for optional_arg)
197 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200198 vararg_count = argcount - ufunc->uf_args.ga_len;
199 if (vararg_count < 0)
200 vararg_count = 0;
201 else
202 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200203 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200204 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200205
206 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200207 }
208
Bram Moolenaarfe270812020-04-11 22:31:27 +0200209 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200210 if (arg_to_add < 0)
211 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200212 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200213 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200214 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200215 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200216 return FAIL;
217 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200218
219 // Reserve space for:
220 // - missing arguments
221 // - stack frame
222 // - local variables
223 // - if needed: a counter for number of closures created in
224 // ectx->ec_funcrefs.
225 varcount = dfunc->df_varcount + dfunc->df_has_closure;
226 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
227 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 return FAIL;
229
Bram Moolenaarfe270812020-04-11 22:31:27 +0200230 // Move the vararg-list to below the missing optional arguments.
231 if (vararg_count > 0 && arg_to_add > 0)
232 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100233
234 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200235 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200236 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200237 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238
239 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
241 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200242 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
243 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244
245 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200246 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200248 if (dfunc->df_has_closure)
249 {
250 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
251
252 tv->v_type = VAR_NUMBER;
253 tv->vval.v_number = 0;
254 }
255 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256
257 // Set execution state to the start of the called function.
258 ectx->ec_dfunc_idx = cdf_idx;
259 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200260 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
261 if (entry != NULL)
262 {
263 // Set the script context to the script where the function was defined.
264 // TODO: save more than the SID?
265 entry->es_save_sid = current_sctx.sc_sid;
266 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
267 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100268
269 // Decide where to start execution, handles optional arguments.
270 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271
272 return OK;
273}
274
275// Get pointer to item in the stack.
276#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
277
278/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200279 * Used when returning from a function: Check if any closure is still
280 * referenced. If so then move the arguments and variables to a separate piece
281 * of stack to be used when the closure is called.
282 * When "free_arguments" is TRUE the arguments are to be freed.
283 * Returns FAIL when out of memory.
284 */
285 static int
286handle_closure_in_use(ectx_T *ectx, int free_arguments)
287{
288 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
289 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200290 int argcount;
291 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200292 int idx;
293 typval_T *tv;
294 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200295 garray_T *gap = &ectx->ec_funcrefs;
296 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200297
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200298 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200299 return OK; // function was freed
300 if (dfunc->df_has_closure == 0)
301 return OK; // no closures
302 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
303 closure_count = tv->vval.v_number;
304 if (closure_count == 0)
305 return OK; // no funcrefs created
306
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200307 argcount = ufunc_argcount(dfunc->df_ufunc);
308 top = ectx->ec_frame_idx - argcount;
309
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200310 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200311 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200312 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200313 partial_T *pt;
314 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200315
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200316 if (off < 0)
317 continue; // count is off or already done
318 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200319 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200320 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200321 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200322 int i;
323
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200324 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200325 // unreferenced on return.
326 for (i = 0; i < dfunc->df_varcount; ++i)
327 {
328 typval_T *stv = STACK_TV(ectx->ec_frame_idx
329 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200330 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200331 --refcount;
332 }
333 if (refcount > 1)
334 {
335 closure_in_use = TRUE;
336 break;
337 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200338 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200339 }
340
341 if (closure_in_use)
342 {
343 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
344 typval_T *stack;
345
346 // A closure is using the arguments and/or local variables.
347 // Move them to the called function.
348 if (funcstack == NULL)
349 return FAIL;
350 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
351 + dfunc->df_varcount;
352 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
353 funcstack->fs_ga.ga_data = stack;
354 if (stack == NULL)
355 {
356 vim_free(funcstack);
357 return FAIL;
358 }
359
360 // Move or copy the arguments.
361 for (idx = 0; idx < argcount; ++idx)
362 {
363 tv = STACK_TV(top + idx);
364 if (free_arguments)
365 {
366 *(stack + idx) = *tv;
367 tv->v_type = VAR_UNKNOWN;
368 }
369 else
370 copy_tv(tv, stack + idx);
371 }
372 // Move the local variables.
373 for (idx = 0; idx < dfunc->df_varcount; ++idx)
374 {
375 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200376
377 // Do not copy a partial created for a local function.
378 // TODO: this won't work if the closure actually uses it. But when
379 // keeping it it gets complicated: it will create a reference cycle
380 // inside the partial, thus needs special handling for garbage
381 // collection.
382 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
383 {
384 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200385
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200386 for (i = 0; i < closure_count; ++i)
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200387 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200388 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
389 - closure_count + i];
390 if (tv->vval.v_partial == pt)
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200391 break;
392 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200393 if (i < closure_count)
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200394 continue;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200395 }
396
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200397 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
398 tv->v_type = VAR_UNKNOWN;
399 }
400
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200401 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200402 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200403 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
404 - closure_count + idx];
405 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200406 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200407 ++funcstack->fs_refcount;
408 pt->pt_funcstack = funcstack;
409 pt->pt_ectx_stack = &funcstack->fs_ga;
410 pt->pt_ectx_frame = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200411 }
412 }
413 }
414
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200415 for (idx = 0; idx < closure_count; ++idx)
416 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
417 - closure_count + idx]);
418 gap->ga_len -= closure_count;
419 if (gap->ga_len == 0)
420 ga_clear(gap);
421
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200422 return OK;
423}
424
425/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100426 * Return from the current function.
427 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200428 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100429func_return(ectx_T *ectx)
430{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100431 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200432 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
433 + ectx->ec_dfunc_idx;
434 int argcount = ufunc_argcount(dfunc->df_ufunc);
435 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200436 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437
438 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200439 entry = estack_pop();
440 if (entry != NULL)
441 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100442
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200443 if (handle_closure_in_use(ectx, TRUE) == FAIL)
444 return FAIL;
445
446 // Clear the arguments.
447 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
448 clear_tv(STACK_TV(idx));
449
450 // Clear local variables and temp values, but not the return value.
451 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100452 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100453 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100454
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100455 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200456 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
457 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
458 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100459 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
460 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100461
462 // Reset the stack to the position before the call, move the return value
463 // to the top of the stack.
464 idx = ectx->ec_stack.ga_len - 1;
465 ectx->ec_stack.ga_len = top + 1;
466 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200467
468 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100469}
470
471#undef STACK_TV
472
473/*
474 * Prepare arguments and rettv for calling a builtin or user function.
475 */
476 static int
477call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
478{
479 int idx;
480 typval_T *tv;
481
482 // Move arguments from bottom of the stack to argvars[] and add terminator.
483 for (idx = 0; idx < argcount; ++idx)
484 argvars[idx] = *STACK_TV_BOT(idx - argcount);
485 argvars[argcount].v_type = VAR_UNKNOWN;
486
487 // Result replaces the arguments on the stack.
488 if (argcount > 0)
489 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200490 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100491 return FAIL;
492 else
493 ++ectx->ec_stack.ga_len;
494
495 // Default return value is zero.
496 tv = STACK_TV_BOT(-1);
497 tv->v_type = VAR_NUMBER;
498 tv->vval.v_number = 0;
499
500 return OK;
501}
502
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200503// Ugly global to avoid passing the execution context around through many
504// layers.
505static ectx_T *current_ectx = NULL;
506
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100507/*
508 * Call a builtin function by index.
509 */
510 static int
511call_bfunc(int func_idx, int argcount, ectx_T *ectx)
512{
513 typval_T argvars[MAX_FUNC_ARGS];
514 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200515 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200516 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100517
518 if (call_prepare(argcount, argvars, ectx) == FAIL)
519 return FAIL;
520
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200521 // Call the builtin function. Set "current_ectx" so that when it
522 // recursively invokes call_def_function() a closure context can be set.
523 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200525 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100526
527 // Clear the arguments.
528 for (idx = 0; idx < argcount; ++idx)
529 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200530
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200531 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200532 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 return OK;
534}
535
536/*
537 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100538 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100539 */
540 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100541call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100542{
543 typval_T argvars[MAX_FUNC_ARGS];
544 funcexe_T funcexe;
545 int error;
546 int idx;
Bram Moolenaared677f52020-08-12 16:38:10 +0200547 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200549 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200550 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
551 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200552 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100553 {
554 // The function has been compiled, can call it quickly. For a function
555 // that was defined later: we can call it directly next time.
556 if (iptr != NULL)
557 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100558 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100559 iptr->isn_type = ISN_DCALL;
560 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
561 iptr->isn_arg.dfunc.cdf_argcount = argcount;
562 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100564 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100565
566 if (call_prepare(argcount, argvars, ectx) == FAIL)
567 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200568 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569 funcexe.evaluate = TRUE;
570
571 // Call the user function. Result goes in last position on the stack.
572 // TODO: add selfdict if there is one
573 error = call_user_func_check(ufunc, argcount, argvars,
574 STACK_TV_BOT(-1), &funcexe, NULL);
575
576 // Clear the arguments.
577 for (idx = 0; idx < argcount; ++idx)
578 clear_tv(&argvars[idx]);
579
580 if (error != FCERR_NONE)
581 {
582 user_func_error(error, ufunc->uf_name);
583 return FAIL;
584 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200585 if (called_emsg > called_emsg_before)
586 // Error other than from calling the function itself.
587 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588 return OK;
589}
590
591/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200592 * Return TRUE if an error was given or CTRL-C was pressed.
593 */
594 static int
595vim9_aborting(int prev_called_emsg)
596{
597 return called_emsg > prev_called_emsg || got_int || did_throw;
598}
599
600/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 * Execute a function by "name".
602 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100603 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604 * Returns FAIL if not found without an error message.
605 */
606 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100607call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608{
609 ufunc_T *ufunc;
610
611 if (builtin_function(name, -1))
612 {
613 int func_idx = find_internal_func(name);
614
615 if (func_idx < 0)
616 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200617 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 return FAIL;
619 return call_bfunc(func_idx, argcount, ectx);
620 }
621
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200622 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200623
624 if (ufunc == NULL)
625 {
626 int called_emsg_before = called_emsg;
627
628 if (script_autoload(name, TRUE))
629 // loaded a package, search for the function again
630 ufunc = find_func(name, FALSE, NULL);
631 if (vim9_aborting(called_emsg_before))
632 return FAIL; // bail out if loading the script caused an error
633 }
634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100636 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637
638 return FAIL;
639}
640
641 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200642call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200644 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200645 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100646 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200647 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100648
649 if (tv->v_type == VAR_PARTIAL)
650 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200651 partial_T *pt = tv->vval.v_partial;
652 int i;
653
654 if (pt->pt_argc > 0)
655 {
656 // Make space for arguments from the partial, shift the "argcount"
657 // arguments up.
658 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
659 return FAIL;
660 for (i = 1; i <= argcount; ++i)
661 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
662 ectx->ec_stack.ga_len += pt->pt_argc;
663 argcount += pt->pt_argc;
664
665 // copy the arguments from the partial onto the stack
666 for (i = 0; i < pt->pt_argc; ++i)
667 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
668 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669
670 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200671 {
672 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
673
674 // closure may need the function context where it was defined
675 ectx->ec_outer_stack = pt->pt_ectx_stack;
676 ectx->ec_outer_frame = pt->pt_ectx_frame;
677
678 return ret;
679 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100680 name = pt->pt_name;
681 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200682 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200684 if (name != NULL)
685 {
686 char_u fname_buf[FLEN_FIXED + 1];
687 char_u *tofree = NULL;
688 int error = FCERR_NONE;
689 char_u *fname;
690
691 // May need to translate <SNR>123_ to K_SNR.
692 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
693 if (error != FCERR_NONE)
694 res = FAIL;
695 else
696 res = call_by_name(fname, argcount, ectx, NULL);
697 vim_free(tofree);
698 }
699
700 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701 {
702 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200703 semsg(_(e_unknownfunc),
704 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705 return FAIL;
706 }
707 return OK;
708}
709
710/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200711 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
712 * TRUE.
713 */
714 static int
715error_if_locked(int lock, char *error)
716{
717 if (lock & (VAR_LOCKED | VAR_FIXED))
718 {
719 emsg(_(error));
720 return TRUE;
721 }
722 return FALSE;
723}
724
725/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100726 * Store "tv" in variable "name".
727 * This is for s: and g: variables.
728 */
729 static void
730store_var(char_u *name, typval_T *tv)
731{
732 funccal_entry_T entry;
733
734 save_funccal(&entry);
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200735 set_var_const(name, NULL, tv, FALSE, ASSIGN_NO_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100736 restore_funccal();
737}
738
Bram Moolenaard3aac292020-04-19 14:32:17 +0200739
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100740/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 * Execute a function by "name".
742 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100743 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744 */
745 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100746call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747{
Bram Moolenaared677f52020-08-12 16:38:10 +0200748 int called_emsg_before = called_emsg;
749 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750
Bram Moolenaared677f52020-08-12 16:38:10 +0200751 res = call_by_name(name, argcount, ectx, iptr);
752 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200754 dictitem_T *v;
755
756 v = find_var(name, NULL, FALSE);
757 if (v == NULL)
758 {
759 semsg(_(e_unknownfunc), name);
760 return FAIL;
761 }
762 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
763 {
764 semsg(_(e_unknownfunc), name);
765 return FAIL;
766 }
767 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200769 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770}
771
772/*
773 * Call a "def" function from old Vim script.
774 * Return OK or FAIL.
775 */
776 int
777call_def_function(
778 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200779 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100780 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200781 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 typval_T *rettv) // return value
783{
784 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200785 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200786 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787 typval_T *tv;
788 int idx;
789 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100790 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200791 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200792 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200793 int called_emsg_before = called_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200794 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100795
796// Get pointer to item in the stack.
797#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
798
799// Get pointer to item at the bottom of the stack, -1 is the bottom.
800#undef STACK_TV_BOT
801#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
802
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200803// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200804#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 +0100805
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200806// Like STACK_TV_VAR but use the outer scope
807#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
808
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200809 if (ufunc->uf_def_status == UF_NOT_COMPILED
810 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200811 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200812 {
813 if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200814 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200815 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200816 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200817 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200818
Bram Moolenaar09689a02020-05-09 22:50:08 +0200819 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200820 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200821 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
822 + ufunc->uf_dfunc_idx;
823 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200824 {
825 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200826 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200827 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200830 CLEAR_FIELD(ectx);
831 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
832 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
833 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
834 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200836 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837
838 // Put arguments on the stack.
839 for (idx = 0; idx < argc; ++idx)
840 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200841 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200842 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
843 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200844 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 copy_tv(&argv[idx], STACK_TV_BOT(0));
846 ++ectx.ec_stack.ga_len;
847 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200848
849 // Turn varargs into a list. Empty list if no args.
850 if (ufunc->uf_va_name != NULL)
851 {
852 int vararg_count = argc - ufunc->uf_args.ga_len;
853
854 if (vararg_count < 0)
855 vararg_count = 0;
856 else
857 argc -= vararg_count;
858 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200859 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200860
861 // Check the type of the list items.
862 tv = STACK_TV_BOT(-1);
863 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +0200864 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200865 && ufunc->uf_va_type->tt_member != &t_any
866 && tv->vval.v_list != NULL)
867 {
868 type_T *expected = ufunc->uf_va_type->tt_member;
869 listitem_T *li = tv->vval.v_list->lv_first;
870
871 for (idx = 0; idx < vararg_count; ++idx)
872 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200873 if (check_typval_type(expected, &li->li_tv,
874 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200875 goto failed_early;
876 li = li->li_next;
877 }
878 }
879
Bram Moolenaar23e03252020-04-12 22:22:31 +0200880 if (defcount > 0)
881 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200882 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200883 --ectx.ec_stack.ga_len;
884 }
885
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100886 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200887 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100888 if (defcount > 0)
889 for (idx = 0; idx < defcount; ++idx)
890 {
891 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
892 ++ectx.ec_stack.ga_len;
893 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200894 if (ufunc->uf_va_name != NULL)
895 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896
897 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200898 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
899 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200901 if (partial != NULL)
902 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200903 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
904 {
905 // TODO: is this always the right way?
906 ectx.ec_outer_stack = &current_ectx->ec_stack;
907 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
908 }
909 else
910 {
911 ectx.ec_outer_stack = partial->pt_ectx_stack;
912 ectx.ec_outer_frame = partial->pt_ectx_frame;
913 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200914 }
915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100916 // dummy frame entries
917 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
918 {
919 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
920 ++ectx.ec_stack.ga_len;
921 }
922
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200923 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200924 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200925 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
926 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200928 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200929 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200930 ectx.ec_stack.ga_len += dfunc->df_varcount;
931 if (dfunc->df_has_closure)
932 {
933 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
934 STACK_TV_VAR(idx)->vval.v_number = 0;
935 ++ectx.ec_stack.ga_len;
936 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200937
938 ectx.ec_instr = dfunc->df_instr;
939 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100940
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200941 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +0200942 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200943 estack_push_ufunc(ufunc, 1);
944 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200945 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
946
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200947 // Do turn errors into exceptions.
948 suppress_errthrow = FALSE;
949
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100950 // Decide where to start execution, handles optional arguments.
951 init_instr_idx(ufunc, argc, &ectx);
952
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 for (;;)
954 {
955 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100956
Bram Moolenaar270d0382020-05-15 21:42:53 +0200957 if (++breakcheck_count >= 100)
958 {
959 line_breakcheck();
960 breakcheck_count = 0;
961 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100962 if (got_int)
963 {
964 // Turn CTRL-C into an exception.
965 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100966 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100967 goto failed;
968 did_throw = TRUE;
969 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970
Bram Moolenaara26b9702020-04-18 19:53:28 +0200971 if (did_emsg && msg_list != NULL && *msg_list != NULL)
972 {
973 // Turn an error message into an exception.
974 did_emsg = FALSE;
975 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
976 goto failed;
977 did_throw = TRUE;
978 *msg_list = NULL;
979 }
980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 if (did_throw && !ectx.ec_in_catch)
982 {
983 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100984 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985
986 // An exception jumps to the first catch, finally, or returns from
987 // the current function.
988 if (trystack->ga_len > 0)
989 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200990 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991 {
992 // jump to ":catch" or ":finally"
993 ectx.ec_in_catch = TRUE;
994 ectx.ec_iidx = trycmd->tcd_catch_idx;
995 }
996 else
997 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200998 // Not inside try or need to return from current functions.
999 // Push a dummy return value.
1000 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1001 goto failed;
1002 tv = STACK_TV_BOT(0);
1003 tv->v_type = VAR_NUMBER;
1004 tv->vval.v_number = 0;
1005 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001006 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001008 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001009 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001010 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1011 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 goto done;
1013 }
1014
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001015 if (func_return(&ectx) == FAIL)
1016 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001017 }
1018 continue;
1019 }
1020
1021 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1022 switch (iptr->isn_type)
1023 {
1024 // execute Ex command line
1025 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001026 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001027 do_cmdline_cmd(iptr->isn_arg.string);
1028 break;
1029
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001030 // execute Ex command from pieces on the stack
1031 case ISN_EXECCONCAT:
1032 {
1033 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001034 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001035 int pass;
1036 int i;
1037 char_u *cmd = NULL;
1038 char_u *str;
1039
1040 for (pass = 1; pass <= 2; ++pass)
1041 {
1042 for (i = 0; i < count; ++i)
1043 {
1044 tv = STACK_TV_BOT(i - count);
1045 str = tv->vval.v_string;
1046 if (str != NULL && *str != NUL)
1047 {
1048 if (pass == 2)
1049 STRCPY(cmd + len, str);
1050 len += STRLEN(str);
1051 }
1052 if (pass == 2)
1053 clear_tv(tv);
1054 }
1055 if (pass == 1)
1056 {
1057 cmd = alloc(len + 1);
1058 if (cmd == NULL)
1059 goto failed;
1060 len = 0;
1061 }
1062 }
1063
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001064 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001065 do_cmdline_cmd(cmd);
1066 vim_free(cmd);
1067 }
1068 break;
1069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070 // execute :echo {string} ...
1071 case ISN_ECHO:
1072 {
1073 int count = iptr->isn_arg.echo.echo_count;
1074 int atstart = TRUE;
1075 int needclr = TRUE;
1076
1077 for (idx = 0; idx < count; ++idx)
1078 {
1079 tv = STACK_TV_BOT(idx - count);
1080 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1081 &atstart, &needclr);
1082 clear_tv(tv);
1083 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001084 if (needclr)
1085 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086 ectx.ec_stack.ga_len -= count;
1087 }
1088 break;
1089
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001090 // :execute {string} ...
1091 // :echomsg {string} ...
1092 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001093 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001094 case ISN_ECHOMSG:
1095 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001096 {
1097 int count = iptr->isn_arg.number;
1098 garray_T ga;
1099 char_u buf[NUMBUFLEN];
1100 char_u *p;
1101 int len;
1102 int failed = FALSE;
1103
1104 ga_init2(&ga, 1, 80);
1105 for (idx = 0; idx < count; ++idx)
1106 {
1107 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001108 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001109 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001110 if (tv->v_type == VAR_CHANNEL
1111 || tv->v_type == VAR_JOB)
1112 {
1113 SOURCING_LNUM = iptr->isn_lnum;
1114 emsg(_(e_inval_string));
1115 break;
1116 }
1117 else
1118 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001119 }
1120 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001121 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001122
1123 len = (int)STRLEN(p);
1124 if (ga_grow(&ga, len + 2) == FAIL)
1125 failed = TRUE;
1126 else
1127 {
1128 if (ga.ga_len > 0)
1129 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1130 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1131 ga.ga_len += len;
1132 }
1133 clear_tv(tv);
1134 }
1135 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001136 if (failed)
1137 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001138
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001139 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001140 {
1141 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001142 {
1143 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001144 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaar430deb12020-08-23 16:29:11 +02001145 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001146 else
1147 {
1148 msg_sb_eol();
1149 if (iptr->isn_type == ISN_ECHOMSG)
1150 {
1151 msg_attr(ga.ga_data, echo_attr);
1152 out_flush();
1153 }
1154 else
1155 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001156 SOURCING_LNUM = iptr->isn_lnum;
1157 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001158 }
1159 }
1160 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001161 ga_clear(&ga);
1162 }
1163 break;
1164
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165 // load local variable or argument
1166 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001167 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 goto failed;
1169 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1170 ++ectx.ec_stack.ga_len;
1171 break;
1172
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001173 // load variable or argument from outer scope
1174 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001175 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001176 goto failed;
1177 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1178 STACK_TV_BOT(0));
1179 ++ectx.ec_stack.ga_len;
1180 break;
1181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 // load v: variable
1183 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001184 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 goto failed;
1186 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1187 ++ectx.ec_stack.ga_len;
1188 break;
1189
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001190 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191 case ISN_LOADSCRIPT:
1192 {
1193 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001194 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195 svar_T *sv;
1196
1197 sv = ((svar_T *)si->sn_var_vals.ga_data)
1198 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001199 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 goto failed;
1201 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1202 ++ectx.ec_stack.ga_len;
1203 }
1204 break;
1205
1206 // load s: variable in old script
1207 case ISN_LOADS:
1208 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001209 hashtab_T *ht = &SCRIPT_VARS(
1210 iptr->isn_arg.loadstore.ls_sid);
1211 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 if (di == NULL)
1215 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001216 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001217 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001218 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 }
1220 else
1221 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001222 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223 goto failed;
1224 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1225 ++ectx.ec_stack.ga_len;
1226 }
1227 }
1228 break;
1229
Bram Moolenaard3aac292020-04-19 14:32:17 +02001230 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001232 case ISN_LOADB:
1233 case ISN_LOADW:
1234 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001236 dictitem_T *di = NULL;
1237 hashtab_T *ht = NULL;
1238 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001239
Bram Moolenaard3aac292020-04-19 14:32:17 +02001240 switch (iptr->isn_type)
1241 {
1242 case ISN_LOADG:
1243 ht = get_globvar_ht();
1244 namespace = 'g';
1245 break;
1246 case ISN_LOADB:
1247 ht = &curbuf->b_vars->dv_hashtab;
1248 namespace = 'b';
1249 break;
1250 case ISN_LOADW:
1251 ht = &curwin->w_vars->dv_hashtab;
1252 namespace = 'w';
1253 break;
1254 case ISN_LOADT:
1255 ht = &curtab->tp_vars->dv_hashtab;
1256 namespace = 't';
1257 break;
1258 default: // Cannot reach here
1259 goto failed;
1260 }
1261 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 if (di == NULL)
1264 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001265 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001266 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001267 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001268 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 }
1270 else
1271 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001272 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 goto failed;
1274 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1275 ++ectx.ec_stack.ga_len;
1276 }
1277 }
1278 break;
1279
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001280 // load g:/b:/w:/t: namespace
1281 case ISN_LOADGDICT:
1282 case ISN_LOADBDICT:
1283 case ISN_LOADWDICT:
1284 case ISN_LOADTDICT:
1285 {
1286 dict_T *d = NULL;
1287
1288 switch (iptr->isn_type)
1289 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001290 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1291 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1292 case ISN_LOADWDICT: d = curwin->w_vars; break;
1293 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001294 default: // Cannot reach here
1295 goto failed;
1296 }
1297 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1298 goto failed;
1299 tv = STACK_TV_BOT(0);
1300 tv->v_type = VAR_DICT;
1301 tv->v_lock = 0;
1302 tv->vval.v_dict = d;
1303 ++ectx.ec_stack.ga_len;
1304 }
1305 break;
1306
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307 // load &option
1308 case ISN_LOADOPT:
1309 {
1310 typval_T optval;
1311 char_u *name = iptr->isn_arg.string;
1312
Bram Moolenaara8c17702020-04-01 21:17:24 +02001313 // This is not expected to fail, name is checked during
1314 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001315 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001317 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001318 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 *STACK_TV_BOT(0) = optval;
1320 ++ectx.ec_stack.ga_len;
1321 }
1322 break;
1323
1324 // load $ENV
1325 case ISN_LOADENV:
1326 {
1327 typval_T optval;
1328 char_u *name = iptr->isn_arg.string;
1329
Bram Moolenaar270d0382020-05-15 21:42:53 +02001330 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001332 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001333 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334 *STACK_TV_BOT(0) = optval;
1335 ++ectx.ec_stack.ga_len;
1336 }
1337 break;
1338
1339 // load @register
1340 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001341 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 goto failed;
1343 tv = STACK_TV_BOT(0);
1344 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001345 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 tv->vval.v_string = get_reg_contents(
1347 iptr->isn_arg.number, GREG_EXPR_SRC);
1348 ++ectx.ec_stack.ga_len;
1349 break;
1350
1351 // store local variable
1352 case ISN_STORE:
1353 --ectx.ec_stack.ga_len;
1354 tv = STACK_TV_VAR(iptr->isn_arg.number);
1355 clear_tv(tv);
1356 *tv = *STACK_TV_BOT(0);
1357 break;
1358
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001359 // store variable or argument in outer scope
1360 case ISN_STOREOUTER:
1361 --ectx.ec_stack.ga_len;
1362 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1363 clear_tv(tv);
1364 *tv = *STACK_TV_BOT(0);
1365 break;
1366
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001367 // store s: variable in old script
1368 case ISN_STORES:
1369 {
1370 hashtab_T *ht = &SCRIPT_VARS(
1371 iptr->isn_arg.loadstore.ls_sid);
1372 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001373 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001374
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001375 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001376 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001377 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001378 else
1379 {
1380 clear_tv(&di->di_tv);
1381 di->di_tv = *STACK_TV_BOT(0);
1382 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001383 }
1384 break;
1385
1386 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 case ISN_STORESCRIPT:
1388 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001389 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390 iptr->isn_arg.script.script_sid);
1391 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1392 + iptr->isn_arg.script.script_idx;
1393
1394 --ectx.ec_stack.ga_len;
1395 clear_tv(sv->sv_tv);
1396 *sv->sv_tv = *STACK_TV_BOT(0);
1397 }
1398 break;
1399
1400 // store option
1401 case ISN_STOREOPT:
1402 {
1403 long n = 0;
1404 char_u *s = NULL;
1405 char *msg;
1406
1407 --ectx.ec_stack.ga_len;
1408 tv = STACK_TV_BOT(0);
1409 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001410 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001412 if (s == NULL)
1413 s = (char_u *)"";
1414 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001416 // must be VAR_NUMBER, CHECKTYPE makes sure
1417 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1419 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001420 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 if (msg != NULL)
1422 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001423 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001425 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001426 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 }
1428 break;
1429
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001430 // store $ENV
1431 case ISN_STOREENV:
1432 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001433 tv = STACK_TV_BOT(0);
1434 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1435 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001436 break;
1437
1438 // store @r
1439 case ISN_STOREREG:
1440 {
1441 int reg = iptr->isn_arg.number;
1442
1443 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001444 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001445 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001446 tv_get_string(tv), -1, FALSE);
1447 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001448 }
1449 break;
1450
1451 // store v: variable
1452 case ISN_STOREV:
1453 --ectx.ec_stack.ga_len;
1454 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1455 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001456 // should not happen, type is checked when compiling
1457 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001458 break;
1459
Bram Moolenaard3aac292020-04-19 14:32:17 +02001460 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001462 case ISN_STOREB:
1463 case ISN_STOREW:
1464 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 {
1466 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001467 hashtab_T *ht;
1468 switch (iptr->isn_type)
1469 {
1470 case ISN_STOREG:
1471 ht = get_globvar_ht();
1472 break;
1473 case ISN_STOREB:
1474 ht = &curbuf->b_vars->dv_hashtab;
1475 break;
1476 case ISN_STOREW:
1477 ht = &curwin->w_vars->dv_hashtab;
1478 break;
1479 case ISN_STORET:
1480 ht = &curtab->tp_vars->dv_hashtab;
1481 break;
1482 default: // Cannot reach here
1483 goto failed;
1484 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485
1486 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001487 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001489 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001490 else
1491 {
1492 clear_tv(&di->di_tv);
1493 di->di_tv = *STACK_TV_BOT(0);
1494 }
1495 }
1496 break;
1497
1498 // store number in local variable
1499 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001500 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001501 clear_tv(tv);
1502 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001503 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 break;
1505
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001506 // store value in list variable
1507 case ISN_STORELIST:
1508 {
1509 typval_T *tv_idx = STACK_TV_BOT(-2);
1510 varnumber_T lidx = tv_idx->vval.v_number;
1511 typval_T *tv_list = STACK_TV_BOT(-1);
1512 list_T *list = tv_list->vval.v_list;
1513
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001514 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001515 if (lidx < 0 && list->lv_len + lidx >= 0)
1516 // negative index is relative to the end
1517 lidx = list->lv_len + lidx;
1518 if (lidx < 0 || lidx > list->lv_len)
1519 {
1520 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001521 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001522 }
1523 tv = STACK_TV_BOT(-3);
1524 if (lidx < list->lv_len)
1525 {
1526 listitem_T *li = list_find(list, lidx);
1527
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001528 if (error_if_locked(li->li_tv.v_lock,
1529 e_cannot_change_list_item))
1530 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001531 // overwrite existing list item
1532 clear_tv(&li->li_tv);
1533 li->li_tv = *tv;
1534 }
1535 else
1536 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001537 if (error_if_locked(list->lv_lock,
1538 e_cannot_change_list))
1539 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001540 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001541 if (list_append_tv(list, tv) == FAIL)
1542 goto failed;
1543 clear_tv(tv);
1544 }
1545 clear_tv(tv_idx);
1546 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001547 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001548 }
1549 break;
1550
1551 // store value in dict variable
1552 case ISN_STOREDICT:
1553 {
1554 typval_T *tv_key = STACK_TV_BOT(-2);
1555 char_u *key = tv_key->vval.v_string;
1556 typval_T *tv_dict = STACK_TV_BOT(-1);
1557 dict_T *dict = tv_dict->vval.v_dict;
1558 dictitem_T *di;
1559
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001560 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001561 if (dict == NULL)
1562 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001563 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001564 goto on_error;
1565 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001566 if (key == NULL)
1567 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001568 tv = STACK_TV_BOT(-3);
1569 di = dict_find(dict, key, -1);
1570 if (di != NULL)
1571 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001572 if (error_if_locked(di->di_tv.v_lock,
1573 e_cannot_change_dict_item))
1574 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001575 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001576 clear_tv(&di->di_tv);
1577 di->di_tv = *tv;
1578 }
1579 else
1580 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001581 if (error_if_locked(dict->dv_lock,
1582 e_cannot_change_dict))
1583 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001584 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001585 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1586 goto failed;
1587 clear_tv(tv);
1588 }
1589 clear_tv(tv_key);
1590 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001591 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001592 }
1593 break;
1594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 // push constant
1596 case ISN_PUSHNR:
1597 case ISN_PUSHBOOL:
1598 case ISN_PUSHSPEC:
1599 case ISN_PUSHF:
1600 case ISN_PUSHS:
1601 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001602 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001603 case ISN_PUSHCHANNEL:
1604 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001605 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 goto failed;
1607 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001608 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 ++ectx.ec_stack.ga_len;
1610 switch (iptr->isn_type)
1611 {
1612 case ISN_PUSHNR:
1613 tv->v_type = VAR_NUMBER;
1614 tv->vval.v_number = iptr->isn_arg.number;
1615 break;
1616 case ISN_PUSHBOOL:
1617 tv->v_type = VAR_BOOL;
1618 tv->vval.v_number = iptr->isn_arg.number;
1619 break;
1620 case ISN_PUSHSPEC:
1621 tv->v_type = VAR_SPECIAL;
1622 tv->vval.v_number = iptr->isn_arg.number;
1623 break;
1624#ifdef FEAT_FLOAT
1625 case ISN_PUSHF:
1626 tv->v_type = VAR_FLOAT;
1627 tv->vval.v_float = iptr->isn_arg.fnumber;
1628 break;
1629#endif
1630 case ISN_PUSHBLOB:
1631 blob_copy(iptr->isn_arg.blob, tv);
1632 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001633 case ISN_PUSHFUNC:
1634 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001635 if (iptr->isn_arg.string == NULL)
1636 tv->vval.v_string = NULL;
1637 else
1638 tv->vval.v_string =
1639 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001640 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001641 case ISN_PUSHCHANNEL:
1642#ifdef FEAT_JOB_CHANNEL
1643 tv->v_type = VAR_CHANNEL;
1644 tv->vval.v_channel = iptr->isn_arg.channel;
1645 if (tv->vval.v_channel != NULL)
1646 ++tv->vval.v_channel->ch_refcount;
1647#endif
1648 break;
1649 case ISN_PUSHJOB:
1650#ifdef FEAT_JOB_CHANNEL
1651 tv->v_type = VAR_JOB;
1652 tv->vval.v_job = iptr->isn_arg.job;
1653 if (tv->vval.v_job != NULL)
1654 ++tv->vval.v_job->jv_refcount;
1655#endif
1656 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 default:
1658 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001659 tv->vval.v_string = vim_strsave(
1660 iptr->isn_arg.string == NULL
1661 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662 }
1663 break;
1664
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001665 case ISN_UNLET:
1666 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1667 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001668 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001669 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001670 case ISN_UNLETENV:
1671 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1672 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001673
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001674 case ISN_LOCKCONST:
1675 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1676 break;
1677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678 // create a list from items on the stack; uses a single allocation
1679 // for the list header and the items
1680 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001681 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1682 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683 break;
1684
1685 // create a dict from items on the stack
1686 case ISN_NEWDICT:
1687 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001688 int count = iptr->isn_arg.number;
1689 dict_T *dict = dict_alloc();
1690 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691
1692 if (dict == NULL)
1693 goto failed;
1694 for (idx = 0; idx < count; ++idx)
1695 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001696 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001698 // check key is unique
1699 item = dict_find(dict, tv->vval.v_string, -1);
1700 if (item != NULL)
1701 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001702 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001703 semsg(_(e_duplicate_key), tv->vval.v_string);
1704 dict_unref(dict);
1705 goto on_error;
1706 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 item = dictitem_alloc(tv->vval.v_string);
1708 clear_tv(tv);
1709 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001710 {
1711 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1715 item->di_tv.v_lock = 0;
1716 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001717 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001718 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001719 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001721 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722 }
1723
1724 if (count > 0)
1725 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001726 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 goto failed;
1728 else
1729 ++ectx.ec_stack.ga_len;
1730 tv = STACK_TV_BOT(-1);
1731 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001732 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 tv->vval.v_dict = dict;
1734 ++dict->dv_refcount;
1735 }
1736 break;
1737
1738 // call a :def function
1739 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001740 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1742 iptr->isn_arg.dfunc.cdf_argcount,
1743 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001744 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 break;
1746
1747 // call a builtin function
1748 case ISN_BCALL:
1749 SOURCING_LNUM = iptr->isn_lnum;
1750 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1751 iptr->isn_arg.bfunc.cbf_argcount,
1752 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001753 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754 break;
1755
1756 // call a funcref or partial
1757 case ISN_PCALL:
1758 {
1759 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1760 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001761 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762
1763 SOURCING_LNUM = iptr->isn_lnum;
1764 if (pfunc->cpf_top)
1765 {
1766 // funcref is above the arguments
1767 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1768 }
1769 else
1770 {
1771 // Get the funcref from the stack.
1772 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001773 partial_tv = *STACK_TV_BOT(0);
1774 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 }
1776 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001777 if (tv == &partial_tv)
1778 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001780 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 }
1782 break;
1783
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001784 case ISN_PCALL_END:
1785 // PCALL finished, arguments have been consumed and replaced by
1786 // the return value. Now clear the funcref from the stack,
1787 // and move the return value in its place.
1788 --ectx.ec_stack.ga_len;
1789 clear_tv(STACK_TV_BOT(-1));
1790 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1791 break;
1792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 // call a user defined function or funcref/partial
1794 case ISN_UCALL:
1795 {
1796 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1797
1798 SOURCING_LNUM = iptr->isn_lnum;
1799 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001800 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001801 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 }
1803 break;
1804
1805 // return from a :def function call
1806 case ISN_RETURN:
1807 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001808 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001809 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001810
1811 if (trystack->ga_len > 0)
1812 trycmd = ((trycmd_T *)trystack->ga_data)
1813 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001814 if (trycmd != NULL
1815 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816 && trycmd->tcd_finally_idx != 0)
1817 {
1818 // jump to ":finally"
1819 ectx.ec_iidx = trycmd->tcd_finally_idx;
1820 trycmd->tcd_return = TRUE;
1821 }
1822 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001823 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 }
1825 break;
1826
1827 // push a function reference to a compiled function
1828 case ISN_FUNCREF:
1829 {
1830 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001831 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832
1833 pt = ALLOC_CLEAR_ONE(partial_T);
1834 if (pt == NULL)
1835 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001836 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001837 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001838 vim_free(pt);
1839 goto failed;
1840 }
1841 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1842 + iptr->isn_arg.funcref.fr_func;
1843 pt->pt_func = pt_dfunc->df_ufunc;
1844 pt->pt_refcount = 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001845
1846 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1847 {
1848 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1849 + ectx.ec_dfunc_idx;
1850
1851 // The closure needs to find arguments and local
1852 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001853 pt->pt_ectx_stack = &ectx.ec_stack;
1854 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001855
1856 // If this function returns and the closure is still
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001857 // being used, we need to make a copy of the context
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001858 // (arguments and local variables). Store a reference
1859 // to the partial so we can handle that.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001860 if (ga_grow(&ectx.ec_funcrefs, 1) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001861 {
Bram Moolenaardec07512020-09-18 23:11:10 +02001862 vim_free(pt);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001863 goto failed;
1864 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001865 // Extra variable keeps the count of closures created
1866 // in the current function call.
1867 tv = STACK_TV_VAR(dfunc->df_varcount);
1868 ++tv->vval.v_number;
1869
1870 ((partial_T **)ectx.ec_funcrefs.ga_data)
1871 [ectx.ec_funcrefs.ga_len] = pt;
1872 ++pt->pt_refcount;
1873 ++ectx.ec_funcrefs.ga_len;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001874 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001875 ++pt_dfunc->df_ufunc->uf_refcount;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 tv = STACK_TV_BOT(0);
1878 ++ectx.ec_stack.ga_len;
1879 tv->vval.v_partial = pt;
1880 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001881 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882 }
1883 break;
1884
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001885 // Create a global function from a lambda.
1886 case ISN_NEWFUNC:
1887 {
1888 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1889
1890 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1891 }
1892 break;
1893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 // jump if a condition is met
1895 case ISN_JUMP:
1896 {
1897 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1898 int jump = TRUE;
1899
1900 if (when != JUMP_ALWAYS)
1901 {
1902 tv = STACK_TV_BOT(-1);
1903 jump = tv2bool(tv);
1904 if (when == JUMP_IF_FALSE
1905 || when == JUMP_AND_KEEP_IF_FALSE)
1906 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001907 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908 {
1909 // drop the value from the stack
1910 clear_tv(tv);
1911 --ectx.ec_stack.ga_len;
1912 }
1913 }
1914 if (jump)
1915 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1916 }
1917 break;
1918
1919 // top of a for loop
1920 case ISN_FOR:
1921 {
1922 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1923 typval_T *idxtv =
1924 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1925
1926 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001927 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001929 ++idxtv->vval.v_number;
1930 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 // past the end of the list, jump to "endfor"
1932 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1933 else if (list->lv_first == &range_list_item)
1934 {
1935 // non-materialized range() list
1936 tv = STACK_TV_BOT(0);
1937 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001938 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 tv->vval.v_number = list_find_nr(
1940 list, idxtv->vval.v_number, NULL);
1941 ++ectx.ec_stack.ga_len;
1942 }
1943 else
1944 {
1945 listitem_T *li = list_find(list, idxtv->vval.v_number);
1946
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1948 ++ectx.ec_stack.ga_len;
1949 }
1950 }
1951 break;
1952
1953 // start of ":try" block
1954 case ISN_TRY:
1955 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001956 trycmd_T *trycmd = NULL;
1957
Bram Moolenaar270d0382020-05-15 21:42:53 +02001958 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 goto failed;
1960 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1961 + ectx.ec_trystack.ga_len;
1962 ++ectx.ec_trystack.ga_len;
1963 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001964 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1966 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001967 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02001968 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 }
1970 break;
1971
1972 case ISN_PUSHEXC:
1973 if (current_exception == NULL)
1974 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001975 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 iemsg("Evaluating catch while current_exception is NULL");
1977 goto failed;
1978 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001979 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 goto failed;
1981 tv = STACK_TV_BOT(0);
1982 ++ectx.ec_stack.ga_len;
1983 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001984 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 tv->vval.v_string = vim_strsave(
1986 (char_u *)current_exception->value);
1987 break;
1988
1989 case ISN_CATCH:
1990 {
1991 garray_T *trystack = &ectx.ec_trystack;
1992
1993 if (trystack->ga_len > 0)
1994 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001995 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001996 + trystack->ga_len - 1;
1997 trycmd->tcd_caught = TRUE;
1998 }
1999 did_emsg = got_int = did_throw = FALSE;
2000 catch_exception(current_exception);
2001 }
2002 break;
2003
2004 // end of ":try" block
2005 case ISN_ENDTRY:
2006 {
2007 garray_T *trystack = &ectx.ec_trystack;
2008
2009 if (trystack->ga_len > 0)
2010 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002011 trycmd_T *trycmd = NULL;
2012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 --trystack->ga_len;
2014 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002015 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 trycmd = ((trycmd_T *)trystack->ga_data)
2017 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002018 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 {
2020 // discard the exception
2021 if (caught_stack == current_exception)
2022 caught_stack = caught_stack->caught;
2023 discard_current_exception();
2024 }
2025
2026 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002027 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028 }
2029 }
2030 break;
2031
2032 case ISN_THROW:
2033 --ectx.ec_stack.ga_len;
2034 tv = STACK_TV_BOT(0);
2035 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2036 {
2037 vim_free(tv->vval.v_string);
2038 goto failed;
2039 }
2040 did_throw = TRUE;
2041 break;
2042
2043 // compare with special values
2044 case ISN_COMPAREBOOL:
2045 case ISN_COMPARESPECIAL:
2046 {
2047 typval_T *tv1 = STACK_TV_BOT(-2);
2048 typval_T *tv2 = STACK_TV_BOT(-1);
2049 varnumber_T arg1 = tv1->vval.v_number;
2050 varnumber_T arg2 = tv2->vval.v_number;
2051 int res;
2052
2053 switch (iptr->isn_arg.op.op_type)
2054 {
2055 case EXPR_EQUAL: res = arg1 == arg2; break;
2056 case EXPR_NEQUAL: res = arg1 != arg2; break;
2057 default: res = 0; break;
2058 }
2059
2060 --ectx.ec_stack.ga_len;
2061 tv1->v_type = VAR_BOOL;
2062 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2063 }
2064 break;
2065
2066 // Operation with two number arguments
2067 case ISN_OPNR:
2068 case ISN_COMPARENR:
2069 {
2070 typval_T *tv1 = STACK_TV_BOT(-2);
2071 typval_T *tv2 = STACK_TV_BOT(-1);
2072 varnumber_T arg1 = tv1->vval.v_number;
2073 varnumber_T arg2 = tv2->vval.v_number;
2074 varnumber_T res;
2075
2076 switch (iptr->isn_arg.op.op_type)
2077 {
2078 case EXPR_MULT: res = arg1 * arg2; break;
2079 case EXPR_DIV: res = arg1 / arg2; break;
2080 case EXPR_REM: res = arg1 % arg2; break;
2081 case EXPR_SUB: res = arg1 - arg2; break;
2082 case EXPR_ADD: res = arg1 + arg2; break;
2083
2084 case EXPR_EQUAL: res = arg1 == arg2; break;
2085 case EXPR_NEQUAL: res = arg1 != arg2; break;
2086 case EXPR_GREATER: res = arg1 > arg2; break;
2087 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2088 case EXPR_SMALLER: res = arg1 < arg2; break;
2089 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2090 default: res = 0; break;
2091 }
2092
2093 --ectx.ec_stack.ga_len;
2094 if (iptr->isn_type == ISN_COMPARENR)
2095 {
2096 tv1->v_type = VAR_BOOL;
2097 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2098 }
2099 else
2100 tv1->vval.v_number = res;
2101 }
2102 break;
2103
2104 // Computation with two float arguments
2105 case ISN_OPFLOAT:
2106 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002107#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108 {
2109 typval_T *tv1 = STACK_TV_BOT(-2);
2110 typval_T *tv2 = STACK_TV_BOT(-1);
2111 float_T arg1 = tv1->vval.v_float;
2112 float_T arg2 = tv2->vval.v_float;
2113 float_T res = 0;
2114 int cmp = FALSE;
2115
2116 switch (iptr->isn_arg.op.op_type)
2117 {
2118 case EXPR_MULT: res = arg1 * arg2; break;
2119 case EXPR_DIV: res = arg1 / arg2; break;
2120 case EXPR_SUB: res = arg1 - arg2; break;
2121 case EXPR_ADD: res = arg1 + arg2; break;
2122
2123 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2124 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2125 case EXPR_GREATER: cmp = arg1 > arg2; break;
2126 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2127 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2128 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2129 default: cmp = 0; break;
2130 }
2131 --ectx.ec_stack.ga_len;
2132 if (iptr->isn_type == ISN_COMPAREFLOAT)
2133 {
2134 tv1->v_type = VAR_BOOL;
2135 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2136 }
2137 else
2138 tv1->vval.v_float = res;
2139 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002140#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 break;
2142
2143 case ISN_COMPARELIST:
2144 {
2145 typval_T *tv1 = STACK_TV_BOT(-2);
2146 typval_T *tv2 = STACK_TV_BOT(-1);
2147 list_T *arg1 = tv1->vval.v_list;
2148 list_T *arg2 = tv2->vval.v_list;
2149 int cmp = FALSE;
2150 int ic = iptr->isn_arg.op.op_ic;
2151
2152 switch (iptr->isn_arg.op.op_type)
2153 {
2154 case EXPR_EQUAL: cmp =
2155 list_equal(arg1, arg2, ic, FALSE); break;
2156 case EXPR_NEQUAL: cmp =
2157 !list_equal(arg1, arg2, ic, FALSE); break;
2158 case EXPR_IS: cmp = arg1 == arg2; break;
2159 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2160 default: cmp = 0; break;
2161 }
2162 --ectx.ec_stack.ga_len;
2163 clear_tv(tv1);
2164 clear_tv(tv2);
2165 tv1->v_type = VAR_BOOL;
2166 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2167 }
2168 break;
2169
2170 case ISN_COMPAREBLOB:
2171 {
2172 typval_T *tv1 = STACK_TV_BOT(-2);
2173 typval_T *tv2 = STACK_TV_BOT(-1);
2174 blob_T *arg1 = tv1->vval.v_blob;
2175 blob_T *arg2 = tv2->vval.v_blob;
2176 int cmp = FALSE;
2177
2178 switch (iptr->isn_arg.op.op_type)
2179 {
2180 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2181 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2182 case EXPR_IS: cmp = arg1 == arg2; break;
2183 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2184 default: cmp = 0; break;
2185 }
2186 --ectx.ec_stack.ga_len;
2187 clear_tv(tv1);
2188 clear_tv(tv2);
2189 tv1->v_type = VAR_BOOL;
2190 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2191 }
2192 break;
2193
2194 // TODO: handle separately
2195 case ISN_COMPARESTRING:
2196 case ISN_COMPAREDICT:
2197 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198 case ISN_COMPAREANY:
2199 {
2200 typval_T *tv1 = STACK_TV_BOT(-2);
2201 typval_T *tv2 = STACK_TV_BOT(-1);
2202 exptype_T exptype = iptr->isn_arg.op.op_type;
2203 int ic = iptr->isn_arg.op.op_ic;
2204
Bram Moolenaareb26f432020-09-14 16:50:05 +02002205 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 typval_compare(tv1, tv2, exptype, ic);
2207 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 --ectx.ec_stack.ga_len;
2209 }
2210 break;
2211
2212 case ISN_ADDLIST:
2213 case ISN_ADDBLOB:
2214 {
2215 typval_T *tv1 = STACK_TV_BOT(-2);
2216 typval_T *tv2 = STACK_TV_BOT(-1);
2217
2218 if (iptr->isn_type == ISN_ADDLIST)
2219 eval_addlist(tv1, tv2);
2220 else
2221 eval_addblob(tv1, tv2);
2222 clear_tv(tv2);
2223 --ectx.ec_stack.ga_len;
2224 }
2225 break;
2226
2227 // Computation with two arguments of unknown type
2228 case ISN_OPANY:
2229 {
2230 typval_T *tv1 = STACK_TV_BOT(-2);
2231 typval_T *tv2 = STACK_TV_BOT(-1);
2232 varnumber_T n1, n2;
2233#ifdef FEAT_FLOAT
2234 float_T f1 = 0, f2 = 0;
2235#endif
2236 int error = FALSE;
2237
2238 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2239 {
2240 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2241 {
2242 eval_addlist(tv1, tv2);
2243 clear_tv(tv2);
2244 --ectx.ec_stack.ga_len;
2245 break;
2246 }
2247 else if (tv1->v_type == VAR_BLOB
2248 && tv2->v_type == VAR_BLOB)
2249 {
2250 eval_addblob(tv1, tv2);
2251 clear_tv(tv2);
2252 --ectx.ec_stack.ga_len;
2253 break;
2254 }
2255 }
2256#ifdef FEAT_FLOAT
2257 if (tv1->v_type == VAR_FLOAT)
2258 {
2259 f1 = tv1->vval.v_float;
2260 n1 = 0;
2261 }
2262 else
2263#endif
2264 {
2265 n1 = tv_get_number_chk(tv1, &error);
2266 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002267 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268#ifdef FEAT_FLOAT
2269 if (tv2->v_type == VAR_FLOAT)
2270 f1 = n1;
2271#endif
2272 }
2273#ifdef FEAT_FLOAT
2274 if (tv2->v_type == VAR_FLOAT)
2275 {
2276 f2 = tv2->vval.v_float;
2277 n2 = 0;
2278 }
2279 else
2280#endif
2281 {
2282 n2 = tv_get_number_chk(tv2, &error);
2283 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002284 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285#ifdef FEAT_FLOAT
2286 if (tv1->v_type == VAR_FLOAT)
2287 f2 = n2;
2288#endif
2289 }
2290#ifdef FEAT_FLOAT
2291 // if there is a float on either side the result is a float
2292 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2293 {
2294 switch (iptr->isn_arg.op.op_type)
2295 {
2296 case EXPR_MULT: f1 = f1 * f2; break;
2297 case EXPR_DIV: f1 = f1 / f2; break;
2298 case EXPR_SUB: f1 = f1 - f2; break;
2299 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002300 default: SOURCING_LNUM = iptr->isn_lnum;
2301 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002302 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 }
2304 clear_tv(tv1);
2305 clear_tv(tv2);
2306 tv1->v_type = VAR_FLOAT;
2307 tv1->vval.v_float = f1;
2308 --ectx.ec_stack.ga_len;
2309 }
2310 else
2311#endif
2312 {
2313 switch (iptr->isn_arg.op.op_type)
2314 {
2315 case EXPR_MULT: n1 = n1 * n2; break;
2316 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2317 case EXPR_SUB: n1 = n1 - n2; break;
2318 case EXPR_ADD: n1 = n1 + n2; break;
2319 default: n1 = num_modulus(n1, n2); break;
2320 }
2321 clear_tv(tv1);
2322 clear_tv(tv2);
2323 tv1->v_type = VAR_NUMBER;
2324 tv1->vval.v_number = n1;
2325 --ectx.ec_stack.ga_len;
2326 }
2327 }
2328 break;
2329
2330 case ISN_CONCAT:
2331 {
2332 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2333 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2334 char_u *res;
2335
2336 res = concat_str(str1, str2);
2337 clear_tv(STACK_TV_BOT(-2));
2338 clear_tv(STACK_TV_BOT(-1));
2339 --ectx.ec_stack.ga_len;
2340 STACK_TV_BOT(-1)->vval.v_string = res;
2341 }
2342 break;
2343
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002344 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002345 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002346 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002347 int is_slice = iptr->isn_type == ISN_STRSLICE;
2348 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002349 char_u *res;
2350
2351 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002352 // string slice: string is at stack-3, first index at
2353 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002354 if (is_slice)
2355 {
2356 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002357 n1 = tv->vval.v_number;
2358 }
2359
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002360 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002361 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002362
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002363 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002364 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002365 if (is_slice)
2366 // Slice: Select the characters from the string
2367 res = string_slice(tv->vval.v_string, n1, n2);
2368 else
2369 // Index: The resulting variable is a string of a
2370 // single character. If the index is too big or
2371 // negative the result is empty.
2372 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002373 vim_free(tv->vval.v_string);
2374 tv->vval.v_string = res;
2375 }
2376 break;
2377
2378 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002379 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380 {
Bram Moolenaared591872020-08-15 22:14:53 +02002381 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002383 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384
2385 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002386 // list slice: list is at stack-3, indexes at stack-2 and
2387 // stack-1
2388 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 list = tv->vval.v_list;
2390
2391 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002392 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002394
2395 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 {
Bram Moolenaared591872020-08-15 22:14:53 +02002397 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002398 n1 = tv->vval.v_number;
2399 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400 }
Bram Moolenaared591872020-08-15 22:14:53 +02002401
2402 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002403 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002404 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002405 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2406 == FAIL)
2407 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 }
2409 break;
2410
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002411 case ISN_ANYINDEX:
2412 case ISN_ANYSLICE:
2413 {
2414 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2415 typval_T *var1, *var2;
2416 int res;
2417
2418 // index: composite is at stack-2, index at stack-1
2419 // slice: composite is at stack-3, indexes at stack-2 and
2420 // stack-1
2421 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002422 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002423 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2424 goto on_error;
2425 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2426 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2427 res = eval_index_inner(tv, is_slice,
2428 var1, var2, NULL, -1, TRUE);
2429 clear_tv(var1);
2430 if (is_slice)
2431 clear_tv(var2);
2432 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2433 if (res == FAIL)
2434 goto on_error;
2435 }
2436 break;
2437
Bram Moolenaar9af78762020-06-16 11:34:42 +02002438 case ISN_SLICE:
2439 {
2440 list_T *list;
2441 int count = iptr->isn_arg.number;
2442
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002443 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002444 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002445 list = tv->vval.v_list;
2446
2447 // no error for short list, expect it to be checked earlier
2448 if (list != NULL && list->lv_len >= count)
2449 {
2450 list_T *newlist = list_slice(list,
2451 count, list->lv_len - 1);
2452
2453 if (newlist != NULL)
2454 {
2455 list_unref(list);
2456 tv->vval.v_list = newlist;
2457 ++newlist->lv_refcount;
2458 }
2459 }
2460 }
2461 break;
2462
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002463 case ISN_GETITEM:
2464 {
2465 listitem_T *li;
2466 int index = iptr->isn_arg.number;
2467
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002468 // Get list item: list is at stack-1, push item.
2469 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002470 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002471 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002472
2473 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2474 goto failed;
2475 ++ectx.ec_stack.ga_len;
2476 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2477 }
2478 break;
2479
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 case ISN_MEMBER:
2481 {
2482 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002483 char_u *key;
2484 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002485 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002486
2487 // dict member: dict is at stack-2, key at stack-1
2488 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002489 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002490 dict = tv->vval.v_dict;
2491
2492 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002493 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002494 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002495
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002496 if ((di = dict_find(dict, key, -1)) == NULL)
2497 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002498 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002499 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002500 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002501 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002502 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002503 --ectx.ec_stack.ga_len;
2504 // Clear the dict after getting the item, to avoid that it
2505 // make the item invalid.
2506 tv = STACK_TV_BOT(-1);
2507 temp_tv = *tv;
2508 copy_tv(&di->di_tv, tv);
2509 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002510 }
2511 break;
2512
2513 // dict member with string key
2514 case ISN_STRINGMEMBER:
2515 {
2516 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002518 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519
2520 tv = STACK_TV_BOT(-1);
2521 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2522 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002523 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002525 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 }
2527 dict = tv->vval.v_dict;
2528
2529 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2530 == NULL)
2531 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002532 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002534 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002536 // Clear the dict after getting the item, to avoid that it
2537 // make the item invalid.
2538 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002540 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 }
2542 break;
2543
2544 case ISN_NEGATENR:
2545 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002546 if (tv->v_type != VAR_NUMBER
2547#ifdef FEAT_FLOAT
2548 && tv->v_type != VAR_FLOAT
2549#endif
2550 )
2551 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002552 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002553 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002554 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002555 }
2556#ifdef FEAT_FLOAT
2557 if (tv->v_type == VAR_FLOAT)
2558 tv->vval.v_float = -tv->vval.v_float;
2559 else
2560#endif
2561 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 break;
2563
2564 case ISN_CHECKNR:
2565 {
2566 int error = FALSE;
2567
2568 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002569 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002571 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 (void)tv_get_number_chk(tv, &error);
2573 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002574 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 }
2576 break;
2577
2578 case ISN_CHECKTYPE:
2579 {
2580 checktype_T *ct = &iptr->isn_arg.type;
2581
2582 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002583 SOURCING_LNUM = iptr->isn_lnum;
2584 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2585 goto on_error;
2586
2587 // number 0 is FALSE, number 1 is TRUE
2588 if (tv->v_type == VAR_NUMBER
2589 && ct->ct_type->tt_type == VAR_BOOL
2590 && (tv->vval.v_number == 0
2591 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002593 tv->v_type = VAR_BOOL;
2594 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002595 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 }
2597 }
2598 break;
2599
Bram Moolenaar9af78762020-06-16 11:34:42 +02002600 case ISN_CHECKLEN:
2601 {
2602 int min_len = iptr->isn_arg.checklen.cl_min_len;
2603 list_T *list = NULL;
2604
2605 tv = STACK_TV_BOT(-1);
2606 if (tv->v_type == VAR_LIST)
2607 list = tv->vval.v_list;
2608 if (list == NULL || list->lv_len < min_len
2609 || (list->lv_len > min_len
2610 && !iptr->isn_arg.checklen.cl_more_OK))
2611 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002612 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002613 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002614 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002615 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002616 }
2617 }
2618 break;
2619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620 case ISN_2BOOL:
2621 {
2622 int n;
2623
2624 tv = STACK_TV_BOT(-1);
2625 n = tv2bool(tv);
2626 if (iptr->isn_arg.number) // invert
2627 n = !n;
2628 clear_tv(tv);
2629 tv->v_type = VAR_BOOL;
2630 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2631 }
2632 break;
2633
2634 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002635 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 {
2637 char_u *str;
2638
2639 tv = STACK_TV_BOT(iptr->isn_arg.number);
2640 if (tv->v_type != VAR_STRING)
2641 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002642 if (iptr->isn_type == ISN_2STRING_ANY)
2643 {
2644 switch (tv->v_type)
2645 {
2646 case VAR_SPECIAL:
2647 case VAR_BOOL:
2648 case VAR_NUMBER:
2649 case VAR_FLOAT:
2650 case VAR_BLOB: break;
2651 default: to_string_error(tv->v_type);
2652 goto on_error;
2653 }
2654 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 str = typval_tostring(tv);
2656 clear_tv(tv);
2657 tv->v_type = VAR_STRING;
2658 tv->vval.v_string = str;
2659 }
2660 }
2661 break;
2662
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002663 case ISN_PUT:
2664 {
2665 int regname = iptr->isn_arg.put.put_regname;
2666 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2667 char_u *expr = NULL;
2668 int dir = FORWARD;
2669
2670 if (regname == '=')
2671 {
2672 tv = STACK_TV_BOT(-1);
2673 if (tv->v_type == VAR_STRING)
2674 expr = tv->vval.v_string;
2675 else
2676 {
2677 expr = typval_tostring(tv); // allocates value
2678 clear_tv(tv);
2679 }
2680 --ectx.ec_stack.ga_len;
2681 }
2682 if (lnum == -2)
2683 // :put! above cursor
2684 dir = BACKWARD;
2685 else if (lnum >= 0)
2686 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2687 check_cursor();
2688 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2689 vim_free(expr);
2690 }
2691 break;
2692
Bram Moolenaar389df252020-07-09 21:20:47 +02002693 case ISN_SHUFFLE:
2694 {
2695 typval_T tmp_tv;
2696 int item = iptr->isn_arg.shuffle.shfl_item;
2697 int up = iptr->isn_arg.shuffle.shfl_up;
2698
2699 tmp_tv = *STACK_TV_BOT(-item);
2700 for ( ; up > 0 && item > 1; --up)
2701 {
2702 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2703 --item;
2704 }
2705 *STACK_TV_BOT(-item) = tmp_tv;
2706 }
2707 break;
2708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 case ISN_DROP:
2710 --ectx.ec_stack.ga_len;
2711 clear_tv(STACK_TV_BOT(0));
2712 break;
2713 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002714 continue;
2715
Bram Moolenaard032f342020-07-18 18:13:02 +02002716func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002717 // Restore previous function. If the frame pointer is where we started
2718 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02002719 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02002720 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002721
Bram Moolenaard032f342020-07-18 18:13:02 +02002722 if (func_return(&ectx) == FAIL)
2723 // only fails when out of memory
2724 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002725 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002726
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002727on_error:
2728 if (trylevel == 0)
2729 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 }
2731
2732done:
2733 // function finished, get result from the stack.
2734 tv = STACK_TV_BOT(-1);
2735 *rettv = *tv;
2736 tv->v_type = VAR_UNKNOWN;
2737 ret = OK;
2738
2739failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002740 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002741 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002742 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002743
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002744 // Deal with any remaining closures, they may be in use somewhere.
2745 if (ectx.ec_funcrefs.ga_len > 0)
2746 handle_closure_in_use(&ectx, FALSE);
2747
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002748 estack_pop();
2749 current_sctx = save_current_sctx;
2750
2751failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002752 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2754 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002757 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002758
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002759 // Not sure if this is necessary.
2760 suppress_errthrow = save_suppress_errthrow;
2761
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002762 if (ret != OK && called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002763 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002764 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 return ret;
2766}
2767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768/*
2769 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002770 * We don't really need this at runtime, but we do have tests that require it,
2771 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 */
2773 void
2774ex_disassemble(exarg_T *eap)
2775{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002776 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002777 char_u *fname;
2778 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779 dfunc_T *dfunc;
2780 isn_T *instr;
2781 int current;
2782 int line_idx = 0;
2783 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002784 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002786 if (STRNCMP(arg, "<lambda>", 8) == 0)
2787 {
2788 arg += 8;
2789 (void)getdigits(&arg);
2790 fname = vim_strnsave(eap->arg, arg - eap->arg);
2791 }
2792 else
2793 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002794 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002795 if (fname == NULL)
2796 {
2797 semsg(_(e_invarg2), eap->arg);
2798 return;
2799 }
2800
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002801 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002802 if (ufunc == NULL)
2803 {
2804 char_u *p = untrans_function_name(fname);
2805
2806 if (p != NULL)
2807 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002808 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002809 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002810 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 if (ufunc == NULL)
2812 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002813 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814 return;
2815 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002816 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002817 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2818 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002819 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002821 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 return;
2823 }
2824 if (ufunc->uf_name_exp != NULL)
2825 msg((char *)ufunc->uf_name_exp);
2826 else
2827 msg((char *)ufunc->uf_name);
2828
2829 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2830 instr = dfunc->df_instr;
2831 for (current = 0; current < dfunc->df_instr_count; ++current)
2832 {
2833 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002834 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835
2836 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2837 {
2838 if (current > prev_current)
2839 {
2840 msg_puts("\n\n");
2841 prev_current = current;
2842 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002843 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2844 if (line != NULL)
2845 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 }
2847
2848 switch (iptr->isn_type)
2849 {
2850 case ISN_EXEC:
2851 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2852 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002853 case ISN_EXECCONCAT:
2854 smsg("%4d EXECCONCAT %lld", current,
2855 (long long)iptr->isn_arg.number);
2856 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 case ISN_ECHO:
2858 {
2859 echo_T *echo = &iptr->isn_arg.echo;
2860
2861 smsg("%4d %s %d", current,
2862 echo->echo_with_white ? "ECHO" : "ECHON",
2863 echo->echo_count);
2864 }
2865 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002866 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002867 smsg("%4d EXECUTE %lld", current,
2868 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002869 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002870 case ISN_ECHOMSG:
2871 smsg("%4d ECHOMSG %lld", current,
2872 (long long)(iptr->isn_arg.number));
2873 break;
2874 case ISN_ECHOERR:
2875 smsg("%4d ECHOERR %lld", current,
2876 (long long)(iptr->isn_arg.number));
2877 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002879 case ISN_LOADOUTER:
2880 {
2881 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2882
2883 if (iptr->isn_arg.number < 0)
2884 smsg("%4d LOAD%s arg[%lld]", current, add,
2885 (long long)(iptr->isn_arg.number
2886 + STACK_FRAME_SIZE));
2887 else
2888 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002889 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002890 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 break;
2892 case ISN_LOADV:
2893 smsg("%4d LOADV v:%s", current,
2894 get_vim_var_name(iptr->isn_arg.number));
2895 break;
2896 case ISN_LOADSCRIPT:
2897 {
2898 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002899 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2901 + iptr->isn_arg.script.script_idx;
2902
2903 smsg("%4d LOADSCRIPT %s from %s", current,
2904 sv->sv_name, si->sn_name);
2905 }
2906 break;
2907 case ISN_LOADS:
2908 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002909 scriptitem_T *si = SCRIPT_ITEM(
2910 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911
2912 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002913 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 }
2915 break;
2916 case ISN_LOADG:
2917 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2918 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002919 case ISN_LOADB:
2920 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2921 break;
2922 case ISN_LOADW:
2923 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2924 break;
2925 case ISN_LOADT:
2926 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2927 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002928 case ISN_LOADGDICT:
2929 smsg("%4d LOAD g:", current);
2930 break;
2931 case ISN_LOADBDICT:
2932 smsg("%4d LOAD b:", current);
2933 break;
2934 case ISN_LOADWDICT:
2935 smsg("%4d LOAD w:", current);
2936 break;
2937 case ISN_LOADTDICT:
2938 smsg("%4d LOAD t:", current);
2939 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 case ISN_LOADOPT:
2941 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2942 break;
2943 case ISN_LOADENV:
2944 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2945 break;
2946 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002947 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 break;
2949
2950 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002951 case ISN_STOREOUTER:
2952 {
2953 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2954
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002955 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002956 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002957 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002958 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002959 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002960 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002963 case ISN_STOREV:
2964 smsg("%4d STOREV v:%s", current,
2965 get_vim_var_name(iptr->isn_arg.number));
2966 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002968 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2969 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002970 case ISN_STOREB:
2971 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2972 break;
2973 case ISN_STOREW:
2974 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2975 break;
2976 case ISN_STORET:
2977 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2978 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002979 case ISN_STORES:
2980 {
2981 scriptitem_T *si = SCRIPT_ITEM(
2982 iptr->isn_arg.loadstore.ls_sid);
2983
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002984 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002985 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002986 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 break;
2988 case ISN_STORESCRIPT:
2989 {
2990 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002991 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2993 + iptr->isn_arg.script.script_idx;
2994
2995 smsg("%4d STORESCRIPT %s in %s", current,
2996 sv->sv_name, si->sn_name);
2997 }
2998 break;
2999 case ISN_STOREOPT:
3000 smsg("%4d STOREOPT &%s", current,
3001 iptr->isn_arg.storeopt.so_name);
3002 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003003 case ISN_STOREENV:
3004 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3005 break;
3006 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003007 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003008 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 case ISN_STORENR:
3010 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003011 iptr->isn_arg.storenr.stnr_val,
3012 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 break;
3014
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003015 case ISN_STORELIST:
3016 smsg("%4d STORELIST", current);
3017 break;
3018
3019 case ISN_STOREDICT:
3020 smsg("%4d STOREDICT", current);
3021 break;
3022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 // constants
3024 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003025 smsg("%4d PUSHNR %lld", current,
3026 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 break;
3028 case ISN_PUSHBOOL:
3029 case ISN_PUSHSPEC:
3030 smsg("%4d PUSH %s", current,
3031 get_var_special_name(iptr->isn_arg.number));
3032 break;
3033 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003034#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003036#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 break;
3038 case ISN_PUSHS:
3039 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3040 break;
3041 case ISN_PUSHBLOB:
3042 {
3043 char_u *r;
3044 char_u numbuf[NUMBUFLEN];
3045 char_u *tofree;
3046
3047 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003048 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 vim_free(tofree);
3050 }
3051 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003052 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003053 {
3054 char *name = (char *)iptr->isn_arg.string;
3055
3056 smsg("%4d PUSHFUNC \"%s\"", current,
3057 name == NULL ? "[none]" : name);
3058 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003059 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003060 case ISN_PUSHCHANNEL:
3061#ifdef FEAT_JOB_CHANNEL
3062 {
3063 channel_T *channel = iptr->isn_arg.channel;
3064
3065 smsg("%4d PUSHCHANNEL %d", current,
3066 channel == NULL ? 0 : channel->ch_id);
3067 }
3068#endif
3069 break;
3070 case ISN_PUSHJOB:
3071#ifdef FEAT_JOB_CHANNEL
3072 {
3073 typval_T tv;
3074 char_u *name;
3075
3076 tv.v_type = VAR_JOB;
3077 tv.vval.v_job = iptr->isn_arg.job;
3078 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003079 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003080 }
3081#endif
3082 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 case ISN_PUSHEXC:
3084 smsg("%4d PUSH v:exception", current);
3085 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003086 case ISN_UNLET:
3087 smsg("%4d UNLET%s %s", current,
3088 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3089 iptr->isn_arg.unlet.ul_name);
3090 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003091 case ISN_UNLETENV:
3092 smsg("%4d UNLETENV%s $%s", current,
3093 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3094 iptr->isn_arg.unlet.ul_name);
3095 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003096 case ISN_LOCKCONST:
3097 smsg("%4d LOCKCONST", current);
3098 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003100 smsg("%4d NEWLIST size %lld", current,
3101 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 break;
3103 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003104 smsg("%4d NEWDICT size %lld", current,
3105 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 break;
3107
3108 // function call
3109 case ISN_BCALL:
3110 {
3111 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3112
3113 smsg("%4d BCALL %s(argc %d)", current,
3114 internal_func_name(cbfunc->cbf_idx),
3115 cbfunc->cbf_argcount);
3116 }
3117 break;
3118 case ISN_DCALL:
3119 {
3120 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3121 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3122 + cdfunc->cdf_idx;
3123
3124 smsg("%4d DCALL %s(argc %d)", current,
3125 df->df_ufunc->uf_name_exp != NULL
3126 ? df->df_ufunc->uf_name_exp
3127 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3128 }
3129 break;
3130 case ISN_UCALL:
3131 {
3132 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3133
3134 smsg("%4d UCALL %s(argc %d)", current,
3135 cufunc->cuf_name, cufunc->cuf_argcount);
3136 }
3137 break;
3138 case ISN_PCALL:
3139 {
3140 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3141
3142 smsg("%4d PCALL%s (argc %d)", current,
3143 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3144 }
3145 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003146 case ISN_PCALL_END:
3147 smsg("%4d PCALL end", current);
3148 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 case ISN_RETURN:
3150 smsg("%4d RETURN", current);
3151 break;
3152 case ISN_FUNCREF:
3153 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003154 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003156 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003158 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 }
3160 break;
3161
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003162 case ISN_NEWFUNC:
3163 {
3164 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3165
3166 smsg("%4d NEWFUNC %s %s", current,
3167 newfunc->nf_lambda, newfunc->nf_global);
3168 }
3169 break;
3170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 case ISN_JUMP:
3172 {
3173 char *when = "?";
3174
3175 switch (iptr->isn_arg.jump.jump_when)
3176 {
3177 case JUMP_ALWAYS:
3178 when = "JUMP";
3179 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 case JUMP_AND_KEEP_IF_TRUE:
3181 when = "JUMP_AND_KEEP_IF_TRUE";
3182 break;
3183 case JUMP_IF_FALSE:
3184 when = "JUMP_IF_FALSE";
3185 break;
3186 case JUMP_AND_KEEP_IF_FALSE:
3187 when = "JUMP_AND_KEEP_IF_FALSE";
3188 break;
3189 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003190 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 iptr->isn_arg.jump.jump_where);
3192 }
3193 break;
3194
3195 case ISN_FOR:
3196 {
3197 forloop_T *forloop = &iptr->isn_arg.forloop;
3198
3199 smsg("%4d FOR $%d -> %d", current,
3200 forloop->for_idx, forloop->for_end);
3201 }
3202 break;
3203
3204 case ISN_TRY:
3205 {
3206 try_T *try = &iptr->isn_arg.try;
3207
3208 smsg("%4d TRY catch -> %d, finally -> %d", current,
3209 try->try_catch, try->try_finally);
3210 }
3211 break;
3212 case ISN_CATCH:
3213 // TODO
3214 smsg("%4d CATCH", current);
3215 break;
3216 case ISN_ENDTRY:
3217 smsg("%4d ENDTRY", current);
3218 break;
3219 case ISN_THROW:
3220 smsg("%4d THROW", current);
3221 break;
3222
3223 // expression operations on number
3224 case ISN_OPNR:
3225 case ISN_OPFLOAT:
3226 case ISN_OPANY:
3227 {
3228 char *what;
3229 char *ins;
3230
3231 switch (iptr->isn_arg.op.op_type)
3232 {
3233 case EXPR_MULT: what = "*"; break;
3234 case EXPR_DIV: what = "/"; break;
3235 case EXPR_REM: what = "%"; break;
3236 case EXPR_SUB: what = "-"; break;
3237 case EXPR_ADD: what = "+"; break;
3238 default: what = "???"; break;
3239 }
3240 switch (iptr->isn_type)
3241 {
3242 case ISN_OPNR: ins = "OPNR"; break;
3243 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3244 case ISN_OPANY: ins = "OPANY"; break;
3245 default: ins = "???"; break;
3246 }
3247 smsg("%4d %s %s", current, ins, what);
3248 }
3249 break;
3250
3251 case ISN_COMPAREBOOL:
3252 case ISN_COMPARESPECIAL:
3253 case ISN_COMPARENR:
3254 case ISN_COMPAREFLOAT:
3255 case ISN_COMPARESTRING:
3256 case ISN_COMPAREBLOB:
3257 case ISN_COMPARELIST:
3258 case ISN_COMPAREDICT:
3259 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 case ISN_COMPAREANY:
3261 {
3262 char *p;
3263 char buf[10];
3264 char *type;
3265
3266 switch (iptr->isn_arg.op.op_type)
3267 {
3268 case EXPR_EQUAL: p = "=="; break;
3269 case EXPR_NEQUAL: p = "!="; break;
3270 case EXPR_GREATER: p = ">"; break;
3271 case EXPR_GEQUAL: p = ">="; break;
3272 case EXPR_SMALLER: p = "<"; break;
3273 case EXPR_SEQUAL: p = "<="; break;
3274 case EXPR_MATCH: p = "=~"; break;
3275 case EXPR_IS: p = "is"; break;
3276 case EXPR_ISNOT: p = "isnot"; break;
3277 case EXPR_NOMATCH: p = "!~"; break;
3278 default: p = "???"; break;
3279 }
3280 STRCPY(buf, p);
3281 if (iptr->isn_arg.op.op_ic == TRUE)
3282 strcat(buf, "?");
3283 switch(iptr->isn_type)
3284 {
3285 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3286 case ISN_COMPARESPECIAL:
3287 type = "COMPARESPECIAL"; break;
3288 case ISN_COMPARENR: type = "COMPARENR"; break;
3289 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3290 case ISN_COMPARESTRING:
3291 type = "COMPARESTRING"; break;
3292 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3293 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3294 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3295 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3297 default: type = "???"; break;
3298 }
3299
3300 smsg("%4d %s %s", current, type, buf);
3301 }
3302 break;
3303
3304 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3305 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3306
3307 // expression operations
3308 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003309 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003310 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003311 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003312 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003313 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3314 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003315 case ISN_SLICE: smsg("%4d SLICE %lld",
3316 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003317 case ISN_GETITEM: smsg("%4d ITEM %lld",
3318 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003319 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3320 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 iptr->isn_arg.string); break;
3322 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3323
3324 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003325 case ISN_CHECKTYPE:
3326 {
3327 char *tofree;
3328
3329 smsg("%4d CHECKTYPE %s stack[%d]", current,
3330 type_name(iptr->isn_arg.type.ct_type, &tofree),
3331 iptr->isn_arg.type.ct_off);
3332 vim_free(tofree);
3333 break;
3334 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003335 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3336 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3337 iptr->isn_arg.checklen.cl_min_len);
3338 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 case ISN_2BOOL: if (iptr->isn_arg.number)
3340 smsg("%4d INVERT (!val)", current);
3341 else
3342 smsg("%4d 2BOOL (!!val)", current);
3343 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003344 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3345 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003346 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003347 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3348 (long long)(iptr->isn_arg.number));
3349 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003350 case ISN_PUT:
3351 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname,
3352 (long)iptr->isn_arg.put.put_lnum);
3353 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354
Bram Moolenaar389df252020-07-09 21:20:47 +02003355 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3356 iptr->isn_arg.shuffle.shfl_item,
3357 iptr->isn_arg.shuffle.shfl_up);
3358 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 case ISN_DROP: smsg("%4d DROP", current); break;
3360 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003361
3362 out_flush(); // output one line at a time
3363 ui_breakcheck();
3364 if (got_int)
3365 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367}
3368
3369/*
3370 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3371 * list, etc. Mostly like what JavaScript does, except that empty list and
3372 * empty dictionary are FALSE.
3373 */
3374 int
3375tv2bool(typval_T *tv)
3376{
3377 switch (tv->v_type)
3378 {
3379 case VAR_NUMBER:
3380 return tv->vval.v_number != 0;
3381 case VAR_FLOAT:
3382#ifdef FEAT_FLOAT
3383 return tv->vval.v_float != 0.0;
3384#else
3385 break;
3386#endif
3387 case VAR_PARTIAL:
3388 return tv->vval.v_partial != NULL;
3389 case VAR_FUNC:
3390 case VAR_STRING:
3391 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3392 case VAR_LIST:
3393 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3394 case VAR_DICT:
3395 return tv->vval.v_dict != NULL
3396 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3397 case VAR_BOOL:
3398 case VAR_SPECIAL:
3399 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3400 case VAR_JOB:
3401#ifdef FEAT_JOB_CHANNEL
3402 return tv->vval.v_job != NULL;
3403#else
3404 break;
3405#endif
3406 case VAR_CHANNEL:
3407#ifdef FEAT_JOB_CHANNEL
3408 return tv->vval.v_channel != NULL;
3409#else
3410 break;
3411#endif
3412 case VAR_BLOB:
3413 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3414 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003415 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 case VAR_VOID:
3417 break;
3418 }
3419 return FALSE;
3420}
3421
3422/*
3423 * If "tv" is a string give an error and return FAIL.
3424 */
3425 int
3426check_not_string(typval_T *tv)
3427{
3428 if (tv->v_type == VAR_STRING)
3429 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003430 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 clear_tv(tv);
3432 return FAIL;
3433 }
3434 return OK;
3435}
3436
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437
3438#endif // FEAT_EVAL