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