blob: 480f1ec565c2a5589f49bc41c8b367cad14cabc7 [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 {
854 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame
855 && trycmd->tcd_finally_idx != 0)
856 {
857 // jump to ":finally"
858 ectx.ec_iidx = trycmd->tcd_finally_idx;
859 trycmd->tcd_return = TRUE;
860 }
861 else
862 {
863 // Restore previous function. If the frame pointer
864 // is zero then there is none and we are done.
865 if (ectx.ec_frame == initial_frame_ptr)
866 goto done;
867
868 func_return(&ectx);
869 }
870 }
871 break;
872
873 // push a function reference to a compiled function
874 case ISN_FUNCREF:
875 {
876 partial_T *pt = NULL;
877
878 pt = ALLOC_CLEAR_ONE(partial_T);
879 if (pt == NULL)
880 goto failed;
881 dfunc = ((dfunc_T *)def_functions.ga_data)
882 + iptr->isn_arg.number;
883 pt->pt_func = dfunc->df_ufunc;
884 pt->pt_refcount = 1;
885 ++dfunc->df_ufunc->uf_refcount;
886
887 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
888 goto failed;
889 tv = STACK_TV_BOT(0);
890 ++ectx.ec_stack.ga_len;
891 tv->vval.v_partial = pt;
892 tv->v_type = VAR_PARTIAL;
893 }
894 break;
895
896 // jump if a condition is met
897 case ISN_JUMP:
898 {
899 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
900 int jump = TRUE;
901
902 if (when != JUMP_ALWAYS)
903 {
904 tv = STACK_TV_BOT(-1);
905 jump = tv2bool(tv);
906 if (when == JUMP_IF_FALSE
907 || when == JUMP_AND_KEEP_IF_FALSE)
908 jump = !jump;
909 if (when == JUMP_IF_FALSE || when == JUMP_IF_TRUE
910 || !jump)
911 {
912 // drop the value from the stack
913 clear_tv(tv);
914 --ectx.ec_stack.ga_len;
915 }
916 }
917 if (jump)
918 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
919 }
920 break;
921
922 // top of a for loop
923 case ISN_FOR:
924 {
925 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
926 typval_T *idxtv =
927 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
928
929 // push the next item from the list
930 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
931 goto failed;
932 if (++idxtv->vval.v_number >= list->lv_len)
933 // past the end of the list, jump to "endfor"
934 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
935 else if (list->lv_first == &range_list_item)
936 {
937 // non-materialized range() list
938 tv = STACK_TV_BOT(0);
939 tv->v_type = VAR_NUMBER;
940 tv->vval.v_number = list_find_nr(
941 list, idxtv->vval.v_number, NULL);
942 ++ectx.ec_stack.ga_len;
943 }
944 else
945 {
946 listitem_T *li = list_find(list, idxtv->vval.v_number);
947
948 if (li == NULL)
949 goto failed;
950 copy_tv(&li->li_tv, STACK_TV_BOT(0));
951 ++ectx.ec_stack.ga_len;
952 }
953 }
954 break;
955
956 // start of ":try" block
957 case ISN_TRY:
958 {
959 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
960 goto failed;
961 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
962 + ectx.ec_trystack.ga_len;
963 ++ectx.ec_trystack.ga_len;
964 ++trylevel;
965 trycmd->tcd_frame = ectx.ec_frame;
966 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
967 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
968 }
969 break;
970
971 case ISN_PUSHEXC:
972 if (current_exception == NULL)
973 {
974 iemsg("Evaluating catch while current_exception is NULL");
975 goto failed;
976 }
977 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
978 goto failed;
979 tv = STACK_TV_BOT(0);
980 ++ectx.ec_stack.ga_len;
981 tv->v_type = VAR_STRING;
982 tv->vval.v_string = vim_strsave(
983 (char_u *)current_exception->value);
984 break;
985
986 case ISN_CATCH:
987 {
988 garray_T *trystack = &ectx.ec_trystack;
989
990 if (trystack->ga_len > 0)
991 {
992 trycmd = ((trycmd_T *)trystack->ga_data)
993 + trystack->ga_len - 1;
994 trycmd->tcd_caught = TRUE;
995 }
996 did_emsg = got_int = did_throw = FALSE;
997 catch_exception(current_exception);
998 }
999 break;
1000
1001 // end of ":try" block
1002 case ISN_ENDTRY:
1003 {
1004 garray_T *trystack = &ectx.ec_trystack;
1005
1006 if (trystack->ga_len > 0)
1007 {
1008 --trystack->ga_len;
1009 --trylevel;
1010 trycmd = ((trycmd_T *)trystack->ga_data)
1011 + trystack->ga_len;
1012 if (trycmd->tcd_caught)
1013 {
1014 // discard the exception
1015 if (caught_stack == current_exception)
1016 caught_stack = caught_stack->caught;
1017 discard_current_exception();
1018 }
1019
1020 if (trycmd->tcd_return)
1021 {
1022 // Restore previous function. If the frame pointer
1023 // is zero then there is none and we are done.
1024 if (ectx.ec_frame == initial_frame_ptr)
1025 goto done;
1026
1027 func_return(&ectx);
1028 }
1029 }
1030 }
1031 break;
1032
1033 case ISN_THROW:
1034 --ectx.ec_stack.ga_len;
1035 tv = STACK_TV_BOT(0);
1036 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1037 {
1038 vim_free(tv->vval.v_string);
1039 goto failed;
1040 }
1041 did_throw = TRUE;
1042 break;
1043
1044 // compare with special values
1045 case ISN_COMPAREBOOL:
1046 case ISN_COMPARESPECIAL:
1047 {
1048 typval_T *tv1 = STACK_TV_BOT(-2);
1049 typval_T *tv2 = STACK_TV_BOT(-1);
1050 varnumber_T arg1 = tv1->vval.v_number;
1051 varnumber_T arg2 = tv2->vval.v_number;
1052 int res;
1053
1054 switch (iptr->isn_arg.op.op_type)
1055 {
1056 case EXPR_EQUAL: res = arg1 == arg2; break;
1057 case EXPR_NEQUAL: res = arg1 != arg2; break;
1058 default: res = 0; break;
1059 }
1060
1061 --ectx.ec_stack.ga_len;
1062 tv1->v_type = VAR_BOOL;
1063 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1064 }
1065 break;
1066
1067 // Operation with two number arguments
1068 case ISN_OPNR:
1069 case ISN_COMPARENR:
1070 {
1071 typval_T *tv1 = STACK_TV_BOT(-2);
1072 typval_T *tv2 = STACK_TV_BOT(-1);
1073 varnumber_T arg1 = tv1->vval.v_number;
1074 varnumber_T arg2 = tv2->vval.v_number;
1075 varnumber_T res;
1076
1077 switch (iptr->isn_arg.op.op_type)
1078 {
1079 case EXPR_MULT: res = arg1 * arg2; break;
1080 case EXPR_DIV: res = arg1 / arg2; break;
1081 case EXPR_REM: res = arg1 % arg2; break;
1082 case EXPR_SUB: res = arg1 - arg2; break;
1083 case EXPR_ADD: res = arg1 + arg2; break;
1084
1085 case EXPR_EQUAL: res = arg1 == arg2; break;
1086 case EXPR_NEQUAL: res = arg1 != arg2; break;
1087 case EXPR_GREATER: res = arg1 > arg2; break;
1088 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1089 case EXPR_SMALLER: res = arg1 < arg2; break;
1090 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1091 default: res = 0; break;
1092 }
1093
1094 --ectx.ec_stack.ga_len;
1095 if (iptr->isn_type == ISN_COMPARENR)
1096 {
1097 tv1->v_type = VAR_BOOL;
1098 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1099 }
1100 else
1101 tv1->vval.v_number = res;
1102 }
1103 break;
1104
1105 // Computation with two float arguments
1106 case ISN_OPFLOAT:
1107 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001108#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 {
1110 typval_T *tv1 = STACK_TV_BOT(-2);
1111 typval_T *tv2 = STACK_TV_BOT(-1);
1112 float_T arg1 = tv1->vval.v_float;
1113 float_T arg2 = tv2->vval.v_float;
1114 float_T res = 0;
1115 int cmp = FALSE;
1116
1117 switch (iptr->isn_arg.op.op_type)
1118 {
1119 case EXPR_MULT: res = arg1 * arg2; break;
1120 case EXPR_DIV: res = arg1 / arg2; break;
1121 case EXPR_SUB: res = arg1 - arg2; break;
1122 case EXPR_ADD: res = arg1 + arg2; break;
1123
1124 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1125 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1126 case EXPR_GREATER: cmp = arg1 > arg2; break;
1127 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1128 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1129 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1130 default: cmp = 0; break;
1131 }
1132 --ectx.ec_stack.ga_len;
1133 if (iptr->isn_type == ISN_COMPAREFLOAT)
1134 {
1135 tv1->v_type = VAR_BOOL;
1136 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1137 }
1138 else
1139 tv1->vval.v_float = res;
1140 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001141#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 break;
1143
1144 case ISN_COMPARELIST:
1145 {
1146 typval_T *tv1 = STACK_TV_BOT(-2);
1147 typval_T *tv2 = STACK_TV_BOT(-1);
1148 list_T *arg1 = tv1->vval.v_list;
1149 list_T *arg2 = tv2->vval.v_list;
1150 int cmp = FALSE;
1151 int ic = iptr->isn_arg.op.op_ic;
1152
1153 switch (iptr->isn_arg.op.op_type)
1154 {
1155 case EXPR_EQUAL: cmp =
1156 list_equal(arg1, arg2, ic, FALSE); break;
1157 case EXPR_NEQUAL: cmp =
1158 !list_equal(arg1, arg2, ic, FALSE); break;
1159 case EXPR_IS: cmp = arg1 == arg2; break;
1160 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1161 default: cmp = 0; break;
1162 }
1163 --ectx.ec_stack.ga_len;
1164 clear_tv(tv1);
1165 clear_tv(tv2);
1166 tv1->v_type = VAR_BOOL;
1167 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1168 }
1169 break;
1170
1171 case ISN_COMPAREBLOB:
1172 {
1173 typval_T *tv1 = STACK_TV_BOT(-2);
1174 typval_T *tv2 = STACK_TV_BOT(-1);
1175 blob_T *arg1 = tv1->vval.v_blob;
1176 blob_T *arg2 = tv2->vval.v_blob;
1177 int cmp = FALSE;
1178
1179 switch (iptr->isn_arg.op.op_type)
1180 {
1181 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1182 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1183 case EXPR_IS: cmp = arg1 == arg2; break;
1184 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1185 default: cmp = 0; break;
1186 }
1187 --ectx.ec_stack.ga_len;
1188 clear_tv(tv1);
1189 clear_tv(tv2);
1190 tv1->v_type = VAR_BOOL;
1191 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1192 }
1193 break;
1194
1195 // TODO: handle separately
1196 case ISN_COMPARESTRING:
1197 case ISN_COMPAREDICT:
1198 case ISN_COMPAREFUNC:
1199 case ISN_COMPAREPARTIAL:
1200 case ISN_COMPAREANY:
1201 {
1202 typval_T *tv1 = STACK_TV_BOT(-2);
1203 typval_T *tv2 = STACK_TV_BOT(-1);
1204 exptype_T exptype = iptr->isn_arg.op.op_type;
1205 int ic = iptr->isn_arg.op.op_ic;
1206
1207 typval_compare(tv1, tv2, exptype, ic);
1208 clear_tv(tv2);
1209 tv1->v_type = VAR_BOOL;
1210 tv1->vval.v_number = tv1->vval.v_number
1211 ? VVAL_TRUE : VVAL_FALSE;
1212 --ectx.ec_stack.ga_len;
1213 }
1214 break;
1215
1216 case ISN_ADDLIST:
1217 case ISN_ADDBLOB:
1218 {
1219 typval_T *tv1 = STACK_TV_BOT(-2);
1220 typval_T *tv2 = STACK_TV_BOT(-1);
1221
1222 if (iptr->isn_type == ISN_ADDLIST)
1223 eval_addlist(tv1, tv2);
1224 else
1225 eval_addblob(tv1, tv2);
1226 clear_tv(tv2);
1227 --ectx.ec_stack.ga_len;
1228 }
1229 break;
1230
1231 // Computation with two arguments of unknown type
1232 case ISN_OPANY:
1233 {
1234 typval_T *tv1 = STACK_TV_BOT(-2);
1235 typval_T *tv2 = STACK_TV_BOT(-1);
1236 varnumber_T n1, n2;
1237#ifdef FEAT_FLOAT
1238 float_T f1 = 0, f2 = 0;
1239#endif
1240 int error = FALSE;
1241
1242 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1243 {
1244 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1245 {
1246 eval_addlist(tv1, tv2);
1247 clear_tv(tv2);
1248 --ectx.ec_stack.ga_len;
1249 break;
1250 }
1251 else if (tv1->v_type == VAR_BLOB
1252 && tv2->v_type == VAR_BLOB)
1253 {
1254 eval_addblob(tv1, tv2);
1255 clear_tv(tv2);
1256 --ectx.ec_stack.ga_len;
1257 break;
1258 }
1259 }
1260#ifdef FEAT_FLOAT
1261 if (tv1->v_type == VAR_FLOAT)
1262 {
1263 f1 = tv1->vval.v_float;
1264 n1 = 0;
1265 }
1266 else
1267#endif
1268 {
1269 n1 = tv_get_number_chk(tv1, &error);
1270 if (error)
1271 goto failed;
1272#ifdef FEAT_FLOAT
1273 if (tv2->v_type == VAR_FLOAT)
1274 f1 = n1;
1275#endif
1276 }
1277#ifdef FEAT_FLOAT
1278 if (tv2->v_type == VAR_FLOAT)
1279 {
1280 f2 = tv2->vval.v_float;
1281 n2 = 0;
1282 }
1283 else
1284#endif
1285 {
1286 n2 = tv_get_number_chk(tv2, &error);
1287 if (error)
1288 goto failed;
1289#ifdef FEAT_FLOAT
1290 if (tv1->v_type == VAR_FLOAT)
1291 f2 = n2;
1292#endif
1293 }
1294#ifdef FEAT_FLOAT
1295 // if there is a float on either side the result is a float
1296 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1297 {
1298 switch (iptr->isn_arg.op.op_type)
1299 {
1300 case EXPR_MULT: f1 = f1 * f2; break;
1301 case EXPR_DIV: f1 = f1 / f2; break;
1302 case EXPR_SUB: f1 = f1 - f2; break;
1303 case EXPR_ADD: f1 = f1 + f2; break;
1304 default: emsg(_(e_modulus)); goto failed;
1305 }
1306 clear_tv(tv1);
1307 clear_tv(tv2);
1308 tv1->v_type = VAR_FLOAT;
1309 tv1->vval.v_float = f1;
1310 --ectx.ec_stack.ga_len;
1311 }
1312 else
1313#endif
1314 {
1315 switch (iptr->isn_arg.op.op_type)
1316 {
1317 case EXPR_MULT: n1 = n1 * n2; break;
1318 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1319 case EXPR_SUB: n1 = n1 - n2; break;
1320 case EXPR_ADD: n1 = n1 + n2; break;
1321 default: n1 = num_modulus(n1, n2); break;
1322 }
1323 clear_tv(tv1);
1324 clear_tv(tv2);
1325 tv1->v_type = VAR_NUMBER;
1326 tv1->vval.v_number = n1;
1327 --ectx.ec_stack.ga_len;
1328 }
1329 }
1330 break;
1331
1332 case ISN_CONCAT:
1333 {
1334 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1335 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1336 char_u *res;
1337
1338 res = concat_str(str1, str2);
1339 clear_tv(STACK_TV_BOT(-2));
1340 clear_tv(STACK_TV_BOT(-1));
1341 --ectx.ec_stack.ga_len;
1342 STACK_TV_BOT(-1)->vval.v_string = res;
1343 }
1344 break;
1345
1346 case ISN_INDEX:
1347 {
1348 list_T *list;
1349 varnumber_T n;
1350 listitem_T *li;
1351
1352 // list index: list is at stack-2, index at stack-1
1353 tv = STACK_TV_BOT(-2);
1354 if (tv->v_type != VAR_LIST)
1355 {
1356 emsg(_(e_listreq));
1357 goto failed;
1358 }
1359 list = tv->vval.v_list;
1360
1361 tv = STACK_TV_BOT(-1);
1362 if (tv->v_type != VAR_NUMBER)
1363 {
1364 emsg(_(e_number_exp));
1365 goto failed;
1366 }
1367 n = tv->vval.v_number;
1368 clear_tv(tv);
1369 if ((li = list_find(list, n)) == NULL)
1370 {
1371 semsg(_(e_listidx), n);
1372 goto failed;
1373 }
1374 --ectx.ec_stack.ga_len;
1375 clear_tv(STACK_TV_BOT(-1));
1376 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
1377 }
1378 break;
1379
1380 // dict member with string key
1381 case ISN_MEMBER:
1382 {
1383 dict_T *dict;
1384 dictitem_T *di;
1385
1386 tv = STACK_TV_BOT(-1);
1387 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
1388 {
1389 emsg(_(e_dictreq));
1390 goto failed;
1391 }
1392 dict = tv->vval.v_dict;
1393
1394 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
1395 == NULL)
1396 {
1397 semsg(_(e_dictkey), iptr->isn_arg.string);
1398 goto failed;
1399 }
1400 clear_tv(tv);
1401 copy_tv(&di->di_tv, tv);
1402 }
1403 break;
1404
1405 case ISN_NEGATENR:
1406 tv = STACK_TV_BOT(-1);
1407 tv->vval.v_number = -tv->vval.v_number;
1408 break;
1409
1410 case ISN_CHECKNR:
1411 {
1412 int error = FALSE;
1413
1414 tv = STACK_TV_BOT(-1);
1415 if (check_not_string(tv) == FAIL)
1416 {
1417 --ectx.ec_stack.ga_len;
1418 goto failed;
1419 }
1420 (void)tv_get_number_chk(tv, &error);
1421 if (error)
1422 goto failed;
1423 }
1424 break;
1425
1426 case ISN_CHECKTYPE:
1427 {
1428 checktype_T *ct = &iptr->isn_arg.type;
1429
1430 tv = STACK_TV_BOT(ct->ct_off);
1431 if (tv->v_type != ct->ct_type)
1432 {
1433 semsg(_("E1029: Expected %s but got %s"),
1434 vartype_name(ct->ct_type),
1435 vartype_name(tv->v_type));
1436 goto failed;
1437 }
1438 }
1439 break;
1440
1441 case ISN_2BOOL:
1442 {
1443 int n;
1444
1445 tv = STACK_TV_BOT(-1);
1446 n = tv2bool(tv);
1447 if (iptr->isn_arg.number) // invert
1448 n = !n;
1449 clear_tv(tv);
1450 tv->v_type = VAR_BOOL;
1451 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
1452 }
1453 break;
1454
1455 case ISN_2STRING:
1456 {
1457 char_u *str;
1458
1459 tv = STACK_TV_BOT(iptr->isn_arg.number);
1460 if (tv->v_type != VAR_STRING)
1461 {
1462 str = typval_tostring(tv);
1463 clear_tv(tv);
1464 tv->v_type = VAR_STRING;
1465 tv->vval.v_string = str;
1466 }
1467 }
1468 break;
1469
1470 case ISN_DROP:
1471 --ectx.ec_stack.ga_len;
1472 clear_tv(STACK_TV_BOT(0));
1473 break;
1474 }
1475 }
1476
1477done:
1478 // function finished, get result from the stack.
1479 tv = STACK_TV_BOT(-1);
1480 *rettv = *tv;
1481 tv->v_type = VAR_UNKNOWN;
1482 ret = OK;
1483
1484failed:
1485 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
1486 clear_tv(STACK_TV(idx));
1487 vim_free(ectx.ec_stack.ga_data);
1488 return ret;
1489}
1490
1491#define DISASSEMBLE 1
1492
1493/*
1494 * ":dissassemble".
1495 */
1496 void
1497ex_disassemble(exarg_T *eap)
1498{
1499#ifdef DISASSEMBLE
1500 ufunc_T *ufunc = find_func(eap->arg, NULL);
1501 dfunc_T *dfunc;
1502 isn_T *instr;
1503 int current;
1504 int line_idx = 0;
1505 int prev_current = 0;
1506
1507 if (ufunc == NULL)
1508 {
1509 semsg("Cannot find function %s", eap->arg);
1510 return;
1511 }
1512 if (ufunc->uf_dfunc_idx < 0)
1513 {
1514 semsg("Function %s is not compiled", eap->arg);
1515 return;
1516 }
1517 if (ufunc->uf_name_exp != NULL)
1518 msg((char *)ufunc->uf_name_exp);
1519 else
1520 msg((char *)ufunc->uf_name);
1521
1522 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
1523 instr = dfunc->df_instr;
1524 for (current = 0; current < dfunc->df_instr_count; ++current)
1525 {
1526 isn_T *iptr = &instr[current];
1527
1528 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
1529 {
1530 if (current > prev_current)
1531 {
1532 msg_puts("\n\n");
1533 prev_current = current;
1534 }
1535 msg(((char **)ufunc->uf_lines.ga_data)[line_idx++]);
1536 }
1537
1538 switch (iptr->isn_type)
1539 {
1540 case ISN_EXEC:
1541 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
1542 break;
1543 case ISN_ECHO:
1544 {
1545 echo_T *echo = &iptr->isn_arg.echo;
1546
1547 smsg("%4d %s %d", current,
1548 echo->echo_with_white ? "ECHO" : "ECHON",
1549 echo->echo_count);
1550 }
1551 break;
1552 case ISN_LOAD:
1553 if (iptr->isn_arg.number < 0)
1554 smsg("%4d LOAD arg[%lld]", current,
1555 iptr->isn_arg.number + STACK_FRAME_SIZE);
1556 else
1557 smsg("%4d LOAD $%lld", current, iptr->isn_arg.number);
1558 break;
1559 case ISN_LOADV:
1560 smsg("%4d LOADV v:%s", current,
1561 get_vim_var_name(iptr->isn_arg.number));
1562 break;
1563 case ISN_LOADSCRIPT:
1564 {
1565 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001566 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1568 + iptr->isn_arg.script.script_idx;
1569
1570 smsg("%4d LOADSCRIPT %s from %s", current,
1571 sv->sv_name, si->sn_name);
1572 }
1573 break;
1574 case ISN_LOADS:
1575 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001576 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.loads.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577
1578 smsg("%4d LOADS s:%s from %s", current,
1579 iptr->isn_arg.string, si->sn_name);
1580 }
1581 break;
1582 case ISN_LOADG:
1583 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
1584 break;
1585 case ISN_LOADOPT:
1586 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
1587 break;
1588 case ISN_LOADENV:
1589 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
1590 break;
1591 case ISN_LOADREG:
1592 smsg("%4d LOADREG @%c", current, iptr->isn_arg.number);
1593 break;
1594
1595 case ISN_STORE:
1596 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
1597 break;
1598 case ISN_STOREG:
1599 smsg("%4d STOREG g:%s", current, iptr->isn_arg.string);
1600 break;
1601 case ISN_STORESCRIPT:
1602 {
1603 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001604 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1606 + iptr->isn_arg.script.script_idx;
1607
1608 smsg("%4d STORESCRIPT %s in %s", current,
1609 sv->sv_name, si->sn_name);
1610 }
1611 break;
1612 case ISN_STOREOPT:
1613 smsg("%4d STOREOPT &%s", current,
1614 iptr->isn_arg.storeopt.so_name);
1615 break;
1616
1617 case ISN_STORENR:
1618 smsg("%4d STORE %lld in $%d", current,
1619 iptr->isn_arg.storenr.str_val,
1620 iptr->isn_arg.storenr.str_idx);
1621 break;
1622
1623 // constants
1624 case ISN_PUSHNR:
1625 smsg("%4d PUSHNR %lld", current, iptr->isn_arg.number);
1626 break;
1627 case ISN_PUSHBOOL:
1628 case ISN_PUSHSPEC:
1629 smsg("%4d PUSH %s", current,
1630 get_var_special_name(iptr->isn_arg.number));
1631 break;
1632 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001633#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01001635#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636 break;
1637 case ISN_PUSHS:
1638 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
1639 break;
1640 case ISN_PUSHBLOB:
1641 {
1642 char_u *r;
1643 char_u numbuf[NUMBUFLEN];
1644 char_u *tofree;
1645
1646 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
1647 smsg("%4d PUSHBLOB \"%s\"", current, r);
1648 vim_free(tofree);
1649 }
1650 break;
1651 case ISN_PUSHEXC:
1652 smsg("%4d PUSH v:exception", current);
1653 break;
1654 case ISN_NEWLIST:
1655 smsg("%4d NEWLIST size %lld", current, iptr->isn_arg.number);
1656 break;
1657 case ISN_NEWDICT:
1658 smsg("%4d NEWDICT size %lld", current, iptr->isn_arg.number);
1659 break;
1660
1661 // function call
1662 case ISN_BCALL:
1663 {
1664 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
1665
1666 smsg("%4d BCALL %s(argc %d)", current,
1667 internal_func_name(cbfunc->cbf_idx),
1668 cbfunc->cbf_argcount);
1669 }
1670 break;
1671 case ISN_DCALL:
1672 {
1673 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
1674 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
1675 + cdfunc->cdf_idx;
1676
1677 smsg("%4d DCALL %s(argc %d)", current,
1678 df->df_ufunc->uf_name_exp != NULL
1679 ? df->df_ufunc->uf_name_exp
1680 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
1681 }
1682 break;
1683 case ISN_UCALL:
1684 {
1685 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1686
1687 smsg("%4d UCALL %s(argc %d)", current,
1688 cufunc->cuf_name, cufunc->cuf_argcount);
1689 }
1690 break;
1691 case ISN_PCALL:
1692 {
1693 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
1694
1695 smsg("%4d PCALL%s (argc %d)", current,
1696 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
1697 }
1698 break;
1699 case ISN_RETURN:
1700 smsg("%4d RETURN", current);
1701 break;
1702 case ISN_FUNCREF:
1703 {
1704 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
1705 + iptr->isn_arg.number;
1706
1707 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
1708 }
1709 break;
1710
1711 case ISN_JUMP:
1712 {
1713 char *when = "?";
1714
1715 switch (iptr->isn_arg.jump.jump_when)
1716 {
1717 case JUMP_ALWAYS:
1718 when = "JUMP";
1719 break;
1720 case JUMP_IF_TRUE:
1721 when = "JUMP_IF_TRUE";
1722 break;
1723 case JUMP_AND_KEEP_IF_TRUE:
1724 when = "JUMP_AND_KEEP_IF_TRUE";
1725 break;
1726 case JUMP_IF_FALSE:
1727 when = "JUMP_IF_FALSE";
1728 break;
1729 case JUMP_AND_KEEP_IF_FALSE:
1730 when = "JUMP_AND_KEEP_IF_FALSE";
1731 break;
1732 }
1733 smsg("%4d %s -> %lld", current, when,
1734 iptr->isn_arg.jump.jump_where);
1735 }
1736 break;
1737
1738 case ISN_FOR:
1739 {
1740 forloop_T *forloop = &iptr->isn_arg.forloop;
1741
1742 smsg("%4d FOR $%d -> %d", current,
1743 forloop->for_idx, forloop->for_end);
1744 }
1745 break;
1746
1747 case ISN_TRY:
1748 {
1749 try_T *try = &iptr->isn_arg.try;
1750
1751 smsg("%4d TRY catch -> %d, finally -> %d", current,
1752 try->try_catch, try->try_finally);
1753 }
1754 break;
1755 case ISN_CATCH:
1756 // TODO
1757 smsg("%4d CATCH", current);
1758 break;
1759 case ISN_ENDTRY:
1760 smsg("%4d ENDTRY", current);
1761 break;
1762 case ISN_THROW:
1763 smsg("%4d THROW", current);
1764 break;
1765
1766 // expression operations on number
1767 case ISN_OPNR:
1768 case ISN_OPFLOAT:
1769 case ISN_OPANY:
1770 {
1771 char *what;
1772 char *ins;
1773
1774 switch (iptr->isn_arg.op.op_type)
1775 {
1776 case EXPR_MULT: what = "*"; break;
1777 case EXPR_DIV: what = "/"; break;
1778 case EXPR_REM: what = "%"; break;
1779 case EXPR_SUB: what = "-"; break;
1780 case EXPR_ADD: what = "+"; break;
1781 default: what = "???"; break;
1782 }
1783 switch (iptr->isn_type)
1784 {
1785 case ISN_OPNR: ins = "OPNR"; break;
1786 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
1787 case ISN_OPANY: ins = "OPANY"; break;
1788 default: ins = "???"; break;
1789 }
1790 smsg("%4d %s %s", current, ins, what);
1791 }
1792 break;
1793
1794 case ISN_COMPAREBOOL:
1795 case ISN_COMPARESPECIAL:
1796 case ISN_COMPARENR:
1797 case ISN_COMPAREFLOAT:
1798 case ISN_COMPARESTRING:
1799 case ISN_COMPAREBLOB:
1800 case ISN_COMPARELIST:
1801 case ISN_COMPAREDICT:
1802 case ISN_COMPAREFUNC:
1803 case ISN_COMPAREPARTIAL:
1804 case ISN_COMPAREANY:
1805 {
1806 char *p;
1807 char buf[10];
1808 char *type;
1809
1810 switch (iptr->isn_arg.op.op_type)
1811 {
1812 case EXPR_EQUAL: p = "=="; break;
1813 case EXPR_NEQUAL: p = "!="; break;
1814 case EXPR_GREATER: p = ">"; break;
1815 case EXPR_GEQUAL: p = ">="; break;
1816 case EXPR_SMALLER: p = "<"; break;
1817 case EXPR_SEQUAL: p = "<="; break;
1818 case EXPR_MATCH: p = "=~"; break;
1819 case EXPR_IS: p = "is"; break;
1820 case EXPR_ISNOT: p = "isnot"; break;
1821 case EXPR_NOMATCH: p = "!~"; break;
1822 default: p = "???"; break;
1823 }
1824 STRCPY(buf, p);
1825 if (iptr->isn_arg.op.op_ic == TRUE)
1826 strcat(buf, "?");
1827 switch(iptr->isn_type)
1828 {
1829 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
1830 case ISN_COMPARESPECIAL:
1831 type = "COMPARESPECIAL"; break;
1832 case ISN_COMPARENR: type = "COMPARENR"; break;
1833 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
1834 case ISN_COMPARESTRING:
1835 type = "COMPARESTRING"; break;
1836 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
1837 case ISN_COMPARELIST: type = "COMPARELIST"; break;
1838 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
1839 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
1840 case ISN_COMPAREPARTIAL:
1841 type = "COMPAREPARTIAL"; break;
1842 case ISN_COMPAREANY: type = "COMPAREANY"; break;
1843 default: type = "???"; break;
1844 }
1845
1846 smsg("%4d %s %s", current, type, buf);
1847 }
1848 break;
1849
1850 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
1851 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
1852
1853 // expression operations
1854 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
1855 case ISN_INDEX: smsg("%4d INDEX", current); break;
1856 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
1857 iptr->isn_arg.string); break;
1858 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
1859
1860 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
1861 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
1862 vartype_name(iptr->isn_arg.type.ct_type),
1863 iptr->isn_arg.type.ct_off);
1864 break;
1865 case ISN_2BOOL: if (iptr->isn_arg.number)
1866 smsg("%4d INVERT (!val)", current);
1867 else
1868 smsg("%4d 2BOOL (!!val)", current);
1869 break;
1870 case ISN_2STRING: smsg("%4d 2STRING stack[%d]", current,
1871 iptr->isn_arg.number);
1872 break;
1873
1874 case ISN_DROP: smsg("%4d DROP", current); break;
1875 }
1876 }
1877#endif
1878}
1879
1880/*
1881 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
1882 * list, etc. Mostly like what JavaScript does, except that empty list and
1883 * empty dictionary are FALSE.
1884 */
1885 int
1886tv2bool(typval_T *tv)
1887{
1888 switch (tv->v_type)
1889 {
1890 case VAR_NUMBER:
1891 return tv->vval.v_number != 0;
1892 case VAR_FLOAT:
1893#ifdef FEAT_FLOAT
1894 return tv->vval.v_float != 0.0;
1895#else
1896 break;
1897#endif
1898 case VAR_PARTIAL:
1899 return tv->vval.v_partial != NULL;
1900 case VAR_FUNC:
1901 case VAR_STRING:
1902 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
1903 case VAR_LIST:
1904 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
1905 case VAR_DICT:
1906 return tv->vval.v_dict != NULL
1907 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
1908 case VAR_BOOL:
1909 case VAR_SPECIAL:
1910 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
1911 case VAR_JOB:
1912#ifdef FEAT_JOB_CHANNEL
1913 return tv->vval.v_job != NULL;
1914#else
1915 break;
1916#endif
1917 case VAR_CHANNEL:
1918#ifdef FEAT_JOB_CHANNEL
1919 return tv->vval.v_channel != NULL;
1920#else
1921 break;
1922#endif
1923 case VAR_BLOB:
1924 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
1925 case VAR_UNKNOWN:
1926 case VAR_VOID:
1927 break;
1928 }
1929 return FALSE;
1930}
1931
1932/*
1933 * If "tv" is a string give an error and return FAIL.
1934 */
1935 int
1936check_not_string(typval_T *tv)
1937{
1938 if (tv->v_type == VAR_STRING)
1939 {
1940 emsg(_("E1030: Using a String as a Number"));
1941 clear_tv(tv);
1942 return FAIL;
1943 }
1944 return OK;
1945}
1946
1947
1948#endif // FEAT_EVAL