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