blob: a0d7f630bc90885a028113a060539d305660928b [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020027 int tcd_frame_idx; // ec_frame_idx when ISN_TRY was encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028 int tcd_catch_idx; // instruction of the first catch
29 int tcd_finally_idx; // instruction of the finally block
30 int tcd_caught; // catch block entered
31 int tcd_return; // when TRUE return from end of :finally
32} trycmd_T;
33
34
35// A stack is used to store:
36// - arguments passed to a :def function
37// - info about the calling function, to use when returning
38// - local variables
39// - temporary values
40//
41// In detail (FP == Frame Pointer):
42// arg1 first argument from caller (if present)
43// arg2 second argument from caller (if present)
44// extra_arg1 any missing optional argument default value
45// FP -> cur_func calling function
46// current previous instruction pointer
47// frame_ptr previous Frame Pointer
48// var1 space for local variable
49// var2 space for local variable
50// .... fixed space for max. number of local variables
51// temp temporary values
52// .... flexible space for temporary values (can grow big)
53
54/*
55 * Execution context.
56 */
57typedef struct {
58 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020059 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020061 garray_T *ec_outer_stack; // stack used for closures
62 int ec_outer_frame; // stack frame in ec_outer_stack
63
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064 garray_T ec_trystack; // stack of trycmd_T values
65 int ec_in_catch; // when TRUE in catch or finally block
66
67 int ec_dfunc_idx; // current function index
68 isn_T *ec_instr; // array with instructions
69 int ec_iidx; // index in ec_instr: instruction to execute
70} ectx_T;
71
72// Get pointer to item relative to the bottom of the stack, -1 is the last one.
73#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
74
75/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010076 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077 */
78 static int
79ufunc_argcount(ufunc_T *ufunc)
80{
81 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
82}
83
84/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010085 * Set the instruction index, depending on omitted arguments, where the default
86 * values are to be computed. If all optional arguments are present, start
87 * with the function body.
88 * The expression evaluation is at the start of the instructions:
89 * 0 -> EVAL default1
90 * STORE arg[-2]
91 * 1 -> EVAL default2
92 * STORE arg[-1]
93 * 2 -> function body
94 */
95 static void
96init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
97{
98 if (ufunc->uf_def_args.ga_len == 0)
99 ectx->ec_iidx = 0;
100 else
101 {
102 int defcount = ufunc->uf_args.ga_len - argcount;
103
104 // If there is a varargs argument defcount can be negative, no defaults
105 // to evaluate then.
106 if (defcount < 0)
107 defcount = 0;
108 ectx->ec_iidx = ufunc->uf_def_arg_idx[
109 ufunc->uf_def_args.ga_len - defcount];
110 }
111}
112
113/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200114 * Create a new list from "count" items at the bottom of the stack.
115 * When "count" is zero an empty list is added to the stack.
116 */
117 static int
118exe_newlist(int count, ectx_T *ectx)
119{
120 list_T *list = list_alloc_with_items(count);
121 int idx;
122 typval_T *tv;
123
124 if (list == NULL)
125 return FAIL;
126 for (idx = 0; idx < count; ++idx)
127 list_set_item(list, idx, STACK_TV_BOT(idx - count));
128
129 if (count > 0)
130 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200131 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200132 return FAIL;
133 else
134 ++ectx->ec_stack.ga_len;
135 tv = STACK_TV_BOT(-1);
136 tv->v_type = VAR_LIST;
137 tv->vval.v_list = list;
138 ++list->lv_refcount;
139 return OK;
140}
141
142/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 * Call compiled function "cdf_idx" from compiled code.
144 *
145 * Stack has:
146 * - current arguments (already there)
147 * - omitted optional argument (default values) added here
148 * - stack frame:
149 * - pointer to calling function
150 * - Index of next instruction in calling function
151 * - previous frame pointer
152 * - reserved space for local variables
153 */
154 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200155call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100156{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200157 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
159 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200160 int arg_to_add;
161 int vararg_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200163 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
165 if (dfunc->df_deleted)
166 {
167 emsg_funcname(e_func_deleted, ufunc->uf_name);
168 return FAIL;
169 }
170
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200171 if (ufunc->uf_va_name != NULL)
172 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200173 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200174 // Stack at time of call with 2 varargs:
175 // normal_arg
176 // optional_arg
177 // vararg_1
178 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 // After creating the list:
180 // normal_arg
181 // optional_arg
182 // vararg-list
183 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200184 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200185 // After creating the list
186 // normal_arg
187 // (space for optional_arg)
188 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200189 vararg_count = argcount - ufunc->uf_args.ga_len;
190 if (vararg_count < 0)
191 vararg_count = 0;
192 else
193 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200194 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200195 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196
197 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200198 }
199
Bram Moolenaarfe270812020-04-11 22:31:27 +0200200 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200201 if (arg_to_add < 0)
202 {
203 iemsg("Argument count wrong?");
204 return FAIL;
205 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200206 if (ga_grow(&ectx->ec_stack, arg_to_add + 3
207 + dfunc->df_varcount + dfunc->df_closure_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208 return FAIL;
209
Bram Moolenaarfe270812020-04-11 22:31:27 +0200210 // Move the vararg-list to below the missing optional arguments.
211 if (vararg_count > 0 && arg_to_add > 0)
212 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100213
214 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200215 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200216 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200217 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218
219 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100220 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
221 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200222 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
223 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224
225 // Initialize local variables
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200226 for (idx = 0; idx < dfunc->df_varcount + dfunc->df_closure_count; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100227 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200228 ectx->ec_stack.ga_len += STACK_FRAME_SIZE
229 + dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230
231 // Set execution state to the start of the called function.
232 ectx->ec_dfunc_idx = cdf_idx;
233 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200234 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
235 if (entry != NULL)
236 {
237 // Set the script context to the script where the function was defined.
238 // TODO: save more than the SID?
239 entry->es_save_sid = current_sctx.sc_sid;
240 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
241 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100242
243 // Decide where to start execution, handles optional arguments.
244 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100245
246 return OK;
247}
248
249// Get pointer to item in the stack.
250#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
251
252/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200253 * Used when returning from a function: Check if any closure is still
254 * referenced. If so then move the arguments and variables to a separate piece
255 * of stack to be used when the closure is called.
256 * When "free_arguments" is TRUE the arguments are to be freed.
257 * Returns FAIL when out of memory.
258 */
259 static int
260handle_closure_in_use(ectx_T *ectx, int free_arguments)
261{
262 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
263 + ectx->ec_dfunc_idx;
264 int argcount = ufunc_argcount(dfunc->df_ufunc);
265 int top = ectx->ec_frame_idx - argcount;
266 int idx;
267 typval_T *tv;
268 int closure_in_use = FALSE;
269
270 // Check if any created closure is still in use.
271 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
272 {
273 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
274 + dfunc->df_varcount + idx);
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200275 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
276 && tv->vval.v_partial->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200277 {
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200278 int refcount = tv->vval.v_partial->pt_refcount;
279 int i;
280
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200281 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200282 // unreferenced on return.
283 for (i = 0; i < dfunc->df_varcount; ++i)
284 {
285 typval_T *stv = STACK_TV(ectx->ec_frame_idx
286 + STACK_FRAME_SIZE + i);
287 if (stv->v_type == VAR_PARTIAL
288 && tv->vval.v_partial == stv->vval.v_partial)
289 --refcount;
290 }
291 if (refcount > 1)
292 {
293 closure_in_use = TRUE;
294 break;
295 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200296 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200297 }
298
299 if (closure_in_use)
300 {
301 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
302 typval_T *stack;
303
304 // A closure is using the arguments and/or local variables.
305 // Move them to the called function.
306 if (funcstack == NULL)
307 return FAIL;
308 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
309 + dfunc->df_varcount;
310 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
311 funcstack->fs_ga.ga_data = stack;
312 if (stack == NULL)
313 {
314 vim_free(funcstack);
315 return FAIL;
316 }
317
318 // Move or copy the arguments.
319 for (idx = 0; idx < argcount; ++idx)
320 {
321 tv = STACK_TV(top + idx);
322 if (free_arguments)
323 {
324 *(stack + idx) = *tv;
325 tv->v_type = VAR_UNKNOWN;
326 }
327 else
328 copy_tv(tv, stack + idx);
329 }
330 // Move the local variables.
331 for (idx = 0; idx < dfunc->df_varcount; ++idx)
332 {
333 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200334
335 // Do not copy a partial created for a local function.
336 // TODO: this won't work if the closure actually uses it. But when
337 // keeping it it gets complicated: it will create a reference cycle
338 // inside the partial, thus needs special handling for garbage
339 // collection.
340 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
341 {
342 int i;
343 typval_T *ctv;
344
345 for (i = 0; i < dfunc->df_closure_count; ++i)
346 {
347 ctv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
348 + dfunc->df_varcount + i);
349 if (tv->vval.v_partial == ctv->vval.v_partial)
350 break;
351 }
352 if (i < dfunc->df_closure_count)
353 {
354 (stack + argcount + STACK_FRAME_SIZE + idx)->v_type =
355 VAR_UNKNOWN;
356 continue;
357 }
358 }
359
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200360 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
361 tv->v_type = VAR_UNKNOWN;
362 }
363
364 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
365 {
366 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
367 + dfunc->df_varcount + idx);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200368 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200369 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200370 partial_T *partial = tv->vval.v_partial;
371
372 if (partial->pt_refcount > 1)
373 {
374 ++funcstack->fs_refcount;
375 partial->pt_funcstack = funcstack;
376 partial->pt_ectx_stack = &funcstack->fs_ga;
377 partial->pt_ectx_frame = ectx->ec_frame_idx - top;
378 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200379 }
380 }
381 }
382
383 return OK;
384}
385
386/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100387 * Return from the current function.
388 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200389 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390func_return(ectx_T *ectx)
391{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100392 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200393 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
394 + ectx->ec_dfunc_idx;
395 int argcount = ufunc_argcount(dfunc->df_ufunc);
396 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200397 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398
399 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200400 entry = estack_pop();
401 if (entry != NULL)
402 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200404 if (handle_closure_in_use(ectx, TRUE) == FAIL)
405 return FAIL;
406
407 // Clear the arguments.
408 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
409 clear_tv(STACK_TV(idx));
410
411 // Clear local variables and temp values, but not the return value.
412 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100413 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100415
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100416 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200417 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
418 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
419 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
421 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100422
423 // Reset the stack to the position before the call, move the return value
424 // to the top of the stack.
425 idx = ectx->ec_stack.ga_len - 1;
426 ectx->ec_stack.ga_len = top + 1;
427 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200428
429 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430}
431
432#undef STACK_TV
433
434/*
435 * Prepare arguments and rettv for calling a builtin or user function.
436 */
437 static int
438call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
439{
440 int idx;
441 typval_T *tv;
442
443 // Move arguments from bottom of the stack to argvars[] and add terminator.
444 for (idx = 0; idx < argcount; ++idx)
445 argvars[idx] = *STACK_TV_BOT(idx - argcount);
446 argvars[argcount].v_type = VAR_UNKNOWN;
447
448 // Result replaces the arguments on the stack.
449 if (argcount > 0)
450 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200451 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100452 return FAIL;
453 else
454 ++ectx->ec_stack.ga_len;
455
456 // Default return value is zero.
457 tv = STACK_TV_BOT(-1);
458 tv->v_type = VAR_NUMBER;
459 tv->vval.v_number = 0;
460
461 return OK;
462}
463
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200464// Ugly global to avoid passing the execution context around through many
465// layers.
466static ectx_T *current_ectx = NULL;
467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468/*
469 * Call a builtin function by index.
470 */
471 static int
472call_bfunc(int func_idx, int argcount, ectx_T *ectx)
473{
474 typval_T argvars[MAX_FUNC_ARGS];
475 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200476 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200477 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100478
479 if (call_prepare(argcount, argvars, ectx) == FAIL)
480 return FAIL;
481
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200482 // Call the builtin function. Set "current_ectx" so that when it
483 // recursively invokes call_def_function() a closure context can be set.
484 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100485 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200486 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487
488 // Clear the arguments.
489 for (idx = 0; idx < argcount; ++idx)
490 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200491
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200492 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200493 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100494 return OK;
495}
496
497/*
498 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100499 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 */
501 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100502call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503{
504 typval_T argvars[MAX_FUNC_ARGS];
505 funcexe_T funcexe;
506 int error;
507 int idx;
508
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200509 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200510 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
511 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200512 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100513 {
514 // The function has been compiled, can call it quickly. For a function
515 // that was defined later: we can call it directly next time.
516 if (iptr != NULL)
517 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100518 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100519 iptr->isn_type = ISN_DCALL;
520 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
521 iptr->isn_arg.dfunc.cdf_argcount = argcount;
522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100524 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100525
526 if (call_prepare(argcount, argvars, ectx) == FAIL)
527 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200528 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 funcexe.evaluate = TRUE;
530
531 // Call the user function. Result goes in last position on the stack.
532 // TODO: add selfdict if there is one
533 error = call_user_func_check(ufunc, argcount, argvars,
534 STACK_TV_BOT(-1), &funcexe, NULL);
535
536 // Clear the arguments.
537 for (idx = 0; idx < argcount; ++idx)
538 clear_tv(&argvars[idx]);
539
540 if (error != FCERR_NONE)
541 {
542 user_func_error(error, ufunc->uf_name);
543 return FAIL;
544 }
545 return OK;
546}
547
548/*
549 * Execute a function by "name".
550 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100551 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552 * Returns FAIL if not found without an error message.
553 */
554 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100555call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556{
557 ufunc_T *ufunc;
558
559 if (builtin_function(name, -1))
560 {
561 int func_idx = find_internal_func(name);
562
563 if (func_idx < 0)
564 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200565 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566 return FAIL;
567 return call_bfunc(func_idx, argcount, ectx);
568 }
569
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200570 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100572 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573
574 return FAIL;
575}
576
577 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200578call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200580 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200581 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 int called_emsg_before = called_emsg;
583
584 if (tv->v_type == VAR_PARTIAL)
585 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200586 partial_T *pt = tv->vval.v_partial;
587 int i;
588
589 if (pt->pt_argc > 0)
590 {
591 // Make space for arguments from the partial, shift the "argcount"
592 // arguments up.
593 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
594 return FAIL;
595 for (i = 1; i <= argcount; ++i)
596 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
597 ectx->ec_stack.ga_len += pt->pt_argc;
598 argcount += pt->pt_argc;
599
600 // copy the arguments from the partial onto the stack
601 for (i = 0; i < pt->pt_argc; ++i)
602 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604
605 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200606 {
607 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
608
609 // closure may need the function context where it was defined
610 ectx->ec_outer_stack = pt->pt_ectx_stack;
611 ectx->ec_outer_frame = pt->pt_ectx_frame;
612
613 return ret;
614 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 name = pt->pt_name;
616 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200617 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200619 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620 {
621 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200622 semsg(_(e_unknownfunc),
623 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 return FAIL;
625 }
626 return OK;
627}
628
629/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100630 * Store "tv" in variable "name".
631 * This is for s: and g: variables.
632 */
633 static void
634store_var(char_u *name, typval_T *tv)
635{
636 funccal_entry_T entry;
637
638 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200639 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100640 restore_funccal();
641}
642
Bram Moolenaard3aac292020-04-19 14:32:17 +0200643
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100644/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645 * Execute a function by "name".
646 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100647 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100648 */
649 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100650call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100651{
652 int called_emsg_before = called_emsg;
653
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100654 if (call_by_name(name, argcount, ectx, iptr) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 && called_emsg == called_emsg_before)
656 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200657 dictitem_T *v;
658
659 v = find_var(name, NULL, FALSE);
660 if (v == NULL)
661 {
662 semsg(_(e_unknownfunc), name);
663 return FAIL;
664 }
665 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
666 {
667 semsg(_(e_unknownfunc), name);
668 return FAIL;
669 }
670 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671 }
672 return OK;
673}
674
675/*
676 * Call a "def" function from old Vim script.
677 * Return OK or FAIL.
678 */
679 int
680call_def_function(
681 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200682 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200684 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685 typval_T *rettv) // return value
686{
687 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200688 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200689 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100690 typval_T *tv;
691 int idx;
692 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100693 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200694 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200695 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200696 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697
698// Get pointer to item in the stack.
699#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
700
701// Get pointer to item at the bottom of the stack, -1 is the bottom.
702#undef STACK_TV_BOT
703#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
704
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200705// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200706#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 +0100707
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200708// Like STACK_TV_VAR but use the outer scope
709#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
710
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200711 if (ufunc->uf_def_status == UF_NOT_COMPILED
712 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200713 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200714 {
715 if (called_emsg == called_emsg_before)
716 semsg(_("E1091: Function is not compiled: %s"),
717 ufunc->uf_name_exp == NULL
718 ? ufunc->uf_name : ufunc->uf_name_exp);
Bram Moolenaar822ba242020-05-24 23:00:18 +0200719 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200720 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200721
Bram Moolenaar09689a02020-05-09 22:50:08 +0200722 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200723 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200724 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
725 + ufunc->uf_dfunc_idx;
726 if (dfunc->df_instr == NULL)
727 return FAIL;
728 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200730 CLEAR_FIELD(ectx);
731 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
732 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
733 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
734 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
736
737 // Put arguments on the stack.
738 for (idx = 0; idx < argc; ++idx)
739 {
740 copy_tv(&argv[idx], STACK_TV_BOT(0));
741 ++ectx.ec_stack.ga_len;
742 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200743
744 // Turn varargs into a list. Empty list if no args.
745 if (ufunc->uf_va_name != NULL)
746 {
747 int vararg_count = argc - ufunc->uf_args.ga_len;
748
749 if (vararg_count < 0)
750 vararg_count = 0;
751 else
752 argc -= vararg_count;
753 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200754 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200755 if (defcount > 0)
756 // Move varargs list to below missing default arguments.
757 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
758 --ectx.ec_stack.ga_len;
759 }
760
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100761 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200762 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100763 if (defcount > 0)
764 for (idx = 0; idx < defcount; ++idx)
765 {
766 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
767 ++ectx.ec_stack.ga_len;
768 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200769 if (ufunc->uf_va_name != NULL)
770 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771
772 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200773 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
774 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200776 if (partial != NULL)
777 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200778 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
779 {
780 // TODO: is this always the right way?
781 ectx.ec_outer_stack = &current_ectx->ec_stack;
782 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
783 }
784 else
785 {
786 ectx.ec_outer_stack = partial->pt_ectx_stack;
787 ectx.ec_outer_frame = partial->pt_ectx_frame;
788 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200789 }
790
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100791 // dummy frame entries
792 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
793 {
794 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
795 ++ectx.ec_stack.ga_len;
796 }
797
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200798 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200799 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200800 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
801 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200802 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200804 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200805 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200806 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200807
808 ectx.ec_instr = dfunc->df_instr;
809 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100810
Bram Moolenaara26b9702020-04-18 19:53:28 +0200811 // Commands behave like vim9script.
812 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
813
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100814 // Decide where to start execution, handles optional arguments.
815 init_instr_idx(ufunc, argc, &ectx);
816
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 for (;;)
818 {
819 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100820
Bram Moolenaar270d0382020-05-15 21:42:53 +0200821 if (++breakcheck_count >= 100)
822 {
823 line_breakcheck();
824 breakcheck_count = 0;
825 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100826 if (got_int)
827 {
828 // Turn CTRL-C into an exception.
829 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100830 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100831 goto failed;
832 did_throw = TRUE;
833 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834
Bram Moolenaara26b9702020-04-18 19:53:28 +0200835 if (did_emsg && msg_list != NULL && *msg_list != NULL)
836 {
837 // Turn an error message into an exception.
838 did_emsg = FALSE;
839 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
840 goto failed;
841 did_throw = TRUE;
842 *msg_list = NULL;
843 }
844
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 if (did_throw && !ectx.ec_in_catch)
846 {
847 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100848 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100849
850 // An exception jumps to the first catch, finally, or returns from
851 // the current function.
852 if (trystack->ga_len > 0)
853 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200854 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100855 {
856 // jump to ":catch" or ":finally"
857 ectx.ec_in_catch = TRUE;
858 ectx.ec_iidx = trycmd->tcd_catch_idx;
859 }
860 else
861 {
862 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200863 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864 {
865 // At the toplevel we are done. Push a dummy return value.
Bram Moolenaar270d0382020-05-15 21:42:53 +0200866 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867 goto failed;
868 tv = STACK_TV_BOT(0);
869 tv->v_type = VAR_NUMBER;
870 tv->vval.v_number = 0;
871 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100872 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200873 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
874 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875 goto done;
876 }
877
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200878 if (func_return(&ectx) == FAIL)
879 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880 }
881 continue;
882 }
883
884 iptr = &ectx.ec_instr[ectx.ec_iidx++];
885 switch (iptr->isn_type)
886 {
887 // execute Ex command line
888 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200889 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890 do_cmdline_cmd(iptr->isn_arg.string);
891 break;
892
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200893 // execute Ex command from pieces on the stack
894 case ISN_EXECCONCAT:
895 {
896 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200897 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200898 int pass;
899 int i;
900 char_u *cmd = NULL;
901 char_u *str;
902
903 for (pass = 1; pass <= 2; ++pass)
904 {
905 for (i = 0; i < count; ++i)
906 {
907 tv = STACK_TV_BOT(i - count);
908 str = tv->vval.v_string;
909 if (str != NULL && *str != NUL)
910 {
911 if (pass == 2)
912 STRCPY(cmd + len, str);
913 len += STRLEN(str);
914 }
915 if (pass == 2)
916 clear_tv(tv);
917 }
918 if (pass == 1)
919 {
920 cmd = alloc(len + 1);
921 if (cmd == NULL)
922 goto failed;
923 len = 0;
924 }
925 }
926
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200927 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200928 do_cmdline_cmd(cmd);
929 vim_free(cmd);
930 }
931 break;
932
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933 // execute :echo {string} ...
934 case ISN_ECHO:
935 {
936 int count = iptr->isn_arg.echo.echo_count;
937 int atstart = TRUE;
938 int needclr = TRUE;
939
940 for (idx = 0; idx < count; ++idx)
941 {
942 tv = STACK_TV_BOT(idx - count);
943 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
944 &atstart, &needclr);
945 clear_tv(tv);
946 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100947 if (needclr)
948 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949 ectx.ec_stack.ga_len -= count;
950 }
951 break;
952
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200953 // :execute {string} ...
954 // :echomsg {string} ...
955 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100956 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200957 case ISN_ECHOMSG:
958 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100959 {
960 int count = iptr->isn_arg.number;
961 garray_T ga;
962 char_u buf[NUMBUFLEN];
963 char_u *p;
964 int len;
965 int failed = FALSE;
966
967 ga_init2(&ga, 1, 80);
968 for (idx = 0; idx < count; ++idx)
969 {
970 tv = STACK_TV_BOT(idx - count);
971 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
972 {
973 emsg(_(e_inval_string));
974 break;
975 }
976 else
977 p = tv_get_string_buf(tv, buf);
978
979 len = (int)STRLEN(p);
980 if (ga_grow(&ga, len + 2) == FAIL)
981 failed = TRUE;
982 else
983 {
984 if (ga.ga_len > 0)
985 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
986 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
987 ga.ga_len += len;
988 }
989 clear_tv(tv);
990 }
991 ectx.ec_stack.ga_len -= count;
992
993 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200994 {
995 if (iptr->isn_type == ISN_EXECUTE)
996 do_cmdline_cmd((char_u *)ga.ga_data);
997 else
998 {
999 msg_sb_eol();
1000 if (iptr->isn_type == ISN_ECHOMSG)
1001 {
1002 msg_attr(ga.ga_data, echo_attr);
1003 out_flush();
1004 }
1005 else
1006 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001007 SOURCING_LNUM = iptr->isn_lnum;
1008 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001009 }
1010 }
1011 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001012 ga_clear(&ga);
1013 }
1014 break;
1015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001016 // load local variable or argument
1017 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001018 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 goto failed;
1020 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1021 ++ectx.ec_stack.ga_len;
1022 break;
1023
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001024 // load variable or argument from outer scope
1025 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001026 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001027 goto failed;
1028 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1029 STACK_TV_BOT(0));
1030 ++ectx.ec_stack.ga_len;
1031 break;
1032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 // load v: variable
1034 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001035 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001036 goto failed;
1037 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1038 ++ectx.ec_stack.ga_len;
1039 break;
1040
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001041 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042 case ISN_LOADSCRIPT:
1043 {
1044 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001045 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046 svar_T *sv;
1047
1048 sv = ((svar_T *)si->sn_var_vals.ga_data)
1049 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001050 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051 goto failed;
1052 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1053 ++ectx.ec_stack.ga_len;
1054 }
1055 break;
1056
1057 // load s: variable in old script
1058 case ISN_LOADS:
1059 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001060 hashtab_T *ht = &SCRIPT_VARS(
1061 iptr->isn_arg.loadstore.ls_sid);
1062 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 if (di == NULL)
1066 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001067 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 goto failed;
1069 }
1070 else
1071 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001072 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073 goto failed;
1074 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1075 ++ectx.ec_stack.ga_len;
1076 }
1077 }
1078 break;
1079
Bram Moolenaard3aac292020-04-19 14:32:17 +02001080 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001082 case ISN_LOADB:
1083 case ISN_LOADW:
1084 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001086 dictitem_T *di = NULL;
1087 hashtab_T *ht = NULL;
1088 char namespace;
1089 switch (iptr->isn_type)
1090 {
1091 case ISN_LOADG:
1092 ht = get_globvar_ht();
1093 namespace = 'g';
1094 break;
1095 case ISN_LOADB:
1096 ht = &curbuf->b_vars->dv_hashtab;
1097 namespace = 'b';
1098 break;
1099 case ISN_LOADW:
1100 ht = &curwin->w_vars->dv_hashtab;
1101 namespace = 'w';
1102 break;
1103 case ISN_LOADT:
1104 ht = &curtab->tp_vars->dv_hashtab;
1105 namespace = 't';
1106 break;
1107 default: // Cannot reach here
1108 goto failed;
1109 }
1110 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112 if (di == NULL)
1113 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001114 semsg(_("E121: Undefined variable: %c:%s"),
1115 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 goto failed;
1117 }
1118 else
1119 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001120 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 goto failed;
1122 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1123 ++ectx.ec_stack.ga_len;
1124 }
1125 }
1126 break;
1127
1128 // load &option
1129 case ISN_LOADOPT:
1130 {
1131 typval_T optval;
1132 char_u *name = iptr->isn_arg.string;
1133
Bram Moolenaara8c17702020-04-01 21:17:24 +02001134 // This is not expected to fail, name is checked during
1135 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001136 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001138 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001139 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 *STACK_TV_BOT(0) = optval;
1141 ++ectx.ec_stack.ga_len;
1142 }
1143 break;
1144
1145 // load $ENV
1146 case ISN_LOADENV:
1147 {
1148 typval_T optval;
1149 char_u *name = iptr->isn_arg.string;
1150
Bram Moolenaar270d0382020-05-15 21:42:53 +02001151 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001153 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001154 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 *STACK_TV_BOT(0) = optval;
1156 ++ectx.ec_stack.ga_len;
1157 }
1158 break;
1159
1160 // load @register
1161 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001162 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163 goto failed;
1164 tv = STACK_TV_BOT(0);
1165 tv->v_type = VAR_STRING;
1166 tv->vval.v_string = get_reg_contents(
1167 iptr->isn_arg.number, GREG_EXPR_SRC);
1168 ++ectx.ec_stack.ga_len;
1169 break;
1170
1171 // store local variable
1172 case ISN_STORE:
1173 --ectx.ec_stack.ga_len;
1174 tv = STACK_TV_VAR(iptr->isn_arg.number);
1175 clear_tv(tv);
1176 *tv = *STACK_TV_BOT(0);
1177 break;
1178
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001179 // store variable or argument in outer scope
1180 case ISN_STOREOUTER:
1181 --ectx.ec_stack.ga_len;
1182 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1183 clear_tv(tv);
1184 *tv = *STACK_TV_BOT(0);
1185 break;
1186
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001187 // store s: variable in old script
1188 case ISN_STORES:
1189 {
1190 hashtab_T *ht = &SCRIPT_VARS(
1191 iptr->isn_arg.loadstore.ls_sid);
1192 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001193 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001194
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001195 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001196 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001197 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001198 else
1199 {
1200 clear_tv(&di->di_tv);
1201 di->di_tv = *STACK_TV_BOT(0);
1202 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001203 }
1204 break;
1205
1206 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 case ISN_STORESCRIPT:
1208 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001209 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 iptr->isn_arg.script.script_sid);
1211 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1212 + iptr->isn_arg.script.script_idx;
1213
1214 --ectx.ec_stack.ga_len;
1215 clear_tv(sv->sv_tv);
1216 *sv->sv_tv = *STACK_TV_BOT(0);
1217 }
1218 break;
1219
1220 // store option
1221 case ISN_STOREOPT:
1222 {
1223 long n = 0;
1224 char_u *s = NULL;
1225 char *msg;
1226
1227 --ectx.ec_stack.ga_len;
1228 tv = STACK_TV_BOT(0);
1229 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001230 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001232 if (s == NULL)
1233 s = (char_u *)"";
1234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001236 // must be VAR_NUMBER, CHECKTYPE makes sure
1237 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1239 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001240 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001241 if (msg != NULL)
1242 {
1243 emsg(_(msg));
1244 goto failed;
1245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246 }
1247 break;
1248
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001249 // store $ENV
1250 case ISN_STOREENV:
1251 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001252 tv = STACK_TV_BOT(0);
1253 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1254 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001255 break;
1256
1257 // store @r
1258 case ISN_STOREREG:
1259 {
1260 int reg = iptr->isn_arg.number;
1261
1262 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001263 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001264 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001265 tv_get_string(tv), -1, FALSE);
1266 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001267 }
1268 break;
1269
1270 // store v: variable
1271 case ISN_STOREV:
1272 --ectx.ec_stack.ga_len;
1273 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1274 == FAIL)
1275 goto failed;
1276 break;
1277
Bram Moolenaard3aac292020-04-19 14:32:17 +02001278 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001280 case ISN_STOREB:
1281 case ISN_STOREW:
1282 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283 {
1284 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001285 hashtab_T *ht;
1286 switch (iptr->isn_type)
1287 {
1288 case ISN_STOREG:
1289 ht = get_globvar_ht();
1290 break;
1291 case ISN_STOREB:
1292 ht = &curbuf->b_vars->dv_hashtab;
1293 break;
1294 case ISN_STOREW:
1295 ht = &curwin->w_vars->dv_hashtab;
1296 break;
1297 case ISN_STORET:
1298 ht = &curtab->tp_vars->dv_hashtab;
1299 break;
1300 default: // Cannot reach here
1301 goto failed;
1302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303
1304 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001305 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001307 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308 else
1309 {
1310 clear_tv(&di->di_tv);
1311 di->di_tv = *STACK_TV_BOT(0);
1312 }
1313 }
1314 break;
1315
1316 // store number in local variable
1317 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001318 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 clear_tv(tv);
1320 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001321 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 break;
1323
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001324 // store value in list variable
1325 case ISN_STORELIST:
1326 {
1327 typval_T *tv_idx = STACK_TV_BOT(-2);
1328 varnumber_T lidx = tv_idx->vval.v_number;
1329 typval_T *tv_list = STACK_TV_BOT(-1);
1330 list_T *list = tv_list->vval.v_list;
1331
1332 if (lidx < 0 && list->lv_len + lidx >= 0)
1333 // negative index is relative to the end
1334 lidx = list->lv_len + lidx;
1335 if (lidx < 0 || lidx > list->lv_len)
1336 {
1337 semsg(_(e_listidx), lidx);
1338 goto failed;
1339 }
1340 tv = STACK_TV_BOT(-3);
1341 if (lidx < list->lv_len)
1342 {
1343 listitem_T *li = list_find(list, lidx);
1344
1345 // overwrite existing list item
1346 clear_tv(&li->li_tv);
1347 li->li_tv = *tv;
1348 }
1349 else
1350 {
1351 // append to list
1352 if (list_append_tv(list, tv) == FAIL)
1353 goto failed;
1354 clear_tv(tv);
1355 }
1356 clear_tv(tv_idx);
1357 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001358 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001359 }
1360 break;
1361
1362 // store value in dict variable
1363 case ISN_STOREDICT:
1364 {
1365 typval_T *tv_key = STACK_TV_BOT(-2);
1366 char_u *key = tv_key->vval.v_string;
1367 typval_T *tv_dict = STACK_TV_BOT(-1);
1368 dict_T *dict = tv_dict->vval.v_dict;
1369 dictitem_T *di;
1370
1371 if (key == NULL || *key == NUL)
1372 {
1373 emsg(_(e_emptykey));
1374 goto failed;
1375 }
1376 tv = STACK_TV_BOT(-3);
1377 di = dict_find(dict, key, -1);
1378 if (di != NULL)
1379 {
1380 clear_tv(&di->di_tv);
1381 di->di_tv = *tv;
1382 }
1383 else
1384 {
1385 // add to dict
1386 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1387 goto failed;
1388 clear_tv(tv);
1389 }
1390 clear_tv(tv_key);
1391 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001392 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001393 }
1394 break;
1395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 // push constant
1397 case ISN_PUSHNR:
1398 case ISN_PUSHBOOL:
1399 case ISN_PUSHSPEC:
1400 case ISN_PUSHF:
1401 case ISN_PUSHS:
1402 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001403 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001404 case ISN_PUSHCHANNEL:
1405 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001406 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 goto failed;
1408 tv = STACK_TV_BOT(0);
1409 ++ectx.ec_stack.ga_len;
1410 switch (iptr->isn_type)
1411 {
1412 case ISN_PUSHNR:
1413 tv->v_type = VAR_NUMBER;
1414 tv->vval.v_number = iptr->isn_arg.number;
1415 break;
1416 case ISN_PUSHBOOL:
1417 tv->v_type = VAR_BOOL;
1418 tv->vval.v_number = iptr->isn_arg.number;
1419 break;
1420 case ISN_PUSHSPEC:
1421 tv->v_type = VAR_SPECIAL;
1422 tv->vval.v_number = iptr->isn_arg.number;
1423 break;
1424#ifdef FEAT_FLOAT
1425 case ISN_PUSHF:
1426 tv->v_type = VAR_FLOAT;
1427 tv->vval.v_float = iptr->isn_arg.fnumber;
1428 break;
1429#endif
1430 case ISN_PUSHBLOB:
1431 blob_copy(iptr->isn_arg.blob, tv);
1432 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001433 case ISN_PUSHFUNC:
1434 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001435 if (iptr->isn_arg.string == NULL)
1436 tv->vval.v_string = NULL;
1437 else
1438 tv->vval.v_string =
1439 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001440 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001441 case ISN_PUSHCHANNEL:
1442#ifdef FEAT_JOB_CHANNEL
1443 tv->v_type = VAR_CHANNEL;
1444 tv->vval.v_channel = iptr->isn_arg.channel;
1445 if (tv->vval.v_channel != NULL)
1446 ++tv->vval.v_channel->ch_refcount;
1447#endif
1448 break;
1449 case ISN_PUSHJOB:
1450#ifdef FEAT_JOB_CHANNEL
1451 tv->v_type = VAR_JOB;
1452 tv->vval.v_job = iptr->isn_arg.job;
1453 if (tv->vval.v_job != NULL)
1454 ++tv->vval.v_job->jv_refcount;
1455#endif
1456 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 default:
1458 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001459 tv->vval.v_string = vim_strsave(
1460 iptr->isn_arg.string == NULL
1461 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 }
1463 break;
1464
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001465 case ISN_UNLET:
1466 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1467 iptr->isn_arg.unlet.ul_forceit) == FAIL)
1468 goto failed;
1469 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001470 case ISN_UNLETENV:
1471 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1472 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 // create a list from items on the stack; uses a single allocation
1475 // for the list header and the items
1476 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001477 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1478 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 break;
1480
1481 // create a dict from items on the stack
1482 case ISN_NEWDICT:
1483 {
1484 int count = iptr->isn_arg.number;
1485 dict_T *dict = dict_alloc();
1486 dictitem_T *item;
1487
1488 if (dict == NULL)
1489 goto failed;
1490 for (idx = 0; idx < count; ++idx)
1491 {
1492 // check key type is VAR_STRING
1493 tv = STACK_TV_BOT(2 * (idx - count));
1494 item = dictitem_alloc(tv->vval.v_string);
1495 clear_tv(tv);
1496 if (item == NULL)
1497 goto failed;
1498 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1499 item->di_tv.v_lock = 0;
1500 if (dict_add(dict, item) == FAIL)
1501 goto failed;
1502 }
1503
1504 if (count > 0)
1505 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001506 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 goto failed;
1508 else
1509 ++ectx.ec_stack.ga_len;
1510 tv = STACK_TV_BOT(-1);
1511 tv->v_type = VAR_DICT;
1512 tv->vval.v_dict = dict;
1513 ++dict->dv_refcount;
1514 }
1515 break;
1516
1517 // call a :def function
1518 case ISN_DCALL:
1519 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1520 iptr->isn_arg.dfunc.cdf_argcount,
1521 &ectx) == FAIL)
1522 goto failed;
1523 break;
1524
1525 // call a builtin function
1526 case ISN_BCALL:
1527 SOURCING_LNUM = iptr->isn_lnum;
1528 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1529 iptr->isn_arg.bfunc.cbf_argcount,
1530 &ectx) == FAIL)
1531 goto failed;
1532 break;
1533
1534 // call a funcref or partial
1535 case ISN_PCALL:
1536 {
1537 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1538 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001539 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540
1541 SOURCING_LNUM = iptr->isn_lnum;
1542 if (pfunc->cpf_top)
1543 {
1544 // funcref is above the arguments
1545 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1546 }
1547 else
1548 {
1549 // Get the funcref from the stack.
1550 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001551 partial_tv = *STACK_TV_BOT(0);
1552 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 }
1554 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001555 if (tv == &partial_tv)
1556 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 if (r == FAIL)
1558 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 }
1560 break;
1561
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001562 case ISN_PCALL_END:
1563 // PCALL finished, arguments have been consumed and replaced by
1564 // the return value. Now clear the funcref from the stack,
1565 // and move the return value in its place.
1566 --ectx.ec_stack.ga_len;
1567 clear_tv(STACK_TV_BOT(-1));
1568 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1569 break;
1570
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571 // call a user defined function or funcref/partial
1572 case ISN_UCALL:
1573 {
1574 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1575
1576 SOURCING_LNUM = iptr->isn_lnum;
1577 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001578 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579 goto failed;
1580 }
1581 break;
1582
1583 // return from a :def function call
1584 case ISN_RETURN:
1585 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001586 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001587 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001588
1589 if (trystack->ga_len > 0)
1590 trycmd = ((trycmd_T *)trystack->ga_data)
1591 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001592 if (trycmd != NULL
1593 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 && trycmd->tcd_finally_idx != 0)
1595 {
1596 // jump to ":finally"
1597 ectx.ec_iidx = trycmd->tcd_finally_idx;
1598 trycmd->tcd_return = TRUE;
1599 }
1600 else
1601 {
1602 // Restore previous function. If the frame pointer
1603 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001604 if (ectx.ec_frame_idx == initial_frame_idx)
1605 {
1606 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1607 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001609 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001611 if (func_return(&ectx) == FAIL)
1612 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 }
1614 }
1615 break;
1616
1617 // push a function reference to a compiled function
1618 case ISN_FUNCREF:
1619 {
1620 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001621 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622
1623 pt = ALLOC_CLEAR_ONE(partial_T);
1624 if (pt == NULL)
1625 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001626 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001627 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001628 vim_free(pt);
1629 goto failed;
1630 }
1631 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1632 + iptr->isn_arg.funcref.fr_func;
1633 pt->pt_func = pt_dfunc->df_ufunc;
1634 pt->pt_refcount = 1;
1635 ++pt_dfunc->df_ufunc->uf_refcount;
1636
1637 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1638 {
1639 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1640 + ectx.ec_dfunc_idx;
1641
1642 // The closure needs to find arguments and local
1643 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001644 pt->pt_ectx_stack = &ectx.ec_stack;
1645 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001646
1647 // If this function returns and the closure is still
1648 // used, we need to make a copy of the context
1649 // (arguments and local variables). Store a reference
1650 // to the partial so we can handle that.
1651 ++pt->pt_refcount;
1652 tv = STACK_TV_VAR(dfunc->df_varcount
1653 + iptr->isn_arg.funcref.fr_var_idx);
1654 if (tv->v_type == VAR_PARTIAL)
1655 {
1656 // TODO: use a garray_T on ectx.
1657 emsg("Multiple closures not supported yet");
1658 goto failed;
1659 }
1660 tv->v_type = VAR_PARTIAL;
1661 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001662 }
1663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 tv = STACK_TV_BOT(0);
1665 ++ectx.ec_stack.ga_len;
1666 tv->vval.v_partial = pt;
1667 tv->v_type = VAR_PARTIAL;
1668 }
1669 break;
1670
1671 // jump if a condition is met
1672 case ISN_JUMP:
1673 {
1674 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1675 int jump = TRUE;
1676
1677 if (when != JUMP_ALWAYS)
1678 {
1679 tv = STACK_TV_BOT(-1);
1680 jump = tv2bool(tv);
1681 if (when == JUMP_IF_FALSE
1682 || when == JUMP_AND_KEEP_IF_FALSE)
1683 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001684 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 {
1686 // drop the value from the stack
1687 clear_tv(tv);
1688 --ectx.ec_stack.ga_len;
1689 }
1690 }
1691 if (jump)
1692 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1693 }
1694 break;
1695
1696 // top of a for loop
1697 case ISN_FOR:
1698 {
1699 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1700 typval_T *idxtv =
1701 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1702
1703 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001704 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 goto failed;
1706 if (++idxtv->vval.v_number >= list->lv_len)
1707 // past the end of the list, jump to "endfor"
1708 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1709 else if (list->lv_first == &range_list_item)
1710 {
1711 // non-materialized range() list
1712 tv = STACK_TV_BOT(0);
1713 tv->v_type = VAR_NUMBER;
1714 tv->vval.v_number = list_find_nr(
1715 list, idxtv->vval.v_number, NULL);
1716 ++ectx.ec_stack.ga_len;
1717 }
1718 else
1719 {
1720 listitem_T *li = list_find(list, idxtv->vval.v_number);
1721
1722 if (li == NULL)
1723 goto failed;
1724 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1725 ++ectx.ec_stack.ga_len;
1726 }
1727 }
1728 break;
1729
1730 // start of ":try" block
1731 case ISN_TRY:
1732 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001733 trycmd_T *trycmd = NULL;
1734
Bram Moolenaar270d0382020-05-15 21:42:53 +02001735 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736 goto failed;
1737 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1738 + ectx.ec_trystack.ga_len;
1739 ++ectx.ec_trystack.ga_len;
1740 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001741 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1743 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001744 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 }
1746 break;
1747
1748 case ISN_PUSHEXC:
1749 if (current_exception == NULL)
1750 {
1751 iemsg("Evaluating catch while current_exception is NULL");
1752 goto failed;
1753 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001754 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755 goto failed;
1756 tv = STACK_TV_BOT(0);
1757 ++ectx.ec_stack.ga_len;
1758 tv->v_type = VAR_STRING;
1759 tv->vval.v_string = vim_strsave(
1760 (char_u *)current_exception->value);
1761 break;
1762
1763 case ISN_CATCH:
1764 {
1765 garray_T *trystack = &ectx.ec_trystack;
1766
1767 if (trystack->ga_len > 0)
1768 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001769 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 + trystack->ga_len - 1;
1771 trycmd->tcd_caught = TRUE;
1772 }
1773 did_emsg = got_int = did_throw = FALSE;
1774 catch_exception(current_exception);
1775 }
1776 break;
1777
1778 // end of ":try" block
1779 case ISN_ENDTRY:
1780 {
1781 garray_T *trystack = &ectx.ec_trystack;
1782
1783 if (trystack->ga_len > 0)
1784 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001785 trycmd_T *trycmd = NULL;
1786
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 --trystack->ga_len;
1788 --trylevel;
1789 trycmd = ((trycmd_T *)trystack->ga_data)
1790 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001791 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 {
1793 // discard the exception
1794 if (caught_stack == current_exception)
1795 caught_stack = caught_stack->caught;
1796 discard_current_exception();
1797 }
1798
1799 if (trycmd->tcd_return)
1800 {
1801 // Restore previous function. If the frame pointer
1802 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001803 if (ectx.ec_frame_idx == initial_frame_idx)
1804 {
1805 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1806 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001808 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001810 if (func_return(&ectx) == FAIL)
1811 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812 }
1813 }
1814 }
1815 break;
1816
1817 case ISN_THROW:
1818 --ectx.ec_stack.ga_len;
1819 tv = STACK_TV_BOT(0);
1820 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1821 {
1822 vim_free(tv->vval.v_string);
1823 goto failed;
1824 }
1825 did_throw = TRUE;
1826 break;
1827
1828 // compare with special values
1829 case ISN_COMPAREBOOL:
1830 case ISN_COMPARESPECIAL:
1831 {
1832 typval_T *tv1 = STACK_TV_BOT(-2);
1833 typval_T *tv2 = STACK_TV_BOT(-1);
1834 varnumber_T arg1 = tv1->vval.v_number;
1835 varnumber_T arg2 = tv2->vval.v_number;
1836 int res;
1837
1838 switch (iptr->isn_arg.op.op_type)
1839 {
1840 case EXPR_EQUAL: res = arg1 == arg2; break;
1841 case EXPR_NEQUAL: res = arg1 != arg2; break;
1842 default: res = 0; break;
1843 }
1844
1845 --ectx.ec_stack.ga_len;
1846 tv1->v_type = VAR_BOOL;
1847 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1848 }
1849 break;
1850
1851 // Operation with two number arguments
1852 case ISN_OPNR:
1853 case ISN_COMPARENR:
1854 {
1855 typval_T *tv1 = STACK_TV_BOT(-2);
1856 typval_T *tv2 = STACK_TV_BOT(-1);
1857 varnumber_T arg1 = tv1->vval.v_number;
1858 varnumber_T arg2 = tv2->vval.v_number;
1859 varnumber_T res;
1860
1861 switch (iptr->isn_arg.op.op_type)
1862 {
1863 case EXPR_MULT: res = arg1 * arg2; break;
1864 case EXPR_DIV: res = arg1 / arg2; break;
1865 case EXPR_REM: res = arg1 % arg2; break;
1866 case EXPR_SUB: res = arg1 - arg2; break;
1867 case EXPR_ADD: res = arg1 + arg2; break;
1868
1869 case EXPR_EQUAL: res = arg1 == arg2; break;
1870 case EXPR_NEQUAL: res = arg1 != arg2; break;
1871 case EXPR_GREATER: res = arg1 > arg2; break;
1872 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1873 case EXPR_SMALLER: res = arg1 < arg2; break;
1874 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1875 default: res = 0; break;
1876 }
1877
1878 --ectx.ec_stack.ga_len;
1879 if (iptr->isn_type == ISN_COMPARENR)
1880 {
1881 tv1->v_type = VAR_BOOL;
1882 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1883 }
1884 else
1885 tv1->vval.v_number = res;
1886 }
1887 break;
1888
1889 // Computation with two float arguments
1890 case ISN_OPFLOAT:
1891 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001892#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 {
1894 typval_T *tv1 = STACK_TV_BOT(-2);
1895 typval_T *tv2 = STACK_TV_BOT(-1);
1896 float_T arg1 = tv1->vval.v_float;
1897 float_T arg2 = tv2->vval.v_float;
1898 float_T res = 0;
1899 int cmp = FALSE;
1900
1901 switch (iptr->isn_arg.op.op_type)
1902 {
1903 case EXPR_MULT: res = arg1 * arg2; break;
1904 case EXPR_DIV: res = arg1 / arg2; break;
1905 case EXPR_SUB: res = arg1 - arg2; break;
1906 case EXPR_ADD: res = arg1 + arg2; break;
1907
1908 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1909 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1910 case EXPR_GREATER: cmp = arg1 > arg2; break;
1911 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1912 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1913 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1914 default: cmp = 0; break;
1915 }
1916 --ectx.ec_stack.ga_len;
1917 if (iptr->isn_type == ISN_COMPAREFLOAT)
1918 {
1919 tv1->v_type = VAR_BOOL;
1920 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1921 }
1922 else
1923 tv1->vval.v_float = res;
1924 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001925#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 break;
1927
1928 case ISN_COMPARELIST:
1929 {
1930 typval_T *tv1 = STACK_TV_BOT(-2);
1931 typval_T *tv2 = STACK_TV_BOT(-1);
1932 list_T *arg1 = tv1->vval.v_list;
1933 list_T *arg2 = tv2->vval.v_list;
1934 int cmp = FALSE;
1935 int ic = iptr->isn_arg.op.op_ic;
1936
1937 switch (iptr->isn_arg.op.op_type)
1938 {
1939 case EXPR_EQUAL: cmp =
1940 list_equal(arg1, arg2, ic, FALSE); break;
1941 case EXPR_NEQUAL: cmp =
1942 !list_equal(arg1, arg2, ic, FALSE); break;
1943 case EXPR_IS: cmp = arg1 == arg2; break;
1944 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1945 default: cmp = 0; break;
1946 }
1947 --ectx.ec_stack.ga_len;
1948 clear_tv(tv1);
1949 clear_tv(tv2);
1950 tv1->v_type = VAR_BOOL;
1951 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1952 }
1953 break;
1954
1955 case ISN_COMPAREBLOB:
1956 {
1957 typval_T *tv1 = STACK_TV_BOT(-2);
1958 typval_T *tv2 = STACK_TV_BOT(-1);
1959 blob_T *arg1 = tv1->vval.v_blob;
1960 blob_T *arg2 = tv2->vval.v_blob;
1961 int cmp = FALSE;
1962
1963 switch (iptr->isn_arg.op.op_type)
1964 {
1965 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1966 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1967 case EXPR_IS: cmp = arg1 == arg2; break;
1968 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1969 default: cmp = 0; break;
1970 }
1971 --ectx.ec_stack.ga_len;
1972 clear_tv(tv1);
1973 clear_tv(tv2);
1974 tv1->v_type = VAR_BOOL;
1975 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1976 }
1977 break;
1978
1979 // TODO: handle separately
1980 case ISN_COMPARESTRING:
1981 case ISN_COMPAREDICT:
1982 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 case ISN_COMPAREANY:
1984 {
1985 typval_T *tv1 = STACK_TV_BOT(-2);
1986 typval_T *tv2 = STACK_TV_BOT(-1);
1987 exptype_T exptype = iptr->isn_arg.op.op_type;
1988 int ic = iptr->isn_arg.op.op_ic;
1989
1990 typval_compare(tv1, tv2, exptype, ic);
1991 clear_tv(tv2);
1992 tv1->v_type = VAR_BOOL;
1993 tv1->vval.v_number = tv1->vval.v_number
1994 ? VVAL_TRUE : VVAL_FALSE;
1995 --ectx.ec_stack.ga_len;
1996 }
1997 break;
1998
1999 case ISN_ADDLIST:
2000 case ISN_ADDBLOB:
2001 {
2002 typval_T *tv1 = STACK_TV_BOT(-2);
2003 typval_T *tv2 = STACK_TV_BOT(-1);
2004
2005 if (iptr->isn_type == ISN_ADDLIST)
2006 eval_addlist(tv1, tv2);
2007 else
2008 eval_addblob(tv1, tv2);
2009 clear_tv(tv2);
2010 --ectx.ec_stack.ga_len;
2011 }
2012 break;
2013
2014 // Computation with two arguments of unknown type
2015 case ISN_OPANY:
2016 {
2017 typval_T *tv1 = STACK_TV_BOT(-2);
2018 typval_T *tv2 = STACK_TV_BOT(-1);
2019 varnumber_T n1, n2;
2020#ifdef FEAT_FLOAT
2021 float_T f1 = 0, f2 = 0;
2022#endif
2023 int error = FALSE;
2024
2025 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2026 {
2027 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2028 {
2029 eval_addlist(tv1, tv2);
2030 clear_tv(tv2);
2031 --ectx.ec_stack.ga_len;
2032 break;
2033 }
2034 else if (tv1->v_type == VAR_BLOB
2035 && tv2->v_type == VAR_BLOB)
2036 {
2037 eval_addblob(tv1, tv2);
2038 clear_tv(tv2);
2039 --ectx.ec_stack.ga_len;
2040 break;
2041 }
2042 }
2043#ifdef FEAT_FLOAT
2044 if (tv1->v_type == VAR_FLOAT)
2045 {
2046 f1 = tv1->vval.v_float;
2047 n1 = 0;
2048 }
2049 else
2050#endif
2051 {
2052 n1 = tv_get_number_chk(tv1, &error);
2053 if (error)
2054 goto failed;
2055#ifdef FEAT_FLOAT
2056 if (tv2->v_type == VAR_FLOAT)
2057 f1 = n1;
2058#endif
2059 }
2060#ifdef FEAT_FLOAT
2061 if (tv2->v_type == VAR_FLOAT)
2062 {
2063 f2 = tv2->vval.v_float;
2064 n2 = 0;
2065 }
2066 else
2067#endif
2068 {
2069 n2 = tv_get_number_chk(tv2, &error);
2070 if (error)
2071 goto failed;
2072#ifdef FEAT_FLOAT
2073 if (tv1->v_type == VAR_FLOAT)
2074 f2 = n2;
2075#endif
2076 }
2077#ifdef FEAT_FLOAT
2078 // if there is a float on either side the result is a float
2079 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2080 {
2081 switch (iptr->isn_arg.op.op_type)
2082 {
2083 case EXPR_MULT: f1 = f1 * f2; break;
2084 case EXPR_DIV: f1 = f1 / f2; break;
2085 case EXPR_SUB: f1 = f1 - f2; break;
2086 case EXPR_ADD: f1 = f1 + f2; break;
2087 default: emsg(_(e_modulus)); goto failed;
2088 }
2089 clear_tv(tv1);
2090 clear_tv(tv2);
2091 tv1->v_type = VAR_FLOAT;
2092 tv1->vval.v_float = f1;
2093 --ectx.ec_stack.ga_len;
2094 }
2095 else
2096#endif
2097 {
2098 switch (iptr->isn_arg.op.op_type)
2099 {
2100 case EXPR_MULT: n1 = n1 * n2; break;
2101 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2102 case EXPR_SUB: n1 = n1 - n2; break;
2103 case EXPR_ADD: n1 = n1 + n2; break;
2104 default: n1 = num_modulus(n1, n2); break;
2105 }
2106 clear_tv(tv1);
2107 clear_tv(tv2);
2108 tv1->v_type = VAR_NUMBER;
2109 tv1->vval.v_number = n1;
2110 --ectx.ec_stack.ga_len;
2111 }
2112 }
2113 break;
2114
2115 case ISN_CONCAT:
2116 {
2117 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2118 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2119 char_u *res;
2120
2121 res = concat_str(str1, str2);
2122 clear_tv(STACK_TV_BOT(-2));
2123 clear_tv(STACK_TV_BOT(-1));
2124 --ectx.ec_stack.ga_len;
2125 STACK_TV_BOT(-1)->vval.v_string = res;
2126 }
2127 break;
2128
2129 case ISN_INDEX:
2130 {
2131 list_T *list;
2132 varnumber_T n;
2133 listitem_T *li;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002134 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135
2136 // list index: list is at stack-2, index at stack-1
2137 tv = STACK_TV_BOT(-2);
2138 if (tv->v_type != VAR_LIST)
2139 {
2140 emsg(_(e_listreq));
2141 goto failed;
2142 }
2143 list = tv->vval.v_list;
2144
2145 tv = STACK_TV_BOT(-1);
2146 if (tv->v_type != VAR_NUMBER)
2147 {
2148 emsg(_(e_number_exp));
2149 goto failed;
2150 }
2151 n = tv->vval.v_number;
2152 clear_tv(tv);
2153 if ((li = list_find(list, n)) == NULL)
2154 {
2155 semsg(_(e_listidx), n);
2156 goto failed;
2157 }
2158 --ectx.ec_stack.ga_len;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002159 // Clear the list after getting the item, to avoid that it
2160 // make the item invalid.
2161 tv = STACK_TV_BOT(-1);
2162 temp_tv = *tv;
2163 copy_tv(&li->li_tv, tv);
2164 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002165 }
2166 break;
2167
Bram Moolenaar9af78762020-06-16 11:34:42 +02002168 case ISN_SLICE:
2169 {
2170 list_T *list;
2171 int count = iptr->isn_arg.number;
2172
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002173 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002174 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002175 list = tv->vval.v_list;
2176
2177 // no error for short list, expect it to be checked earlier
2178 if (list != NULL && list->lv_len >= count)
2179 {
2180 list_T *newlist = list_slice(list,
2181 count, list->lv_len - 1);
2182
2183 if (newlist != NULL)
2184 {
2185 list_unref(list);
2186 tv->vval.v_list = newlist;
2187 ++newlist->lv_refcount;
2188 }
2189 }
2190 }
2191 break;
2192
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002193 case ISN_GETITEM:
2194 {
2195 listitem_T *li;
2196 int index = iptr->isn_arg.number;
2197
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002198 // Get list item: list is at stack-1, push item.
2199 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002200 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002201 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002202
2203 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2204 goto failed;
2205 ++ectx.ec_stack.ga_len;
2206 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2207 }
2208 break;
2209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 case ISN_MEMBER:
2211 {
2212 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002213 char_u *key;
2214 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002215 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002216
2217 // dict member: dict is at stack-2, key at stack-1
2218 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002219 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002220 dict = tv->vval.v_dict;
2221
2222 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002223 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002224 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002225
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002226 if ((di = dict_find(dict, key, -1)) == NULL)
2227 {
2228 semsg(_(e_dictkey), key);
2229 goto failed;
2230 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002231 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002232 --ectx.ec_stack.ga_len;
2233 // Clear the dict after getting the item, to avoid that it
2234 // make the item invalid.
2235 tv = STACK_TV_BOT(-1);
2236 temp_tv = *tv;
2237 copy_tv(&di->di_tv, tv);
2238 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002239 }
2240 break;
2241
2242 // dict member with string key
2243 case ISN_STRINGMEMBER:
2244 {
2245 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002247 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248
2249 tv = STACK_TV_BOT(-1);
2250 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2251 {
2252 emsg(_(e_dictreq));
2253 goto failed;
2254 }
2255 dict = tv->vval.v_dict;
2256
2257 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2258 == NULL)
2259 {
2260 semsg(_(e_dictkey), iptr->isn_arg.string);
2261 goto failed;
2262 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002263 // Clear the dict after getting the item, to avoid that it
2264 // make the item invalid.
2265 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002267 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 }
2269 break;
2270
2271 case ISN_NEGATENR:
2272 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002273 if (tv->v_type != VAR_NUMBER
2274#ifdef FEAT_FLOAT
2275 && tv->v_type != VAR_FLOAT
2276#endif
2277 )
2278 {
2279 emsg(_(e_number_exp));
2280 goto failed;
2281 }
2282#ifdef FEAT_FLOAT
2283 if (tv->v_type == VAR_FLOAT)
2284 tv->vval.v_float = -tv->vval.v_float;
2285 else
2286#endif
2287 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288 break;
2289
2290 case ISN_CHECKNR:
2291 {
2292 int error = FALSE;
2293
2294 tv = STACK_TV_BOT(-1);
2295 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 (void)tv_get_number_chk(tv, &error);
2298 if (error)
2299 goto failed;
2300 }
2301 break;
2302
2303 case ISN_CHECKTYPE:
2304 {
2305 checktype_T *ct = &iptr->isn_arg.type;
2306
2307 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002308 // TODO: better type comparison
2309 if (tv->v_type != ct->ct_type
2310 && !((tv->v_type == VAR_PARTIAL
2311 && ct->ct_type == VAR_FUNC)
2312 || (tv->v_type == VAR_FUNC
2313 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314 {
2315 semsg(_("E1029: Expected %s but got %s"),
2316 vartype_name(ct->ct_type),
2317 vartype_name(tv->v_type));
2318 goto failed;
2319 }
2320 }
2321 break;
2322
Bram Moolenaar9af78762020-06-16 11:34:42 +02002323 case ISN_CHECKLEN:
2324 {
2325 int min_len = iptr->isn_arg.checklen.cl_min_len;
2326 list_T *list = NULL;
2327
2328 tv = STACK_TV_BOT(-1);
2329 if (tv->v_type == VAR_LIST)
2330 list = tv->vval.v_list;
2331 if (list == NULL || list->lv_len < min_len
2332 || (list->lv_len > min_len
2333 && !iptr->isn_arg.checklen.cl_more_OK))
2334 {
2335 semsg(_("E1093: Expected %d items but got %d"),
2336 min_len, list == NULL ? 0 : list->lv_len);
2337 goto failed;
2338 }
2339 }
2340 break;
2341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342 case ISN_2BOOL:
2343 {
2344 int n;
2345
2346 tv = STACK_TV_BOT(-1);
2347 n = tv2bool(tv);
2348 if (iptr->isn_arg.number) // invert
2349 n = !n;
2350 clear_tv(tv);
2351 tv->v_type = VAR_BOOL;
2352 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2353 }
2354 break;
2355
2356 case ISN_2STRING:
2357 {
2358 char_u *str;
2359
2360 tv = STACK_TV_BOT(iptr->isn_arg.number);
2361 if (tv->v_type != VAR_STRING)
2362 {
2363 str = typval_tostring(tv);
2364 clear_tv(tv);
2365 tv->v_type = VAR_STRING;
2366 tv->vval.v_string = str;
2367 }
2368 }
2369 break;
2370
Bram Moolenaar389df252020-07-09 21:20:47 +02002371 case ISN_SHUFFLE:
2372 {
2373 typval_T tmp_tv;
2374 int item = iptr->isn_arg.shuffle.shfl_item;
2375 int up = iptr->isn_arg.shuffle.shfl_up;
2376
2377 tmp_tv = *STACK_TV_BOT(-item);
2378 for ( ; up > 0 && item > 1; --up)
2379 {
2380 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2381 --item;
2382 }
2383 *STACK_TV_BOT(-item) = tmp_tv;
2384 }
2385 break;
2386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387 case ISN_DROP:
2388 --ectx.ec_stack.ga_len;
2389 clear_tv(STACK_TV_BOT(0));
2390 break;
2391 }
2392 }
2393
2394done:
2395 // function finished, get result from the stack.
2396 tv = STACK_TV_BOT(-1);
2397 *rettv = *tv;
2398 tv->v_type = VAR_UNKNOWN;
2399 ret = OK;
2400
2401failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002402 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002403 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002404 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002405failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002406 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002407
2408 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2410 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002413 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 return ret;
2415}
2416
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417/*
2418 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002419 * We don't really need this at runtime, but we do have tests that require it,
2420 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 */
2422 void
2423ex_disassemble(exarg_T *eap)
2424{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002425 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002426 char_u *fname;
2427 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 dfunc_T *dfunc;
2429 isn_T *instr;
2430 int current;
2431 int line_idx = 0;
2432 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002433 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002435 if (STRNCMP(arg, "<lambda>", 8) == 0)
2436 {
2437 arg += 8;
2438 (void)getdigits(&arg);
2439 fname = vim_strnsave(eap->arg, arg - eap->arg);
2440 }
2441 else
2442 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002443 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002444 if (fname == NULL)
2445 {
2446 semsg(_(e_invarg2), eap->arg);
2447 return;
2448 }
2449
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002450 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002451 if (ufunc == NULL)
2452 {
2453 char_u *p = untrans_function_name(fname);
2454
2455 if (p != NULL)
2456 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002457 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002458 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002459 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460 if (ufunc == NULL)
2461 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002462 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 return;
2464 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002465 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002466 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2467 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002468 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002470 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 return;
2472 }
2473 if (ufunc->uf_name_exp != NULL)
2474 msg((char *)ufunc->uf_name_exp);
2475 else
2476 msg((char *)ufunc->uf_name);
2477
2478 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2479 instr = dfunc->df_instr;
2480 for (current = 0; current < dfunc->df_instr_count; ++current)
2481 {
2482 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002483 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484
2485 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2486 {
2487 if (current > prev_current)
2488 {
2489 msg_puts("\n\n");
2490 prev_current = current;
2491 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002492 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2493 if (line != NULL)
2494 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 }
2496
2497 switch (iptr->isn_type)
2498 {
2499 case ISN_EXEC:
2500 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2501 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002502 case ISN_EXECCONCAT:
2503 smsg("%4d EXECCONCAT %lld", current,
2504 (long long)iptr->isn_arg.number);
2505 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 case ISN_ECHO:
2507 {
2508 echo_T *echo = &iptr->isn_arg.echo;
2509
2510 smsg("%4d %s %d", current,
2511 echo->echo_with_white ? "ECHO" : "ECHON",
2512 echo->echo_count);
2513 }
2514 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002515 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002516 smsg("%4d EXECUTE %lld", current,
2517 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002518 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002519 case ISN_ECHOMSG:
2520 smsg("%4d ECHOMSG %lld", current,
2521 (long long)(iptr->isn_arg.number));
2522 break;
2523 case ISN_ECHOERR:
2524 smsg("%4d ECHOERR %lld", current,
2525 (long long)(iptr->isn_arg.number));
2526 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002528 case ISN_LOADOUTER:
2529 {
2530 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2531
2532 if (iptr->isn_arg.number < 0)
2533 smsg("%4d LOAD%s arg[%lld]", current, add,
2534 (long long)(iptr->isn_arg.number
2535 + STACK_FRAME_SIZE));
2536 else
2537 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002538 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002539 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 break;
2541 case ISN_LOADV:
2542 smsg("%4d LOADV v:%s", current,
2543 get_vim_var_name(iptr->isn_arg.number));
2544 break;
2545 case ISN_LOADSCRIPT:
2546 {
2547 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002548 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2550 + iptr->isn_arg.script.script_idx;
2551
2552 smsg("%4d LOADSCRIPT %s from %s", current,
2553 sv->sv_name, si->sn_name);
2554 }
2555 break;
2556 case ISN_LOADS:
2557 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002558 scriptitem_T *si = SCRIPT_ITEM(
2559 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560
2561 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002562 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 }
2564 break;
2565 case ISN_LOADG:
2566 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2567 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002568 case ISN_LOADB:
2569 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2570 break;
2571 case ISN_LOADW:
2572 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2573 break;
2574 case ISN_LOADT:
2575 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2576 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 case ISN_LOADOPT:
2578 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2579 break;
2580 case ISN_LOADENV:
2581 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2582 break;
2583 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002584 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 break;
2586
2587 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002588 case ISN_STOREOUTER:
2589 {
2590 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2591
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002592 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002593 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002594 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002595 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002596 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002597 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002598 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002600 case ISN_STOREV:
2601 smsg("%4d STOREV v:%s", current,
2602 get_vim_var_name(iptr->isn_arg.number));
2603 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002605 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2606 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002607 case ISN_STOREB:
2608 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2609 break;
2610 case ISN_STOREW:
2611 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2612 break;
2613 case ISN_STORET:
2614 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2615 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002616 case ISN_STORES:
2617 {
2618 scriptitem_T *si = SCRIPT_ITEM(
2619 iptr->isn_arg.loadstore.ls_sid);
2620
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002621 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002622 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002623 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 break;
2625 case ISN_STORESCRIPT:
2626 {
2627 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002628 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2630 + iptr->isn_arg.script.script_idx;
2631
2632 smsg("%4d STORESCRIPT %s in %s", current,
2633 sv->sv_name, si->sn_name);
2634 }
2635 break;
2636 case ISN_STOREOPT:
2637 smsg("%4d STOREOPT &%s", current,
2638 iptr->isn_arg.storeopt.so_name);
2639 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002640 case ISN_STOREENV:
2641 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2642 break;
2643 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002644 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002645 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 case ISN_STORENR:
2647 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002648 iptr->isn_arg.storenr.stnr_val,
2649 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650 break;
2651
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002652 case ISN_STORELIST:
2653 smsg("%4d STORELIST", current);
2654 break;
2655
2656 case ISN_STOREDICT:
2657 smsg("%4d STOREDICT", current);
2658 break;
2659
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 // constants
2661 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002662 smsg("%4d PUSHNR %lld", current,
2663 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 break;
2665 case ISN_PUSHBOOL:
2666 case ISN_PUSHSPEC:
2667 smsg("%4d PUSH %s", current,
2668 get_var_special_name(iptr->isn_arg.number));
2669 break;
2670 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002671#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002673#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 break;
2675 case ISN_PUSHS:
2676 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2677 break;
2678 case ISN_PUSHBLOB:
2679 {
2680 char_u *r;
2681 char_u numbuf[NUMBUFLEN];
2682 char_u *tofree;
2683
2684 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002685 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 vim_free(tofree);
2687 }
2688 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002689 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002690 {
2691 char *name = (char *)iptr->isn_arg.string;
2692
2693 smsg("%4d PUSHFUNC \"%s\"", current,
2694 name == NULL ? "[none]" : name);
2695 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002696 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002697 case ISN_PUSHCHANNEL:
2698#ifdef FEAT_JOB_CHANNEL
2699 {
2700 channel_T *channel = iptr->isn_arg.channel;
2701
2702 smsg("%4d PUSHCHANNEL %d", current,
2703 channel == NULL ? 0 : channel->ch_id);
2704 }
2705#endif
2706 break;
2707 case ISN_PUSHJOB:
2708#ifdef FEAT_JOB_CHANNEL
2709 {
2710 typval_T tv;
2711 char_u *name;
2712
2713 tv.v_type = VAR_JOB;
2714 tv.vval.v_job = iptr->isn_arg.job;
2715 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002716 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002717 }
2718#endif
2719 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 case ISN_PUSHEXC:
2721 smsg("%4d PUSH v:exception", current);
2722 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002723 case ISN_UNLET:
2724 smsg("%4d UNLET%s %s", current,
2725 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2726 iptr->isn_arg.unlet.ul_name);
2727 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002728 case ISN_UNLETENV:
2729 smsg("%4d UNLETENV%s $%s", current,
2730 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2731 iptr->isn_arg.unlet.ul_name);
2732 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002734 smsg("%4d NEWLIST size %lld", current,
2735 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 break;
2737 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002738 smsg("%4d NEWDICT size %lld", current,
2739 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 break;
2741
2742 // function call
2743 case ISN_BCALL:
2744 {
2745 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2746
2747 smsg("%4d BCALL %s(argc %d)", current,
2748 internal_func_name(cbfunc->cbf_idx),
2749 cbfunc->cbf_argcount);
2750 }
2751 break;
2752 case ISN_DCALL:
2753 {
2754 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2755 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2756 + cdfunc->cdf_idx;
2757
2758 smsg("%4d DCALL %s(argc %d)", current,
2759 df->df_ufunc->uf_name_exp != NULL
2760 ? df->df_ufunc->uf_name_exp
2761 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2762 }
2763 break;
2764 case ISN_UCALL:
2765 {
2766 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2767
2768 smsg("%4d UCALL %s(argc %d)", current,
2769 cufunc->cuf_name, cufunc->cuf_argcount);
2770 }
2771 break;
2772 case ISN_PCALL:
2773 {
2774 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2775
2776 smsg("%4d PCALL%s (argc %d)", current,
2777 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2778 }
2779 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002780 case ISN_PCALL_END:
2781 smsg("%4d PCALL end", current);
2782 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 case ISN_RETURN:
2784 smsg("%4d RETURN", current);
2785 break;
2786 case ISN_FUNCREF:
2787 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002788 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002790 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002792 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002793 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 }
2795 break;
2796
2797 case ISN_JUMP:
2798 {
2799 char *when = "?";
2800
2801 switch (iptr->isn_arg.jump.jump_when)
2802 {
2803 case JUMP_ALWAYS:
2804 when = "JUMP";
2805 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 case JUMP_AND_KEEP_IF_TRUE:
2807 when = "JUMP_AND_KEEP_IF_TRUE";
2808 break;
2809 case JUMP_IF_FALSE:
2810 when = "JUMP_IF_FALSE";
2811 break;
2812 case JUMP_AND_KEEP_IF_FALSE:
2813 when = "JUMP_AND_KEEP_IF_FALSE";
2814 break;
2815 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002816 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 iptr->isn_arg.jump.jump_where);
2818 }
2819 break;
2820
2821 case ISN_FOR:
2822 {
2823 forloop_T *forloop = &iptr->isn_arg.forloop;
2824
2825 smsg("%4d FOR $%d -> %d", current,
2826 forloop->for_idx, forloop->for_end);
2827 }
2828 break;
2829
2830 case ISN_TRY:
2831 {
2832 try_T *try = &iptr->isn_arg.try;
2833
2834 smsg("%4d TRY catch -> %d, finally -> %d", current,
2835 try->try_catch, try->try_finally);
2836 }
2837 break;
2838 case ISN_CATCH:
2839 // TODO
2840 smsg("%4d CATCH", current);
2841 break;
2842 case ISN_ENDTRY:
2843 smsg("%4d ENDTRY", current);
2844 break;
2845 case ISN_THROW:
2846 smsg("%4d THROW", current);
2847 break;
2848
2849 // expression operations on number
2850 case ISN_OPNR:
2851 case ISN_OPFLOAT:
2852 case ISN_OPANY:
2853 {
2854 char *what;
2855 char *ins;
2856
2857 switch (iptr->isn_arg.op.op_type)
2858 {
2859 case EXPR_MULT: what = "*"; break;
2860 case EXPR_DIV: what = "/"; break;
2861 case EXPR_REM: what = "%"; break;
2862 case EXPR_SUB: what = "-"; break;
2863 case EXPR_ADD: what = "+"; break;
2864 default: what = "???"; break;
2865 }
2866 switch (iptr->isn_type)
2867 {
2868 case ISN_OPNR: ins = "OPNR"; break;
2869 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2870 case ISN_OPANY: ins = "OPANY"; break;
2871 default: ins = "???"; break;
2872 }
2873 smsg("%4d %s %s", current, ins, what);
2874 }
2875 break;
2876
2877 case ISN_COMPAREBOOL:
2878 case ISN_COMPARESPECIAL:
2879 case ISN_COMPARENR:
2880 case ISN_COMPAREFLOAT:
2881 case ISN_COMPARESTRING:
2882 case ISN_COMPAREBLOB:
2883 case ISN_COMPARELIST:
2884 case ISN_COMPAREDICT:
2885 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 case ISN_COMPAREANY:
2887 {
2888 char *p;
2889 char buf[10];
2890 char *type;
2891
2892 switch (iptr->isn_arg.op.op_type)
2893 {
2894 case EXPR_EQUAL: p = "=="; break;
2895 case EXPR_NEQUAL: p = "!="; break;
2896 case EXPR_GREATER: p = ">"; break;
2897 case EXPR_GEQUAL: p = ">="; break;
2898 case EXPR_SMALLER: p = "<"; break;
2899 case EXPR_SEQUAL: p = "<="; break;
2900 case EXPR_MATCH: p = "=~"; break;
2901 case EXPR_IS: p = "is"; break;
2902 case EXPR_ISNOT: p = "isnot"; break;
2903 case EXPR_NOMATCH: p = "!~"; break;
2904 default: p = "???"; break;
2905 }
2906 STRCPY(buf, p);
2907 if (iptr->isn_arg.op.op_ic == TRUE)
2908 strcat(buf, "?");
2909 switch(iptr->isn_type)
2910 {
2911 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2912 case ISN_COMPARESPECIAL:
2913 type = "COMPARESPECIAL"; break;
2914 case ISN_COMPARENR: type = "COMPARENR"; break;
2915 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2916 case ISN_COMPARESTRING:
2917 type = "COMPARESTRING"; break;
2918 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2919 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2920 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2921 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2923 default: type = "???"; break;
2924 }
2925
2926 smsg("%4d %s %s", current, type, buf);
2927 }
2928 break;
2929
2930 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2931 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2932
2933 // expression operations
2934 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2935 case ISN_INDEX: smsg("%4d INDEX", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002936 case ISN_SLICE: smsg("%4d SLICE %lld",
2937 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002938 case ISN_GETITEM: smsg("%4d ITEM %lld",
2939 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002940 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
2941 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 iptr->isn_arg.string); break;
2943 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2944
2945 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2946 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2947 vartype_name(iptr->isn_arg.type.ct_type),
2948 iptr->isn_arg.type.ct_off);
2949 break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002950 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
2951 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
2952 iptr->isn_arg.checklen.cl_min_len);
2953 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 case ISN_2BOOL: if (iptr->isn_arg.number)
2955 smsg("%4d INVERT (!val)", current);
2956 else
2957 smsg("%4d 2BOOL (!!val)", current);
2958 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002959 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2960 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02002961 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962
Bram Moolenaar389df252020-07-09 21:20:47 +02002963 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
2964 iptr->isn_arg.shuffle.shfl_item,
2965 iptr->isn_arg.shuffle.shfl_up);
2966 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 case ISN_DROP: smsg("%4d DROP", current); break;
2968 }
2969 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970}
2971
2972/*
2973 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2974 * list, etc. Mostly like what JavaScript does, except that empty list and
2975 * empty dictionary are FALSE.
2976 */
2977 int
2978tv2bool(typval_T *tv)
2979{
2980 switch (tv->v_type)
2981 {
2982 case VAR_NUMBER:
2983 return tv->vval.v_number != 0;
2984 case VAR_FLOAT:
2985#ifdef FEAT_FLOAT
2986 return tv->vval.v_float != 0.0;
2987#else
2988 break;
2989#endif
2990 case VAR_PARTIAL:
2991 return tv->vval.v_partial != NULL;
2992 case VAR_FUNC:
2993 case VAR_STRING:
2994 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2995 case VAR_LIST:
2996 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2997 case VAR_DICT:
2998 return tv->vval.v_dict != NULL
2999 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3000 case VAR_BOOL:
3001 case VAR_SPECIAL:
3002 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3003 case VAR_JOB:
3004#ifdef FEAT_JOB_CHANNEL
3005 return tv->vval.v_job != NULL;
3006#else
3007 break;
3008#endif
3009 case VAR_CHANNEL:
3010#ifdef FEAT_JOB_CHANNEL
3011 return tv->vval.v_channel != NULL;
3012#else
3013 break;
3014#endif
3015 case VAR_BLOB:
3016 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3017 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003018 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 case VAR_VOID:
3020 break;
3021 }
3022 return FALSE;
3023}
3024
3025/*
3026 * If "tv" is a string give an error and return FAIL.
3027 */
3028 int
3029check_not_string(typval_T *tv)
3030{
3031 if (tv->v_type == VAR_STRING)
3032 {
3033 emsg(_("E1030: Using a String as a Number"));
3034 clear_tv(tv);
3035 return FAIL;
3036 }
3037 return OK;
3038}
3039
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040
3041#endif // FEAT_EVAL