blob: ca702f67d3ca6baa09a16a8886c17648d63eff6d [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
Bram Moolenaarab360522021-01-10 14:02:28 +010063 garray_T *ec_outer_up_stack; // ec_outer_stack one level up
64 int ec_outer_up_frame; // ec_outer_frame one level up
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010066 garray_T ec_trystack; // stack of trycmd_T values
67 int ec_in_catch; // when TRUE in catch or finally block
68
69 int ec_dfunc_idx; // current function index
70 isn_T *ec_instr; // array with instructions
71 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020072
73 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010074};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010075
76// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020077#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 +010078
Bram Moolenaar418f1df2020-08-12 21:34:49 +020079 void
80to_string_error(vartype_T vartype)
81{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020082 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020083}
84
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010086 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087 */
88 static int
89ufunc_argcount(ufunc_T *ufunc)
90{
91 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
92}
93
94/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010095 * Set the instruction index, depending on omitted arguments, where the default
96 * values are to be computed. If all optional arguments are present, start
97 * with the function body.
98 * The expression evaluation is at the start of the instructions:
99 * 0 -> EVAL default1
100 * STORE arg[-2]
101 * 1 -> EVAL default2
102 * STORE arg[-1]
103 * 2 -> function body
104 */
105 static void
106init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
107{
108 if (ufunc->uf_def_args.ga_len == 0)
109 ectx->ec_iidx = 0;
110 else
111 {
112 int defcount = ufunc->uf_args.ga_len - argcount;
113
114 // If there is a varargs argument defcount can be negative, no defaults
115 // to evaluate then.
116 if (defcount < 0)
117 defcount = 0;
118 ectx->ec_iidx = ufunc->uf_def_arg_idx[
119 ufunc->uf_def_args.ga_len - defcount];
120 }
121}
122
123/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200124 * Create a new list from "count" items at the bottom of the stack.
125 * When "count" is zero an empty list is added to the stack.
126 */
127 static int
128exe_newlist(int count, ectx_T *ectx)
129{
130 list_T *list = list_alloc_with_items(count);
131 int idx;
132 typval_T *tv;
133
134 if (list == NULL)
135 return FAIL;
136 for (idx = 0; idx < count; ++idx)
137 list_set_item(list, idx, STACK_TV_BOT(idx - count));
138
139 if (count > 0)
140 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200141 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200142 return FAIL;
143 else
144 ++ectx->ec_stack.ga_len;
145 tv = STACK_TV_BOT(-1);
146 tv->v_type = VAR_LIST;
147 tv->vval.v_list = list;
148 ++list->lv_refcount;
149 return OK;
150}
151
152/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100154 * This adds a stack frame and sets the instruction pointer to the start of the
155 * called function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100156 *
157 * Stack has:
158 * - current arguments (already there)
159 * - omitted optional argument (default values) added here
160 * - stack frame:
161 * - pointer to calling function
162 * - Index of next instruction in calling function
163 * - previous frame pointer
164 * - reserved space for local variables
165 */
166 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200167call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200169 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100170 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
171 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200172 int arg_to_add;
173 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200174 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200176 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100177
178 if (dfunc->df_deleted)
179 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100180 // don't use ufunc->uf_name, it may have been freed
181 emsg_funcname(e_func_deleted,
182 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183 return FAIL;
184 }
185
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200186 if (ufunc->uf_va_name != NULL)
187 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200188 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200189 // Stack at time of call with 2 varargs:
190 // normal_arg
191 // optional_arg
192 // vararg_1
193 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200194 // After creating the list:
195 // normal_arg
196 // optional_arg
197 // vararg-list
198 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200199 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200200 // After creating the list
201 // normal_arg
202 // (space for optional_arg)
203 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200204 vararg_count = argcount - ufunc->uf_args.ga_len;
205 if (vararg_count < 0)
206 vararg_count = 0;
207 else
208 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200209 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200210 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200211
212 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200213 }
214
Bram Moolenaarfe270812020-04-11 22:31:27 +0200215 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200216 if (arg_to_add < 0)
217 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200218 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200219 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200220 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200221 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200222 return FAIL;
223 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200224
225 // Reserve space for:
226 // - missing arguments
227 // - stack frame
228 // - local variables
229 // - if needed: a counter for number of closures created in
230 // ectx->ec_funcrefs.
231 varcount = dfunc->df_varcount + dfunc->df_has_closure;
232 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
233 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 return FAIL;
235
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100236 // If depth of calling is getting too high, don't execute the function.
237 if (funcdepth_increment() == FAIL)
238 return FAIL;
239
Bram Moolenaarfe270812020-04-11 22:31:27 +0200240 // Move the vararg-list to below the missing optional arguments.
241 if (vararg_count > 0 && arg_to_add > 0)
242 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100243
244 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200245 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200246 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200247 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248
249 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100250 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
251 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200252 STACK_TV_BOT(2)->vval.v_string = (void *)ectx->ec_outer_stack;
253 STACK_TV_BOT(3)->vval.v_number = ectx->ec_outer_frame;
254 STACK_TV_BOT(4)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +0100255 // TODO: save ec_outer_up_stack as well?
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200256 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257
258 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200259 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100260 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200261 if (dfunc->df_has_closure)
262 {
263 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
264
265 tv->v_type = VAR_NUMBER;
266 tv->vval.v_number = 0;
267 }
268 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100270 if (ufunc->uf_partial != NULL)
271 {
272 ectx->ec_outer_stack = ufunc->uf_partial->pt_ectx_stack;
273 ectx->ec_outer_frame = ufunc->uf_partial->pt_ectx_frame;
Bram Moolenaarab360522021-01-10 14:02:28 +0100274 ectx->ec_outer_up_stack = ufunc->uf_partial->pt_outer_stack;
275 ectx->ec_outer_up_frame = ufunc->uf_partial->pt_outer_frame;
276 }
277 else if (ufunc->uf_flags & FC_CLOSURE)
278 {
279 ectx->ec_outer_stack = &ectx->ec_stack;
280 ectx->ec_outer_frame = ectx->ec_frame_idx;
281 ectx->ec_outer_up_stack = ectx->ec_outer_stack;
282 ectx->ec_outer_up_frame = ectx->ec_outer_frame;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100283 }
284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285 // Set execution state to the start of the called function.
286 ectx->ec_dfunc_idx = cdf_idx;
287 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200288 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
289 if (entry != NULL)
290 {
291 // Set the script context to the script where the function was defined.
292 // TODO: save more than the SID?
293 entry->es_save_sid = current_sctx.sc_sid;
294 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
295 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100296
297 // Decide where to start execution, handles optional arguments.
298 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299
300 return OK;
301}
302
303// Get pointer to item in the stack.
304#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
305
306/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200307 * Used when returning from a function: Check if any closure is still
308 * referenced. If so then move the arguments and variables to a separate piece
309 * of stack to be used when the closure is called.
310 * When "free_arguments" is TRUE the arguments are to be freed.
311 * Returns FAIL when out of memory.
312 */
313 static int
314handle_closure_in_use(ectx_T *ectx, int free_arguments)
315{
316 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
317 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200318 int argcount;
319 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200320 int idx;
321 typval_T *tv;
322 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200323 garray_T *gap = &ectx->ec_funcrefs;
324 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200325
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200326 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200327 return OK; // function was freed
328 if (dfunc->df_has_closure == 0)
329 return OK; // no closures
330 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
331 closure_count = tv->vval.v_number;
332 if (closure_count == 0)
333 return OK; // no funcrefs created
334
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200335 argcount = ufunc_argcount(dfunc->df_ufunc);
336 top = ectx->ec_frame_idx - argcount;
337
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200338 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200339 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200340 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200341 partial_T *pt;
342 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200343
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200344 if (off < 0)
345 continue; // count is off or already done
346 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200347 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200348 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200349 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200350 int i;
351
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200352 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200353 // unreferenced on return.
354 for (i = 0; i < dfunc->df_varcount; ++i)
355 {
356 typval_T *stv = STACK_TV(ectx->ec_frame_idx
357 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200358 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200359 --refcount;
360 }
361 if (refcount > 1)
362 {
363 closure_in_use = TRUE;
364 break;
365 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200366 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200367 }
368
369 if (closure_in_use)
370 {
371 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
372 typval_T *stack;
373
374 // A closure is using the arguments and/or local variables.
375 // Move them to the called function.
376 if (funcstack == NULL)
377 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200378 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
379 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200380 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
381 funcstack->fs_ga.ga_data = stack;
382 if (stack == NULL)
383 {
384 vim_free(funcstack);
385 return FAIL;
386 }
387
388 // Move or copy the arguments.
389 for (idx = 0; idx < argcount; ++idx)
390 {
391 tv = STACK_TV(top + idx);
392 if (free_arguments)
393 {
394 *(stack + idx) = *tv;
395 tv->v_type = VAR_UNKNOWN;
396 }
397 else
398 copy_tv(tv, stack + idx);
399 }
400 // Move the local variables.
401 for (idx = 0; idx < dfunc->df_varcount; ++idx)
402 {
403 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200404
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200405 // A partial created for a local function, that is also used as a
406 // local variable, has a reference count for the variable, thus
407 // will never go down to zero. When all these refcounts are one
408 // then the funcstack is unused. We need to count how many we have
409 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200410 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
411 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200412 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200413
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200414 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200415 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
416 gap->ga_len - closure_count + i])
417 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200418 }
419
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200420 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200421 tv->v_type = VAR_UNKNOWN;
422 }
423
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200424 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200425 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200426 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
427 - closure_count + idx];
428 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200429 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200430 ++funcstack->fs_refcount;
431 pt->pt_funcstack = funcstack;
432 pt->pt_ectx_stack = &funcstack->fs_ga;
433 pt->pt_ectx_frame = ectx->ec_frame_idx - top;
Bram Moolenaarab360522021-01-10 14:02:28 +0100434 pt->pt_outer_stack = ectx->ec_outer_stack;
435 pt->pt_outer_frame = ectx->ec_outer_frame;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200436 }
437 }
438 }
439
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200440 for (idx = 0; idx < closure_count; ++idx)
441 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
442 - closure_count + idx]);
443 gap->ga_len -= closure_count;
444 if (gap->ga_len == 0)
445 ga_clear(gap);
446
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200447 return OK;
448}
449
450/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200451 * Called when a partial is freed or its reference count goes down to one. The
452 * funcstack may be the only reference to the partials in the local variables.
453 * Go over all of them, the funcref and can be freed if all partials
454 * referencing the funcstack have a reference count of one.
455 */
456 void
457funcstack_check_refcount(funcstack_T *funcstack)
458{
459 int i;
460 garray_T *gap = &funcstack->fs_ga;
461 int done = 0;
462
463 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
464 return;
465 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
466 {
467 typval_T *tv = ((typval_T *)gap->ga_data) + i;
468
469 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
470 && tv->vval.v_partial->pt_funcstack == funcstack
471 && tv->vval.v_partial->pt_refcount == 1)
472 ++done;
473 }
474 if (done == funcstack->fs_min_refcount)
475 {
476 typval_T *stack = gap->ga_data;
477
478 // All partials referencing the funcstack have a reference count of
479 // one, thus the funcstack is no longer of use.
480 for (i = 0; i < gap->ga_len; ++i)
481 clear_tv(stack + i);
482 vim_free(stack);
483 vim_free(funcstack);
484 }
485}
486
487/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100488 * Return from the current function.
489 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200490 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100491func_return(ectx_T *ectx)
492{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100493 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100494 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200495 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
496 + ectx->ec_dfunc_idx;
497 int argcount = ufunc_argcount(dfunc->df_ufunc);
498 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200499 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500
501 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200502 entry = estack_pop();
503 if (entry != NULL)
504 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100505
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200506 if (handle_closure_in_use(ectx, TRUE) == FAIL)
507 return FAIL;
508
509 // Clear the arguments.
510 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
511 clear_tv(STACK_TV(idx));
512
513 // Clear local variables and temp values, but not the return value.
514 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100515 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100517
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100518 // The return value should be on top of the stack. However, when aborting
519 // it may not be there and ec_frame_idx is the top of the stack.
520 ret_idx = ectx->ec_stack.ga_len - 1;
521 if (ret_idx == ectx->ec_frame_idx + 4)
522 ret_idx = 0;
523
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100524 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200525 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
526 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200527 ectx->ec_outer_stack =
528 (void *)STACK_TV(ectx->ec_frame_idx + 2)->vval.v_string;
529 ectx->ec_outer_frame = STACK_TV(ectx->ec_frame_idx + 3)->vval.v_number;
530 // restoring ec_frame_idx must be last
531 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 4)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
533 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100534
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100535 if (ret_idx > 0)
536 {
537 // Reset the stack to the position before the call, with a spot for the
538 // return value, moved there from above the frame.
539 ectx->ec_stack.ga_len = top + 1;
540 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
541 }
542 else
543 // Reset the stack to the position before the call.
544 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200545
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100546 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200547 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548}
549
550#undef STACK_TV
551
552/*
553 * Prepare arguments and rettv for calling a builtin or user function.
554 */
555 static int
556call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
557{
558 int idx;
559 typval_T *tv;
560
561 // Move arguments from bottom of the stack to argvars[] and add terminator.
562 for (idx = 0; idx < argcount; ++idx)
563 argvars[idx] = *STACK_TV_BOT(idx - argcount);
564 argvars[argcount].v_type = VAR_UNKNOWN;
565
566 // Result replaces the arguments on the stack.
567 if (argcount > 0)
568 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200569 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100570 return FAIL;
571 else
572 ++ectx->ec_stack.ga_len;
573
574 // Default return value is zero.
575 tv = STACK_TV_BOT(-1);
576 tv->v_type = VAR_NUMBER;
577 tv->vval.v_number = 0;
578
579 return OK;
580}
581
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200582// Ugly global to avoid passing the execution context around through many
583// layers.
584static ectx_T *current_ectx = NULL;
585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586/*
587 * Call a builtin function by index.
588 */
589 static int
590call_bfunc(int func_idx, int argcount, ectx_T *ectx)
591{
592 typval_T argvars[MAX_FUNC_ARGS];
593 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100594 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200595 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596
597 if (call_prepare(argcount, argvars, ectx) == FAIL)
598 return FAIL;
599
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200600 // Call the builtin function. Set "current_ectx" so that when it
601 // recursively invokes call_def_function() a closure context can be set.
602 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200604 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605
606 // Clear the arguments.
607 for (idx = 0; idx < argcount; ++idx)
608 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200609
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100610 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200611 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100612 return OK;
613}
614
615/*
616 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100617 * If the function is compiled this will add a stack frame and set the
618 * instruction pointer at the start of the function.
619 * Otherwise the function is called here.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100620 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 */
622 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100623call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624{
625 typval_T argvars[MAX_FUNC_ARGS];
626 funcexe_T funcexe;
627 int error;
628 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100629 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100630
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200631 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200632 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
633 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200634 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100635 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100636 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100637 if (error != FCERR_UNKNOWN)
638 {
639 if (error == FCERR_TOOMANY)
640 semsg(_(e_toomanyarg), ufunc->uf_name);
641 else
642 semsg(_(e_toofewarg), ufunc->uf_name);
643 return FAIL;
644 }
645
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100646 // The function has been compiled, can call it quickly. For a function
647 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100648 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100649 if (iptr != NULL)
650 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100651 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100652 iptr->isn_type = ISN_DCALL;
653 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
654 iptr->isn_arg.dfunc.cdf_argcount = argcount;
655 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100657 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658
659 if (call_prepare(argcount, argvars, ectx) == FAIL)
660 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200661 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100662 funcexe.evaluate = TRUE;
663
664 // Call the user function. Result goes in last position on the stack.
665 // TODO: add selfdict if there is one
666 error = call_user_func_check(ufunc, argcount, argvars,
667 STACK_TV_BOT(-1), &funcexe, NULL);
668
669 // Clear the arguments.
670 for (idx = 0; idx < argcount; ++idx)
671 clear_tv(&argvars[idx]);
672
673 if (error != FCERR_NONE)
674 {
675 user_func_error(error, ufunc->uf_name);
676 return FAIL;
677 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100678 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200679 // Error other than from calling the function itself.
680 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 return OK;
682}
683
684/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200685 * Return TRUE if an error was given or CTRL-C was pressed.
686 */
687 static int
688vim9_aborting(int prev_called_emsg)
689{
690 return called_emsg > prev_called_emsg || got_int || did_throw;
691}
692
693/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 * Execute a function by "name".
695 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100696 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 * Returns FAIL if not found without an error message.
698 */
699 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100700call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701{
702 ufunc_T *ufunc;
703
704 if (builtin_function(name, -1))
705 {
706 int func_idx = find_internal_func(name);
707
708 if (func_idx < 0)
709 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200710 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 return FAIL;
712 return call_bfunc(func_idx, argcount, ectx);
713 }
714
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200715 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200716
717 if (ufunc == NULL)
718 {
719 int called_emsg_before = called_emsg;
720
721 if (script_autoload(name, TRUE))
722 // loaded a package, search for the function again
723 ufunc = find_func(name, FALSE, NULL);
724 if (vim9_aborting(called_emsg_before))
725 return FAIL; // bail out if loading the script caused an error
726 }
727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100729 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100730
731 return FAIL;
732}
733
734 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200735call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200737 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200738 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100739 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200740 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741
742 if (tv->v_type == VAR_PARTIAL)
743 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200744 partial_T *pt = tv->vval.v_partial;
745 int i;
746
747 if (pt->pt_argc > 0)
748 {
749 // Make space for arguments from the partial, shift the "argcount"
750 // arguments up.
751 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
752 return FAIL;
753 for (i = 1; i <= argcount; ++i)
754 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
755 ectx->ec_stack.ga_len += pt->pt_argc;
756 argcount += pt->pt_argc;
757
758 // copy the arguments from the partial onto the stack
759 for (i = 0; i < pt->pt_argc; ++i)
760 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
761 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762
763 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200764 {
Bram Moolenaarab360522021-01-10 14:02:28 +0100765 int frame_idx = ectx->ec_frame_idx;
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200766 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
767
Bram Moolenaarab360522021-01-10 14:02:28 +0100768 if (ectx->ec_frame_idx != frame_idx)
769 {
770 // call_dfunc() added a stack frame, closure may need the
771 // function context where it was defined.
772 ectx->ec_outer_stack = pt->pt_ectx_stack;
773 ectx->ec_outer_frame = pt->pt_ectx_frame;
774 ectx->ec_outer_up_stack = pt->pt_outer_stack;
775 ectx->ec_outer_up_frame = pt->pt_outer_frame;
776 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200777
778 return ret;
779 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100780 name = pt->pt_name;
781 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200782 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200784 if (name != NULL)
785 {
786 char_u fname_buf[FLEN_FIXED + 1];
787 char_u *tofree = NULL;
788 int error = FCERR_NONE;
789 char_u *fname;
790
791 // May need to translate <SNR>123_ to K_SNR.
792 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
793 if (error != FCERR_NONE)
794 res = FAIL;
795 else
796 res = call_by_name(fname, argcount, ectx, NULL);
797 vim_free(tofree);
798 }
799
800 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100801 {
802 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200803 semsg(_(e_unknownfunc),
804 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805 return FAIL;
806 }
807 return OK;
808}
809
810/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200811 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
812 * TRUE.
813 */
814 static int
815error_if_locked(int lock, char *error)
816{
817 if (lock & (VAR_LOCKED | VAR_FIXED))
818 {
819 emsg(_(error));
820 return TRUE;
821 }
822 return FALSE;
823}
824
825/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100826 * Store "tv" in variable "name".
827 * This is for s: and g: variables.
828 */
829 static void
830store_var(char_u *name, typval_T *tv)
831{
832 funccal_entry_T entry;
833
834 save_funccal(&entry);
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100835 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100836 restore_funccal();
837}
838
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100839/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100840 * Convert "tv" to a string.
841 * Return FAIL if not allowed.
842 */
843 static int
844do_2string(typval_T *tv, int is_2string_any)
845{
846 if (tv->v_type != VAR_STRING)
847 {
848 char_u *str;
849
850 if (is_2string_any)
851 {
852 switch (tv->v_type)
853 {
854 case VAR_SPECIAL:
855 case VAR_BOOL:
856 case VAR_NUMBER:
857 case VAR_FLOAT:
858 case VAR_BLOB: break;
859 default: to_string_error(tv->v_type);
860 return FAIL;
861 }
862 }
863 str = typval_tostring(tv);
864 clear_tv(tv);
865 tv->v_type = VAR_STRING;
866 tv->vval.v_string = str;
867 }
868 return OK;
869}
870
871/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100872 * When the value of "sv" is a null list of dict, allocate it.
873 */
874 static void
875allocate_if_null(typval_T *tv)
876{
877 switch (tv->v_type)
878 {
879 case VAR_LIST:
880 if (tv->vval.v_list == NULL)
881 rettv_list_alloc(tv);
882 break;
883 case VAR_DICT:
884 if (tv->vval.v_dict == NULL)
885 rettv_dict_alloc(tv);
886 break;
887 default:
888 break;
889 }
890}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200891
Bram Moolenaare7525c52021-01-09 13:20:37 +0100892/*
893 * Return the character "str[index]" where "index" is the character index. If
894 * "index" is out of range NULL is returned.
895 */
896 char_u *
897char_from_string(char_u *str, varnumber_T index)
898{
899 size_t nbyte = 0;
900 varnumber_T nchar = index;
901 size_t slen;
902
903 if (str == NULL)
904 return NULL;
905 slen = STRLEN(str);
906
907 // do the same as for a list: a negative index counts from the end
908 if (index < 0)
909 {
910 int clen = 0;
911
912 for (nbyte = 0; nbyte < slen; ++clen)
913 nbyte += MB_CPTR2LEN(str + nbyte);
914 nchar = clen + index;
915 if (nchar < 0)
916 // unlike list: index out of range results in empty string
917 return NULL;
918 }
919
920 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
921 nbyte += MB_CPTR2LEN(str + nbyte);
922 if (nbyte >= slen)
923 return NULL;
924 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
925}
926
927/*
928 * Get the byte index for character index "idx" in string "str" with length
929 * "str_len".
930 * If going over the end return "str_len".
931 * If "idx" is negative count from the end, -1 is the last character.
932 * When going over the start return -1.
933 */
934 static long
935char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
936{
937 varnumber_T nchar = idx;
938 size_t nbyte = 0;
939
940 if (nchar >= 0)
941 {
942 while (nchar > 0 && nbyte < str_len)
943 {
944 nbyte += MB_CPTR2LEN(str + nbyte);
945 --nchar;
946 }
947 }
948 else
949 {
950 nbyte = str_len;
951 while (nchar < 0 && nbyte > 0)
952 {
953 --nbyte;
954 nbyte -= mb_head_off(str, str + nbyte);
955 ++nchar;
956 }
957 if (nchar < 0)
958 return -1;
959 }
960 return (long)nbyte;
961}
962
963/*
964 * Return the slice "str[first:last]" using character indexes.
965 * Return NULL when the result is empty.
966 */
967 char_u *
968string_slice(char_u *str, varnumber_T first, varnumber_T last)
969{
970 long start_byte, end_byte;
971 size_t slen;
972
973 if (str == NULL)
974 return NULL;
975 slen = STRLEN(str);
976 start_byte = char_idx2byte(str, slen, first);
977 if (start_byte < 0)
978 start_byte = 0; // first index very negative: use zero
979 if (last == -1)
980 end_byte = (long)slen;
981 else
982 {
983 end_byte = char_idx2byte(str, slen, last);
984 if (end_byte >= 0 && end_byte < (long)slen)
985 // end index is inclusive
986 end_byte += MB_CPTR2LEN(str + end_byte);
987 }
988
989 if (start_byte >= (long)slen || end_byte <= start_byte)
990 return NULL;
991 return vim_strnsave(str + start_byte, end_byte - start_byte);
992}
993
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100994 static svar_T *
995get_script_svar(scriptref_T *sref, ectx_T *ectx)
996{
997 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
998 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
999 + ectx->ec_dfunc_idx;
1000 svar_T *sv;
1001
1002 if (sref->sref_seq != si->sn_script_seq)
1003 {
1004 // The script was reloaded after the function was
1005 // compiled, the script_idx may not be valid.
1006 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1007 dfunc->df_ufunc->uf_name_exp);
1008 return NULL;
1009 }
1010 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1011 if (!equal_type(sv->sv_type, sref->sref_type))
1012 {
1013 emsg(_(e_script_variable_type_changed));
1014 return NULL;
1015 }
1016 return sv;
1017}
1018
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001019/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 * Execute a function by "name".
1021 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001022 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023 */
1024 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001025call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001026{
Bram Moolenaared677f52020-08-12 16:38:10 +02001027 int called_emsg_before = called_emsg;
1028 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029
Bram Moolenaared677f52020-08-12 16:38:10 +02001030 res = call_by_name(name, argcount, ectx, iptr);
1031 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001033 dictitem_T *v;
1034
1035 v = find_var(name, NULL, FALSE);
1036 if (v == NULL)
1037 {
1038 semsg(_(e_unknownfunc), name);
1039 return FAIL;
1040 }
1041 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1042 {
1043 semsg(_(e_unknownfunc), name);
1044 return FAIL;
1045 }
1046 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001048 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049}
1050
1051/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001052 * When a function reference is used, fill a partial with the information
1053 * needed, especially when it is used as a closure.
1054 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001055 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001056fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1057{
1058 pt->pt_func = ufunc;
1059 pt->pt_refcount = 1;
1060
1061 if (pt->pt_func->uf_flags & FC_CLOSURE)
1062 {
1063 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1064 + ectx->ec_dfunc_idx;
1065
1066 // The closure needs to find arguments and local
1067 // variables in the current stack.
1068 pt->pt_ectx_stack = &ectx->ec_stack;
1069 pt->pt_ectx_frame = ectx->ec_frame_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01001070 pt->pt_outer_stack = ectx->ec_outer_stack;
1071 pt->pt_outer_frame = ectx->ec_outer_frame;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001072
1073 // If this function returns and the closure is still
1074 // being used, we need to make a copy of the context
1075 // (arguments and local variables). Store a reference
1076 // to the partial so we can handle that.
1077 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1078 {
1079 vim_free(pt);
1080 return FAIL;
1081 }
1082 // Extra variable keeps the count of closures created
1083 // in the current function call.
1084 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1085 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1086
1087 ((partial_T **)ectx->ec_funcrefs.ga_data)
1088 [ectx->ec_funcrefs.ga_len] = pt;
1089 ++pt->pt_refcount;
1090 ++ectx->ec_funcrefs.ga_len;
1091 }
1092 ++pt->pt_func->uf_refcount;
1093 return OK;
1094}
1095
1096/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 * Call a "def" function from old Vim script.
1098 * Return OK or FAIL.
1099 */
1100 int
1101call_def_function(
1102 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001103 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001105 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 typval_T *rettv) // return value
1107{
1108 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001109 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001110 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111 typval_T *tv;
1112 int idx;
1113 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001114 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001115 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001116 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001117 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001118 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001119 msglist_T **saved_msg_list = NULL;
1120 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001121 cmdmod_T save_cmdmod;
1122 int restore_cmdmod = FALSE;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001123 int save_emsg_silent_def = emsg_silent_def;
1124 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001125 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001126 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127
1128// Get pointer to item in the stack.
1129#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1130
1131// Get pointer to item at the bottom of the stack, -1 is the bottom.
1132#undef STACK_TV_BOT
1133#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1134
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001135// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001136#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 +01001137
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001138// Like STACK_TV_VAR but use the outer scope
1139#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
1140
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001141 if (ufunc->uf_def_status == UF_NOT_COMPILED
1142 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02001143 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001144 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001145 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001146 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001147 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001148 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001149 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001150
Bram Moolenaar09689a02020-05-09 22:50:08 +02001151 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001152 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001153 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1154 + ufunc->uf_dfunc_idx;
1155 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001156 {
1157 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001158 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001159 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001160 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001162 // If depth of calling is getting too high, don't execute the function.
1163 orig_funcdepth = funcdepth_get();
1164 if (funcdepth_increment() == FAIL)
1165 return FAIL;
1166
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001167 CLEAR_FIELD(ectx);
1168 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1169 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1170 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001171 {
1172 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001173 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001174 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001176 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001178 // Put arguments on the stack, but no more than what the function expects.
1179 // A lambda can be called with more arguments than it uses.
1180 for (idx = 0; idx < argc
1181 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1182 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001184 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001185 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
1186 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001187 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 copy_tv(&argv[idx], STACK_TV_BOT(0));
1189 ++ectx.ec_stack.ga_len;
1190 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001191
1192 // Turn varargs into a list. Empty list if no args.
1193 if (ufunc->uf_va_name != NULL)
1194 {
1195 int vararg_count = argc - ufunc->uf_args.ga_len;
1196
1197 if (vararg_count < 0)
1198 vararg_count = 0;
1199 else
1200 argc -= vararg_count;
1201 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001202 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001203
1204 // Check the type of the list items.
1205 tv = STACK_TV_BOT(-1);
1206 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001207 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001208 && ufunc->uf_va_type->tt_member != &t_any
1209 && tv->vval.v_list != NULL)
1210 {
1211 type_T *expected = ufunc->uf_va_type->tt_member;
1212 listitem_T *li = tv->vval.v_list->lv_first;
1213
1214 for (idx = 0; idx < vararg_count; ++idx)
1215 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001216 if (check_typval_type(expected, &li->li_tv,
1217 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001218 goto failed_early;
1219 li = li->li_next;
1220 }
1221 }
1222
Bram Moolenaar23e03252020-04-12 22:22:31 +02001223 if (defcount > 0)
1224 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001225 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001226 --ectx.ec_stack.ga_len;
1227 }
1228
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001229 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001230 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001231 if (defcount > 0)
1232 for (idx = 0; idx < defcount; ++idx)
1233 {
1234 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1235 ++ectx.ec_stack.ga_len;
1236 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001237 if (ufunc->uf_va_name != NULL)
1238 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239
1240 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001241 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1242 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001244 if (partial != NULL)
1245 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001246 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
1247 {
1248 // TODO: is this always the right way?
1249 ectx.ec_outer_stack = &current_ectx->ec_stack;
1250 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01001251 ectx.ec_outer_up_stack = current_ectx->ec_outer_stack;
1252 ectx.ec_outer_up_frame = current_ectx->ec_outer_frame;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001253 }
1254 else
1255 {
1256 ectx.ec_outer_stack = partial->pt_ectx_stack;
1257 ectx.ec_outer_frame = partial->pt_ectx_frame;
Bram Moolenaarab360522021-01-10 14:02:28 +01001258 ectx.ec_outer_up_stack = partial->pt_outer_stack;
1259 ectx.ec_outer_up_frame = partial->pt_outer_frame;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001260 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001261 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001262 else if (ufunc->uf_partial != NULL)
1263 {
1264 ectx.ec_outer_stack = ufunc->uf_partial->pt_ectx_stack;
1265 ectx.ec_outer_frame = ufunc->uf_partial->pt_ectx_frame;
Bram Moolenaarab360522021-01-10 14:02:28 +01001266 ectx.ec_outer_up_stack = ufunc->uf_partial->pt_outer_stack;
1267 ectx.ec_outer_up_frame = ufunc->uf_partial->pt_outer_frame;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001268 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001269
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 // dummy frame entries
1271 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1272 {
1273 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1274 ++ectx.ec_stack.ga_len;
1275 }
1276
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001277 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001278 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001279 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1280 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001282 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001283 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001284 ectx.ec_stack.ga_len += dfunc->df_varcount;
1285 if (dfunc->df_has_closure)
1286 {
1287 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1288 STACK_TV_VAR(idx)->vval.v_number = 0;
1289 ++ectx.ec_stack.ga_len;
1290 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001291
1292 ectx.ec_instr = dfunc->df_instr;
1293 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001294
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001295 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001296 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001297 estack_push_ufunc(ufunc, 1);
1298 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001299 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1300
Bram Moolenaar352134b2020-10-17 22:04:08 +02001301 // Use a specific location for storing error messages to be converted to an
1302 // exception.
1303 saved_msg_list = msg_list;
1304 msg_list = &private_msg_list;
1305
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001306 // Do turn errors into exceptions.
1307 suppress_errthrow = FALSE;
1308
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001309 // When ":silent!" was used before calling then we still abort the
1310 // function. If ":silent!" is used in the function then we don't.
1311 emsg_silent_def = emsg_silent;
1312 did_emsg_def = 0;
1313
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001314 // Decide where to start execution, handles optional arguments.
1315 init_instr_idx(ufunc, argc, &ectx);
1316
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 for (;;)
1318 {
1319 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001320
Bram Moolenaar270d0382020-05-15 21:42:53 +02001321 if (++breakcheck_count >= 100)
1322 {
1323 line_breakcheck();
1324 breakcheck_count = 0;
1325 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001326 if (got_int)
1327 {
1328 // Turn CTRL-C into an exception.
1329 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001330 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001331 goto failed;
1332 did_throw = TRUE;
1333 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334
Bram Moolenaara26b9702020-04-18 19:53:28 +02001335 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1336 {
1337 // Turn an error message into an exception.
1338 did_emsg = FALSE;
1339 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1340 goto failed;
1341 did_throw = TRUE;
1342 *msg_list = NULL;
1343 }
1344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 if (did_throw && !ectx.ec_in_catch)
1346 {
1347 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001348 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349
1350 // An exception jumps to the first catch, finally, or returns from
1351 // the current function.
1352 if (trystack->ga_len > 0)
1353 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001354 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 {
1356 // jump to ":catch" or ":finally"
1357 ectx.ec_in_catch = TRUE;
1358 ectx.ec_iidx = trycmd->tcd_catch_idx;
1359 }
1360 else
1361 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001362 // Not inside try or need to return from current functions.
1363 // Push a dummy return value.
1364 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1365 goto failed;
1366 tv = STACK_TV_BOT(0);
1367 tv->v_type = VAR_NUMBER;
1368 tv->vval.v_number = 0;
1369 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001370 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001372 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001373 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001374 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1375 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 goto done;
1377 }
1378
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001379 if (func_return(&ectx) == FAIL)
1380 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 }
1382 continue;
1383 }
1384
1385 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1386 switch (iptr->isn_type)
1387 {
1388 // execute Ex command line
1389 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001390 {
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001391 SOURCING_LNUM = iptr->isn_lnum;
1392 do_cmdline_cmd(iptr->isn_arg.string);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001393 if (did_emsg)
1394 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001395 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 break;
1397
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001398 // execute Ex command from pieces on the stack
1399 case ISN_EXECCONCAT:
1400 {
1401 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001402 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001403 int pass;
1404 int i;
1405 char_u *cmd = NULL;
1406 char_u *str;
1407
1408 for (pass = 1; pass <= 2; ++pass)
1409 {
1410 for (i = 0; i < count; ++i)
1411 {
1412 tv = STACK_TV_BOT(i - count);
1413 str = tv->vval.v_string;
1414 if (str != NULL && *str != NUL)
1415 {
1416 if (pass == 2)
1417 STRCPY(cmd + len, str);
1418 len += STRLEN(str);
1419 }
1420 if (pass == 2)
1421 clear_tv(tv);
1422 }
1423 if (pass == 1)
1424 {
1425 cmd = alloc(len + 1);
1426 if (cmd == NULL)
1427 goto failed;
1428 len = 0;
1429 }
1430 }
1431
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001432 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001433 do_cmdline_cmd(cmd);
1434 vim_free(cmd);
1435 }
1436 break;
1437
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438 // execute :echo {string} ...
1439 case ISN_ECHO:
1440 {
1441 int count = iptr->isn_arg.echo.echo_count;
1442 int atstart = TRUE;
1443 int needclr = TRUE;
1444
1445 for (idx = 0; idx < count; ++idx)
1446 {
1447 tv = STACK_TV_BOT(idx - count);
1448 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1449 &atstart, &needclr);
1450 clear_tv(tv);
1451 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001452 if (needclr)
1453 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 ectx.ec_stack.ga_len -= count;
1455 }
1456 break;
1457
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001458 // :execute {string} ...
1459 // :echomsg {string} ...
1460 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001461 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001462 case ISN_ECHOMSG:
1463 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001464 {
1465 int count = iptr->isn_arg.number;
1466 garray_T ga;
1467 char_u buf[NUMBUFLEN];
1468 char_u *p;
1469 int len;
1470 int failed = FALSE;
1471
1472 ga_init2(&ga, 1, 80);
1473 for (idx = 0; idx < count; ++idx)
1474 {
1475 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001476 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001477 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001478 if (tv->v_type == VAR_CHANNEL
1479 || tv->v_type == VAR_JOB)
1480 {
1481 SOURCING_LNUM = iptr->isn_lnum;
1482 emsg(_(e_inval_string));
1483 break;
1484 }
1485 else
1486 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001487 }
1488 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001489 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001490
1491 len = (int)STRLEN(p);
1492 if (ga_grow(&ga, len + 2) == FAIL)
1493 failed = TRUE;
1494 else
1495 {
1496 if (ga.ga_len > 0)
1497 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1498 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1499 ga.ga_len += len;
1500 }
1501 clear_tv(tv);
1502 }
1503 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001504 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001505 {
1506 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001507 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001508 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001509
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001510 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001511 {
1512 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001513 {
1514 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001515 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001516 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001517 {
1518 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001519 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001520 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001521 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001522 else
1523 {
1524 msg_sb_eol();
1525 if (iptr->isn_type == ISN_ECHOMSG)
1526 {
1527 msg_attr(ga.ga_data, echo_attr);
1528 out_flush();
1529 }
1530 else
1531 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001532 SOURCING_LNUM = iptr->isn_lnum;
1533 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001534 }
1535 }
1536 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001537 ga_clear(&ga);
1538 }
1539 break;
1540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 // load local variable or argument
1542 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001543 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 goto failed;
1545 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1546 ++ectx.ec_stack.ga_len;
1547 break;
1548
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001549 // load variable or argument from outer scope
1550 case ISN_LOADOUTER:
Bram Moolenaarab360522021-01-10 14:02:28 +01001551 {
1552 typval_T *stack;
1553 int depth = iptr->isn_arg.outer.outer_depth;
1554
1555 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1556 goto failed;
1557 if (depth <= 1)
1558 stack = ((typval_T *)ectx.ec_outer_stack->ga_data)
1559 + ectx.ec_outer_frame;
1560 else if (depth == 2)
1561 stack = ((typval_T *)ectx.ec_outer_up_stack->ga_data)
1562 + ectx.ec_outer_up_frame;
1563 else
1564 {
1565 SOURCING_LNUM = iptr->isn_lnum;
1566 iemsg("LOADOUTER level > 2 not supported yet");
1567 goto failed;
1568 }
1569
1570 copy_tv(stack + STACK_FRAME_SIZE
1571 + iptr->isn_arg.outer.outer_idx,
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001572 STACK_TV_BOT(0));
Bram Moolenaarab360522021-01-10 14:02:28 +01001573 ++ectx.ec_stack.ga_len;
1574 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001575 break;
1576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 // load v: variable
1578 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001579 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580 goto failed;
1581 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1582 ++ectx.ec_stack.ga_len;
1583 break;
1584
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001585 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586 case ISN_LOADSCRIPT:
1587 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001588 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589 svar_T *sv;
1590
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001591 sv = get_script_svar(sref, &ectx);
1592 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001593 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001594 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001595 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 goto failed;
1597 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1598 ++ectx.ec_stack.ga_len;
1599 }
1600 break;
1601
1602 // load s: variable in old script
1603 case ISN_LOADS:
1604 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001605 hashtab_T *ht = &SCRIPT_VARS(
1606 iptr->isn_arg.loadstore.ls_sid);
1607 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001609
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 if (di == NULL)
1611 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001612 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001613 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001614 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 }
1616 else
1617 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001618 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619 goto failed;
1620 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1621 ++ectx.ec_stack.ga_len;
1622 }
1623 }
1624 break;
1625
Bram Moolenaard3aac292020-04-19 14:32:17 +02001626 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001628 case ISN_LOADB:
1629 case ISN_LOADW:
1630 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001632 dictitem_T *di = NULL;
1633 hashtab_T *ht = NULL;
1634 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001635
Bram Moolenaard3aac292020-04-19 14:32:17 +02001636 switch (iptr->isn_type)
1637 {
1638 case ISN_LOADG:
1639 ht = get_globvar_ht();
1640 namespace = 'g';
1641 break;
1642 case ISN_LOADB:
1643 ht = &curbuf->b_vars->dv_hashtab;
1644 namespace = 'b';
1645 break;
1646 case ISN_LOADW:
1647 ht = &curwin->w_vars->dv_hashtab;
1648 namespace = 'w';
1649 break;
1650 case ISN_LOADT:
1651 ht = &curtab->tp_vars->dv_hashtab;
1652 namespace = 't';
1653 break;
1654 default: // Cannot reach here
1655 goto failed;
1656 }
1657 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 if (di == NULL)
1660 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001661 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001662 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001663 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001664 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 }
1666 else
1667 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001668 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 goto failed;
1670 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1671 ++ectx.ec_stack.ga_len;
1672 }
1673 }
1674 break;
1675
Bram Moolenaar03290b82020-12-19 16:30:44 +01001676 // load autoload variable
1677 case ISN_LOADAUTO:
1678 {
1679 char_u *name = iptr->isn_arg.string;
1680
1681 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1682 goto failed;
1683 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001684 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001685 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1686 goto on_error;
1687 ++ectx.ec_stack.ga_len;
1688 }
1689 break;
1690
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001691 // load g:/b:/w:/t: namespace
1692 case ISN_LOADGDICT:
1693 case ISN_LOADBDICT:
1694 case ISN_LOADWDICT:
1695 case ISN_LOADTDICT:
1696 {
1697 dict_T *d = NULL;
1698
1699 switch (iptr->isn_type)
1700 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001701 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1702 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1703 case ISN_LOADWDICT: d = curwin->w_vars; break;
1704 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001705 default: // Cannot reach here
1706 goto failed;
1707 }
1708 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1709 goto failed;
1710 tv = STACK_TV_BOT(0);
1711 tv->v_type = VAR_DICT;
1712 tv->v_lock = 0;
1713 tv->vval.v_dict = d;
1714 ++ectx.ec_stack.ga_len;
1715 }
1716 break;
1717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 // load &option
1719 case ISN_LOADOPT:
1720 {
1721 typval_T optval;
1722 char_u *name = iptr->isn_arg.string;
1723
Bram Moolenaara8c17702020-04-01 21:17:24 +02001724 // This is not expected to fail, name is checked during
1725 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001726 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001728 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001729 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 *STACK_TV_BOT(0) = optval;
1731 ++ectx.ec_stack.ga_len;
1732 }
1733 break;
1734
1735 // load $ENV
1736 case ISN_LOADENV:
1737 {
1738 typval_T optval;
1739 char_u *name = iptr->isn_arg.string;
1740
Bram Moolenaar270d0382020-05-15 21:42:53 +02001741 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001743 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001744 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 *STACK_TV_BOT(0) = optval;
1746 ++ectx.ec_stack.ga_len;
1747 }
1748 break;
1749
1750 // load @register
1751 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001752 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 goto failed;
1754 tv = STACK_TV_BOT(0);
1755 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001756 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001757 // This may result in NULL, which should be equivalent to an
1758 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001759 tv->vval.v_string = get_reg_contents(
1760 iptr->isn_arg.number, GREG_EXPR_SRC);
1761 ++ectx.ec_stack.ga_len;
1762 break;
1763
1764 // store local variable
1765 case ISN_STORE:
1766 --ectx.ec_stack.ga_len;
1767 tv = STACK_TV_VAR(iptr->isn_arg.number);
1768 clear_tv(tv);
1769 *tv = *STACK_TV_BOT(0);
1770 break;
1771
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001772 // store variable or argument in outer scope
1773 case ISN_STOREOUTER:
1774 --ectx.ec_stack.ga_len;
Bram Moolenaarab360522021-01-10 14:02:28 +01001775 // TODO: use outer_depth
1776 tv = STACK_OUT_TV_VAR(iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001777 clear_tv(tv);
1778 *tv = *STACK_TV_BOT(0);
1779 break;
1780
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001781 // store s: variable in old script
1782 case ISN_STORES:
1783 {
1784 hashtab_T *ht = &SCRIPT_VARS(
1785 iptr->isn_arg.loadstore.ls_sid);
1786 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001787 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001788
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001789 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001790 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001791 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001792 else
1793 {
1794 clear_tv(&di->di_tv);
1795 di->di_tv = *STACK_TV_BOT(0);
1796 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001797 }
1798 break;
1799
1800 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 case ISN_STORESCRIPT:
1802 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001803 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1804 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001806 sv = get_script_svar(sref, &ectx);
1807 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001808 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 --ectx.ec_stack.ga_len;
1810 clear_tv(sv->sv_tv);
1811 *sv->sv_tv = *STACK_TV_BOT(0);
1812 }
1813 break;
1814
1815 // store option
1816 case ISN_STOREOPT:
1817 {
1818 long n = 0;
1819 char_u *s = NULL;
1820 char *msg;
1821
1822 --ectx.ec_stack.ga_len;
1823 tv = STACK_TV_BOT(0);
1824 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001825 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001827 if (s == NULL)
1828 s = (char_u *)"";
1829 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001831 // must be VAR_NUMBER, CHECKTYPE makes sure
1832 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1834 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001835 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 if (msg != NULL)
1837 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001838 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001840 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 }
1843 break;
1844
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001845 // store $ENV
1846 case ISN_STOREENV:
1847 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001848 tv = STACK_TV_BOT(0);
1849 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1850 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001851 break;
1852
1853 // store @r
1854 case ISN_STOREREG:
1855 {
1856 int reg = iptr->isn_arg.number;
1857
1858 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001859 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001860 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001861 tv_get_string(tv), -1, FALSE);
1862 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001863 }
1864 break;
1865
1866 // store v: variable
1867 case ISN_STOREV:
1868 --ectx.ec_stack.ga_len;
1869 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1870 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001871 // should not happen, type is checked when compiling
1872 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001873 break;
1874
Bram Moolenaard3aac292020-04-19 14:32:17 +02001875 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001877 case ISN_STOREB:
1878 case ISN_STOREW:
1879 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001881 dictitem_T *di;
1882 hashtab_T *ht;
1883 char_u *name = iptr->isn_arg.string + 2;
1884
Bram Moolenaard3aac292020-04-19 14:32:17 +02001885 switch (iptr->isn_type)
1886 {
1887 case ISN_STOREG:
1888 ht = get_globvar_ht();
1889 break;
1890 case ISN_STOREB:
1891 ht = &curbuf->b_vars->dv_hashtab;
1892 break;
1893 case ISN_STOREW:
1894 ht = &curwin->w_vars->dv_hashtab;
1895 break;
1896 case ISN_STORET:
1897 ht = &curtab->tp_vars->dv_hashtab;
1898 break;
1899 default: // Cannot reach here
1900 goto failed;
1901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902
1903 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001904 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001906 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 else
1908 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001909 SOURCING_LNUM = iptr->isn_lnum;
1910 if (var_check_permission(di, name) == FAIL)
1911 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 clear_tv(&di->di_tv);
1913 di->di_tv = *STACK_TV_BOT(0);
1914 }
1915 }
1916 break;
1917
Bram Moolenaar03290b82020-12-19 16:30:44 +01001918 // store an autoload variable
1919 case ISN_STOREAUTO:
1920 SOURCING_LNUM = iptr->isn_lnum;
1921 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1922 clear_tv(STACK_TV_BOT(-1));
1923 --ectx.ec_stack.ga_len;
1924 break;
1925
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 // store number in local variable
1927 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001928 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 clear_tv(tv);
1930 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001931 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 break;
1933
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001934 // store value in list or dict variable
1935 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001936 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001937 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001938 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001939 typval_T *tv_dest = STACK_TV_BOT(-1);
1940 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001941
Bram Moolenaar752fc692021-01-04 21:57:11 +01001942 // Stack contains:
1943 // -3 value to be stored
1944 // -2 index
1945 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001946 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001947 SOURCING_LNUM = iptr->isn_lnum;
1948 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001949 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001950 dest_type = tv_dest->v_type;
1951 if (dest_type == VAR_DICT)
1952 status = do_2string(tv_idx, TRUE);
1953 else if (dest_type == VAR_LIST
1954 && tv_idx->v_type != VAR_NUMBER)
1955 {
1956 emsg(_(e_number_exp));
1957 status = FAIL;
1958 }
1959 }
1960 else if (dest_type != tv_dest->v_type)
1961 {
1962 // just in case, should be OK
1963 semsg(_(e_expected_str_but_got_str),
1964 vartype_name(dest_type),
1965 vartype_name(tv_dest->v_type));
1966 status = FAIL;
1967 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001968
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001969 if (status == OK && dest_type == VAR_LIST)
1970 {
1971 varnumber_T lidx = tv_idx->vval.v_number;
1972 list_T *list = tv_dest->vval.v_list;
1973
1974 if (list == NULL)
1975 {
1976 emsg(_(e_list_not_set));
1977 goto on_error;
1978 }
1979 if (lidx < 0 && list->lv_len + lidx >= 0)
1980 // negative index is relative to the end
1981 lidx = list->lv_len + lidx;
1982 if (lidx < 0 || lidx > list->lv_len)
1983 {
1984 semsg(_(e_listidx), lidx);
1985 goto on_error;
1986 }
1987 if (lidx < list->lv_len)
1988 {
1989 listitem_T *li = list_find(list, lidx);
1990
1991 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001992 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001993 goto on_error;
1994 // overwrite existing list item
1995 clear_tv(&li->li_tv);
1996 li->li_tv = *tv;
1997 }
1998 else
1999 {
2000 if (error_if_locked(list->lv_lock,
2001 e_cannot_change_list))
2002 goto on_error;
2003 // append to list, only fails when out of memory
2004 if (list_append_tv(list, tv) == FAIL)
2005 goto failed;
2006 clear_tv(tv);
2007 }
2008 }
2009 else if (status == OK && dest_type == VAR_DICT)
2010 {
2011 char_u *key = tv_idx->vval.v_string;
2012 dict_T *dict = tv_dest->vval.v_dict;
2013 dictitem_T *di;
2014
2015 SOURCING_LNUM = iptr->isn_lnum;
2016 if (dict == NULL)
2017 {
2018 emsg(_(e_dictionary_not_set));
2019 goto on_error;
2020 }
2021 if (key == NULL)
2022 key = (char_u *)"";
2023 di = dict_find(dict, key, -1);
2024 if (di != NULL)
2025 {
2026 if (error_if_locked(di->di_tv.v_lock,
2027 e_cannot_change_dict_item))
2028 goto on_error;
2029 // overwrite existing value
2030 clear_tv(&di->di_tv);
2031 di->di_tv = *tv;
2032 }
2033 else
2034 {
2035 if (error_if_locked(dict->dv_lock,
2036 e_cannot_change_dict))
2037 goto on_error;
2038 // add to dict, only fails when out of memory
2039 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2040 goto failed;
2041 clear_tv(tv);
2042 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002043 }
2044 else
2045 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002046 status = FAIL;
2047 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002048 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002049
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002050 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002051 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002052 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002053 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002054 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002055 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002056 goto on_error;
2057 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002058 }
2059 break;
2060
Bram Moolenaar752fc692021-01-04 21:57:11 +01002061 // unlet item in list or dict variable
2062 case ISN_UNLETINDEX:
2063 {
2064 typval_T *tv_idx = STACK_TV_BOT(-2);
2065 typval_T *tv_dest = STACK_TV_BOT(-1);
2066 int status = OK;
2067
2068 // Stack contains:
2069 // -2 index
2070 // -1 dict or list
2071 if (tv_dest->v_type == VAR_DICT)
2072 {
2073 // unlet a dict item, index must be a string
2074 if (tv_idx->v_type != VAR_STRING)
2075 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002076 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002077 semsg(_(e_expected_str_but_got_str),
2078 vartype_name(VAR_STRING),
2079 vartype_name(tv_idx->v_type));
2080 status = FAIL;
2081 }
2082 else
2083 {
2084 dict_T *d = tv_dest->vval.v_dict;
2085 char_u *key = tv_idx->vval.v_string;
2086 dictitem_T *di = NULL;
2087
2088 if (key == NULL)
2089 key = (char_u *)"";
2090 if (d != NULL)
2091 di = dict_find(d, key, (int)STRLEN(key));
2092 if (di == NULL)
2093 {
2094 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002095 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002096 semsg(_(e_dictkey), key);
2097 status = FAIL;
2098 }
2099 else
2100 {
2101 // TODO: check for dict or item locked
2102 dictitem_remove(d, di);
2103 }
2104 }
2105 }
2106 else if (tv_dest->v_type == VAR_LIST)
2107 {
2108 // unlet a List item, index must be a number
2109 if (tv_idx->v_type != VAR_NUMBER)
2110 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002111 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002112 semsg(_(e_expected_str_but_got_str),
2113 vartype_name(VAR_NUMBER),
2114 vartype_name(tv_idx->v_type));
2115 status = FAIL;
2116 }
2117 else
2118 {
2119 list_T *l = tv_dest->vval.v_list;
2120 varnumber_T n = tv_idx->vval.v_number;
2121 listitem_T *li = NULL;
2122
2123 li = list_find(l, n);
2124 if (li == NULL)
2125 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002126 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002127 semsg(_(e_listidx), n);
2128 status = FAIL;
2129 }
2130 else
2131 // TODO: check for list or item locked
2132 listitem_remove(l, li);
2133 }
2134 }
2135 else
2136 {
2137 status = FAIL;
2138 semsg(_(e_cannot_index_str),
2139 vartype_name(tv_dest->v_type));
2140 }
2141
2142 clear_tv(tv_idx);
2143 clear_tv(tv_dest);
2144 ectx.ec_stack.ga_len -= 2;
2145 if (status == FAIL)
2146 goto on_error;
2147 }
2148 break;
2149
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150 // push constant
2151 case ISN_PUSHNR:
2152 case ISN_PUSHBOOL:
2153 case ISN_PUSHSPEC:
2154 case ISN_PUSHF:
2155 case ISN_PUSHS:
2156 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002157 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002158 case ISN_PUSHCHANNEL:
2159 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002160 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 goto failed;
2162 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002163 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 ++ectx.ec_stack.ga_len;
2165 switch (iptr->isn_type)
2166 {
2167 case ISN_PUSHNR:
2168 tv->v_type = VAR_NUMBER;
2169 tv->vval.v_number = iptr->isn_arg.number;
2170 break;
2171 case ISN_PUSHBOOL:
2172 tv->v_type = VAR_BOOL;
2173 tv->vval.v_number = iptr->isn_arg.number;
2174 break;
2175 case ISN_PUSHSPEC:
2176 tv->v_type = VAR_SPECIAL;
2177 tv->vval.v_number = iptr->isn_arg.number;
2178 break;
2179#ifdef FEAT_FLOAT
2180 case ISN_PUSHF:
2181 tv->v_type = VAR_FLOAT;
2182 tv->vval.v_float = iptr->isn_arg.fnumber;
2183 break;
2184#endif
2185 case ISN_PUSHBLOB:
2186 blob_copy(iptr->isn_arg.blob, tv);
2187 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002188 case ISN_PUSHFUNC:
2189 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002190 if (iptr->isn_arg.string == NULL)
2191 tv->vval.v_string = NULL;
2192 else
2193 tv->vval.v_string =
2194 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002195 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002196 case ISN_PUSHCHANNEL:
2197#ifdef FEAT_JOB_CHANNEL
2198 tv->v_type = VAR_CHANNEL;
2199 tv->vval.v_channel = iptr->isn_arg.channel;
2200 if (tv->vval.v_channel != NULL)
2201 ++tv->vval.v_channel->ch_refcount;
2202#endif
2203 break;
2204 case ISN_PUSHJOB:
2205#ifdef FEAT_JOB_CHANNEL
2206 tv->v_type = VAR_JOB;
2207 tv->vval.v_job = iptr->isn_arg.job;
2208 if (tv->vval.v_job != NULL)
2209 ++tv->vval.v_job->jv_refcount;
2210#endif
2211 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 default:
2213 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002214 tv->vval.v_string = vim_strsave(
2215 iptr->isn_arg.string == NULL
2216 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002217 }
2218 break;
2219
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002220 case ISN_UNLET:
2221 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2222 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002223 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002224 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002225 case ISN_UNLETENV:
2226 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2227 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002228
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002229 case ISN_LOCKCONST:
2230 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2231 break;
2232
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 // create a list from items on the stack; uses a single allocation
2234 // for the list header and the items
2235 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002236 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2237 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 break;
2239
2240 // create a dict from items on the stack
2241 case ISN_NEWDICT:
2242 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002243 int count = iptr->isn_arg.number;
2244 dict_T *dict = dict_alloc();
2245 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002246 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247
2248 if (dict == NULL)
2249 goto failed;
2250 for (idx = 0; idx < count; ++idx)
2251 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002252 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002254 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002255 key = tv->vval.v_string == NULL
2256 ? (char_u *)"" : tv->vval.v_string;
2257 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002258 if (item != NULL)
2259 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002260 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002261 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002262 dict_unref(dict);
2263 goto on_error;
2264 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002265 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 clear_tv(tv);
2267 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002268 {
2269 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002271 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2273 item->di_tv.v_lock = 0;
2274 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002275 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002276 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002277 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002279 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280 }
2281
2282 if (count > 0)
2283 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002284 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 goto failed;
2286 else
2287 ++ectx.ec_stack.ga_len;
2288 tv = STACK_TV_BOT(-1);
2289 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002290 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291 tv->vval.v_dict = dict;
2292 ++dict->dv_refcount;
2293 }
2294 break;
2295
2296 // call a :def function
2297 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002298 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2300 iptr->isn_arg.dfunc.cdf_argcount,
2301 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002302 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 break;
2304
2305 // call a builtin function
2306 case ISN_BCALL:
2307 SOURCING_LNUM = iptr->isn_lnum;
2308 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2309 iptr->isn_arg.bfunc.cbf_argcount,
2310 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002311 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 break;
2313
2314 // call a funcref or partial
2315 case ISN_PCALL:
2316 {
2317 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2318 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002319 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320
2321 SOURCING_LNUM = iptr->isn_lnum;
2322 if (pfunc->cpf_top)
2323 {
2324 // funcref is above the arguments
2325 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2326 }
2327 else
2328 {
2329 // Get the funcref from the stack.
2330 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002331 partial_tv = *STACK_TV_BOT(0);
2332 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 }
2334 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002335 if (tv == &partial_tv)
2336 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002338 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339 }
2340 break;
2341
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002342 case ISN_PCALL_END:
2343 // PCALL finished, arguments have been consumed and replaced by
2344 // the return value. Now clear the funcref from the stack,
2345 // and move the return value in its place.
2346 --ectx.ec_stack.ga_len;
2347 clear_tv(STACK_TV_BOT(-1));
2348 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2349 break;
2350
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351 // call a user defined function or funcref/partial
2352 case ISN_UCALL:
2353 {
2354 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2355
2356 SOURCING_LNUM = iptr->isn_lnum;
2357 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002358 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002359 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002360 }
2361 break;
2362
2363 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002364 case ISN_RETURN_ZERO:
2365 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2366 goto failed;
2367 tv = STACK_TV_BOT(0);
2368 ++ectx.ec_stack.ga_len;
2369 tv->v_type = VAR_NUMBER;
2370 tv->vval.v_number = 0;
2371 tv->v_lock = 0;
2372 // FALLTHROUGH
2373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 case ISN_RETURN:
2375 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002376 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002377 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002378
2379 if (trystack->ga_len > 0)
2380 trycmd = ((trycmd_T *)trystack->ga_data)
2381 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002382 if (trycmd != NULL
2383 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 && trycmd->tcd_finally_idx != 0)
2385 {
2386 // jump to ":finally"
2387 ectx.ec_iidx = trycmd->tcd_finally_idx;
2388 trycmd->tcd_return = TRUE;
2389 }
2390 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002391 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392 }
2393 break;
2394
2395 // push a function reference to a compiled function
2396 case ISN_FUNCREF:
2397 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002398 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2399 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2400 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 if (pt == NULL)
2403 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002404 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002405 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002406 vim_free(pt);
2407 goto failed;
2408 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002409 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2410 &ectx) == FAIL)
2411 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 tv = STACK_TV_BOT(0);
2414 ++ectx.ec_stack.ga_len;
2415 tv->vval.v_partial = pt;
2416 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002417 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 }
2419 break;
2420
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002421 // Create a global function from a lambda.
2422 case ISN_NEWFUNC:
2423 {
2424 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2425
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002426 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002427 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002428 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002429 }
2430 break;
2431
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002432 // List functions
2433 case ISN_DEF:
2434 if (iptr->isn_arg.string == NULL)
2435 list_functions(NULL);
2436 else
2437 {
2438 exarg_T ea;
2439
2440 CLEAR_FIELD(ea);
2441 ea.cmd = ea.arg = iptr->isn_arg.string;
2442 define_function(&ea, NULL);
2443 }
2444 break;
2445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446 // jump if a condition is met
2447 case ISN_JUMP:
2448 {
2449 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002450 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 int jump = TRUE;
2452
2453 if (when != JUMP_ALWAYS)
2454 {
2455 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002456 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002457 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002458 || when == JUMP_IF_COND_TRUE)
2459 {
2460 SOURCING_LNUM = iptr->isn_lnum;
2461 jump = tv_get_bool_chk(tv, &error);
2462 if (error)
2463 goto on_error;
2464 }
2465 else
2466 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002468 || when == JUMP_AND_KEEP_IF_FALSE
2469 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002471 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 {
2473 // drop the value from the stack
2474 clear_tv(tv);
2475 --ectx.ec_stack.ga_len;
2476 }
2477 }
2478 if (jump)
2479 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2480 }
2481 break;
2482
2483 // top of a for loop
2484 case ISN_FOR:
2485 {
2486 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2487 typval_T *idxtv =
2488 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2489
2490 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002491 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002493 ++idxtv->vval.v_number;
2494 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 // past the end of the list, jump to "endfor"
2496 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2497 else if (list->lv_first == &range_list_item)
2498 {
2499 // non-materialized range() list
2500 tv = STACK_TV_BOT(0);
2501 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002502 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 tv->vval.v_number = list_find_nr(
2504 list, idxtv->vval.v_number, NULL);
2505 ++ectx.ec_stack.ga_len;
2506 }
2507 else
2508 {
2509 listitem_T *li = list_find(list, idxtv->vval.v_number);
2510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2512 ++ectx.ec_stack.ga_len;
2513 }
2514 }
2515 break;
2516
2517 // start of ":try" block
2518 case ISN_TRY:
2519 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002520 trycmd_T *trycmd = NULL;
2521
Bram Moolenaar270d0382020-05-15 21:42:53 +02002522 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 goto failed;
2524 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2525 + ectx.ec_trystack.ga_len;
2526 ++ectx.ec_trystack.ga_len;
2527 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002528 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2530 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002531 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002532 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 }
2534 break;
2535
2536 case ISN_PUSHEXC:
2537 if (current_exception == NULL)
2538 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002539 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 iemsg("Evaluating catch while current_exception is NULL");
2541 goto failed;
2542 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002543 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 goto failed;
2545 tv = STACK_TV_BOT(0);
2546 ++ectx.ec_stack.ga_len;
2547 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002548 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 tv->vval.v_string = vim_strsave(
2550 (char_u *)current_exception->value);
2551 break;
2552
2553 case ISN_CATCH:
2554 {
2555 garray_T *trystack = &ectx.ec_trystack;
2556
Bram Moolenaar20a76292020-12-25 19:47:24 +01002557 if (restore_cmdmod)
2558 {
2559 cmdmod.cmod_filter_regmatch.regprog = NULL;
2560 undo_cmdmod(&cmdmod);
2561 cmdmod = save_cmdmod;
2562 restore_cmdmod = FALSE;
2563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 if (trystack->ga_len > 0)
2565 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002566 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 + trystack->ga_len - 1;
2568 trycmd->tcd_caught = TRUE;
2569 }
2570 did_emsg = got_int = did_throw = FALSE;
2571 catch_exception(current_exception);
2572 }
2573 break;
2574
2575 // end of ":try" block
2576 case ISN_ENDTRY:
2577 {
2578 garray_T *trystack = &ectx.ec_trystack;
2579
2580 if (trystack->ga_len > 0)
2581 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002582 trycmd_T *trycmd = NULL;
2583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 --trystack->ga_len;
2585 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002586 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 trycmd = ((trycmd_T *)trystack->ga_data)
2588 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002589 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 {
2591 // discard the exception
2592 if (caught_stack == current_exception)
2593 caught_stack = caught_stack->caught;
2594 discard_current_exception();
2595 }
2596
2597 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002598 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 }
2600 }
2601 break;
2602
2603 case ISN_THROW:
2604 --ectx.ec_stack.ga_len;
2605 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002606 if (tv->vval.v_string == NULL
2607 || *skipwhite(tv->vval.v_string) == NUL)
2608 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002609 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002610 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002611 emsg(_(e_throw_with_empty_string));
2612 goto failed;
2613 }
2614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2616 {
2617 vim_free(tv->vval.v_string);
2618 goto failed;
2619 }
2620 did_throw = TRUE;
2621 break;
2622
2623 // compare with special values
2624 case ISN_COMPAREBOOL:
2625 case ISN_COMPARESPECIAL:
2626 {
2627 typval_T *tv1 = STACK_TV_BOT(-2);
2628 typval_T *tv2 = STACK_TV_BOT(-1);
2629 varnumber_T arg1 = tv1->vval.v_number;
2630 varnumber_T arg2 = tv2->vval.v_number;
2631 int res;
2632
2633 switch (iptr->isn_arg.op.op_type)
2634 {
2635 case EXPR_EQUAL: res = arg1 == arg2; break;
2636 case EXPR_NEQUAL: res = arg1 != arg2; break;
2637 default: res = 0; break;
2638 }
2639
2640 --ectx.ec_stack.ga_len;
2641 tv1->v_type = VAR_BOOL;
2642 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2643 }
2644 break;
2645
2646 // Operation with two number arguments
2647 case ISN_OPNR:
2648 case ISN_COMPARENR:
2649 {
2650 typval_T *tv1 = STACK_TV_BOT(-2);
2651 typval_T *tv2 = STACK_TV_BOT(-1);
2652 varnumber_T arg1 = tv1->vval.v_number;
2653 varnumber_T arg2 = tv2->vval.v_number;
2654 varnumber_T res;
2655
2656 switch (iptr->isn_arg.op.op_type)
2657 {
2658 case EXPR_MULT: res = arg1 * arg2; break;
2659 case EXPR_DIV: res = arg1 / arg2; break;
2660 case EXPR_REM: res = arg1 % arg2; break;
2661 case EXPR_SUB: res = arg1 - arg2; break;
2662 case EXPR_ADD: res = arg1 + arg2; break;
2663
2664 case EXPR_EQUAL: res = arg1 == arg2; break;
2665 case EXPR_NEQUAL: res = arg1 != arg2; break;
2666 case EXPR_GREATER: res = arg1 > arg2; break;
2667 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2668 case EXPR_SMALLER: res = arg1 < arg2; break;
2669 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2670 default: res = 0; break;
2671 }
2672
2673 --ectx.ec_stack.ga_len;
2674 if (iptr->isn_type == ISN_COMPARENR)
2675 {
2676 tv1->v_type = VAR_BOOL;
2677 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2678 }
2679 else
2680 tv1->vval.v_number = res;
2681 }
2682 break;
2683
2684 // Computation with two float arguments
2685 case ISN_OPFLOAT:
2686 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002687#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 {
2689 typval_T *tv1 = STACK_TV_BOT(-2);
2690 typval_T *tv2 = STACK_TV_BOT(-1);
2691 float_T arg1 = tv1->vval.v_float;
2692 float_T arg2 = tv2->vval.v_float;
2693 float_T res = 0;
2694 int cmp = FALSE;
2695
2696 switch (iptr->isn_arg.op.op_type)
2697 {
2698 case EXPR_MULT: res = arg1 * arg2; break;
2699 case EXPR_DIV: res = arg1 / arg2; break;
2700 case EXPR_SUB: res = arg1 - arg2; break;
2701 case EXPR_ADD: res = arg1 + arg2; break;
2702
2703 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2704 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2705 case EXPR_GREATER: cmp = arg1 > arg2; break;
2706 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2707 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2708 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2709 default: cmp = 0; break;
2710 }
2711 --ectx.ec_stack.ga_len;
2712 if (iptr->isn_type == ISN_COMPAREFLOAT)
2713 {
2714 tv1->v_type = VAR_BOOL;
2715 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2716 }
2717 else
2718 tv1->vval.v_float = res;
2719 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002720#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 break;
2722
2723 case ISN_COMPARELIST:
2724 {
2725 typval_T *tv1 = STACK_TV_BOT(-2);
2726 typval_T *tv2 = STACK_TV_BOT(-1);
2727 list_T *arg1 = tv1->vval.v_list;
2728 list_T *arg2 = tv2->vval.v_list;
2729 int cmp = FALSE;
2730 int ic = iptr->isn_arg.op.op_ic;
2731
2732 switch (iptr->isn_arg.op.op_type)
2733 {
2734 case EXPR_EQUAL: cmp =
2735 list_equal(arg1, arg2, ic, FALSE); break;
2736 case EXPR_NEQUAL: cmp =
2737 !list_equal(arg1, arg2, ic, FALSE); break;
2738 case EXPR_IS: cmp = arg1 == arg2; break;
2739 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2740 default: cmp = 0; break;
2741 }
2742 --ectx.ec_stack.ga_len;
2743 clear_tv(tv1);
2744 clear_tv(tv2);
2745 tv1->v_type = VAR_BOOL;
2746 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2747 }
2748 break;
2749
2750 case ISN_COMPAREBLOB:
2751 {
2752 typval_T *tv1 = STACK_TV_BOT(-2);
2753 typval_T *tv2 = STACK_TV_BOT(-1);
2754 blob_T *arg1 = tv1->vval.v_blob;
2755 blob_T *arg2 = tv2->vval.v_blob;
2756 int cmp = FALSE;
2757
2758 switch (iptr->isn_arg.op.op_type)
2759 {
2760 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2761 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2762 case EXPR_IS: cmp = arg1 == arg2; break;
2763 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2764 default: cmp = 0; break;
2765 }
2766 --ectx.ec_stack.ga_len;
2767 clear_tv(tv1);
2768 clear_tv(tv2);
2769 tv1->v_type = VAR_BOOL;
2770 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2771 }
2772 break;
2773
2774 // TODO: handle separately
2775 case ISN_COMPARESTRING:
2776 case ISN_COMPAREDICT:
2777 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 case ISN_COMPAREANY:
2779 {
2780 typval_T *tv1 = STACK_TV_BOT(-2);
2781 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01002782 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 int ic = iptr->isn_arg.op.op_ic;
2784
Bram Moolenaareb26f432020-09-14 16:50:05 +02002785 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002786 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 --ectx.ec_stack.ga_len;
2789 }
2790 break;
2791
2792 case ISN_ADDLIST:
2793 case ISN_ADDBLOB:
2794 {
2795 typval_T *tv1 = STACK_TV_BOT(-2);
2796 typval_T *tv2 = STACK_TV_BOT(-1);
2797
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002798 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 if (iptr->isn_type == ISN_ADDLIST)
2800 eval_addlist(tv1, tv2);
2801 else
2802 eval_addblob(tv1, tv2);
2803 clear_tv(tv2);
2804 --ectx.ec_stack.ga_len;
2805 }
2806 break;
2807
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002808 case ISN_LISTAPPEND:
2809 {
2810 typval_T *tv1 = STACK_TV_BOT(-2);
2811 typval_T *tv2 = STACK_TV_BOT(-1);
2812 list_T *l = tv1->vval.v_list;
2813
2814 // add an item to a list
2815 if (l == NULL)
2816 {
2817 SOURCING_LNUM = iptr->isn_lnum;
2818 emsg(_(e_cannot_add_to_null_list));
2819 goto on_error;
2820 }
2821 if (list_append_tv(l, tv2) == FAIL)
2822 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002823 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002824 --ectx.ec_stack.ga_len;
2825 }
2826 break;
2827
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002828 case ISN_BLOBAPPEND:
2829 {
2830 typval_T *tv1 = STACK_TV_BOT(-2);
2831 typval_T *tv2 = STACK_TV_BOT(-1);
2832 blob_T *b = tv1->vval.v_blob;
2833 int error = FALSE;
2834 varnumber_T n;
2835
2836 // add a number to a blob
2837 if (b == NULL)
2838 {
2839 SOURCING_LNUM = iptr->isn_lnum;
2840 emsg(_(e_cannot_add_to_null_blob));
2841 goto on_error;
2842 }
2843 n = tv_get_number_chk(tv2, &error);
2844 if (error)
2845 goto on_error;
2846 ga_append(&b->bv_ga, (int)n);
2847 --ectx.ec_stack.ga_len;
2848 }
2849 break;
2850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 // Computation with two arguments of unknown type
2852 case ISN_OPANY:
2853 {
2854 typval_T *tv1 = STACK_TV_BOT(-2);
2855 typval_T *tv2 = STACK_TV_BOT(-1);
2856 varnumber_T n1, n2;
2857#ifdef FEAT_FLOAT
2858 float_T f1 = 0, f2 = 0;
2859#endif
2860 int error = FALSE;
2861
2862 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2863 {
2864 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2865 {
2866 eval_addlist(tv1, tv2);
2867 clear_tv(tv2);
2868 --ectx.ec_stack.ga_len;
2869 break;
2870 }
2871 else if (tv1->v_type == VAR_BLOB
2872 && tv2->v_type == VAR_BLOB)
2873 {
2874 eval_addblob(tv1, tv2);
2875 clear_tv(tv2);
2876 --ectx.ec_stack.ga_len;
2877 break;
2878 }
2879 }
2880#ifdef FEAT_FLOAT
2881 if (tv1->v_type == VAR_FLOAT)
2882 {
2883 f1 = tv1->vval.v_float;
2884 n1 = 0;
2885 }
2886 else
2887#endif
2888 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002889 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 n1 = tv_get_number_chk(tv1, &error);
2891 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002892 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893#ifdef FEAT_FLOAT
2894 if (tv2->v_type == VAR_FLOAT)
2895 f1 = n1;
2896#endif
2897 }
2898#ifdef FEAT_FLOAT
2899 if (tv2->v_type == VAR_FLOAT)
2900 {
2901 f2 = tv2->vval.v_float;
2902 n2 = 0;
2903 }
2904 else
2905#endif
2906 {
2907 n2 = tv_get_number_chk(tv2, &error);
2908 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002909 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910#ifdef FEAT_FLOAT
2911 if (tv1->v_type == VAR_FLOAT)
2912 f2 = n2;
2913#endif
2914 }
2915#ifdef FEAT_FLOAT
2916 // if there is a float on either side the result is a float
2917 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2918 {
2919 switch (iptr->isn_arg.op.op_type)
2920 {
2921 case EXPR_MULT: f1 = f1 * f2; break;
2922 case EXPR_DIV: f1 = f1 / f2; break;
2923 case EXPR_SUB: f1 = f1 - f2; break;
2924 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002925 default: SOURCING_LNUM = iptr->isn_lnum;
2926 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002927 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 }
2929 clear_tv(tv1);
2930 clear_tv(tv2);
2931 tv1->v_type = VAR_FLOAT;
2932 tv1->vval.v_float = f1;
2933 --ectx.ec_stack.ga_len;
2934 }
2935 else
2936#endif
2937 {
2938 switch (iptr->isn_arg.op.op_type)
2939 {
2940 case EXPR_MULT: n1 = n1 * n2; break;
2941 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2942 case EXPR_SUB: n1 = n1 - n2; break;
2943 case EXPR_ADD: n1 = n1 + n2; break;
2944 default: n1 = num_modulus(n1, n2); break;
2945 }
2946 clear_tv(tv1);
2947 clear_tv(tv2);
2948 tv1->v_type = VAR_NUMBER;
2949 tv1->vval.v_number = n1;
2950 --ectx.ec_stack.ga_len;
2951 }
2952 }
2953 break;
2954
2955 case ISN_CONCAT:
2956 {
2957 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2958 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2959 char_u *res;
2960
2961 res = concat_str(str1, str2);
2962 clear_tv(STACK_TV_BOT(-2));
2963 clear_tv(STACK_TV_BOT(-1));
2964 --ectx.ec_stack.ga_len;
2965 STACK_TV_BOT(-1)->vval.v_string = res;
2966 }
2967 break;
2968
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002969 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002970 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002971 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002972 int is_slice = iptr->isn_type == ISN_STRSLICE;
2973 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002974 char_u *res;
2975
2976 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002977 // string slice: string is at stack-3, first index at
2978 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002979 if (is_slice)
2980 {
2981 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002982 n1 = tv->vval.v_number;
2983 }
2984
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002985 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002986 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002987
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002988 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002989 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002990 if (is_slice)
2991 // Slice: Select the characters from the string
2992 res = string_slice(tv->vval.v_string, n1, n2);
2993 else
2994 // Index: The resulting variable is a string of a
2995 // single character. If the index is too big or
2996 // negative the result is empty.
2997 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002998 vim_free(tv->vval.v_string);
2999 tv->vval.v_string = res;
3000 }
3001 break;
3002
3003 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003004 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 {
Bram Moolenaared591872020-08-15 22:14:53 +02003006 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003008 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009
3010 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003011 // list slice: list is at stack-3, indexes at stack-2 and
3012 // stack-1
3013 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 list = tv->vval.v_list;
3015
3016 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003017 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003019
3020 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 {
Bram Moolenaared591872020-08-15 22:14:53 +02003022 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003023 n1 = tv->vval.v_number;
3024 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 }
Bram Moolenaared591872020-08-15 22:14:53 +02003026
3027 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003028 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003029 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02003030 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
3031 == FAIL)
3032 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 }
3034 break;
3035
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003036 case ISN_ANYINDEX:
3037 case ISN_ANYSLICE:
3038 {
3039 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3040 typval_T *var1, *var2;
3041 int res;
3042
3043 // index: composite is at stack-2, index at stack-1
3044 // slice: composite is at stack-3, indexes at stack-2 and
3045 // stack-1
3046 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003047 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003048 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3049 goto on_error;
3050 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3051 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
3052 res = eval_index_inner(tv, is_slice,
3053 var1, var2, NULL, -1, TRUE);
3054 clear_tv(var1);
3055 if (is_slice)
3056 clear_tv(var2);
3057 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3058 if (res == FAIL)
3059 goto on_error;
3060 }
3061 break;
3062
Bram Moolenaar9af78762020-06-16 11:34:42 +02003063 case ISN_SLICE:
3064 {
3065 list_T *list;
3066 int count = iptr->isn_arg.number;
3067
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003068 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003069 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003070 list = tv->vval.v_list;
3071
3072 // no error for short list, expect it to be checked earlier
3073 if (list != NULL && list->lv_len >= count)
3074 {
3075 list_T *newlist = list_slice(list,
3076 count, list->lv_len - 1);
3077
3078 if (newlist != NULL)
3079 {
3080 list_unref(list);
3081 tv->vval.v_list = newlist;
3082 ++newlist->lv_refcount;
3083 }
3084 }
3085 }
3086 break;
3087
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003088 case ISN_GETITEM:
3089 {
3090 listitem_T *li;
3091 int index = iptr->isn_arg.number;
3092
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003093 // Get list item: list is at stack-1, push item.
3094 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003095 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003096 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003097
3098 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3099 goto failed;
3100 ++ectx.ec_stack.ga_len;
3101 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
3102 }
3103 break;
3104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 case ISN_MEMBER:
3106 {
3107 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003108 char_u *key;
3109 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003110 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003111
3112 // dict member: dict is at stack-2, key at stack-1
3113 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003114 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003115 dict = tv->vval.v_dict;
3116
3117 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003118 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003119 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003120 if (key == NULL)
3121 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003122
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003123 if ((di = dict_find(dict, key, -1)) == NULL)
3124 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003125 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003126 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003127
3128 // If :silent! is used we will continue, make sure the
3129 // stack contents makes sense.
3130 clear_tv(tv);
3131 --ectx.ec_stack.ga_len;
3132 tv = STACK_TV_BOT(-1);
3133 clear_tv(tv);
3134 tv->v_type = VAR_NUMBER;
3135 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003136 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003137 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003138 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003139 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003140 // Clear the dict only after getting the item, to avoid
3141 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003142 tv = STACK_TV_BOT(-1);
3143 temp_tv = *tv;
3144 copy_tv(&di->di_tv, tv);
3145 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003146 }
3147 break;
3148
3149 // dict member with string key
3150 case ISN_STRINGMEMBER:
3151 {
3152 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003154 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155
3156 tv = STACK_TV_BOT(-1);
3157 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3158 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003159 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003161 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 }
3163 dict = tv->vval.v_dict;
3164
3165 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3166 == NULL)
3167 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003168 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003170 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003172 // Clear the dict after getting the item, to avoid that it
3173 // make the item invalid.
3174 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003176 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 }
3178 break;
3179
3180 case ISN_NEGATENR:
3181 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003182 if (tv->v_type != VAR_NUMBER
3183#ifdef FEAT_FLOAT
3184 && tv->v_type != VAR_FLOAT
3185#endif
3186 )
3187 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003188 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003189 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003190 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003191 }
3192#ifdef FEAT_FLOAT
3193 if (tv->v_type == VAR_FLOAT)
3194 tv->vval.v_float = -tv->vval.v_float;
3195 else
3196#endif
3197 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 break;
3199
3200 case ISN_CHECKNR:
3201 {
3202 int error = FALSE;
3203
3204 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003205 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003207 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 (void)tv_get_number_chk(tv, &error);
3209 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003210 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 }
3212 break;
3213
3214 case ISN_CHECKTYPE:
3215 {
3216 checktype_T *ct = &iptr->isn_arg.type;
3217
3218 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003219 SOURCING_LNUM = iptr->isn_lnum;
3220 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
3221 goto on_error;
3222
3223 // number 0 is FALSE, number 1 is TRUE
3224 if (tv->v_type == VAR_NUMBER
3225 && ct->ct_type->tt_type == VAR_BOOL
3226 && (tv->vval.v_number == 0
3227 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003229 tv->v_type = VAR_BOOL;
3230 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003231 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 }
3233 }
3234 break;
3235
Bram Moolenaar9af78762020-06-16 11:34:42 +02003236 case ISN_CHECKLEN:
3237 {
3238 int min_len = iptr->isn_arg.checklen.cl_min_len;
3239 list_T *list = NULL;
3240
3241 tv = STACK_TV_BOT(-1);
3242 if (tv->v_type == VAR_LIST)
3243 list = tv->vval.v_list;
3244 if (list == NULL || list->lv_len < min_len
3245 || (list->lv_len > min_len
3246 && !iptr->isn_arg.checklen.cl_more_OK))
3247 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003248 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003249 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003250 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003251 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003252 }
3253 }
3254 break;
3255
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003256 case ISN_SETTYPE:
3257 {
3258 checktype_T *ct = &iptr->isn_arg.type;
3259
3260 tv = STACK_TV_BOT(-1);
3261 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3262 {
3263 free_type(tv->vval.v_dict->dv_type);
3264 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3265 }
3266 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3267 {
3268 free_type(tv->vval.v_list->lv_type);
3269 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3270 }
3271 }
3272 break;
3273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003275 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 {
3277 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003278 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279
3280 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003281 if (iptr->isn_type == ISN_2BOOL)
3282 {
3283 n = tv2bool(tv);
3284 if (iptr->isn_arg.number) // invert
3285 n = !n;
3286 }
3287 else
3288 {
3289 SOURCING_LNUM = iptr->isn_lnum;
3290 n = tv_get_bool_chk(tv, &error);
3291 if (error)
3292 goto on_error;
3293 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 clear_tv(tv);
3295 tv->v_type = VAR_BOOL;
3296 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3297 }
3298 break;
3299
3300 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003301 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003302 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003303 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3304 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3305 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 break;
3307
Bram Moolenaar08597872020-12-10 19:43:40 +01003308 case ISN_RANGE:
3309 {
3310 exarg_T ea;
3311 char *errormsg;
3312
3313 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3314 goto failed;
3315 ++ectx.ec_stack.ga_len;
3316 tv = STACK_TV_BOT(-1);
Bram Moolenaarece0b872021-01-08 20:40:45 +01003317 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003318 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003319 ea.addr_type = ADDR_LINES;
3320 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003321 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003322 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003323 goto on_error;
Bram Moolenaar08597872020-12-10 19:43:40 +01003324 if (ea.addr_count == 0)
3325 tv->vval.v_number = curwin->w_cursor.lnum;
3326 else
3327 tv->vval.v_number = ea.line2;
3328 }
3329 break;
3330
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003331 case ISN_PUT:
3332 {
3333 int regname = iptr->isn_arg.put.put_regname;
3334 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3335 char_u *expr = NULL;
3336 int dir = FORWARD;
3337
3338 if (regname == '=')
3339 {
3340 tv = STACK_TV_BOT(-1);
3341 if (tv->v_type == VAR_STRING)
3342 expr = tv->vval.v_string;
3343 else
3344 {
3345 expr = typval_tostring(tv); // allocates value
3346 clear_tv(tv);
3347 }
3348 --ectx.ec_stack.ga_len;
3349 }
Bram Moolenaar08597872020-12-10 19:43:40 +01003350 if (lnum < -2)
3351 {
3352 // line number was put on the stack by ISN_RANGE
3353 tv = STACK_TV_BOT(-1);
3354 curwin->w_cursor.lnum = tv->vval.v_number;
3355 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3356 dir = BACKWARD;
3357 --ectx.ec_stack.ga_len;
3358 }
3359 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003360 // :put! above cursor
3361 dir = BACKWARD;
3362 else if (lnum >= 0)
3363 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
3364 check_cursor();
3365 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3366 vim_free(expr);
3367 }
3368 break;
3369
Bram Moolenaar02194d22020-10-24 23:08:38 +02003370 case ISN_CMDMOD:
3371 save_cmdmod = cmdmod;
3372 restore_cmdmod = TRUE;
3373 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3374 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003375 break;
3376
Bram Moolenaar02194d22020-10-24 23:08:38 +02003377 case ISN_CMDMOD_REV:
3378 // filter regprog is owned by the instruction, don't free it
3379 cmdmod.cmod_filter_regmatch.regprog = NULL;
3380 undo_cmdmod(&cmdmod);
3381 cmdmod = save_cmdmod;
3382 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003383 break;
3384
Bram Moolenaar792f7862020-11-23 08:31:18 +01003385 case ISN_UNPACK:
3386 {
3387 int count = iptr->isn_arg.unpack.unp_count;
3388 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3389 list_T *l;
3390 listitem_T *li;
3391 int i;
3392
3393 // Check there is a valid list to unpack.
3394 tv = STACK_TV_BOT(-1);
3395 if (tv->v_type != VAR_LIST)
3396 {
3397 SOURCING_LNUM = iptr->isn_lnum;
3398 emsg(_(e_for_argument_must_be_sequence_of_lists));
3399 goto on_error;
3400 }
3401 l = tv->vval.v_list;
3402 if (l == NULL
3403 || l->lv_len < (semicolon ? count - 1 : count))
3404 {
3405 SOURCING_LNUM = iptr->isn_lnum;
3406 emsg(_(e_list_value_does_not_have_enough_items));
3407 goto on_error;
3408 }
3409 else if (!semicolon && l->lv_len > count)
3410 {
3411 SOURCING_LNUM = iptr->isn_lnum;
3412 emsg(_(e_list_value_has_more_items_than_targets));
3413 goto on_error;
3414 }
3415
3416 CHECK_LIST_MATERIALIZE(l);
3417 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3418 goto failed;
3419 ectx.ec_stack.ga_len += count - 1;
3420
3421 // Variable after semicolon gets a list with the remaining
3422 // items.
3423 if (semicolon)
3424 {
3425 list_T *rem_list =
3426 list_alloc_with_items(l->lv_len - count + 1);
3427
3428 if (rem_list == NULL)
3429 goto failed;
3430 tv = STACK_TV_BOT(-count);
3431 tv->vval.v_list = rem_list;
3432 ++rem_list->lv_refcount;
3433 tv->v_lock = 0;
3434 li = l->lv_first;
3435 for (i = 0; i < count - 1; ++i)
3436 li = li->li_next;
3437 for (i = 0; li != NULL; ++i)
3438 {
3439 list_set_item(rem_list, i, &li->li_tv);
3440 li = li->li_next;
3441 }
3442 --count;
3443 }
3444
3445 // Produce the values in reverse order, first item last.
3446 li = l->lv_first;
3447 for (i = 0; i < count; ++i)
3448 {
3449 tv = STACK_TV_BOT(-i - 1);
3450 copy_tv(&li->li_tv, tv);
3451 li = li->li_next;
3452 }
3453
3454 list_unref(l);
3455 }
3456 break;
3457
Bram Moolenaar389df252020-07-09 21:20:47 +02003458 case ISN_SHUFFLE:
3459 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003460 typval_T tmp_tv;
3461 int item = iptr->isn_arg.shuffle.shfl_item;
3462 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003463
3464 tmp_tv = *STACK_TV_BOT(-item);
3465 for ( ; up > 0 && item > 1; --up)
3466 {
3467 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3468 --item;
3469 }
3470 *STACK_TV_BOT(-item) = tmp_tv;
3471 }
3472 break;
3473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 case ISN_DROP:
3475 --ectx.ec_stack.ga_len;
3476 clear_tv(STACK_TV_BOT(0));
3477 break;
3478 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003479 continue;
3480
Bram Moolenaard032f342020-07-18 18:13:02 +02003481func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003482 // Restore previous function. If the frame pointer is where we started
3483 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003484 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003485 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003486
Bram Moolenaard032f342020-07-18 18:13:02 +02003487 if (func_return(&ectx) == FAIL)
3488 // only fails when out of memory
3489 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003490 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003491
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003492on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003493 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003494 // If "emsg_silent" is set then ignore the error, unless it was set
3495 // when calling the function.
3496 if (did_emsg_cumul + did_emsg == did_emsg_before
3497 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003498 continue;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003499on_fatal_error:
3500 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003501 // If we are not inside a try-catch started here, abort execution.
3502 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003503 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 }
3505
3506done:
3507 // function finished, get result from the stack.
3508 tv = STACK_TV_BOT(-1);
3509 *rettv = *tv;
3510 tv->v_type = VAR_UNKNOWN;
3511 ret = OK;
3512
3513failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003514 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003515 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003516 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003517
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003518 // Deal with any remaining closures, they may be in use somewhere.
3519 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003520 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003521 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003522 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3523 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003524
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003525 estack_pop();
3526 current_sctx = save_current_sctx;
3527
Bram Moolenaar352134b2020-10-17 22:04:08 +02003528 if (*msg_list != NULL && saved_msg_list != NULL)
3529 {
3530 msglist_T **plist = saved_msg_list;
3531
3532 // Append entries from the current msg_list (uncaught exceptions) to
3533 // the saved msg_list.
3534 while (*plist != NULL)
3535 plist = &(*plist)->next;
3536
3537 *plist = *msg_list;
3538 }
3539 msg_list = saved_msg_list;
3540
Bram Moolenaar02194d22020-10-24 23:08:38 +02003541 if (restore_cmdmod)
3542 {
3543 cmdmod.cmod_filter_regmatch.regprog = NULL;
3544 undo_cmdmod(&cmdmod);
3545 cmdmod = save_cmdmod;
3546 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003547 emsg_silent_def = save_emsg_silent_def;
3548 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003549
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003550failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003551 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3553 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003556 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003557
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003558 // Not sure if this is necessary.
3559 suppress_errthrow = save_suppress_errthrow;
3560
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003561 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003562 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003563 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003564 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 return ret;
3566}
3567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003569 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003570 * We don't really need this at runtime, but we do have tests that require it,
3571 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 */
3573 void
3574ex_disassemble(exarg_T *eap)
3575{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003576 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003577 char_u *fname;
3578 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 dfunc_T *dfunc;
3580 isn_T *instr;
3581 int current;
3582 int line_idx = 0;
3583 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003584 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003586 if (STRNCMP(arg, "<lambda>", 8) == 0)
3587 {
3588 arg += 8;
3589 (void)getdigits(&arg);
3590 fname = vim_strnsave(eap->arg, arg - eap->arg);
3591 }
3592 else
3593 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003594 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003595 if (fname == NULL)
3596 {
3597 semsg(_(e_invarg2), eap->arg);
3598 return;
3599 }
3600
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003601 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003602 if (ufunc == NULL)
3603 {
3604 char_u *p = untrans_function_name(fname);
3605
3606 if (p != NULL)
3607 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003608 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003609 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003610 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 if (ufunc == NULL)
3612 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003613 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 return;
3615 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003616 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003617 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3618 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003619 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003621 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 return;
3623 }
3624 if (ufunc->uf_name_exp != NULL)
3625 msg((char *)ufunc->uf_name_exp);
3626 else
3627 msg((char *)ufunc->uf_name);
3628
3629 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3630 instr = dfunc->df_instr;
3631 for (current = 0; current < dfunc->df_instr_count; ++current)
3632 {
3633 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003634 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635
3636 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3637 {
3638 if (current > prev_current)
3639 {
3640 msg_puts("\n\n");
3641 prev_current = current;
3642 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003643 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3644 if (line != NULL)
3645 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 }
3647
3648 switch (iptr->isn_type)
3649 {
3650 case ISN_EXEC:
3651 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3652 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003653 case ISN_EXECCONCAT:
3654 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003655 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003656 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 case ISN_ECHO:
3658 {
3659 echo_T *echo = &iptr->isn_arg.echo;
3660
3661 smsg("%4d %s %d", current,
3662 echo->echo_with_white ? "ECHO" : "ECHON",
3663 echo->echo_count);
3664 }
3665 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003666 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003667 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003668 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003669 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003670 case ISN_ECHOMSG:
3671 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003672 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003673 break;
3674 case ISN_ECHOERR:
3675 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003676 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003677 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003679 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003680 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003681 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003682 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003683 + STACK_FRAME_SIZE));
3684 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003685 smsg("%4d LOAD $%lld", current,
3686 (varnumber_T)(iptr->isn_arg.number));
3687 }
3688 break;
3689 case ISN_LOADOUTER:
3690 {
3691 if (iptr->isn_arg.number < 0)
3692 smsg("%4d LOADOUTER level %d arg[%d]", current,
3693 iptr->isn_arg.outer.outer_depth,
3694 iptr->isn_arg.outer.outer_idx
3695 + STACK_FRAME_SIZE);
3696 else
3697 smsg("%4d LOADOUTER level %d $%d", current,
3698 iptr->isn_arg.outer.outer_depth,
3699 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003700 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 break;
3702 case ISN_LOADV:
3703 smsg("%4d LOADV v:%s", current,
3704 get_vim_var_name(iptr->isn_arg.number));
3705 break;
3706 case ISN_LOADSCRIPT:
3707 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003708 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3709 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003711 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003713 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3714 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003715 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003716 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 }
3718 break;
3719 case ISN_LOADS:
3720 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003721 scriptitem_T *si = SCRIPT_ITEM(
3722 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723
3724 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003725 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 }
3727 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003728 case ISN_LOADAUTO:
3729 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3730 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 case ISN_LOADG:
3732 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3733 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003734 case ISN_LOADB:
3735 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3736 break;
3737 case ISN_LOADW:
3738 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3739 break;
3740 case ISN_LOADT:
3741 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3742 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003743 case ISN_LOADGDICT:
3744 smsg("%4d LOAD g:", current);
3745 break;
3746 case ISN_LOADBDICT:
3747 smsg("%4d LOAD b:", current);
3748 break;
3749 case ISN_LOADWDICT:
3750 smsg("%4d LOAD w:", current);
3751 break;
3752 case ISN_LOADTDICT:
3753 smsg("%4d LOAD t:", current);
3754 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 case ISN_LOADOPT:
3756 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3757 break;
3758 case ISN_LOADENV:
3759 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3760 break;
3761 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003762 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763 break;
3764
3765 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01003766 if (iptr->isn_arg.number < 0)
3767 smsg("%4d STORE arg[%lld]", current,
3768 iptr->isn_arg.number + STACK_FRAME_SIZE);
3769 else
3770 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
3771 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003772 case ISN_STOREOUTER:
3773 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003774 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003775 smsg("%4d STOREOUTEr level %d arg[%d]", current,
3776 iptr->isn_arg.outer.outer_depth,
3777 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003778 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003779 smsg("%4d STOREOUTER level %d $%d", current,
3780 iptr->isn_arg.outer.outer_depth,
3781 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003782 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003784 case ISN_STOREV:
3785 smsg("%4d STOREV v:%s", current,
3786 get_vim_var_name(iptr->isn_arg.number));
3787 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003788 case ISN_STOREAUTO:
3789 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3790 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003792 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3793 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003794 case ISN_STOREB:
3795 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3796 break;
3797 case ISN_STOREW:
3798 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3799 break;
3800 case ISN_STORET:
3801 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3802 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003803 case ISN_STORES:
3804 {
3805 scriptitem_T *si = SCRIPT_ITEM(
3806 iptr->isn_arg.loadstore.ls_sid);
3807
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003808 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003809 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 break;
3812 case ISN_STORESCRIPT:
3813 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003814 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3815 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003817 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003819 smsg("%4d STORESCRIPT %s-%d in %s", current,
3820 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003821 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003822 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 }
3824 break;
3825 case ISN_STOREOPT:
3826 smsg("%4d STOREOPT &%s", current,
3827 iptr->isn_arg.storeopt.so_name);
3828 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003829 case ISN_STOREENV:
3830 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3831 break;
3832 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003833 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003834 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835 case ISN_STORENR:
3836 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003837 iptr->isn_arg.storenr.stnr_val,
3838 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 break;
3840
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003841 case ISN_STOREINDEX:
3842 switch (iptr->isn_arg.vartype)
3843 {
3844 case VAR_LIST:
3845 smsg("%4d STORELIST", current);
3846 break;
3847 case VAR_DICT:
3848 smsg("%4d STOREDICT", current);
3849 break;
3850 case VAR_ANY:
3851 smsg("%4d STOREINDEX", current);
3852 break;
3853 default: break;
3854 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003855 break;
3856
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 // constants
3858 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003859 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003860 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 break;
3862 case ISN_PUSHBOOL:
3863 case ISN_PUSHSPEC:
3864 smsg("%4d PUSH %s", current,
3865 get_var_special_name(iptr->isn_arg.number));
3866 break;
3867 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003868#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003870#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 break;
3872 case ISN_PUSHS:
3873 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3874 break;
3875 case ISN_PUSHBLOB:
3876 {
3877 char_u *r;
3878 char_u numbuf[NUMBUFLEN];
3879 char_u *tofree;
3880
3881 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003882 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 vim_free(tofree);
3884 }
3885 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003886 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003887 {
3888 char *name = (char *)iptr->isn_arg.string;
3889
3890 smsg("%4d PUSHFUNC \"%s\"", current,
3891 name == NULL ? "[none]" : name);
3892 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003893 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003894 case ISN_PUSHCHANNEL:
3895#ifdef FEAT_JOB_CHANNEL
3896 {
3897 channel_T *channel = iptr->isn_arg.channel;
3898
3899 smsg("%4d PUSHCHANNEL %d", current,
3900 channel == NULL ? 0 : channel->ch_id);
3901 }
3902#endif
3903 break;
3904 case ISN_PUSHJOB:
3905#ifdef FEAT_JOB_CHANNEL
3906 {
3907 typval_T tv;
3908 char_u *name;
3909
3910 tv.v_type = VAR_JOB;
3911 tv.vval.v_job = iptr->isn_arg.job;
3912 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003913 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003914 }
3915#endif
3916 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 case ISN_PUSHEXC:
3918 smsg("%4d PUSH v:exception", current);
3919 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003920 case ISN_UNLET:
3921 smsg("%4d UNLET%s %s", current,
3922 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3923 iptr->isn_arg.unlet.ul_name);
3924 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003925 case ISN_UNLETENV:
3926 smsg("%4d UNLETENV%s $%s", current,
3927 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3928 iptr->isn_arg.unlet.ul_name);
3929 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003930 case ISN_UNLETINDEX:
3931 smsg("%4d UNLETINDEX", current);
3932 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003933 case ISN_LOCKCONST:
3934 smsg("%4d LOCKCONST", current);
3935 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003937 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003938 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 break;
3940 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003941 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003942 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 break;
3944
3945 // function call
3946 case ISN_BCALL:
3947 {
3948 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3949
3950 smsg("%4d BCALL %s(argc %d)", current,
3951 internal_func_name(cbfunc->cbf_idx),
3952 cbfunc->cbf_argcount);
3953 }
3954 break;
3955 case ISN_DCALL:
3956 {
3957 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3958 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3959 + cdfunc->cdf_idx;
3960
3961 smsg("%4d DCALL %s(argc %d)", current,
3962 df->df_ufunc->uf_name_exp != NULL
3963 ? df->df_ufunc->uf_name_exp
3964 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3965 }
3966 break;
3967 case ISN_UCALL:
3968 {
3969 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3970
3971 smsg("%4d UCALL %s(argc %d)", current,
3972 cufunc->cuf_name, cufunc->cuf_argcount);
3973 }
3974 break;
3975 case ISN_PCALL:
3976 {
3977 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3978
3979 smsg("%4d PCALL%s (argc %d)", current,
3980 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3981 }
3982 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003983 case ISN_PCALL_END:
3984 smsg("%4d PCALL end", current);
3985 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 case ISN_RETURN:
3987 smsg("%4d RETURN", current);
3988 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003989 case ISN_RETURN_ZERO:
3990 smsg("%4d RETURN 0", current);
3991 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 case ISN_FUNCREF:
3993 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003994 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003996 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003998 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 }
4000 break;
4001
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004002 case ISN_NEWFUNC:
4003 {
4004 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4005
4006 smsg("%4d NEWFUNC %s %s", current,
4007 newfunc->nf_lambda, newfunc->nf_global);
4008 }
4009 break;
4010
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004011 case ISN_DEF:
4012 {
4013 char_u *name = iptr->isn_arg.string;
4014
4015 smsg("%4d DEF %s", current,
4016 name == NULL ? (char_u *)"" : name);
4017 }
4018 break;
4019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 case ISN_JUMP:
4021 {
4022 char *when = "?";
4023
4024 switch (iptr->isn_arg.jump.jump_when)
4025 {
4026 case JUMP_ALWAYS:
4027 when = "JUMP";
4028 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 case JUMP_AND_KEEP_IF_TRUE:
4030 when = "JUMP_AND_KEEP_IF_TRUE";
4031 break;
4032 case JUMP_IF_FALSE:
4033 when = "JUMP_IF_FALSE";
4034 break;
4035 case JUMP_AND_KEEP_IF_FALSE:
4036 when = "JUMP_AND_KEEP_IF_FALSE";
4037 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004038 case JUMP_IF_COND_FALSE:
4039 when = "JUMP_IF_COND_FALSE";
4040 break;
4041 case JUMP_IF_COND_TRUE:
4042 when = "JUMP_IF_COND_TRUE";
4043 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004045 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 iptr->isn_arg.jump.jump_where);
4047 }
4048 break;
4049
4050 case ISN_FOR:
4051 {
4052 forloop_T *forloop = &iptr->isn_arg.forloop;
4053
4054 smsg("%4d FOR $%d -> %d", current,
4055 forloop->for_idx, forloop->for_end);
4056 }
4057 break;
4058
4059 case ISN_TRY:
4060 {
4061 try_T *try = &iptr->isn_arg.try;
4062
4063 smsg("%4d TRY catch -> %d, finally -> %d", current,
4064 try->try_catch, try->try_finally);
4065 }
4066 break;
4067 case ISN_CATCH:
4068 // TODO
4069 smsg("%4d CATCH", current);
4070 break;
4071 case ISN_ENDTRY:
4072 smsg("%4d ENDTRY", current);
4073 break;
4074 case ISN_THROW:
4075 smsg("%4d THROW", current);
4076 break;
4077
4078 // expression operations on number
4079 case ISN_OPNR:
4080 case ISN_OPFLOAT:
4081 case ISN_OPANY:
4082 {
4083 char *what;
4084 char *ins;
4085
4086 switch (iptr->isn_arg.op.op_type)
4087 {
4088 case EXPR_MULT: what = "*"; break;
4089 case EXPR_DIV: what = "/"; break;
4090 case EXPR_REM: what = "%"; break;
4091 case EXPR_SUB: what = "-"; break;
4092 case EXPR_ADD: what = "+"; break;
4093 default: what = "???"; break;
4094 }
4095 switch (iptr->isn_type)
4096 {
4097 case ISN_OPNR: ins = "OPNR"; break;
4098 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4099 case ISN_OPANY: ins = "OPANY"; break;
4100 default: ins = "???"; break;
4101 }
4102 smsg("%4d %s %s", current, ins, what);
4103 }
4104 break;
4105
4106 case ISN_COMPAREBOOL:
4107 case ISN_COMPARESPECIAL:
4108 case ISN_COMPARENR:
4109 case ISN_COMPAREFLOAT:
4110 case ISN_COMPARESTRING:
4111 case ISN_COMPAREBLOB:
4112 case ISN_COMPARELIST:
4113 case ISN_COMPAREDICT:
4114 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 case ISN_COMPAREANY:
4116 {
4117 char *p;
4118 char buf[10];
4119 char *type;
4120
4121 switch (iptr->isn_arg.op.op_type)
4122 {
4123 case EXPR_EQUAL: p = "=="; break;
4124 case EXPR_NEQUAL: p = "!="; break;
4125 case EXPR_GREATER: p = ">"; break;
4126 case EXPR_GEQUAL: p = ">="; break;
4127 case EXPR_SMALLER: p = "<"; break;
4128 case EXPR_SEQUAL: p = "<="; break;
4129 case EXPR_MATCH: p = "=~"; break;
4130 case EXPR_IS: p = "is"; break;
4131 case EXPR_ISNOT: p = "isnot"; break;
4132 case EXPR_NOMATCH: p = "!~"; break;
4133 default: p = "???"; break;
4134 }
4135 STRCPY(buf, p);
4136 if (iptr->isn_arg.op.op_ic == TRUE)
4137 strcat(buf, "?");
4138 switch(iptr->isn_type)
4139 {
4140 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4141 case ISN_COMPARESPECIAL:
4142 type = "COMPARESPECIAL"; break;
4143 case ISN_COMPARENR: type = "COMPARENR"; break;
4144 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4145 case ISN_COMPARESTRING:
4146 type = "COMPARESTRING"; break;
4147 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4148 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4149 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4150 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4152 default: type = "???"; break;
4153 }
4154
4155 smsg("%4d %s %s", current, type, buf);
4156 }
4157 break;
4158
4159 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4160 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4161
4162 // expression operations
4163 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004164 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004165 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004166 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004167 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004168 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004169 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004170 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4171 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004172 case ISN_SLICE: smsg("%4d SLICE %lld",
4173 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004174 case ISN_GETITEM: smsg("%4d ITEM %lld",
4175 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004176 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4177 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 iptr->isn_arg.string); break;
4179 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4180
4181 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004182 case ISN_CHECKTYPE:
4183 {
4184 char *tofree;
4185
4186 smsg("%4d CHECKTYPE %s stack[%d]", current,
4187 type_name(iptr->isn_arg.type.ct_type, &tofree),
4188 iptr->isn_arg.type.ct_off);
4189 vim_free(tofree);
4190 break;
4191 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004192 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4193 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4194 iptr->isn_arg.checklen.cl_min_len);
4195 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004196 case ISN_SETTYPE:
4197 {
4198 char *tofree;
4199
4200 smsg("%4d SETTYPE %s", current,
4201 type_name(iptr->isn_arg.type.ct_type, &tofree));
4202 vim_free(tofree);
4203 break;
4204 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004205 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 case ISN_2BOOL: if (iptr->isn_arg.number)
4207 smsg("%4d INVERT (!val)", current);
4208 else
4209 smsg("%4d 2BOOL (!!val)", current);
4210 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004211 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004212 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004213 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004214 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004215 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004216 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004217 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4218 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004219 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004220 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4221 smsg("%4d PUT %c above range",
4222 current, iptr->isn_arg.put.put_regname);
4223 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4224 smsg("%4d PUT %c range",
4225 current, iptr->isn_arg.put.put_regname);
4226 else
4227 smsg("%4d PUT %c %ld", current,
4228 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004229 (long)iptr->isn_arg.put.put_lnum);
4230 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231
Bram Moolenaar02194d22020-10-24 23:08:38 +02004232 // TODO: summarize modifiers
4233 case ISN_CMDMOD:
4234 {
4235 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004236 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004237 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4238
4239 buf = alloc(len + 1);
4240 if (buf != NULL)
4241 {
4242 (void)produce_cmdmods(
4243 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4244 smsg("%4d CMDMOD %s", current, buf);
4245 vim_free(buf);
4246 }
4247 break;
4248 }
4249 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004250
Bram Moolenaar792f7862020-11-23 08:31:18 +01004251 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4252 iptr->isn_arg.unpack.unp_count,
4253 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4254 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004255 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4256 iptr->isn_arg.shuffle.shfl_item,
4257 iptr->isn_arg.shuffle.shfl_up);
4258 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 case ISN_DROP: smsg("%4d DROP", current); break;
4260 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004261
4262 out_flush(); // output one line at a time
4263 ui_breakcheck();
4264 if (got_int)
4265 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267}
4268
4269/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004270 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271 * list, etc. Mostly like what JavaScript does, except that empty list and
4272 * empty dictionary are FALSE.
4273 */
4274 int
4275tv2bool(typval_T *tv)
4276{
4277 switch (tv->v_type)
4278 {
4279 case VAR_NUMBER:
4280 return tv->vval.v_number != 0;
4281 case VAR_FLOAT:
4282#ifdef FEAT_FLOAT
4283 return tv->vval.v_float != 0.0;
4284#else
4285 break;
4286#endif
4287 case VAR_PARTIAL:
4288 return tv->vval.v_partial != NULL;
4289 case VAR_FUNC:
4290 case VAR_STRING:
4291 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4292 case VAR_LIST:
4293 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4294 case VAR_DICT:
4295 return tv->vval.v_dict != NULL
4296 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4297 case VAR_BOOL:
4298 case VAR_SPECIAL:
4299 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4300 case VAR_JOB:
4301#ifdef FEAT_JOB_CHANNEL
4302 return tv->vval.v_job != NULL;
4303#else
4304 break;
4305#endif
4306 case VAR_CHANNEL:
4307#ifdef FEAT_JOB_CHANNEL
4308 return tv->vval.v_channel != NULL;
4309#else
4310 break;
4311#endif
4312 case VAR_BLOB:
4313 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4314 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004315 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 case VAR_VOID:
4317 break;
4318 }
4319 return FALSE;
4320}
4321
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004322 void
4323emsg_using_string_as(typval_T *tv, int as_number)
4324{
4325 semsg(_(as_number ? e_using_string_as_number_str
4326 : e_using_string_as_bool_str),
4327 tv->vval.v_string == NULL
4328 ? (char_u *)"" : tv->vval.v_string);
4329}
4330
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331/*
4332 * If "tv" is a string give an error and return FAIL.
4333 */
4334 int
4335check_not_string(typval_T *tv)
4336{
4337 if (tv->v_type == VAR_STRING)
4338 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004339 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 clear_tv(tv);
4341 return FAIL;
4342 }
4343 return OK;
4344}
4345
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346
4347#endif // FEAT_EVAL