blob: 6e003d9389c065002622a23a0a56bbe469d3e601 [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;
365
366// Get pointer to item in the stack.
367#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
368
369// Get pointer to item at the bottom of the stack, -1 is the bottom.
370#undef STACK_TV_BOT
371#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
372
373// Get pointer to local variable on the stack.
374#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame + STACK_FRAME_SIZE + idx)
375
376 vim_memset(&ectx, 0, sizeof(ectx));
377 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
378 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
379 goto failed;
380 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
381
382 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
383
384 // Put arguments on the stack.
385 for (idx = 0; idx < argc; ++idx)
386 {
387 copy_tv(&argv[idx], STACK_TV_BOT(0));
388 ++ectx.ec_stack.ga_len;
389 }
390
391 // Frame pointer points to just after arguments.
392 ectx.ec_frame = ectx.ec_stack.ga_len;
393 initial_frame_ptr = ectx.ec_frame;
394
395 // dummy frame entries
396 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
397 {
398 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
399 ++ectx.ec_stack.ga_len;
400 }
401
402 // Reserve space for local variables.
403 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
404 for (idx = 0; idx < dfunc->df_varcount; ++idx)
405 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
406 ectx.ec_stack.ga_len += dfunc->df_varcount;
407
408 ectx.ec_instr = dfunc->df_instr;
409 ectx.ec_iidx = 0;
410 for (;;)
411 {
412 isn_T *iptr;
413 trycmd_T *trycmd = NULL;
414
415 if (did_throw && !ectx.ec_in_catch)
416 {
417 garray_T *trystack = &ectx.ec_trystack;
418
419 // An exception jumps to the first catch, finally, or returns from
420 // the current function.
421 if (trystack->ga_len > 0)
422 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
423 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame)
424 {
425 // jump to ":catch" or ":finally"
426 ectx.ec_in_catch = TRUE;
427 ectx.ec_iidx = trycmd->tcd_catch_idx;
428 }
429 else
430 {
431 // not inside try or need to return from current functions.
432 if (ectx.ec_frame == initial_frame_ptr)
433 {
434 // At the toplevel we are done. Push a dummy return value.
435 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
436 goto failed;
437 tv = STACK_TV_BOT(0);
438 tv->v_type = VAR_NUMBER;
439 tv->vval.v_number = 0;
440 ++ectx.ec_stack.ga_len;
441 goto done;
442 }
443
444 func_return(&ectx);
445 }
446 continue;
447 }
448
449 iptr = &ectx.ec_instr[ectx.ec_iidx++];
450 switch (iptr->isn_type)
451 {
452 // execute Ex command line
453 case ISN_EXEC:
454 do_cmdline_cmd(iptr->isn_arg.string);
455 break;
456
457 // execute :echo {string} ...
458 case ISN_ECHO:
459 {
460 int count = iptr->isn_arg.echo.echo_count;
461 int atstart = TRUE;
462 int needclr = TRUE;
463
464 for (idx = 0; idx < count; ++idx)
465 {
466 tv = STACK_TV_BOT(idx - count);
467 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
468 &atstart, &needclr);
469 clear_tv(tv);
470 }
471 ectx.ec_stack.ga_len -= count;
472 }
473 break;
474
475 // load local variable or argument
476 case ISN_LOAD:
477 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
478 goto failed;
479 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
480 ++ectx.ec_stack.ga_len;
481 break;
482
483 // load v: variable
484 case ISN_LOADV:
485 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
486 goto failed;
487 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
488 ++ectx.ec_stack.ga_len;
489 break;
490
491 // load s: variable in vim9script
492 case ISN_LOADSCRIPT:
493 {
494 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100495 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496 svar_T *sv;
497
498 sv = ((svar_T *)si->sn_var_vals.ga_data)
499 + iptr->isn_arg.script.script_idx;
500 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
501 goto failed;
502 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
503 ++ectx.ec_stack.ga_len;
504 }
505 break;
506
507 // load s: variable in old script
508 case ISN_LOADS:
509 {
510 hashtab_T *ht = &SCRIPT_VARS(iptr->isn_arg.loads.ls_sid);
511 char_u *name = iptr->isn_arg.loads.ls_name;
512 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
513 if (di == NULL)
514 {
515 semsg(_("E121: Undefined variable: s:%s"), name);
516 goto failed;
517 }
518 else
519 {
520 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
521 goto failed;
522 copy_tv(&di->di_tv, STACK_TV_BOT(0));
523 ++ectx.ec_stack.ga_len;
524 }
525 }
526 break;
527
528 // load g: variable
529 case ISN_LOADG:
530 {
531 dictitem_T *di;
532
533 di = find_var_in_ht(get_globvar_ht(), 0,
534 iptr->isn_arg.string, TRUE);
535 if (di == NULL)
536 {
537 semsg(_("E121: Undefined variable: g:%s"),
538 iptr->isn_arg.string);
539 goto failed;
540 }
541 else
542 {
543 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
544 goto failed;
545 copy_tv(&di->di_tv, STACK_TV_BOT(0));
546 ++ectx.ec_stack.ga_len;
547 }
548 }
549 break;
550
551 // load &option
552 case ISN_LOADOPT:
553 {
554 typval_T optval;
555 char_u *name = iptr->isn_arg.string;
556
557 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
558 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +0100559 if (get_option_tv(&name, &optval, TRUE) == FAIL)
560 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100561 *STACK_TV_BOT(0) = optval;
562 ++ectx.ec_stack.ga_len;
563 }
564 break;
565
566 // load $ENV
567 case ISN_LOADENV:
568 {
569 typval_T optval;
570 char_u *name = iptr->isn_arg.string;
571
572 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
573 goto failed;
Bram Moolenaar07da94b2020-01-28 22:39:19 +0100574 if (get_env_tv(&name, &optval, TRUE) == FAIL)
575 {
576 semsg(_("E1060: Invalid environment variable name: %s"),
577 iptr->isn_arg.string);
578 goto failed;
579 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 *STACK_TV_BOT(0) = optval;
581 ++ectx.ec_stack.ga_len;
582 }
583 break;
584
585 // load @register
586 case ISN_LOADREG:
587 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
588 goto failed;
589 tv = STACK_TV_BOT(0);
590 tv->v_type = VAR_STRING;
591 tv->vval.v_string = get_reg_contents(
592 iptr->isn_arg.number, GREG_EXPR_SRC);
593 ++ectx.ec_stack.ga_len;
594 break;
595
596 // store local variable
597 case ISN_STORE:
598 --ectx.ec_stack.ga_len;
599 tv = STACK_TV_VAR(iptr->isn_arg.number);
600 clear_tv(tv);
601 *tv = *STACK_TV_BOT(0);
602 break;
603
604 // store script-local variable
605 case ISN_STORESCRIPT:
606 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100607 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608 iptr->isn_arg.script.script_sid);
609 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
610 + iptr->isn_arg.script.script_idx;
611
612 --ectx.ec_stack.ga_len;
613 clear_tv(sv->sv_tv);
614 *sv->sv_tv = *STACK_TV_BOT(0);
615 }
616 break;
617
618 // store option
619 case ISN_STOREOPT:
620 {
621 long n = 0;
622 char_u *s = NULL;
623 char *msg;
624
625 --ectx.ec_stack.ga_len;
626 tv = STACK_TV_BOT(0);
627 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100628 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100629 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100630 if (s == NULL)
631 s = (char_u *)"";
632 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633 else if (tv->v_type == VAR_NUMBER)
634 n = tv->vval.v_number;
635 else
636 {
637 emsg(_("E1051: Expected string or number"));
638 goto failed;
639 }
640 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
641 n, s, iptr->isn_arg.storeopt.so_flags);
642 if (msg != NULL)
643 {
644 emsg(_(msg));
645 goto failed;
646 }
647 clear_tv(tv);
648 }
649 break;
650
651 // store g: variable
652 case ISN_STOREG:
653 {
654 dictitem_T *di;
655
656 --ectx.ec_stack.ga_len;
657 di = find_var_in_ht(get_globvar_ht(), 0,
658 iptr->isn_arg.string, TRUE);
659 if (di == NULL)
660 {
661 funccal_entry_T entry;
662
663 save_funccal(&entry);
664 set_var_const(iptr->isn_arg.string, NULL,
665 STACK_TV_BOT(0), FALSE, 0);
666 restore_funccal();
667 }
668 else
669 {
670 clear_tv(&di->di_tv);
671 di->di_tv = *STACK_TV_BOT(0);
672 }
673 }
674 break;
675
676 // store number in local variable
677 case ISN_STORENR:
678 tv = STACK_TV_VAR(iptr->isn_arg.storenr.str_idx);
679 clear_tv(tv);
680 tv->v_type = VAR_NUMBER;
681 tv->vval.v_number = iptr->isn_arg.storenr.str_val;
682 break;
683
684 // push constant
685 case ISN_PUSHNR:
686 case ISN_PUSHBOOL:
687 case ISN_PUSHSPEC:
688 case ISN_PUSHF:
689 case ISN_PUSHS:
690 case ISN_PUSHBLOB:
691 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
692 goto failed;
693 tv = STACK_TV_BOT(0);
694 ++ectx.ec_stack.ga_len;
695 switch (iptr->isn_type)
696 {
697 case ISN_PUSHNR:
698 tv->v_type = VAR_NUMBER;
699 tv->vval.v_number = iptr->isn_arg.number;
700 break;
701 case ISN_PUSHBOOL:
702 tv->v_type = VAR_BOOL;
703 tv->vval.v_number = iptr->isn_arg.number;
704 break;
705 case ISN_PUSHSPEC:
706 tv->v_type = VAR_SPECIAL;
707 tv->vval.v_number = iptr->isn_arg.number;
708 break;
709#ifdef FEAT_FLOAT
710 case ISN_PUSHF:
711 tv->v_type = VAR_FLOAT;
712 tv->vval.v_float = iptr->isn_arg.fnumber;
713 break;
714#endif
715 case ISN_PUSHBLOB:
716 blob_copy(iptr->isn_arg.blob, tv);
717 break;
718 default:
719 tv->v_type = VAR_STRING;
720 tv->vval.v_string = vim_strsave(iptr->isn_arg.string);
721 }
722 break;
723
724 // create a list from items on the stack; uses a single allocation
725 // for the list header and the items
726 case ISN_NEWLIST:
727 {
728 int count = iptr->isn_arg.number;
729 list_T *list = list_alloc_with_items(count);
730
731 if (list == NULL)
732 goto failed;
733 for (idx = 0; idx < count; ++idx)
734 list_set_item(list, idx, STACK_TV_BOT(idx - count));
735
736 if (count > 0)
737 ectx.ec_stack.ga_len -= count - 1;
738 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
739 goto failed;
740 else
741 ++ectx.ec_stack.ga_len;
742 tv = STACK_TV_BOT(-1);
743 tv->v_type = VAR_LIST;
744 tv->vval.v_list = list;
745 ++list->lv_refcount;
746 }
747 break;
748
749 // create a dict from items on the stack
750 case ISN_NEWDICT:
751 {
752 int count = iptr->isn_arg.number;
753 dict_T *dict = dict_alloc();
754 dictitem_T *item;
755
756 if (dict == NULL)
757 goto failed;
758 for (idx = 0; idx < count; ++idx)
759 {
760 // check key type is VAR_STRING
761 tv = STACK_TV_BOT(2 * (idx - count));
762 item = dictitem_alloc(tv->vval.v_string);
763 clear_tv(tv);
764 if (item == NULL)
765 goto failed;
766 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
767 item->di_tv.v_lock = 0;
768 if (dict_add(dict, item) == FAIL)
769 goto failed;
770 }
771
772 if (count > 0)
773 ectx.ec_stack.ga_len -= 2 * count - 1;
774 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
775 goto failed;
776 else
777 ++ectx.ec_stack.ga_len;
778 tv = STACK_TV_BOT(-1);
779 tv->v_type = VAR_DICT;
780 tv->vval.v_dict = dict;
781 ++dict->dv_refcount;
782 }
783 break;
784
785 // call a :def function
786 case ISN_DCALL:
787 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
788 iptr->isn_arg.dfunc.cdf_argcount,
789 &ectx) == FAIL)
790 goto failed;
791 break;
792
793 // call a builtin function
794 case ISN_BCALL:
795 SOURCING_LNUM = iptr->isn_lnum;
796 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
797 iptr->isn_arg.bfunc.cbf_argcount,
798 &ectx) == FAIL)
799 goto failed;
800 break;
801
802 // call a funcref or partial
803 case ISN_PCALL:
804 {
805 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
806 int r;
807 typval_T partial;
808
809 SOURCING_LNUM = iptr->isn_lnum;
810 if (pfunc->cpf_top)
811 {
812 // funcref is above the arguments
813 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
814 }
815 else
816 {
817 // Get the funcref from the stack.
818 --ectx.ec_stack.ga_len;
819 partial = *STACK_TV_BOT(0);
820 tv = &partial;
821 }
822 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
823 if (tv == &partial)
824 clear_tv(&partial);
825 if (r == FAIL)
826 goto failed;
827
828 if (pfunc->cpf_top)
829 {
830 // Get the funcref from the stack, overwrite with the
831 // return value.
832 clear_tv(tv);
833 --ectx.ec_stack.ga_len;
834 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
835 }
836 }
837 break;
838
839 // call a user defined function or funcref/partial
840 case ISN_UCALL:
841 {
842 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
843
844 SOURCING_LNUM = iptr->isn_lnum;
845 if (call_eval_func(cufunc->cuf_name,
846 cufunc->cuf_argcount, &ectx) == FAIL)
847 goto failed;
848 }
849 break;
850
851 // return from a :def function call
852 case ISN_RETURN:
853 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +0100854 garray_T *trystack = &ectx.ec_trystack;
855
856 if (trystack->ga_len > 0)
857 trycmd = ((trycmd_T *)trystack->ga_data)
858 + trystack->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame
860 && trycmd->tcd_finally_idx != 0)
861 {
862 // jump to ":finally"
863 ectx.ec_iidx = trycmd->tcd_finally_idx;
864 trycmd->tcd_return = TRUE;
865 }
866 else
867 {
868 // Restore previous function. If the frame pointer
869 // is zero then there is none and we are done.
870 if (ectx.ec_frame == initial_frame_ptr)
871 goto done;
872
873 func_return(&ectx);
874 }
875 }
876 break;
877
878 // push a function reference to a compiled function
879 case ISN_FUNCREF:
880 {
881 partial_T *pt = NULL;
882
883 pt = ALLOC_CLEAR_ONE(partial_T);
884 if (pt == NULL)
885 goto failed;
886 dfunc = ((dfunc_T *)def_functions.ga_data)
887 + iptr->isn_arg.number;
888 pt->pt_func = dfunc->df_ufunc;
889 pt->pt_refcount = 1;
890 ++dfunc->df_ufunc->uf_refcount;
891
892 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
893 goto failed;
894 tv = STACK_TV_BOT(0);
895 ++ectx.ec_stack.ga_len;
896 tv->vval.v_partial = pt;
897 tv->v_type = VAR_PARTIAL;
898 }
899 break;
900
901 // jump if a condition is met
902 case ISN_JUMP:
903 {
904 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
905 int jump = TRUE;
906
907 if (when != JUMP_ALWAYS)
908 {
909 tv = STACK_TV_BOT(-1);
910 jump = tv2bool(tv);
911 if (when == JUMP_IF_FALSE
912 || when == JUMP_AND_KEEP_IF_FALSE)
913 jump = !jump;
914 if (when == JUMP_IF_FALSE || when == JUMP_IF_TRUE
915 || !jump)
916 {
917 // drop the value from the stack
918 clear_tv(tv);
919 --ectx.ec_stack.ga_len;
920 }
921 }
922 if (jump)
923 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
924 }
925 break;
926
927 // top of a for loop
928 case ISN_FOR:
929 {
930 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
931 typval_T *idxtv =
932 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
933
934 // push the next item from the list
935 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
936 goto failed;
937 if (++idxtv->vval.v_number >= list->lv_len)
938 // past the end of the list, jump to "endfor"
939 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
940 else if (list->lv_first == &range_list_item)
941 {
942 // non-materialized range() list
943 tv = STACK_TV_BOT(0);
944 tv->v_type = VAR_NUMBER;
945 tv->vval.v_number = list_find_nr(
946 list, idxtv->vval.v_number, NULL);
947 ++ectx.ec_stack.ga_len;
948 }
949 else
950 {
951 listitem_T *li = list_find(list, idxtv->vval.v_number);
952
953 if (li == NULL)
954 goto failed;
955 copy_tv(&li->li_tv, STACK_TV_BOT(0));
956 ++ectx.ec_stack.ga_len;
957 }
958 }
959 break;
960
961 // start of ":try" block
962 case ISN_TRY:
963 {
964 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
965 goto failed;
966 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
967 + ectx.ec_trystack.ga_len;
968 ++ectx.ec_trystack.ga_len;
969 ++trylevel;
970 trycmd->tcd_frame = ectx.ec_frame;
971 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
972 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
973 }
974 break;
975
976 case ISN_PUSHEXC:
977 if (current_exception == NULL)
978 {
979 iemsg("Evaluating catch while current_exception is NULL");
980 goto failed;
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->v_type = VAR_STRING;
987 tv->vval.v_string = vim_strsave(
988 (char_u *)current_exception->value);
989 break;
990
991 case ISN_CATCH:
992 {
993 garray_T *trystack = &ectx.ec_trystack;
994
995 if (trystack->ga_len > 0)
996 {
997 trycmd = ((trycmd_T *)trystack->ga_data)
998 + trystack->ga_len - 1;
999 trycmd->tcd_caught = TRUE;
1000 }
1001 did_emsg = got_int = did_throw = FALSE;
1002 catch_exception(current_exception);
1003 }
1004 break;
1005
1006 // end of ":try" block
1007 case ISN_ENDTRY:
1008 {
1009 garray_T *trystack = &ectx.ec_trystack;
1010
1011 if (trystack->ga_len > 0)
1012 {
1013 --trystack->ga_len;
1014 --trylevel;
1015 trycmd = ((trycmd_T *)trystack->ga_data)
1016 + trystack->ga_len;
1017 if (trycmd->tcd_caught)
1018 {
1019 // discard the exception
1020 if (caught_stack == current_exception)
1021 caught_stack = caught_stack->caught;
1022 discard_current_exception();
1023 }
1024
1025 if (trycmd->tcd_return)
1026 {
1027 // Restore previous function. If the frame pointer
1028 // is zero then there is none and we are done.
1029 if (ectx.ec_frame == initial_frame_ptr)
1030 goto done;
1031
1032 func_return(&ectx);
1033 }
1034 }
1035 }
1036 break;
1037
1038 case ISN_THROW:
1039 --ectx.ec_stack.ga_len;
1040 tv = STACK_TV_BOT(0);
1041 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1042 {
1043 vim_free(tv->vval.v_string);
1044 goto failed;
1045 }
1046 did_throw = TRUE;
1047 break;
1048
1049 // compare with special values
1050 case ISN_COMPAREBOOL:
1051 case ISN_COMPARESPECIAL:
1052 {
1053 typval_T *tv1 = STACK_TV_BOT(-2);
1054 typval_T *tv2 = STACK_TV_BOT(-1);
1055 varnumber_T arg1 = tv1->vval.v_number;
1056 varnumber_T arg2 = tv2->vval.v_number;
1057 int res;
1058
1059 switch (iptr->isn_arg.op.op_type)
1060 {
1061 case EXPR_EQUAL: res = arg1 == arg2; break;
1062 case EXPR_NEQUAL: res = arg1 != arg2; break;
1063 default: res = 0; break;
1064 }
1065
1066 --ectx.ec_stack.ga_len;
1067 tv1->v_type = VAR_BOOL;
1068 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1069 }
1070 break;
1071
1072 // Operation with two number arguments
1073 case ISN_OPNR:
1074 case ISN_COMPARENR:
1075 {
1076 typval_T *tv1 = STACK_TV_BOT(-2);
1077 typval_T *tv2 = STACK_TV_BOT(-1);
1078 varnumber_T arg1 = tv1->vval.v_number;
1079 varnumber_T arg2 = tv2->vval.v_number;
1080 varnumber_T res;
1081
1082 switch (iptr->isn_arg.op.op_type)
1083 {
1084 case EXPR_MULT: res = arg1 * arg2; break;
1085 case EXPR_DIV: res = arg1 / arg2; break;
1086 case EXPR_REM: res = arg1 % arg2; break;
1087 case EXPR_SUB: res = arg1 - arg2; break;
1088 case EXPR_ADD: res = arg1 + arg2; break;
1089
1090 case EXPR_EQUAL: res = arg1 == arg2; break;
1091 case EXPR_NEQUAL: res = arg1 != arg2; break;
1092 case EXPR_GREATER: res = arg1 > arg2; break;
1093 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1094 case EXPR_SMALLER: res = arg1 < arg2; break;
1095 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1096 default: res = 0; break;
1097 }
1098
1099 --ectx.ec_stack.ga_len;
1100 if (iptr->isn_type == ISN_COMPARENR)
1101 {
1102 tv1->v_type = VAR_BOOL;
1103 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1104 }
1105 else
1106 tv1->vval.v_number = res;
1107 }
1108 break;
1109
1110 // Computation with two float arguments
1111 case ISN_OPFLOAT:
1112 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001113#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001114 {
1115 typval_T *tv1 = STACK_TV_BOT(-2);
1116 typval_T *tv2 = STACK_TV_BOT(-1);
1117 float_T arg1 = tv1->vval.v_float;
1118 float_T arg2 = tv2->vval.v_float;
1119 float_T res = 0;
1120 int cmp = FALSE;
1121
1122 switch (iptr->isn_arg.op.op_type)
1123 {
1124 case EXPR_MULT: res = arg1 * arg2; break;
1125 case EXPR_DIV: res = arg1 / arg2; break;
1126 case EXPR_SUB: res = arg1 - arg2; break;
1127 case EXPR_ADD: res = arg1 + arg2; break;
1128
1129 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1130 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1131 case EXPR_GREATER: cmp = arg1 > arg2; break;
1132 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1133 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1134 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1135 default: cmp = 0; break;
1136 }
1137 --ectx.ec_stack.ga_len;
1138 if (iptr->isn_type == ISN_COMPAREFLOAT)
1139 {
1140 tv1->v_type = VAR_BOOL;
1141 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1142 }
1143 else
1144 tv1->vval.v_float = res;
1145 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001146#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001147 break;
1148
1149 case ISN_COMPARELIST:
1150 {
1151 typval_T *tv1 = STACK_TV_BOT(-2);
1152 typval_T *tv2 = STACK_TV_BOT(-1);
1153 list_T *arg1 = tv1->vval.v_list;
1154 list_T *arg2 = tv2->vval.v_list;
1155 int cmp = FALSE;
1156 int ic = iptr->isn_arg.op.op_ic;
1157
1158 switch (iptr->isn_arg.op.op_type)
1159 {
1160 case EXPR_EQUAL: cmp =
1161 list_equal(arg1, arg2, ic, FALSE); break;
1162 case EXPR_NEQUAL: cmp =
1163 !list_equal(arg1, arg2, ic, FALSE); break;
1164 case EXPR_IS: cmp = arg1 == arg2; break;
1165 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1166 default: cmp = 0; break;
1167 }
1168 --ectx.ec_stack.ga_len;
1169 clear_tv(tv1);
1170 clear_tv(tv2);
1171 tv1->v_type = VAR_BOOL;
1172 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1173 }
1174 break;
1175
1176 case ISN_COMPAREBLOB:
1177 {
1178 typval_T *tv1 = STACK_TV_BOT(-2);
1179 typval_T *tv2 = STACK_TV_BOT(-1);
1180 blob_T *arg1 = tv1->vval.v_blob;
1181 blob_T *arg2 = tv2->vval.v_blob;
1182 int cmp = FALSE;
1183
1184 switch (iptr->isn_arg.op.op_type)
1185 {
1186 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1187 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1188 case EXPR_IS: cmp = arg1 == arg2; break;
1189 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1190 default: cmp = 0; break;
1191 }
1192 --ectx.ec_stack.ga_len;
1193 clear_tv(tv1);
1194 clear_tv(tv2);
1195 tv1->v_type = VAR_BOOL;
1196 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1197 }
1198 break;
1199
1200 // TODO: handle separately
1201 case ISN_COMPARESTRING:
1202 case ISN_COMPAREDICT:
1203 case ISN_COMPAREFUNC:
1204 case ISN_COMPAREPARTIAL:
1205 case ISN_COMPAREANY:
1206 {
1207 typval_T *tv1 = STACK_TV_BOT(-2);
1208 typval_T *tv2 = STACK_TV_BOT(-1);
1209 exptype_T exptype = iptr->isn_arg.op.op_type;
1210 int ic = iptr->isn_arg.op.op_ic;
1211
1212 typval_compare(tv1, tv2, exptype, ic);
1213 clear_tv(tv2);
1214 tv1->v_type = VAR_BOOL;
1215 tv1->vval.v_number = tv1->vval.v_number
1216 ? VVAL_TRUE : VVAL_FALSE;
1217 --ectx.ec_stack.ga_len;
1218 }
1219 break;
1220
1221 case ISN_ADDLIST:
1222 case ISN_ADDBLOB:
1223 {
1224 typval_T *tv1 = STACK_TV_BOT(-2);
1225 typval_T *tv2 = STACK_TV_BOT(-1);
1226
1227 if (iptr->isn_type == ISN_ADDLIST)
1228 eval_addlist(tv1, tv2);
1229 else
1230 eval_addblob(tv1, tv2);
1231 clear_tv(tv2);
1232 --ectx.ec_stack.ga_len;
1233 }
1234 break;
1235
1236 // Computation with two arguments of unknown type
1237 case ISN_OPANY:
1238 {
1239 typval_T *tv1 = STACK_TV_BOT(-2);
1240 typval_T *tv2 = STACK_TV_BOT(-1);
1241 varnumber_T n1, n2;
1242#ifdef FEAT_FLOAT
1243 float_T f1 = 0, f2 = 0;
1244#endif
1245 int error = FALSE;
1246
1247 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1248 {
1249 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1250 {
1251 eval_addlist(tv1, tv2);
1252 clear_tv(tv2);
1253 --ectx.ec_stack.ga_len;
1254 break;
1255 }
1256 else if (tv1->v_type == VAR_BLOB
1257 && tv2->v_type == VAR_BLOB)
1258 {
1259 eval_addblob(tv1, tv2);
1260 clear_tv(tv2);
1261 --ectx.ec_stack.ga_len;
1262 break;
1263 }
1264 }
1265#ifdef FEAT_FLOAT
1266 if (tv1->v_type == VAR_FLOAT)
1267 {
1268 f1 = tv1->vval.v_float;
1269 n1 = 0;
1270 }
1271 else
1272#endif
1273 {
1274 n1 = tv_get_number_chk(tv1, &error);
1275 if (error)
1276 goto failed;
1277#ifdef FEAT_FLOAT
1278 if (tv2->v_type == VAR_FLOAT)
1279 f1 = n1;
1280#endif
1281 }
1282#ifdef FEAT_FLOAT
1283 if (tv2->v_type == VAR_FLOAT)
1284 {
1285 f2 = tv2->vval.v_float;
1286 n2 = 0;
1287 }
1288 else
1289#endif
1290 {
1291 n2 = tv_get_number_chk(tv2, &error);
1292 if (error)
1293 goto failed;
1294#ifdef FEAT_FLOAT
1295 if (tv1->v_type == VAR_FLOAT)
1296 f2 = n2;
1297#endif
1298 }
1299#ifdef FEAT_FLOAT
1300 // if there is a float on either side the result is a float
1301 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1302 {
1303 switch (iptr->isn_arg.op.op_type)
1304 {
1305 case EXPR_MULT: f1 = f1 * f2; break;
1306 case EXPR_DIV: f1 = f1 / f2; break;
1307 case EXPR_SUB: f1 = f1 - f2; break;
1308 case EXPR_ADD: f1 = f1 + f2; break;
1309 default: emsg(_(e_modulus)); goto failed;
1310 }
1311 clear_tv(tv1);
1312 clear_tv(tv2);
1313 tv1->v_type = VAR_FLOAT;
1314 tv1->vval.v_float = f1;
1315 --ectx.ec_stack.ga_len;
1316 }
1317 else
1318#endif
1319 {
1320 switch (iptr->isn_arg.op.op_type)
1321 {
1322 case EXPR_MULT: n1 = n1 * n2; break;
1323 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1324 case EXPR_SUB: n1 = n1 - n2; break;
1325 case EXPR_ADD: n1 = n1 + n2; break;
1326 default: n1 = num_modulus(n1, n2); break;
1327 }
1328 clear_tv(tv1);
1329 clear_tv(tv2);
1330 tv1->v_type = VAR_NUMBER;
1331 tv1->vval.v_number = n1;
1332 --ectx.ec_stack.ga_len;
1333 }
1334 }
1335 break;
1336
1337 case ISN_CONCAT:
1338 {
1339 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1340 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1341 char_u *res;
1342
1343 res = concat_str(str1, str2);
1344 clear_tv(STACK_TV_BOT(-2));
1345 clear_tv(STACK_TV_BOT(-1));
1346 --ectx.ec_stack.ga_len;
1347 STACK_TV_BOT(-1)->vval.v_string = res;
1348 }
1349 break;
1350
1351 case ISN_INDEX:
1352 {
1353 list_T *list;
1354 varnumber_T n;
1355 listitem_T *li;
1356
1357 // list index: list is at stack-2, index at stack-1
1358 tv = STACK_TV_BOT(-2);
1359 if (tv->v_type != VAR_LIST)
1360 {
1361 emsg(_(e_listreq));
1362 goto failed;
1363 }
1364 list = tv->vval.v_list;
1365
1366 tv = STACK_TV_BOT(-1);
1367 if (tv->v_type != VAR_NUMBER)
1368 {
1369 emsg(_(e_number_exp));
1370 goto failed;
1371 }
1372 n = tv->vval.v_number;
1373 clear_tv(tv);
1374 if ((li = list_find(list, n)) == NULL)
1375 {
1376 semsg(_(e_listidx), n);
1377 goto failed;
1378 }
1379 --ectx.ec_stack.ga_len;
1380 clear_tv(STACK_TV_BOT(-1));
1381 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
1382 }
1383 break;
1384
1385 // dict member with string key
1386 case ISN_MEMBER:
1387 {
1388 dict_T *dict;
1389 dictitem_T *di;
1390
1391 tv = STACK_TV_BOT(-1);
1392 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
1393 {
1394 emsg(_(e_dictreq));
1395 goto failed;
1396 }
1397 dict = tv->vval.v_dict;
1398
1399 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
1400 == NULL)
1401 {
1402 semsg(_(e_dictkey), iptr->isn_arg.string);
1403 goto failed;
1404 }
1405 clear_tv(tv);
1406 copy_tv(&di->di_tv, tv);
1407 }
1408 break;
1409
1410 case ISN_NEGATENR:
1411 tv = STACK_TV_BOT(-1);
1412 tv->vval.v_number = -tv->vval.v_number;
1413 break;
1414
1415 case ISN_CHECKNR:
1416 {
1417 int error = FALSE;
1418
1419 tv = STACK_TV_BOT(-1);
1420 if (check_not_string(tv) == FAIL)
1421 {
1422 --ectx.ec_stack.ga_len;
1423 goto failed;
1424 }
1425 (void)tv_get_number_chk(tv, &error);
1426 if (error)
1427 goto failed;
1428 }
1429 break;
1430
1431 case ISN_CHECKTYPE:
1432 {
1433 checktype_T *ct = &iptr->isn_arg.type;
1434
1435 tv = STACK_TV_BOT(ct->ct_off);
1436 if (tv->v_type != ct->ct_type)
1437 {
1438 semsg(_("E1029: Expected %s but got %s"),
1439 vartype_name(ct->ct_type),
1440 vartype_name(tv->v_type));
1441 goto failed;
1442 }
1443 }
1444 break;
1445
1446 case ISN_2BOOL:
1447 {
1448 int n;
1449
1450 tv = STACK_TV_BOT(-1);
1451 n = tv2bool(tv);
1452 if (iptr->isn_arg.number) // invert
1453 n = !n;
1454 clear_tv(tv);
1455 tv->v_type = VAR_BOOL;
1456 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
1457 }
1458 break;
1459
1460 case ISN_2STRING:
1461 {
1462 char_u *str;
1463
1464 tv = STACK_TV_BOT(iptr->isn_arg.number);
1465 if (tv->v_type != VAR_STRING)
1466 {
1467 str = typval_tostring(tv);
1468 clear_tv(tv);
1469 tv->v_type = VAR_STRING;
1470 tv->vval.v_string = str;
1471 }
1472 }
1473 break;
1474
1475 case ISN_DROP:
1476 --ectx.ec_stack.ga_len;
1477 clear_tv(STACK_TV_BOT(0));
1478 break;
1479 }
1480 }
1481
1482done:
1483 // function finished, get result from the stack.
1484 tv = STACK_TV_BOT(-1);
1485 *rettv = *tv;
1486 tv->v_type = VAR_UNKNOWN;
1487 ret = OK;
1488
1489failed:
1490 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
1491 clear_tv(STACK_TV(idx));
1492 vim_free(ectx.ec_stack.ga_data);
1493 return ret;
1494}
1495
1496#define DISASSEMBLE 1
1497
1498/*
1499 * ":dissassemble".
1500 */
1501 void
1502ex_disassemble(exarg_T *eap)
1503{
1504#ifdef DISASSEMBLE
1505 ufunc_T *ufunc = find_func(eap->arg, NULL);
1506 dfunc_T *dfunc;
1507 isn_T *instr;
1508 int current;
1509 int line_idx = 0;
1510 int prev_current = 0;
1511
1512 if (ufunc == NULL)
1513 {
1514 semsg("Cannot find function %s", eap->arg);
1515 return;
1516 }
1517 if (ufunc->uf_dfunc_idx < 0)
1518 {
1519 semsg("Function %s is not compiled", eap->arg);
1520 return;
1521 }
1522 if (ufunc->uf_name_exp != NULL)
1523 msg((char *)ufunc->uf_name_exp);
1524 else
1525 msg((char *)ufunc->uf_name);
1526
1527 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
1528 instr = dfunc->df_instr;
1529 for (current = 0; current < dfunc->df_instr_count; ++current)
1530 {
1531 isn_T *iptr = &instr[current];
1532
1533 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
1534 {
1535 if (current > prev_current)
1536 {
1537 msg_puts("\n\n");
1538 prev_current = current;
1539 }
1540 msg(((char **)ufunc->uf_lines.ga_data)[line_idx++]);
1541 }
1542
1543 switch (iptr->isn_type)
1544 {
1545 case ISN_EXEC:
1546 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
1547 break;
1548 case ISN_ECHO:
1549 {
1550 echo_T *echo = &iptr->isn_arg.echo;
1551
1552 smsg("%4d %s %d", current,
1553 echo->echo_with_white ? "ECHO" : "ECHON",
1554 echo->echo_count);
1555 }
1556 break;
1557 case ISN_LOAD:
1558 if (iptr->isn_arg.number < 0)
1559 smsg("%4d LOAD arg[%lld]", current,
1560 iptr->isn_arg.number + STACK_FRAME_SIZE);
1561 else
1562 smsg("%4d LOAD $%lld", current, iptr->isn_arg.number);
1563 break;
1564 case ISN_LOADV:
1565 smsg("%4d LOADV v:%s", current,
1566 get_vim_var_name(iptr->isn_arg.number));
1567 break;
1568 case ISN_LOADSCRIPT:
1569 {
1570 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001571 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1573 + iptr->isn_arg.script.script_idx;
1574
1575 smsg("%4d LOADSCRIPT %s from %s", current,
1576 sv->sv_name, si->sn_name);
1577 }
1578 break;
1579 case ISN_LOADS:
1580 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001581 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.loads.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582
1583 smsg("%4d LOADS s:%s from %s", current,
1584 iptr->isn_arg.string, si->sn_name);
1585 }
1586 break;
1587 case ISN_LOADG:
1588 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
1589 break;
1590 case ISN_LOADOPT:
1591 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
1592 break;
1593 case ISN_LOADENV:
1594 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
1595 break;
1596 case ISN_LOADREG:
1597 smsg("%4d LOADREG @%c", current, iptr->isn_arg.number);
1598 break;
1599
1600 case ISN_STORE:
1601 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
1602 break;
1603 case ISN_STOREG:
1604 smsg("%4d STOREG g:%s", current, iptr->isn_arg.string);
1605 break;
1606 case ISN_STORESCRIPT:
1607 {
1608 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001609 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1611 + iptr->isn_arg.script.script_idx;
1612
1613 smsg("%4d STORESCRIPT %s in %s", current,
1614 sv->sv_name, si->sn_name);
1615 }
1616 break;
1617 case ISN_STOREOPT:
1618 smsg("%4d STOREOPT &%s", current,
1619 iptr->isn_arg.storeopt.so_name);
1620 break;
1621
1622 case ISN_STORENR:
1623 smsg("%4d STORE %lld in $%d", current,
1624 iptr->isn_arg.storenr.str_val,
1625 iptr->isn_arg.storenr.str_idx);
1626 break;
1627
1628 // constants
1629 case ISN_PUSHNR:
1630 smsg("%4d PUSHNR %lld", current, iptr->isn_arg.number);
1631 break;
1632 case ISN_PUSHBOOL:
1633 case ISN_PUSHSPEC:
1634 smsg("%4d PUSH %s", current,
1635 get_var_special_name(iptr->isn_arg.number));
1636 break;
1637 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001638#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01001640#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 break;
1642 case ISN_PUSHS:
1643 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
1644 break;
1645 case ISN_PUSHBLOB:
1646 {
1647 char_u *r;
1648 char_u numbuf[NUMBUFLEN];
1649 char_u *tofree;
1650
1651 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
1652 smsg("%4d PUSHBLOB \"%s\"", current, r);
1653 vim_free(tofree);
1654 }
1655 break;
1656 case ISN_PUSHEXC:
1657 smsg("%4d PUSH v:exception", current);
1658 break;
1659 case ISN_NEWLIST:
1660 smsg("%4d NEWLIST size %lld", current, iptr->isn_arg.number);
1661 break;
1662 case ISN_NEWDICT:
1663 smsg("%4d NEWDICT size %lld", current, iptr->isn_arg.number);
1664 break;
1665
1666 // function call
1667 case ISN_BCALL:
1668 {
1669 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
1670
1671 smsg("%4d BCALL %s(argc %d)", current,
1672 internal_func_name(cbfunc->cbf_idx),
1673 cbfunc->cbf_argcount);
1674 }
1675 break;
1676 case ISN_DCALL:
1677 {
1678 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
1679 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
1680 + cdfunc->cdf_idx;
1681
1682 smsg("%4d DCALL %s(argc %d)", current,
1683 df->df_ufunc->uf_name_exp != NULL
1684 ? df->df_ufunc->uf_name_exp
1685 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
1686 }
1687 break;
1688 case ISN_UCALL:
1689 {
1690 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1691
1692 smsg("%4d UCALL %s(argc %d)", current,
1693 cufunc->cuf_name, cufunc->cuf_argcount);
1694 }
1695 break;
1696 case ISN_PCALL:
1697 {
1698 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
1699
1700 smsg("%4d PCALL%s (argc %d)", current,
1701 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
1702 }
1703 break;
1704 case ISN_RETURN:
1705 smsg("%4d RETURN", current);
1706 break;
1707 case ISN_FUNCREF:
1708 {
1709 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
1710 + iptr->isn_arg.number;
1711
1712 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
1713 }
1714 break;
1715
1716 case ISN_JUMP:
1717 {
1718 char *when = "?";
1719
1720 switch (iptr->isn_arg.jump.jump_when)
1721 {
1722 case JUMP_ALWAYS:
1723 when = "JUMP";
1724 break;
1725 case JUMP_IF_TRUE:
1726 when = "JUMP_IF_TRUE";
1727 break;
1728 case JUMP_AND_KEEP_IF_TRUE:
1729 when = "JUMP_AND_KEEP_IF_TRUE";
1730 break;
1731 case JUMP_IF_FALSE:
1732 when = "JUMP_IF_FALSE";
1733 break;
1734 case JUMP_AND_KEEP_IF_FALSE:
1735 when = "JUMP_AND_KEEP_IF_FALSE";
1736 break;
1737 }
1738 smsg("%4d %s -> %lld", current, when,
1739 iptr->isn_arg.jump.jump_where);
1740 }
1741 break;
1742
1743 case ISN_FOR:
1744 {
1745 forloop_T *forloop = &iptr->isn_arg.forloop;
1746
1747 smsg("%4d FOR $%d -> %d", current,
1748 forloop->for_idx, forloop->for_end);
1749 }
1750 break;
1751
1752 case ISN_TRY:
1753 {
1754 try_T *try = &iptr->isn_arg.try;
1755
1756 smsg("%4d TRY catch -> %d, finally -> %d", current,
1757 try->try_catch, try->try_finally);
1758 }
1759 break;
1760 case ISN_CATCH:
1761 // TODO
1762 smsg("%4d CATCH", current);
1763 break;
1764 case ISN_ENDTRY:
1765 smsg("%4d ENDTRY", current);
1766 break;
1767 case ISN_THROW:
1768 smsg("%4d THROW", current);
1769 break;
1770
1771 // expression operations on number
1772 case ISN_OPNR:
1773 case ISN_OPFLOAT:
1774 case ISN_OPANY:
1775 {
1776 char *what;
1777 char *ins;
1778
1779 switch (iptr->isn_arg.op.op_type)
1780 {
1781 case EXPR_MULT: what = "*"; break;
1782 case EXPR_DIV: what = "/"; break;
1783 case EXPR_REM: what = "%"; break;
1784 case EXPR_SUB: what = "-"; break;
1785 case EXPR_ADD: what = "+"; break;
1786 default: what = "???"; break;
1787 }
1788 switch (iptr->isn_type)
1789 {
1790 case ISN_OPNR: ins = "OPNR"; break;
1791 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
1792 case ISN_OPANY: ins = "OPANY"; break;
1793 default: ins = "???"; break;
1794 }
1795 smsg("%4d %s %s", current, ins, what);
1796 }
1797 break;
1798
1799 case ISN_COMPAREBOOL:
1800 case ISN_COMPARESPECIAL:
1801 case ISN_COMPARENR:
1802 case ISN_COMPAREFLOAT:
1803 case ISN_COMPARESTRING:
1804 case ISN_COMPAREBLOB:
1805 case ISN_COMPARELIST:
1806 case ISN_COMPAREDICT:
1807 case ISN_COMPAREFUNC:
1808 case ISN_COMPAREPARTIAL:
1809 case ISN_COMPAREANY:
1810 {
1811 char *p;
1812 char buf[10];
1813 char *type;
1814
1815 switch (iptr->isn_arg.op.op_type)
1816 {
1817 case EXPR_EQUAL: p = "=="; break;
1818 case EXPR_NEQUAL: p = "!="; break;
1819 case EXPR_GREATER: p = ">"; break;
1820 case EXPR_GEQUAL: p = ">="; break;
1821 case EXPR_SMALLER: p = "<"; break;
1822 case EXPR_SEQUAL: p = "<="; break;
1823 case EXPR_MATCH: p = "=~"; break;
1824 case EXPR_IS: p = "is"; break;
1825 case EXPR_ISNOT: p = "isnot"; break;
1826 case EXPR_NOMATCH: p = "!~"; break;
1827 default: p = "???"; break;
1828 }
1829 STRCPY(buf, p);
1830 if (iptr->isn_arg.op.op_ic == TRUE)
1831 strcat(buf, "?");
1832 switch(iptr->isn_type)
1833 {
1834 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
1835 case ISN_COMPARESPECIAL:
1836 type = "COMPARESPECIAL"; break;
1837 case ISN_COMPARENR: type = "COMPARENR"; break;
1838 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
1839 case ISN_COMPARESTRING:
1840 type = "COMPARESTRING"; break;
1841 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
1842 case ISN_COMPARELIST: type = "COMPARELIST"; break;
1843 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
1844 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
1845 case ISN_COMPAREPARTIAL:
1846 type = "COMPAREPARTIAL"; break;
1847 case ISN_COMPAREANY: type = "COMPAREANY"; break;
1848 default: type = "???"; break;
1849 }
1850
1851 smsg("%4d %s %s", current, type, buf);
1852 }
1853 break;
1854
1855 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
1856 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
1857
1858 // expression operations
1859 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
1860 case ISN_INDEX: smsg("%4d INDEX", current); break;
1861 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
1862 iptr->isn_arg.string); break;
1863 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
1864
1865 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
1866 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
1867 vartype_name(iptr->isn_arg.type.ct_type),
1868 iptr->isn_arg.type.ct_off);
1869 break;
1870 case ISN_2BOOL: if (iptr->isn_arg.number)
1871 smsg("%4d INVERT (!val)", current);
1872 else
1873 smsg("%4d 2BOOL (!!val)", current);
1874 break;
1875 case ISN_2STRING: smsg("%4d 2STRING stack[%d]", current,
1876 iptr->isn_arg.number);
1877 break;
1878
1879 case ISN_DROP: smsg("%4d DROP", current); break;
1880 }
1881 }
1882#endif
1883}
1884
1885/*
1886 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
1887 * list, etc. Mostly like what JavaScript does, except that empty list and
1888 * empty dictionary are FALSE.
1889 */
1890 int
1891tv2bool(typval_T *tv)
1892{
1893 switch (tv->v_type)
1894 {
1895 case VAR_NUMBER:
1896 return tv->vval.v_number != 0;
1897 case VAR_FLOAT:
1898#ifdef FEAT_FLOAT
1899 return tv->vval.v_float != 0.0;
1900#else
1901 break;
1902#endif
1903 case VAR_PARTIAL:
1904 return tv->vval.v_partial != NULL;
1905 case VAR_FUNC:
1906 case VAR_STRING:
1907 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
1908 case VAR_LIST:
1909 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
1910 case VAR_DICT:
1911 return tv->vval.v_dict != NULL
1912 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
1913 case VAR_BOOL:
1914 case VAR_SPECIAL:
1915 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
1916 case VAR_JOB:
1917#ifdef FEAT_JOB_CHANNEL
1918 return tv->vval.v_job != NULL;
1919#else
1920 break;
1921#endif
1922 case VAR_CHANNEL:
1923#ifdef FEAT_JOB_CHANNEL
1924 return tv->vval.v_channel != NULL;
1925#else
1926 break;
1927#endif
1928 case VAR_BLOB:
1929 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
1930 case VAR_UNKNOWN:
1931 case VAR_VOID:
1932 break;
1933 }
1934 return FALSE;
1935}
1936
1937/*
1938 * If "tv" is a string give an error and return FAIL.
1939 */
1940 int
1941check_not_string(typval_T *tv)
1942{
1943 if (tv->v_type == VAR_STRING)
1944 {
1945 emsg(_("E1030: Using a String as a Number"));
1946 clear_tv(tv);
1947 return FAIL;
1948 }
1949 return OK;
1950}
1951
1952
1953#endif // FEAT_EVAL