blob: 9a76cc9c36c90bcaab2d9a3ec98e8a05ae5705e9 [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 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010057struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010058 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 Moolenaarcd45ed02020-12-22 17:35:54 +010072};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073
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 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100176 // don't use ufunc->uf_name, it may have been freed
177 emsg_funcname(e_func_deleted,
178 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 return FAIL;
180 }
181
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200182 if (ufunc->uf_va_name != NULL)
183 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200184 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200185 // Stack at time of call with 2 varargs:
186 // normal_arg
187 // optional_arg
188 // vararg_1
189 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190 // After creating the list:
191 // normal_arg
192 // optional_arg
193 // vararg-list
194 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200195 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196 // After creating the list
197 // normal_arg
198 // (space for optional_arg)
199 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200200 vararg_count = argcount - ufunc->uf_args.ga_len;
201 if (vararg_count < 0)
202 vararg_count = 0;
203 else
204 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200205 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200206 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200207
208 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200209 }
210
Bram Moolenaarfe270812020-04-11 22:31:27 +0200211 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200212 if (arg_to_add < 0)
213 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200214 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200215 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200216 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200217 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200218 return FAIL;
219 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200220
221 // Reserve space for:
222 // - missing arguments
223 // - stack frame
224 // - local variables
225 // - if needed: a counter for number of closures created in
226 // ectx->ec_funcrefs.
227 varcount = dfunc->df_varcount + dfunc->df_has_closure;
228 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
229 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230 return FAIL;
231
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100232 // If depth of calling is getting too high, don't execute the function.
233 if (funcdepth_increment() == FAIL)
234 return FAIL;
235
Bram Moolenaarfe270812020-04-11 22:31:27 +0200236 // Move the vararg-list to below the missing optional arguments.
237 if (vararg_count > 0 && arg_to_add > 0)
238 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100239
240 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200241 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200242 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200243 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244
245 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
247 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200248 STACK_TV_BOT(2)->vval.v_string = (void *)ectx->ec_outer_stack;
249 STACK_TV_BOT(3)->vval.v_number = ectx->ec_outer_frame;
250 STACK_TV_BOT(4)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200251 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252
253 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200254 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200256 if (dfunc->df_has_closure)
257 {
258 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
259
260 tv->v_type = VAR_NUMBER;
261 tv->vval.v_number = 0;
262 }
263 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100265 if (ufunc->uf_partial != NULL)
266 {
267 ectx->ec_outer_stack = ufunc->uf_partial->pt_ectx_stack;
268 ectx->ec_outer_frame = ufunc->uf_partial->pt_ectx_frame;
269 }
270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 // Set execution state to the start of the called function.
272 ectx->ec_dfunc_idx = cdf_idx;
273 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200274 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
275 if (entry != NULL)
276 {
277 // Set the script context to the script where the function was defined.
278 // TODO: save more than the SID?
279 entry->es_save_sid = current_sctx.sc_sid;
280 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
281 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100282
283 // Decide where to start execution, handles optional arguments.
284 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285
286 return OK;
287}
288
289// Get pointer to item in the stack.
290#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
291
292/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200293 * Used when returning from a function: Check if any closure is still
294 * referenced. If so then move the arguments and variables to a separate piece
295 * of stack to be used when the closure is called.
296 * When "free_arguments" is TRUE the arguments are to be freed.
297 * Returns FAIL when out of memory.
298 */
299 static int
300handle_closure_in_use(ectx_T *ectx, int free_arguments)
301{
302 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
303 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200304 int argcount;
305 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200306 int idx;
307 typval_T *tv;
308 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200309 garray_T *gap = &ectx->ec_funcrefs;
310 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200311
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200312 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200313 return OK; // function was freed
314 if (dfunc->df_has_closure == 0)
315 return OK; // no closures
316 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
317 closure_count = tv->vval.v_number;
318 if (closure_count == 0)
319 return OK; // no funcrefs created
320
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200321 argcount = ufunc_argcount(dfunc->df_ufunc);
322 top = ectx->ec_frame_idx - argcount;
323
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200324 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200325 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200326 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200327 partial_T *pt;
328 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200329
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200330 if (off < 0)
331 continue; // count is off or already done
332 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200333 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200334 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200335 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200336 int i;
337
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200338 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200339 // unreferenced on return.
340 for (i = 0; i < dfunc->df_varcount; ++i)
341 {
342 typval_T *stv = STACK_TV(ectx->ec_frame_idx
343 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200344 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200345 --refcount;
346 }
347 if (refcount > 1)
348 {
349 closure_in_use = TRUE;
350 break;
351 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200352 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200353 }
354
355 if (closure_in_use)
356 {
357 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
358 typval_T *stack;
359
360 // A closure is using the arguments and/or local variables.
361 // Move them to the called function.
362 if (funcstack == NULL)
363 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200364 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
365 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200366 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
367 funcstack->fs_ga.ga_data = stack;
368 if (stack == NULL)
369 {
370 vim_free(funcstack);
371 return FAIL;
372 }
373
374 // Move or copy the arguments.
375 for (idx = 0; idx < argcount; ++idx)
376 {
377 tv = STACK_TV(top + idx);
378 if (free_arguments)
379 {
380 *(stack + idx) = *tv;
381 tv->v_type = VAR_UNKNOWN;
382 }
383 else
384 copy_tv(tv, stack + idx);
385 }
386 // Move the local variables.
387 for (idx = 0; idx < dfunc->df_varcount; ++idx)
388 {
389 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200390
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200391 // A partial created for a local function, that is also used as a
392 // local variable, has a reference count for the variable, thus
393 // will never go down to zero. When all these refcounts are one
394 // then the funcstack is unused. We need to count how many we have
395 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200396 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
397 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200398 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200399
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200400 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200401 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
402 gap->ga_len - closure_count + i])
403 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200404 }
405
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200406 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200407 tv->v_type = VAR_UNKNOWN;
408 }
409
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200410 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200411 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200412 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
413 - closure_count + idx];
414 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200415 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200416 ++funcstack->fs_refcount;
417 pt->pt_funcstack = funcstack;
418 pt->pt_ectx_stack = &funcstack->fs_ga;
419 pt->pt_ectx_frame = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200420 }
421 }
422 }
423
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200424 for (idx = 0; idx < closure_count; ++idx)
425 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
426 - closure_count + idx]);
427 gap->ga_len -= closure_count;
428 if (gap->ga_len == 0)
429 ga_clear(gap);
430
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200431 return OK;
432}
433
434/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200435 * Called when a partial is freed or its reference count goes down to one. The
436 * funcstack may be the only reference to the partials in the local variables.
437 * Go over all of them, the funcref and can be freed if all partials
438 * referencing the funcstack have a reference count of one.
439 */
440 void
441funcstack_check_refcount(funcstack_T *funcstack)
442{
443 int i;
444 garray_T *gap = &funcstack->fs_ga;
445 int done = 0;
446
447 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
448 return;
449 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
450 {
451 typval_T *tv = ((typval_T *)gap->ga_data) + i;
452
453 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
454 && tv->vval.v_partial->pt_funcstack == funcstack
455 && tv->vval.v_partial->pt_refcount == 1)
456 ++done;
457 }
458 if (done == funcstack->fs_min_refcount)
459 {
460 typval_T *stack = gap->ga_data;
461
462 // All partials referencing the funcstack have a reference count of
463 // one, thus the funcstack is no longer of use.
464 for (i = 0; i < gap->ga_len; ++i)
465 clear_tv(stack + i);
466 vim_free(stack);
467 vim_free(funcstack);
468 }
469}
470
471/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100472 * Return from the current function.
473 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200474 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475func_return(ectx_T *ectx)
476{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100478 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200479 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
480 + ectx->ec_dfunc_idx;
481 int argcount = ufunc_argcount(dfunc->df_ufunc);
482 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200483 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484
485 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200486 entry = estack_pop();
487 if (entry != NULL)
488 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200490 if (handle_closure_in_use(ectx, TRUE) == FAIL)
491 return FAIL;
492
493 // Clear the arguments.
494 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
495 clear_tv(STACK_TV(idx));
496
497 // Clear local variables and temp values, but not the return value.
498 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100499 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100501
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100502 // The return value should be on top of the stack. However, when aborting
503 // it may not be there and ec_frame_idx is the top of the stack.
504 ret_idx = ectx->ec_stack.ga_len - 1;
505 if (ret_idx == ectx->ec_frame_idx + 4)
506 ret_idx = 0;
507
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100508 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200509 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
510 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200511 ectx->ec_outer_stack =
512 (void *)STACK_TV(ectx->ec_frame_idx + 2)->vval.v_string;
513 ectx->ec_outer_frame = STACK_TV(ectx->ec_frame_idx + 3)->vval.v_number;
514 // restoring ec_frame_idx must be last
515 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 4)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
517 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100518
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100519 if (ret_idx > 0)
520 {
521 // Reset the stack to the position before the call, with a spot for the
522 // return value, moved there from above the frame.
523 ectx->ec_stack.ga_len = top + 1;
524 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
525 }
526 else
527 // Reset the stack to the position before the call.
528 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200529
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100530 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200531 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532}
533
534#undef STACK_TV
535
536/*
537 * Prepare arguments and rettv for calling a builtin or user function.
538 */
539 static int
540call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
541{
542 int idx;
543 typval_T *tv;
544
545 // Move arguments from bottom of the stack to argvars[] and add terminator.
546 for (idx = 0; idx < argcount; ++idx)
547 argvars[idx] = *STACK_TV_BOT(idx - argcount);
548 argvars[argcount].v_type = VAR_UNKNOWN;
549
550 // Result replaces the arguments on the stack.
551 if (argcount > 0)
552 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200553 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554 return FAIL;
555 else
556 ++ectx->ec_stack.ga_len;
557
558 // Default return value is zero.
559 tv = STACK_TV_BOT(-1);
560 tv->v_type = VAR_NUMBER;
561 tv->vval.v_number = 0;
562
563 return OK;
564}
565
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200566// Ugly global to avoid passing the execution context around through many
567// layers.
568static ectx_T *current_ectx = NULL;
569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100570/*
571 * Call a builtin function by index.
572 */
573 static int
574call_bfunc(int func_idx, int argcount, ectx_T *ectx)
575{
576 typval_T argvars[MAX_FUNC_ARGS];
577 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100578 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200579 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580
581 if (call_prepare(argcount, argvars, ectx) == FAIL)
582 return FAIL;
583
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200584 // Call the builtin function. Set "current_ectx" so that when it
585 // recursively invokes call_def_function() a closure context can be set.
586 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200588 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589
590 // Clear the arguments.
591 for (idx = 0; idx < argcount; ++idx)
592 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200593
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100594 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200595 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596 return OK;
597}
598
599/*
600 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100601 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100602 */
603 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100604call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605{
606 typval_T argvars[MAX_FUNC_ARGS];
607 funcexe_T funcexe;
608 int error;
609 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100610 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100611
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200612 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200613 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
614 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200615 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100616 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100617 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100618 if (error != FCERR_UNKNOWN)
619 {
620 if (error == FCERR_TOOMANY)
621 semsg(_(e_toomanyarg), ufunc->uf_name);
622 else
623 semsg(_(e_toofewarg), ufunc->uf_name);
624 return FAIL;
625 }
626
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100627 // The function has been compiled, can call it quickly. For a function
628 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100629 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100630 if (iptr != NULL)
631 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100632 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100633 iptr->isn_type = ISN_DCALL;
634 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
635 iptr->isn_arg.dfunc.cdf_argcount = argcount;
636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100638 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100639
640 if (call_prepare(argcount, argvars, ectx) == FAIL)
641 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200642 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 funcexe.evaluate = TRUE;
644
645 // Call the user function. Result goes in last position on the stack.
646 // TODO: add selfdict if there is one
647 error = call_user_func_check(ufunc, argcount, argvars,
648 STACK_TV_BOT(-1), &funcexe, NULL);
649
650 // Clear the arguments.
651 for (idx = 0; idx < argcount; ++idx)
652 clear_tv(&argvars[idx]);
653
654 if (error != FCERR_NONE)
655 {
656 user_func_error(error, ufunc->uf_name);
657 return FAIL;
658 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100659 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200660 // Error other than from calling the function itself.
661 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100662 return OK;
663}
664
665/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200666 * Return TRUE if an error was given or CTRL-C was pressed.
667 */
668 static int
669vim9_aborting(int prev_called_emsg)
670{
671 return called_emsg > prev_called_emsg || got_int || did_throw;
672}
673
674/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675 * Execute a function by "name".
676 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100677 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100678 * Returns FAIL if not found without an error message.
679 */
680 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100681call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682{
683 ufunc_T *ufunc;
684
685 if (builtin_function(name, -1))
686 {
687 int func_idx = find_internal_func(name);
688
689 if (func_idx < 0)
690 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200691 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692 return FAIL;
693 return call_bfunc(func_idx, argcount, ectx);
694 }
695
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200696 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200697
698 if (ufunc == NULL)
699 {
700 int called_emsg_before = called_emsg;
701
702 if (script_autoload(name, TRUE))
703 // loaded a package, search for the function again
704 ufunc = find_func(name, FALSE, NULL);
705 if (vim9_aborting(called_emsg_before))
706 return FAIL; // bail out if loading the script caused an error
707 }
708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100710 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711
712 return FAIL;
713}
714
715 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200716call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200718 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200719 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200721 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722
723 if (tv->v_type == VAR_PARTIAL)
724 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200725 partial_T *pt = tv->vval.v_partial;
726 int i;
727
728 if (pt->pt_argc > 0)
729 {
730 // Make space for arguments from the partial, shift the "argcount"
731 // arguments up.
732 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
733 return FAIL;
734 for (i = 1; i <= argcount; ++i)
735 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
736 ectx->ec_stack.ga_len += pt->pt_argc;
737 argcount += pt->pt_argc;
738
739 // copy the arguments from the partial onto the stack
740 for (i = 0; i < pt->pt_argc; ++i)
741 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
742 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743
744 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200745 {
746 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
747
748 // closure may need the function context where it was defined
749 ectx->ec_outer_stack = pt->pt_ectx_stack;
750 ectx->ec_outer_frame = pt->pt_ectx_frame;
751
752 return ret;
753 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100754 name = pt->pt_name;
755 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200756 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200758 if (name != NULL)
759 {
760 char_u fname_buf[FLEN_FIXED + 1];
761 char_u *tofree = NULL;
762 int error = FCERR_NONE;
763 char_u *fname;
764
765 // May need to translate <SNR>123_ to K_SNR.
766 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
767 if (error != FCERR_NONE)
768 res = FAIL;
769 else
770 res = call_by_name(fname, argcount, ectx, NULL);
771 vim_free(tofree);
772 }
773
774 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 {
776 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200777 semsg(_(e_unknownfunc),
778 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 return FAIL;
780 }
781 return OK;
782}
783
784/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200785 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
786 * TRUE.
787 */
788 static int
789error_if_locked(int lock, char *error)
790{
791 if (lock & (VAR_LOCKED | VAR_FIXED))
792 {
793 emsg(_(error));
794 return TRUE;
795 }
796 return FALSE;
797}
798
799/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100800 * Store "tv" in variable "name".
801 * This is for s: and g: variables.
802 */
803 static void
804store_var(char_u *name, typval_T *tv)
805{
806 funccal_entry_T entry;
807
808 save_funccal(&entry);
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100809 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100810 restore_funccal();
811}
812
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100813/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100814 * Convert "tv" to a string.
815 * Return FAIL if not allowed.
816 */
817 static int
818do_2string(typval_T *tv, int is_2string_any)
819{
820 if (tv->v_type != VAR_STRING)
821 {
822 char_u *str;
823
824 if (is_2string_any)
825 {
826 switch (tv->v_type)
827 {
828 case VAR_SPECIAL:
829 case VAR_BOOL:
830 case VAR_NUMBER:
831 case VAR_FLOAT:
832 case VAR_BLOB: break;
833 default: to_string_error(tv->v_type);
834 return FAIL;
835 }
836 }
837 str = typval_tostring(tv);
838 clear_tv(tv);
839 tv->v_type = VAR_STRING;
840 tv->vval.v_string = str;
841 }
842 return OK;
843}
844
845/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100846 * When the value of "sv" is a null list of dict, allocate it.
847 */
848 static void
849allocate_if_null(typval_T *tv)
850{
851 switch (tv->v_type)
852 {
853 case VAR_LIST:
854 if (tv->vval.v_list == NULL)
855 rettv_list_alloc(tv);
856 break;
857 case VAR_DICT:
858 if (tv->vval.v_dict == NULL)
859 rettv_dict_alloc(tv);
860 break;
861 default:
862 break;
863 }
864}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200865
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100866 static svar_T *
867get_script_svar(scriptref_T *sref, ectx_T *ectx)
868{
869 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
870 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
871 + ectx->ec_dfunc_idx;
872 svar_T *sv;
873
874 if (sref->sref_seq != si->sn_script_seq)
875 {
876 // The script was reloaded after the function was
877 // compiled, the script_idx may not be valid.
878 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
879 dfunc->df_ufunc->uf_name_exp);
880 return NULL;
881 }
882 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
883 if (!equal_type(sv->sv_type, sref->sref_type))
884 {
885 emsg(_(e_script_variable_type_changed));
886 return NULL;
887 }
888 return sv;
889}
890
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100891/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 * Execute a function by "name".
893 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100894 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895 */
896 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100897call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898{
Bram Moolenaared677f52020-08-12 16:38:10 +0200899 int called_emsg_before = called_emsg;
900 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901
Bram Moolenaared677f52020-08-12 16:38:10 +0200902 res = call_by_name(name, argcount, ectx, iptr);
903 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200905 dictitem_T *v;
906
907 v = find_var(name, NULL, FALSE);
908 if (v == NULL)
909 {
910 semsg(_(e_unknownfunc), name);
911 return FAIL;
912 }
913 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
914 {
915 semsg(_(e_unknownfunc), name);
916 return FAIL;
917 }
918 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100919 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200920 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100921}
922
923/*
Bram Moolenaarf112f302020-12-20 17:47:52 +0100924 * When a function reference is used, fill a partial with the information
925 * needed, especially when it is used as a closure.
926 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100927 int
Bram Moolenaarf112f302020-12-20 17:47:52 +0100928fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
929{
930 pt->pt_func = ufunc;
931 pt->pt_refcount = 1;
932
933 if (pt->pt_func->uf_flags & FC_CLOSURE)
934 {
935 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
936 + ectx->ec_dfunc_idx;
937
938 // The closure needs to find arguments and local
939 // variables in the current stack.
940 pt->pt_ectx_stack = &ectx->ec_stack;
941 pt->pt_ectx_frame = ectx->ec_frame_idx;
942
943 // If this function returns and the closure is still
944 // being used, we need to make a copy of the context
945 // (arguments and local variables). Store a reference
946 // to the partial so we can handle that.
947 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
948 {
949 vim_free(pt);
950 return FAIL;
951 }
952 // Extra variable keeps the count of closures created
953 // in the current function call.
954 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
955 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
956
957 ((partial_T **)ectx->ec_funcrefs.ga_data)
958 [ectx->ec_funcrefs.ga_len] = pt;
959 ++pt->pt_refcount;
960 ++ectx->ec_funcrefs.ga_len;
961 }
962 ++pt->pt_func->uf_refcount;
963 return OK;
964}
965
966/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967 * Call a "def" function from old Vim script.
968 * Return OK or FAIL.
969 */
970 int
971call_def_function(
972 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200973 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100974 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200975 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976 typval_T *rettv) // return value
977{
978 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200979 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200980 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 typval_T *tv;
982 int idx;
983 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100984 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200985 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200986 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100987 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200988 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +0200989 msglist_T **saved_msg_list = NULL;
990 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +0200991 cmdmod_T save_cmdmod;
992 int restore_cmdmod = FALSE;
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100993 int save_emsg_silent_def = emsg_silent_def;
994 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100995 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100996 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997
998// Get pointer to item in the stack.
999#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1000
1001// Get pointer to item at the bottom of the stack, -1 is the bottom.
1002#undef STACK_TV_BOT
1003#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1004
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001005// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001006#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 +01001007
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001008// Like STACK_TV_VAR but use the outer scope
1009#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
1010
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001011 if (ufunc->uf_def_status == UF_NOT_COMPILED
1012 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02001013 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001014 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001015 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001016 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001017 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001018 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001019 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001020
Bram Moolenaar09689a02020-05-09 22:50:08 +02001021 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001022 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001023 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1024 + ufunc->uf_dfunc_idx;
1025 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001026 {
1027 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001028 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001029 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001030 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001032 // If depth of calling is getting too high, don't execute the function.
1033 orig_funcdepth = funcdepth_get();
1034 if (funcdepth_increment() == FAIL)
1035 return FAIL;
1036
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001037 CLEAR_FIELD(ectx);
1038 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1039 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1040 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001041 {
1042 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001043 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001044 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001045 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001046 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001048 // Put arguments on the stack, but no more than what the function expects.
1049 // A lambda can be called with more arguments than it uses.
1050 for (idx = 0; idx < argc
1051 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1052 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001054 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001055 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
1056 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001057 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058 copy_tv(&argv[idx], STACK_TV_BOT(0));
1059 ++ectx.ec_stack.ga_len;
1060 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001061
1062 // Turn varargs into a list. Empty list if no args.
1063 if (ufunc->uf_va_name != NULL)
1064 {
1065 int vararg_count = argc - ufunc->uf_args.ga_len;
1066
1067 if (vararg_count < 0)
1068 vararg_count = 0;
1069 else
1070 argc -= vararg_count;
1071 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001072 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001073
1074 // Check the type of the list items.
1075 tv = STACK_TV_BOT(-1);
1076 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001077 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001078 && ufunc->uf_va_type->tt_member != &t_any
1079 && tv->vval.v_list != NULL)
1080 {
1081 type_T *expected = ufunc->uf_va_type->tt_member;
1082 listitem_T *li = tv->vval.v_list->lv_first;
1083
1084 for (idx = 0; idx < vararg_count; ++idx)
1085 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001086 if (check_typval_type(expected, &li->li_tv,
1087 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001088 goto failed_early;
1089 li = li->li_next;
1090 }
1091 }
1092
Bram Moolenaar23e03252020-04-12 22:22:31 +02001093 if (defcount > 0)
1094 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001095 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001096 --ectx.ec_stack.ga_len;
1097 }
1098
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001099 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001100 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001101 if (defcount > 0)
1102 for (idx = 0; idx < defcount; ++idx)
1103 {
1104 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1105 ++ectx.ec_stack.ga_len;
1106 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001107 if (ufunc->uf_va_name != NULL)
1108 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109
1110 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001111 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1112 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001114 if (partial != NULL)
1115 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001116 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
1117 {
1118 // TODO: is this always the right way?
1119 ectx.ec_outer_stack = &current_ectx->ec_stack;
1120 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
1121 }
1122 else
1123 {
1124 ectx.ec_outer_stack = partial->pt_ectx_stack;
1125 ectx.ec_outer_frame = partial->pt_ectx_frame;
1126 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001127 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001128 else if (ufunc->uf_partial != NULL)
1129 {
1130 ectx.ec_outer_stack = ufunc->uf_partial->pt_ectx_stack;
1131 ectx.ec_outer_frame = ufunc->uf_partial->pt_ectx_frame;
1132 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001133
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134 // dummy frame entries
1135 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1136 {
1137 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1138 ++ectx.ec_stack.ga_len;
1139 }
1140
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001141 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001142 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001143 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1144 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001146 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001147 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001148 ectx.ec_stack.ga_len += dfunc->df_varcount;
1149 if (dfunc->df_has_closure)
1150 {
1151 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1152 STACK_TV_VAR(idx)->vval.v_number = 0;
1153 ++ectx.ec_stack.ga_len;
1154 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001155
1156 ectx.ec_instr = dfunc->df_instr;
1157 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001158
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001159 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001160 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001161 estack_push_ufunc(ufunc, 1);
1162 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001163 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1164
Bram Moolenaar352134b2020-10-17 22:04:08 +02001165 // Use a specific location for storing error messages to be converted to an
1166 // exception.
1167 saved_msg_list = msg_list;
1168 msg_list = &private_msg_list;
1169
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001170 // Do turn errors into exceptions.
1171 suppress_errthrow = FALSE;
1172
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001173 // When ":silent!" was used before calling then we still abort the
1174 // function. If ":silent!" is used in the function then we don't.
1175 emsg_silent_def = emsg_silent;
1176 did_emsg_def = 0;
1177
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001178 // Decide where to start execution, handles optional arguments.
1179 init_instr_idx(ufunc, argc, &ectx);
1180
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181 for (;;)
1182 {
1183 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001184
Bram Moolenaar270d0382020-05-15 21:42:53 +02001185 if (++breakcheck_count >= 100)
1186 {
1187 line_breakcheck();
1188 breakcheck_count = 0;
1189 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001190 if (got_int)
1191 {
1192 // Turn CTRL-C into an exception.
1193 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001194 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001195 goto failed;
1196 did_throw = TRUE;
1197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198
Bram Moolenaara26b9702020-04-18 19:53:28 +02001199 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1200 {
1201 // Turn an error message into an exception.
1202 did_emsg = FALSE;
1203 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1204 goto failed;
1205 did_throw = TRUE;
1206 *msg_list = NULL;
1207 }
1208
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209 if (did_throw && !ectx.ec_in_catch)
1210 {
1211 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001212 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213
1214 // An exception jumps to the first catch, finally, or returns from
1215 // the current function.
1216 if (trystack->ga_len > 0)
1217 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001218 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 {
1220 // jump to ":catch" or ":finally"
1221 ectx.ec_in_catch = TRUE;
1222 ectx.ec_iidx = trycmd->tcd_catch_idx;
1223 }
1224 else
1225 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001226 // Not inside try or need to return from current functions.
1227 // Push a dummy return value.
1228 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1229 goto failed;
1230 tv = STACK_TV_BOT(0);
1231 tv->v_type = VAR_NUMBER;
1232 tv->vval.v_number = 0;
1233 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001234 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001236 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001237 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001238 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1239 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 goto done;
1241 }
1242
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001243 if (func_return(&ectx) == FAIL)
1244 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 }
1246 continue;
1247 }
1248
1249 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1250 switch (iptr->isn_type)
1251 {
1252 // execute Ex command line
1253 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001254 {
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001255 SOURCING_LNUM = iptr->isn_lnum;
1256 do_cmdline_cmd(iptr->isn_arg.string);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001257 if (did_emsg)
1258 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001259 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 break;
1261
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001262 // execute Ex command from pieces on the stack
1263 case ISN_EXECCONCAT:
1264 {
1265 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001266 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001267 int pass;
1268 int i;
1269 char_u *cmd = NULL;
1270 char_u *str;
1271
1272 for (pass = 1; pass <= 2; ++pass)
1273 {
1274 for (i = 0; i < count; ++i)
1275 {
1276 tv = STACK_TV_BOT(i - count);
1277 str = tv->vval.v_string;
1278 if (str != NULL && *str != NUL)
1279 {
1280 if (pass == 2)
1281 STRCPY(cmd + len, str);
1282 len += STRLEN(str);
1283 }
1284 if (pass == 2)
1285 clear_tv(tv);
1286 }
1287 if (pass == 1)
1288 {
1289 cmd = alloc(len + 1);
1290 if (cmd == NULL)
1291 goto failed;
1292 len = 0;
1293 }
1294 }
1295
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001296 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001297 do_cmdline_cmd(cmd);
1298 vim_free(cmd);
1299 }
1300 break;
1301
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001302 // execute :echo {string} ...
1303 case ISN_ECHO:
1304 {
1305 int count = iptr->isn_arg.echo.echo_count;
1306 int atstart = TRUE;
1307 int needclr = TRUE;
1308
1309 for (idx = 0; idx < count; ++idx)
1310 {
1311 tv = STACK_TV_BOT(idx - count);
1312 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1313 &atstart, &needclr);
1314 clear_tv(tv);
1315 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001316 if (needclr)
1317 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 ectx.ec_stack.ga_len -= count;
1319 }
1320 break;
1321
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001322 // :execute {string} ...
1323 // :echomsg {string} ...
1324 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001325 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001326 case ISN_ECHOMSG:
1327 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001328 {
1329 int count = iptr->isn_arg.number;
1330 garray_T ga;
1331 char_u buf[NUMBUFLEN];
1332 char_u *p;
1333 int len;
1334 int failed = FALSE;
1335
1336 ga_init2(&ga, 1, 80);
1337 for (idx = 0; idx < count; ++idx)
1338 {
1339 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001340 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001341 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001342 if (tv->v_type == VAR_CHANNEL
1343 || tv->v_type == VAR_JOB)
1344 {
1345 SOURCING_LNUM = iptr->isn_lnum;
1346 emsg(_(e_inval_string));
1347 break;
1348 }
1349 else
1350 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001351 }
1352 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001353 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001354
1355 len = (int)STRLEN(p);
1356 if (ga_grow(&ga, len + 2) == FAIL)
1357 failed = TRUE;
1358 else
1359 {
1360 if (ga.ga_len > 0)
1361 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1362 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1363 ga.ga_len += len;
1364 }
1365 clear_tv(tv);
1366 }
1367 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001368 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001369 {
1370 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001371 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001372 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001373
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001374 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001375 {
1376 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001377 {
1378 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001379 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001380 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001381 {
1382 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001383 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001384 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001385 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001386 else
1387 {
1388 msg_sb_eol();
1389 if (iptr->isn_type == ISN_ECHOMSG)
1390 {
1391 msg_attr(ga.ga_data, echo_attr);
1392 out_flush();
1393 }
1394 else
1395 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001396 SOURCING_LNUM = iptr->isn_lnum;
1397 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001398 }
1399 }
1400 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001401 ga_clear(&ga);
1402 }
1403 break;
1404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405 // load local variable or argument
1406 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001407 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408 goto failed;
1409 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1410 ++ectx.ec_stack.ga_len;
1411 break;
1412
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001413 // load variable or argument from outer scope
1414 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001415 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001416 goto failed;
1417 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1418 STACK_TV_BOT(0));
1419 ++ectx.ec_stack.ga_len;
1420 break;
1421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422 // load v: variable
1423 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001424 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 goto failed;
1426 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1427 ++ectx.ec_stack.ga_len;
1428 break;
1429
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001430 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 case ISN_LOADSCRIPT:
1432 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001433 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 svar_T *sv;
1435
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001436 sv = get_script_svar(sref, &ectx);
1437 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001438 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001439 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001440 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 goto failed;
1442 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1443 ++ectx.ec_stack.ga_len;
1444 }
1445 break;
1446
1447 // load s: variable in old script
1448 case ISN_LOADS:
1449 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001450 hashtab_T *ht = &SCRIPT_VARS(
1451 iptr->isn_arg.loadstore.ls_sid);
1452 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001454
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 if (di == NULL)
1456 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001457 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001458 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001459 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460 }
1461 else
1462 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001463 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 goto failed;
1465 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1466 ++ectx.ec_stack.ga_len;
1467 }
1468 }
1469 break;
1470
Bram Moolenaard3aac292020-04-19 14:32:17 +02001471 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001473 case ISN_LOADB:
1474 case ISN_LOADW:
1475 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001477 dictitem_T *di = NULL;
1478 hashtab_T *ht = NULL;
1479 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001480
Bram Moolenaard3aac292020-04-19 14:32:17 +02001481 switch (iptr->isn_type)
1482 {
1483 case ISN_LOADG:
1484 ht = get_globvar_ht();
1485 namespace = 'g';
1486 break;
1487 case ISN_LOADB:
1488 ht = &curbuf->b_vars->dv_hashtab;
1489 namespace = 'b';
1490 break;
1491 case ISN_LOADW:
1492 ht = &curwin->w_vars->dv_hashtab;
1493 namespace = 'w';
1494 break;
1495 case ISN_LOADT:
1496 ht = &curtab->tp_vars->dv_hashtab;
1497 namespace = 't';
1498 break;
1499 default: // Cannot reach here
1500 goto failed;
1501 }
1502 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001503
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 if (di == NULL)
1505 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001506 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001507 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001508 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001509 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 }
1511 else
1512 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001513 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514 goto failed;
1515 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1516 ++ectx.ec_stack.ga_len;
1517 }
1518 }
1519 break;
1520
Bram Moolenaar03290b82020-12-19 16:30:44 +01001521 // load autoload variable
1522 case ISN_LOADAUTO:
1523 {
1524 char_u *name = iptr->isn_arg.string;
1525
1526 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1527 goto failed;
1528 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001529 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001530 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1531 goto on_error;
1532 ++ectx.ec_stack.ga_len;
1533 }
1534 break;
1535
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001536 // load g:/b:/w:/t: namespace
1537 case ISN_LOADGDICT:
1538 case ISN_LOADBDICT:
1539 case ISN_LOADWDICT:
1540 case ISN_LOADTDICT:
1541 {
1542 dict_T *d = NULL;
1543
1544 switch (iptr->isn_type)
1545 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001546 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1547 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1548 case ISN_LOADWDICT: d = curwin->w_vars; break;
1549 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001550 default: // Cannot reach here
1551 goto failed;
1552 }
1553 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1554 goto failed;
1555 tv = STACK_TV_BOT(0);
1556 tv->v_type = VAR_DICT;
1557 tv->v_lock = 0;
1558 tv->vval.v_dict = d;
1559 ++ectx.ec_stack.ga_len;
1560 }
1561 break;
1562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 // load &option
1564 case ISN_LOADOPT:
1565 {
1566 typval_T optval;
1567 char_u *name = iptr->isn_arg.string;
1568
Bram Moolenaara8c17702020-04-01 21:17:24 +02001569 // This is not expected to fail, name is checked during
1570 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001571 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001573 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001574 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 *STACK_TV_BOT(0) = optval;
1576 ++ectx.ec_stack.ga_len;
1577 }
1578 break;
1579
1580 // load $ENV
1581 case ISN_LOADENV:
1582 {
1583 typval_T optval;
1584 char_u *name = iptr->isn_arg.string;
1585
Bram Moolenaar270d0382020-05-15 21:42:53 +02001586 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001588 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001589 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 *STACK_TV_BOT(0) = optval;
1591 ++ectx.ec_stack.ga_len;
1592 }
1593 break;
1594
1595 // load @register
1596 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001597 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 goto failed;
1599 tv = STACK_TV_BOT(0);
1600 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001601 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001602 // This may result in NULL, which should be equivalent to an
1603 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 tv->vval.v_string = get_reg_contents(
1605 iptr->isn_arg.number, GREG_EXPR_SRC);
1606 ++ectx.ec_stack.ga_len;
1607 break;
1608
1609 // store local variable
1610 case ISN_STORE:
1611 --ectx.ec_stack.ga_len;
1612 tv = STACK_TV_VAR(iptr->isn_arg.number);
1613 clear_tv(tv);
1614 *tv = *STACK_TV_BOT(0);
1615 break;
1616
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001617 // store variable or argument in outer scope
1618 case ISN_STOREOUTER:
1619 --ectx.ec_stack.ga_len;
1620 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1621 clear_tv(tv);
1622 *tv = *STACK_TV_BOT(0);
1623 break;
1624
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001625 // store s: variable in old script
1626 case ISN_STORES:
1627 {
1628 hashtab_T *ht = &SCRIPT_VARS(
1629 iptr->isn_arg.loadstore.ls_sid);
1630 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001631 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001632
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001633 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001634 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001635 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001636 else
1637 {
1638 clear_tv(&di->di_tv);
1639 di->di_tv = *STACK_TV_BOT(0);
1640 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001641 }
1642 break;
1643
1644 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 case ISN_STORESCRIPT:
1646 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001647 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1648 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001650 sv = get_script_svar(sref, &ectx);
1651 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001652 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653 --ectx.ec_stack.ga_len;
1654 clear_tv(sv->sv_tv);
1655 *sv->sv_tv = *STACK_TV_BOT(0);
1656 }
1657 break;
1658
1659 // store option
1660 case ISN_STOREOPT:
1661 {
1662 long n = 0;
1663 char_u *s = NULL;
1664 char *msg;
1665
1666 --ectx.ec_stack.ga_len;
1667 tv = STACK_TV_BOT(0);
1668 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001669 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001671 if (s == NULL)
1672 s = (char_u *)"";
1673 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001675 // must be VAR_NUMBER, CHECKTYPE makes sure
1676 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1678 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001679 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 if (msg != NULL)
1681 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001682 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001684 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 }
1687 break;
1688
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001689 // store $ENV
1690 case ISN_STOREENV:
1691 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001692 tv = STACK_TV_BOT(0);
1693 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1694 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001695 break;
1696
1697 // store @r
1698 case ISN_STOREREG:
1699 {
1700 int reg = iptr->isn_arg.number;
1701
1702 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001703 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001704 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001705 tv_get_string(tv), -1, FALSE);
1706 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001707 }
1708 break;
1709
1710 // store v: variable
1711 case ISN_STOREV:
1712 --ectx.ec_stack.ga_len;
1713 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1714 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001715 // should not happen, type is checked when compiling
1716 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001717 break;
1718
Bram Moolenaard3aac292020-04-19 14:32:17 +02001719 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001721 case ISN_STOREB:
1722 case ISN_STOREW:
1723 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001725 dictitem_T *di;
1726 hashtab_T *ht;
1727 char_u *name = iptr->isn_arg.string + 2;
1728
Bram Moolenaard3aac292020-04-19 14:32:17 +02001729 switch (iptr->isn_type)
1730 {
1731 case ISN_STOREG:
1732 ht = get_globvar_ht();
1733 break;
1734 case ISN_STOREB:
1735 ht = &curbuf->b_vars->dv_hashtab;
1736 break;
1737 case ISN_STOREW:
1738 ht = &curwin->w_vars->dv_hashtab;
1739 break;
1740 case ISN_STORET:
1741 ht = &curtab->tp_vars->dv_hashtab;
1742 break;
1743 default: // Cannot reach here
1744 goto failed;
1745 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001746
1747 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001748 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001749 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001750 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 else
1752 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001753 SOURCING_LNUM = iptr->isn_lnum;
1754 if (var_check_permission(di, name) == FAIL)
1755 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 clear_tv(&di->di_tv);
1757 di->di_tv = *STACK_TV_BOT(0);
1758 }
1759 }
1760 break;
1761
Bram Moolenaar03290b82020-12-19 16:30:44 +01001762 // store an autoload variable
1763 case ISN_STOREAUTO:
1764 SOURCING_LNUM = iptr->isn_lnum;
1765 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1766 clear_tv(STACK_TV_BOT(-1));
1767 --ectx.ec_stack.ga_len;
1768 break;
1769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 // store number in local variable
1771 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001772 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 clear_tv(tv);
1774 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001775 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 break;
1777
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001778 // store value in list or dict variable
1779 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001780 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001781 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001782 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001783 typval_T *tv_dest = STACK_TV_BOT(-1);
1784 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001785
Bram Moolenaar752fc692021-01-04 21:57:11 +01001786 // Stack contains:
1787 // -3 value to be stored
1788 // -2 index
1789 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001790 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001791 SOURCING_LNUM = iptr->isn_lnum;
1792 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001793 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001794 dest_type = tv_dest->v_type;
1795 if (dest_type == VAR_DICT)
1796 status = do_2string(tv_idx, TRUE);
1797 else if (dest_type == VAR_LIST
1798 && tv_idx->v_type != VAR_NUMBER)
1799 {
1800 emsg(_(e_number_exp));
1801 status = FAIL;
1802 }
1803 }
1804 else if (dest_type != tv_dest->v_type)
1805 {
1806 // just in case, should be OK
1807 semsg(_(e_expected_str_but_got_str),
1808 vartype_name(dest_type),
1809 vartype_name(tv_dest->v_type));
1810 status = FAIL;
1811 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001812
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001813 if (status == OK && dest_type == VAR_LIST)
1814 {
1815 varnumber_T lidx = tv_idx->vval.v_number;
1816 list_T *list = tv_dest->vval.v_list;
1817
1818 if (list == NULL)
1819 {
1820 emsg(_(e_list_not_set));
1821 goto on_error;
1822 }
1823 if (lidx < 0 && list->lv_len + lidx >= 0)
1824 // negative index is relative to the end
1825 lidx = list->lv_len + lidx;
1826 if (lidx < 0 || lidx > list->lv_len)
1827 {
1828 semsg(_(e_listidx), lidx);
1829 goto on_error;
1830 }
1831 if (lidx < list->lv_len)
1832 {
1833 listitem_T *li = list_find(list, lidx);
1834
1835 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001836 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001837 goto on_error;
1838 // overwrite existing list item
1839 clear_tv(&li->li_tv);
1840 li->li_tv = *tv;
1841 }
1842 else
1843 {
1844 if (error_if_locked(list->lv_lock,
1845 e_cannot_change_list))
1846 goto on_error;
1847 // append to list, only fails when out of memory
1848 if (list_append_tv(list, tv) == FAIL)
1849 goto failed;
1850 clear_tv(tv);
1851 }
1852 }
1853 else if (status == OK && dest_type == VAR_DICT)
1854 {
1855 char_u *key = tv_idx->vval.v_string;
1856 dict_T *dict = tv_dest->vval.v_dict;
1857 dictitem_T *di;
1858
1859 SOURCING_LNUM = iptr->isn_lnum;
1860 if (dict == NULL)
1861 {
1862 emsg(_(e_dictionary_not_set));
1863 goto on_error;
1864 }
1865 if (key == NULL)
1866 key = (char_u *)"";
1867 di = dict_find(dict, key, -1);
1868 if (di != NULL)
1869 {
1870 if (error_if_locked(di->di_tv.v_lock,
1871 e_cannot_change_dict_item))
1872 goto on_error;
1873 // overwrite existing value
1874 clear_tv(&di->di_tv);
1875 di->di_tv = *tv;
1876 }
1877 else
1878 {
1879 if (error_if_locked(dict->dv_lock,
1880 e_cannot_change_dict))
1881 goto on_error;
1882 // add to dict, only fails when out of memory
1883 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1884 goto failed;
1885 clear_tv(tv);
1886 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001887 }
1888 else
1889 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001890 status = FAIL;
1891 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001892 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001893
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001894 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001895 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001896 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001897 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001898 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001899 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001900 goto on_error;
1901 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001902 }
1903 break;
1904
Bram Moolenaar752fc692021-01-04 21:57:11 +01001905 // unlet item in list or dict variable
1906 case ISN_UNLETINDEX:
1907 {
1908 typval_T *tv_idx = STACK_TV_BOT(-2);
1909 typval_T *tv_dest = STACK_TV_BOT(-1);
1910 int status = OK;
1911
1912 // Stack contains:
1913 // -2 index
1914 // -1 dict or list
1915 if (tv_dest->v_type == VAR_DICT)
1916 {
1917 // unlet a dict item, index must be a string
1918 if (tv_idx->v_type != VAR_STRING)
1919 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01001920 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001921 semsg(_(e_expected_str_but_got_str),
1922 vartype_name(VAR_STRING),
1923 vartype_name(tv_idx->v_type));
1924 status = FAIL;
1925 }
1926 else
1927 {
1928 dict_T *d = tv_dest->vval.v_dict;
1929 char_u *key = tv_idx->vval.v_string;
1930 dictitem_T *di = NULL;
1931
1932 if (key == NULL)
1933 key = (char_u *)"";
1934 if (d != NULL)
1935 di = dict_find(d, key, (int)STRLEN(key));
1936 if (di == NULL)
1937 {
1938 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01001939 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001940 semsg(_(e_dictkey), key);
1941 status = FAIL;
1942 }
1943 else
1944 {
1945 // TODO: check for dict or item locked
1946 dictitem_remove(d, di);
1947 }
1948 }
1949 }
1950 else if (tv_dest->v_type == VAR_LIST)
1951 {
1952 // unlet a List item, index must be a number
1953 if (tv_idx->v_type != VAR_NUMBER)
1954 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01001955 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001956 semsg(_(e_expected_str_but_got_str),
1957 vartype_name(VAR_NUMBER),
1958 vartype_name(tv_idx->v_type));
1959 status = FAIL;
1960 }
1961 else
1962 {
1963 list_T *l = tv_dest->vval.v_list;
1964 varnumber_T n = tv_idx->vval.v_number;
1965 listitem_T *li = NULL;
1966
1967 li = list_find(l, n);
1968 if (li == NULL)
1969 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01001970 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001971 semsg(_(e_listidx), n);
1972 status = FAIL;
1973 }
1974 else
1975 // TODO: check for list or item locked
1976 listitem_remove(l, li);
1977 }
1978 }
1979 else
1980 {
1981 status = FAIL;
1982 semsg(_(e_cannot_index_str),
1983 vartype_name(tv_dest->v_type));
1984 }
1985
1986 clear_tv(tv_idx);
1987 clear_tv(tv_dest);
1988 ectx.ec_stack.ga_len -= 2;
1989 if (status == FAIL)
1990 goto on_error;
1991 }
1992 break;
1993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994 // push constant
1995 case ISN_PUSHNR:
1996 case ISN_PUSHBOOL:
1997 case ISN_PUSHSPEC:
1998 case ISN_PUSHF:
1999 case ISN_PUSHS:
2000 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002001 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002002 case ISN_PUSHCHANNEL:
2003 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002004 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005 goto failed;
2006 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002007 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 ++ectx.ec_stack.ga_len;
2009 switch (iptr->isn_type)
2010 {
2011 case ISN_PUSHNR:
2012 tv->v_type = VAR_NUMBER;
2013 tv->vval.v_number = iptr->isn_arg.number;
2014 break;
2015 case ISN_PUSHBOOL:
2016 tv->v_type = VAR_BOOL;
2017 tv->vval.v_number = iptr->isn_arg.number;
2018 break;
2019 case ISN_PUSHSPEC:
2020 tv->v_type = VAR_SPECIAL;
2021 tv->vval.v_number = iptr->isn_arg.number;
2022 break;
2023#ifdef FEAT_FLOAT
2024 case ISN_PUSHF:
2025 tv->v_type = VAR_FLOAT;
2026 tv->vval.v_float = iptr->isn_arg.fnumber;
2027 break;
2028#endif
2029 case ISN_PUSHBLOB:
2030 blob_copy(iptr->isn_arg.blob, tv);
2031 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002032 case ISN_PUSHFUNC:
2033 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002034 if (iptr->isn_arg.string == NULL)
2035 tv->vval.v_string = NULL;
2036 else
2037 tv->vval.v_string =
2038 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002039 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002040 case ISN_PUSHCHANNEL:
2041#ifdef FEAT_JOB_CHANNEL
2042 tv->v_type = VAR_CHANNEL;
2043 tv->vval.v_channel = iptr->isn_arg.channel;
2044 if (tv->vval.v_channel != NULL)
2045 ++tv->vval.v_channel->ch_refcount;
2046#endif
2047 break;
2048 case ISN_PUSHJOB:
2049#ifdef FEAT_JOB_CHANNEL
2050 tv->v_type = VAR_JOB;
2051 tv->vval.v_job = iptr->isn_arg.job;
2052 if (tv->vval.v_job != NULL)
2053 ++tv->vval.v_job->jv_refcount;
2054#endif
2055 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 default:
2057 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002058 tv->vval.v_string = vim_strsave(
2059 iptr->isn_arg.string == NULL
2060 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002061 }
2062 break;
2063
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002064 case ISN_UNLET:
2065 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2066 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002067 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002068 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002069 case ISN_UNLETENV:
2070 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2071 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002072
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002073 case ISN_LOCKCONST:
2074 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2075 break;
2076
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002077 // create a list from items on the stack; uses a single allocation
2078 // for the list header and the items
2079 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002080 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2081 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 break;
2083
2084 // create a dict from items on the stack
2085 case ISN_NEWDICT:
2086 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002087 int count = iptr->isn_arg.number;
2088 dict_T *dict = dict_alloc();
2089 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002090 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002091
2092 if (dict == NULL)
2093 goto failed;
2094 for (idx = 0; idx < count; ++idx)
2095 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002096 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002098 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002099 key = tv->vval.v_string == NULL
2100 ? (char_u *)"" : tv->vval.v_string;
2101 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002102 if (item != NULL)
2103 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002104 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002105 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002106 dict_unref(dict);
2107 goto on_error;
2108 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002109 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 clear_tv(tv);
2111 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002112 {
2113 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2117 item->di_tv.v_lock = 0;
2118 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002119 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002120 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002121 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002123 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 }
2125
2126 if (count > 0)
2127 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002128 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129 goto failed;
2130 else
2131 ++ectx.ec_stack.ga_len;
2132 tv = STACK_TV_BOT(-1);
2133 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002134 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135 tv->vval.v_dict = dict;
2136 ++dict->dv_refcount;
2137 }
2138 break;
2139
2140 // call a :def function
2141 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002142 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2144 iptr->isn_arg.dfunc.cdf_argcount,
2145 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002146 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147 break;
2148
2149 // call a builtin function
2150 case ISN_BCALL:
2151 SOURCING_LNUM = iptr->isn_lnum;
2152 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2153 iptr->isn_arg.bfunc.cbf_argcount,
2154 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002155 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156 break;
2157
2158 // call a funcref or partial
2159 case ISN_PCALL:
2160 {
2161 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2162 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002163 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164
2165 SOURCING_LNUM = iptr->isn_lnum;
2166 if (pfunc->cpf_top)
2167 {
2168 // funcref is above the arguments
2169 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2170 }
2171 else
2172 {
2173 // Get the funcref from the stack.
2174 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002175 partial_tv = *STACK_TV_BOT(0);
2176 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 }
2178 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002179 if (tv == &partial_tv)
2180 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002182 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002183 }
2184 break;
2185
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002186 case ISN_PCALL_END:
2187 // PCALL finished, arguments have been consumed and replaced by
2188 // the return value. Now clear the funcref from the stack,
2189 // and move the return value in its place.
2190 --ectx.ec_stack.ga_len;
2191 clear_tv(STACK_TV_BOT(-1));
2192 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2193 break;
2194
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195 // call a user defined function or funcref/partial
2196 case ISN_UCALL:
2197 {
2198 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2199
2200 SOURCING_LNUM = iptr->isn_lnum;
2201 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002202 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002203 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 }
2205 break;
2206
2207 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002208 case ISN_RETURN_ZERO:
2209 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2210 goto failed;
2211 tv = STACK_TV_BOT(0);
2212 ++ectx.ec_stack.ga_len;
2213 tv->v_type = VAR_NUMBER;
2214 tv->vval.v_number = 0;
2215 tv->v_lock = 0;
2216 // FALLTHROUGH
2217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002218 case ISN_RETURN:
2219 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002220 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002221 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002222
2223 if (trystack->ga_len > 0)
2224 trycmd = ((trycmd_T *)trystack->ga_data)
2225 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002226 if (trycmd != NULL
2227 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 && trycmd->tcd_finally_idx != 0)
2229 {
2230 // jump to ":finally"
2231 ectx.ec_iidx = trycmd->tcd_finally_idx;
2232 trycmd->tcd_return = TRUE;
2233 }
2234 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002235 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 }
2237 break;
2238
2239 // push a function reference to a compiled function
2240 case ISN_FUNCREF:
2241 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002242 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2243 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2244 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 if (pt == NULL)
2247 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002248 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002249 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002250 vim_free(pt);
2251 goto failed;
2252 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002253 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2254 &ectx) == FAIL)
2255 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002256
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 tv = STACK_TV_BOT(0);
2258 ++ectx.ec_stack.ga_len;
2259 tv->vval.v_partial = pt;
2260 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002261 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002262 }
2263 break;
2264
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002265 // Create a global function from a lambda.
2266 case ISN_NEWFUNC:
2267 {
2268 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2269
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002270 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002271 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002272 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002273 }
2274 break;
2275
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002276 // List functions
2277 case ISN_DEF:
2278 if (iptr->isn_arg.string == NULL)
2279 list_functions(NULL);
2280 else
2281 {
2282 exarg_T ea;
2283
2284 CLEAR_FIELD(ea);
2285 ea.cmd = ea.arg = iptr->isn_arg.string;
2286 define_function(&ea, NULL);
2287 }
2288 break;
2289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 // jump if a condition is met
2291 case ISN_JUMP:
2292 {
2293 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002294 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 int jump = TRUE;
2296
2297 if (when != JUMP_ALWAYS)
2298 {
2299 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002300 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002301 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002302 || when == JUMP_IF_COND_TRUE)
2303 {
2304 SOURCING_LNUM = iptr->isn_lnum;
2305 jump = tv_get_bool_chk(tv, &error);
2306 if (error)
2307 goto on_error;
2308 }
2309 else
2310 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002312 || when == JUMP_AND_KEEP_IF_FALSE
2313 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002315 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 {
2317 // drop the value from the stack
2318 clear_tv(tv);
2319 --ectx.ec_stack.ga_len;
2320 }
2321 }
2322 if (jump)
2323 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2324 }
2325 break;
2326
2327 // top of a for loop
2328 case ISN_FOR:
2329 {
2330 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2331 typval_T *idxtv =
2332 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2333
2334 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002335 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002337 ++idxtv->vval.v_number;
2338 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339 // past the end of the list, jump to "endfor"
2340 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2341 else if (list->lv_first == &range_list_item)
2342 {
2343 // non-materialized range() list
2344 tv = STACK_TV_BOT(0);
2345 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002346 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 tv->vval.v_number = list_find_nr(
2348 list, idxtv->vval.v_number, NULL);
2349 ++ectx.ec_stack.ga_len;
2350 }
2351 else
2352 {
2353 listitem_T *li = list_find(list, idxtv->vval.v_number);
2354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2356 ++ectx.ec_stack.ga_len;
2357 }
2358 }
2359 break;
2360
2361 // start of ":try" block
2362 case ISN_TRY:
2363 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002364 trycmd_T *trycmd = NULL;
2365
Bram Moolenaar270d0382020-05-15 21:42:53 +02002366 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002367 goto failed;
2368 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2369 + ectx.ec_trystack.ga_len;
2370 ++ectx.ec_trystack.ga_len;
2371 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002372 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2374 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002375 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002376 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377 }
2378 break;
2379
2380 case ISN_PUSHEXC:
2381 if (current_exception == NULL)
2382 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002383 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 iemsg("Evaluating catch while current_exception is NULL");
2385 goto failed;
2386 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002387 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 goto failed;
2389 tv = STACK_TV_BOT(0);
2390 ++ectx.ec_stack.ga_len;
2391 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002392 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 tv->vval.v_string = vim_strsave(
2394 (char_u *)current_exception->value);
2395 break;
2396
2397 case ISN_CATCH:
2398 {
2399 garray_T *trystack = &ectx.ec_trystack;
2400
Bram Moolenaar20a76292020-12-25 19:47:24 +01002401 if (restore_cmdmod)
2402 {
2403 cmdmod.cmod_filter_regmatch.regprog = NULL;
2404 undo_cmdmod(&cmdmod);
2405 cmdmod = save_cmdmod;
2406 restore_cmdmod = FALSE;
2407 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 if (trystack->ga_len > 0)
2409 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002410 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 + trystack->ga_len - 1;
2412 trycmd->tcd_caught = TRUE;
2413 }
2414 did_emsg = got_int = did_throw = FALSE;
2415 catch_exception(current_exception);
2416 }
2417 break;
2418
2419 // end of ":try" block
2420 case ISN_ENDTRY:
2421 {
2422 garray_T *trystack = &ectx.ec_trystack;
2423
2424 if (trystack->ga_len > 0)
2425 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002426 trycmd_T *trycmd = NULL;
2427
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 --trystack->ga_len;
2429 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002430 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002431 trycmd = ((trycmd_T *)trystack->ga_data)
2432 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002433 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434 {
2435 // discard the exception
2436 if (caught_stack == current_exception)
2437 caught_stack = caught_stack->caught;
2438 discard_current_exception();
2439 }
2440
2441 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002442 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 }
2444 }
2445 break;
2446
2447 case ISN_THROW:
2448 --ectx.ec_stack.ga_len;
2449 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002450 if (tv->vval.v_string == NULL
2451 || *skipwhite(tv->vval.v_string) == NUL)
2452 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002453 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002454 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002455 emsg(_(e_throw_with_empty_string));
2456 goto failed;
2457 }
2458
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2460 {
2461 vim_free(tv->vval.v_string);
2462 goto failed;
2463 }
2464 did_throw = TRUE;
2465 break;
2466
2467 // compare with special values
2468 case ISN_COMPAREBOOL:
2469 case ISN_COMPARESPECIAL:
2470 {
2471 typval_T *tv1 = STACK_TV_BOT(-2);
2472 typval_T *tv2 = STACK_TV_BOT(-1);
2473 varnumber_T arg1 = tv1->vval.v_number;
2474 varnumber_T arg2 = tv2->vval.v_number;
2475 int res;
2476
2477 switch (iptr->isn_arg.op.op_type)
2478 {
2479 case EXPR_EQUAL: res = arg1 == arg2; break;
2480 case EXPR_NEQUAL: res = arg1 != arg2; break;
2481 default: res = 0; break;
2482 }
2483
2484 --ectx.ec_stack.ga_len;
2485 tv1->v_type = VAR_BOOL;
2486 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2487 }
2488 break;
2489
2490 // Operation with two number arguments
2491 case ISN_OPNR:
2492 case ISN_COMPARENR:
2493 {
2494 typval_T *tv1 = STACK_TV_BOT(-2);
2495 typval_T *tv2 = STACK_TV_BOT(-1);
2496 varnumber_T arg1 = tv1->vval.v_number;
2497 varnumber_T arg2 = tv2->vval.v_number;
2498 varnumber_T res;
2499
2500 switch (iptr->isn_arg.op.op_type)
2501 {
2502 case EXPR_MULT: res = arg1 * arg2; break;
2503 case EXPR_DIV: res = arg1 / arg2; break;
2504 case EXPR_REM: res = arg1 % arg2; break;
2505 case EXPR_SUB: res = arg1 - arg2; break;
2506 case EXPR_ADD: res = arg1 + arg2; break;
2507
2508 case EXPR_EQUAL: res = arg1 == arg2; break;
2509 case EXPR_NEQUAL: res = arg1 != arg2; break;
2510 case EXPR_GREATER: res = arg1 > arg2; break;
2511 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2512 case EXPR_SMALLER: res = arg1 < arg2; break;
2513 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2514 default: res = 0; break;
2515 }
2516
2517 --ectx.ec_stack.ga_len;
2518 if (iptr->isn_type == ISN_COMPARENR)
2519 {
2520 tv1->v_type = VAR_BOOL;
2521 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2522 }
2523 else
2524 tv1->vval.v_number = res;
2525 }
2526 break;
2527
2528 // Computation with two float arguments
2529 case ISN_OPFLOAT:
2530 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002531#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 {
2533 typval_T *tv1 = STACK_TV_BOT(-2);
2534 typval_T *tv2 = STACK_TV_BOT(-1);
2535 float_T arg1 = tv1->vval.v_float;
2536 float_T arg2 = tv2->vval.v_float;
2537 float_T res = 0;
2538 int cmp = FALSE;
2539
2540 switch (iptr->isn_arg.op.op_type)
2541 {
2542 case EXPR_MULT: res = arg1 * arg2; break;
2543 case EXPR_DIV: res = arg1 / arg2; break;
2544 case EXPR_SUB: res = arg1 - arg2; break;
2545 case EXPR_ADD: res = arg1 + arg2; break;
2546
2547 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2548 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2549 case EXPR_GREATER: cmp = arg1 > arg2; break;
2550 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2551 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2552 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2553 default: cmp = 0; break;
2554 }
2555 --ectx.ec_stack.ga_len;
2556 if (iptr->isn_type == ISN_COMPAREFLOAT)
2557 {
2558 tv1->v_type = VAR_BOOL;
2559 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2560 }
2561 else
2562 tv1->vval.v_float = res;
2563 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002564#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565 break;
2566
2567 case ISN_COMPARELIST:
2568 {
2569 typval_T *tv1 = STACK_TV_BOT(-2);
2570 typval_T *tv2 = STACK_TV_BOT(-1);
2571 list_T *arg1 = tv1->vval.v_list;
2572 list_T *arg2 = tv2->vval.v_list;
2573 int cmp = FALSE;
2574 int ic = iptr->isn_arg.op.op_ic;
2575
2576 switch (iptr->isn_arg.op.op_type)
2577 {
2578 case EXPR_EQUAL: cmp =
2579 list_equal(arg1, arg2, ic, FALSE); break;
2580 case EXPR_NEQUAL: cmp =
2581 !list_equal(arg1, arg2, ic, FALSE); break;
2582 case EXPR_IS: cmp = arg1 == arg2; break;
2583 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2584 default: cmp = 0; break;
2585 }
2586 --ectx.ec_stack.ga_len;
2587 clear_tv(tv1);
2588 clear_tv(tv2);
2589 tv1->v_type = VAR_BOOL;
2590 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2591 }
2592 break;
2593
2594 case ISN_COMPAREBLOB:
2595 {
2596 typval_T *tv1 = STACK_TV_BOT(-2);
2597 typval_T *tv2 = STACK_TV_BOT(-1);
2598 blob_T *arg1 = tv1->vval.v_blob;
2599 blob_T *arg2 = tv2->vval.v_blob;
2600 int cmp = FALSE;
2601
2602 switch (iptr->isn_arg.op.op_type)
2603 {
2604 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2605 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2606 case EXPR_IS: cmp = arg1 == arg2; break;
2607 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2608 default: cmp = 0; break;
2609 }
2610 --ectx.ec_stack.ga_len;
2611 clear_tv(tv1);
2612 clear_tv(tv2);
2613 tv1->v_type = VAR_BOOL;
2614 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2615 }
2616 break;
2617
2618 // TODO: handle separately
2619 case ISN_COMPARESTRING:
2620 case ISN_COMPAREDICT:
2621 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622 case ISN_COMPAREANY:
2623 {
2624 typval_T *tv1 = STACK_TV_BOT(-2);
2625 typval_T *tv2 = STACK_TV_BOT(-1);
2626 exptype_T exptype = iptr->isn_arg.op.op_type;
2627 int ic = iptr->isn_arg.op.op_ic;
2628
Bram Moolenaareb26f432020-09-14 16:50:05 +02002629 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 typval_compare(tv1, tv2, exptype, ic);
2631 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 --ectx.ec_stack.ga_len;
2633 }
2634 break;
2635
2636 case ISN_ADDLIST:
2637 case ISN_ADDBLOB:
2638 {
2639 typval_T *tv1 = STACK_TV_BOT(-2);
2640 typval_T *tv2 = STACK_TV_BOT(-1);
2641
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002642 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 if (iptr->isn_type == ISN_ADDLIST)
2644 eval_addlist(tv1, tv2);
2645 else
2646 eval_addblob(tv1, tv2);
2647 clear_tv(tv2);
2648 --ectx.ec_stack.ga_len;
2649 }
2650 break;
2651
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002652 case ISN_LISTAPPEND:
2653 {
2654 typval_T *tv1 = STACK_TV_BOT(-2);
2655 typval_T *tv2 = STACK_TV_BOT(-1);
2656 list_T *l = tv1->vval.v_list;
2657
2658 // add an item to a list
2659 if (l == NULL)
2660 {
2661 SOURCING_LNUM = iptr->isn_lnum;
2662 emsg(_(e_cannot_add_to_null_list));
2663 goto on_error;
2664 }
2665 if (list_append_tv(l, tv2) == FAIL)
2666 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002667 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002668 --ectx.ec_stack.ga_len;
2669 }
2670 break;
2671
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002672 case ISN_BLOBAPPEND:
2673 {
2674 typval_T *tv1 = STACK_TV_BOT(-2);
2675 typval_T *tv2 = STACK_TV_BOT(-1);
2676 blob_T *b = tv1->vval.v_blob;
2677 int error = FALSE;
2678 varnumber_T n;
2679
2680 // add a number to a blob
2681 if (b == NULL)
2682 {
2683 SOURCING_LNUM = iptr->isn_lnum;
2684 emsg(_(e_cannot_add_to_null_blob));
2685 goto on_error;
2686 }
2687 n = tv_get_number_chk(tv2, &error);
2688 if (error)
2689 goto on_error;
2690 ga_append(&b->bv_ga, (int)n);
2691 --ectx.ec_stack.ga_len;
2692 }
2693 break;
2694
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 // Computation with two arguments of unknown type
2696 case ISN_OPANY:
2697 {
2698 typval_T *tv1 = STACK_TV_BOT(-2);
2699 typval_T *tv2 = STACK_TV_BOT(-1);
2700 varnumber_T n1, n2;
2701#ifdef FEAT_FLOAT
2702 float_T f1 = 0, f2 = 0;
2703#endif
2704 int error = FALSE;
2705
2706 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2707 {
2708 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2709 {
2710 eval_addlist(tv1, tv2);
2711 clear_tv(tv2);
2712 --ectx.ec_stack.ga_len;
2713 break;
2714 }
2715 else if (tv1->v_type == VAR_BLOB
2716 && tv2->v_type == VAR_BLOB)
2717 {
2718 eval_addblob(tv1, tv2);
2719 clear_tv(tv2);
2720 --ectx.ec_stack.ga_len;
2721 break;
2722 }
2723 }
2724#ifdef FEAT_FLOAT
2725 if (tv1->v_type == VAR_FLOAT)
2726 {
2727 f1 = tv1->vval.v_float;
2728 n1 = 0;
2729 }
2730 else
2731#endif
2732 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002733 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 n1 = tv_get_number_chk(tv1, &error);
2735 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002736 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737#ifdef FEAT_FLOAT
2738 if (tv2->v_type == VAR_FLOAT)
2739 f1 = n1;
2740#endif
2741 }
2742#ifdef FEAT_FLOAT
2743 if (tv2->v_type == VAR_FLOAT)
2744 {
2745 f2 = tv2->vval.v_float;
2746 n2 = 0;
2747 }
2748 else
2749#endif
2750 {
2751 n2 = tv_get_number_chk(tv2, &error);
2752 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002753 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754#ifdef FEAT_FLOAT
2755 if (tv1->v_type == VAR_FLOAT)
2756 f2 = n2;
2757#endif
2758 }
2759#ifdef FEAT_FLOAT
2760 // if there is a float on either side the result is a float
2761 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2762 {
2763 switch (iptr->isn_arg.op.op_type)
2764 {
2765 case EXPR_MULT: f1 = f1 * f2; break;
2766 case EXPR_DIV: f1 = f1 / f2; break;
2767 case EXPR_SUB: f1 = f1 - f2; break;
2768 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002769 default: SOURCING_LNUM = iptr->isn_lnum;
2770 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002771 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 }
2773 clear_tv(tv1);
2774 clear_tv(tv2);
2775 tv1->v_type = VAR_FLOAT;
2776 tv1->vval.v_float = f1;
2777 --ectx.ec_stack.ga_len;
2778 }
2779 else
2780#endif
2781 {
2782 switch (iptr->isn_arg.op.op_type)
2783 {
2784 case EXPR_MULT: n1 = n1 * n2; break;
2785 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2786 case EXPR_SUB: n1 = n1 - n2; break;
2787 case EXPR_ADD: n1 = n1 + n2; break;
2788 default: n1 = num_modulus(n1, n2); break;
2789 }
2790 clear_tv(tv1);
2791 clear_tv(tv2);
2792 tv1->v_type = VAR_NUMBER;
2793 tv1->vval.v_number = n1;
2794 --ectx.ec_stack.ga_len;
2795 }
2796 }
2797 break;
2798
2799 case ISN_CONCAT:
2800 {
2801 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2802 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2803 char_u *res;
2804
2805 res = concat_str(str1, str2);
2806 clear_tv(STACK_TV_BOT(-2));
2807 clear_tv(STACK_TV_BOT(-1));
2808 --ectx.ec_stack.ga_len;
2809 STACK_TV_BOT(-1)->vval.v_string = res;
2810 }
2811 break;
2812
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002813 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002814 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002815 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002816 int is_slice = iptr->isn_type == ISN_STRSLICE;
2817 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002818 char_u *res;
2819
2820 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002821 // string slice: string is at stack-3, first index at
2822 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002823 if (is_slice)
2824 {
2825 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002826 n1 = tv->vval.v_number;
2827 }
2828
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002829 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002830 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002831
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002832 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002833 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002834 if (is_slice)
2835 // Slice: Select the characters from the string
2836 res = string_slice(tv->vval.v_string, n1, n2);
2837 else
2838 // Index: The resulting variable is a string of a
2839 // single character. If the index is too big or
2840 // negative the result is empty.
2841 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002842 vim_free(tv->vval.v_string);
2843 tv->vval.v_string = res;
2844 }
2845 break;
2846
2847 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002848 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 {
Bram Moolenaared591872020-08-15 22:14:53 +02002850 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002852 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853
2854 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002855 // list slice: list is at stack-3, indexes at stack-2 and
2856 // stack-1
2857 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 list = tv->vval.v_list;
2859
2860 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002861 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002863
2864 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 {
Bram Moolenaared591872020-08-15 22:14:53 +02002866 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002867 n1 = tv->vval.v_number;
2868 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 }
Bram Moolenaared591872020-08-15 22:14:53 +02002870
2871 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002872 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002873 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002874 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2875 == FAIL)
2876 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 }
2878 break;
2879
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002880 case ISN_ANYINDEX:
2881 case ISN_ANYSLICE:
2882 {
2883 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2884 typval_T *var1, *var2;
2885 int res;
2886
2887 // index: composite is at stack-2, index at stack-1
2888 // slice: composite is at stack-3, indexes at stack-2 and
2889 // stack-1
2890 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002891 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002892 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2893 goto on_error;
2894 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2895 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2896 res = eval_index_inner(tv, is_slice,
2897 var1, var2, NULL, -1, TRUE);
2898 clear_tv(var1);
2899 if (is_slice)
2900 clear_tv(var2);
2901 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2902 if (res == FAIL)
2903 goto on_error;
2904 }
2905 break;
2906
Bram Moolenaar9af78762020-06-16 11:34:42 +02002907 case ISN_SLICE:
2908 {
2909 list_T *list;
2910 int count = iptr->isn_arg.number;
2911
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002912 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002913 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002914 list = tv->vval.v_list;
2915
2916 // no error for short list, expect it to be checked earlier
2917 if (list != NULL && list->lv_len >= count)
2918 {
2919 list_T *newlist = list_slice(list,
2920 count, list->lv_len - 1);
2921
2922 if (newlist != NULL)
2923 {
2924 list_unref(list);
2925 tv->vval.v_list = newlist;
2926 ++newlist->lv_refcount;
2927 }
2928 }
2929 }
2930 break;
2931
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002932 case ISN_GETITEM:
2933 {
2934 listitem_T *li;
2935 int index = iptr->isn_arg.number;
2936
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002937 // Get list item: list is at stack-1, push item.
2938 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002939 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002940 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002941
2942 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2943 goto failed;
2944 ++ectx.ec_stack.ga_len;
2945 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2946 }
2947 break;
2948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 case ISN_MEMBER:
2950 {
2951 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002952 char_u *key;
2953 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002954 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002955
2956 // dict member: dict is at stack-2, key at stack-1
2957 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002958 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002959 dict = tv->vval.v_dict;
2960
2961 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002962 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002963 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01002964 if (key == NULL)
2965 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002966
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002967 if ((di = dict_find(dict, key, -1)) == NULL)
2968 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002969 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002970 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002971
2972 // If :silent! is used we will continue, make sure the
2973 // stack contents makes sense.
2974 clear_tv(tv);
2975 --ectx.ec_stack.ga_len;
2976 tv = STACK_TV_BOT(-1);
2977 clear_tv(tv);
2978 tv->v_type = VAR_NUMBER;
2979 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002980 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002981 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002982 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002983 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002984 // Clear the dict only after getting the item, to avoid
2985 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002986 tv = STACK_TV_BOT(-1);
2987 temp_tv = *tv;
2988 copy_tv(&di->di_tv, tv);
2989 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002990 }
2991 break;
2992
2993 // dict member with string key
2994 case ISN_STRINGMEMBER:
2995 {
2996 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002998 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999
3000 tv = STACK_TV_BOT(-1);
3001 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3002 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003003 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003005 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 }
3007 dict = tv->vval.v_dict;
3008
3009 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3010 == NULL)
3011 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003012 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003014 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003016 // Clear the dict after getting the item, to avoid that it
3017 // make the item invalid.
3018 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003020 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 }
3022 break;
3023
3024 case ISN_NEGATENR:
3025 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003026 if (tv->v_type != VAR_NUMBER
3027#ifdef FEAT_FLOAT
3028 && tv->v_type != VAR_FLOAT
3029#endif
3030 )
3031 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003032 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003033 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003034 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003035 }
3036#ifdef FEAT_FLOAT
3037 if (tv->v_type == VAR_FLOAT)
3038 tv->vval.v_float = -tv->vval.v_float;
3039 else
3040#endif
3041 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 break;
3043
3044 case ISN_CHECKNR:
3045 {
3046 int error = FALSE;
3047
3048 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003049 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003051 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 (void)tv_get_number_chk(tv, &error);
3053 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003054 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 }
3056 break;
3057
3058 case ISN_CHECKTYPE:
3059 {
3060 checktype_T *ct = &iptr->isn_arg.type;
3061
3062 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003063 SOURCING_LNUM = iptr->isn_lnum;
3064 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
3065 goto on_error;
3066
3067 // number 0 is FALSE, number 1 is TRUE
3068 if (tv->v_type == VAR_NUMBER
3069 && ct->ct_type->tt_type == VAR_BOOL
3070 && (tv->vval.v_number == 0
3071 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003073 tv->v_type = VAR_BOOL;
3074 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003075 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 }
3077 }
3078 break;
3079
Bram Moolenaar9af78762020-06-16 11:34:42 +02003080 case ISN_CHECKLEN:
3081 {
3082 int min_len = iptr->isn_arg.checklen.cl_min_len;
3083 list_T *list = NULL;
3084
3085 tv = STACK_TV_BOT(-1);
3086 if (tv->v_type == VAR_LIST)
3087 list = tv->vval.v_list;
3088 if (list == NULL || list->lv_len < min_len
3089 || (list->lv_len > min_len
3090 && !iptr->isn_arg.checklen.cl_more_OK))
3091 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003092 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003093 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003094 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003095 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003096 }
3097 }
3098 break;
3099
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003100 case ISN_SETTYPE:
3101 {
3102 checktype_T *ct = &iptr->isn_arg.type;
3103
3104 tv = STACK_TV_BOT(-1);
3105 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3106 {
3107 free_type(tv->vval.v_dict->dv_type);
3108 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3109 }
3110 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3111 {
3112 free_type(tv->vval.v_list->lv_type);
3113 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3114 }
3115 }
3116 break;
3117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003119 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 {
3121 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003122 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123
3124 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003125 if (iptr->isn_type == ISN_2BOOL)
3126 {
3127 n = tv2bool(tv);
3128 if (iptr->isn_arg.number) // invert
3129 n = !n;
3130 }
3131 else
3132 {
3133 SOURCING_LNUM = iptr->isn_lnum;
3134 n = tv_get_bool_chk(tv, &error);
3135 if (error)
3136 goto on_error;
3137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 clear_tv(tv);
3139 tv->v_type = VAR_BOOL;
3140 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3141 }
3142 break;
3143
3144 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003145 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003146 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003147 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3148 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3149 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 break;
3151
Bram Moolenaar08597872020-12-10 19:43:40 +01003152 case ISN_RANGE:
3153 {
3154 exarg_T ea;
3155 char *errormsg;
3156
3157 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3158 goto failed;
3159 ++ectx.ec_stack.ga_len;
3160 tv = STACK_TV_BOT(-1);
Bram Moolenaarece0b872021-01-08 20:40:45 +01003161 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003162 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003163 ea.addr_type = ADDR_LINES;
3164 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003165 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003166 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003167 goto on_error;
Bram Moolenaar08597872020-12-10 19:43:40 +01003168 if (ea.addr_count == 0)
3169 tv->vval.v_number = curwin->w_cursor.lnum;
3170 else
3171 tv->vval.v_number = ea.line2;
3172 }
3173 break;
3174
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003175 case ISN_PUT:
3176 {
3177 int regname = iptr->isn_arg.put.put_regname;
3178 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3179 char_u *expr = NULL;
3180 int dir = FORWARD;
3181
3182 if (regname == '=')
3183 {
3184 tv = STACK_TV_BOT(-1);
3185 if (tv->v_type == VAR_STRING)
3186 expr = tv->vval.v_string;
3187 else
3188 {
3189 expr = typval_tostring(tv); // allocates value
3190 clear_tv(tv);
3191 }
3192 --ectx.ec_stack.ga_len;
3193 }
Bram Moolenaar08597872020-12-10 19:43:40 +01003194 if (lnum < -2)
3195 {
3196 // line number was put on the stack by ISN_RANGE
3197 tv = STACK_TV_BOT(-1);
3198 curwin->w_cursor.lnum = tv->vval.v_number;
3199 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3200 dir = BACKWARD;
3201 --ectx.ec_stack.ga_len;
3202 }
3203 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003204 // :put! above cursor
3205 dir = BACKWARD;
3206 else if (lnum >= 0)
3207 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
3208 check_cursor();
3209 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3210 vim_free(expr);
3211 }
3212 break;
3213
Bram Moolenaar02194d22020-10-24 23:08:38 +02003214 case ISN_CMDMOD:
3215 save_cmdmod = cmdmod;
3216 restore_cmdmod = TRUE;
3217 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3218 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003219 break;
3220
Bram Moolenaar02194d22020-10-24 23:08:38 +02003221 case ISN_CMDMOD_REV:
3222 // filter regprog is owned by the instruction, don't free it
3223 cmdmod.cmod_filter_regmatch.regprog = NULL;
3224 undo_cmdmod(&cmdmod);
3225 cmdmod = save_cmdmod;
3226 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003227 break;
3228
Bram Moolenaar792f7862020-11-23 08:31:18 +01003229 case ISN_UNPACK:
3230 {
3231 int count = iptr->isn_arg.unpack.unp_count;
3232 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3233 list_T *l;
3234 listitem_T *li;
3235 int i;
3236
3237 // Check there is a valid list to unpack.
3238 tv = STACK_TV_BOT(-1);
3239 if (tv->v_type != VAR_LIST)
3240 {
3241 SOURCING_LNUM = iptr->isn_lnum;
3242 emsg(_(e_for_argument_must_be_sequence_of_lists));
3243 goto on_error;
3244 }
3245 l = tv->vval.v_list;
3246 if (l == NULL
3247 || l->lv_len < (semicolon ? count - 1 : count))
3248 {
3249 SOURCING_LNUM = iptr->isn_lnum;
3250 emsg(_(e_list_value_does_not_have_enough_items));
3251 goto on_error;
3252 }
3253 else if (!semicolon && l->lv_len > count)
3254 {
3255 SOURCING_LNUM = iptr->isn_lnum;
3256 emsg(_(e_list_value_has_more_items_than_targets));
3257 goto on_error;
3258 }
3259
3260 CHECK_LIST_MATERIALIZE(l);
3261 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3262 goto failed;
3263 ectx.ec_stack.ga_len += count - 1;
3264
3265 // Variable after semicolon gets a list with the remaining
3266 // items.
3267 if (semicolon)
3268 {
3269 list_T *rem_list =
3270 list_alloc_with_items(l->lv_len - count + 1);
3271
3272 if (rem_list == NULL)
3273 goto failed;
3274 tv = STACK_TV_BOT(-count);
3275 tv->vval.v_list = rem_list;
3276 ++rem_list->lv_refcount;
3277 tv->v_lock = 0;
3278 li = l->lv_first;
3279 for (i = 0; i < count - 1; ++i)
3280 li = li->li_next;
3281 for (i = 0; li != NULL; ++i)
3282 {
3283 list_set_item(rem_list, i, &li->li_tv);
3284 li = li->li_next;
3285 }
3286 --count;
3287 }
3288
3289 // Produce the values in reverse order, first item last.
3290 li = l->lv_first;
3291 for (i = 0; i < count; ++i)
3292 {
3293 tv = STACK_TV_BOT(-i - 1);
3294 copy_tv(&li->li_tv, tv);
3295 li = li->li_next;
3296 }
3297
3298 list_unref(l);
3299 }
3300 break;
3301
Bram Moolenaar389df252020-07-09 21:20:47 +02003302 case ISN_SHUFFLE:
3303 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003304 typval_T tmp_tv;
3305 int item = iptr->isn_arg.shuffle.shfl_item;
3306 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003307
3308 tmp_tv = *STACK_TV_BOT(-item);
3309 for ( ; up > 0 && item > 1; --up)
3310 {
3311 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3312 --item;
3313 }
3314 *STACK_TV_BOT(-item) = tmp_tv;
3315 }
3316 break;
3317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 case ISN_DROP:
3319 --ectx.ec_stack.ga_len;
3320 clear_tv(STACK_TV_BOT(0));
3321 break;
3322 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003323 continue;
3324
Bram Moolenaard032f342020-07-18 18:13:02 +02003325func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003326 // Restore previous function. If the frame pointer is where we started
3327 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003328 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003329 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003330
Bram Moolenaard032f342020-07-18 18:13:02 +02003331 if (func_return(&ectx) == FAIL)
3332 // only fails when out of memory
3333 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003334 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003335
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003336on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003337 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003338 // If "emsg_silent" is set then ignore the error, unless it was set
3339 // when calling the function.
3340 if (did_emsg_cumul + did_emsg == did_emsg_before
3341 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003342 continue;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003343on_fatal_error:
3344 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003345 // If we are not inside a try-catch started here, abort execution.
3346 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003347 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 }
3349
3350done:
3351 // function finished, get result from the stack.
3352 tv = STACK_TV_BOT(-1);
3353 *rettv = *tv;
3354 tv->v_type = VAR_UNKNOWN;
3355 ret = OK;
3356
3357failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003358 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003359 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003360 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003361
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003362 // Deal with any remaining closures, they may be in use somewhere.
3363 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003364 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003365 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003366 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3367 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003368
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003369 estack_pop();
3370 current_sctx = save_current_sctx;
3371
Bram Moolenaar352134b2020-10-17 22:04:08 +02003372 if (*msg_list != NULL && saved_msg_list != NULL)
3373 {
3374 msglist_T **plist = saved_msg_list;
3375
3376 // Append entries from the current msg_list (uncaught exceptions) to
3377 // the saved msg_list.
3378 while (*plist != NULL)
3379 plist = &(*plist)->next;
3380
3381 *plist = *msg_list;
3382 }
3383 msg_list = saved_msg_list;
3384
Bram Moolenaar02194d22020-10-24 23:08:38 +02003385 if (restore_cmdmod)
3386 {
3387 cmdmod.cmod_filter_regmatch.regprog = NULL;
3388 undo_cmdmod(&cmdmod);
3389 cmdmod = save_cmdmod;
3390 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003391 emsg_silent_def = save_emsg_silent_def;
3392 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003393
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003394failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003395 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3397 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003400 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003401
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003402 // Not sure if this is necessary.
3403 suppress_errthrow = save_suppress_errthrow;
3404
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003405 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003406 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003407 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003408 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 return ret;
3410}
3411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003413 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003414 * We don't really need this at runtime, but we do have tests that require it,
3415 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 */
3417 void
3418ex_disassemble(exarg_T *eap)
3419{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003420 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003421 char_u *fname;
3422 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 dfunc_T *dfunc;
3424 isn_T *instr;
3425 int current;
3426 int line_idx = 0;
3427 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003428 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003430 if (STRNCMP(arg, "<lambda>", 8) == 0)
3431 {
3432 arg += 8;
3433 (void)getdigits(&arg);
3434 fname = vim_strnsave(eap->arg, arg - eap->arg);
3435 }
3436 else
3437 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003438 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003439 if (fname == NULL)
3440 {
3441 semsg(_(e_invarg2), eap->arg);
3442 return;
3443 }
3444
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003445 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003446 if (ufunc == NULL)
3447 {
3448 char_u *p = untrans_function_name(fname);
3449
3450 if (p != NULL)
3451 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003452 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003453 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003454 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 if (ufunc == NULL)
3456 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003457 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 return;
3459 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003460 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003461 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3462 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003463 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003465 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 return;
3467 }
3468 if (ufunc->uf_name_exp != NULL)
3469 msg((char *)ufunc->uf_name_exp);
3470 else
3471 msg((char *)ufunc->uf_name);
3472
3473 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3474 instr = dfunc->df_instr;
3475 for (current = 0; current < dfunc->df_instr_count; ++current)
3476 {
3477 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003478 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479
3480 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3481 {
3482 if (current > prev_current)
3483 {
3484 msg_puts("\n\n");
3485 prev_current = current;
3486 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003487 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3488 if (line != NULL)
3489 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490 }
3491
3492 switch (iptr->isn_type)
3493 {
3494 case ISN_EXEC:
3495 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3496 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003497 case ISN_EXECCONCAT:
3498 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003499 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003500 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 case ISN_ECHO:
3502 {
3503 echo_T *echo = &iptr->isn_arg.echo;
3504
3505 smsg("%4d %s %d", current,
3506 echo->echo_with_white ? "ECHO" : "ECHON",
3507 echo->echo_count);
3508 }
3509 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003510 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003511 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003512 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003513 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003514 case ISN_ECHOMSG:
3515 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003516 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003517 break;
3518 case ISN_ECHOERR:
3519 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003520 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003521 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003523 case ISN_LOADOUTER:
3524 {
3525 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
3526
3527 if (iptr->isn_arg.number < 0)
3528 smsg("%4d LOAD%s arg[%lld]", current, add,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003529 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003530 + STACK_FRAME_SIZE));
3531 else
3532 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003533 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003534 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 break;
3536 case ISN_LOADV:
3537 smsg("%4d LOADV v:%s", current,
3538 get_vim_var_name(iptr->isn_arg.number));
3539 break;
3540 case ISN_LOADSCRIPT:
3541 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003542 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3543 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003545 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003547 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3548 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003549 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003550 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 }
3552 break;
3553 case ISN_LOADS:
3554 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003555 scriptitem_T *si = SCRIPT_ITEM(
3556 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557
3558 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003559 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 }
3561 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003562 case ISN_LOADAUTO:
3563 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3564 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 case ISN_LOADG:
3566 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3567 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003568 case ISN_LOADB:
3569 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3570 break;
3571 case ISN_LOADW:
3572 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3573 break;
3574 case ISN_LOADT:
3575 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3576 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003577 case ISN_LOADGDICT:
3578 smsg("%4d LOAD g:", current);
3579 break;
3580 case ISN_LOADBDICT:
3581 smsg("%4d LOAD b:", current);
3582 break;
3583 case ISN_LOADWDICT:
3584 smsg("%4d LOAD w:", current);
3585 break;
3586 case ISN_LOADTDICT:
3587 smsg("%4d LOAD t:", current);
3588 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 case ISN_LOADOPT:
3590 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3591 break;
3592 case ISN_LOADENV:
3593 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3594 break;
3595 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003596 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 break;
3598
3599 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003600 case ISN_STOREOUTER:
3601 {
3602 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
3603
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003604 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003605 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003606 (varnumber_T)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003607 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003608 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003609 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003610 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003612 case ISN_STOREV:
3613 smsg("%4d STOREV v:%s", current,
3614 get_vim_var_name(iptr->isn_arg.number));
3615 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003616 case ISN_STOREAUTO:
3617 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3618 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003620 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3621 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003622 case ISN_STOREB:
3623 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3624 break;
3625 case ISN_STOREW:
3626 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3627 break;
3628 case ISN_STORET:
3629 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3630 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003631 case ISN_STORES:
3632 {
3633 scriptitem_T *si = SCRIPT_ITEM(
3634 iptr->isn_arg.loadstore.ls_sid);
3635
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003636 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003637 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003638 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 break;
3640 case ISN_STORESCRIPT:
3641 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003642 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3643 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003645 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003647 smsg("%4d STORESCRIPT %s-%d in %s", current,
3648 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003649 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003650 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 }
3652 break;
3653 case ISN_STOREOPT:
3654 smsg("%4d STOREOPT &%s", current,
3655 iptr->isn_arg.storeopt.so_name);
3656 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003657 case ISN_STOREENV:
3658 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3659 break;
3660 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003661 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003662 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 case ISN_STORENR:
3664 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003665 iptr->isn_arg.storenr.stnr_val,
3666 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 break;
3668
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003669 case ISN_STOREINDEX:
3670 switch (iptr->isn_arg.vartype)
3671 {
3672 case VAR_LIST:
3673 smsg("%4d STORELIST", current);
3674 break;
3675 case VAR_DICT:
3676 smsg("%4d STOREDICT", current);
3677 break;
3678 case VAR_ANY:
3679 smsg("%4d STOREINDEX", current);
3680 break;
3681 default: break;
3682 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003683 break;
3684
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 // constants
3686 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003687 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003688 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 break;
3690 case ISN_PUSHBOOL:
3691 case ISN_PUSHSPEC:
3692 smsg("%4d PUSH %s", current,
3693 get_var_special_name(iptr->isn_arg.number));
3694 break;
3695 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003696#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003698#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 break;
3700 case ISN_PUSHS:
3701 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3702 break;
3703 case ISN_PUSHBLOB:
3704 {
3705 char_u *r;
3706 char_u numbuf[NUMBUFLEN];
3707 char_u *tofree;
3708
3709 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003710 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 vim_free(tofree);
3712 }
3713 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003714 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003715 {
3716 char *name = (char *)iptr->isn_arg.string;
3717
3718 smsg("%4d PUSHFUNC \"%s\"", current,
3719 name == NULL ? "[none]" : name);
3720 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003721 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003722 case ISN_PUSHCHANNEL:
3723#ifdef FEAT_JOB_CHANNEL
3724 {
3725 channel_T *channel = iptr->isn_arg.channel;
3726
3727 smsg("%4d PUSHCHANNEL %d", current,
3728 channel == NULL ? 0 : channel->ch_id);
3729 }
3730#endif
3731 break;
3732 case ISN_PUSHJOB:
3733#ifdef FEAT_JOB_CHANNEL
3734 {
3735 typval_T tv;
3736 char_u *name;
3737
3738 tv.v_type = VAR_JOB;
3739 tv.vval.v_job = iptr->isn_arg.job;
3740 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003741 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003742 }
3743#endif
3744 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 case ISN_PUSHEXC:
3746 smsg("%4d PUSH v:exception", current);
3747 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003748 case ISN_UNLET:
3749 smsg("%4d UNLET%s %s", current,
3750 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3751 iptr->isn_arg.unlet.ul_name);
3752 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003753 case ISN_UNLETENV:
3754 smsg("%4d UNLETENV%s $%s", current,
3755 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3756 iptr->isn_arg.unlet.ul_name);
3757 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003758 case ISN_UNLETINDEX:
3759 smsg("%4d UNLETINDEX", current);
3760 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003761 case ISN_LOCKCONST:
3762 smsg("%4d LOCKCONST", current);
3763 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003765 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003766 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 break;
3768 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003769 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003770 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 break;
3772
3773 // function call
3774 case ISN_BCALL:
3775 {
3776 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3777
3778 smsg("%4d BCALL %s(argc %d)", current,
3779 internal_func_name(cbfunc->cbf_idx),
3780 cbfunc->cbf_argcount);
3781 }
3782 break;
3783 case ISN_DCALL:
3784 {
3785 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3786 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3787 + cdfunc->cdf_idx;
3788
3789 smsg("%4d DCALL %s(argc %d)", current,
3790 df->df_ufunc->uf_name_exp != NULL
3791 ? df->df_ufunc->uf_name_exp
3792 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3793 }
3794 break;
3795 case ISN_UCALL:
3796 {
3797 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3798
3799 smsg("%4d UCALL %s(argc %d)", current,
3800 cufunc->cuf_name, cufunc->cuf_argcount);
3801 }
3802 break;
3803 case ISN_PCALL:
3804 {
3805 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3806
3807 smsg("%4d PCALL%s (argc %d)", current,
3808 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3809 }
3810 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003811 case ISN_PCALL_END:
3812 smsg("%4d PCALL end", current);
3813 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 case ISN_RETURN:
3815 smsg("%4d RETURN", current);
3816 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003817 case ISN_RETURN_ZERO:
3818 smsg("%4d RETURN 0", current);
3819 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 case ISN_FUNCREF:
3821 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003822 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003824 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003826 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 }
3828 break;
3829
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003830 case ISN_NEWFUNC:
3831 {
3832 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3833
3834 smsg("%4d NEWFUNC %s %s", current,
3835 newfunc->nf_lambda, newfunc->nf_global);
3836 }
3837 break;
3838
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003839 case ISN_DEF:
3840 {
3841 char_u *name = iptr->isn_arg.string;
3842
3843 smsg("%4d DEF %s", current,
3844 name == NULL ? (char_u *)"" : name);
3845 }
3846 break;
3847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 case ISN_JUMP:
3849 {
3850 char *when = "?";
3851
3852 switch (iptr->isn_arg.jump.jump_when)
3853 {
3854 case JUMP_ALWAYS:
3855 when = "JUMP";
3856 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 case JUMP_AND_KEEP_IF_TRUE:
3858 when = "JUMP_AND_KEEP_IF_TRUE";
3859 break;
3860 case JUMP_IF_FALSE:
3861 when = "JUMP_IF_FALSE";
3862 break;
3863 case JUMP_AND_KEEP_IF_FALSE:
3864 when = "JUMP_AND_KEEP_IF_FALSE";
3865 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003866 case JUMP_IF_COND_FALSE:
3867 when = "JUMP_IF_COND_FALSE";
3868 break;
3869 case JUMP_IF_COND_TRUE:
3870 when = "JUMP_IF_COND_TRUE";
3871 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003873 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 iptr->isn_arg.jump.jump_where);
3875 }
3876 break;
3877
3878 case ISN_FOR:
3879 {
3880 forloop_T *forloop = &iptr->isn_arg.forloop;
3881
3882 smsg("%4d FOR $%d -> %d", current,
3883 forloop->for_idx, forloop->for_end);
3884 }
3885 break;
3886
3887 case ISN_TRY:
3888 {
3889 try_T *try = &iptr->isn_arg.try;
3890
3891 smsg("%4d TRY catch -> %d, finally -> %d", current,
3892 try->try_catch, try->try_finally);
3893 }
3894 break;
3895 case ISN_CATCH:
3896 // TODO
3897 smsg("%4d CATCH", current);
3898 break;
3899 case ISN_ENDTRY:
3900 smsg("%4d ENDTRY", current);
3901 break;
3902 case ISN_THROW:
3903 smsg("%4d THROW", current);
3904 break;
3905
3906 // expression operations on number
3907 case ISN_OPNR:
3908 case ISN_OPFLOAT:
3909 case ISN_OPANY:
3910 {
3911 char *what;
3912 char *ins;
3913
3914 switch (iptr->isn_arg.op.op_type)
3915 {
3916 case EXPR_MULT: what = "*"; break;
3917 case EXPR_DIV: what = "/"; break;
3918 case EXPR_REM: what = "%"; break;
3919 case EXPR_SUB: what = "-"; break;
3920 case EXPR_ADD: what = "+"; break;
3921 default: what = "???"; break;
3922 }
3923 switch (iptr->isn_type)
3924 {
3925 case ISN_OPNR: ins = "OPNR"; break;
3926 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3927 case ISN_OPANY: ins = "OPANY"; break;
3928 default: ins = "???"; break;
3929 }
3930 smsg("%4d %s %s", current, ins, what);
3931 }
3932 break;
3933
3934 case ISN_COMPAREBOOL:
3935 case ISN_COMPARESPECIAL:
3936 case ISN_COMPARENR:
3937 case ISN_COMPAREFLOAT:
3938 case ISN_COMPARESTRING:
3939 case ISN_COMPAREBLOB:
3940 case ISN_COMPARELIST:
3941 case ISN_COMPAREDICT:
3942 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 case ISN_COMPAREANY:
3944 {
3945 char *p;
3946 char buf[10];
3947 char *type;
3948
3949 switch (iptr->isn_arg.op.op_type)
3950 {
3951 case EXPR_EQUAL: p = "=="; break;
3952 case EXPR_NEQUAL: p = "!="; break;
3953 case EXPR_GREATER: p = ">"; break;
3954 case EXPR_GEQUAL: p = ">="; break;
3955 case EXPR_SMALLER: p = "<"; break;
3956 case EXPR_SEQUAL: p = "<="; break;
3957 case EXPR_MATCH: p = "=~"; break;
3958 case EXPR_IS: p = "is"; break;
3959 case EXPR_ISNOT: p = "isnot"; break;
3960 case EXPR_NOMATCH: p = "!~"; break;
3961 default: p = "???"; break;
3962 }
3963 STRCPY(buf, p);
3964 if (iptr->isn_arg.op.op_ic == TRUE)
3965 strcat(buf, "?");
3966 switch(iptr->isn_type)
3967 {
3968 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3969 case ISN_COMPARESPECIAL:
3970 type = "COMPARESPECIAL"; break;
3971 case ISN_COMPARENR: type = "COMPARENR"; break;
3972 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3973 case ISN_COMPARESTRING:
3974 type = "COMPARESTRING"; break;
3975 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3976 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3977 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3978 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3980 default: type = "???"; break;
3981 }
3982
3983 smsg("%4d %s %s", current, type, buf);
3984 }
3985 break;
3986
3987 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3988 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3989
3990 // expression operations
3991 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003992 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003993 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003994 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003995 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003996 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003997 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003998 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3999 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004000 case ISN_SLICE: smsg("%4d SLICE %lld",
4001 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004002 case ISN_GETITEM: smsg("%4d ITEM %lld",
4003 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004004 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4005 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 iptr->isn_arg.string); break;
4007 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4008
4009 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004010 case ISN_CHECKTYPE:
4011 {
4012 char *tofree;
4013
4014 smsg("%4d CHECKTYPE %s stack[%d]", current,
4015 type_name(iptr->isn_arg.type.ct_type, &tofree),
4016 iptr->isn_arg.type.ct_off);
4017 vim_free(tofree);
4018 break;
4019 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004020 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4021 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4022 iptr->isn_arg.checklen.cl_min_len);
4023 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004024 case ISN_SETTYPE:
4025 {
4026 char *tofree;
4027
4028 smsg("%4d SETTYPE %s", current,
4029 type_name(iptr->isn_arg.type.ct_type, &tofree));
4030 vim_free(tofree);
4031 break;
4032 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004033 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 case ISN_2BOOL: if (iptr->isn_arg.number)
4035 smsg("%4d INVERT (!val)", current);
4036 else
4037 smsg("%4d 2BOOL (!!val)", current);
4038 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004039 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004040 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004041 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004042 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004043 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004044 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004045 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4046 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004047 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004048 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4049 smsg("%4d PUT %c above range",
4050 current, iptr->isn_arg.put.put_regname);
4051 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4052 smsg("%4d PUT %c range",
4053 current, iptr->isn_arg.put.put_regname);
4054 else
4055 smsg("%4d PUT %c %ld", current,
4056 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004057 (long)iptr->isn_arg.put.put_lnum);
4058 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059
Bram Moolenaar02194d22020-10-24 23:08:38 +02004060 // TODO: summarize modifiers
4061 case ISN_CMDMOD:
4062 {
4063 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004064 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004065 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4066
4067 buf = alloc(len + 1);
4068 if (buf != NULL)
4069 {
4070 (void)produce_cmdmods(
4071 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4072 smsg("%4d CMDMOD %s", current, buf);
4073 vim_free(buf);
4074 }
4075 break;
4076 }
4077 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004078
Bram Moolenaar792f7862020-11-23 08:31:18 +01004079 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4080 iptr->isn_arg.unpack.unp_count,
4081 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4082 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004083 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4084 iptr->isn_arg.shuffle.shfl_item,
4085 iptr->isn_arg.shuffle.shfl_up);
4086 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 case ISN_DROP: smsg("%4d DROP", current); break;
4088 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004089
4090 out_flush(); // output one line at a time
4091 ui_breakcheck();
4092 if (got_int)
4093 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095}
4096
4097/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004098 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 * list, etc. Mostly like what JavaScript does, except that empty list and
4100 * empty dictionary are FALSE.
4101 */
4102 int
4103tv2bool(typval_T *tv)
4104{
4105 switch (tv->v_type)
4106 {
4107 case VAR_NUMBER:
4108 return tv->vval.v_number != 0;
4109 case VAR_FLOAT:
4110#ifdef FEAT_FLOAT
4111 return tv->vval.v_float != 0.0;
4112#else
4113 break;
4114#endif
4115 case VAR_PARTIAL:
4116 return tv->vval.v_partial != NULL;
4117 case VAR_FUNC:
4118 case VAR_STRING:
4119 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4120 case VAR_LIST:
4121 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4122 case VAR_DICT:
4123 return tv->vval.v_dict != NULL
4124 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4125 case VAR_BOOL:
4126 case VAR_SPECIAL:
4127 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4128 case VAR_JOB:
4129#ifdef FEAT_JOB_CHANNEL
4130 return tv->vval.v_job != NULL;
4131#else
4132 break;
4133#endif
4134 case VAR_CHANNEL:
4135#ifdef FEAT_JOB_CHANNEL
4136 return tv->vval.v_channel != NULL;
4137#else
4138 break;
4139#endif
4140 case VAR_BLOB:
4141 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4142 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004143 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 case VAR_VOID:
4145 break;
4146 }
4147 return FALSE;
4148}
4149
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004150 void
4151emsg_using_string_as(typval_T *tv, int as_number)
4152{
4153 semsg(_(as_number ? e_using_string_as_number_str
4154 : e_using_string_as_bool_str),
4155 tv->vval.v_string == NULL
4156 ? (char_u *)"" : tv->vval.v_string);
4157}
4158
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159/*
4160 * If "tv" is a string give an error and return FAIL.
4161 */
4162 int
4163check_not_string(typval_T *tv)
4164{
4165 if (tv->v_type == VAR_STRING)
4166 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004167 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 clear_tv(tv);
4169 return FAIL;
4170 }
4171 return OK;
4172}
4173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174
4175#endif // FEAT_EVAL