blob: e7a632a19312e32cee7fac5cf9109397553de030 [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 Moolenaare7525c52021-01-09 13:20:37 +0100866/*
867 * Return the character "str[index]" where "index" is the character index. If
868 * "index" is out of range NULL is returned.
869 */
870 char_u *
871char_from_string(char_u *str, varnumber_T index)
872{
873 size_t nbyte = 0;
874 varnumber_T nchar = index;
875 size_t slen;
876
877 if (str == NULL)
878 return NULL;
879 slen = STRLEN(str);
880
881 // do the same as for a list: a negative index counts from the end
882 if (index < 0)
883 {
884 int clen = 0;
885
886 for (nbyte = 0; nbyte < slen; ++clen)
887 nbyte += MB_CPTR2LEN(str + nbyte);
888 nchar = clen + index;
889 if (nchar < 0)
890 // unlike list: index out of range results in empty string
891 return NULL;
892 }
893
894 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
895 nbyte += MB_CPTR2LEN(str + nbyte);
896 if (nbyte >= slen)
897 return NULL;
898 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
899}
900
901/*
902 * Get the byte index for character index "idx" in string "str" with length
903 * "str_len".
904 * If going over the end return "str_len".
905 * If "idx" is negative count from the end, -1 is the last character.
906 * When going over the start return -1.
907 */
908 static long
909char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
910{
911 varnumber_T nchar = idx;
912 size_t nbyte = 0;
913
914 if (nchar >= 0)
915 {
916 while (nchar > 0 && nbyte < str_len)
917 {
918 nbyte += MB_CPTR2LEN(str + nbyte);
919 --nchar;
920 }
921 }
922 else
923 {
924 nbyte = str_len;
925 while (nchar < 0 && nbyte > 0)
926 {
927 --nbyte;
928 nbyte -= mb_head_off(str, str + nbyte);
929 ++nchar;
930 }
931 if (nchar < 0)
932 return -1;
933 }
934 return (long)nbyte;
935}
936
937/*
938 * Return the slice "str[first:last]" using character indexes.
939 * Return NULL when the result is empty.
940 */
941 char_u *
942string_slice(char_u *str, varnumber_T first, varnumber_T last)
943{
944 long start_byte, end_byte;
945 size_t slen;
946
947 if (str == NULL)
948 return NULL;
949 slen = STRLEN(str);
950 start_byte = char_idx2byte(str, slen, first);
951 if (start_byte < 0)
952 start_byte = 0; // first index very negative: use zero
953 if (last == -1)
954 end_byte = (long)slen;
955 else
956 {
957 end_byte = char_idx2byte(str, slen, last);
958 if (end_byte >= 0 && end_byte < (long)slen)
959 // end index is inclusive
960 end_byte += MB_CPTR2LEN(str + end_byte);
961 }
962
963 if (start_byte >= (long)slen || end_byte <= start_byte)
964 return NULL;
965 return vim_strnsave(str + start_byte, end_byte - start_byte);
966}
967
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100968 static svar_T *
969get_script_svar(scriptref_T *sref, ectx_T *ectx)
970{
971 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
972 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
973 + ectx->ec_dfunc_idx;
974 svar_T *sv;
975
976 if (sref->sref_seq != si->sn_script_seq)
977 {
978 // The script was reloaded after the function was
979 // compiled, the script_idx may not be valid.
980 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
981 dfunc->df_ufunc->uf_name_exp);
982 return NULL;
983 }
984 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
985 if (!equal_type(sv->sv_type, sref->sref_type))
986 {
987 emsg(_(e_script_variable_type_changed));
988 return NULL;
989 }
990 return sv;
991}
992
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100993/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994 * Execute a function by "name".
995 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100996 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997 */
998 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100999call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001000{
Bram Moolenaared677f52020-08-12 16:38:10 +02001001 int called_emsg_before = called_emsg;
1002 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003
Bram Moolenaared677f52020-08-12 16:38:10 +02001004 res = call_by_name(name, argcount, ectx, iptr);
1005 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001007 dictitem_T *v;
1008
1009 v = find_var(name, NULL, FALSE);
1010 if (v == NULL)
1011 {
1012 semsg(_(e_unknownfunc), name);
1013 return FAIL;
1014 }
1015 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1016 {
1017 semsg(_(e_unknownfunc), name);
1018 return FAIL;
1019 }
1020 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001021 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001022 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023}
1024
1025/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001026 * When a function reference is used, fill a partial with the information
1027 * needed, especially when it is used as a closure.
1028 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001029 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001030fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1031{
1032 pt->pt_func = ufunc;
1033 pt->pt_refcount = 1;
1034
1035 if (pt->pt_func->uf_flags & FC_CLOSURE)
1036 {
1037 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1038 + ectx->ec_dfunc_idx;
1039
1040 // The closure needs to find arguments and local
1041 // variables in the current stack.
1042 pt->pt_ectx_stack = &ectx->ec_stack;
1043 pt->pt_ectx_frame = ectx->ec_frame_idx;
1044
1045 // If this function returns and the closure is still
1046 // being used, we need to make a copy of the context
1047 // (arguments and local variables). Store a reference
1048 // to the partial so we can handle that.
1049 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1050 {
1051 vim_free(pt);
1052 return FAIL;
1053 }
1054 // Extra variable keeps the count of closures created
1055 // in the current function call.
1056 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1057 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1058
1059 ((partial_T **)ectx->ec_funcrefs.ga_data)
1060 [ectx->ec_funcrefs.ga_len] = pt;
1061 ++pt->pt_refcount;
1062 ++ectx->ec_funcrefs.ga_len;
1063 }
1064 ++pt->pt_func->uf_refcount;
1065 return OK;
1066}
1067
1068/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069 * Call a "def" function from old Vim script.
1070 * Return OK or FAIL.
1071 */
1072 int
1073call_def_function(
1074 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001075 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001077 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078 typval_T *rettv) // return value
1079{
1080 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001081 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001082 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 typval_T *tv;
1084 int idx;
1085 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001086 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001087 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001088 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001089 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001090 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001091 msglist_T **saved_msg_list = NULL;
1092 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001093 cmdmod_T save_cmdmod;
1094 int restore_cmdmod = FALSE;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001095 int save_emsg_silent_def = emsg_silent_def;
1096 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001097 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001098 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099
1100// Get pointer to item in the stack.
1101#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1102
1103// Get pointer to item at the bottom of the stack, -1 is the bottom.
1104#undef STACK_TV_BOT
1105#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1106
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001107// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001108#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 +01001109
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001110// Like STACK_TV_VAR but use the outer scope
1111#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
1112
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001113 if (ufunc->uf_def_status == UF_NOT_COMPILED
1114 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02001115 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001116 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001117 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001118 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001119 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001120 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001121 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001122
Bram Moolenaar09689a02020-05-09 22:50:08 +02001123 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001124 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001125 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1126 + ufunc->uf_dfunc_idx;
1127 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001128 {
1129 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001130 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001131 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001134 // If depth of calling is getting too high, don't execute the function.
1135 orig_funcdepth = funcdepth_get();
1136 if (funcdepth_increment() == FAIL)
1137 return FAIL;
1138
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001139 CLEAR_FIELD(ectx);
1140 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1141 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1142 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001143 {
1144 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001145 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001146 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001147 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001148 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001150 // Put arguments on the stack, but no more than what the function expects.
1151 // A lambda can be called with more arguments than it uses.
1152 for (idx = 0; idx < argc
1153 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1154 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001156 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001157 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
1158 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001159 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001160 copy_tv(&argv[idx], STACK_TV_BOT(0));
1161 ++ectx.ec_stack.ga_len;
1162 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001163
1164 // Turn varargs into a list. Empty list if no args.
1165 if (ufunc->uf_va_name != NULL)
1166 {
1167 int vararg_count = argc - ufunc->uf_args.ga_len;
1168
1169 if (vararg_count < 0)
1170 vararg_count = 0;
1171 else
1172 argc -= vararg_count;
1173 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001174 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001175
1176 // Check the type of the list items.
1177 tv = STACK_TV_BOT(-1);
1178 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001179 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001180 && ufunc->uf_va_type->tt_member != &t_any
1181 && tv->vval.v_list != NULL)
1182 {
1183 type_T *expected = ufunc->uf_va_type->tt_member;
1184 listitem_T *li = tv->vval.v_list->lv_first;
1185
1186 for (idx = 0; idx < vararg_count; ++idx)
1187 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001188 if (check_typval_type(expected, &li->li_tv,
1189 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001190 goto failed_early;
1191 li = li->li_next;
1192 }
1193 }
1194
Bram Moolenaar23e03252020-04-12 22:22:31 +02001195 if (defcount > 0)
1196 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001197 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001198 --ectx.ec_stack.ga_len;
1199 }
1200
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001201 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001202 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001203 if (defcount > 0)
1204 for (idx = 0; idx < defcount; ++idx)
1205 {
1206 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1207 ++ectx.ec_stack.ga_len;
1208 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001209 if (ufunc->uf_va_name != NULL)
1210 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211
1212 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001213 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1214 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001216 if (partial != NULL)
1217 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001218 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
1219 {
1220 // TODO: is this always the right way?
1221 ectx.ec_outer_stack = &current_ectx->ec_stack;
1222 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
1223 }
1224 else
1225 {
1226 ectx.ec_outer_stack = partial->pt_ectx_stack;
1227 ectx.ec_outer_frame = partial->pt_ectx_frame;
1228 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001229 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001230 else if (ufunc->uf_partial != NULL)
1231 {
1232 ectx.ec_outer_stack = ufunc->uf_partial->pt_ectx_stack;
1233 ectx.ec_outer_frame = ufunc->uf_partial->pt_ectx_frame;
1234 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001236 // dummy frame entries
1237 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1238 {
1239 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1240 ++ectx.ec_stack.ga_len;
1241 }
1242
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001243 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001244 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001245 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1246 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001248 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001249 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001250 ectx.ec_stack.ga_len += dfunc->df_varcount;
1251 if (dfunc->df_has_closure)
1252 {
1253 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1254 STACK_TV_VAR(idx)->vval.v_number = 0;
1255 ++ectx.ec_stack.ga_len;
1256 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001257
1258 ectx.ec_instr = dfunc->df_instr;
1259 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001260
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001261 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001262 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001263 estack_push_ufunc(ufunc, 1);
1264 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001265 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1266
Bram Moolenaar352134b2020-10-17 22:04:08 +02001267 // Use a specific location for storing error messages to be converted to an
1268 // exception.
1269 saved_msg_list = msg_list;
1270 msg_list = &private_msg_list;
1271
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001272 // Do turn errors into exceptions.
1273 suppress_errthrow = FALSE;
1274
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001275 // When ":silent!" was used before calling then we still abort the
1276 // function. If ":silent!" is used in the function then we don't.
1277 emsg_silent_def = emsg_silent;
1278 did_emsg_def = 0;
1279
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001280 // Decide where to start execution, handles optional arguments.
1281 init_instr_idx(ufunc, argc, &ectx);
1282
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283 for (;;)
1284 {
1285 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001286
Bram Moolenaar270d0382020-05-15 21:42:53 +02001287 if (++breakcheck_count >= 100)
1288 {
1289 line_breakcheck();
1290 breakcheck_count = 0;
1291 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001292 if (got_int)
1293 {
1294 // Turn CTRL-C into an exception.
1295 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001296 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001297 goto failed;
1298 did_throw = TRUE;
1299 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300
Bram Moolenaara26b9702020-04-18 19:53:28 +02001301 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1302 {
1303 // Turn an error message into an exception.
1304 did_emsg = FALSE;
1305 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1306 goto failed;
1307 did_throw = TRUE;
1308 *msg_list = NULL;
1309 }
1310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001311 if (did_throw && !ectx.ec_in_catch)
1312 {
1313 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001314 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315
1316 // An exception jumps to the first catch, finally, or returns from
1317 // the current function.
1318 if (trystack->ga_len > 0)
1319 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001320 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 {
1322 // jump to ":catch" or ":finally"
1323 ectx.ec_in_catch = TRUE;
1324 ectx.ec_iidx = trycmd->tcd_catch_idx;
1325 }
1326 else
1327 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001328 // Not inside try or need to return from current functions.
1329 // Push a dummy return value.
1330 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1331 goto failed;
1332 tv = STACK_TV_BOT(0);
1333 tv->v_type = VAR_NUMBER;
1334 tv->vval.v_number = 0;
1335 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001336 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001338 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001339 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001340 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1341 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 goto done;
1343 }
1344
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001345 if (func_return(&ectx) == FAIL)
1346 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 }
1348 continue;
1349 }
1350
1351 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1352 switch (iptr->isn_type)
1353 {
1354 // execute Ex command line
1355 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001356 {
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001357 SOURCING_LNUM = iptr->isn_lnum;
1358 do_cmdline_cmd(iptr->isn_arg.string);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001359 if (did_emsg)
1360 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001361 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 break;
1363
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001364 // execute Ex command from pieces on the stack
1365 case ISN_EXECCONCAT:
1366 {
1367 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001368 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001369 int pass;
1370 int i;
1371 char_u *cmd = NULL;
1372 char_u *str;
1373
1374 for (pass = 1; pass <= 2; ++pass)
1375 {
1376 for (i = 0; i < count; ++i)
1377 {
1378 tv = STACK_TV_BOT(i - count);
1379 str = tv->vval.v_string;
1380 if (str != NULL && *str != NUL)
1381 {
1382 if (pass == 2)
1383 STRCPY(cmd + len, str);
1384 len += STRLEN(str);
1385 }
1386 if (pass == 2)
1387 clear_tv(tv);
1388 }
1389 if (pass == 1)
1390 {
1391 cmd = alloc(len + 1);
1392 if (cmd == NULL)
1393 goto failed;
1394 len = 0;
1395 }
1396 }
1397
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001398 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001399 do_cmdline_cmd(cmd);
1400 vim_free(cmd);
1401 }
1402 break;
1403
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 // execute :echo {string} ...
1405 case ISN_ECHO:
1406 {
1407 int count = iptr->isn_arg.echo.echo_count;
1408 int atstart = TRUE;
1409 int needclr = TRUE;
1410
1411 for (idx = 0; idx < count; ++idx)
1412 {
1413 tv = STACK_TV_BOT(idx - count);
1414 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1415 &atstart, &needclr);
1416 clear_tv(tv);
1417 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001418 if (needclr)
1419 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 ectx.ec_stack.ga_len -= count;
1421 }
1422 break;
1423
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001424 // :execute {string} ...
1425 // :echomsg {string} ...
1426 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001427 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001428 case ISN_ECHOMSG:
1429 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001430 {
1431 int count = iptr->isn_arg.number;
1432 garray_T ga;
1433 char_u buf[NUMBUFLEN];
1434 char_u *p;
1435 int len;
1436 int failed = FALSE;
1437
1438 ga_init2(&ga, 1, 80);
1439 for (idx = 0; idx < count; ++idx)
1440 {
1441 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001442 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001443 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001444 if (tv->v_type == VAR_CHANNEL
1445 || tv->v_type == VAR_JOB)
1446 {
1447 SOURCING_LNUM = iptr->isn_lnum;
1448 emsg(_(e_inval_string));
1449 break;
1450 }
1451 else
1452 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001453 }
1454 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001455 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001456
1457 len = (int)STRLEN(p);
1458 if (ga_grow(&ga, len + 2) == FAIL)
1459 failed = TRUE;
1460 else
1461 {
1462 if (ga.ga_len > 0)
1463 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1464 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1465 ga.ga_len += len;
1466 }
1467 clear_tv(tv);
1468 }
1469 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001470 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001471 {
1472 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001473 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001474 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001475
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001476 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001477 {
1478 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001479 {
1480 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001481 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001482 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001483 {
1484 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001485 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001486 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001487 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001488 else
1489 {
1490 msg_sb_eol();
1491 if (iptr->isn_type == ISN_ECHOMSG)
1492 {
1493 msg_attr(ga.ga_data, echo_attr);
1494 out_flush();
1495 }
1496 else
1497 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001498 SOURCING_LNUM = iptr->isn_lnum;
1499 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001500 }
1501 }
1502 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001503 ga_clear(&ga);
1504 }
1505 break;
1506
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 // load local variable or argument
1508 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001509 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 goto failed;
1511 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1512 ++ectx.ec_stack.ga_len;
1513 break;
1514
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001515 // load variable or argument from outer scope
1516 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001517 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001518 goto failed;
1519 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1520 STACK_TV_BOT(0));
1521 ++ectx.ec_stack.ga_len;
1522 break;
1523
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001524 // load v: variable
1525 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001526 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527 goto failed;
1528 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1529 ++ectx.ec_stack.ga_len;
1530 break;
1531
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001532 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 case ISN_LOADSCRIPT:
1534 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001535 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 svar_T *sv;
1537
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001538 sv = get_script_svar(sref, &ectx);
1539 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001540 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001541 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001542 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543 goto failed;
1544 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1545 ++ectx.ec_stack.ga_len;
1546 }
1547 break;
1548
1549 // load s: variable in old script
1550 case ISN_LOADS:
1551 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001552 hashtab_T *ht = &SCRIPT_VARS(
1553 iptr->isn_arg.loadstore.ls_sid);
1554 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 if (di == NULL)
1558 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001559 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001560 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001561 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 }
1563 else
1564 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001565 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566 goto failed;
1567 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1568 ++ectx.ec_stack.ga_len;
1569 }
1570 }
1571 break;
1572
Bram Moolenaard3aac292020-04-19 14:32:17 +02001573 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001575 case ISN_LOADB:
1576 case ISN_LOADW:
1577 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001579 dictitem_T *di = NULL;
1580 hashtab_T *ht = NULL;
1581 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001582
Bram Moolenaard3aac292020-04-19 14:32:17 +02001583 switch (iptr->isn_type)
1584 {
1585 case ISN_LOADG:
1586 ht = get_globvar_ht();
1587 namespace = 'g';
1588 break;
1589 case ISN_LOADB:
1590 ht = &curbuf->b_vars->dv_hashtab;
1591 namespace = 'b';
1592 break;
1593 case ISN_LOADW:
1594 ht = &curwin->w_vars->dv_hashtab;
1595 namespace = 'w';
1596 break;
1597 case ISN_LOADT:
1598 ht = &curtab->tp_vars->dv_hashtab;
1599 namespace = 't';
1600 break;
1601 default: // Cannot reach here
1602 goto failed;
1603 }
1604 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 if (di == NULL)
1607 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001608 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001609 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001610 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001611 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 }
1613 else
1614 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001615 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 goto failed;
1617 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1618 ++ectx.ec_stack.ga_len;
1619 }
1620 }
1621 break;
1622
Bram Moolenaar03290b82020-12-19 16:30:44 +01001623 // load autoload variable
1624 case ISN_LOADAUTO:
1625 {
1626 char_u *name = iptr->isn_arg.string;
1627
1628 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1629 goto failed;
1630 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001631 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001632 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1633 goto on_error;
1634 ++ectx.ec_stack.ga_len;
1635 }
1636 break;
1637
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001638 // load g:/b:/w:/t: namespace
1639 case ISN_LOADGDICT:
1640 case ISN_LOADBDICT:
1641 case ISN_LOADWDICT:
1642 case ISN_LOADTDICT:
1643 {
1644 dict_T *d = NULL;
1645
1646 switch (iptr->isn_type)
1647 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001648 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1649 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1650 case ISN_LOADWDICT: d = curwin->w_vars; break;
1651 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001652 default: // Cannot reach here
1653 goto failed;
1654 }
1655 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1656 goto failed;
1657 tv = STACK_TV_BOT(0);
1658 tv->v_type = VAR_DICT;
1659 tv->v_lock = 0;
1660 tv->vval.v_dict = d;
1661 ++ectx.ec_stack.ga_len;
1662 }
1663 break;
1664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 // load &option
1666 case ISN_LOADOPT:
1667 {
1668 typval_T optval;
1669 char_u *name = iptr->isn_arg.string;
1670
Bram Moolenaara8c17702020-04-01 21:17:24 +02001671 // This is not expected to fail, name is checked during
1672 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001673 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001675 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001676 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677 *STACK_TV_BOT(0) = optval;
1678 ++ectx.ec_stack.ga_len;
1679 }
1680 break;
1681
1682 // load $ENV
1683 case ISN_LOADENV:
1684 {
1685 typval_T optval;
1686 char_u *name = iptr->isn_arg.string;
1687
Bram Moolenaar270d0382020-05-15 21:42:53 +02001688 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001690 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001691 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692 *STACK_TV_BOT(0) = optval;
1693 ++ectx.ec_stack.ga_len;
1694 }
1695 break;
1696
1697 // load @register
1698 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001699 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 goto failed;
1701 tv = STACK_TV_BOT(0);
1702 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001703 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001704 // This may result in NULL, which should be equivalent to an
1705 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706 tv->vval.v_string = get_reg_contents(
1707 iptr->isn_arg.number, GREG_EXPR_SRC);
1708 ++ectx.ec_stack.ga_len;
1709 break;
1710
1711 // store local variable
1712 case ISN_STORE:
1713 --ectx.ec_stack.ga_len;
1714 tv = STACK_TV_VAR(iptr->isn_arg.number);
1715 clear_tv(tv);
1716 *tv = *STACK_TV_BOT(0);
1717 break;
1718
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001719 // store variable or argument in outer scope
1720 case ISN_STOREOUTER:
1721 --ectx.ec_stack.ga_len;
1722 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1723 clear_tv(tv);
1724 *tv = *STACK_TV_BOT(0);
1725 break;
1726
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001727 // store s: variable in old script
1728 case ISN_STORES:
1729 {
1730 hashtab_T *ht = &SCRIPT_VARS(
1731 iptr->isn_arg.loadstore.ls_sid);
1732 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001733 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001734
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001735 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001736 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001737 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001738 else
1739 {
1740 clear_tv(&di->di_tv);
1741 di->di_tv = *STACK_TV_BOT(0);
1742 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001743 }
1744 break;
1745
1746 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 case ISN_STORESCRIPT:
1748 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001749 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1750 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001752 sv = get_script_svar(sref, &ectx);
1753 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001754 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755 --ectx.ec_stack.ga_len;
1756 clear_tv(sv->sv_tv);
1757 *sv->sv_tv = *STACK_TV_BOT(0);
1758 }
1759 break;
1760
1761 // store option
1762 case ISN_STOREOPT:
1763 {
1764 long n = 0;
1765 char_u *s = NULL;
1766 char *msg;
1767
1768 --ectx.ec_stack.ga_len;
1769 tv = STACK_TV_BOT(0);
1770 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001771 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001773 if (s == NULL)
1774 s = (char_u *)"";
1775 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001777 // must be VAR_NUMBER, CHECKTYPE makes sure
1778 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1780 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001781 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 if (msg != NULL)
1783 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001784 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001786 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 }
1789 break;
1790
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001791 // store $ENV
1792 case ISN_STOREENV:
1793 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001794 tv = STACK_TV_BOT(0);
1795 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1796 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001797 break;
1798
1799 // store @r
1800 case ISN_STOREREG:
1801 {
1802 int reg = iptr->isn_arg.number;
1803
1804 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001805 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001806 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001807 tv_get_string(tv), -1, FALSE);
1808 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001809 }
1810 break;
1811
1812 // store v: variable
1813 case ISN_STOREV:
1814 --ectx.ec_stack.ga_len;
1815 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1816 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001817 // should not happen, type is checked when compiling
1818 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001819 break;
1820
Bram Moolenaard3aac292020-04-19 14:32:17 +02001821 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001823 case ISN_STOREB:
1824 case ISN_STOREW:
1825 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001827 dictitem_T *di;
1828 hashtab_T *ht;
1829 char_u *name = iptr->isn_arg.string + 2;
1830
Bram Moolenaard3aac292020-04-19 14:32:17 +02001831 switch (iptr->isn_type)
1832 {
1833 case ISN_STOREG:
1834 ht = get_globvar_ht();
1835 break;
1836 case ISN_STOREB:
1837 ht = &curbuf->b_vars->dv_hashtab;
1838 break;
1839 case ISN_STOREW:
1840 ht = &curwin->w_vars->dv_hashtab;
1841 break;
1842 case ISN_STORET:
1843 ht = &curtab->tp_vars->dv_hashtab;
1844 break;
1845 default: // Cannot reach here
1846 goto failed;
1847 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848
1849 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001850 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001852 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853 else
1854 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001855 SOURCING_LNUM = iptr->isn_lnum;
1856 if (var_check_permission(di, name) == FAIL)
1857 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858 clear_tv(&di->di_tv);
1859 di->di_tv = *STACK_TV_BOT(0);
1860 }
1861 }
1862 break;
1863
Bram Moolenaar03290b82020-12-19 16:30:44 +01001864 // store an autoload variable
1865 case ISN_STOREAUTO:
1866 SOURCING_LNUM = iptr->isn_lnum;
1867 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1868 clear_tv(STACK_TV_BOT(-1));
1869 --ectx.ec_stack.ga_len;
1870 break;
1871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 // store number in local variable
1873 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001874 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 clear_tv(tv);
1876 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001877 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878 break;
1879
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001880 // store value in list or dict variable
1881 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001882 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001883 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001884 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001885 typval_T *tv_dest = STACK_TV_BOT(-1);
1886 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001887
Bram Moolenaar752fc692021-01-04 21:57:11 +01001888 // Stack contains:
1889 // -3 value to be stored
1890 // -2 index
1891 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001892 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001893 SOURCING_LNUM = iptr->isn_lnum;
1894 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001895 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001896 dest_type = tv_dest->v_type;
1897 if (dest_type == VAR_DICT)
1898 status = do_2string(tv_idx, TRUE);
1899 else if (dest_type == VAR_LIST
1900 && tv_idx->v_type != VAR_NUMBER)
1901 {
1902 emsg(_(e_number_exp));
1903 status = FAIL;
1904 }
1905 }
1906 else if (dest_type != tv_dest->v_type)
1907 {
1908 // just in case, should be OK
1909 semsg(_(e_expected_str_but_got_str),
1910 vartype_name(dest_type),
1911 vartype_name(tv_dest->v_type));
1912 status = FAIL;
1913 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001914
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001915 if (status == OK && dest_type == VAR_LIST)
1916 {
1917 varnumber_T lidx = tv_idx->vval.v_number;
1918 list_T *list = tv_dest->vval.v_list;
1919
1920 if (list == NULL)
1921 {
1922 emsg(_(e_list_not_set));
1923 goto on_error;
1924 }
1925 if (lidx < 0 && list->lv_len + lidx >= 0)
1926 // negative index is relative to the end
1927 lidx = list->lv_len + lidx;
1928 if (lidx < 0 || lidx > list->lv_len)
1929 {
1930 semsg(_(e_listidx), lidx);
1931 goto on_error;
1932 }
1933 if (lidx < list->lv_len)
1934 {
1935 listitem_T *li = list_find(list, lidx);
1936
1937 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001938 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001939 goto on_error;
1940 // overwrite existing list item
1941 clear_tv(&li->li_tv);
1942 li->li_tv = *tv;
1943 }
1944 else
1945 {
1946 if (error_if_locked(list->lv_lock,
1947 e_cannot_change_list))
1948 goto on_error;
1949 // append to list, only fails when out of memory
1950 if (list_append_tv(list, tv) == FAIL)
1951 goto failed;
1952 clear_tv(tv);
1953 }
1954 }
1955 else if (status == OK && dest_type == VAR_DICT)
1956 {
1957 char_u *key = tv_idx->vval.v_string;
1958 dict_T *dict = tv_dest->vval.v_dict;
1959 dictitem_T *di;
1960
1961 SOURCING_LNUM = iptr->isn_lnum;
1962 if (dict == NULL)
1963 {
1964 emsg(_(e_dictionary_not_set));
1965 goto on_error;
1966 }
1967 if (key == NULL)
1968 key = (char_u *)"";
1969 di = dict_find(dict, key, -1);
1970 if (di != NULL)
1971 {
1972 if (error_if_locked(di->di_tv.v_lock,
1973 e_cannot_change_dict_item))
1974 goto on_error;
1975 // overwrite existing value
1976 clear_tv(&di->di_tv);
1977 di->di_tv = *tv;
1978 }
1979 else
1980 {
1981 if (error_if_locked(dict->dv_lock,
1982 e_cannot_change_dict))
1983 goto on_error;
1984 // add to dict, only fails when out of memory
1985 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1986 goto failed;
1987 clear_tv(tv);
1988 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001989 }
1990 else
1991 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001992 status = FAIL;
1993 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001994 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001995
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001996 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001997 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001998 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001999 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002000 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002001 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002002 goto on_error;
2003 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002004 }
2005 break;
2006
Bram Moolenaar752fc692021-01-04 21:57:11 +01002007 // unlet item in list or dict variable
2008 case ISN_UNLETINDEX:
2009 {
2010 typval_T *tv_idx = STACK_TV_BOT(-2);
2011 typval_T *tv_dest = STACK_TV_BOT(-1);
2012 int status = OK;
2013
2014 // Stack contains:
2015 // -2 index
2016 // -1 dict or list
2017 if (tv_dest->v_type == VAR_DICT)
2018 {
2019 // unlet a dict item, index must be a string
2020 if (tv_idx->v_type != VAR_STRING)
2021 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002022 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002023 semsg(_(e_expected_str_but_got_str),
2024 vartype_name(VAR_STRING),
2025 vartype_name(tv_idx->v_type));
2026 status = FAIL;
2027 }
2028 else
2029 {
2030 dict_T *d = tv_dest->vval.v_dict;
2031 char_u *key = tv_idx->vval.v_string;
2032 dictitem_T *di = NULL;
2033
2034 if (key == NULL)
2035 key = (char_u *)"";
2036 if (d != NULL)
2037 di = dict_find(d, key, (int)STRLEN(key));
2038 if (di == NULL)
2039 {
2040 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002041 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002042 semsg(_(e_dictkey), key);
2043 status = FAIL;
2044 }
2045 else
2046 {
2047 // TODO: check for dict or item locked
2048 dictitem_remove(d, di);
2049 }
2050 }
2051 }
2052 else if (tv_dest->v_type == VAR_LIST)
2053 {
2054 // unlet a List item, index must be a number
2055 if (tv_idx->v_type != VAR_NUMBER)
2056 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002057 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002058 semsg(_(e_expected_str_but_got_str),
2059 vartype_name(VAR_NUMBER),
2060 vartype_name(tv_idx->v_type));
2061 status = FAIL;
2062 }
2063 else
2064 {
2065 list_T *l = tv_dest->vval.v_list;
2066 varnumber_T n = tv_idx->vval.v_number;
2067 listitem_T *li = NULL;
2068
2069 li = list_find(l, n);
2070 if (li == NULL)
2071 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002072 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002073 semsg(_(e_listidx), n);
2074 status = FAIL;
2075 }
2076 else
2077 // TODO: check for list or item locked
2078 listitem_remove(l, li);
2079 }
2080 }
2081 else
2082 {
2083 status = FAIL;
2084 semsg(_(e_cannot_index_str),
2085 vartype_name(tv_dest->v_type));
2086 }
2087
2088 clear_tv(tv_idx);
2089 clear_tv(tv_dest);
2090 ectx.ec_stack.ga_len -= 2;
2091 if (status == FAIL)
2092 goto on_error;
2093 }
2094 break;
2095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096 // push constant
2097 case ISN_PUSHNR:
2098 case ISN_PUSHBOOL:
2099 case ISN_PUSHSPEC:
2100 case ISN_PUSHF:
2101 case ISN_PUSHS:
2102 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002103 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002104 case ISN_PUSHCHANNEL:
2105 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002106 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 goto failed;
2108 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002109 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 ++ectx.ec_stack.ga_len;
2111 switch (iptr->isn_type)
2112 {
2113 case ISN_PUSHNR:
2114 tv->v_type = VAR_NUMBER;
2115 tv->vval.v_number = iptr->isn_arg.number;
2116 break;
2117 case ISN_PUSHBOOL:
2118 tv->v_type = VAR_BOOL;
2119 tv->vval.v_number = iptr->isn_arg.number;
2120 break;
2121 case ISN_PUSHSPEC:
2122 tv->v_type = VAR_SPECIAL;
2123 tv->vval.v_number = iptr->isn_arg.number;
2124 break;
2125#ifdef FEAT_FLOAT
2126 case ISN_PUSHF:
2127 tv->v_type = VAR_FLOAT;
2128 tv->vval.v_float = iptr->isn_arg.fnumber;
2129 break;
2130#endif
2131 case ISN_PUSHBLOB:
2132 blob_copy(iptr->isn_arg.blob, tv);
2133 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002134 case ISN_PUSHFUNC:
2135 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002136 if (iptr->isn_arg.string == NULL)
2137 tv->vval.v_string = NULL;
2138 else
2139 tv->vval.v_string =
2140 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002141 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002142 case ISN_PUSHCHANNEL:
2143#ifdef FEAT_JOB_CHANNEL
2144 tv->v_type = VAR_CHANNEL;
2145 tv->vval.v_channel = iptr->isn_arg.channel;
2146 if (tv->vval.v_channel != NULL)
2147 ++tv->vval.v_channel->ch_refcount;
2148#endif
2149 break;
2150 case ISN_PUSHJOB:
2151#ifdef FEAT_JOB_CHANNEL
2152 tv->v_type = VAR_JOB;
2153 tv->vval.v_job = iptr->isn_arg.job;
2154 if (tv->vval.v_job != NULL)
2155 ++tv->vval.v_job->jv_refcount;
2156#endif
2157 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158 default:
2159 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002160 tv->vval.v_string = vim_strsave(
2161 iptr->isn_arg.string == NULL
2162 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 }
2164 break;
2165
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002166 case ISN_UNLET:
2167 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2168 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002169 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002170 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002171 case ISN_UNLETENV:
2172 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2173 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002174
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002175 case ISN_LOCKCONST:
2176 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2177 break;
2178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 // create a list from items on the stack; uses a single allocation
2180 // for the list header and the items
2181 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002182 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2183 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184 break;
2185
2186 // create a dict from items on the stack
2187 case ISN_NEWDICT:
2188 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002189 int count = iptr->isn_arg.number;
2190 dict_T *dict = dict_alloc();
2191 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002192 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193
2194 if (dict == NULL)
2195 goto failed;
2196 for (idx = 0; idx < count; ++idx)
2197 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002198 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002200 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002201 key = tv->vval.v_string == NULL
2202 ? (char_u *)"" : tv->vval.v_string;
2203 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002204 if (item != NULL)
2205 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002206 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002207 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002208 dict_unref(dict);
2209 goto on_error;
2210 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002211 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 clear_tv(tv);
2213 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002214 {
2215 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002218 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2219 item->di_tv.v_lock = 0;
2220 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002221 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002222 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002223 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226 }
2227
2228 if (count > 0)
2229 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002230 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 goto failed;
2232 else
2233 ++ectx.ec_stack.ga_len;
2234 tv = STACK_TV_BOT(-1);
2235 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002236 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 tv->vval.v_dict = dict;
2238 ++dict->dv_refcount;
2239 }
2240 break;
2241
2242 // call a :def function
2243 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002244 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2246 iptr->isn_arg.dfunc.cdf_argcount,
2247 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002248 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 break;
2250
2251 // call a builtin function
2252 case ISN_BCALL:
2253 SOURCING_LNUM = iptr->isn_lnum;
2254 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2255 iptr->isn_arg.bfunc.cbf_argcount,
2256 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002257 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 break;
2259
2260 // call a funcref or partial
2261 case ISN_PCALL:
2262 {
2263 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2264 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002265 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266
2267 SOURCING_LNUM = iptr->isn_lnum;
2268 if (pfunc->cpf_top)
2269 {
2270 // funcref is above the arguments
2271 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2272 }
2273 else
2274 {
2275 // Get the funcref from the stack.
2276 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002277 partial_tv = *STACK_TV_BOT(0);
2278 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 }
2280 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002281 if (tv == &partial_tv)
2282 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002284 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 }
2286 break;
2287
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002288 case ISN_PCALL_END:
2289 // PCALL finished, arguments have been consumed and replaced by
2290 // the return value. Now clear the funcref from the stack,
2291 // and move the return value in its place.
2292 --ectx.ec_stack.ga_len;
2293 clear_tv(STACK_TV_BOT(-1));
2294 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2295 break;
2296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 // call a user defined function or funcref/partial
2298 case ISN_UCALL:
2299 {
2300 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2301
2302 SOURCING_LNUM = iptr->isn_lnum;
2303 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002304 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002305 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 }
2307 break;
2308
2309 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002310 case ISN_RETURN_ZERO:
2311 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2312 goto failed;
2313 tv = STACK_TV_BOT(0);
2314 ++ectx.ec_stack.ga_len;
2315 tv->v_type = VAR_NUMBER;
2316 tv->vval.v_number = 0;
2317 tv->v_lock = 0;
2318 // FALLTHROUGH
2319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 case ISN_RETURN:
2321 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002322 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002323 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002324
2325 if (trystack->ga_len > 0)
2326 trycmd = ((trycmd_T *)trystack->ga_data)
2327 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002328 if (trycmd != NULL
2329 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 && trycmd->tcd_finally_idx != 0)
2331 {
2332 // jump to ":finally"
2333 ectx.ec_iidx = trycmd->tcd_finally_idx;
2334 trycmd->tcd_return = TRUE;
2335 }
2336 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002337 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338 }
2339 break;
2340
2341 // push a function reference to a compiled function
2342 case ISN_FUNCREF:
2343 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002344 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2345 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2346 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 if (pt == NULL)
2349 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002350 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002351 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002352 vim_free(pt);
2353 goto failed;
2354 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002355 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2356 &ectx) == FAIL)
2357 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 tv = STACK_TV_BOT(0);
2360 ++ectx.ec_stack.ga_len;
2361 tv->vval.v_partial = pt;
2362 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002363 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 }
2365 break;
2366
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002367 // Create a global function from a lambda.
2368 case ISN_NEWFUNC:
2369 {
2370 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2371
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002372 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002373 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002374 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002375 }
2376 break;
2377
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002378 // List functions
2379 case ISN_DEF:
2380 if (iptr->isn_arg.string == NULL)
2381 list_functions(NULL);
2382 else
2383 {
2384 exarg_T ea;
2385
2386 CLEAR_FIELD(ea);
2387 ea.cmd = ea.arg = iptr->isn_arg.string;
2388 define_function(&ea, NULL);
2389 }
2390 break;
2391
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392 // jump if a condition is met
2393 case ISN_JUMP:
2394 {
2395 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002396 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 int jump = TRUE;
2398
2399 if (when != JUMP_ALWAYS)
2400 {
2401 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002402 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002403 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002404 || when == JUMP_IF_COND_TRUE)
2405 {
2406 SOURCING_LNUM = iptr->isn_lnum;
2407 jump = tv_get_bool_chk(tv, &error);
2408 if (error)
2409 goto on_error;
2410 }
2411 else
2412 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002414 || when == JUMP_AND_KEEP_IF_FALSE
2415 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002417 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 {
2419 // drop the value from the stack
2420 clear_tv(tv);
2421 --ectx.ec_stack.ga_len;
2422 }
2423 }
2424 if (jump)
2425 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2426 }
2427 break;
2428
2429 // top of a for loop
2430 case ISN_FOR:
2431 {
2432 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2433 typval_T *idxtv =
2434 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2435
2436 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002437 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002439 ++idxtv->vval.v_number;
2440 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441 // past the end of the list, jump to "endfor"
2442 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2443 else if (list->lv_first == &range_list_item)
2444 {
2445 // non-materialized range() list
2446 tv = STACK_TV_BOT(0);
2447 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002448 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 tv->vval.v_number = list_find_nr(
2450 list, idxtv->vval.v_number, NULL);
2451 ++ectx.ec_stack.ga_len;
2452 }
2453 else
2454 {
2455 listitem_T *li = list_find(list, idxtv->vval.v_number);
2456
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2458 ++ectx.ec_stack.ga_len;
2459 }
2460 }
2461 break;
2462
2463 // start of ":try" block
2464 case ISN_TRY:
2465 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002466 trycmd_T *trycmd = NULL;
2467
Bram Moolenaar270d0382020-05-15 21:42:53 +02002468 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 goto failed;
2470 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2471 + ectx.ec_trystack.ga_len;
2472 ++ectx.ec_trystack.ga_len;
2473 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002474 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2476 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002477 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002478 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 }
2480 break;
2481
2482 case ISN_PUSHEXC:
2483 if (current_exception == NULL)
2484 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002485 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 iemsg("Evaluating catch while current_exception is NULL");
2487 goto failed;
2488 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002489 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 goto failed;
2491 tv = STACK_TV_BOT(0);
2492 ++ectx.ec_stack.ga_len;
2493 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002494 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 tv->vval.v_string = vim_strsave(
2496 (char_u *)current_exception->value);
2497 break;
2498
2499 case ISN_CATCH:
2500 {
2501 garray_T *trystack = &ectx.ec_trystack;
2502
Bram Moolenaar20a76292020-12-25 19:47:24 +01002503 if (restore_cmdmod)
2504 {
2505 cmdmod.cmod_filter_regmatch.regprog = NULL;
2506 undo_cmdmod(&cmdmod);
2507 cmdmod = save_cmdmod;
2508 restore_cmdmod = FALSE;
2509 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 if (trystack->ga_len > 0)
2511 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002512 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 + trystack->ga_len - 1;
2514 trycmd->tcd_caught = TRUE;
2515 }
2516 did_emsg = got_int = did_throw = FALSE;
2517 catch_exception(current_exception);
2518 }
2519 break;
2520
2521 // end of ":try" block
2522 case ISN_ENDTRY:
2523 {
2524 garray_T *trystack = &ectx.ec_trystack;
2525
2526 if (trystack->ga_len > 0)
2527 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002528 trycmd_T *trycmd = NULL;
2529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 --trystack->ga_len;
2531 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002532 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 trycmd = ((trycmd_T *)trystack->ga_data)
2534 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002535 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 {
2537 // discard the exception
2538 if (caught_stack == current_exception)
2539 caught_stack = caught_stack->caught;
2540 discard_current_exception();
2541 }
2542
2543 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002544 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 }
2546 }
2547 break;
2548
2549 case ISN_THROW:
2550 --ectx.ec_stack.ga_len;
2551 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002552 if (tv->vval.v_string == NULL
2553 || *skipwhite(tv->vval.v_string) == NUL)
2554 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002555 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002556 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002557 emsg(_(e_throw_with_empty_string));
2558 goto failed;
2559 }
2560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2562 {
2563 vim_free(tv->vval.v_string);
2564 goto failed;
2565 }
2566 did_throw = TRUE;
2567 break;
2568
2569 // compare with special values
2570 case ISN_COMPAREBOOL:
2571 case ISN_COMPARESPECIAL:
2572 {
2573 typval_T *tv1 = STACK_TV_BOT(-2);
2574 typval_T *tv2 = STACK_TV_BOT(-1);
2575 varnumber_T arg1 = tv1->vval.v_number;
2576 varnumber_T arg2 = tv2->vval.v_number;
2577 int res;
2578
2579 switch (iptr->isn_arg.op.op_type)
2580 {
2581 case EXPR_EQUAL: res = arg1 == arg2; break;
2582 case EXPR_NEQUAL: res = arg1 != arg2; break;
2583 default: res = 0; break;
2584 }
2585
2586 --ectx.ec_stack.ga_len;
2587 tv1->v_type = VAR_BOOL;
2588 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2589 }
2590 break;
2591
2592 // Operation with two number arguments
2593 case ISN_OPNR:
2594 case ISN_COMPARENR:
2595 {
2596 typval_T *tv1 = STACK_TV_BOT(-2);
2597 typval_T *tv2 = STACK_TV_BOT(-1);
2598 varnumber_T arg1 = tv1->vval.v_number;
2599 varnumber_T arg2 = tv2->vval.v_number;
2600 varnumber_T res;
2601
2602 switch (iptr->isn_arg.op.op_type)
2603 {
2604 case EXPR_MULT: res = arg1 * arg2; break;
2605 case EXPR_DIV: res = arg1 / arg2; break;
2606 case EXPR_REM: res = arg1 % arg2; break;
2607 case EXPR_SUB: res = arg1 - arg2; break;
2608 case EXPR_ADD: res = arg1 + arg2; break;
2609
2610 case EXPR_EQUAL: res = arg1 == arg2; break;
2611 case EXPR_NEQUAL: res = arg1 != arg2; break;
2612 case EXPR_GREATER: res = arg1 > arg2; break;
2613 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2614 case EXPR_SMALLER: res = arg1 < arg2; break;
2615 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2616 default: res = 0; break;
2617 }
2618
2619 --ectx.ec_stack.ga_len;
2620 if (iptr->isn_type == ISN_COMPARENR)
2621 {
2622 tv1->v_type = VAR_BOOL;
2623 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2624 }
2625 else
2626 tv1->vval.v_number = res;
2627 }
2628 break;
2629
2630 // Computation with two float arguments
2631 case ISN_OPFLOAT:
2632 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002633#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 {
2635 typval_T *tv1 = STACK_TV_BOT(-2);
2636 typval_T *tv2 = STACK_TV_BOT(-1);
2637 float_T arg1 = tv1->vval.v_float;
2638 float_T arg2 = tv2->vval.v_float;
2639 float_T res = 0;
2640 int cmp = FALSE;
2641
2642 switch (iptr->isn_arg.op.op_type)
2643 {
2644 case EXPR_MULT: res = arg1 * arg2; break;
2645 case EXPR_DIV: res = arg1 / arg2; break;
2646 case EXPR_SUB: res = arg1 - arg2; break;
2647 case EXPR_ADD: res = arg1 + arg2; break;
2648
2649 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2650 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2651 case EXPR_GREATER: cmp = arg1 > arg2; break;
2652 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2653 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2654 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2655 default: cmp = 0; break;
2656 }
2657 --ectx.ec_stack.ga_len;
2658 if (iptr->isn_type == ISN_COMPAREFLOAT)
2659 {
2660 tv1->v_type = VAR_BOOL;
2661 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2662 }
2663 else
2664 tv1->vval.v_float = res;
2665 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002666#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667 break;
2668
2669 case ISN_COMPARELIST:
2670 {
2671 typval_T *tv1 = STACK_TV_BOT(-2);
2672 typval_T *tv2 = STACK_TV_BOT(-1);
2673 list_T *arg1 = tv1->vval.v_list;
2674 list_T *arg2 = tv2->vval.v_list;
2675 int cmp = FALSE;
2676 int ic = iptr->isn_arg.op.op_ic;
2677
2678 switch (iptr->isn_arg.op.op_type)
2679 {
2680 case EXPR_EQUAL: cmp =
2681 list_equal(arg1, arg2, ic, FALSE); break;
2682 case EXPR_NEQUAL: cmp =
2683 !list_equal(arg1, arg2, ic, FALSE); break;
2684 case EXPR_IS: cmp = arg1 == arg2; break;
2685 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2686 default: cmp = 0; break;
2687 }
2688 --ectx.ec_stack.ga_len;
2689 clear_tv(tv1);
2690 clear_tv(tv2);
2691 tv1->v_type = VAR_BOOL;
2692 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2693 }
2694 break;
2695
2696 case ISN_COMPAREBLOB:
2697 {
2698 typval_T *tv1 = STACK_TV_BOT(-2);
2699 typval_T *tv2 = STACK_TV_BOT(-1);
2700 blob_T *arg1 = tv1->vval.v_blob;
2701 blob_T *arg2 = tv2->vval.v_blob;
2702 int cmp = FALSE;
2703
2704 switch (iptr->isn_arg.op.op_type)
2705 {
2706 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2707 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2708 case EXPR_IS: cmp = arg1 == arg2; break;
2709 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2710 default: cmp = 0; break;
2711 }
2712 --ectx.ec_stack.ga_len;
2713 clear_tv(tv1);
2714 clear_tv(tv2);
2715 tv1->v_type = VAR_BOOL;
2716 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2717 }
2718 break;
2719
2720 // TODO: handle separately
2721 case ISN_COMPARESTRING:
2722 case ISN_COMPAREDICT:
2723 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 case ISN_COMPAREANY:
2725 {
2726 typval_T *tv1 = STACK_TV_BOT(-2);
2727 typval_T *tv2 = STACK_TV_BOT(-1);
2728 exptype_T exptype = iptr->isn_arg.op.op_type;
2729 int ic = iptr->isn_arg.op.op_ic;
2730
Bram Moolenaareb26f432020-09-14 16:50:05 +02002731 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 typval_compare(tv1, tv2, exptype, ic);
2733 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 --ectx.ec_stack.ga_len;
2735 }
2736 break;
2737
2738 case ISN_ADDLIST:
2739 case ISN_ADDBLOB:
2740 {
2741 typval_T *tv1 = STACK_TV_BOT(-2);
2742 typval_T *tv2 = STACK_TV_BOT(-1);
2743
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002744 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 if (iptr->isn_type == ISN_ADDLIST)
2746 eval_addlist(tv1, tv2);
2747 else
2748 eval_addblob(tv1, tv2);
2749 clear_tv(tv2);
2750 --ectx.ec_stack.ga_len;
2751 }
2752 break;
2753
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002754 case ISN_LISTAPPEND:
2755 {
2756 typval_T *tv1 = STACK_TV_BOT(-2);
2757 typval_T *tv2 = STACK_TV_BOT(-1);
2758 list_T *l = tv1->vval.v_list;
2759
2760 // add an item to a list
2761 if (l == NULL)
2762 {
2763 SOURCING_LNUM = iptr->isn_lnum;
2764 emsg(_(e_cannot_add_to_null_list));
2765 goto on_error;
2766 }
2767 if (list_append_tv(l, tv2) == FAIL)
2768 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002769 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002770 --ectx.ec_stack.ga_len;
2771 }
2772 break;
2773
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002774 case ISN_BLOBAPPEND:
2775 {
2776 typval_T *tv1 = STACK_TV_BOT(-2);
2777 typval_T *tv2 = STACK_TV_BOT(-1);
2778 blob_T *b = tv1->vval.v_blob;
2779 int error = FALSE;
2780 varnumber_T n;
2781
2782 // add a number to a blob
2783 if (b == NULL)
2784 {
2785 SOURCING_LNUM = iptr->isn_lnum;
2786 emsg(_(e_cannot_add_to_null_blob));
2787 goto on_error;
2788 }
2789 n = tv_get_number_chk(tv2, &error);
2790 if (error)
2791 goto on_error;
2792 ga_append(&b->bv_ga, (int)n);
2793 --ectx.ec_stack.ga_len;
2794 }
2795 break;
2796
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 // Computation with two arguments of unknown type
2798 case ISN_OPANY:
2799 {
2800 typval_T *tv1 = STACK_TV_BOT(-2);
2801 typval_T *tv2 = STACK_TV_BOT(-1);
2802 varnumber_T n1, n2;
2803#ifdef FEAT_FLOAT
2804 float_T f1 = 0, f2 = 0;
2805#endif
2806 int error = FALSE;
2807
2808 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2809 {
2810 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2811 {
2812 eval_addlist(tv1, tv2);
2813 clear_tv(tv2);
2814 --ectx.ec_stack.ga_len;
2815 break;
2816 }
2817 else if (tv1->v_type == VAR_BLOB
2818 && tv2->v_type == VAR_BLOB)
2819 {
2820 eval_addblob(tv1, tv2);
2821 clear_tv(tv2);
2822 --ectx.ec_stack.ga_len;
2823 break;
2824 }
2825 }
2826#ifdef FEAT_FLOAT
2827 if (tv1->v_type == VAR_FLOAT)
2828 {
2829 f1 = tv1->vval.v_float;
2830 n1 = 0;
2831 }
2832 else
2833#endif
2834 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002835 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 n1 = tv_get_number_chk(tv1, &error);
2837 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002838 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839#ifdef FEAT_FLOAT
2840 if (tv2->v_type == VAR_FLOAT)
2841 f1 = n1;
2842#endif
2843 }
2844#ifdef FEAT_FLOAT
2845 if (tv2->v_type == VAR_FLOAT)
2846 {
2847 f2 = tv2->vval.v_float;
2848 n2 = 0;
2849 }
2850 else
2851#endif
2852 {
2853 n2 = tv_get_number_chk(tv2, &error);
2854 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002855 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856#ifdef FEAT_FLOAT
2857 if (tv1->v_type == VAR_FLOAT)
2858 f2 = n2;
2859#endif
2860 }
2861#ifdef FEAT_FLOAT
2862 // if there is a float on either side the result is a float
2863 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2864 {
2865 switch (iptr->isn_arg.op.op_type)
2866 {
2867 case EXPR_MULT: f1 = f1 * f2; break;
2868 case EXPR_DIV: f1 = f1 / f2; break;
2869 case EXPR_SUB: f1 = f1 - f2; break;
2870 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002871 default: SOURCING_LNUM = iptr->isn_lnum;
2872 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002873 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 }
2875 clear_tv(tv1);
2876 clear_tv(tv2);
2877 tv1->v_type = VAR_FLOAT;
2878 tv1->vval.v_float = f1;
2879 --ectx.ec_stack.ga_len;
2880 }
2881 else
2882#endif
2883 {
2884 switch (iptr->isn_arg.op.op_type)
2885 {
2886 case EXPR_MULT: n1 = n1 * n2; break;
2887 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2888 case EXPR_SUB: n1 = n1 - n2; break;
2889 case EXPR_ADD: n1 = n1 + n2; break;
2890 default: n1 = num_modulus(n1, n2); break;
2891 }
2892 clear_tv(tv1);
2893 clear_tv(tv2);
2894 tv1->v_type = VAR_NUMBER;
2895 tv1->vval.v_number = n1;
2896 --ectx.ec_stack.ga_len;
2897 }
2898 }
2899 break;
2900
2901 case ISN_CONCAT:
2902 {
2903 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2904 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2905 char_u *res;
2906
2907 res = concat_str(str1, str2);
2908 clear_tv(STACK_TV_BOT(-2));
2909 clear_tv(STACK_TV_BOT(-1));
2910 --ectx.ec_stack.ga_len;
2911 STACK_TV_BOT(-1)->vval.v_string = res;
2912 }
2913 break;
2914
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002915 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002916 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002917 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002918 int is_slice = iptr->isn_type == ISN_STRSLICE;
2919 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002920 char_u *res;
2921
2922 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002923 // string slice: string is at stack-3, first index at
2924 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002925 if (is_slice)
2926 {
2927 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002928 n1 = tv->vval.v_number;
2929 }
2930
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002931 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002932 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002933
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002934 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002935 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002936 if (is_slice)
2937 // Slice: Select the characters from the string
2938 res = string_slice(tv->vval.v_string, n1, n2);
2939 else
2940 // Index: The resulting variable is a string of a
2941 // single character. If the index is too big or
2942 // negative the result is empty.
2943 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002944 vim_free(tv->vval.v_string);
2945 tv->vval.v_string = res;
2946 }
2947 break;
2948
2949 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002950 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 {
Bram Moolenaared591872020-08-15 22:14:53 +02002952 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002954 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955
2956 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002957 // list slice: list is at stack-3, indexes at stack-2 and
2958 // stack-1
2959 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 list = tv->vval.v_list;
2961
2962 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002963 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002965
2966 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 {
Bram Moolenaared591872020-08-15 22:14:53 +02002968 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002969 n1 = tv->vval.v_number;
2970 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 }
Bram Moolenaared591872020-08-15 22:14:53 +02002972
2973 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002974 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002975 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002976 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2977 == FAIL)
2978 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 }
2980 break;
2981
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002982 case ISN_ANYINDEX:
2983 case ISN_ANYSLICE:
2984 {
2985 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2986 typval_T *var1, *var2;
2987 int res;
2988
2989 // index: composite is at stack-2, index at stack-1
2990 // slice: composite is at stack-3, indexes at stack-2 and
2991 // stack-1
2992 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002993 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002994 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2995 goto on_error;
2996 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2997 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2998 res = eval_index_inner(tv, is_slice,
2999 var1, var2, NULL, -1, TRUE);
3000 clear_tv(var1);
3001 if (is_slice)
3002 clear_tv(var2);
3003 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3004 if (res == FAIL)
3005 goto on_error;
3006 }
3007 break;
3008
Bram Moolenaar9af78762020-06-16 11:34:42 +02003009 case ISN_SLICE:
3010 {
3011 list_T *list;
3012 int count = iptr->isn_arg.number;
3013
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003014 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003015 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003016 list = tv->vval.v_list;
3017
3018 // no error for short list, expect it to be checked earlier
3019 if (list != NULL && list->lv_len >= count)
3020 {
3021 list_T *newlist = list_slice(list,
3022 count, list->lv_len - 1);
3023
3024 if (newlist != NULL)
3025 {
3026 list_unref(list);
3027 tv->vval.v_list = newlist;
3028 ++newlist->lv_refcount;
3029 }
3030 }
3031 }
3032 break;
3033
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003034 case ISN_GETITEM:
3035 {
3036 listitem_T *li;
3037 int index = iptr->isn_arg.number;
3038
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003039 // Get list item: list is at stack-1, push item.
3040 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003041 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003042 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003043
3044 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3045 goto failed;
3046 ++ectx.ec_stack.ga_len;
3047 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
3048 }
3049 break;
3050
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 case ISN_MEMBER:
3052 {
3053 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003054 char_u *key;
3055 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003056 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003057
3058 // dict member: dict is at stack-2, key at stack-1
3059 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003060 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003061 dict = tv->vval.v_dict;
3062
3063 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003064 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003065 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003066 if (key == NULL)
3067 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003068
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003069 if ((di = dict_find(dict, key, -1)) == NULL)
3070 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003071 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003072 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003073
3074 // If :silent! is used we will continue, make sure the
3075 // stack contents makes sense.
3076 clear_tv(tv);
3077 --ectx.ec_stack.ga_len;
3078 tv = STACK_TV_BOT(-1);
3079 clear_tv(tv);
3080 tv->v_type = VAR_NUMBER;
3081 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003082 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003083 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003084 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003085 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003086 // Clear the dict only after getting the item, to avoid
3087 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003088 tv = STACK_TV_BOT(-1);
3089 temp_tv = *tv;
3090 copy_tv(&di->di_tv, tv);
3091 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003092 }
3093 break;
3094
3095 // dict member with string key
3096 case ISN_STRINGMEMBER:
3097 {
3098 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003100 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101
3102 tv = STACK_TV_BOT(-1);
3103 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3104 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003105 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003107 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 }
3109 dict = tv->vval.v_dict;
3110
3111 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3112 == NULL)
3113 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003114 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003116 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003118 // Clear the dict after getting the item, to avoid that it
3119 // make the item invalid.
3120 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003122 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 }
3124 break;
3125
3126 case ISN_NEGATENR:
3127 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003128 if (tv->v_type != VAR_NUMBER
3129#ifdef FEAT_FLOAT
3130 && tv->v_type != VAR_FLOAT
3131#endif
3132 )
3133 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003134 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003135 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003136 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003137 }
3138#ifdef FEAT_FLOAT
3139 if (tv->v_type == VAR_FLOAT)
3140 tv->vval.v_float = -tv->vval.v_float;
3141 else
3142#endif
3143 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 break;
3145
3146 case ISN_CHECKNR:
3147 {
3148 int error = FALSE;
3149
3150 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003151 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003153 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 (void)tv_get_number_chk(tv, &error);
3155 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003156 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 }
3158 break;
3159
3160 case ISN_CHECKTYPE:
3161 {
3162 checktype_T *ct = &iptr->isn_arg.type;
3163
3164 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003165 SOURCING_LNUM = iptr->isn_lnum;
3166 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
3167 goto on_error;
3168
3169 // number 0 is FALSE, number 1 is TRUE
3170 if (tv->v_type == VAR_NUMBER
3171 && ct->ct_type->tt_type == VAR_BOOL
3172 && (tv->vval.v_number == 0
3173 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003175 tv->v_type = VAR_BOOL;
3176 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003177 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 }
3179 }
3180 break;
3181
Bram Moolenaar9af78762020-06-16 11:34:42 +02003182 case ISN_CHECKLEN:
3183 {
3184 int min_len = iptr->isn_arg.checklen.cl_min_len;
3185 list_T *list = NULL;
3186
3187 tv = STACK_TV_BOT(-1);
3188 if (tv->v_type == VAR_LIST)
3189 list = tv->vval.v_list;
3190 if (list == NULL || list->lv_len < min_len
3191 || (list->lv_len > min_len
3192 && !iptr->isn_arg.checklen.cl_more_OK))
3193 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003194 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003195 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003196 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003197 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003198 }
3199 }
3200 break;
3201
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003202 case ISN_SETTYPE:
3203 {
3204 checktype_T *ct = &iptr->isn_arg.type;
3205
3206 tv = STACK_TV_BOT(-1);
3207 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3208 {
3209 free_type(tv->vval.v_dict->dv_type);
3210 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3211 }
3212 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3213 {
3214 free_type(tv->vval.v_list->lv_type);
3215 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3216 }
3217 }
3218 break;
3219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003221 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 {
3223 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003224 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225
3226 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003227 if (iptr->isn_type == ISN_2BOOL)
3228 {
3229 n = tv2bool(tv);
3230 if (iptr->isn_arg.number) // invert
3231 n = !n;
3232 }
3233 else
3234 {
3235 SOURCING_LNUM = iptr->isn_lnum;
3236 n = tv_get_bool_chk(tv, &error);
3237 if (error)
3238 goto on_error;
3239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 clear_tv(tv);
3241 tv->v_type = VAR_BOOL;
3242 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3243 }
3244 break;
3245
3246 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003247 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003248 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003249 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3250 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3251 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 break;
3253
Bram Moolenaar08597872020-12-10 19:43:40 +01003254 case ISN_RANGE:
3255 {
3256 exarg_T ea;
3257 char *errormsg;
3258
3259 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3260 goto failed;
3261 ++ectx.ec_stack.ga_len;
3262 tv = STACK_TV_BOT(-1);
Bram Moolenaarece0b872021-01-08 20:40:45 +01003263 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003264 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003265 ea.addr_type = ADDR_LINES;
3266 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003267 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003268 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003269 goto on_error;
Bram Moolenaar08597872020-12-10 19:43:40 +01003270 if (ea.addr_count == 0)
3271 tv->vval.v_number = curwin->w_cursor.lnum;
3272 else
3273 tv->vval.v_number = ea.line2;
3274 }
3275 break;
3276
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003277 case ISN_PUT:
3278 {
3279 int regname = iptr->isn_arg.put.put_regname;
3280 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3281 char_u *expr = NULL;
3282 int dir = FORWARD;
3283
3284 if (regname == '=')
3285 {
3286 tv = STACK_TV_BOT(-1);
3287 if (tv->v_type == VAR_STRING)
3288 expr = tv->vval.v_string;
3289 else
3290 {
3291 expr = typval_tostring(tv); // allocates value
3292 clear_tv(tv);
3293 }
3294 --ectx.ec_stack.ga_len;
3295 }
Bram Moolenaar08597872020-12-10 19:43:40 +01003296 if (lnum < -2)
3297 {
3298 // line number was put on the stack by ISN_RANGE
3299 tv = STACK_TV_BOT(-1);
3300 curwin->w_cursor.lnum = tv->vval.v_number;
3301 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3302 dir = BACKWARD;
3303 --ectx.ec_stack.ga_len;
3304 }
3305 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003306 // :put! above cursor
3307 dir = BACKWARD;
3308 else if (lnum >= 0)
3309 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
3310 check_cursor();
3311 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3312 vim_free(expr);
3313 }
3314 break;
3315
Bram Moolenaar02194d22020-10-24 23:08:38 +02003316 case ISN_CMDMOD:
3317 save_cmdmod = cmdmod;
3318 restore_cmdmod = TRUE;
3319 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3320 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003321 break;
3322
Bram Moolenaar02194d22020-10-24 23:08:38 +02003323 case ISN_CMDMOD_REV:
3324 // filter regprog is owned by the instruction, don't free it
3325 cmdmod.cmod_filter_regmatch.regprog = NULL;
3326 undo_cmdmod(&cmdmod);
3327 cmdmod = save_cmdmod;
3328 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003329 break;
3330
Bram Moolenaar792f7862020-11-23 08:31:18 +01003331 case ISN_UNPACK:
3332 {
3333 int count = iptr->isn_arg.unpack.unp_count;
3334 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3335 list_T *l;
3336 listitem_T *li;
3337 int i;
3338
3339 // Check there is a valid list to unpack.
3340 tv = STACK_TV_BOT(-1);
3341 if (tv->v_type != VAR_LIST)
3342 {
3343 SOURCING_LNUM = iptr->isn_lnum;
3344 emsg(_(e_for_argument_must_be_sequence_of_lists));
3345 goto on_error;
3346 }
3347 l = tv->vval.v_list;
3348 if (l == NULL
3349 || l->lv_len < (semicolon ? count - 1 : count))
3350 {
3351 SOURCING_LNUM = iptr->isn_lnum;
3352 emsg(_(e_list_value_does_not_have_enough_items));
3353 goto on_error;
3354 }
3355 else if (!semicolon && l->lv_len > count)
3356 {
3357 SOURCING_LNUM = iptr->isn_lnum;
3358 emsg(_(e_list_value_has_more_items_than_targets));
3359 goto on_error;
3360 }
3361
3362 CHECK_LIST_MATERIALIZE(l);
3363 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3364 goto failed;
3365 ectx.ec_stack.ga_len += count - 1;
3366
3367 // Variable after semicolon gets a list with the remaining
3368 // items.
3369 if (semicolon)
3370 {
3371 list_T *rem_list =
3372 list_alloc_with_items(l->lv_len - count + 1);
3373
3374 if (rem_list == NULL)
3375 goto failed;
3376 tv = STACK_TV_BOT(-count);
3377 tv->vval.v_list = rem_list;
3378 ++rem_list->lv_refcount;
3379 tv->v_lock = 0;
3380 li = l->lv_first;
3381 for (i = 0; i < count - 1; ++i)
3382 li = li->li_next;
3383 for (i = 0; li != NULL; ++i)
3384 {
3385 list_set_item(rem_list, i, &li->li_tv);
3386 li = li->li_next;
3387 }
3388 --count;
3389 }
3390
3391 // Produce the values in reverse order, first item last.
3392 li = l->lv_first;
3393 for (i = 0; i < count; ++i)
3394 {
3395 tv = STACK_TV_BOT(-i - 1);
3396 copy_tv(&li->li_tv, tv);
3397 li = li->li_next;
3398 }
3399
3400 list_unref(l);
3401 }
3402 break;
3403
Bram Moolenaar389df252020-07-09 21:20:47 +02003404 case ISN_SHUFFLE:
3405 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003406 typval_T tmp_tv;
3407 int item = iptr->isn_arg.shuffle.shfl_item;
3408 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003409
3410 tmp_tv = *STACK_TV_BOT(-item);
3411 for ( ; up > 0 && item > 1; --up)
3412 {
3413 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3414 --item;
3415 }
3416 *STACK_TV_BOT(-item) = tmp_tv;
3417 }
3418 break;
3419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 case ISN_DROP:
3421 --ectx.ec_stack.ga_len;
3422 clear_tv(STACK_TV_BOT(0));
3423 break;
3424 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003425 continue;
3426
Bram Moolenaard032f342020-07-18 18:13:02 +02003427func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003428 // Restore previous function. If the frame pointer is where we started
3429 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003430 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003431 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003432
Bram Moolenaard032f342020-07-18 18:13:02 +02003433 if (func_return(&ectx) == FAIL)
3434 // only fails when out of memory
3435 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003436 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003437
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003438on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003439 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003440 // If "emsg_silent" is set then ignore the error, unless it was set
3441 // when calling the function.
3442 if (did_emsg_cumul + did_emsg == did_emsg_before
3443 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003444 continue;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003445on_fatal_error:
3446 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003447 // If we are not inside a try-catch started here, abort execution.
3448 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003449 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 }
3451
3452done:
3453 // function finished, get result from the stack.
3454 tv = STACK_TV_BOT(-1);
3455 *rettv = *tv;
3456 tv->v_type = VAR_UNKNOWN;
3457 ret = OK;
3458
3459failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003460 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003461 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003462 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003463
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003464 // Deal with any remaining closures, they may be in use somewhere.
3465 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003466 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003467 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003468 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3469 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003470
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003471 estack_pop();
3472 current_sctx = save_current_sctx;
3473
Bram Moolenaar352134b2020-10-17 22:04:08 +02003474 if (*msg_list != NULL && saved_msg_list != NULL)
3475 {
3476 msglist_T **plist = saved_msg_list;
3477
3478 // Append entries from the current msg_list (uncaught exceptions) to
3479 // the saved msg_list.
3480 while (*plist != NULL)
3481 plist = &(*plist)->next;
3482
3483 *plist = *msg_list;
3484 }
3485 msg_list = saved_msg_list;
3486
Bram Moolenaar02194d22020-10-24 23:08:38 +02003487 if (restore_cmdmod)
3488 {
3489 cmdmod.cmod_filter_regmatch.regprog = NULL;
3490 undo_cmdmod(&cmdmod);
3491 cmdmod = save_cmdmod;
3492 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003493 emsg_silent_def = save_emsg_silent_def;
3494 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003495
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003496failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003497 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3499 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003500
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003502 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003503
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003504 // Not sure if this is necessary.
3505 suppress_errthrow = save_suppress_errthrow;
3506
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003507 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003508 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003509 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003510 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 return ret;
3512}
3513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003515 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003516 * We don't really need this at runtime, but we do have tests that require it,
3517 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 */
3519 void
3520ex_disassemble(exarg_T *eap)
3521{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003522 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003523 char_u *fname;
3524 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 dfunc_T *dfunc;
3526 isn_T *instr;
3527 int current;
3528 int line_idx = 0;
3529 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003530 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003532 if (STRNCMP(arg, "<lambda>", 8) == 0)
3533 {
3534 arg += 8;
3535 (void)getdigits(&arg);
3536 fname = vim_strnsave(eap->arg, arg - eap->arg);
3537 }
3538 else
3539 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003540 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003541 if (fname == NULL)
3542 {
3543 semsg(_(e_invarg2), eap->arg);
3544 return;
3545 }
3546
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003547 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003548 if (ufunc == NULL)
3549 {
3550 char_u *p = untrans_function_name(fname);
3551
3552 if (p != NULL)
3553 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003554 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003555 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003556 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 if (ufunc == NULL)
3558 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003559 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 return;
3561 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003562 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003563 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3564 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003565 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003567 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 return;
3569 }
3570 if (ufunc->uf_name_exp != NULL)
3571 msg((char *)ufunc->uf_name_exp);
3572 else
3573 msg((char *)ufunc->uf_name);
3574
3575 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3576 instr = dfunc->df_instr;
3577 for (current = 0; current < dfunc->df_instr_count; ++current)
3578 {
3579 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003580 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581
3582 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3583 {
3584 if (current > prev_current)
3585 {
3586 msg_puts("\n\n");
3587 prev_current = current;
3588 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003589 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3590 if (line != NULL)
3591 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 }
3593
3594 switch (iptr->isn_type)
3595 {
3596 case ISN_EXEC:
3597 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3598 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003599 case ISN_EXECCONCAT:
3600 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003601 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003602 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 case ISN_ECHO:
3604 {
3605 echo_T *echo = &iptr->isn_arg.echo;
3606
3607 smsg("%4d %s %d", current,
3608 echo->echo_with_white ? "ECHO" : "ECHON",
3609 echo->echo_count);
3610 }
3611 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003612 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003613 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003614 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003615 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003616 case ISN_ECHOMSG:
3617 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003618 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003619 break;
3620 case ISN_ECHOERR:
3621 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003622 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003623 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003625 case ISN_LOADOUTER:
3626 {
3627 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
3628
3629 if (iptr->isn_arg.number < 0)
3630 smsg("%4d LOAD%s arg[%lld]", current, add,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003631 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003632 + STACK_FRAME_SIZE));
3633 else
3634 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003635 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 break;
3638 case ISN_LOADV:
3639 smsg("%4d LOADV v:%s", current,
3640 get_vim_var_name(iptr->isn_arg.number));
3641 break;
3642 case ISN_LOADSCRIPT:
3643 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003644 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3645 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003647 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003649 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3650 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003651 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003652 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 }
3654 break;
3655 case ISN_LOADS:
3656 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003657 scriptitem_T *si = SCRIPT_ITEM(
3658 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659
3660 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003661 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 }
3663 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003664 case ISN_LOADAUTO:
3665 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3666 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 case ISN_LOADG:
3668 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3669 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003670 case ISN_LOADB:
3671 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3672 break;
3673 case ISN_LOADW:
3674 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3675 break;
3676 case ISN_LOADT:
3677 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3678 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003679 case ISN_LOADGDICT:
3680 smsg("%4d LOAD g:", current);
3681 break;
3682 case ISN_LOADBDICT:
3683 smsg("%4d LOAD b:", current);
3684 break;
3685 case ISN_LOADWDICT:
3686 smsg("%4d LOAD w:", current);
3687 break;
3688 case ISN_LOADTDICT:
3689 smsg("%4d LOAD t:", current);
3690 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 case ISN_LOADOPT:
3692 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3693 break;
3694 case ISN_LOADENV:
3695 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3696 break;
3697 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003698 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 break;
3700
3701 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003702 case ISN_STOREOUTER:
3703 {
3704 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
3705
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003706 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003707 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003708 (varnumber_T)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003709 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003710 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003711 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003712 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003714 case ISN_STOREV:
3715 smsg("%4d STOREV v:%s", current,
3716 get_vim_var_name(iptr->isn_arg.number));
3717 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003718 case ISN_STOREAUTO:
3719 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3720 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003722 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3723 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003724 case ISN_STOREB:
3725 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3726 break;
3727 case ISN_STOREW:
3728 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3729 break;
3730 case ISN_STORET:
3731 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3732 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003733 case ISN_STORES:
3734 {
3735 scriptitem_T *si = SCRIPT_ITEM(
3736 iptr->isn_arg.loadstore.ls_sid);
3737
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003738 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003739 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003740 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 break;
3742 case ISN_STORESCRIPT:
3743 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003744 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3745 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003747 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003749 smsg("%4d STORESCRIPT %s-%d in %s", current,
3750 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003751 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003752 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 }
3754 break;
3755 case ISN_STOREOPT:
3756 smsg("%4d STOREOPT &%s", current,
3757 iptr->isn_arg.storeopt.so_name);
3758 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003759 case ISN_STOREENV:
3760 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3761 break;
3762 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003763 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003764 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 case ISN_STORENR:
3766 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003767 iptr->isn_arg.storenr.stnr_val,
3768 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 break;
3770
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003771 case ISN_STOREINDEX:
3772 switch (iptr->isn_arg.vartype)
3773 {
3774 case VAR_LIST:
3775 smsg("%4d STORELIST", current);
3776 break;
3777 case VAR_DICT:
3778 smsg("%4d STOREDICT", current);
3779 break;
3780 case VAR_ANY:
3781 smsg("%4d STOREINDEX", current);
3782 break;
3783 default: break;
3784 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003785 break;
3786
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 // constants
3788 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003789 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003790 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 break;
3792 case ISN_PUSHBOOL:
3793 case ISN_PUSHSPEC:
3794 smsg("%4d PUSH %s", current,
3795 get_var_special_name(iptr->isn_arg.number));
3796 break;
3797 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003798#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003800#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 break;
3802 case ISN_PUSHS:
3803 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3804 break;
3805 case ISN_PUSHBLOB:
3806 {
3807 char_u *r;
3808 char_u numbuf[NUMBUFLEN];
3809 char_u *tofree;
3810
3811 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003812 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 vim_free(tofree);
3814 }
3815 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003816 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003817 {
3818 char *name = (char *)iptr->isn_arg.string;
3819
3820 smsg("%4d PUSHFUNC \"%s\"", current,
3821 name == NULL ? "[none]" : name);
3822 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003823 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003824 case ISN_PUSHCHANNEL:
3825#ifdef FEAT_JOB_CHANNEL
3826 {
3827 channel_T *channel = iptr->isn_arg.channel;
3828
3829 smsg("%4d PUSHCHANNEL %d", current,
3830 channel == NULL ? 0 : channel->ch_id);
3831 }
3832#endif
3833 break;
3834 case ISN_PUSHJOB:
3835#ifdef FEAT_JOB_CHANNEL
3836 {
3837 typval_T tv;
3838 char_u *name;
3839
3840 tv.v_type = VAR_JOB;
3841 tv.vval.v_job = iptr->isn_arg.job;
3842 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003843 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003844 }
3845#endif
3846 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 case ISN_PUSHEXC:
3848 smsg("%4d PUSH v:exception", current);
3849 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003850 case ISN_UNLET:
3851 smsg("%4d UNLET%s %s", current,
3852 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3853 iptr->isn_arg.unlet.ul_name);
3854 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003855 case ISN_UNLETENV:
3856 smsg("%4d UNLETENV%s $%s", current,
3857 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3858 iptr->isn_arg.unlet.ul_name);
3859 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003860 case ISN_UNLETINDEX:
3861 smsg("%4d UNLETINDEX", current);
3862 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003863 case ISN_LOCKCONST:
3864 smsg("%4d LOCKCONST", current);
3865 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003867 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003868 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 break;
3870 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003871 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003872 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 break;
3874
3875 // function call
3876 case ISN_BCALL:
3877 {
3878 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3879
3880 smsg("%4d BCALL %s(argc %d)", current,
3881 internal_func_name(cbfunc->cbf_idx),
3882 cbfunc->cbf_argcount);
3883 }
3884 break;
3885 case ISN_DCALL:
3886 {
3887 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3888 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3889 + cdfunc->cdf_idx;
3890
3891 smsg("%4d DCALL %s(argc %d)", current,
3892 df->df_ufunc->uf_name_exp != NULL
3893 ? df->df_ufunc->uf_name_exp
3894 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3895 }
3896 break;
3897 case ISN_UCALL:
3898 {
3899 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3900
3901 smsg("%4d UCALL %s(argc %d)", current,
3902 cufunc->cuf_name, cufunc->cuf_argcount);
3903 }
3904 break;
3905 case ISN_PCALL:
3906 {
3907 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3908
3909 smsg("%4d PCALL%s (argc %d)", current,
3910 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3911 }
3912 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003913 case ISN_PCALL_END:
3914 smsg("%4d PCALL end", current);
3915 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 case ISN_RETURN:
3917 smsg("%4d RETURN", current);
3918 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003919 case ISN_RETURN_ZERO:
3920 smsg("%4d RETURN 0", current);
3921 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 case ISN_FUNCREF:
3923 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003924 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003926 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003928 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929 }
3930 break;
3931
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003932 case ISN_NEWFUNC:
3933 {
3934 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3935
3936 smsg("%4d NEWFUNC %s %s", current,
3937 newfunc->nf_lambda, newfunc->nf_global);
3938 }
3939 break;
3940
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003941 case ISN_DEF:
3942 {
3943 char_u *name = iptr->isn_arg.string;
3944
3945 smsg("%4d DEF %s", current,
3946 name == NULL ? (char_u *)"" : name);
3947 }
3948 break;
3949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 case ISN_JUMP:
3951 {
3952 char *when = "?";
3953
3954 switch (iptr->isn_arg.jump.jump_when)
3955 {
3956 case JUMP_ALWAYS:
3957 when = "JUMP";
3958 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 case JUMP_AND_KEEP_IF_TRUE:
3960 when = "JUMP_AND_KEEP_IF_TRUE";
3961 break;
3962 case JUMP_IF_FALSE:
3963 when = "JUMP_IF_FALSE";
3964 break;
3965 case JUMP_AND_KEEP_IF_FALSE:
3966 when = "JUMP_AND_KEEP_IF_FALSE";
3967 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003968 case JUMP_IF_COND_FALSE:
3969 when = "JUMP_IF_COND_FALSE";
3970 break;
3971 case JUMP_IF_COND_TRUE:
3972 when = "JUMP_IF_COND_TRUE";
3973 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003975 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 iptr->isn_arg.jump.jump_where);
3977 }
3978 break;
3979
3980 case ISN_FOR:
3981 {
3982 forloop_T *forloop = &iptr->isn_arg.forloop;
3983
3984 smsg("%4d FOR $%d -> %d", current,
3985 forloop->for_idx, forloop->for_end);
3986 }
3987 break;
3988
3989 case ISN_TRY:
3990 {
3991 try_T *try = &iptr->isn_arg.try;
3992
3993 smsg("%4d TRY catch -> %d, finally -> %d", current,
3994 try->try_catch, try->try_finally);
3995 }
3996 break;
3997 case ISN_CATCH:
3998 // TODO
3999 smsg("%4d CATCH", current);
4000 break;
4001 case ISN_ENDTRY:
4002 smsg("%4d ENDTRY", current);
4003 break;
4004 case ISN_THROW:
4005 smsg("%4d THROW", current);
4006 break;
4007
4008 // expression operations on number
4009 case ISN_OPNR:
4010 case ISN_OPFLOAT:
4011 case ISN_OPANY:
4012 {
4013 char *what;
4014 char *ins;
4015
4016 switch (iptr->isn_arg.op.op_type)
4017 {
4018 case EXPR_MULT: what = "*"; break;
4019 case EXPR_DIV: what = "/"; break;
4020 case EXPR_REM: what = "%"; break;
4021 case EXPR_SUB: what = "-"; break;
4022 case EXPR_ADD: what = "+"; break;
4023 default: what = "???"; break;
4024 }
4025 switch (iptr->isn_type)
4026 {
4027 case ISN_OPNR: ins = "OPNR"; break;
4028 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4029 case ISN_OPANY: ins = "OPANY"; break;
4030 default: ins = "???"; break;
4031 }
4032 smsg("%4d %s %s", current, ins, what);
4033 }
4034 break;
4035
4036 case ISN_COMPAREBOOL:
4037 case ISN_COMPARESPECIAL:
4038 case ISN_COMPARENR:
4039 case ISN_COMPAREFLOAT:
4040 case ISN_COMPARESTRING:
4041 case ISN_COMPAREBLOB:
4042 case ISN_COMPARELIST:
4043 case ISN_COMPAREDICT:
4044 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 case ISN_COMPAREANY:
4046 {
4047 char *p;
4048 char buf[10];
4049 char *type;
4050
4051 switch (iptr->isn_arg.op.op_type)
4052 {
4053 case EXPR_EQUAL: p = "=="; break;
4054 case EXPR_NEQUAL: p = "!="; break;
4055 case EXPR_GREATER: p = ">"; break;
4056 case EXPR_GEQUAL: p = ">="; break;
4057 case EXPR_SMALLER: p = "<"; break;
4058 case EXPR_SEQUAL: p = "<="; break;
4059 case EXPR_MATCH: p = "=~"; break;
4060 case EXPR_IS: p = "is"; break;
4061 case EXPR_ISNOT: p = "isnot"; break;
4062 case EXPR_NOMATCH: p = "!~"; break;
4063 default: p = "???"; break;
4064 }
4065 STRCPY(buf, p);
4066 if (iptr->isn_arg.op.op_ic == TRUE)
4067 strcat(buf, "?");
4068 switch(iptr->isn_type)
4069 {
4070 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4071 case ISN_COMPARESPECIAL:
4072 type = "COMPARESPECIAL"; break;
4073 case ISN_COMPARENR: type = "COMPARENR"; break;
4074 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4075 case ISN_COMPARESTRING:
4076 type = "COMPARESTRING"; break;
4077 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4078 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4079 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4080 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4082 default: type = "???"; break;
4083 }
4084
4085 smsg("%4d %s %s", current, type, buf);
4086 }
4087 break;
4088
4089 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4090 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4091
4092 // expression operations
4093 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004094 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004095 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004096 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004097 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004098 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004099 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004100 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4101 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004102 case ISN_SLICE: smsg("%4d SLICE %lld",
4103 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004104 case ISN_GETITEM: smsg("%4d ITEM %lld",
4105 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004106 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4107 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 iptr->isn_arg.string); break;
4109 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4110
4111 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004112 case ISN_CHECKTYPE:
4113 {
4114 char *tofree;
4115
4116 smsg("%4d CHECKTYPE %s stack[%d]", current,
4117 type_name(iptr->isn_arg.type.ct_type, &tofree),
4118 iptr->isn_arg.type.ct_off);
4119 vim_free(tofree);
4120 break;
4121 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004122 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4123 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4124 iptr->isn_arg.checklen.cl_min_len);
4125 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004126 case ISN_SETTYPE:
4127 {
4128 char *tofree;
4129
4130 smsg("%4d SETTYPE %s", current,
4131 type_name(iptr->isn_arg.type.ct_type, &tofree));
4132 vim_free(tofree);
4133 break;
4134 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004135 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 case ISN_2BOOL: if (iptr->isn_arg.number)
4137 smsg("%4d INVERT (!val)", current);
4138 else
4139 smsg("%4d 2BOOL (!!val)", current);
4140 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004141 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004142 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004143 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004144 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004145 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004146 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004147 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4148 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004149 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004150 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4151 smsg("%4d PUT %c above range",
4152 current, iptr->isn_arg.put.put_regname);
4153 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4154 smsg("%4d PUT %c range",
4155 current, iptr->isn_arg.put.put_regname);
4156 else
4157 smsg("%4d PUT %c %ld", current,
4158 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004159 (long)iptr->isn_arg.put.put_lnum);
4160 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161
Bram Moolenaar02194d22020-10-24 23:08:38 +02004162 // TODO: summarize modifiers
4163 case ISN_CMDMOD:
4164 {
4165 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004166 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004167 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4168
4169 buf = alloc(len + 1);
4170 if (buf != NULL)
4171 {
4172 (void)produce_cmdmods(
4173 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4174 smsg("%4d CMDMOD %s", current, buf);
4175 vim_free(buf);
4176 }
4177 break;
4178 }
4179 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004180
Bram Moolenaar792f7862020-11-23 08:31:18 +01004181 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4182 iptr->isn_arg.unpack.unp_count,
4183 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4184 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004185 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4186 iptr->isn_arg.shuffle.shfl_item,
4187 iptr->isn_arg.shuffle.shfl_up);
4188 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189 case ISN_DROP: smsg("%4d DROP", current); break;
4190 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004191
4192 out_flush(); // output one line at a time
4193 ui_breakcheck();
4194 if (got_int)
4195 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197}
4198
4199/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004200 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 * list, etc. Mostly like what JavaScript does, except that empty list and
4202 * empty dictionary are FALSE.
4203 */
4204 int
4205tv2bool(typval_T *tv)
4206{
4207 switch (tv->v_type)
4208 {
4209 case VAR_NUMBER:
4210 return tv->vval.v_number != 0;
4211 case VAR_FLOAT:
4212#ifdef FEAT_FLOAT
4213 return tv->vval.v_float != 0.0;
4214#else
4215 break;
4216#endif
4217 case VAR_PARTIAL:
4218 return tv->vval.v_partial != NULL;
4219 case VAR_FUNC:
4220 case VAR_STRING:
4221 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4222 case VAR_LIST:
4223 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4224 case VAR_DICT:
4225 return tv->vval.v_dict != NULL
4226 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4227 case VAR_BOOL:
4228 case VAR_SPECIAL:
4229 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4230 case VAR_JOB:
4231#ifdef FEAT_JOB_CHANNEL
4232 return tv->vval.v_job != NULL;
4233#else
4234 break;
4235#endif
4236 case VAR_CHANNEL:
4237#ifdef FEAT_JOB_CHANNEL
4238 return tv->vval.v_channel != NULL;
4239#else
4240 break;
4241#endif
4242 case VAR_BLOB:
4243 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4244 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004245 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 case VAR_VOID:
4247 break;
4248 }
4249 return FALSE;
4250}
4251
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004252 void
4253emsg_using_string_as(typval_T *tv, int as_number)
4254{
4255 semsg(_(as_number ? e_using_string_as_number_str
4256 : e_using_string_as_bool_str),
4257 tv->vval.v_string == NULL
4258 ? (char_u *)"" : tv->vval.v_string);
4259}
4260
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261/*
4262 * If "tv" is a string give an error and return FAIL.
4263 */
4264 int
4265check_not_string(typval_T *tv)
4266{
4267 if (tv->v_type == VAR_STRING)
4268 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004269 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 clear_tv(tv);
4271 return FAIL;
4272 }
4273 return OK;
4274}
4275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276
4277#endif // FEAT_EVAL