blob: 7e5a1dea3792e9e7b6abd8a2dd48010763ffe677 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020027 int tcd_frame_idx; // ec_frame_idx when ISN_TRY was encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028 int tcd_catch_idx; // instruction of the first catch
29 int tcd_finally_idx; // instruction of the finally block
30 int tcd_caught; // catch block entered
31 int tcd_return; // when TRUE return from end of :finally
32} trycmd_T;
33
34
35// A stack is used to store:
36// - arguments passed to a :def function
37// - info about the calling function, to use when returning
38// - local variables
39// - temporary values
40//
41// In detail (FP == Frame Pointer):
42// arg1 first argument from caller (if present)
43// arg2 second argument from caller (if present)
44// extra_arg1 any missing optional argument default value
45// FP -> cur_func calling function
46// current previous instruction pointer
47// frame_ptr previous Frame Pointer
48// var1 space for local variable
49// var2 space for local variable
50// .... fixed space for max. number of local variables
51// temp temporary values
52// .... flexible space for temporary values (can grow big)
53
54/*
55 * Execution context.
56 */
57typedef struct {
58 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020059 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020061 garray_T *ec_outer_stack; // stack used for closures
62 int ec_outer_frame; // stack frame in ec_outer_stack
63
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064 garray_T ec_trystack; // stack of trycmd_T values
65 int ec_in_catch; // when TRUE in catch or finally block
66
67 int ec_dfunc_idx; // current function index
68 isn_T *ec_instr; // array with instructions
69 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020070
71 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072} ectx_T;
73
74// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020075#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010076
Bram Moolenaar418f1df2020-08-12 21:34:49 +020077 void
78to_string_error(vartype_T vartype)
79{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020080 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020081}
82
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010084 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 */
86 static int
87ufunc_argcount(ufunc_T *ufunc)
88{
89 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
90}
91
92/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010093 * Set the instruction index, depending on omitted arguments, where the default
94 * values are to be computed. If all optional arguments are present, start
95 * with the function body.
96 * The expression evaluation is at the start of the instructions:
97 * 0 -> EVAL default1
98 * STORE arg[-2]
99 * 1 -> EVAL default2
100 * STORE arg[-1]
101 * 2 -> function body
102 */
103 static void
104init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
105{
106 if (ufunc->uf_def_args.ga_len == 0)
107 ectx->ec_iidx = 0;
108 else
109 {
110 int defcount = ufunc->uf_args.ga_len - argcount;
111
112 // If there is a varargs argument defcount can be negative, no defaults
113 // to evaluate then.
114 if (defcount < 0)
115 defcount = 0;
116 ectx->ec_iidx = ufunc->uf_def_arg_idx[
117 ufunc->uf_def_args.ga_len - defcount];
118 }
119}
120
121/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200122 * Create a new list from "count" items at the bottom of the stack.
123 * When "count" is zero an empty list is added to the stack.
124 */
125 static int
126exe_newlist(int count, ectx_T *ectx)
127{
128 list_T *list = list_alloc_with_items(count);
129 int idx;
130 typval_T *tv;
131
132 if (list == NULL)
133 return FAIL;
134 for (idx = 0; idx < count; ++idx)
135 list_set_item(list, idx, STACK_TV_BOT(idx - count));
136
137 if (count > 0)
138 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200139 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200140 return FAIL;
141 else
142 ++ectx->ec_stack.ga_len;
143 tv = STACK_TV_BOT(-1);
144 tv->v_type = VAR_LIST;
145 tv->vval.v_list = list;
146 ++list->lv_refcount;
147 return OK;
148}
149
150/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 * Call compiled function "cdf_idx" from compiled code.
152 *
153 * Stack has:
154 * - current arguments (already there)
155 * - omitted optional argument (default values) added here
156 * - stack frame:
157 * - pointer to calling function
158 * - Index of next instruction in calling function
159 * - previous frame pointer
160 * - reserved space for local variables
161 */
162 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200163call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200165 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
167 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200168 int arg_to_add;
169 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200170 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200172 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173
174 if (dfunc->df_deleted)
175 {
176 emsg_funcname(e_func_deleted, ufunc->uf_name);
177 return FAIL;
178 }
179
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200180 if (ufunc->uf_va_name != NULL)
181 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200182 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200183 // Stack at time of call with 2 varargs:
184 // normal_arg
185 // optional_arg
186 // vararg_1
187 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200188 // After creating the list:
189 // normal_arg
190 // optional_arg
191 // vararg-list
192 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200193 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200194 // After creating the list
195 // normal_arg
196 // (space for optional_arg)
197 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200198 vararg_count = argcount - ufunc->uf_args.ga_len;
199 if (vararg_count < 0)
200 vararg_count = 0;
201 else
202 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200203 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200204 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200205
206 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200207 }
208
Bram Moolenaarfe270812020-04-11 22:31:27 +0200209 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200210 if (arg_to_add < 0)
211 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200212 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200213 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200214 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200215 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200216 return FAIL;
217 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200218
219 // Reserve space for:
220 // - missing arguments
221 // - stack frame
222 // - local variables
223 // - if needed: a counter for number of closures created in
224 // ectx->ec_funcrefs.
225 varcount = dfunc->df_varcount + dfunc->df_has_closure;
226 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
227 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 return FAIL;
229
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100230 // If depth of calling is getting too high, don't execute the function.
231 if (funcdepth_increment() == FAIL)
232 return FAIL;
233
Bram Moolenaarfe270812020-04-11 22:31:27 +0200234 // Move the vararg-list to below the missing optional arguments.
235 if (vararg_count > 0 && arg_to_add > 0)
236 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100237
238 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200239 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200240 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200241 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242
243 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
245 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200246 STACK_TV_BOT(2)->vval.v_string = (void *)ectx->ec_outer_stack;
247 STACK_TV_BOT(3)->vval.v_number = ectx->ec_outer_frame;
248 STACK_TV_BOT(4)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200249 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100250
251 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200252 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200254 if (dfunc->df_has_closure)
255 {
256 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
257
258 tv->v_type = VAR_NUMBER;
259 tv->vval.v_number = 0;
260 }
261 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262
263 // Set execution state to the start of the called function.
264 ectx->ec_dfunc_idx = cdf_idx;
265 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200266 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
267 if (entry != NULL)
268 {
269 // Set the script context to the script where the function was defined.
270 // TODO: save more than the SID?
271 entry->es_save_sid = current_sctx.sc_sid;
272 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
273 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100274
275 // Decide where to start execution, handles optional arguments.
276 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277
278 return OK;
279}
280
281// Get pointer to item in the stack.
282#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
283
284/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200285 * Used when returning from a function: Check if any closure is still
286 * referenced. If so then move the arguments and variables to a separate piece
287 * of stack to be used when the closure is called.
288 * When "free_arguments" is TRUE the arguments are to be freed.
289 * Returns FAIL when out of memory.
290 */
291 static int
292handle_closure_in_use(ectx_T *ectx, int free_arguments)
293{
294 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
295 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200296 int argcount;
297 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200298 int idx;
299 typval_T *tv;
300 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200301 garray_T *gap = &ectx->ec_funcrefs;
302 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200303
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200304 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200305 return OK; // function was freed
306 if (dfunc->df_has_closure == 0)
307 return OK; // no closures
308 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
309 closure_count = tv->vval.v_number;
310 if (closure_count == 0)
311 return OK; // no funcrefs created
312
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200313 argcount = ufunc_argcount(dfunc->df_ufunc);
314 top = ectx->ec_frame_idx - argcount;
315
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200316 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200317 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200318 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200319 partial_T *pt;
320 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200321
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200322 if (off < 0)
323 continue; // count is off or already done
324 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200325 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200326 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200327 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200328 int i;
329
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200330 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200331 // unreferenced on return.
332 for (i = 0; i < dfunc->df_varcount; ++i)
333 {
334 typval_T *stv = STACK_TV(ectx->ec_frame_idx
335 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200336 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200337 --refcount;
338 }
339 if (refcount > 1)
340 {
341 closure_in_use = TRUE;
342 break;
343 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200344 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200345 }
346
347 if (closure_in_use)
348 {
349 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
350 typval_T *stack;
351
352 // A closure is using the arguments and/or local variables.
353 // Move them to the called function.
354 if (funcstack == NULL)
355 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200356 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
357 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200358 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
359 funcstack->fs_ga.ga_data = stack;
360 if (stack == NULL)
361 {
362 vim_free(funcstack);
363 return FAIL;
364 }
365
366 // Move or copy the arguments.
367 for (idx = 0; idx < argcount; ++idx)
368 {
369 tv = STACK_TV(top + idx);
370 if (free_arguments)
371 {
372 *(stack + idx) = *tv;
373 tv->v_type = VAR_UNKNOWN;
374 }
375 else
376 copy_tv(tv, stack + idx);
377 }
378 // Move the local variables.
379 for (idx = 0; idx < dfunc->df_varcount; ++idx)
380 {
381 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200382
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200383 // A partial created for a local function, that is also used as a
384 // local variable, has a reference count for the variable, thus
385 // will never go down to zero. When all these refcounts are one
386 // then the funcstack is unused. We need to count how many we have
387 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200388 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
389 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200390 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200391
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200392 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200393 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
394 gap->ga_len - closure_count + i])
395 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200396 }
397
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200398 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200399 tv->v_type = VAR_UNKNOWN;
400 }
401
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200402 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200403 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200404 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
405 - closure_count + idx];
406 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200407 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200408 ++funcstack->fs_refcount;
409 pt->pt_funcstack = funcstack;
410 pt->pt_ectx_stack = &funcstack->fs_ga;
411 pt->pt_ectx_frame = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200412 }
413 }
414 }
415
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200416 for (idx = 0; idx < closure_count; ++idx)
417 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
418 - closure_count + idx]);
419 gap->ga_len -= closure_count;
420 if (gap->ga_len == 0)
421 ga_clear(gap);
422
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200423 return OK;
424}
425
426/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200427 * Called when a partial is freed or its reference count goes down to one. The
428 * funcstack may be the only reference to the partials in the local variables.
429 * Go over all of them, the funcref and can be freed if all partials
430 * referencing the funcstack have a reference count of one.
431 */
432 void
433funcstack_check_refcount(funcstack_T *funcstack)
434{
435 int i;
436 garray_T *gap = &funcstack->fs_ga;
437 int done = 0;
438
439 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
440 return;
441 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
442 {
443 typval_T *tv = ((typval_T *)gap->ga_data) + i;
444
445 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
446 && tv->vval.v_partial->pt_funcstack == funcstack
447 && tv->vval.v_partial->pt_refcount == 1)
448 ++done;
449 }
450 if (done == funcstack->fs_min_refcount)
451 {
452 typval_T *stack = gap->ga_data;
453
454 // All partials referencing the funcstack have a reference count of
455 // one, thus the funcstack is no longer of use.
456 for (i = 0; i < gap->ga_len; ++i)
457 clear_tv(stack + i);
458 vim_free(stack);
459 vim_free(funcstack);
460 }
461}
462
463/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 * Return from the current function.
465 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200466 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100467func_return(ectx_T *ectx)
468{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100469 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100470 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200471 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
472 + ectx->ec_dfunc_idx;
473 int argcount = ufunc_argcount(dfunc->df_ufunc);
474 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200475 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476
477 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200478 entry = estack_pop();
479 if (entry != NULL)
480 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200482 if (handle_closure_in_use(ectx, TRUE) == FAIL)
483 return FAIL;
484
485 // Clear the arguments.
486 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
487 clear_tv(STACK_TV(idx));
488
489 // Clear local variables and temp values, but not the return value.
490 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100491 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100492 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100493
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100494 // The return value should be on top of the stack. However, when aborting
495 // it may not be there and ec_frame_idx is the top of the stack.
496 ret_idx = ectx->ec_stack.ga_len - 1;
497 if (ret_idx == ectx->ec_frame_idx + 4)
498 ret_idx = 0;
499
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100500 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200501 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
502 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200503 ectx->ec_outer_stack =
504 (void *)STACK_TV(ectx->ec_frame_idx + 2)->vval.v_string;
505 ectx->ec_outer_frame = STACK_TV(ectx->ec_frame_idx + 3)->vval.v_number;
506 // restoring ec_frame_idx must be last
507 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 4)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
509 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100510
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100511 if (ret_idx > 0)
512 {
513 // Reset the stack to the position before the call, with a spot for the
514 // return value, moved there from above the frame.
515 ectx->ec_stack.ga_len = top + 1;
516 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
517 }
518 else
519 // Reset the stack to the position before the call.
520 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200521
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100522 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200523 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524}
525
526#undef STACK_TV
527
528/*
529 * Prepare arguments and rettv for calling a builtin or user function.
530 */
531 static int
532call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
533{
534 int idx;
535 typval_T *tv;
536
537 // Move arguments from bottom of the stack to argvars[] and add terminator.
538 for (idx = 0; idx < argcount; ++idx)
539 argvars[idx] = *STACK_TV_BOT(idx - argcount);
540 argvars[argcount].v_type = VAR_UNKNOWN;
541
542 // Result replaces the arguments on the stack.
543 if (argcount > 0)
544 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200545 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100546 return FAIL;
547 else
548 ++ectx->ec_stack.ga_len;
549
550 // Default return value is zero.
551 tv = STACK_TV_BOT(-1);
552 tv->v_type = VAR_NUMBER;
553 tv->vval.v_number = 0;
554
555 return OK;
556}
557
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200558// Ugly global to avoid passing the execution context around through many
559// layers.
560static ectx_T *current_ectx = NULL;
561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562/*
563 * Call a builtin function by index.
564 */
565 static int
566call_bfunc(int func_idx, int argcount, ectx_T *ectx)
567{
568 typval_T argvars[MAX_FUNC_ARGS];
569 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100570 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200571 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572
573 if (call_prepare(argcount, argvars, ectx) == FAIL)
574 return FAIL;
575
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200576 // Call the builtin function. Set "current_ectx" so that when it
577 // recursively invokes call_def_function() a closure context can be set.
578 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200580 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581
582 // Clear the arguments.
583 for (idx = 0; idx < argcount; ++idx)
584 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200585
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100586 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200587 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588 return OK;
589}
590
591/*
592 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100593 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594 */
595 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100596call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597{
598 typval_T argvars[MAX_FUNC_ARGS];
599 funcexe_T funcexe;
600 int error;
601 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100602 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200604 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200605 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
606 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200607 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100608 {
609 // The function has been compiled, can call it quickly. For a function
610 // that was defined later: we can call it directly next time.
611 if (iptr != NULL)
612 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100613 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100614 iptr->isn_type = ISN_DCALL;
615 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
616 iptr->isn_arg.dfunc.cdf_argcount = argcount;
617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100619 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620
621 if (call_prepare(argcount, argvars, ectx) == FAIL)
622 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200623 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 funcexe.evaluate = TRUE;
625
626 // Call the user function. Result goes in last position on the stack.
627 // TODO: add selfdict if there is one
628 error = call_user_func_check(ufunc, argcount, argvars,
629 STACK_TV_BOT(-1), &funcexe, NULL);
630
631 // Clear the arguments.
632 for (idx = 0; idx < argcount; ++idx)
633 clear_tv(&argvars[idx]);
634
635 if (error != FCERR_NONE)
636 {
637 user_func_error(error, ufunc->uf_name);
638 return FAIL;
639 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100640 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200641 // Error other than from calling the function itself.
642 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 return OK;
644}
645
646/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200647 * Return TRUE if an error was given or CTRL-C was pressed.
648 */
649 static int
650vim9_aborting(int prev_called_emsg)
651{
652 return called_emsg > prev_called_emsg || got_int || did_throw;
653}
654
655/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656 * Execute a function by "name".
657 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100658 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100659 * Returns FAIL if not found without an error message.
660 */
661 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100662call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663{
664 ufunc_T *ufunc;
665
666 if (builtin_function(name, -1))
667 {
668 int func_idx = find_internal_func(name);
669
670 if (func_idx < 0)
671 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200672 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673 return FAIL;
674 return call_bfunc(func_idx, argcount, ectx);
675 }
676
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200677 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200678
679 if (ufunc == NULL)
680 {
681 int called_emsg_before = called_emsg;
682
683 if (script_autoload(name, TRUE))
684 // loaded a package, search for the function again
685 ufunc = find_func(name, FALSE, NULL);
686 if (vim9_aborting(called_emsg_before))
687 return FAIL; // bail out if loading the script caused an error
688 }
689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100690 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100691 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692
693 return FAIL;
694}
695
696 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200697call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200699 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200700 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200702 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703
704 if (tv->v_type == VAR_PARTIAL)
705 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200706 partial_T *pt = tv->vval.v_partial;
707 int i;
708
709 if (pt->pt_argc > 0)
710 {
711 // Make space for arguments from the partial, shift the "argcount"
712 // arguments up.
713 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
714 return FAIL;
715 for (i = 1; i <= argcount; ++i)
716 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
717 ectx->ec_stack.ga_len += pt->pt_argc;
718 argcount += pt->pt_argc;
719
720 // copy the arguments from the partial onto the stack
721 for (i = 0; i < pt->pt_argc; ++i)
722 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
723 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724
725 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200726 {
727 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
728
729 // closure may need the function context where it was defined
730 ectx->ec_outer_stack = pt->pt_ectx_stack;
731 ectx->ec_outer_frame = pt->pt_ectx_frame;
732
733 return ret;
734 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735 name = pt->pt_name;
736 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200737 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200739 if (name != NULL)
740 {
741 char_u fname_buf[FLEN_FIXED + 1];
742 char_u *tofree = NULL;
743 int error = FCERR_NONE;
744 char_u *fname;
745
746 // May need to translate <SNR>123_ to K_SNR.
747 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
748 if (error != FCERR_NONE)
749 res = FAIL;
750 else
751 res = call_by_name(fname, argcount, ectx, NULL);
752 vim_free(tofree);
753 }
754
755 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 {
757 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200758 semsg(_(e_unknownfunc),
759 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 return FAIL;
761 }
762 return OK;
763}
764
765/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200766 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
767 * TRUE.
768 */
769 static int
770error_if_locked(int lock, char *error)
771{
772 if (lock & (VAR_LOCKED | VAR_FIXED))
773 {
774 emsg(_(error));
775 return TRUE;
776 }
777 return FALSE;
778}
779
780/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100781 * Store "tv" in variable "name".
782 * This is for s: and g: variables.
783 */
784 static void
785store_var(char_u *name, typval_T *tv)
786{
787 funccal_entry_T entry;
788
789 save_funccal(&entry);
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200790 set_var_const(name, NULL, tv, FALSE, ASSIGN_NO_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100791 restore_funccal();
792}
793
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100794/*
795 * When the value of "sv" is a null list of dict, allocate it.
796 */
797 static void
798allocate_if_null(typval_T *tv)
799{
800 switch (tv->v_type)
801 {
802 case VAR_LIST:
803 if (tv->vval.v_list == NULL)
804 rettv_list_alloc(tv);
805 break;
806 case VAR_DICT:
807 if (tv->vval.v_dict == NULL)
808 rettv_dict_alloc(tv);
809 break;
810 default:
811 break;
812 }
813}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200814
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100815/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100816 * Execute a function by "name".
817 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100818 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100819 */
820 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100821call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100822{
Bram Moolenaared677f52020-08-12 16:38:10 +0200823 int called_emsg_before = called_emsg;
824 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825
Bram Moolenaared677f52020-08-12 16:38:10 +0200826 res = call_by_name(name, argcount, ectx, iptr);
827 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100828 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200829 dictitem_T *v;
830
831 v = find_var(name, NULL, FALSE);
832 if (v == NULL)
833 {
834 semsg(_(e_unknownfunc), name);
835 return FAIL;
836 }
837 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
838 {
839 semsg(_(e_unknownfunc), name);
840 return FAIL;
841 }
842 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200844 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845}
846
847/*
848 * Call a "def" function from old Vim script.
849 * Return OK or FAIL.
850 */
851 int
852call_def_function(
853 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200854 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100855 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200856 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 typval_T *rettv) // return value
858{
859 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200860 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200861 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 typval_T *tv;
863 int idx;
864 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100865 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200866 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200867 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100868 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200869 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +0200870 msglist_T **saved_msg_list = NULL;
871 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +0200872 cmdmod_T save_cmdmod;
873 int restore_cmdmod = FALSE;
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100874 int save_emsg_silent_def = emsg_silent_def;
875 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100876 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100877 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878
879// Get pointer to item in the stack.
880#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
881
882// Get pointer to item at the bottom of the stack, -1 is the bottom.
883#undef STACK_TV_BOT
884#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
885
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200886// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200887#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 +0100888
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200889// Like STACK_TV_VAR but use the outer scope
890#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
891
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200892 if (ufunc->uf_def_status == UF_NOT_COMPILED
893 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200894 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200895 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100896 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200897 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200898 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200899 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200900 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200901
Bram Moolenaar09689a02020-05-09 22:50:08 +0200902 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200903 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200904 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
905 + ufunc->uf_dfunc_idx;
906 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200907 {
908 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200909 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200910 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200911 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100913 // If depth of calling is getting too high, don't execute the function.
914 orig_funcdepth = funcdepth_get();
915 if (funcdepth_increment() == FAIL)
916 return FAIL;
917
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200918 CLEAR_FIELD(ectx);
919 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
920 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
921 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100922 {
923 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200924 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100925 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200927 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928
929 // Put arguments on the stack.
930 for (idx = 0; idx < argc; ++idx)
931 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200932 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200933 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
934 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200935 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 copy_tv(&argv[idx], STACK_TV_BOT(0));
937 ++ectx.ec_stack.ga_len;
938 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200939
940 // Turn varargs into a list. Empty list if no args.
941 if (ufunc->uf_va_name != NULL)
942 {
943 int vararg_count = argc - ufunc->uf_args.ga_len;
944
945 if (vararg_count < 0)
946 vararg_count = 0;
947 else
948 argc -= vararg_count;
949 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200950 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200951
952 // Check the type of the list items.
953 tv = STACK_TV_BOT(-1);
954 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +0200955 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200956 && ufunc->uf_va_type->tt_member != &t_any
957 && tv->vval.v_list != NULL)
958 {
959 type_T *expected = ufunc->uf_va_type->tt_member;
960 listitem_T *li = tv->vval.v_list->lv_first;
961
962 for (idx = 0; idx < vararg_count; ++idx)
963 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200964 if (check_typval_type(expected, &li->li_tv,
965 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200966 goto failed_early;
967 li = li->li_next;
968 }
969 }
970
Bram Moolenaar23e03252020-04-12 22:22:31 +0200971 if (defcount > 0)
972 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200973 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200974 --ectx.ec_stack.ga_len;
975 }
976
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100977 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200978 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100979 if (defcount > 0)
980 for (idx = 0; idx < defcount; ++idx)
981 {
982 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
983 ++ectx.ec_stack.ga_len;
984 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200985 if (ufunc->uf_va_name != NULL)
986 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987
988 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200989 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
990 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200992 if (partial != NULL)
993 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200994 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
995 {
996 // TODO: is this always the right way?
997 ectx.ec_outer_stack = &current_ectx->ec_stack;
998 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
999 }
1000 else
1001 {
1002 ectx.ec_outer_stack = partial->pt_ectx_stack;
1003 ectx.ec_outer_frame = partial->pt_ectx_frame;
1004 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001005 }
1006
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 // dummy frame entries
1008 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1009 {
1010 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1011 ++ectx.ec_stack.ga_len;
1012 }
1013
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001014 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001015 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001016 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1017 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001018
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001019 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001020 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001021 ectx.ec_stack.ga_len += dfunc->df_varcount;
1022 if (dfunc->df_has_closure)
1023 {
1024 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1025 STACK_TV_VAR(idx)->vval.v_number = 0;
1026 ++ectx.ec_stack.ga_len;
1027 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001028
1029 ectx.ec_instr = dfunc->df_instr;
1030 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001031
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001032 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001033 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001034 estack_push_ufunc(ufunc, 1);
1035 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001036 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1037
Bram Moolenaar352134b2020-10-17 22:04:08 +02001038 // Use a specific location for storing error messages to be converted to an
1039 // exception.
1040 saved_msg_list = msg_list;
1041 msg_list = &private_msg_list;
1042
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001043 // Do turn errors into exceptions.
1044 suppress_errthrow = FALSE;
1045
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001046 // When ":silent!" was used before calling then we still abort the
1047 // function. If ":silent!" is used in the function then we don't.
1048 emsg_silent_def = emsg_silent;
1049 did_emsg_def = 0;
1050
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001051 // Decide where to start execution, handles optional arguments.
1052 init_instr_idx(ufunc, argc, &ectx);
1053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001054 for (;;)
1055 {
1056 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001057
Bram Moolenaar270d0382020-05-15 21:42:53 +02001058 if (++breakcheck_count >= 100)
1059 {
1060 line_breakcheck();
1061 breakcheck_count = 0;
1062 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001063 if (got_int)
1064 {
1065 // Turn CTRL-C into an exception.
1066 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001067 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001068 goto failed;
1069 did_throw = TRUE;
1070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001071
Bram Moolenaara26b9702020-04-18 19:53:28 +02001072 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1073 {
1074 // Turn an error message into an exception.
1075 did_emsg = FALSE;
1076 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1077 goto failed;
1078 did_throw = TRUE;
1079 *msg_list = NULL;
1080 }
1081
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001082 if (did_throw && !ectx.ec_in_catch)
1083 {
1084 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001085 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086
1087 // An exception jumps to the first catch, finally, or returns from
1088 // the current function.
1089 if (trystack->ga_len > 0)
1090 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001091 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092 {
1093 // jump to ":catch" or ":finally"
1094 ectx.ec_in_catch = TRUE;
1095 ectx.ec_iidx = trycmd->tcd_catch_idx;
1096 }
1097 else
1098 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001099 // Not inside try or need to return from current functions.
1100 // Push a dummy return value.
1101 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1102 goto failed;
1103 tv = STACK_TV_BOT(0);
1104 tv->v_type = VAR_NUMBER;
1105 tv->vval.v_number = 0;
1106 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001107 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001109 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001110 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001111 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1112 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113 goto done;
1114 }
1115
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001116 if (func_return(&ectx) == FAIL)
1117 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 }
1119 continue;
1120 }
1121
1122 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1123 switch (iptr->isn_type)
1124 {
1125 // execute Ex command line
1126 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001127 {
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001128 SOURCING_LNUM = iptr->isn_lnum;
1129 do_cmdline_cmd(iptr->isn_arg.string);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001130 if (did_emsg)
1131 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133 break;
1134
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001135 // execute Ex command from pieces on the stack
1136 case ISN_EXECCONCAT:
1137 {
1138 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001139 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001140 int pass;
1141 int i;
1142 char_u *cmd = NULL;
1143 char_u *str;
1144
1145 for (pass = 1; pass <= 2; ++pass)
1146 {
1147 for (i = 0; i < count; ++i)
1148 {
1149 tv = STACK_TV_BOT(i - count);
1150 str = tv->vval.v_string;
1151 if (str != NULL && *str != NUL)
1152 {
1153 if (pass == 2)
1154 STRCPY(cmd + len, str);
1155 len += STRLEN(str);
1156 }
1157 if (pass == 2)
1158 clear_tv(tv);
1159 }
1160 if (pass == 1)
1161 {
1162 cmd = alloc(len + 1);
1163 if (cmd == NULL)
1164 goto failed;
1165 len = 0;
1166 }
1167 }
1168
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001169 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001170 do_cmdline_cmd(cmd);
1171 vim_free(cmd);
1172 }
1173 break;
1174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 // execute :echo {string} ...
1176 case ISN_ECHO:
1177 {
1178 int count = iptr->isn_arg.echo.echo_count;
1179 int atstart = TRUE;
1180 int needclr = TRUE;
1181
1182 for (idx = 0; idx < count; ++idx)
1183 {
1184 tv = STACK_TV_BOT(idx - count);
1185 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1186 &atstart, &needclr);
1187 clear_tv(tv);
1188 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001189 if (needclr)
1190 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191 ectx.ec_stack.ga_len -= count;
1192 }
1193 break;
1194
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001195 // :execute {string} ...
1196 // :echomsg {string} ...
1197 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001198 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001199 case ISN_ECHOMSG:
1200 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001201 {
1202 int count = iptr->isn_arg.number;
1203 garray_T ga;
1204 char_u buf[NUMBUFLEN];
1205 char_u *p;
1206 int len;
1207 int failed = FALSE;
1208
1209 ga_init2(&ga, 1, 80);
1210 for (idx = 0; idx < count; ++idx)
1211 {
1212 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001213 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001214 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001215 if (tv->v_type == VAR_CHANNEL
1216 || tv->v_type == VAR_JOB)
1217 {
1218 SOURCING_LNUM = iptr->isn_lnum;
1219 emsg(_(e_inval_string));
1220 break;
1221 }
1222 else
1223 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001224 }
1225 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001226 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001227
1228 len = (int)STRLEN(p);
1229 if (ga_grow(&ga, len + 2) == FAIL)
1230 failed = TRUE;
1231 else
1232 {
1233 if (ga.ga_len > 0)
1234 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1235 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1236 ga.ga_len += len;
1237 }
1238 clear_tv(tv);
1239 }
1240 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001241 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001242 {
1243 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001244 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001245 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001246
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001247 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001248 {
1249 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001250 {
1251 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001252 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001253 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001254 {
1255 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001256 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001257 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001258 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001259 else
1260 {
1261 msg_sb_eol();
1262 if (iptr->isn_type == ISN_ECHOMSG)
1263 {
1264 msg_attr(ga.ga_data, echo_attr);
1265 out_flush();
1266 }
1267 else
1268 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001269 SOURCING_LNUM = iptr->isn_lnum;
1270 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001271 }
1272 }
1273 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001274 ga_clear(&ga);
1275 }
1276 break;
1277
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001278 // load local variable or argument
1279 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001280 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 goto failed;
1282 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1283 ++ectx.ec_stack.ga_len;
1284 break;
1285
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001286 // load variable or argument from outer scope
1287 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001288 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001289 goto failed;
1290 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1291 STACK_TV_BOT(0));
1292 ++ectx.ec_stack.ga_len;
1293 break;
1294
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 // load v: variable
1296 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001297 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298 goto failed;
1299 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1300 ++ectx.ec_stack.ga_len;
1301 break;
1302
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001303 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001304 case ISN_LOADSCRIPT:
1305 {
1306 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001307 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308 svar_T *sv;
1309
1310 sv = ((svar_T *)si->sn_var_vals.ga_data)
1311 + iptr->isn_arg.script.script_idx;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001312 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001313 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314 goto failed;
1315 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1316 ++ectx.ec_stack.ga_len;
1317 }
1318 break;
1319
1320 // load s: variable in old script
1321 case ISN_LOADS:
1322 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001323 hashtab_T *ht = &SCRIPT_VARS(
1324 iptr->isn_arg.loadstore.ls_sid);
1325 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001327
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 if (di == NULL)
1329 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001330 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001331 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001332 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 }
1334 else
1335 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001336 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 goto failed;
1338 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1339 ++ectx.ec_stack.ga_len;
1340 }
1341 }
1342 break;
1343
Bram Moolenaard3aac292020-04-19 14:32:17 +02001344 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001346 case ISN_LOADB:
1347 case ISN_LOADW:
1348 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001350 dictitem_T *di = NULL;
1351 hashtab_T *ht = NULL;
1352 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001353
Bram Moolenaard3aac292020-04-19 14:32:17 +02001354 switch (iptr->isn_type)
1355 {
1356 case ISN_LOADG:
1357 ht = get_globvar_ht();
1358 namespace = 'g';
1359 break;
1360 case ISN_LOADB:
1361 ht = &curbuf->b_vars->dv_hashtab;
1362 namespace = 'b';
1363 break;
1364 case ISN_LOADW:
1365 ht = &curwin->w_vars->dv_hashtab;
1366 namespace = 'w';
1367 break;
1368 case ISN_LOADT:
1369 ht = &curtab->tp_vars->dv_hashtab;
1370 namespace = 't';
1371 break;
1372 default: // Cannot reach here
1373 goto failed;
1374 }
1375 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 if (di == NULL)
1378 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001379 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001380 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001381 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001382 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 }
1384 else
1385 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001386 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 goto failed;
1388 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1389 ++ectx.ec_stack.ga_len;
1390 }
1391 }
1392 break;
1393
Bram Moolenaar03290b82020-12-19 16:30:44 +01001394 // load autoload variable
1395 case ISN_LOADAUTO:
1396 {
1397 char_u *name = iptr->isn_arg.string;
1398
1399 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1400 goto failed;
1401 SOURCING_LNUM = iptr->isn_lnum;
1402 if (eval_variable(name, STRLEN(name),
1403 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1404 goto on_error;
1405 ++ectx.ec_stack.ga_len;
1406 }
1407 break;
1408
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001409 // load g:/b:/w:/t: namespace
1410 case ISN_LOADGDICT:
1411 case ISN_LOADBDICT:
1412 case ISN_LOADWDICT:
1413 case ISN_LOADTDICT:
1414 {
1415 dict_T *d = NULL;
1416
1417 switch (iptr->isn_type)
1418 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001419 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1420 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1421 case ISN_LOADWDICT: d = curwin->w_vars; break;
1422 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001423 default: // Cannot reach here
1424 goto failed;
1425 }
1426 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1427 goto failed;
1428 tv = STACK_TV_BOT(0);
1429 tv->v_type = VAR_DICT;
1430 tv->v_lock = 0;
1431 tv->vval.v_dict = d;
1432 ++ectx.ec_stack.ga_len;
1433 }
1434 break;
1435
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001436 // load &option
1437 case ISN_LOADOPT:
1438 {
1439 typval_T optval;
1440 char_u *name = iptr->isn_arg.string;
1441
Bram Moolenaara8c17702020-04-01 21:17:24 +02001442 // This is not expected to fail, name is checked during
1443 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001444 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001446 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001447 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 *STACK_TV_BOT(0) = optval;
1449 ++ectx.ec_stack.ga_len;
1450 }
1451 break;
1452
1453 // load $ENV
1454 case ISN_LOADENV:
1455 {
1456 typval_T optval;
1457 char_u *name = iptr->isn_arg.string;
1458
Bram Moolenaar270d0382020-05-15 21:42:53 +02001459 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001461 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001462 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 *STACK_TV_BOT(0) = optval;
1464 ++ectx.ec_stack.ga_len;
1465 }
1466 break;
1467
1468 // load @register
1469 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001470 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 goto failed;
1472 tv = STACK_TV_BOT(0);
1473 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001474 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001475 // This may result in NULL, which should be equivalent to an
1476 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001477 tv->vval.v_string = get_reg_contents(
1478 iptr->isn_arg.number, GREG_EXPR_SRC);
1479 ++ectx.ec_stack.ga_len;
1480 break;
1481
1482 // store local variable
1483 case ISN_STORE:
1484 --ectx.ec_stack.ga_len;
1485 tv = STACK_TV_VAR(iptr->isn_arg.number);
1486 clear_tv(tv);
1487 *tv = *STACK_TV_BOT(0);
1488 break;
1489
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001490 // store variable or argument in outer scope
1491 case ISN_STOREOUTER:
1492 --ectx.ec_stack.ga_len;
1493 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1494 clear_tv(tv);
1495 *tv = *STACK_TV_BOT(0);
1496 break;
1497
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001498 // store s: variable in old script
1499 case ISN_STORES:
1500 {
1501 hashtab_T *ht = &SCRIPT_VARS(
1502 iptr->isn_arg.loadstore.ls_sid);
1503 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001504 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001505
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001506 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001507 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001508 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001509 else
1510 {
1511 clear_tv(&di->di_tv);
1512 di->di_tv = *STACK_TV_BOT(0);
1513 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001514 }
1515 break;
1516
1517 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 case ISN_STORESCRIPT:
1519 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001520 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 iptr->isn_arg.script.script_sid);
1522 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1523 + iptr->isn_arg.script.script_idx;
1524
1525 --ectx.ec_stack.ga_len;
1526 clear_tv(sv->sv_tv);
1527 *sv->sv_tv = *STACK_TV_BOT(0);
1528 }
1529 break;
1530
1531 // store option
1532 case ISN_STOREOPT:
1533 {
1534 long n = 0;
1535 char_u *s = NULL;
1536 char *msg;
1537
1538 --ectx.ec_stack.ga_len;
1539 tv = STACK_TV_BOT(0);
1540 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001541 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001543 if (s == NULL)
1544 s = (char_u *)"";
1545 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001547 // must be VAR_NUMBER, CHECKTYPE makes sure
1548 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1550 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001551 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 if (msg != NULL)
1553 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001554 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001556 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 }
1559 break;
1560
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001561 // store $ENV
1562 case ISN_STOREENV:
1563 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001564 tv = STACK_TV_BOT(0);
1565 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1566 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001567 break;
1568
1569 // store @r
1570 case ISN_STOREREG:
1571 {
1572 int reg = iptr->isn_arg.number;
1573
1574 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001575 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001576 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001577 tv_get_string(tv), -1, FALSE);
1578 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001579 }
1580 break;
1581
1582 // store v: variable
1583 case ISN_STOREV:
1584 --ectx.ec_stack.ga_len;
1585 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1586 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001587 // should not happen, type is checked when compiling
1588 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001589 break;
1590
Bram Moolenaard3aac292020-04-19 14:32:17 +02001591 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001593 case ISN_STOREB:
1594 case ISN_STOREW:
1595 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 {
1597 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001598 hashtab_T *ht;
1599 switch (iptr->isn_type)
1600 {
1601 case ISN_STOREG:
1602 ht = get_globvar_ht();
1603 break;
1604 case ISN_STOREB:
1605 ht = &curbuf->b_vars->dv_hashtab;
1606 break;
1607 case ISN_STOREW:
1608 ht = &curwin->w_vars->dv_hashtab;
1609 break;
1610 case ISN_STORET:
1611 ht = &curtab->tp_vars->dv_hashtab;
1612 break;
1613 default: // Cannot reach here
1614 goto failed;
1615 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616
1617 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001618 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001620 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 else
1622 {
1623 clear_tv(&di->di_tv);
1624 di->di_tv = *STACK_TV_BOT(0);
1625 }
1626 }
1627 break;
1628
Bram Moolenaar03290b82020-12-19 16:30:44 +01001629 // store an autoload variable
1630 case ISN_STOREAUTO:
1631 SOURCING_LNUM = iptr->isn_lnum;
1632 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1633 clear_tv(STACK_TV_BOT(-1));
1634 --ectx.ec_stack.ga_len;
1635 break;
1636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637 // store number in local variable
1638 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001639 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 clear_tv(tv);
1641 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001642 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001643 break;
1644
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001645 // store value in list variable
1646 case ISN_STORELIST:
1647 {
1648 typval_T *tv_idx = STACK_TV_BOT(-2);
1649 varnumber_T lidx = tv_idx->vval.v_number;
1650 typval_T *tv_list = STACK_TV_BOT(-1);
1651 list_T *list = tv_list->vval.v_list;
1652
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001653 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001654 if (lidx < 0 && list->lv_len + lidx >= 0)
1655 // negative index is relative to the end
1656 lidx = list->lv_len + lidx;
1657 if (lidx < 0 || lidx > list->lv_len)
1658 {
1659 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001660 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001661 }
1662 tv = STACK_TV_BOT(-3);
1663 if (lidx < list->lv_len)
1664 {
1665 listitem_T *li = list_find(list, lidx);
1666
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001667 if (error_if_locked(li->li_tv.v_lock,
1668 e_cannot_change_list_item))
1669 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001670 // overwrite existing list item
1671 clear_tv(&li->li_tv);
1672 li->li_tv = *tv;
1673 }
1674 else
1675 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001676 if (error_if_locked(list->lv_lock,
1677 e_cannot_change_list))
1678 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001679 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001680 if (list_append_tv(list, tv) == FAIL)
1681 goto failed;
1682 clear_tv(tv);
1683 }
1684 clear_tv(tv_idx);
1685 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001686 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001687 }
1688 break;
1689
1690 // store value in dict variable
1691 case ISN_STOREDICT:
1692 {
1693 typval_T *tv_key = STACK_TV_BOT(-2);
1694 char_u *key = tv_key->vval.v_string;
1695 typval_T *tv_dict = STACK_TV_BOT(-1);
1696 dict_T *dict = tv_dict->vval.v_dict;
1697 dictitem_T *di;
1698
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001699 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001700 if (dict == NULL)
1701 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001702 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001703 goto on_error;
1704 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001705 if (key == NULL)
1706 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001707 tv = STACK_TV_BOT(-3);
1708 di = dict_find(dict, key, -1);
1709 if (di != NULL)
1710 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001711 if (error_if_locked(di->di_tv.v_lock,
1712 e_cannot_change_dict_item))
1713 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001714 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001715 clear_tv(&di->di_tv);
1716 di->di_tv = *tv;
1717 }
1718 else
1719 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001720 if (error_if_locked(dict->dv_lock,
1721 e_cannot_change_dict))
1722 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001723 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001724 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1725 goto failed;
1726 clear_tv(tv);
1727 }
1728 clear_tv(tv_key);
1729 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001730 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001731 }
1732 break;
1733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 // push constant
1735 case ISN_PUSHNR:
1736 case ISN_PUSHBOOL:
1737 case ISN_PUSHSPEC:
1738 case ISN_PUSHF:
1739 case ISN_PUSHS:
1740 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001741 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001742 case ISN_PUSHCHANNEL:
1743 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001744 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 goto failed;
1746 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001747 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 ++ectx.ec_stack.ga_len;
1749 switch (iptr->isn_type)
1750 {
1751 case ISN_PUSHNR:
1752 tv->v_type = VAR_NUMBER;
1753 tv->vval.v_number = iptr->isn_arg.number;
1754 break;
1755 case ISN_PUSHBOOL:
1756 tv->v_type = VAR_BOOL;
1757 tv->vval.v_number = iptr->isn_arg.number;
1758 break;
1759 case ISN_PUSHSPEC:
1760 tv->v_type = VAR_SPECIAL;
1761 tv->vval.v_number = iptr->isn_arg.number;
1762 break;
1763#ifdef FEAT_FLOAT
1764 case ISN_PUSHF:
1765 tv->v_type = VAR_FLOAT;
1766 tv->vval.v_float = iptr->isn_arg.fnumber;
1767 break;
1768#endif
1769 case ISN_PUSHBLOB:
1770 blob_copy(iptr->isn_arg.blob, tv);
1771 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001772 case ISN_PUSHFUNC:
1773 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001774 if (iptr->isn_arg.string == NULL)
1775 tv->vval.v_string = NULL;
1776 else
1777 tv->vval.v_string =
1778 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001779 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001780 case ISN_PUSHCHANNEL:
1781#ifdef FEAT_JOB_CHANNEL
1782 tv->v_type = VAR_CHANNEL;
1783 tv->vval.v_channel = iptr->isn_arg.channel;
1784 if (tv->vval.v_channel != NULL)
1785 ++tv->vval.v_channel->ch_refcount;
1786#endif
1787 break;
1788 case ISN_PUSHJOB:
1789#ifdef FEAT_JOB_CHANNEL
1790 tv->v_type = VAR_JOB;
1791 tv->vval.v_job = iptr->isn_arg.job;
1792 if (tv->vval.v_job != NULL)
1793 ++tv->vval.v_job->jv_refcount;
1794#endif
1795 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 default:
1797 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001798 tv->vval.v_string = vim_strsave(
1799 iptr->isn_arg.string == NULL
1800 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 }
1802 break;
1803
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001804 case ISN_UNLET:
1805 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1806 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001807 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001808 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001809 case ISN_UNLETENV:
1810 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1811 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001812
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001813 case ISN_LOCKCONST:
1814 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1815 break;
1816
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 // create a list from items on the stack; uses a single allocation
1818 // for the list header and the items
1819 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001820 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1821 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 break;
1823
1824 // create a dict from items on the stack
1825 case ISN_NEWDICT:
1826 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001827 int count = iptr->isn_arg.number;
1828 dict_T *dict = dict_alloc();
1829 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001830 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831
1832 if (dict == NULL)
1833 goto failed;
1834 for (idx = 0; idx < count; ++idx)
1835 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001836 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001838 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001839 key = tv->vval.v_string == NULL
1840 ? (char_u *)"" : tv->vval.v_string;
1841 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02001842 if (item != NULL)
1843 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001844 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001845 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02001846 dict_unref(dict);
1847 goto on_error;
1848 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001849 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 clear_tv(tv);
1851 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001852 {
1853 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001855 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1857 item->di_tv.v_lock = 0;
1858 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001859 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001860 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001861 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001863 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 }
1865
1866 if (count > 0)
1867 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001868 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 goto failed;
1870 else
1871 ++ectx.ec_stack.ga_len;
1872 tv = STACK_TV_BOT(-1);
1873 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001874 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 tv->vval.v_dict = dict;
1876 ++dict->dv_refcount;
1877 }
1878 break;
1879
1880 // call a :def function
1881 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001882 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1884 iptr->isn_arg.dfunc.cdf_argcount,
1885 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001886 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 break;
1888
1889 // call a builtin function
1890 case ISN_BCALL:
1891 SOURCING_LNUM = iptr->isn_lnum;
1892 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1893 iptr->isn_arg.bfunc.cbf_argcount,
1894 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001895 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896 break;
1897
1898 // call a funcref or partial
1899 case ISN_PCALL:
1900 {
1901 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1902 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001903 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904
1905 SOURCING_LNUM = iptr->isn_lnum;
1906 if (pfunc->cpf_top)
1907 {
1908 // funcref is above the arguments
1909 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1910 }
1911 else
1912 {
1913 // Get the funcref from the stack.
1914 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001915 partial_tv = *STACK_TV_BOT(0);
1916 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 }
1918 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001919 if (tv == &partial_tv)
1920 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001922 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923 }
1924 break;
1925
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001926 case ISN_PCALL_END:
1927 // PCALL finished, arguments have been consumed and replaced by
1928 // the return value. Now clear the funcref from the stack,
1929 // and move the return value in its place.
1930 --ectx.ec_stack.ga_len;
1931 clear_tv(STACK_TV_BOT(-1));
1932 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1933 break;
1934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 // call a user defined function or funcref/partial
1936 case ISN_UCALL:
1937 {
1938 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1939
1940 SOURCING_LNUM = iptr->isn_lnum;
1941 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001942 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001943 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 }
1945 break;
1946
1947 // return from a :def function call
1948 case ISN_RETURN:
1949 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001950 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001951 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001952
1953 if (trystack->ga_len > 0)
1954 trycmd = ((trycmd_T *)trystack->ga_data)
1955 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001956 if (trycmd != NULL
1957 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 && trycmd->tcd_finally_idx != 0)
1959 {
1960 // jump to ":finally"
1961 ectx.ec_iidx = trycmd->tcd_finally_idx;
1962 trycmd->tcd_return = TRUE;
1963 }
1964 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001965 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 }
1967 break;
1968
1969 // push a function reference to a compiled function
1970 case ISN_FUNCREF:
1971 {
1972 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001973 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974
1975 pt = ALLOC_CLEAR_ONE(partial_T);
1976 if (pt == NULL)
1977 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001978 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001979 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001980 vim_free(pt);
1981 goto failed;
1982 }
1983 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1984 + iptr->isn_arg.funcref.fr_func;
1985 pt->pt_func = pt_dfunc->df_ufunc;
1986 pt->pt_refcount = 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001987
1988 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1989 {
1990 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1991 + ectx.ec_dfunc_idx;
1992
1993 // The closure needs to find arguments and local
1994 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001995 pt->pt_ectx_stack = &ectx.ec_stack;
1996 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001997
1998 // If this function returns and the closure is still
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001999 // being used, we need to make a copy of the context
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002000 // (arguments and local variables). Store a reference
2001 // to the partial so we can handle that.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002002 if (ga_grow(&ectx.ec_funcrefs, 1) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002003 {
Bram Moolenaardec07512020-09-18 23:11:10 +02002004 vim_free(pt);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002005 goto failed;
2006 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002007 // Extra variable keeps the count of closures created
2008 // in the current function call.
2009 tv = STACK_TV_VAR(dfunc->df_varcount);
2010 ++tv->vval.v_number;
2011
2012 ((partial_T **)ectx.ec_funcrefs.ga_data)
2013 [ectx.ec_funcrefs.ga_len] = pt;
2014 ++pt->pt_refcount;
2015 ++ectx.ec_funcrefs.ga_len;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002016 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002017 ++pt_dfunc->df_ufunc->uf_refcount;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 tv = STACK_TV_BOT(0);
2020 ++ectx.ec_stack.ga_len;
2021 tv->vval.v_partial = pt;
2022 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002023 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 }
2025 break;
2026
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002027 // Create a global function from a lambda.
2028 case ISN_NEWFUNC:
2029 {
2030 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2031
2032 copy_func(newfunc->nf_lambda, newfunc->nf_global);
2033 }
2034 break;
2035
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002036 // List functions
2037 case ISN_DEF:
2038 if (iptr->isn_arg.string == NULL)
2039 list_functions(NULL);
2040 else
2041 {
2042 exarg_T ea;
2043
2044 CLEAR_FIELD(ea);
2045 ea.cmd = ea.arg = iptr->isn_arg.string;
2046 define_function(&ea, NULL);
2047 }
2048 break;
2049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 // jump if a condition is met
2051 case ISN_JUMP:
2052 {
2053 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002054 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055 int jump = TRUE;
2056
2057 if (when != JUMP_ALWAYS)
2058 {
2059 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002060 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002061 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002062 || when == JUMP_IF_COND_TRUE)
2063 {
2064 SOURCING_LNUM = iptr->isn_lnum;
2065 jump = tv_get_bool_chk(tv, &error);
2066 if (error)
2067 goto on_error;
2068 }
2069 else
2070 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002072 || when == JUMP_AND_KEEP_IF_FALSE
2073 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002075 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 {
2077 // drop the value from the stack
2078 clear_tv(tv);
2079 --ectx.ec_stack.ga_len;
2080 }
2081 }
2082 if (jump)
2083 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2084 }
2085 break;
2086
2087 // top of a for loop
2088 case ISN_FOR:
2089 {
2090 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2091 typval_T *idxtv =
2092 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2093
2094 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002095 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002097 ++idxtv->vval.v_number;
2098 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 // past the end of the list, jump to "endfor"
2100 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2101 else if (list->lv_first == &range_list_item)
2102 {
2103 // non-materialized range() list
2104 tv = STACK_TV_BOT(0);
2105 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002106 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 tv->vval.v_number = list_find_nr(
2108 list, idxtv->vval.v_number, NULL);
2109 ++ectx.ec_stack.ga_len;
2110 }
2111 else
2112 {
2113 listitem_T *li = list_find(list, idxtv->vval.v_number);
2114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2116 ++ectx.ec_stack.ga_len;
2117 }
2118 }
2119 break;
2120
2121 // start of ":try" block
2122 case ISN_TRY:
2123 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002124 trycmd_T *trycmd = NULL;
2125
Bram Moolenaar270d0382020-05-15 21:42:53 +02002126 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127 goto failed;
2128 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2129 + ectx.ec_trystack.ga_len;
2130 ++ectx.ec_trystack.ga_len;
2131 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002132 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2134 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002135 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002136 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 }
2138 break;
2139
2140 case ISN_PUSHEXC:
2141 if (current_exception == NULL)
2142 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002143 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144 iemsg("Evaluating catch while current_exception is NULL");
2145 goto failed;
2146 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002147 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 goto failed;
2149 tv = STACK_TV_BOT(0);
2150 ++ectx.ec_stack.ga_len;
2151 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002152 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 tv->vval.v_string = vim_strsave(
2154 (char_u *)current_exception->value);
2155 break;
2156
2157 case ISN_CATCH:
2158 {
2159 garray_T *trystack = &ectx.ec_trystack;
2160
2161 if (trystack->ga_len > 0)
2162 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002163 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 + trystack->ga_len - 1;
2165 trycmd->tcd_caught = TRUE;
2166 }
2167 did_emsg = got_int = did_throw = FALSE;
2168 catch_exception(current_exception);
2169 }
2170 break;
2171
2172 // end of ":try" block
2173 case ISN_ENDTRY:
2174 {
2175 garray_T *trystack = &ectx.ec_trystack;
2176
2177 if (trystack->ga_len > 0)
2178 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002179 trycmd_T *trycmd = NULL;
2180
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 --trystack->ga_len;
2182 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002183 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184 trycmd = ((trycmd_T *)trystack->ga_data)
2185 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002186 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 {
2188 // discard the exception
2189 if (caught_stack == current_exception)
2190 caught_stack = caught_stack->caught;
2191 discard_current_exception();
2192 }
2193
2194 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002195 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 }
2197 }
2198 break;
2199
2200 case ISN_THROW:
2201 --ectx.ec_stack.ga_len;
2202 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002203 if (tv->vval.v_string == NULL
2204 || *skipwhite(tv->vval.v_string) == NUL)
2205 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002206 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002207 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002208 emsg(_(e_throw_with_empty_string));
2209 goto failed;
2210 }
2211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2213 {
2214 vim_free(tv->vval.v_string);
2215 goto failed;
2216 }
2217 did_throw = TRUE;
2218 break;
2219
2220 // compare with special values
2221 case ISN_COMPAREBOOL:
2222 case ISN_COMPARESPECIAL:
2223 {
2224 typval_T *tv1 = STACK_TV_BOT(-2);
2225 typval_T *tv2 = STACK_TV_BOT(-1);
2226 varnumber_T arg1 = tv1->vval.v_number;
2227 varnumber_T arg2 = tv2->vval.v_number;
2228 int res;
2229
2230 switch (iptr->isn_arg.op.op_type)
2231 {
2232 case EXPR_EQUAL: res = arg1 == arg2; break;
2233 case EXPR_NEQUAL: res = arg1 != arg2; break;
2234 default: res = 0; break;
2235 }
2236
2237 --ectx.ec_stack.ga_len;
2238 tv1->v_type = VAR_BOOL;
2239 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2240 }
2241 break;
2242
2243 // Operation with two number arguments
2244 case ISN_OPNR:
2245 case ISN_COMPARENR:
2246 {
2247 typval_T *tv1 = STACK_TV_BOT(-2);
2248 typval_T *tv2 = STACK_TV_BOT(-1);
2249 varnumber_T arg1 = tv1->vval.v_number;
2250 varnumber_T arg2 = tv2->vval.v_number;
2251 varnumber_T res;
2252
2253 switch (iptr->isn_arg.op.op_type)
2254 {
2255 case EXPR_MULT: res = arg1 * arg2; break;
2256 case EXPR_DIV: res = arg1 / arg2; break;
2257 case EXPR_REM: res = arg1 % arg2; break;
2258 case EXPR_SUB: res = arg1 - arg2; break;
2259 case EXPR_ADD: res = arg1 + arg2; break;
2260
2261 case EXPR_EQUAL: res = arg1 == arg2; break;
2262 case EXPR_NEQUAL: res = arg1 != arg2; break;
2263 case EXPR_GREATER: res = arg1 > arg2; break;
2264 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2265 case EXPR_SMALLER: res = arg1 < arg2; break;
2266 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2267 default: res = 0; break;
2268 }
2269
2270 --ectx.ec_stack.ga_len;
2271 if (iptr->isn_type == ISN_COMPARENR)
2272 {
2273 tv1->v_type = VAR_BOOL;
2274 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2275 }
2276 else
2277 tv1->vval.v_number = res;
2278 }
2279 break;
2280
2281 // Computation with two float arguments
2282 case ISN_OPFLOAT:
2283 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002284#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 {
2286 typval_T *tv1 = STACK_TV_BOT(-2);
2287 typval_T *tv2 = STACK_TV_BOT(-1);
2288 float_T arg1 = tv1->vval.v_float;
2289 float_T arg2 = tv2->vval.v_float;
2290 float_T res = 0;
2291 int cmp = FALSE;
2292
2293 switch (iptr->isn_arg.op.op_type)
2294 {
2295 case EXPR_MULT: res = arg1 * arg2; break;
2296 case EXPR_DIV: res = arg1 / arg2; break;
2297 case EXPR_SUB: res = arg1 - arg2; break;
2298 case EXPR_ADD: res = arg1 + arg2; break;
2299
2300 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2301 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2302 case EXPR_GREATER: cmp = arg1 > arg2; break;
2303 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2304 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2305 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2306 default: cmp = 0; break;
2307 }
2308 --ectx.ec_stack.ga_len;
2309 if (iptr->isn_type == ISN_COMPAREFLOAT)
2310 {
2311 tv1->v_type = VAR_BOOL;
2312 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2313 }
2314 else
2315 tv1->vval.v_float = res;
2316 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002317#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318 break;
2319
2320 case ISN_COMPARELIST:
2321 {
2322 typval_T *tv1 = STACK_TV_BOT(-2);
2323 typval_T *tv2 = STACK_TV_BOT(-1);
2324 list_T *arg1 = tv1->vval.v_list;
2325 list_T *arg2 = tv2->vval.v_list;
2326 int cmp = FALSE;
2327 int ic = iptr->isn_arg.op.op_ic;
2328
2329 switch (iptr->isn_arg.op.op_type)
2330 {
2331 case EXPR_EQUAL: cmp =
2332 list_equal(arg1, arg2, ic, FALSE); break;
2333 case EXPR_NEQUAL: cmp =
2334 !list_equal(arg1, arg2, ic, FALSE); break;
2335 case EXPR_IS: cmp = arg1 == arg2; break;
2336 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2337 default: cmp = 0; break;
2338 }
2339 --ectx.ec_stack.ga_len;
2340 clear_tv(tv1);
2341 clear_tv(tv2);
2342 tv1->v_type = VAR_BOOL;
2343 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2344 }
2345 break;
2346
2347 case ISN_COMPAREBLOB:
2348 {
2349 typval_T *tv1 = STACK_TV_BOT(-2);
2350 typval_T *tv2 = STACK_TV_BOT(-1);
2351 blob_T *arg1 = tv1->vval.v_blob;
2352 blob_T *arg2 = tv2->vval.v_blob;
2353 int cmp = FALSE;
2354
2355 switch (iptr->isn_arg.op.op_type)
2356 {
2357 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2358 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2359 case EXPR_IS: cmp = arg1 == arg2; break;
2360 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2361 default: cmp = 0; break;
2362 }
2363 --ectx.ec_stack.ga_len;
2364 clear_tv(tv1);
2365 clear_tv(tv2);
2366 tv1->v_type = VAR_BOOL;
2367 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2368 }
2369 break;
2370
2371 // TODO: handle separately
2372 case ISN_COMPARESTRING:
2373 case ISN_COMPAREDICT:
2374 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 case ISN_COMPAREANY:
2376 {
2377 typval_T *tv1 = STACK_TV_BOT(-2);
2378 typval_T *tv2 = STACK_TV_BOT(-1);
2379 exptype_T exptype = iptr->isn_arg.op.op_type;
2380 int ic = iptr->isn_arg.op.op_ic;
2381
Bram Moolenaareb26f432020-09-14 16:50:05 +02002382 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 typval_compare(tv1, tv2, exptype, ic);
2384 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 --ectx.ec_stack.ga_len;
2386 }
2387 break;
2388
2389 case ISN_ADDLIST:
2390 case ISN_ADDBLOB:
2391 {
2392 typval_T *tv1 = STACK_TV_BOT(-2);
2393 typval_T *tv2 = STACK_TV_BOT(-1);
2394
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002395 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 if (iptr->isn_type == ISN_ADDLIST)
2397 eval_addlist(tv1, tv2);
2398 else
2399 eval_addblob(tv1, tv2);
2400 clear_tv(tv2);
2401 --ectx.ec_stack.ga_len;
2402 }
2403 break;
2404
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002405 case ISN_LISTAPPEND:
2406 {
2407 typval_T *tv1 = STACK_TV_BOT(-2);
2408 typval_T *tv2 = STACK_TV_BOT(-1);
2409 list_T *l = tv1->vval.v_list;
2410
2411 // add an item to a list
2412 if (l == NULL)
2413 {
2414 SOURCING_LNUM = iptr->isn_lnum;
2415 emsg(_(e_cannot_add_to_null_list));
2416 goto on_error;
2417 }
2418 if (list_append_tv(l, tv2) == FAIL)
2419 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002420 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002421 --ectx.ec_stack.ga_len;
2422 }
2423 break;
2424
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002425 case ISN_BLOBAPPEND:
2426 {
2427 typval_T *tv1 = STACK_TV_BOT(-2);
2428 typval_T *tv2 = STACK_TV_BOT(-1);
2429 blob_T *b = tv1->vval.v_blob;
2430 int error = FALSE;
2431 varnumber_T n;
2432
2433 // add a number to a blob
2434 if (b == NULL)
2435 {
2436 SOURCING_LNUM = iptr->isn_lnum;
2437 emsg(_(e_cannot_add_to_null_blob));
2438 goto on_error;
2439 }
2440 n = tv_get_number_chk(tv2, &error);
2441 if (error)
2442 goto on_error;
2443 ga_append(&b->bv_ga, (int)n);
2444 --ectx.ec_stack.ga_len;
2445 }
2446 break;
2447
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 // Computation with two arguments of unknown type
2449 case ISN_OPANY:
2450 {
2451 typval_T *tv1 = STACK_TV_BOT(-2);
2452 typval_T *tv2 = STACK_TV_BOT(-1);
2453 varnumber_T n1, n2;
2454#ifdef FEAT_FLOAT
2455 float_T f1 = 0, f2 = 0;
2456#endif
2457 int error = FALSE;
2458
2459 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2460 {
2461 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2462 {
2463 eval_addlist(tv1, tv2);
2464 clear_tv(tv2);
2465 --ectx.ec_stack.ga_len;
2466 break;
2467 }
2468 else if (tv1->v_type == VAR_BLOB
2469 && tv2->v_type == VAR_BLOB)
2470 {
2471 eval_addblob(tv1, tv2);
2472 clear_tv(tv2);
2473 --ectx.ec_stack.ga_len;
2474 break;
2475 }
2476 }
2477#ifdef FEAT_FLOAT
2478 if (tv1->v_type == VAR_FLOAT)
2479 {
2480 f1 = tv1->vval.v_float;
2481 n1 = 0;
2482 }
2483 else
2484#endif
2485 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002486 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 n1 = tv_get_number_chk(tv1, &error);
2488 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002489 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490#ifdef FEAT_FLOAT
2491 if (tv2->v_type == VAR_FLOAT)
2492 f1 = n1;
2493#endif
2494 }
2495#ifdef FEAT_FLOAT
2496 if (tv2->v_type == VAR_FLOAT)
2497 {
2498 f2 = tv2->vval.v_float;
2499 n2 = 0;
2500 }
2501 else
2502#endif
2503 {
2504 n2 = tv_get_number_chk(tv2, &error);
2505 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002506 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507#ifdef FEAT_FLOAT
2508 if (tv1->v_type == VAR_FLOAT)
2509 f2 = n2;
2510#endif
2511 }
2512#ifdef FEAT_FLOAT
2513 // if there is a float on either side the result is a float
2514 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2515 {
2516 switch (iptr->isn_arg.op.op_type)
2517 {
2518 case EXPR_MULT: f1 = f1 * f2; break;
2519 case EXPR_DIV: f1 = f1 / f2; break;
2520 case EXPR_SUB: f1 = f1 - f2; break;
2521 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002522 default: SOURCING_LNUM = iptr->isn_lnum;
2523 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002524 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 }
2526 clear_tv(tv1);
2527 clear_tv(tv2);
2528 tv1->v_type = VAR_FLOAT;
2529 tv1->vval.v_float = f1;
2530 --ectx.ec_stack.ga_len;
2531 }
2532 else
2533#endif
2534 {
2535 switch (iptr->isn_arg.op.op_type)
2536 {
2537 case EXPR_MULT: n1 = n1 * n2; break;
2538 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2539 case EXPR_SUB: n1 = n1 - n2; break;
2540 case EXPR_ADD: n1 = n1 + n2; break;
2541 default: n1 = num_modulus(n1, n2); break;
2542 }
2543 clear_tv(tv1);
2544 clear_tv(tv2);
2545 tv1->v_type = VAR_NUMBER;
2546 tv1->vval.v_number = n1;
2547 --ectx.ec_stack.ga_len;
2548 }
2549 }
2550 break;
2551
2552 case ISN_CONCAT:
2553 {
2554 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2555 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2556 char_u *res;
2557
2558 res = concat_str(str1, str2);
2559 clear_tv(STACK_TV_BOT(-2));
2560 clear_tv(STACK_TV_BOT(-1));
2561 --ectx.ec_stack.ga_len;
2562 STACK_TV_BOT(-1)->vval.v_string = res;
2563 }
2564 break;
2565
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002566 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002567 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002568 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002569 int is_slice = iptr->isn_type == ISN_STRSLICE;
2570 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002571 char_u *res;
2572
2573 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002574 // string slice: string is at stack-3, first index at
2575 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002576 if (is_slice)
2577 {
2578 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002579 n1 = tv->vval.v_number;
2580 }
2581
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002582 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002583 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002584
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002585 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002586 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002587 if (is_slice)
2588 // Slice: Select the characters from the string
2589 res = string_slice(tv->vval.v_string, n1, n2);
2590 else
2591 // Index: The resulting variable is a string of a
2592 // single character. If the index is too big or
2593 // negative the result is empty.
2594 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002595 vim_free(tv->vval.v_string);
2596 tv->vval.v_string = res;
2597 }
2598 break;
2599
2600 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002601 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 {
Bram Moolenaared591872020-08-15 22:14:53 +02002603 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002605 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606
2607 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002608 // list slice: list is at stack-3, indexes at stack-2 and
2609 // stack-1
2610 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 list = tv->vval.v_list;
2612
2613 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002614 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002616
2617 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 {
Bram Moolenaared591872020-08-15 22:14:53 +02002619 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002620 n1 = tv->vval.v_number;
2621 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622 }
Bram Moolenaared591872020-08-15 22:14:53 +02002623
2624 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002625 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002626 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002627 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2628 == FAIL)
2629 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 }
2631 break;
2632
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002633 case ISN_ANYINDEX:
2634 case ISN_ANYSLICE:
2635 {
2636 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2637 typval_T *var1, *var2;
2638 int res;
2639
2640 // index: composite is at stack-2, index at stack-1
2641 // slice: composite is at stack-3, indexes at stack-2 and
2642 // stack-1
2643 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002644 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002645 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2646 goto on_error;
2647 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2648 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2649 res = eval_index_inner(tv, is_slice,
2650 var1, var2, NULL, -1, TRUE);
2651 clear_tv(var1);
2652 if (is_slice)
2653 clear_tv(var2);
2654 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2655 if (res == FAIL)
2656 goto on_error;
2657 }
2658 break;
2659
Bram Moolenaar9af78762020-06-16 11:34:42 +02002660 case ISN_SLICE:
2661 {
2662 list_T *list;
2663 int count = iptr->isn_arg.number;
2664
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002665 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002666 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002667 list = tv->vval.v_list;
2668
2669 // no error for short list, expect it to be checked earlier
2670 if (list != NULL && list->lv_len >= count)
2671 {
2672 list_T *newlist = list_slice(list,
2673 count, list->lv_len - 1);
2674
2675 if (newlist != NULL)
2676 {
2677 list_unref(list);
2678 tv->vval.v_list = newlist;
2679 ++newlist->lv_refcount;
2680 }
2681 }
2682 }
2683 break;
2684
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002685 case ISN_GETITEM:
2686 {
2687 listitem_T *li;
2688 int index = iptr->isn_arg.number;
2689
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002690 // Get list item: list is at stack-1, push item.
2691 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002692 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002693 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002694
2695 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2696 goto failed;
2697 ++ectx.ec_stack.ga_len;
2698 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2699 }
2700 break;
2701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 case ISN_MEMBER:
2703 {
2704 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002705 char_u *key;
2706 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002707 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002708
2709 // dict member: dict is at stack-2, key at stack-1
2710 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002711 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002712 dict = tv->vval.v_dict;
2713
2714 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002715 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002716 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01002717 if (key == NULL)
2718 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002719
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002720 if ((di = dict_find(dict, key, -1)) == NULL)
2721 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002722 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002723 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002724
2725 // If :silent! is used we will continue, make sure the
2726 // stack contents makes sense.
2727 clear_tv(tv);
2728 --ectx.ec_stack.ga_len;
2729 tv = STACK_TV_BOT(-1);
2730 clear_tv(tv);
2731 tv->v_type = VAR_NUMBER;
2732 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002733 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002734 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002735 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002736 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002737 // Clear the dict only after getting the item, to avoid
2738 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002739 tv = STACK_TV_BOT(-1);
2740 temp_tv = *tv;
2741 copy_tv(&di->di_tv, tv);
2742 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002743 }
2744 break;
2745
2746 // dict member with string key
2747 case ISN_STRINGMEMBER:
2748 {
2749 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002751 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752
2753 tv = STACK_TV_BOT(-1);
2754 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2755 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002756 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002758 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 }
2760 dict = tv->vval.v_dict;
2761
2762 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2763 == NULL)
2764 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002765 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002767 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002769 // Clear the dict after getting the item, to avoid that it
2770 // make the item invalid.
2771 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002773 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 }
2775 break;
2776
2777 case ISN_NEGATENR:
2778 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002779 if (tv->v_type != VAR_NUMBER
2780#ifdef FEAT_FLOAT
2781 && tv->v_type != VAR_FLOAT
2782#endif
2783 )
2784 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002785 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002786 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002787 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002788 }
2789#ifdef FEAT_FLOAT
2790 if (tv->v_type == VAR_FLOAT)
2791 tv->vval.v_float = -tv->vval.v_float;
2792 else
2793#endif
2794 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 break;
2796
2797 case ISN_CHECKNR:
2798 {
2799 int error = FALSE;
2800
2801 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002802 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002804 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 (void)tv_get_number_chk(tv, &error);
2806 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002807 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 }
2809 break;
2810
2811 case ISN_CHECKTYPE:
2812 {
2813 checktype_T *ct = &iptr->isn_arg.type;
2814
2815 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002816 SOURCING_LNUM = iptr->isn_lnum;
2817 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2818 goto on_error;
2819
2820 // number 0 is FALSE, number 1 is TRUE
2821 if (tv->v_type == VAR_NUMBER
2822 && ct->ct_type->tt_type == VAR_BOOL
2823 && (tv->vval.v_number == 0
2824 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002826 tv->v_type = VAR_BOOL;
2827 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002828 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 }
2830 }
2831 break;
2832
Bram Moolenaar9af78762020-06-16 11:34:42 +02002833 case ISN_CHECKLEN:
2834 {
2835 int min_len = iptr->isn_arg.checklen.cl_min_len;
2836 list_T *list = NULL;
2837
2838 tv = STACK_TV_BOT(-1);
2839 if (tv->v_type == VAR_LIST)
2840 list = tv->vval.v_list;
2841 if (list == NULL || list->lv_len < min_len
2842 || (list->lv_len > min_len
2843 && !iptr->isn_arg.checklen.cl_more_OK))
2844 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002845 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002846 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002847 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002848 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002849 }
2850 }
2851 break;
2852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002854 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 {
2856 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002857 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858
2859 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002860 if (iptr->isn_type == ISN_2BOOL)
2861 {
2862 n = tv2bool(tv);
2863 if (iptr->isn_arg.number) // invert
2864 n = !n;
2865 }
2866 else
2867 {
2868 SOURCING_LNUM = iptr->isn_lnum;
2869 n = tv_get_bool_chk(tv, &error);
2870 if (error)
2871 goto on_error;
2872 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 clear_tv(tv);
2874 tv->v_type = VAR_BOOL;
2875 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2876 }
2877 break;
2878
2879 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002880 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 {
2882 char_u *str;
2883
2884 tv = STACK_TV_BOT(iptr->isn_arg.number);
2885 if (tv->v_type != VAR_STRING)
2886 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002887 if (iptr->isn_type == ISN_2STRING_ANY)
2888 {
2889 switch (tv->v_type)
2890 {
2891 case VAR_SPECIAL:
2892 case VAR_BOOL:
2893 case VAR_NUMBER:
2894 case VAR_FLOAT:
2895 case VAR_BLOB: break;
2896 default: to_string_error(tv->v_type);
2897 goto on_error;
2898 }
2899 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 str = typval_tostring(tv);
2901 clear_tv(tv);
2902 tv->v_type = VAR_STRING;
2903 tv->vval.v_string = str;
2904 }
2905 }
2906 break;
2907
Bram Moolenaar08597872020-12-10 19:43:40 +01002908 case ISN_RANGE:
2909 {
2910 exarg_T ea;
2911 char *errormsg;
2912
2913 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2914 goto failed;
2915 ++ectx.ec_stack.ga_len;
2916 tv = STACK_TV_BOT(-1);
2917 ea.addr_type = ADDR_LINES;
2918 ea.cmd = iptr->isn_arg.string;
2919 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
2920 goto failed;
2921 if (ea.addr_count == 0)
2922 tv->vval.v_number = curwin->w_cursor.lnum;
2923 else
2924 tv->vval.v_number = ea.line2;
2925 }
2926 break;
2927
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002928 case ISN_PUT:
2929 {
2930 int regname = iptr->isn_arg.put.put_regname;
2931 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2932 char_u *expr = NULL;
2933 int dir = FORWARD;
2934
2935 if (regname == '=')
2936 {
2937 tv = STACK_TV_BOT(-1);
2938 if (tv->v_type == VAR_STRING)
2939 expr = tv->vval.v_string;
2940 else
2941 {
2942 expr = typval_tostring(tv); // allocates value
2943 clear_tv(tv);
2944 }
2945 --ectx.ec_stack.ga_len;
2946 }
Bram Moolenaar08597872020-12-10 19:43:40 +01002947 if (lnum < -2)
2948 {
2949 // line number was put on the stack by ISN_RANGE
2950 tv = STACK_TV_BOT(-1);
2951 curwin->w_cursor.lnum = tv->vval.v_number;
2952 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
2953 dir = BACKWARD;
2954 --ectx.ec_stack.ga_len;
2955 }
2956 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002957 // :put! above cursor
2958 dir = BACKWARD;
2959 else if (lnum >= 0)
2960 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2961 check_cursor();
2962 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2963 vim_free(expr);
2964 }
2965 break;
2966
Bram Moolenaar02194d22020-10-24 23:08:38 +02002967 case ISN_CMDMOD:
2968 save_cmdmod = cmdmod;
2969 restore_cmdmod = TRUE;
2970 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
2971 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002972 break;
2973
Bram Moolenaar02194d22020-10-24 23:08:38 +02002974 case ISN_CMDMOD_REV:
2975 // filter regprog is owned by the instruction, don't free it
2976 cmdmod.cmod_filter_regmatch.regprog = NULL;
2977 undo_cmdmod(&cmdmod);
2978 cmdmod = save_cmdmod;
2979 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002980 break;
2981
Bram Moolenaar792f7862020-11-23 08:31:18 +01002982 case ISN_UNPACK:
2983 {
2984 int count = iptr->isn_arg.unpack.unp_count;
2985 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
2986 list_T *l;
2987 listitem_T *li;
2988 int i;
2989
2990 // Check there is a valid list to unpack.
2991 tv = STACK_TV_BOT(-1);
2992 if (tv->v_type != VAR_LIST)
2993 {
2994 SOURCING_LNUM = iptr->isn_lnum;
2995 emsg(_(e_for_argument_must_be_sequence_of_lists));
2996 goto on_error;
2997 }
2998 l = tv->vval.v_list;
2999 if (l == NULL
3000 || l->lv_len < (semicolon ? count - 1 : count))
3001 {
3002 SOURCING_LNUM = iptr->isn_lnum;
3003 emsg(_(e_list_value_does_not_have_enough_items));
3004 goto on_error;
3005 }
3006 else if (!semicolon && l->lv_len > count)
3007 {
3008 SOURCING_LNUM = iptr->isn_lnum;
3009 emsg(_(e_list_value_has_more_items_than_targets));
3010 goto on_error;
3011 }
3012
3013 CHECK_LIST_MATERIALIZE(l);
3014 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3015 goto failed;
3016 ectx.ec_stack.ga_len += count - 1;
3017
3018 // Variable after semicolon gets a list with the remaining
3019 // items.
3020 if (semicolon)
3021 {
3022 list_T *rem_list =
3023 list_alloc_with_items(l->lv_len - count + 1);
3024
3025 if (rem_list == NULL)
3026 goto failed;
3027 tv = STACK_TV_BOT(-count);
3028 tv->vval.v_list = rem_list;
3029 ++rem_list->lv_refcount;
3030 tv->v_lock = 0;
3031 li = l->lv_first;
3032 for (i = 0; i < count - 1; ++i)
3033 li = li->li_next;
3034 for (i = 0; li != NULL; ++i)
3035 {
3036 list_set_item(rem_list, i, &li->li_tv);
3037 li = li->li_next;
3038 }
3039 --count;
3040 }
3041
3042 // Produce the values in reverse order, first item last.
3043 li = l->lv_first;
3044 for (i = 0; i < count; ++i)
3045 {
3046 tv = STACK_TV_BOT(-i - 1);
3047 copy_tv(&li->li_tv, tv);
3048 li = li->li_next;
3049 }
3050
3051 list_unref(l);
3052 }
3053 break;
3054
Bram Moolenaar389df252020-07-09 21:20:47 +02003055 case ISN_SHUFFLE:
3056 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003057 typval_T tmp_tv;
3058 int item = iptr->isn_arg.shuffle.shfl_item;
3059 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003060
3061 tmp_tv = *STACK_TV_BOT(-item);
3062 for ( ; up > 0 && item > 1; --up)
3063 {
3064 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3065 --item;
3066 }
3067 *STACK_TV_BOT(-item) = tmp_tv;
3068 }
3069 break;
3070
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 case ISN_DROP:
3072 --ectx.ec_stack.ga_len;
3073 clear_tv(STACK_TV_BOT(0));
3074 break;
3075 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003076 continue;
3077
Bram Moolenaard032f342020-07-18 18:13:02 +02003078func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003079 // Restore previous function. If the frame pointer is where we started
3080 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003081 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003082 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003083
Bram Moolenaard032f342020-07-18 18:13:02 +02003084 if (func_return(&ectx) == FAIL)
3085 // only fails when out of memory
3086 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003087 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003088
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003089on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003090 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003091 // If "emsg_silent" is set then ignore the error, unless it was set
3092 // when calling the function.
3093 if (did_emsg_cumul + did_emsg == did_emsg_before
3094 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003095 continue;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003096on_fatal_error:
3097 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003098 // If we are not inside a try-catch started here, abort execution.
3099 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003100 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 }
3102
3103done:
3104 // function finished, get result from the stack.
3105 tv = STACK_TV_BOT(-1);
3106 *rettv = *tv;
3107 tv->v_type = VAR_UNKNOWN;
3108 ret = OK;
3109
3110failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003111 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003112 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003113 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003114
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003115 // Deal with any remaining closures, they may be in use somewhere.
3116 if (ectx.ec_funcrefs.ga_len > 0)
3117 handle_closure_in_use(&ectx, FALSE);
3118
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003119 estack_pop();
3120 current_sctx = save_current_sctx;
3121
Bram Moolenaar352134b2020-10-17 22:04:08 +02003122 if (*msg_list != NULL && saved_msg_list != NULL)
3123 {
3124 msglist_T **plist = saved_msg_list;
3125
3126 // Append entries from the current msg_list (uncaught exceptions) to
3127 // the saved msg_list.
3128 while (*plist != NULL)
3129 plist = &(*plist)->next;
3130
3131 *plist = *msg_list;
3132 }
3133 msg_list = saved_msg_list;
3134
Bram Moolenaar02194d22020-10-24 23:08:38 +02003135 if (restore_cmdmod)
3136 {
3137 cmdmod.cmod_filter_regmatch.regprog = NULL;
3138 undo_cmdmod(&cmdmod);
3139 cmdmod = save_cmdmod;
3140 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003141 emsg_silent_def = save_emsg_silent_def;
3142 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003143
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003144failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003145 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3147 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003150 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003151
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003152 // Not sure if this is necessary.
3153 suppress_errthrow = save_suppress_errthrow;
3154
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003155 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003156 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003157 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003158 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 return ret;
3160}
3161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003163 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003164 * We don't really need this at runtime, but we do have tests that require it,
3165 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 */
3167 void
3168ex_disassemble(exarg_T *eap)
3169{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003170 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003171 char_u *fname;
3172 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 dfunc_T *dfunc;
3174 isn_T *instr;
3175 int current;
3176 int line_idx = 0;
3177 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003178 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003180 if (STRNCMP(arg, "<lambda>", 8) == 0)
3181 {
3182 arg += 8;
3183 (void)getdigits(&arg);
3184 fname = vim_strnsave(eap->arg, arg - eap->arg);
3185 }
3186 else
3187 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003188 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003189 if (fname == NULL)
3190 {
3191 semsg(_(e_invarg2), eap->arg);
3192 return;
3193 }
3194
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003195 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003196 if (ufunc == NULL)
3197 {
3198 char_u *p = untrans_function_name(fname);
3199
3200 if (p != NULL)
3201 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003202 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003203 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003204 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 if (ufunc == NULL)
3206 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003207 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 return;
3209 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003210 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003211 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3212 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003213 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003215 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 return;
3217 }
3218 if (ufunc->uf_name_exp != NULL)
3219 msg((char *)ufunc->uf_name_exp);
3220 else
3221 msg((char *)ufunc->uf_name);
3222
3223 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3224 instr = dfunc->df_instr;
3225 for (current = 0; current < dfunc->df_instr_count; ++current)
3226 {
3227 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003228 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229
3230 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3231 {
3232 if (current > prev_current)
3233 {
3234 msg_puts("\n\n");
3235 prev_current = current;
3236 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003237 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3238 if (line != NULL)
3239 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 }
3241
3242 switch (iptr->isn_type)
3243 {
3244 case ISN_EXEC:
3245 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3246 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003247 case ISN_EXECCONCAT:
3248 smsg("%4d EXECCONCAT %lld", current,
3249 (long long)iptr->isn_arg.number);
3250 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 case ISN_ECHO:
3252 {
3253 echo_T *echo = &iptr->isn_arg.echo;
3254
3255 smsg("%4d %s %d", current,
3256 echo->echo_with_white ? "ECHO" : "ECHON",
3257 echo->echo_count);
3258 }
3259 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003260 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003261 smsg("%4d EXECUTE %lld", current,
3262 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003263 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003264 case ISN_ECHOMSG:
3265 smsg("%4d ECHOMSG %lld", current,
3266 (long long)(iptr->isn_arg.number));
3267 break;
3268 case ISN_ECHOERR:
3269 smsg("%4d ECHOERR %lld", current,
3270 (long long)(iptr->isn_arg.number));
3271 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003273 case ISN_LOADOUTER:
3274 {
3275 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
3276
3277 if (iptr->isn_arg.number < 0)
3278 smsg("%4d LOAD%s arg[%lld]", current, add,
3279 (long long)(iptr->isn_arg.number
3280 + STACK_FRAME_SIZE));
3281 else
3282 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003283 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003284 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 break;
3286 case ISN_LOADV:
3287 smsg("%4d LOADV v:%s", current,
3288 get_vim_var_name(iptr->isn_arg.number));
3289 break;
3290 case ISN_LOADSCRIPT:
3291 {
3292 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003293 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3295 + iptr->isn_arg.script.script_idx;
3296
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003297 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3298 sv->sv_name,
3299 iptr->isn_arg.script.script_idx,
3300 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 }
3302 break;
3303 case ISN_LOADS:
3304 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003305 scriptitem_T *si = SCRIPT_ITEM(
3306 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307
3308 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003309 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 }
3311 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003312 case ISN_LOADAUTO:
3313 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3314 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 case ISN_LOADG:
3316 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3317 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003318 case ISN_LOADB:
3319 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3320 break;
3321 case ISN_LOADW:
3322 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3323 break;
3324 case ISN_LOADT:
3325 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3326 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003327 case ISN_LOADGDICT:
3328 smsg("%4d LOAD g:", current);
3329 break;
3330 case ISN_LOADBDICT:
3331 smsg("%4d LOAD b:", current);
3332 break;
3333 case ISN_LOADWDICT:
3334 smsg("%4d LOAD w:", current);
3335 break;
3336 case ISN_LOADTDICT:
3337 smsg("%4d LOAD t:", current);
3338 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 case ISN_LOADOPT:
3340 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3341 break;
3342 case ISN_LOADENV:
3343 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3344 break;
3345 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003346 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 break;
3348
3349 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003350 case ISN_STOREOUTER:
3351 {
3352 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
3353
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003354 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003355 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003356 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003357 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003358 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003359 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003360 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003362 case ISN_STOREV:
3363 smsg("%4d STOREV v:%s", current,
3364 get_vim_var_name(iptr->isn_arg.number));
3365 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003366 case ISN_STOREAUTO:
3367 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3368 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003370 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3371 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003372 case ISN_STOREB:
3373 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3374 break;
3375 case ISN_STOREW:
3376 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3377 break;
3378 case ISN_STORET:
3379 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3380 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003381 case ISN_STORES:
3382 {
3383 scriptitem_T *si = SCRIPT_ITEM(
3384 iptr->isn_arg.loadstore.ls_sid);
3385
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003386 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003387 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 break;
3390 case ISN_STORESCRIPT:
3391 {
3392 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003393 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3395 + iptr->isn_arg.script.script_idx;
3396
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003397 smsg("%4d STORESCRIPT %s-%d in %s", current,
3398 sv->sv_name,
3399 iptr->isn_arg.script.script_idx,
3400 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 }
3402 break;
3403 case ISN_STOREOPT:
3404 smsg("%4d STOREOPT &%s", current,
3405 iptr->isn_arg.storeopt.so_name);
3406 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003407 case ISN_STOREENV:
3408 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3409 break;
3410 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003411 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003412 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 case ISN_STORENR:
3414 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003415 iptr->isn_arg.storenr.stnr_val,
3416 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 break;
3418
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003419 case ISN_STORELIST:
3420 smsg("%4d STORELIST", current);
3421 break;
3422
3423 case ISN_STOREDICT:
3424 smsg("%4d STOREDICT", current);
3425 break;
3426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 // constants
3428 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003429 smsg("%4d PUSHNR %lld", current,
3430 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 break;
3432 case ISN_PUSHBOOL:
3433 case ISN_PUSHSPEC:
3434 smsg("%4d PUSH %s", current,
3435 get_var_special_name(iptr->isn_arg.number));
3436 break;
3437 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003438#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003440#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 break;
3442 case ISN_PUSHS:
3443 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3444 break;
3445 case ISN_PUSHBLOB:
3446 {
3447 char_u *r;
3448 char_u numbuf[NUMBUFLEN];
3449 char_u *tofree;
3450
3451 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003452 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 vim_free(tofree);
3454 }
3455 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003456 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003457 {
3458 char *name = (char *)iptr->isn_arg.string;
3459
3460 smsg("%4d PUSHFUNC \"%s\"", current,
3461 name == NULL ? "[none]" : name);
3462 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003463 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003464 case ISN_PUSHCHANNEL:
3465#ifdef FEAT_JOB_CHANNEL
3466 {
3467 channel_T *channel = iptr->isn_arg.channel;
3468
3469 smsg("%4d PUSHCHANNEL %d", current,
3470 channel == NULL ? 0 : channel->ch_id);
3471 }
3472#endif
3473 break;
3474 case ISN_PUSHJOB:
3475#ifdef FEAT_JOB_CHANNEL
3476 {
3477 typval_T tv;
3478 char_u *name;
3479
3480 tv.v_type = VAR_JOB;
3481 tv.vval.v_job = iptr->isn_arg.job;
3482 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003483 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003484 }
3485#endif
3486 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 case ISN_PUSHEXC:
3488 smsg("%4d PUSH v:exception", current);
3489 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003490 case ISN_UNLET:
3491 smsg("%4d UNLET%s %s", current,
3492 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3493 iptr->isn_arg.unlet.ul_name);
3494 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003495 case ISN_UNLETENV:
3496 smsg("%4d UNLETENV%s $%s", current,
3497 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3498 iptr->isn_arg.unlet.ul_name);
3499 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003500 case ISN_LOCKCONST:
3501 smsg("%4d LOCKCONST", current);
3502 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003504 smsg("%4d NEWLIST size %lld", current,
3505 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 break;
3507 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003508 smsg("%4d NEWDICT size %lld", current,
3509 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 break;
3511
3512 // function call
3513 case ISN_BCALL:
3514 {
3515 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3516
3517 smsg("%4d BCALL %s(argc %d)", current,
3518 internal_func_name(cbfunc->cbf_idx),
3519 cbfunc->cbf_argcount);
3520 }
3521 break;
3522 case ISN_DCALL:
3523 {
3524 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3525 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3526 + cdfunc->cdf_idx;
3527
3528 smsg("%4d DCALL %s(argc %d)", current,
3529 df->df_ufunc->uf_name_exp != NULL
3530 ? df->df_ufunc->uf_name_exp
3531 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3532 }
3533 break;
3534 case ISN_UCALL:
3535 {
3536 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3537
3538 smsg("%4d UCALL %s(argc %d)", current,
3539 cufunc->cuf_name, cufunc->cuf_argcount);
3540 }
3541 break;
3542 case ISN_PCALL:
3543 {
3544 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3545
3546 smsg("%4d PCALL%s (argc %d)", current,
3547 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3548 }
3549 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003550 case ISN_PCALL_END:
3551 smsg("%4d PCALL end", current);
3552 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 case ISN_RETURN:
3554 smsg("%4d RETURN", current);
3555 break;
3556 case ISN_FUNCREF:
3557 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003558 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003560 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003562 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 }
3564 break;
3565
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003566 case ISN_NEWFUNC:
3567 {
3568 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3569
3570 smsg("%4d NEWFUNC %s %s", current,
3571 newfunc->nf_lambda, newfunc->nf_global);
3572 }
3573 break;
3574
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003575 case ISN_DEF:
3576 {
3577 char_u *name = iptr->isn_arg.string;
3578
3579 smsg("%4d DEF %s", current,
3580 name == NULL ? (char_u *)"" : name);
3581 }
3582 break;
3583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 case ISN_JUMP:
3585 {
3586 char *when = "?";
3587
3588 switch (iptr->isn_arg.jump.jump_when)
3589 {
3590 case JUMP_ALWAYS:
3591 when = "JUMP";
3592 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 case JUMP_AND_KEEP_IF_TRUE:
3594 when = "JUMP_AND_KEEP_IF_TRUE";
3595 break;
3596 case JUMP_IF_FALSE:
3597 when = "JUMP_IF_FALSE";
3598 break;
3599 case JUMP_AND_KEEP_IF_FALSE:
3600 when = "JUMP_AND_KEEP_IF_FALSE";
3601 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003602 case JUMP_IF_COND_FALSE:
3603 when = "JUMP_IF_COND_FALSE";
3604 break;
3605 case JUMP_IF_COND_TRUE:
3606 when = "JUMP_IF_COND_TRUE";
3607 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003609 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 iptr->isn_arg.jump.jump_where);
3611 }
3612 break;
3613
3614 case ISN_FOR:
3615 {
3616 forloop_T *forloop = &iptr->isn_arg.forloop;
3617
3618 smsg("%4d FOR $%d -> %d", current,
3619 forloop->for_idx, forloop->for_end);
3620 }
3621 break;
3622
3623 case ISN_TRY:
3624 {
3625 try_T *try = &iptr->isn_arg.try;
3626
3627 smsg("%4d TRY catch -> %d, finally -> %d", current,
3628 try->try_catch, try->try_finally);
3629 }
3630 break;
3631 case ISN_CATCH:
3632 // TODO
3633 smsg("%4d CATCH", current);
3634 break;
3635 case ISN_ENDTRY:
3636 smsg("%4d ENDTRY", current);
3637 break;
3638 case ISN_THROW:
3639 smsg("%4d THROW", current);
3640 break;
3641
3642 // expression operations on number
3643 case ISN_OPNR:
3644 case ISN_OPFLOAT:
3645 case ISN_OPANY:
3646 {
3647 char *what;
3648 char *ins;
3649
3650 switch (iptr->isn_arg.op.op_type)
3651 {
3652 case EXPR_MULT: what = "*"; break;
3653 case EXPR_DIV: what = "/"; break;
3654 case EXPR_REM: what = "%"; break;
3655 case EXPR_SUB: what = "-"; break;
3656 case EXPR_ADD: what = "+"; break;
3657 default: what = "???"; break;
3658 }
3659 switch (iptr->isn_type)
3660 {
3661 case ISN_OPNR: ins = "OPNR"; break;
3662 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3663 case ISN_OPANY: ins = "OPANY"; break;
3664 default: ins = "???"; break;
3665 }
3666 smsg("%4d %s %s", current, ins, what);
3667 }
3668 break;
3669
3670 case ISN_COMPAREBOOL:
3671 case ISN_COMPARESPECIAL:
3672 case ISN_COMPARENR:
3673 case ISN_COMPAREFLOAT:
3674 case ISN_COMPARESTRING:
3675 case ISN_COMPAREBLOB:
3676 case ISN_COMPARELIST:
3677 case ISN_COMPAREDICT:
3678 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 case ISN_COMPAREANY:
3680 {
3681 char *p;
3682 char buf[10];
3683 char *type;
3684
3685 switch (iptr->isn_arg.op.op_type)
3686 {
3687 case EXPR_EQUAL: p = "=="; break;
3688 case EXPR_NEQUAL: p = "!="; break;
3689 case EXPR_GREATER: p = ">"; break;
3690 case EXPR_GEQUAL: p = ">="; break;
3691 case EXPR_SMALLER: p = "<"; break;
3692 case EXPR_SEQUAL: p = "<="; break;
3693 case EXPR_MATCH: p = "=~"; break;
3694 case EXPR_IS: p = "is"; break;
3695 case EXPR_ISNOT: p = "isnot"; break;
3696 case EXPR_NOMATCH: p = "!~"; break;
3697 default: p = "???"; break;
3698 }
3699 STRCPY(buf, p);
3700 if (iptr->isn_arg.op.op_ic == TRUE)
3701 strcat(buf, "?");
3702 switch(iptr->isn_type)
3703 {
3704 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3705 case ISN_COMPARESPECIAL:
3706 type = "COMPARESPECIAL"; break;
3707 case ISN_COMPARENR: type = "COMPARENR"; break;
3708 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3709 case ISN_COMPARESTRING:
3710 type = "COMPARESTRING"; break;
3711 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3712 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3713 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3714 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3716 default: type = "???"; break;
3717 }
3718
3719 smsg("%4d %s %s", current, type, buf);
3720 }
3721 break;
3722
3723 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3724 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3725
3726 // expression operations
3727 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003728 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003729 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003730 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003731 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003732 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003733 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003734 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3735 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003736 case ISN_SLICE: smsg("%4d SLICE %lld",
3737 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003738 case ISN_GETITEM: smsg("%4d ITEM %lld",
3739 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003740 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3741 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 iptr->isn_arg.string); break;
3743 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3744
3745 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003746 case ISN_CHECKTYPE:
3747 {
3748 char *tofree;
3749
3750 smsg("%4d CHECKTYPE %s stack[%d]", current,
3751 type_name(iptr->isn_arg.type.ct_type, &tofree),
3752 iptr->isn_arg.type.ct_off);
3753 vim_free(tofree);
3754 break;
3755 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003756 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3757 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3758 iptr->isn_arg.checklen.cl_min_len);
3759 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003760 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 case ISN_2BOOL: if (iptr->isn_arg.number)
3762 smsg("%4d INVERT (!val)", current);
3763 else
3764 smsg("%4d 2BOOL (!!val)", current);
3765 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003766 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3767 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003768 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003769 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3770 (long long)(iptr->isn_arg.number));
3771 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01003772 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
3773 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003774 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01003775 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
3776 smsg("%4d PUT %c above range",
3777 current, iptr->isn_arg.put.put_regname);
3778 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
3779 smsg("%4d PUT %c range",
3780 current, iptr->isn_arg.put.put_regname);
3781 else
3782 smsg("%4d PUT %c %ld", current,
3783 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003784 (long)iptr->isn_arg.put.put_lnum);
3785 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786
Bram Moolenaar02194d22020-10-24 23:08:38 +02003787 // TODO: summarize modifiers
3788 case ISN_CMDMOD:
3789 {
3790 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01003791 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02003792 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3793
3794 buf = alloc(len + 1);
3795 if (buf != NULL)
3796 {
3797 (void)produce_cmdmods(
3798 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3799 smsg("%4d CMDMOD %s", current, buf);
3800 vim_free(buf);
3801 }
3802 break;
3803 }
3804 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003805
Bram Moolenaar792f7862020-11-23 08:31:18 +01003806 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
3807 iptr->isn_arg.unpack.unp_count,
3808 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
3809 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02003810 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3811 iptr->isn_arg.shuffle.shfl_item,
3812 iptr->isn_arg.shuffle.shfl_up);
3813 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 case ISN_DROP: smsg("%4d DROP", current); break;
3815 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003816
3817 out_flush(); // output one line at a time
3818 ui_breakcheck();
3819 if (got_int)
3820 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822}
3823
3824/*
Bram Moolenaar13106602020-10-04 16:06:05 +02003825 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 * list, etc. Mostly like what JavaScript does, except that empty list and
3827 * empty dictionary are FALSE.
3828 */
3829 int
3830tv2bool(typval_T *tv)
3831{
3832 switch (tv->v_type)
3833 {
3834 case VAR_NUMBER:
3835 return tv->vval.v_number != 0;
3836 case VAR_FLOAT:
3837#ifdef FEAT_FLOAT
3838 return tv->vval.v_float != 0.0;
3839#else
3840 break;
3841#endif
3842 case VAR_PARTIAL:
3843 return tv->vval.v_partial != NULL;
3844 case VAR_FUNC:
3845 case VAR_STRING:
3846 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3847 case VAR_LIST:
3848 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3849 case VAR_DICT:
3850 return tv->vval.v_dict != NULL
3851 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3852 case VAR_BOOL:
3853 case VAR_SPECIAL:
3854 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3855 case VAR_JOB:
3856#ifdef FEAT_JOB_CHANNEL
3857 return tv->vval.v_job != NULL;
3858#else
3859 break;
3860#endif
3861 case VAR_CHANNEL:
3862#ifdef FEAT_JOB_CHANNEL
3863 return tv->vval.v_channel != NULL;
3864#else
3865 break;
3866#endif
3867 case VAR_BLOB:
3868 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3869 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003870 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 case VAR_VOID:
3872 break;
3873 }
3874 return FALSE;
3875}
3876
Bram Moolenaarea2d4072020-11-12 12:08:51 +01003877 void
3878emsg_using_string_as(typval_T *tv, int as_number)
3879{
3880 semsg(_(as_number ? e_using_string_as_number_str
3881 : e_using_string_as_bool_str),
3882 tv->vval.v_string == NULL
3883 ? (char_u *)"" : tv->vval.v_string);
3884}
3885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886/*
3887 * If "tv" is a string give an error and return FAIL.
3888 */
3889 int
3890check_not_string(typval_T *tv)
3891{
3892 if (tv->v_type == VAR_STRING)
3893 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01003894 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 clear_tv(tv);
3896 return FAIL;
3897 }
3898 return OK;
3899}
3900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901
3902#endif // FEAT_EVAL