blob: d40320122eabad56fca834eed31de7e731994b8b [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 {
27 int tcd_frame; // ec_frame when ISN_TRY was encountered
28 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
59 int ec_frame; // index in ec_stack: context of ec_dfunc_idx
60
61 garray_T ec_trystack; // stack of trycmd_T values
62 int ec_in_catch; // when TRUE in catch or finally block
63
64 int ec_dfunc_idx; // current function index
65 isn_T *ec_instr; // array with instructions
66 int ec_iidx; // index in ec_instr: instruction to execute
67} ectx_T;
68
69// Get pointer to item relative to the bottom of the stack, -1 is the last one.
70#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
71
72/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010073 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074 */
75 static int
76ufunc_argcount(ufunc_T *ufunc)
77{
78 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
79}
80
81/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010082 * Set the instruction index, depending on omitted arguments, where the default
83 * values are to be computed. If all optional arguments are present, start
84 * with the function body.
85 * The expression evaluation is at the start of the instructions:
86 * 0 -> EVAL default1
87 * STORE arg[-2]
88 * 1 -> EVAL default2
89 * STORE arg[-1]
90 * 2 -> function body
91 */
92 static void
93init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
94{
95 if (ufunc->uf_def_args.ga_len == 0)
96 ectx->ec_iidx = 0;
97 else
98 {
99 int defcount = ufunc->uf_args.ga_len - argcount;
100
101 // If there is a varargs argument defcount can be negative, no defaults
102 // to evaluate then.
103 if (defcount < 0)
104 defcount = 0;
105 ectx->ec_iidx = ufunc->uf_def_arg_idx[
106 ufunc->uf_def_args.ga_len - defcount];
107 }
108}
109
110/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100111 * Call compiled function "cdf_idx" from compiled code.
112 *
113 * Stack has:
114 * - current arguments (already there)
115 * - omitted optional argument (default values) added here
116 * - stack frame:
117 * - pointer to calling function
118 * - Index of next instruction in calling function
119 * - previous frame pointer
120 * - reserved space for local variables
121 */
122 static int
123call_dfunc(int cdf_idx, int argcount, ectx_T *ectx)
124{
125 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
126 ufunc_T *ufunc = dfunc->df_ufunc;
127 int optcount = ufunc_argcount(ufunc) - argcount;
128 int idx;
129
130 if (dfunc->df_deleted)
131 {
132 emsg_funcname(e_func_deleted, ufunc->uf_name);
133 return FAIL;
134 }
135
136 if (ga_grow(&ectx->ec_stack, optcount + 3 + dfunc->df_varcount) == FAIL)
137 return FAIL;
138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139 if (optcount < 0)
140 {
141 emsg("argument count wrong?");
142 return FAIL;
143 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100144
145 // Reserve space for omitted optional arguments, filled in soon.
146 // Also any empty varargs argument.
147 ectx->ec_stack.ga_len += optcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100148
149 // Store current execution state in stack frame for ISN_RETURN.
150 // TODO: If the actual number of arguments doesn't match what the called
151 // function expects things go bad.
152 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
153 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
154 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame;
155 ectx->ec_frame = ectx->ec_stack.ga_len;
156
157 // Initialize local variables
158 for (idx = 0; idx < dfunc->df_varcount; ++idx)
159 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
160 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + dfunc->df_varcount;
161
162 // Set execution state to the start of the called function.
163 ectx->ec_dfunc_idx = cdf_idx;
164 ectx->ec_instr = dfunc->df_instr;
165 estack_push_ufunc(ETYPE_UFUNC, dfunc->df_ufunc, 1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100166
167 // Decide where to start execution, handles optional arguments.
168 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169
170 return OK;
171}
172
173// Get pointer to item in the stack.
174#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
175
176/*
177 * Return from the current function.
178 */
179 static void
180func_return(ectx_T *ectx)
181{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 int idx;
183 dfunc_T *dfunc;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100184 int top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100185
186 // execution context goes one level up
187 estack_pop();
188
189 // Clear the local variables and temporary values, but not
190 // the return value.
191 for (idx = ectx->ec_frame + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100192 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100193 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100194
195 // Clear the arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100197 top = ectx->ec_frame - ufunc_argcount(dfunc->df_ufunc);
198 for (idx = top; idx < ectx->ec_frame; ++idx)
199 clear_tv(STACK_TV(idx));
200
201 // Restore the previous frame.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100202 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame)->vval.v_number;
203 ectx->ec_iidx = STACK_TV(ectx->ec_frame + 1)->vval.v_number;
204 ectx->ec_frame = STACK_TV(ectx->ec_frame + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
206 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100207
208 // Reset the stack to the position before the call, move the return value
209 // to the top of the stack.
210 idx = ectx->ec_stack.ga_len - 1;
211 ectx->ec_stack.ga_len = top + 1;
212 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213}
214
215#undef STACK_TV
216
217/*
218 * Prepare arguments and rettv for calling a builtin or user function.
219 */
220 static int
221call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
222{
223 int idx;
224 typval_T *tv;
225
226 // Move arguments from bottom of the stack to argvars[] and add terminator.
227 for (idx = 0; idx < argcount; ++idx)
228 argvars[idx] = *STACK_TV_BOT(idx - argcount);
229 argvars[argcount].v_type = VAR_UNKNOWN;
230
231 // Result replaces the arguments on the stack.
232 if (argcount > 0)
233 ectx->ec_stack.ga_len -= argcount - 1;
234 else if (ga_grow(&ectx->ec_stack, 1) == FAIL)
235 return FAIL;
236 else
237 ++ectx->ec_stack.ga_len;
238
239 // Default return value is zero.
240 tv = STACK_TV_BOT(-1);
241 tv->v_type = VAR_NUMBER;
242 tv->vval.v_number = 0;
243
244 return OK;
245}
246
247/*
248 * Call a builtin function by index.
249 */
250 static int
251call_bfunc(int func_idx, int argcount, ectx_T *ectx)
252{
253 typval_T argvars[MAX_FUNC_ARGS];
254 int idx;
255
256 if (call_prepare(argcount, argvars, ectx) == FAIL)
257 return FAIL;
258
259 // Call the builtin function.
260 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
261
262 // Clear the arguments.
263 for (idx = 0; idx < argcount; ++idx)
264 clear_tv(&argvars[idx]);
265 return OK;
266}
267
268/*
269 * Execute a user defined function.
270 */
271 static int
272call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx)
273{
274 typval_T argvars[MAX_FUNC_ARGS];
275 funcexe_T funcexe;
276 int error;
277 int idx;
278
279 if (ufunc->uf_dfunc_idx >= 0)
280 // The function has been compiled, can call it quickly.
281 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
282
283 if (call_prepare(argcount, argvars, ectx) == FAIL)
284 return FAIL;
285 vim_memset(&funcexe, 0, sizeof(funcexe));
286 funcexe.evaluate = TRUE;
287
288 // Call the user function. Result goes in last position on the stack.
289 // TODO: add selfdict if there is one
290 error = call_user_func_check(ufunc, argcount, argvars,
291 STACK_TV_BOT(-1), &funcexe, NULL);
292
293 // Clear the arguments.
294 for (idx = 0; idx < argcount; ++idx)
295 clear_tv(&argvars[idx]);
296
297 if (error != FCERR_NONE)
298 {
299 user_func_error(error, ufunc->uf_name);
300 return FAIL;
301 }
302 return OK;
303}
304
305/*
306 * Execute a function by "name".
307 * This can be a builtin function or a user function.
308 * Returns FAIL if not found without an error message.
309 */
310 static int
311call_by_name(char_u *name, int argcount, ectx_T *ectx)
312{
313 ufunc_T *ufunc;
314
315 if (builtin_function(name, -1))
316 {
317 int func_idx = find_internal_func(name);
318
319 if (func_idx < 0)
320 return FAIL;
321 if (check_internal_func(func_idx, argcount) == FAIL)
322 return FAIL;
323 return call_bfunc(func_idx, argcount, ectx);
324 }
325
326 ufunc = find_func(name, NULL);
327 if (ufunc != NULL)
328 return call_ufunc(ufunc, argcount, ectx);
329
330 return FAIL;
331}
332
333 static int
334call_partial(typval_T *tv, int argcount, ectx_T *ectx)
335{
336 char_u *name;
337 int called_emsg_before = called_emsg;
338
339 if (tv->v_type == VAR_PARTIAL)
340 {
341 partial_T *pt = tv->vval.v_partial;
342
343 if (pt->pt_func != NULL)
344 return call_ufunc(pt->pt_func, argcount, ectx);
345 name = pt->pt_name;
346 }
347 else
348 name = tv->vval.v_string;
349 if (call_by_name(name, argcount, ectx) == FAIL)
350 {
351 if (called_emsg == called_emsg_before)
352 semsg(_(e_unknownfunc), name);
353 return FAIL;
354 }
355 return OK;
356}
357
358/*
359 * Execute a function by "name".
360 * This can be a builtin function, user function or a funcref.
361 */
362 static int
363call_eval_func(char_u *name, int argcount, ectx_T *ectx)
364{
365 int called_emsg_before = called_emsg;
366
367 if (call_by_name(name, argcount, ectx) == FAIL
368 && called_emsg == called_emsg_before)
369 {
370 // "name" may be a variable that is a funcref or partial
371 // if find variable
372 // call_partial()
373 // else
374 // semsg(_(e_unknownfunc), name);
375 emsg("call_eval_func(partial) not implemented yet");
376 return FAIL;
377 }
378 return OK;
379}
380
381/*
382 * Call a "def" function from old Vim script.
383 * Return OK or FAIL.
384 */
385 int
386call_def_function(
387 ufunc_T *ufunc,
388 int argc, // nr of arguments
389 typval_T *argv, // arguments
390 typval_T *rettv) // return value
391{
392 ectx_T ectx; // execution context
393 int initial_frame_ptr;
394 typval_T *tv;
395 int idx;
396 int ret = FAIL;
397 dfunc_T *dfunc;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100398 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399
400// Get pointer to item in the stack.
401#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
402
403// Get pointer to item at the bottom of the stack, -1 is the bottom.
404#undef STACK_TV_BOT
405#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
406
407// Get pointer to local variable on the stack.
408#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame + STACK_FRAME_SIZE + idx)
409
410 vim_memset(&ectx, 0, sizeof(ectx));
411 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
412 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
413 goto failed;
414 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
415
416 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
417
418 // Put arguments on the stack.
419 for (idx = 0; idx < argc; ++idx)
420 {
421 copy_tv(&argv[idx], STACK_TV_BOT(0));
422 ++ectx.ec_stack.ga_len;
423 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100424 // Make space for omitted arguments, will store default value below.
425 if (defcount > 0)
426 for (idx = 0; idx < defcount; ++idx)
427 {
428 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
429 ++ectx.ec_stack.ga_len;
430 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100431
432 // Frame pointer points to just after arguments.
433 ectx.ec_frame = ectx.ec_stack.ga_len;
434 initial_frame_ptr = ectx.ec_frame;
435
436 // dummy frame entries
437 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
438 {
439 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
440 ++ectx.ec_stack.ga_len;
441 }
442
443 // Reserve space for local variables.
444 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
445 for (idx = 0; idx < dfunc->df_varcount; ++idx)
446 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
447 ectx.ec_stack.ga_len += dfunc->df_varcount;
448
449 ectx.ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100450
451 // Decide where to start execution, handles optional arguments.
452 init_instr_idx(ufunc, argc, &ectx);
453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454 for (;;)
455 {
456 isn_T *iptr;
457 trycmd_T *trycmd = NULL;
458
459 if (did_throw && !ectx.ec_in_catch)
460 {
461 garray_T *trystack = &ectx.ec_trystack;
462
463 // An exception jumps to the first catch, finally, or returns from
464 // the current function.
465 if (trystack->ga_len > 0)
466 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
467 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame)
468 {
469 // jump to ":catch" or ":finally"
470 ectx.ec_in_catch = TRUE;
471 ectx.ec_iidx = trycmd->tcd_catch_idx;
472 }
473 else
474 {
475 // not inside try or need to return from current functions.
476 if (ectx.ec_frame == initial_frame_ptr)
477 {
478 // At the toplevel we are done. Push a dummy return value.
479 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
480 goto failed;
481 tv = STACK_TV_BOT(0);
482 tv->v_type = VAR_NUMBER;
483 tv->vval.v_number = 0;
484 ++ectx.ec_stack.ga_len;
485 goto done;
486 }
487
488 func_return(&ectx);
489 }
490 continue;
491 }
492
493 iptr = &ectx.ec_instr[ectx.ec_iidx++];
494 switch (iptr->isn_type)
495 {
496 // execute Ex command line
497 case ISN_EXEC:
498 do_cmdline_cmd(iptr->isn_arg.string);
499 break;
500
501 // execute :echo {string} ...
502 case ISN_ECHO:
503 {
504 int count = iptr->isn_arg.echo.echo_count;
505 int atstart = TRUE;
506 int needclr = TRUE;
507
508 for (idx = 0; idx < count; ++idx)
509 {
510 tv = STACK_TV_BOT(idx - count);
511 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
512 &atstart, &needclr);
513 clear_tv(tv);
514 }
515 ectx.ec_stack.ga_len -= count;
516 }
517 break;
518
519 // load local variable or argument
520 case ISN_LOAD:
521 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
522 goto failed;
523 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
524 ++ectx.ec_stack.ga_len;
525 break;
526
527 // load v: variable
528 case ISN_LOADV:
529 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
530 goto failed;
531 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
532 ++ectx.ec_stack.ga_len;
533 break;
534
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100535 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536 case ISN_LOADSCRIPT:
537 {
538 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100539 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100540 svar_T *sv;
541
542 sv = ((svar_T *)si->sn_var_vals.ga_data)
543 + iptr->isn_arg.script.script_idx;
544 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
545 goto failed;
546 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
547 ++ectx.ec_stack.ga_len;
548 }
549 break;
550
551 // load s: variable in old script
552 case ISN_LOADS:
553 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100554 hashtab_T *ht = &SCRIPT_VARS(
555 iptr->isn_arg.loadstore.ls_sid);
556 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100557 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
558 if (di == NULL)
559 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100560 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100561 goto failed;
562 }
563 else
564 {
565 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
566 goto failed;
567 copy_tv(&di->di_tv, STACK_TV_BOT(0));
568 ++ectx.ec_stack.ga_len;
569 }
570 }
571 break;
572
573 // load g: variable
574 case ISN_LOADG:
575 {
576 dictitem_T *di;
577
578 di = find_var_in_ht(get_globvar_ht(), 0,
579 iptr->isn_arg.string, TRUE);
580 if (di == NULL)
581 {
582 semsg(_("E121: Undefined variable: g:%s"),
583 iptr->isn_arg.string);
584 goto failed;
585 }
586 else
587 {
588 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
589 goto failed;
590 copy_tv(&di->di_tv, STACK_TV_BOT(0));
591 ++ectx.ec_stack.ga_len;
592 }
593 }
594 break;
595
596 // load &option
597 case ISN_LOADOPT:
598 {
599 typval_T optval;
600 char_u *name = iptr->isn_arg.string;
601
602 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
603 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +0100604 if (get_option_tv(&name, &optval, TRUE) == FAIL)
605 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 *STACK_TV_BOT(0) = optval;
607 ++ectx.ec_stack.ga_len;
608 }
609 break;
610
611 // load $ENV
612 case ISN_LOADENV:
613 {
614 typval_T optval;
615 char_u *name = iptr->isn_arg.string;
616
617 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
618 goto failed;
Bram Moolenaar07da94b2020-01-28 22:39:19 +0100619 if (get_env_tv(&name, &optval, TRUE) == FAIL)
620 {
621 semsg(_("E1060: Invalid environment variable name: %s"),
622 iptr->isn_arg.string);
623 goto failed;
624 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625 *STACK_TV_BOT(0) = optval;
626 ++ectx.ec_stack.ga_len;
627 }
628 break;
629
630 // load @register
631 case ISN_LOADREG:
632 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
633 goto failed;
634 tv = STACK_TV_BOT(0);
635 tv->v_type = VAR_STRING;
636 tv->vval.v_string = get_reg_contents(
637 iptr->isn_arg.number, GREG_EXPR_SRC);
638 ++ectx.ec_stack.ga_len;
639 break;
640
641 // store local variable
642 case ISN_STORE:
643 --ectx.ec_stack.ga_len;
644 tv = STACK_TV_VAR(iptr->isn_arg.number);
645 clear_tv(tv);
646 *tv = *STACK_TV_BOT(0);
647 break;
648
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100649 // store s: variable in old script
650 case ISN_STORES:
651 {
652 hashtab_T *ht = &SCRIPT_VARS(
653 iptr->isn_arg.loadstore.ls_sid);
654 char_u *name = iptr->isn_arg.loadstore.ls_name;
655 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
656
657 if (di == NULL)
658 {
659 semsg(_(e_undefvar), name);
660 goto failed;
661 }
662 --ectx.ec_stack.ga_len;
663 clear_tv(&di->di_tv);
664 di->di_tv = *STACK_TV_BOT(0);
665 }
666 break;
667
668 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669 case ISN_STORESCRIPT:
670 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100671 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 iptr->isn_arg.script.script_sid);
673 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
674 + iptr->isn_arg.script.script_idx;
675
676 --ectx.ec_stack.ga_len;
677 clear_tv(sv->sv_tv);
678 *sv->sv_tv = *STACK_TV_BOT(0);
679 }
680 break;
681
682 // store option
683 case ISN_STOREOPT:
684 {
685 long n = 0;
686 char_u *s = NULL;
687 char *msg;
688
689 --ectx.ec_stack.ga_len;
690 tv = STACK_TV_BOT(0);
691 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100692 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100694 if (s == NULL)
695 s = (char_u *)"";
696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 else if (tv->v_type == VAR_NUMBER)
698 n = tv->vval.v_number;
699 else
700 {
701 emsg(_("E1051: Expected string or number"));
702 goto failed;
703 }
704 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
705 n, s, iptr->isn_arg.storeopt.so_flags);
706 if (msg != NULL)
707 {
708 emsg(_(msg));
709 goto failed;
710 }
711 clear_tv(tv);
712 }
713 break;
714
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100715 // store $ENV
716 case ISN_STOREENV:
717 --ectx.ec_stack.ga_len;
718 vim_setenv_ext(iptr->isn_arg.string,
719 tv_get_string(STACK_TV_BOT(0)));
720 break;
721
722 // store @r
723 case ISN_STOREREG:
724 {
725 int reg = iptr->isn_arg.number;
726
727 --ectx.ec_stack.ga_len;
728 write_reg_contents(reg == '@' ? '"' : reg,
729 tv_get_string(STACK_TV_BOT(0)), -1, FALSE);
730 }
731 break;
732
733 // store v: variable
734 case ISN_STOREV:
735 --ectx.ec_stack.ga_len;
736 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
737 == FAIL)
738 goto failed;
739 break;
740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 // store g: variable
742 case ISN_STOREG:
743 {
744 dictitem_T *di;
745
746 --ectx.ec_stack.ga_len;
747 di = find_var_in_ht(get_globvar_ht(), 0,
748 iptr->isn_arg.string, TRUE);
749 if (di == NULL)
750 {
751 funccal_entry_T entry;
752
753 save_funccal(&entry);
754 set_var_const(iptr->isn_arg.string, NULL,
755 STACK_TV_BOT(0), FALSE, 0);
756 restore_funccal();
757 }
758 else
759 {
760 clear_tv(&di->di_tv);
761 di->di_tv = *STACK_TV_BOT(0);
762 }
763 }
764 break;
765
766 // store number in local variable
767 case ISN_STORENR:
768 tv = STACK_TV_VAR(iptr->isn_arg.storenr.str_idx);
769 clear_tv(tv);
770 tv->v_type = VAR_NUMBER;
771 tv->vval.v_number = iptr->isn_arg.storenr.str_val;
772 break;
773
774 // push constant
775 case ISN_PUSHNR:
776 case ISN_PUSHBOOL:
777 case ISN_PUSHSPEC:
778 case ISN_PUSHF:
779 case ISN_PUSHS:
780 case ISN_PUSHBLOB:
781 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
782 goto failed;
783 tv = STACK_TV_BOT(0);
784 ++ectx.ec_stack.ga_len;
785 switch (iptr->isn_type)
786 {
787 case ISN_PUSHNR:
788 tv->v_type = VAR_NUMBER;
789 tv->vval.v_number = iptr->isn_arg.number;
790 break;
791 case ISN_PUSHBOOL:
792 tv->v_type = VAR_BOOL;
793 tv->vval.v_number = iptr->isn_arg.number;
794 break;
795 case ISN_PUSHSPEC:
796 tv->v_type = VAR_SPECIAL;
797 tv->vval.v_number = iptr->isn_arg.number;
798 break;
799#ifdef FEAT_FLOAT
800 case ISN_PUSHF:
801 tv->v_type = VAR_FLOAT;
802 tv->vval.v_float = iptr->isn_arg.fnumber;
803 break;
804#endif
805 case ISN_PUSHBLOB:
806 blob_copy(iptr->isn_arg.blob, tv);
807 break;
808 default:
809 tv->v_type = VAR_STRING;
810 tv->vval.v_string = vim_strsave(iptr->isn_arg.string);
811 }
812 break;
813
814 // create a list from items on the stack; uses a single allocation
815 // for the list header and the items
816 case ISN_NEWLIST:
817 {
818 int count = iptr->isn_arg.number;
819 list_T *list = list_alloc_with_items(count);
820
821 if (list == NULL)
822 goto failed;
823 for (idx = 0; idx < count; ++idx)
824 list_set_item(list, idx, STACK_TV_BOT(idx - count));
825
826 if (count > 0)
827 ectx.ec_stack.ga_len -= count - 1;
828 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
829 goto failed;
830 else
831 ++ectx.ec_stack.ga_len;
832 tv = STACK_TV_BOT(-1);
833 tv->v_type = VAR_LIST;
834 tv->vval.v_list = list;
835 ++list->lv_refcount;
836 }
837 break;
838
839 // create a dict from items on the stack
840 case ISN_NEWDICT:
841 {
842 int count = iptr->isn_arg.number;
843 dict_T *dict = dict_alloc();
844 dictitem_T *item;
845
846 if (dict == NULL)
847 goto failed;
848 for (idx = 0; idx < count; ++idx)
849 {
850 // check key type is VAR_STRING
851 tv = STACK_TV_BOT(2 * (idx - count));
852 item = dictitem_alloc(tv->vval.v_string);
853 clear_tv(tv);
854 if (item == NULL)
855 goto failed;
856 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
857 item->di_tv.v_lock = 0;
858 if (dict_add(dict, item) == FAIL)
859 goto failed;
860 }
861
862 if (count > 0)
863 ectx.ec_stack.ga_len -= 2 * count - 1;
864 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
865 goto failed;
866 else
867 ++ectx.ec_stack.ga_len;
868 tv = STACK_TV_BOT(-1);
869 tv->v_type = VAR_DICT;
870 tv->vval.v_dict = dict;
871 ++dict->dv_refcount;
872 }
873 break;
874
875 // call a :def function
876 case ISN_DCALL:
877 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
878 iptr->isn_arg.dfunc.cdf_argcount,
879 &ectx) == FAIL)
880 goto failed;
881 break;
882
883 // call a builtin function
884 case ISN_BCALL:
885 SOURCING_LNUM = iptr->isn_lnum;
886 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
887 iptr->isn_arg.bfunc.cbf_argcount,
888 &ectx) == FAIL)
889 goto failed;
890 break;
891
892 // call a funcref or partial
893 case ISN_PCALL:
894 {
895 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
896 int r;
897 typval_T partial;
898
899 SOURCING_LNUM = iptr->isn_lnum;
900 if (pfunc->cpf_top)
901 {
902 // funcref is above the arguments
903 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
904 }
905 else
906 {
907 // Get the funcref from the stack.
908 --ectx.ec_stack.ga_len;
909 partial = *STACK_TV_BOT(0);
910 tv = &partial;
911 }
912 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
913 if (tv == &partial)
914 clear_tv(&partial);
915 if (r == FAIL)
916 goto failed;
917
918 if (pfunc->cpf_top)
919 {
920 // Get the funcref from the stack, overwrite with the
921 // return value.
922 clear_tv(tv);
923 --ectx.ec_stack.ga_len;
924 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
925 }
926 }
927 break;
928
929 // call a user defined function or funcref/partial
930 case ISN_UCALL:
931 {
932 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
933
934 SOURCING_LNUM = iptr->isn_lnum;
935 if (call_eval_func(cufunc->cuf_name,
936 cufunc->cuf_argcount, &ectx) == FAIL)
937 goto failed;
938 }
939 break;
940
941 // return from a :def function call
942 case ISN_RETURN:
943 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +0100944 garray_T *trystack = &ectx.ec_trystack;
945
946 if (trystack->ga_len > 0)
947 trycmd = ((trycmd_T *)trystack->ga_data)
948 + trystack->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame
950 && trycmd->tcd_finally_idx != 0)
951 {
952 // jump to ":finally"
953 ectx.ec_iidx = trycmd->tcd_finally_idx;
954 trycmd->tcd_return = TRUE;
955 }
956 else
957 {
958 // Restore previous function. If the frame pointer
959 // is zero then there is none and we are done.
960 if (ectx.ec_frame == initial_frame_ptr)
961 goto done;
962
963 func_return(&ectx);
964 }
965 }
966 break;
967
968 // push a function reference to a compiled function
969 case ISN_FUNCREF:
970 {
971 partial_T *pt = NULL;
972
973 pt = ALLOC_CLEAR_ONE(partial_T);
974 if (pt == NULL)
975 goto failed;
976 dfunc = ((dfunc_T *)def_functions.ga_data)
977 + iptr->isn_arg.number;
978 pt->pt_func = dfunc->df_ufunc;
979 pt->pt_refcount = 1;
980 ++dfunc->df_ufunc->uf_refcount;
981
982 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
983 goto failed;
984 tv = STACK_TV_BOT(0);
985 ++ectx.ec_stack.ga_len;
986 tv->vval.v_partial = pt;
987 tv->v_type = VAR_PARTIAL;
988 }
989 break;
990
991 // jump if a condition is met
992 case ISN_JUMP:
993 {
994 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
995 int jump = TRUE;
996
997 if (when != JUMP_ALWAYS)
998 {
999 tv = STACK_TV_BOT(-1);
1000 jump = tv2bool(tv);
1001 if (when == JUMP_IF_FALSE
1002 || when == JUMP_AND_KEEP_IF_FALSE)
1003 jump = !jump;
1004 if (when == JUMP_IF_FALSE || when == JUMP_IF_TRUE
1005 || !jump)
1006 {
1007 // drop the value from the stack
1008 clear_tv(tv);
1009 --ectx.ec_stack.ga_len;
1010 }
1011 }
1012 if (jump)
1013 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1014 }
1015 break;
1016
1017 // top of a for loop
1018 case ISN_FOR:
1019 {
1020 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1021 typval_T *idxtv =
1022 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1023
1024 // push the next item from the list
1025 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1026 goto failed;
1027 if (++idxtv->vval.v_number >= list->lv_len)
1028 // past the end of the list, jump to "endfor"
1029 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1030 else if (list->lv_first == &range_list_item)
1031 {
1032 // non-materialized range() list
1033 tv = STACK_TV_BOT(0);
1034 tv->v_type = VAR_NUMBER;
1035 tv->vval.v_number = list_find_nr(
1036 list, idxtv->vval.v_number, NULL);
1037 ++ectx.ec_stack.ga_len;
1038 }
1039 else
1040 {
1041 listitem_T *li = list_find(list, idxtv->vval.v_number);
1042
1043 if (li == NULL)
1044 goto failed;
1045 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1046 ++ectx.ec_stack.ga_len;
1047 }
1048 }
1049 break;
1050
1051 // start of ":try" block
1052 case ISN_TRY:
1053 {
1054 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
1055 goto failed;
1056 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1057 + ectx.ec_trystack.ga_len;
1058 ++ectx.ec_trystack.ga_len;
1059 ++trylevel;
1060 trycmd->tcd_frame = ectx.ec_frame;
1061 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1062 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
1063 }
1064 break;
1065
1066 case ISN_PUSHEXC:
1067 if (current_exception == NULL)
1068 {
1069 iemsg("Evaluating catch while current_exception is NULL");
1070 goto failed;
1071 }
1072 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1073 goto failed;
1074 tv = STACK_TV_BOT(0);
1075 ++ectx.ec_stack.ga_len;
1076 tv->v_type = VAR_STRING;
1077 tv->vval.v_string = vim_strsave(
1078 (char_u *)current_exception->value);
1079 break;
1080
1081 case ISN_CATCH:
1082 {
1083 garray_T *trystack = &ectx.ec_trystack;
1084
1085 if (trystack->ga_len > 0)
1086 {
1087 trycmd = ((trycmd_T *)trystack->ga_data)
1088 + trystack->ga_len - 1;
1089 trycmd->tcd_caught = TRUE;
1090 }
1091 did_emsg = got_int = did_throw = FALSE;
1092 catch_exception(current_exception);
1093 }
1094 break;
1095
1096 // end of ":try" block
1097 case ISN_ENDTRY:
1098 {
1099 garray_T *trystack = &ectx.ec_trystack;
1100
1101 if (trystack->ga_len > 0)
1102 {
1103 --trystack->ga_len;
1104 --trylevel;
1105 trycmd = ((trycmd_T *)trystack->ga_data)
1106 + trystack->ga_len;
1107 if (trycmd->tcd_caught)
1108 {
1109 // discard the exception
1110 if (caught_stack == current_exception)
1111 caught_stack = caught_stack->caught;
1112 discard_current_exception();
1113 }
1114
1115 if (trycmd->tcd_return)
1116 {
1117 // Restore previous function. If the frame pointer
1118 // is zero then there is none and we are done.
1119 if (ectx.ec_frame == initial_frame_ptr)
1120 goto done;
1121
1122 func_return(&ectx);
1123 }
1124 }
1125 }
1126 break;
1127
1128 case ISN_THROW:
1129 --ectx.ec_stack.ga_len;
1130 tv = STACK_TV_BOT(0);
1131 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1132 {
1133 vim_free(tv->vval.v_string);
1134 goto failed;
1135 }
1136 did_throw = TRUE;
1137 break;
1138
1139 // compare with special values
1140 case ISN_COMPAREBOOL:
1141 case ISN_COMPARESPECIAL:
1142 {
1143 typval_T *tv1 = STACK_TV_BOT(-2);
1144 typval_T *tv2 = STACK_TV_BOT(-1);
1145 varnumber_T arg1 = tv1->vval.v_number;
1146 varnumber_T arg2 = tv2->vval.v_number;
1147 int res;
1148
1149 switch (iptr->isn_arg.op.op_type)
1150 {
1151 case EXPR_EQUAL: res = arg1 == arg2; break;
1152 case EXPR_NEQUAL: res = arg1 != arg2; break;
1153 default: res = 0; break;
1154 }
1155
1156 --ectx.ec_stack.ga_len;
1157 tv1->v_type = VAR_BOOL;
1158 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1159 }
1160 break;
1161
1162 // Operation with two number arguments
1163 case ISN_OPNR:
1164 case ISN_COMPARENR:
1165 {
1166 typval_T *tv1 = STACK_TV_BOT(-2);
1167 typval_T *tv2 = STACK_TV_BOT(-1);
1168 varnumber_T arg1 = tv1->vval.v_number;
1169 varnumber_T arg2 = tv2->vval.v_number;
1170 varnumber_T res;
1171
1172 switch (iptr->isn_arg.op.op_type)
1173 {
1174 case EXPR_MULT: res = arg1 * arg2; break;
1175 case EXPR_DIV: res = arg1 / arg2; break;
1176 case EXPR_REM: res = arg1 % arg2; break;
1177 case EXPR_SUB: res = arg1 - arg2; break;
1178 case EXPR_ADD: res = arg1 + arg2; break;
1179
1180 case EXPR_EQUAL: res = arg1 == arg2; break;
1181 case EXPR_NEQUAL: res = arg1 != arg2; break;
1182 case EXPR_GREATER: res = arg1 > arg2; break;
1183 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1184 case EXPR_SMALLER: res = arg1 < arg2; break;
1185 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1186 default: res = 0; break;
1187 }
1188
1189 --ectx.ec_stack.ga_len;
1190 if (iptr->isn_type == ISN_COMPARENR)
1191 {
1192 tv1->v_type = VAR_BOOL;
1193 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1194 }
1195 else
1196 tv1->vval.v_number = res;
1197 }
1198 break;
1199
1200 // Computation with two float arguments
1201 case ISN_OPFLOAT:
1202 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001203#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 {
1205 typval_T *tv1 = STACK_TV_BOT(-2);
1206 typval_T *tv2 = STACK_TV_BOT(-1);
1207 float_T arg1 = tv1->vval.v_float;
1208 float_T arg2 = tv2->vval.v_float;
1209 float_T res = 0;
1210 int cmp = FALSE;
1211
1212 switch (iptr->isn_arg.op.op_type)
1213 {
1214 case EXPR_MULT: res = arg1 * arg2; break;
1215 case EXPR_DIV: res = arg1 / arg2; break;
1216 case EXPR_SUB: res = arg1 - arg2; break;
1217 case EXPR_ADD: res = arg1 + arg2; break;
1218
1219 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1220 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1221 case EXPR_GREATER: cmp = arg1 > arg2; break;
1222 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1223 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1224 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1225 default: cmp = 0; break;
1226 }
1227 --ectx.ec_stack.ga_len;
1228 if (iptr->isn_type == ISN_COMPAREFLOAT)
1229 {
1230 tv1->v_type = VAR_BOOL;
1231 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1232 }
1233 else
1234 tv1->vval.v_float = res;
1235 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001236#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 break;
1238
1239 case ISN_COMPARELIST:
1240 {
1241 typval_T *tv1 = STACK_TV_BOT(-2);
1242 typval_T *tv2 = STACK_TV_BOT(-1);
1243 list_T *arg1 = tv1->vval.v_list;
1244 list_T *arg2 = tv2->vval.v_list;
1245 int cmp = FALSE;
1246 int ic = iptr->isn_arg.op.op_ic;
1247
1248 switch (iptr->isn_arg.op.op_type)
1249 {
1250 case EXPR_EQUAL: cmp =
1251 list_equal(arg1, arg2, ic, FALSE); break;
1252 case EXPR_NEQUAL: cmp =
1253 !list_equal(arg1, arg2, ic, FALSE); break;
1254 case EXPR_IS: cmp = arg1 == arg2; break;
1255 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1256 default: cmp = 0; break;
1257 }
1258 --ectx.ec_stack.ga_len;
1259 clear_tv(tv1);
1260 clear_tv(tv2);
1261 tv1->v_type = VAR_BOOL;
1262 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1263 }
1264 break;
1265
1266 case ISN_COMPAREBLOB:
1267 {
1268 typval_T *tv1 = STACK_TV_BOT(-2);
1269 typval_T *tv2 = STACK_TV_BOT(-1);
1270 blob_T *arg1 = tv1->vval.v_blob;
1271 blob_T *arg2 = tv2->vval.v_blob;
1272 int cmp = FALSE;
1273
1274 switch (iptr->isn_arg.op.op_type)
1275 {
1276 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1277 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1278 case EXPR_IS: cmp = arg1 == arg2; break;
1279 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1280 default: cmp = 0; break;
1281 }
1282 --ectx.ec_stack.ga_len;
1283 clear_tv(tv1);
1284 clear_tv(tv2);
1285 tv1->v_type = VAR_BOOL;
1286 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1287 }
1288 break;
1289
1290 // TODO: handle separately
1291 case ISN_COMPARESTRING:
1292 case ISN_COMPAREDICT:
1293 case ISN_COMPAREFUNC:
1294 case ISN_COMPAREPARTIAL:
1295 case ISN_COMPAREANY:
1296 {
1297 typval_T *tv1 = STACK_TV_BOT(-2);
1298 typval_T *tv2 = STACK_TV_BOT(-1);
1299 exptype_T exptype = iptr->isn_arg.op.op_type;
1300 int ic = iptr->isn_arg.op.op_ic;
1301
1302 typval_compare(tv1, tv2, exptype, ic);
1303 clear_tv(tv2);
1304 tv1->v_type = VAR_BOOL;
1305 tv1->vval.v_number = tv1->vval.v_number
1306 ? VVAL_TRUE : VVAL_FALSE;
1307 --ectx.ec_stack.ga_len;
1308 }
1309 break;
1310
1311 case ISN_ADDLIST:
1312 case ISN_ADDBLOB:
1313 {
1314 typval_T *tv1 = STACK_TV_BOT(-2);
1315 typval_T *tv2 = STACK_TV_BOT(-1);
1316
1317 if (iptr->isn_type == ISN_ADDLIST)
1318 eval_addlist(tv1, tv2);
1319 else
1320 eval_addblob(tv1, tv2);
1321 clear_tv(tv2);
1322 --ectx.ec_stack.ga_len;
1323 }
1324 break;
1325
1326 // Computation with two arguments of unknown type
1327 case ISN_OPANY:
1328 {
1329 typval_T *tv1 = STACK_TV_BOT(-2);
1330 typval_T *tv2 = STACK_TV_BOT(-1);
1331 varnumber_T n1, n2;
1332#ifdef FEAT_FLOAT
1333 float_T f1 = 0, f2 = 0;
1334#endif
1335 int error = FALSE;
1336
1337 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1338 {
1339 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1340 {
1341 eval_addlist(tv1, tv2);
1342 clear_tv(tv2);
1343 --ectx.ec_stack.ga_len;
1344 break;
1345 }
1346 else if (tv1->v_type == VAR_BLOB
1347 && tv2->v_type == VAR_BLOB)
1348 {
1349 eval_addblob(tv1, tv2);
1350 clear_tv(tv2);
1351 --ectx.ec_stack.ga_len;
1352 break;
1353 }
1354 }
1355#ifdef FEAT_FLOAT
1356 if (tv1->v_type == VAR_FLOAT)
1357 {
1358 f1 = tv1->vval.v_float;
1359 n1 = 0;
1360 }
1361 else
1362#endif
1363 {
1364 n1 = tv_get_number_chk(tv1, &error);
1365 if (error)
1366 goto failed;
1367#ifdef FEAT_FLOAT
1368 if (tv2->v_type == VAR_FLOAT)
1369 f1 = n1;
1370#endif
1371 }
1372#ifdef FEAT_FLOAT
1373 if (tv2->v_type == VAR_FLOAT)
1374 {
1375 f2 = tv2->vval.v_float;
1376 n2 = 0;
1377 }
1378 else
1379#endif
1380 {
1381 n2 = tv_get_number_chk(tv2, &error);
1382 if (error)
1383 goto failed;
1384#ifdef FEAT_FLOAT
1385 if (tv1->v_type == VAR_FLOAT)
1386 f2 = n2;
1387#endif
1388 }
1389#ifdef FEAT_FLOAT
1390 // if there is a float on either side the result is a float
1391 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1392 {
1393 switch (iptr->isn_arg.op.op_type)
1394 {
1395 case EXPR_MULT: f1 = f1 * f2; break;
1396 case EXPR_DIV: f1 = f1 / f2; break;
1397 case EXPR_SUB: f1 = f1 - f2; break;
1398 case EXPR_ADD: f1 = f1 + f2; break;
1399 default: emsg(_(e_modulus)); goto failed;
1400 }
1401 clear_tv(tv1);
1402 clear_tv(tv2);
1403 tv1->v_type = VAR_FLOAT;
1404 tv1->vval.v_float = f1;
1405 --ectx.ec_stack.ga_len;
1406 }
1407 else
1408#endif
1409 {
1410 switch (iptr->isn_arg.op.op_type)
1411 {
1412 case EXPR_MULT: n1 = n1 * n2; break;
1413 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1414 case EXPR_SUB: n1 = n1 - n2; break;
1415 case EXPR_ADD: n1 = n1 + n2; break;
1416 default: n1 = num_modulus(n1, n2); break;
1417 }
1418 clear_tv(tv1);
1419 clear_tv(tv2);
1420 tv1->v_type = VAR_NUMBER;
1421 tv1->vval.v_number = n1;
1422 --ectx.ec_stack.ga_len;
1423 }
1424 }
1425 break;
1426
1427 case ISN_CONCAT:
1428 {
1429 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1430 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1431 char_u *res;
1432
1433 res = concat_str(str1, str2);
1434 clear_tv(STACK_TV_BOT(-2));
1435 clear_tv(STACK_TV_BOT(-1));
1436 --ectx.ec_stack.ga_len;
1437 STACK_TV_BOT(-1)->vval.v_string = res;
1438 }
1439 break;
1440
1441 case ISN_INDEX:
1442 {
1443 list_T *list;
1444 varnumber_T n;
1445 listitem_T *li;
1446
1447 // list index: list is at stack-2, index at stack-1
1448 tv = STACK_TV_BOT(-2);
1449 if (tv->v_type != VAR_LIST)
1450 {
1451 emsg(_(e_listreq));
1452 goto failed;
1453 }
1454 list = tv->vval.v_list;
1455
1456 tv = STACK_TV_BOT(-1);
1457 if (tv->v_type != VAR_NUMBER)
1458 {
1459 emsg(_(e_number_exp));
1460 goto failed;
1461 }
1462 n = tv->vval.v_number;
1463 clear_tv(tv);
1464 if ((li = list_find(list, n)) == NULL)
1465 {
1466 semsg(_(e_listidx), n);
1467 goto failed;
1468 }
1469 --ectx.ec_stack.ga_len;
1470 clear_tv(STACK_TV_BOT(-1));
1471 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
1472 }
1473 break;
1474
1475 // dict member with string key
1476 case ISN_MEMBER:
1477 {
1478 dict_T *dict;
1479 dictitem_T *di;
1480
1481 tv = STACK_TV_BOT(-1);
1482 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
1483 {
1484 emsg(_(e_dictreq));
1485 goto failed;
1486 }
1487 dict = tv->vval.v_dict;
1488
1489 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
1490 == NULL)
1491 {
1492 semsg(_(e_dictkey), iptr->isn_arg.string);
1493 goto failed;
1494 }
1495 clear_tv(tv);
1496 copy_tv(&di->di_tv, tv);
1497 }
1498 break;
1499
1500 case ISN_NEGATENR:
1501 tv = STACK_TV_BOT(-1);
1502 tv->vval.v_number = -tv->vval.v_number;
1503 break;
1504
1505 case ISN_CHECKNR:
1506 {
1507 int error = FALSE;
1508
1509 tv = STACK_TV_BOT(-1);
1510 if (check_not_string(tv) == FAIL)
1511 {
1512 --ectx.ec_stack.ga_len;
1513 goto failed;
1514 }
1515 (void)tv_get_number_chk(tv, &error);
1516 if (error)
1517 goto failed;
1518 }
1519 break;
1520
1521 case ISN_CHECKTYPE:
1522 {
1523 checktype_T *ct = &iptr->isn_arg.type;
1524
1525 tv = STACK_TV_BOT(ct->ct_off);
1526 if (tv->v_type != ct->ct_type)
1527 {
1528 semsg(_("E1029: Expected %s but got %s"),
1529 vartype_name(ct->ct_type),
1530 vartype_name(tv->v_type));
1531 goto failed;
1532 }
1533 }
1534 break;
1535
1536 case ISN_2BOOL:
1537 {
1538 int n;
1539
1540 tv = STACK_TV_BOT(-1);
1541 n = tv2bool(tv);
1542 if (iptr->isn_arg.number) // invert
1543 n = !n;
1544 clear_tv(tv);
1545 tv->v_type = VAR_BOOL;
1546 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
1547 }
1548 break;
1549
1550 case ISN_2STRING:
1551 {
1552 char_u *str;
1553
1554 tv = STACK_TV_BOT(iptr->isn_arg.number);
1555 if (tv->v_type != VAR_STRING)
1556 {
1557 str = typval_tostring(tv);
1558 clear_tv(tv);
1559 tv->v_type = VAR_STRING;
1560 tv->vval.v_string = str;
1561 }
1562 }
1563 break;
1564
1565 case ISN_DROP:
1566 --ectx.ec_stack.ga_len;
1567 clear_tv(STACK_TV_BOT(0));
1568 break;
1569 }
1570 }
1571
1572done:
1573 // function finished, get result from the stack.
1574 tv = STACK_TV_BOT(-1);
1575 *rettv = *tv;
1576 tv->v_type = VAR_UNKNOWN;
1577 ret = OK;
1578
1579failed:
1580 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
1581 clear_tv(STACK_TV(idx));
1582 vim_free(ectx.ec_stack.ga_data);
1583 return ret;
1584}
1585
1586#define DISASSEMBLE 1
1587
1588/*
1589 * ":dissassemble".
1590 */
1591 void
1592ex_disassemble(exarg_T *eap)
1593{
1594#ifdef DISASSEMBLE
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001595 char_u *fname;
1596 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 dfunc_T *dfunc;
1598 isn_T *instr;
1599 int current;
1600 int line_idx = 0;
1601 int prev_current = 0;
1602
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001603 fname = trans_function_name(&eap->arg, FALSE,
1604 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
1605 ufunc = find_func(fname, NULL);
1606 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 if (ufunc == NULL)
1608 {
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001609 semsg("E1061: Cannot find function %s", eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 return;
1611 }
1612 if (ufunc->uf_dfunc_idx < 0)
1613 {
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001614 semsg("E1062: Function %s is not compiled", eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 return;
1616 }
1617 if (ufunc->uf_name_exp != NULL)
1618 msg((char *)ufunc->uf_name_exp);
1619 else
1620 msg((char *)ufunc->uf_name);
1621
1622 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
1623 instr = dfunc->df_instr;
1624 for (current = 0; current < dfunc->df_instr_count; ++current)
1625 {
1626 isn_T *iptr = &instr[current];
1627
1628 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
1629 {
1630 if (current > prev_current)
1631 {
1632 msg_puts("\n\n");
1633 prev_current = current;
1634 }
1635 msg(((char **)ufunc->uf_lines.ga_data)[line_idx++]);
1636 }
1637
1638 switch (iptr->isn_type)
1639 {
1640 case ISN_EXEC:
1641 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
1642 break;
1643 case ISN_ECHO:
1644 {
1645 echo_T *echo = &iptr->isn_arg.echo;
1646
1647 smsg("%4d %s %d", current,
1648 echo->echo_with_white ? "ECHO" : "ECHON",
1649 echo->echo_count);
1650 }
1651 break;
1652 case ISN_LOAD:
1653 if (iptr->isn_arg.number < 0)
1654 smsg("%4d LOAD arg[%lld]", current,
1655 iptr->isn_arg.number + STACK_FRAME_SIZE);
1656 else
1657 smsg("%4d LOAD $%lld", current, iptr->isn_arg.number);
1658 break;
1659 case ISN_LOADV:
1660 smsg("%4d LOADV v:%s", current,
1661 get_vim_var_name(iptr->isn_arg.number));
1662 break;
1663 case ISN_LOADSCRIPT:
1664 {
1665 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001666 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1668 + iptr->isn_arg.script.script_idx;
1669
1670 smsg("%4d LOADSCRIPT %s from %s", current,
1671 sv->sv_name, si->sn_name);
1672 }
1673 break;
1674 case ISN_LOADS:
1675 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001676 scriptitem_T *si = SCRIPT_ITEM(
1677 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678
1679 smsg("%4d LOADS s:%s from %s", current,
1680 iptr->isn_arg.string, si->sn_name);
1681 }
1682 break;
1683 case ISN_LOADG:
1684 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
1685 break;
1686 case ISN_LOADOPT:
1687 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
1688 break;
1689 case ISN_LOADENV:
1690 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
1691 break;
1692 case ISN_LOADREG:
1693 smsg("%4d LOADREG @%c", current, iptr->isn_arg.number);
1694 break;
1695
1696 case ISN_STORE:
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001697 if (iptr->isn_arg.number < 0)
1698 smsg("%4d STORE arg[%lld]", current,
1699 iptr->isn_arg.number + STACK_FRAME_SIZE);
1700 else
1701 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001703 case ISN_STOREV:
1704 smsg("%4d STOREV v:%s", current,
1705 get_vim_var_name(iptr->isn_arg.number));
1706 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001708 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
1709 break;
1710 case ISN_STORES:
1711 {
1712 scriptitem_T *si = SCRIPT_ITEM(
1713 iptr->isn_arg.loadstore.ls_sid);
1714
1715 smsg("%4d STORES s:%s in %s", current,
1716 iptr->isn_arg.string, si->sn_name);
1717 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 break;
1719 case ISN_STORESCRIPT:
1720 {
1721 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001722 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1724 + iptr->isn_arg.script.script_idx;
1725
1726 smsg("%4d STORESCRIPT %s in %s", current,
1727 sv->sv_name, si->sn_name);
1728 }
1729 break;
1730 case ISN_STOREOPT:
1731 smsg("%4d STOREOPT &%s", current,
1732 iptr->isn_arg.storeopt.so_name);
1733 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001734 case ISN_STOREENV:
1735 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
1736 break;
1737 case ISN_STOREREG:
1738 smsg("%4d STOREREG @%c", current, iptr->isn_arg.number);
1739 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 case ISN_STORENR:
1741 smsg("%4d STORE %lld in $%d", current,
1742 iptr->isn_arg.storenr.str_val,
1743 iptr->isn_arg.storenr.str_idx);
1744 break;
1745
1746 // constants
1747 case ISN_PUSHNR:
1748 smsg("%4d PUSHNR %lld", current, iptr->isn_arg.number);
1749 break;
1750 case ISN_PUSHBOOL:
1751 case ISN_PUSHSPEC:
1752 smsg("%4d PUSH %s", current,
1753 get_var_special_name(iptr->isn_arg.number));
1754 break;
1755 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001756#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01001758#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001759 break;
1760 case ISN_PUSHS:
1761 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
1762 break;
1763 case ISN_PUSHBLOB:
1764 {
1765 char_u *r;
1766 char_u numbuf[NUMBUFLEN];
1767 char_u *tofree;
1768
1769 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01001770 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 vim_free(tofree);
1772 }
1773 break;
1774 case ISN_PUSHEXC:
1775 smsg("%4d PUSH v:exception", current);
1776 break;
1777 case ISN_NEWLIST:
1778 smsg("%4d NEWLIST size %lld", current, iptr->isn_arg.number);
1779 break;
1780 case ISN_NEWDICT:
1781 smsg("%4d NEWDICT size %lld", current, iptr->isn_arg.number);
1782 break;
1783
1784 // function call
1785 case ISN_BCALL:
1786 {
1787 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
1788
1789 smsg("%4d BCALL %s(argc %d)", current,
1790 internal_func_name(cbfunc->cbf_idx),
1791 cbfunc->cbf_argcount);
1792 }
1793 break;
1794 case ISN_DCALL:
1795 {
1796 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
1797 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
1798 + cdfunc->cdf_idx;
1799
1800 smsg("%4d DCALL %s(argc %d)", current,
1801 df->df_ufunc->uf_name_exp != NULL
1802 ? df->df_ufunc->uf_name_exp
1803 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
1804 }
1805 break;
1806 case ISN_UCALL:
1807 {
1808 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1809
1810 smsg("%4d UCALL %s(argc %d)", current,
1811 cufunc->cuf_name, cufunc->cuf_argcount);
1812 }
1813 break;
1814 case ISN_PCALL:
1815 {
1816 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
1817
1818 smsg("%4d PCALL%s (argc %d)", current,
1819 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
1820 }
1821 break;
1822 case ISN_RETURN:
1823 smsg("%4d RETURN", current);
1824 break;
1825 case ISN_FUNCREF:
1826 {
1827 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
1828 + iptr->isn_arg.number;
1829
1830 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
1831 }
1832 break;
1833
1834 case ISN_JUMP:
1835 {
1836 char *when = "?";
1837
1838 switch (iptr->isn_arg.jump.jump_when)
1839 {
1840 case JUMP_ALWAYS:
1841 when = "JUMP";
1842 break;
1843 case JUMP_IF_TRUE:
1844 when = "JUMP_IF_TRUE";
1845 break;
1846 case JUMP_AND_KEEP_IF_TRUE:
1847 when = "JUMP_AND_KEEP_IF_TRUE";
1848 break;
1849 case JUMP_IF_FALSE:
1850 when = "JUMP_IF_FALSE";
1851 break;
1852 case JUMP_AND_KEEP_IF_FALSE:
1853 when = "JUMP_AND_KEEP_IF_FALSE";
1854 break;
1855 }
1856 smsg("%4d %s -> %lld", current, when,
1857 iptr->isn_arg.jump.jump_where);
1858 }
1859 break;
1860
1861 case ISN_FOR:
1862 {
1863 forloop_T *forloop = &iptr->isn_arg.forloop;
1864
1865 smsg("%4d FOR $%d -> %d", current,
1866 forloop->for_idx, forloop->for_end);
1867 }
1868 break;
1869
1870 case ISN_TRY:
1871 {
1872 try_T *try = &iptr->isn_arg.try;
1873
1874 smsg("%4d TRY catch -> %d, finally -> %d", current,
1875 try->try_catch, try->try_finally);
1876 }
1877 break;
1878 case ISN_CATCH:
1879 // TODO
1880 smsg("%4d CATCH", current);
1881 break;
1882 case ISN_ENDTRY:
1883 smsg("%4d ENDTRY", current);
1884 break;
1885 case ISN_THROW:
1886 smsg("%4d THROW", current);
1887 break;
1888
1889 // expression operations on number
1890 case ISN_OPNR:
1891 case ISN_OPFLOAT:
1892 case ISN_OPANY:
1893 {
1894 char *what;
1895 char *ins;
1896
1897 switch (iptr->isn_arg.op.op_type)
1898 {
1899 case EXPR_MULT: what = "*"; break;
1900 case EXPR_DIV: what = "/"; break;
1901 case EXPR_REM: what = "%"; break;
1902 case EXPR_SUB: what = "-"; break;
1903 case EXPR_ADD: what = "+"; break;
1904 default: what = "???"; break;
1905 }
1906 switch (iptr->isn_type)
1907 {
1908 case ISN_OPNR: ins = "OPNR"; break;
1909 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
1910 case ISN_OPANY: ins = "OPANY"; break;
1911 default: ins = "???"; break;
1912 }
1913 smsg("%4d %s %s", current, ins, what);
1914 }
1915 break;
1916
1917 case ISN_COMPAREBOOL:
1918 case ISN_COMPARESPECIAL:
1919 case ISN_COMPARENR:
1920 case ISN_COMPAREFLOAT:
1921 case ISN_COMPARESTRING:
1922 case ISN_COMPAREBLOB:
1923 case ISN_COMPARELIST:
1924 case ISN_COMPAREDICT:
1925 case ISN_COMPAREFUNC:
1926 case ISN_COMPAREPARTIAL:
1927 case ISN_COMPAREANY:
1928 {
1929 char *p;
1930 char buf[10];
1931 char *type;
1932
1933 switch (iptr->isn_arg.op.op_type)
1934 {
1935 case EXPR_EQUAL: p = "=="; break;
1936 case EXPR_NEQUAL: p = "!="; break;
1937 case EXPR_GREATER: p = ">"; break;
1938 case EXPR_GEQUAL: p = ">="; break;
1939 case EXPR_SMALLER: p = "<"; break;
1940 case EXPR_SEQUAL: p = "<="; break;
1941 case EXPR_MATCH: p = "=~"; break;
1942 case EXPR_IS: p = "is"; break;
1943 case EXPR_ISNOT: p = "isnot"; break;
1944 case EXPR_NOMATCH: p = "!~"; break;
1945 default: p = "???"; break;
1946 }
1947 STRCPY(buf, p);
1948 if (iptr->isn_arg.op.op_ic == TRUE)
1949 strcat(buf, "?");
1950 switch(iptr->isn_type)
1951 {
1952 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
1953 case ISN_COMPARESPECIAL:
1954 type = "COMPARESPECIAL"; break;
1955 case ISN_COMPARENR: type = "COMPARENR"; break;
1956 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
1957 case ISN_COMPARESTRING:
1958 type = "COMPARESTRING"; break;
1959 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
1960 case ISN_COMPARELIST: type = "COMPARELIST"; break;
1961 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
1962 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
1963 case ISN_COMPAREPARTIAL:
1964 type = "COMPAREPARTIAL"; break;
1965 case ISN_COMPAREANY: type = "COMPAREANY"; break;
1966 default: type = "???"; break;
1967 }
1968
1969 smsg("%4d %s %s", current, type, buf);
1970 }
1971 break;
1972
1973 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
1974 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
1975
1976 // expression operations
1977 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
1978 case ISN_INDEX: smsg("%4d INDEX", current); break;
1979 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
1980 iptr->isn_arg.string); break;
1981 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
1982
1983 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
1984 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
1985 vartype_name(iptr->isn_arg.type.ct_type),
1986 iptr->isn_arg.type.ct_off);
1987 break;
1988 case ISN_2BOOL: if (iptr->isn_arg.number)
1989 smsg("%4d INVERT (!val)", current);
1990 else
1991 smsg("%4d 2BOOL (!!val)", current);
1992 break;
1993 case ISN_2STRING: smsg("%4d 2STRING stack[%d]", current,
1994 iptr->isn_arg.number);
1995 break;
1996
1997 case ISN_DROP: smsg("%4d DROP", current); break;
1998 }
1999 }
2000#endif
2001}
2002
2003/*
2004 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2005 * list, etc. Mostly like what JavaScript does, except that empty list and
2006 * empty dictionary are FALSE.
2007 */
2008 int
2009tv2bool(typval_T *tv)
2010{
2011 switch (tv->v_type)
2012 {
2013 case VAR_NUMBER:
2014 return tv->vval.v_number != 0;
2015 case VAR_FLOAT:
2016#ifdef FEAT_FLOAT
2017 return tv->vval.v_float != 0.0;
2018#else
2019 break;
2020#endif
2021 case VAR_PARTIAL:
2022 return tv->vval.v_partial != NULL;
2023 case VAR_FUNC:
2024 case VAR_STRING:
2025 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2026 case VAR_LIST:
2027 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2028 case VAR_DICT:
2029 return tv->vval.v_dict != NULL
2030 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2031 case VAR_BOOL:
2032 case VAR_SPECIAL:
2033 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2034 case VAR_JOB:
2035#ifdef FEAT_JOB_CHANNEL
2036 return tv->vval.v_job != NULL;
2037#else
2038 break;
2039#endif
2040 case VAR_CHANNEL:
2041#ifdef FEAT_JOB_CHANNEL
2042 return tv->vval.v_channel != NULL;
2043#else
2044 break;
2045#endif
2046 case VAR_BLOB:
2047 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2048 case VAR_UNKNOWN:
2049 case VAR_VOID:
2050 break;
2051 }
2052 return FALSE;
2053}
2054
2055/*
2056 * If "tv" is a string give an error and return FAIL.
2057 */
2058 int
2059check_not_string(typval_T *tv)
2060{
2061 if (tv->v_type == VAR_STRING)
2062 {
2063 emsg(_("E1030: Using a String as a Number"));
2064 clear_tv(tv);
2065 return FAIL;
2066 }
2067 return OK;
2068}
2069
2070
2071#endif // FEAT_EVAL