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