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, 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 do_cmdline_cmd(iptr->isn_arg.string); 649 break; 650 651 // execute :echo {string} ... 652 case ISN_ECHO: 653 { 654 int count = iptr->isn_arg.echo.echo_count; 655 int atstart = TRUE; 656 int needclr = TRUE; 657 658 for (idx = 0; idx < count; ++idx) 659 { 660 tv = STACK_TV_BOT(idx - count); 661 echo_one(tv, iptr->isn_arg.echo.echo_with_white, 662 &atstart, &needclr); 663 clear_tv(tv); 664 } 665 if (needclr) 666 msg_clr_eos(); 667 ectx.ec_stack.ga_len -= count; 668 } 669 break; 670 671 // :execute {string} ... 672 // :echomsg {string} ... 673 // :echoerr {string} ... 674 case ISN_EXECUTE: 675 case ISN_ECHOMSG: 676 case ISN_ECHOERR: 677 { 678 int count = iptr->isn_arg.number; 679 garray_T ga; 680 char_u buf[NUMBUFLEN]; 681 char_u *p; 682 int len; 683 int failed = FALSE; 684 685 ga_init2(&ga, 1, 80); 686 for (idx = 0; idx < count; ++idx) 687 { 688 tv = STACK_TV_BOT(idx - count); 689 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB) 690 { 691 emsg(_(e_inval_string)); 692 break; 693 } 694 else 695 p = tv_get_string_buf(tv, buf); 696 697 len = (int)STRLEN(p); 698 if (ga_grow(&ga, len + 2) == FAIL) 699 failed = TRUE; 700 else 701 { 702 if (ga.ga_len > 0) 703 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; 704 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); 705 ga.ga_len += len; 706 } 707 clear_tv(tv); 708 } 709 ectx.ec_stack.ga_len -= count; 710 711 if (!failed && ga.ga_data != NULL) 712 { 713 if (iptr->isn_type == ISN_EXECUTE) 714 do_cmdline_cmd((char_u *)ga.ga_data); 715 else 716 { 717 msg_sb_eol(); 718 if (iptr->isn_type == ISN_ECHOMSG) 719 { 720 msg_attr(ga.ga_data, echo_attr); 721 out_flush(); 722 } 723 else 724 { 725 int save_did_emsg = did_emsg; 726 727 SOURCING_LNUM = iptr->isn_lnum; 728 emsg(ga.ga_data); 729 if (!force_abort) 730 // We don't want to abort following 731 // commands, restore did_emsg. 732 did_emsg = save_did_emsg; 733 } 734 } 735 } 736 ga_clear(&ga); 737 } 738 break; 739 740 // load local variable or argument 741 case ISN_LOAD: 742 if (ga_grow(&ectx.ec_stack, 1) == FAIL) 743 goto failed; 744 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0)); 745 ++ectx.ec_stack.ga_len; 746 break; 747 748 // load v: variable 749 case ISN_LOADV: 750 if (ga_grow(&ectx.ec_stack, 1) == FAIL) 751 goto failed; 752 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0)); 753 ++ectx.ec_stack.ga_len; 754 break; 755 756 // load s: variable in Vim9 script 757 case ISN_LOADSCRIPT: 758 { 759 scriptitem_T *si = 760 SCRIPT_ITEM(iptr->isn_arg.script.script_sid); 761 svar_T *sv; 762 763 sv = ((svar_T *)si->sn_var_vals.ga_data) 764 + iptr->isn_arg.script.script_idx; 765 if (ga_grow(&ectx.ec_stack, 1) == FAIL) 766 goto failed; 767 copy_tv(sv->sv_tv, STACK_TV_BOT(0)); 768 ++ectx.ec_stack.ga_len; 769 } 770 break; 771 772 // load s: variable in old script 773 case ISN_LOADS: 774 { 775 hashtab_T *ht = &SCRIPT_VARS( 776 iptr->isn_arg.loadstore.ls_sid); 777 char_u *name = iptr->isn_arg.loadstore.ls_name; 778 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE); 779 780 if (di == NULL) 781 { 782 semsg(_(e_undefvar), name); 783 goto failed; 784 } 785 else 786 { 787 if (ga_grow(&ectx.ec_stack, 1) == FAIL) 788 goto failed; 789 copy_tv(&di->di_tv, STACK_TV_BOT(0)); 790 ++ectx.ec_stack.ga_len; 791 } 792 } 793 break; 794 795 // load g:/b:/w:/t: variable 796 case ISN_LOADG: 797 case ISN_LOADB: 798 case ISN_LOADW: 799 case ISN_LOADT: 800 { 801 dictitem_T *di = NULL; 802 hashtab_T *ht = NULL; 803 char namespace; 804 switch (iptr->isn_type) 805 { 806 case ISN_LOADG: 807 ht = get_globvar_ht(); 808 namespace = 'g'; 809 break; 810 case ISN_LOADB: 811 ht = &curbuf->b_vars->dv_hashtab; 812 namespace = 'b'; 813 break; 814 case ISN_LOADW: 815 ht = &curwin->w_vars->dv_hashtab; 816 namespace = 'w'; 817 break; 818 case ISN_LOADT: 819 ht = &curtab->tp_vars->dv_hashtab; 820 namespace = 't'; 821 break; 822 default: // Cannot reach here 823 goto failed; 824 } 825 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE); 826 827 if (di == NULL) 828 { 829 semsg(_("E121: Undefined variable: %c:%s"), 830 namespace, iptr->isn_arg.string); 831 goto failed; 832 } 833 else 834 { 835 if (ga_grow(&ectx.ec_stack, 1) == FAIL) 836 goto failed; 837 copy_tv(&di->di_tv, STACK_TV_BOT(0)); 838 ++ectx.ec_stack.ga_len; 839 } 840 } 841 break; 842 843 // load &option 844 case ISN_LOADOPT: 845 { 846 typval_T optval; 847 char_u *name = iptr->isn_arg.string; 848 849 // This is not expected to fail, name is checked during 850 // compilation: don't set SOURCING_LNUM. 851 if (ga_grow(&ectx.ec_stack, 1) == FAIL) 852 goto failed; 853 if (get_option_tv(&name, &optval, TRUE) == FAIL) 854 goto failed; 855 *STACK_TV_BOT(0) = optval; 856 ++ectx.ec_stack.ga_len; 857 } 858 break; 859 860 // load $ENV 861 case ISN_LOADENV: 862 { 863 typval_T optval; 864 char_u *name = iptr->isn_arg.string; 865 866 if (ga_grow(&ectx.ec_stack, 1) == FAIL) 867 goto failed; 868 // name is always valid, checked when compiling 869 (void)get_env_tv(&name, &optval, TRUE); 870 *STACK_TV_BOT(0) = optval; 871 ++ectx.ec_stack.ga_len; 872 } 873 break; 874 875 // load @register 876 case ISN_LOADREG: 877 if (ga_grow(&ectx.ec_stack, 1) == FAIL) 878 goto failed; 879 tv = STACK_TV_BOT(0); 880 tv->v_type = VAR_STRING; 881 tv->vval.v_string = get_reg_contents( 882 iptr->isn_arg.number, GREG_EXPR_SRC); 883 ++ectx.ec_stack.ga_len; 884 break; 885 886 // store local variable 887 case ISN_STORE: 888 --ectx.ec_stack.ga_len; 889 tv = STACK_TV_VAR(iptr->isn_arg.number); 890 clear_tv(tv); 891 *tv = *STACK_TV_BOT(0); 892 break; 893 894 // store s: variable in old script 895 case ISN_STORES: 896 { 897 hashtab_T *ht = &SCRIPT_VARS( 898 iptr->isn_arg.loadstore.ls_sid); 899 char_u *name = iptr->isn_arg.loadstore.ls_name; 900 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE); 901 902 --ectx.ec_stack.ga_len; 903 if (di == NULL) 904 store_var(name, STACK_TV_BOT(0)); 905 else 906 { 907 clear_tv(&di->di_tv); 908 di->di_tv = *STACK_TV_BOT(0); 909 } 910 } 911 break; 912 913 // store script-local variable in Vim9 script 914 case ISN_STORESCRIPT: 915 { 916 scriptitem_T *si = SCRIPT_ITEM( 917 iptr->isn_arg.script.script_sid); 918 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 919 + iptr->isn_arg.script.script_idx; 920 921 --ectx.ec_stack.ga_len; 922 clear_tv(sv->sv_tv); 923 *sv->sv_tv = *STACK_TV_BOT(0); 924 } 925 break; 926 927 // store option 928 case ISN_STOREOPT: 929 { 930 long n = 0; 931 char_u *s = NULL; 932 char *msg; 933 934 --ectx.ec_stack.ga_len; 935 tv = STACK_TV_BOT(0); 936 if (tv->v_type == VAR_STRING) 937 { 938 s = tv->vval.v_string; 939 if (s == NULL) 940 s = (char_u *)""; 941 } 942 else if (tv->v_type == VAR_NUMBER) 943 n = tv->vval.v_number; 944 else 945 { 946 emsg(_("E1051: Expected string or number")); 947 goto failed; 948 } 949 msg = set_option_value(iptr->isn_arg.storeopt.so_name, 950 n, s, iptr->isn_arg.storeopt.so_flags); 951 if (msg != NULL) 952 { 953 emsg(_(msg)); 954 goto failed; 955 } 956 clear_tv(tv); 957 } 958 break; 959 960 // store $ENV 961 case ISN_STOREENV: 962 --ectx.ec_stack.ga_len; 963 tv = STACK_TV_BOT(0); 964 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv)); 965 clear_tv(tv); 966 break; 967 968 // store @r 969 case ISN_STOREREG: 970 { 971 int reg = iptr->isn_arg.number; 972 973 --ectx.ec_stack.ga_len; 974 tv = STACK_TV_BOT(0); 975 write_reg_contents(reg == '@' ? '"' : reg, 976 tv_get_string(tv), -1, FALSE); 977 clear_tv(tv); 978 } 979 break; 980 981 // store v: variable 982 case ISN_STOREV: 983 --ectx.ec_stack.ga_len; 984 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0)) 985 == FAIL) 986 goto failed; 987 break; 988 989 // store g:/b:/w:/t: variable 990 case ISN_STOREG: 991 case ISN_STOREB: 992 case ISN_STOREW: 993 case ISN_STORET: 994 { 995 dictitem_T *di; 996 hashtab_T *ht; 997 switch (iptr->isn_type) 998 { 999 case ISN_STOREG: 1000 ht = get_globvar_ht(); 1001 break; 1002 case ISN_STOREB: 1003 ht = &curbuf->b_vars->dv_hashtab; 1004 break; 1005 case ISN_STOREW: 1006 ht = &curwin->w_vars->dv_hashtab; 1007 break; 1008 case ISN_STORET: 1009 ht = &curtab->tp_vars->dv_hashtab; 1010 break; 1011 default: // Cannot reach here 1012 goto failed; 1013 } 1014 1015 --ectx.ec_stack.ga_len; 1016 di = find_var_in_ht(ht, 0, 1017 iptr->isn_arg.string + 2, TRUE); 1018 if (di == NULL) 1019 store_var(iptr->isn_arg.string, STACK_TV_BOT(0)); 1020 else 1021 { 1022 clear_tv(&di->di_tv); 1023 di->di_tv = *STACK_TV_BOT(0); 1024 } 1025 } 1026 break; 1027 1028 // store number in local variable 1029 case ISN_STORENR: 1030 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx); 1031 clear_tv(tv); 1032 tv->v_type = VAR_NUMBER; 1033 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val; 1034 break; 1035 1036 // push constant 1037 case ISN_PUSHNR: 1038 case ISN_PUSHBOOL: 1039 case ISN_PUSHSPEC: 1040 case ISN_PUSHF: 1041 case ISN_PUSHS: 1042 case ISN_PUSHBLOB: 1043 case ISN_PUSHFUNC: 1044 case ISN_PUSHCHANNEL: 1045 case ISN_PUSHJOB: 1046 if (ga_grow(&ectx.ec_stack, 1) == FAIL) 1047 goto failed; 1048 tv = STACK_TV_BOT(0); 1049 ++ectx.ec_stack.ga_len; 1050 switch (iptr->isn_type) 1051 { 1052 case ISN_PUSHNR: 1053 tv->v_type = VAR_NUMBER; 1054 tv->vval.v_number = iptr->isn_arg.number; 1055 break; 1056 case ISN_PUSHBOOL: 1057 tv->v_type = VAR_BOOL; 1058 tv->vval.v_number = iptr->isn_arg.number; 1059 break; 1060 case ISN_PUSHSPEC: 1061 tv->v_type = VAR_SPECIAL; 1062 tv->vval.v_number = iptr->isn_arg.number; 1063 break; 1064 #ifdef FEAT_FLOAT 1065 case ISN_PUSHF: 1066 tv->v_type = VAR_FLOAT; 1067 tv->vval.v_float = iptr->isn_arg.fnumber; 1068 break; 1069 #endif 1070 case ISN_PUSHBLOB: 1071 blob_copy(iptr->isn_arg.blob, tv); 1072 break; 1073 case ISN_PUSHFUNC: 1074 tv->v_type = VAR_FUNC; 1075 if (iptr->isn_arg.string == NULL) 1076 tv->vval.v_string = NULL; 1077 else 1078 tv->vval.v_string = 1079 vim_strsave(iptr->isn_arg.string); 1080 break; 1081 case ISN_PUSHCHANNEL: 1082 #ifdef FEAT_JOB_CHANNEL 1083 tv->v_type = VAR_CHANNEL; 1084 tv->vval.v_channel = iptr->isn_arg.channel; 1085 if (tv->vval.v_channel != NULL) 1086 ++tv->vval.v_channel->ch_refcount; 1087 #endif 1088 break; 1089 case ISN_PUSHJOB: 1090 #ifdef FEAT_JOB_CHANNEL 1091 tv->v_type = VAR_JOB; 1092 tv->vval.v_job = iptr->isn_arg.job; 1093 if (tv->vval.v_job != NULL) 1094 ++tv->vval.v_job->jv_refcount; 1095 #endif 1096 break; 1097 default: 1098 tv->v_type = VAR_STRING; 1099 tv->vval.v_string = vim_strsave( 1100 iptr->isn_arg.string == NULL 1101 ? (char_u *)"" : iptr->isn_arg.string); 1102 } 1103 break; 1104 1105 case ISN_UNLET: 1106 if (do_unlet(iptr->isn_arg.unlet.ul_name, 1107 iptr->isn_arg.unlet.ul_forceit) == FAIL) 1108 goto failed; 1109 break; 1110 case ISN_UNLETENV: 1111 vim_unsetenv(iptr->isn_arg.unlet.ul_name); 1112 break; 1113 1114 // create a list from items on the stack; uses a single allocation 1115 // for the list header and the items 1116 case ISN_NEWLIST: 1117 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL) 1118 goto failed; 1119 break; 1120 1121 // create a dict from items on the stack 1122 case ISN_NEWDICT: 1123 { 1124 int count = iptr->isn_arg.number; 1125 dict_T *dict = dict_alloc(); 1126 dictitem_T *item; 1127 1128 if (dict == NULL) 1129 goto failed; 1130 for (idx = 0; idx < count; ++idx) 1131 { 1132 // check key type is VAR_STRING 1133 tv = STACK_TV_BOT(2 * (idx - count)); 1134 item = dictitem_alloc(tv->vval.v_string); 1135 clear_tv(tv); 1136 if (item == NULL) 1137 goto failed; 1138 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1); 1139 item->di_tv.v_lock = 0; 1140 if (dict_add(dict, item) == FAIL) 1141 goto failed; 1142 } 1143 1144 if (count > 0) 1145 ectx.ec_stack.ga_len -= 2 * count - 1; 1146 else if (ga_grow(&ectx.ec_stack, 1) == FAIL) 1147 goto failed; 1148 else 1149 ++ectx.ec_stack.ga_len; 1150 tv = STACK_TV_BOT(-1); 1151 tv->v_type = VAR_DICT; 1152 tv->vval.v_dict = dict; 1153 ++dict->dv_refcount; 1154 } 1155 break; 1156 1157 // call a :def function 1158 case ISN_DCALL: 1159 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, 1160 iptr->isn_arg.dfunc.cdf_argcount, 1161 &ectx) == FAIL) 1162 goto failed; 1163 break; 1164 1165 // call a builtin function 1166 case ISN_BCALL: 1167 SOURCING_LNUM = iptr->isn_lnum; 1168 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx, 1169 iptr->isn_arg.bfunc.cbf_argcount, 1170 &ectx) == FAIL) 1171 goto failed; 1172 break; 1173 1174 // call a funcref or partial 1175 case ISN_PCALL: 1176 { 1177 cpfunc_T *pfunc = &iptr->isn_arg.pfunc; 1178 int r; 1179 typval_T partial; 1180 1181 SOURCING_LNUM = iptr->isn_lnum; 1182 if (pfunc->cpf_top) 1183 { 1184 // funcref is above the arguments 1185 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1); 1186 } 1187 else 1188 { 1189 // Get the funcref from the stack. 1190 --ectx.ec_stack.ga_len; 1191 partial = *STACK_TV_BOT(0); 1192 tv = &partial; 1193 } 1194 r = call_partial(tv, pfunc->cpf_argcount, &ectx); 1195 if (tv == &partial) 1196 clear_tv(&partial); 1197 if (r == FAIL) 1198 goto failed; 1199 } 1200 break; 1201 1202 case ISN_PCALL_END: 1203 // PCALL finished, arguments have been consumed and replaced by 1204 // the return value. Now clear the funcref from the stack, 1205 // and move the return value in its place. 1206 --ectx.ec_stack.ga_len; 1207 clear_tv(STACK_TV_BOT(-1)); 1208 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0); 1209 break; 1210 1211 // call a user defined function or funcref/partial 1212 case ISN_UCALL: 1213 { 1214 cufunc_T *cufunc = &iptr->isn_arg.ufunc; 1215 1216 SOURCING_LNUM = iptr->isn_lnum; 1217 if (call_eval_func(cufunc->cuf_name, 1218 cufunc->cuf_argcount, &ectx, iptr) == FAIL) 1219 goto failed; 1220 } 1221 break; 1222 1223 // return from a :def function call 1224 case ISN_RETURN: 1225 { 1226 garray_T *trystack = &ectx.ec_trystack; 1227 trycmd_T *trycmd = NULL; 1228 1229 if (trystack->ga_len > 0) 1230 trycmd = ((trycmd_T *)trystack->ga_data) 1231 + trystack->ga_len - 1; 1232 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame 1233 && trycmd->tcd_finally_idx != 0) 1234 { 1235 // jump to ":finally" 1236 ectx.ec_iidx = trycmd->tcd_finally_idx; 1237 trycmd->tcd_return = TRUE; 1238 } 1239 else 1240 { 1241 // Restore previous function. If the frame pointer 1242 // is zero then there is none and we are done. 1243 if (ectx.ec_frame == initial_frame_ptr) 1244 goto done; 1245 1246 func_return(&ectx); 1247 } 1248 } 1249 break; 1250 1251 // push a function reference to a compiled function 1252 case ISN_FUNCREF: 1253 { 1254 partial_T *pt = NULL; 1255 dfunc_T *dfunc; 1256 1257 pt = ALLOC_CLEAR_ONE(partial_T); 1258 if (pt == NULL) 1259 goto failed; 1260 dfunc = ((dfunc_T *)def_functions.ga_data) 1261 + iptr->isn_arg.number; 1262 pt->pt_func = dfunc->df_ufunc; 1263 pt->pt_refcount = 1; 1264 ++dfunc->df_ufunc->uf_refcount; 1265 1266 if (ga_grow(&ectx.ec_stack, 1) == FAIL) 1267 goto failed; 1268 tv = STACK_TV_BOT(0); 1269 ++ectx.ec_stack.ga_len; 1270 tv->vval.v_partial = pt; 1271 tv->v_type = VAR_PARTIAL; 1272 } 1273 break; 1274 1275 // jump if a condition is met 1276 case ISN_JUMP: 1277 { 1278 jumpwhen_T when = iptr->isn_arg.jump.jump_when; 1279 int jump = TRUE; 1280 1281 if (when != JUMP_ALWAYS) 1282 { 1283 tv = STACK_TV_BOT(-1); 1284 jump = tv2bool(tv); 1285 if (when == JUMP_IF_FALSE 1286 || when == JUMP_AND_KEEP_IF_FALSE) 1287 jump = !jump; 1288 if (when == JUMP_IF_FALSE || !jump) 1289 { 1290 // drop the value from the stack 1291 clear_tv(tv); 1292 --ectx.ec_stack.ga_len; 1293 } 1294 } 1295 if (jump) 1296 ectx.ec_iidx = iptr->isn_arg.jump.jump_where; 1297 } 1298 break; 1299 1300 // top of a for loop 1301 case ISN_FOR: 1302 { 1303 list_T *list = STACK_TV_BOT(-1)->vval.v_list; 1304 typval_T *idxtv = 1305 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx); 1306 1307 // push the next item from the list 1308 if (ga_grow(&ectx.ec_stack, 1) == FAIL) 1309 goto failed; 1310 if (++idxtv->vval.v_number >= list->lv_len) 1311 // past the end of the list, jump to "endfor" 1312 ectx.ec_iidx = iptr->isn_arg.forloop.for_end; 1313 else if (list->lv_first == &range_list_item) 1314 { 1315 // non-materialized range() list 1316 tv = STACK_TV_BOT(0); 1317 tv->v_type = VAR_NUMBER; 1318 tv->vval.v_number = list_find_nr( 1319 list, idxtv->vval.v_number, NULL); 1320 ++ectx.ec_stack.ga_len; 1321 } 1322 else 1323 { 1324 listitem_T *li = list_find(list, idxtv->vval.v_number); 1325 1326 if (li == NULL) 1327 goto failed; 1328 copy_tv(&li->li_tv, STACK_TV_BOT(0)); 1329 ++ectx.ec_stack.ga_len; 1330 } 1331 } 1332 break; 1333 1334 // start of ":try" block 1335 case ISN_TRY: 1336 { 1337 trycmd_T *trycmd = NULL; 1338 1339 if (ga_grow(&ectx.ec_trystack, 1) == FAIL) 1340 goto failed; 1341 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data) 1342 + ectx.ec_trystack.ga_len; 1343 ++ectx.ec_trystack.ga_len; 1344 ++trylevel; 1345 trycmd->tcd_frame = ectx.ec_frame; 1346 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch; 1347 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally; 1348 trycmd->tcd_caught = FALSE; 1349 } 1350 break; 1351 1352 case ISN_PUSHEXC: 1353 if (current_exception == NULL) 1354 { 1355 iemsg("Evaluating catch while current_exception is NULL"); 1356 goto failed; 1357 } 1358 if (ga_grow(&ectx.ec_stack, 1) == FAIL) 1359 goto failed; 1360 tv = STACK_TV_BOT(0); 1361 ++ectx.ec_stack.ga_len; 1362 tv->v_type = VAR_STRING; 1363 tv->vval.v_string = vim_strsave( 1364 (char_u *)current_exception->value); 1365 break; 1366 1367 case ISN_CATCH: 1368 { 1369 garray_T *trystack = &ectx.ec_trystack; 1370 1371 if (trystack->ga_len > 0) 1372 { 1373 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) 1374 + trystack->ga_len - 1; 1375 trycmd->tcd_caught = TRUE; 1376 } 1377 did_emsg = got_int = did_throw = FALSE; 1378 catch_exception(current_exception); 1379 } 1380 break; 1381 1382 // end of ":try" block 1383 case ISN_ENDTRY: 1384 { 1385 garray_T *trystack = &ectx.ec_trystack; 1386 1387 if (trystack->ga_len > 0) 1388 { 1389 trycmd_T *trycmd = NULL; 1390 1391 --trystack->ga_len; 1392 --trylevel; 1393 trycmd = ((trycmd_T *)trystack->ga_data) 1394 + trystack->ga_len; 1395 if (trycmd->tcd_caught && current_exception != NULL) 1396 { 1397 // discard the exception 1398 if (caught_stack == current_exception) 1399 caught_stack = caught_stack->caught; 1400 discard_current_exception(); 1401 } 1402 1403 if (trycmd->tcd_return) 1404 { 1405 // Restore previous function. If the frame pointer 1406 // is zero then there is none and we are done. 1407 if (ectx.ec_frame == initial_frame_ptr) 1408 goto done; 1409 1410 func_return(&ectx); 1411 } 1412 } 1413 } 1414 break; 1415 1416 case ISN_THROW: 1417 --ectx.ec_stack.ga_len; 1418 tv = STACK_TV_BOT(0); 1419 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL) 1420 { 1421 vim_free(tv->vval.v_string); 1422 goto failed; 1423 } 1424 did_throw = TRUE; 1425 break; 1426 1427 // compare with special values 1428 case ISN_COMPAREBOOL: 1429 case ISN_COMPARESPECIAL: 1430 { 1431 typval_T *tv1 = STACK_TV_BOT(-2); 1432 typval_T *tv2 = STACK_TV_BOT(-1); 1433 varnumber_T arg1 = tv1->vval.v_number; 1434 varnumber_T arg2 = tv2->vval.v_number; 1435 int res; 1436 1437 switch (iptr->isn_arg.op.op_type) 1438 { 1439 case EXPR_EQUAL: res = arg1 == arg2; break; 1440 case EXPR_NEQUAL: res = arg1 != arg2; break; 1441 default: res = 0; break; 1442 } 1443 1444 --ectx.ec_stack.ga_len; 1445 tv1->v_type = VAR_BOOL; 1446 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; 1447 } 1448 break; 1449 1450 // Operation with two number arguments 1451 case ISN_OPNR: 1452 case ISN_COMPARENR: 1453 { 1454 typval_T *tv1 = STACK_TV_BOT(-2); 1455 typval_T *tv2 = STACK_TV_BOT(-1); 1456 varnumber_T arg1 = tv1->vval.v_number; 1457 varnumber_T arg2 = tv2->vval.v_number; 1458 varnumber_T res; 1459 1460 switch (iptr->isn_arg.op.op_type) 1461 { 1462 case EXPR_MULT: res = arg1 * arg2; break; 1463 case EXPR_DIV: res = arg1 / arg2; break; 1464 case EXPR_REM: res = arg1 % arg2; break; 1465 case EXPR_SUB: res = arg1 - arg2; break; 1466 case EXPR_ADD: res = arg1 + arg2; break; 1467 1468 case EXPR_EQUAL: res = arg1 == arg2; break; 1469 case EXPR_NEQUAL: res = arg1 != arg2; break; 1470 case EXPR_GREATER: res = arg1 > arg2; break; 1471 case EXPR_GEQUAL: res = arg1 >= arg2; break; 1472 case EXPR_SMALLER: res = arg1 < arg2; break; 1473 case EXPR_SEQUAL: res = arg1 <= arg2; break; 1474 default: res = 0; break; 1475 } 1476 1477 --ectx.ec_stack.ga_len; 1478 if (iptr->isn_type == ISN_COMPARENR) 1479 { 1480 tv1->v_type = VAR_BOOL; 1481 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; 1482 } 1483 else 1484 tv1->vval.v_number = res; 1485 } 1486 break; 1487 1488 // Computation with two float arguments 1489 case ISN_OPFLOAT: 1490 case ISN_COMPAREFLOAT: 1491 #ifdef FEAT_FLOAT 1492 { 1493 typval_T *tv1 = STACK_TV_BOT(-2); 1494 typval_T *tv2 = STACK_TV_BOT(-1); 1495 float_T arg1 = tv1->vval.v_float; 1496 float_T arg2 = tv2->vval.v_float; 1497 float_T res = 0; 1498 int cmp = FALSE; 1499 1500 switch (iptr->isn_arg.op.op_type) 1501 { 1502 case EXPR_MULT: res = arg1 * arg2; break; 1503 case EXPR_DIV: res = arg1 / arg2; break; 1504 case EXPR_SUB: res = arg1 - arg2; break; 1505 case EXPR_ADD: res = arg1 + arg2; break; 1506 1507 case EXPR_EQUAL: cmp = arg1 == arg2; break; 1508 case EXPR_NEQUAL: cmp = arg1 != arg2; break; 1509 case EXPR_GREATER: cmp = arg1 > arg2; break; 1510 case EXPR_GEQUAL: cmp = arg1 >= arg2; break; 1511 case EXPR_SMALLER: cmp = arg1 < arg2; break; 1512 case EXPR_SEQUAL: cmp = arg1 <= arg2; break; 1513 default: cmp = 0; break; 1514 } 1515 --ectx.ec_stack.ga_len; 1516 if (iptr->isn_type == ISN_COMPAREFLOAT) 1517 { 1518 tv1->v_type = VAR_BOOL; 1519 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 1520 } 1521 else 1522 tv1->vval.v_float = res; 1523 } 1524 #endif 1525 break; 1526 1527 case ISN_COMPARELIST: 1528 { 1529 typval_T *tv1 = STACK_TV_BOT(-2); 1530 typval_T *tv2 = STACK_TV_BOT(-1); 1531 list_T *arg1 = tv1->vval.v_list; 1532 list_T *arg2 = tv2->vval.v_list; 1533 int cmp = FALSE; 1534 int ic = iptr->isn_arg.op.op_ic; 1535 1536 switch (iptr->isn_arg.op.op_type) 1537 { 1538 case EXPR_EQUAL: cmp = 1539 list_equal(arg1, arg2, ic, FALSE); break; 1540 case EXPR_NEQUAL: cmp = 1541 !list_equal(arg1, arg2, ic, FALSE); break; 1542 case EXPR_IS: cmp = arg1 == arg2; break; 1543 case EXPR_ISNOT: cmp = arg1 != arg2; break; 1544 default: cmp = 0; break; 1545 } 1546 --ectx.ec_stack.ga_len; 1547 clear_tv(tv1); 1548 clear_tv(tv2); 1549 tv1->v_type = VAR_BOOL; 1550 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 1551 } 1552 break; 1553 1554 case ISN_COMPAREBLOB: 1555 { 1556 typval_T *tv1 = STACK_TV_BOT(-2); 1557 typval_T *tv2 = STACK_TV_BOT(-1); 1558 blob_T *arg1 = tv1->vval.v_blob; 1559 blob_T *arg2 = tv2->vval.v_blob; 1560 int cmp = FALSE; 1561 1562 switch (iptr->isn_arg.op.op_type) 1563 { 1564 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break; 1565 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break; 1566 case EXPR_IS: cmp = arg1 == arg2; break; 1567 case EXPR_ISNOT: cmp = arg1 != arg2; break; 1568 default: cmp = 0; break; 1569 } 1570 --ectx.ec_stack.ga_len; 1571 clear_tv(tv1); 1572 clear_tv(tv2); 1573 tv1->v_type = VAR_BOOL; 1574 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 1575 } 1576 break; 1577 1578 // TODO: handle separately 1579 case ISN_COMPARESTRING: 1580 case ISN_COMPAREDICT: 1581 case ISN_COMPAREFUNC: 1582 case ISN_COMPAREANY: 1583 { 1584 typval_T *tv1 = STACK_TV_BOT(-2); 1585 typval_T *tv2 = STACK_TV_BOT(-1); 1586 exptype_T exptype = iptr->isn_arg.op.op_type; 1587 int ic = iptr->isn_arg.op.op_ic; 1588 1589 typval_compare(tv1, tv2, exptype, ic); 1590 clear_tv(tv2); 1591 tv1->v_type = VAR_BOOL; 1592 tv1->vval.v_number = tv1->vval.v_number 1593 ? VVAL_TRUE : VVAL_FALSE; 1594 --ectx.ec_stack.ga_len; 1595 } 1596 break; 1597 1598 case ISN_ADDLIST: 1599 case ISN_ADDBLOB: 1600 { 1601 typval_T *tv1 = STACK_TV_BOT(-2); 1602 typval_T *tv2 = STACK_TV_BOT(-1); 1603 1604 if (iptr->isn_type == ISN_ADDLIST) 1605 eval_addlist(tv1, tv2); 1606 else 1607 eval_addblob(tv1, tv2); 1608 clear_tv(tv2); 1609 --ectx.ec_stack.ga_len; 1610 } 1611 break; 1612 1613 // Computation with two arguments of unknown type 1614 case ISN_OPANY: 1615 { 1616 typval_T *tv1 = STACK_TV_BOT(-2); 1617 typval_T *tv2 = STACK_TV_BOT(-1); 1618 varnumber_T n1, n2; 1619 #ifdef FEAT_FLOAT 1620 float_T f1 = 0, f2 = 0; 1621 #endif 1622 int error = FALSE; 1623 1624 if (iptr->isn_arg.op.op_type == EXPR_ADD) 1625 { 1626 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST) 1627 { 1628 eval_addlist(tv1, tv2); 1629 clear_tv(tv2); 1630 --ectx.ec_stack.ga_len; 1631 break; 1632 } 1633 else if (tv1->v_type == VAR_BLOB 1634 && tv2->v_type == VAR_BLOB) 1635 { 1636 eval_addblob(tv1, tv2); 1637 clear_tv(tv2); 1638 --ectx.ec_stack.ga_len; 1639 break; 1640 } 1641 } 1642 #ifdef FEAT_FLOAT 1643 if (tv1->v_type == VAR_FLOAT) 1644 { 1645 f1 = tv1->vval.v_float; 1646 n1 = 0; 1647 } 1648 else 1649 #endif 1650 { 1651 n1 = tv_get_number_chk(tv1, &error); 1652 if (error) 1653 goto failed; 1654 #ifdef FEAT_FLOAT 1655 if (tv2->v_type == VAR_FLOAT) 1656 f1 = n1; 1657 #endif 1658 } 1659 #ifdef FEAT_FLOAT 1660 if (tv2->v_type == VAR_FLOAT) 1661 { 1662 f2 = tv2->vval.v_float; 1663 n2 = 0; 1664 } 1665 else 1666 #endif 1667 { 1668 n2 = tv_get_number_chk(tv2, &error); 1669 if (error) 1670 goto failed; 1671 #ifdef FEAT_FLOAT 1672 if (tv1->v_type == VAR_FLOAT) 1673 f2 = n2; 1674 #endif 1675 } 1676 #ifdef FEAT_FLOAT 1677 // if there is a float on either side the result is a float 1678 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT) 1679 { 1680 switch (iptr->isn_arg.op.op_type) 1681 { 1682 case EXPR_MULT: f1 = f1 * f2; break; 1683 case EXPR_DIV: f1 = f1 / f2; break; 1684 case EXPR_SUB: f1 = f1 - f2; break; 1685 case EXPR_ADD: f1 = f1 + f2; break; 1686 default: emsg(_(e_modulus)); goto failed; 1687 } 1688 clear_tv(tv1); 1689 clear_tv(tv2); 1690 tv1->v_type = VAR_FLOAT; 1691 tv1->vval.v_float = f1; 1692 --ectx.ec_stack.ga_len; 1693 } 1694 else 1695 #endif 1696 { 1697 switch (iptr->isn_arg.op.op_type) 1698 { 1699 case EXPR_MULT: n1 = n1 * n2; break; 1700 case EXPR_DIV: n1 = num_divide(n1, n2); break; 1701 case EXPR_SUB: n1 = n1 - n2; break; 1702 case EXPR_ADD: n1 = n1 + n2; break; 1703 default: n1 = num_modulus(n1, n2); break; 1704 } 1705 clear_tv(tv1); 1706 clear_tv(tv2); 1707 tv1->v_type = VAR_NUMBER; 1708 tv1->vval.v_number = n1; 1709 --ectx.ec_stack.ga_len; 1710 } 1711 } 1712 break; 1713 1714 case ISN_CONCAT: 1715 { 1716 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string; 1717 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string; 1718 char_u *res; 1719 1720 res = concat_str(str1, str2); 1721 clear_tv(STACK_TV_BOT(-2)); 1722 clear_tv(STACK_TV_BOT(-1)); 1723 --ectx.ec_stack.ga_len; 1724 STACK_TV_BOT(-1)->vval.v_string = res; 1725 } 1726 break; 1727 1728 case ISN_INDEX: 1729 { 1730 list_T *list; 1731 varnumber_T n; 1732 listitem_T *li; 1733 1734 // list index: list is at stack-2, index at stack-1 1735 tv = STACK_TV_BOT(-2); 1736 if (tv->v_type != VAR_LIST) 1737 { 1738 emsg(_(e_listreq)); 1739 goto failed; 1740 } 1741 list = tv->vval.v_list; 1742 1743 tv = STACK_TV_BOT(-1); 1744 if (tv->v_type != VAR_NUMBER) 1745 { 1746 emsg(_(e_number_exp)); 1747 goto failed; 1748 } 1749 n = tv->vval.v_number; 1750 clear_tv(tv); 1751 if ((li = list_find(list, n)) == NULL) 1752 { 1753 semsg(_(e_listidx), n); 1754 goto failed; 1755 } 1756 --ectx.ec_stack.ga_len; 1757 clear_tv(STACK_TV_BOT(-1)); 1758 copy_tv(&li->li_tv, STACK_TV_BOT(-1)); 1759 } 1760 break; 1761 1762 // dict member with string key 1763 case ISN_MEMBER: 1764 { 1765 dict_T *dict; 1766 dictitem_T *di; 1767 1768 tv = STACK_TV_BOT(-1); 1769 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL) 1770 { 1771 emsg(_(e_dictreq)); 1772 goto failed; 1773 } 1774 dict = tv->vval.v_dict; 1775 1776 if ((di = dict_find(dict, iptr->isn_arg.string, -1)) 1777 == NULL) 1778 { 1779 semsg(_(e_dictkey), iptr->isn_arg.string); 1780 goto failed; 1781 } 1782 clear_tv(tv); 1783 copy_tv(&di->di_tv, tv); 1784 } 1785 break; 1786 1787 case ISN_NEGATENR: 1788 tv = STACK_TV_BOT(-1); 1789 if (tv->v_type != VAR_NUMBER 1790 #ifdef FEAT_FLOAT 1791 && tv->v_type != VAR_FLOAT 1792 #endif 1793 ) 1794 { 1795 emsg(_(e_number_exp)); 1796 goto failed; 1797 } 1798 #ifdef FEAT_FLOAT 1799 if (tv->v_type == VAR_FLOAT) 1800 tv->vval.v_float = -tv->vval.v_float; 1801 else 1802 #endif 1803 tv->vval.v_number = -tv->vval.v_number; 1804 break; 1805 1806 case ISN_CHECKNR: 1807 { 1808 int error = FALSE; 1809 1810 tv = STACK_TV_BOT(-1); 1811 if (check_not_string(tv) == FAIL) 1812 goto failed; 1813 (void)tv_get_number_chk(tv, &error); 1814 if (error) 1815 goto failed; 1816 } 1817 break; 1818 1819 case ISN_CHECKTYPE: 1820 { 1821 checktype_T *ct = &iptr->isn_arg.type; 1822 1823 tv = STACK_TV_BOT(ct->ct_off); 1824 if (tv->v_type != ct->ct_type) 1825 { 1826 semsg(_("E1029: Expected %s but got %s"), 1827 vartype_name(ct->ct_type), 1828 vartype_name(tv->v_type)); 1829 goto failed; 1830 } 1831 } 1832 break; 1833 1834 case ISN_2BOOL: 1835 { 1836 int n; 1837 1838 tv = STACK_TV_BOT(-1); 1839 n = tv2bool(tv); 1840 if (iptr->isn_arg.number) // invert 1841 n = !n; 1842 clear_tv(tv); 1843 tv->v_type = VAR_BOOL; 1844 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE; 1845 } 1846 break; 1847 1848 case ISN_2STRING: 1849 { 1850 char_u *str; 1851 1852 tv = STACK_TV_BOT(iptr->isn_arg.number); 1853 if (tv->v_type != VAR_STRING) 1854 { 1855 str = typval_tostring(tv); 1856 clear_tv(tv); 1857 tv->v_type = VAR_STRING; 1858 tv->vval.v_string = str; 1859 } 1860 } 1861 break; 1862 1863 case ISN_DROP: 1864 --ectx.ec_stack.ga_len; 1865 clear_tv(STACK_TV_BOT(0)); 1866 break; 1867 } 1868 } 1869 1870 done: 1871 // function finished, get result from the stack. 1872 tv = STACK_TV_BOT(-1); 1873 *rettv = *tv; 1874 tv->v_type = VAR_UNKNOWN; 1875 ret = OK; 1876 1877 failed: 1878 // When failed need to unwind the call stack. 1879 while (ectx.ec_frame != initial_frame_ptr) 1880 func_return(&ectx); 1881 failed_early: 1882 current_sctx.sc_version = save_sc_version; 1883 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx) 1884 clear_tv(STACK_TV(idx)); 1885 vim_free(ectx.ec_stack.ga_data); 1886 vim_free(ectx.ec_trystack.ga_data); 1887 return ret; 1888 } 1889 1890 /* 1891 * ":dissassemble". 1892 * We don't really need this at runtime, but we do have tests that require it, 1893 * so always include this. 1894 */ 1895 void 1896 ex_disassemble(exarg_T *eap) 1897 { 1898 char_u *arg = eap->arg; 1899 char_u *fname; 1900 ufunc_T *ufunc; 1901 dfunc_T *dfunc; 1902 isn_T *instr; 1903 int current; 1904 int line_idx = 0; 1905 int prev_current = 0; 1906 1907 fname = trans_function_name(&arg, FALSE, 1908 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL); 1909 if (fname == NULL) 1910 { 1911 semsg(_(e_invarg2), eap->arg); 1912 return; 1913 } 1914 1915 ufunc = find_func(fname, NULL); 1916 if (ufunc == NULL) 1917 { 1918 char_u *p = untrans_function_name(fname); 1919 1920 if (p != NULL) 1921 // Try again without making it script-local. 1922 ufunc = find_func(p, NULL); 1923 } 1924 vim_free(fname); 1925 if (ufunc == NULL) 1926 { 1927 semsg(_("E1061: Cannot find function %s"), eap->arg); 1928 return; 1929 } 1930 if (ufunc->uf_dfunc_idx < 0) 1931 { 1932 semsg(_("E1062: Function %s is not compiled"), eap->arg); 1933 return; 1934 } 1935 if (ufunc->uf_name_exp != NULL) 1936 msg((char *)ufunc->uf_name_exp); 1937 else 1938 msg((char *)ufunc->uf_name); 1939 1940 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; 1941 instr = dfunc->df_instr; 1942 for (current = 0; current < dfunc->df_instr_count; ++current) 1943 { 1944 isn_T *iptr = &instr[current]; 1945 char *line; 1946 1947 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len) 1948 { 1949 if (current > prev_current) 1950 { 1951 msg_puts("\n\n"); 1952 prev_current = current; 1953 } 1954 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++]; 1955 if (line != NULL) 1956 msg(line); 1957 } 1958 1959 switch (iptr->isn_type) 1960 { 1961 case ISN_EXEC: 1962 smsg("%4d EXEC %s", current, iptr->isn_arg.string); 1963 break; 1964 case ISN_ECHO: 1965 { 1966 echo_T *echo = &iptr->isn_arg.echo; 1967 1968 smsg("%4d %s %d", current, 1969 echo->echo_with_white ? "ECHO" : "ECHON", 1970 echo->echo_count); 1971 } 1972 break; 1973 case ISN_EXECUTE: 1974 smsg("%4d EXECUTE %lld", current, 1975 (long long)(iptr->isn_arg.number)); 1976 break; 1977 case ISN_ECHOMSG: 1978 smsg("%4d ECHOMSG %lld", current, 1979 (long long)(iptr->isn_arg.number)); 1980 break; 1981 case ISN_ECHOERR: 1982 smsg("%4d ECHOERR %lld", current, 1983 (long long)(iptr->isn_arg.number)); 1984 break; 1985 case ISN_LOAD: 1986 if (iptr->isn_arg.number < 0) 1987 smsg("%4d LOAD arg[%lld]", current, 1988 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE)); 1989 else 1990 smsg("%4d LOAD $%lld", current, 1991 (long long)(iptr->isn_arg.number)); 1992 break; 1993 case ISN_LOADV: 1994 smsg("%4d LOADV v:%s", current, 1995 get_vim_var_name(iptr->isn_arg.number)); 1996 break; 1997 case ISN_LOADSCRIPT: 1998 { 1999 scriptitem_T *si = 2000 SCRIPT_ITEM(iptr->isn_arg.script.script_sid); 2001 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 2002 + iptr->isn_arg.script.script_idx; 2003 2004 smsg("%4d LOADSCRIPT %s from %s", current, 2005 sv->sv_name, si->sn_name); 2006 } 2007 break; 2008 case ISN_LOADS: 2009 { 2010 scriptitem_T *si = SCRIPT_ITEM( 2011 iptr->isn_arg.loadstore.ls_sid); 2012 2013 smsg("%4d LOADS s:%s from %s", current, 2014 iptr->isn_arg.loadstore.ls_name, si->sn_name); 2015 } 2016 break; 2017 case ISN_LOADG: 2018 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string); 2019 break; 2020 case ISN_LOADB: 2021 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string); 2022 break; 2023 case ISN_LOADW: 2024 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string); 2025 break; 2026 case ISN_LOADT: 2027 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string); 2028 break; 2029 case ISN_LOADOPT: 2030 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string); 2031 break; 2032 case ISN_LOADENV: 2033 smsg("%4d LOADENV %s", current, iptr->isn_arg.string); 2034 break; 2035 case ISN_LOADREG: 2036 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number)); 2037 break; 2038 2039 case ISN_STORE: 2040 if (iptr->isn_arg.number < 0) 2041 smsg("%4d STORE arg[%lld]", current, 2042 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE)); 2043 else 2044 smsg("%4d STORE $%lld", current, 2045 (long long)(iptr->isn_arg.number)); 2046 break; 2047 case ISN_STOREV: 2048 smsg("%4d STOREV v:%s", current, 2049 get_vim_var_name(iptr->isn_arg.number)); 2050 break; 2051 case ISN_STOREG: 2052 smsg("%4d STOREG %s", current, iptr->isn_arg.string); 2053 break; 2054 case ISN_STOREB: 2055 smsg("%4d STOREB %s", current, iptr->isn_arg.string); 2056 break; 2057 case ISN_STOREW: 2058 smsg("%4d STOREW %s", current, iptr->isn_arg.string); 2059 break; 2060 case ISN_STORET: 2061 smsg("%4d STORET %s", current, iptr->isn_arg.string); 2062 break; 2063 case ISN_STORES: 2064 { 2065 scriptitem_T *si = SCRIPT_ITEM( 2066 iptr->isn_arg.loadstore.ls_sid); 2067 2068 smsg("%4d STORES %s in %s", current, 2069 iptr->isn_arg.loadstore.ls_name, si->sn_name); 2070 } 2071 break; 2072 case ISN_STORESCRIPT: 2073 { 2074 scriptitem_T *si = 2075 SCRIPT_ITEM(iptr->isn_arg.script.script_sid); 2076 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 2077 + iptr->isn_arg.script.script_idx; 2078 2079 smsg("%4d STORESCRIPT %s in %s", current, 2080 sv->sv_name, si->sn_name); 2081 } 2082 break; 2083 case ISN_STOREOPT: 2084 smsg("%4d STOREOPT &%s", current, 2085 iptr->isn_arg.storeopt.so_name); 2086 break; 2087 case ISN_STOREENV: 2088 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string); 2089 break; 2090 case ISN_STOREREG: 2091 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number); 2092 break; 2093 case ISN_STORENR: 2094 smsg("%4d STORE %lld in $%d", current, 2095 iptr->isn_arg.storenr.stnr_val, 2096 iptr->isn_arg.storenr.stnr_idx); 2097 break; 2098 2099 // constants 2100 case ISN_PUSHNR: 2101 smsg("%4d PUSHNR %lld", current, 2102 (long long)(iptr->isn_arg.number)); 2103 break; 2104 case ISN_PUSHBOOL: 2105 case ISN_PUSHSPEC: 2106 smsg("%4d PUSH %s", current, 2107 get_var_special_name(iptr->isn_arg.number)); 2108 break; 2109 case ISN_PUSHF: 2110 #ifdef FEAT_FLOAT 2111 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber); 2112 #endif 2113 break; 2114 case ISN_PUSHS: 2115 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string); 2116 break; 2117 case ISN_PUSHBLOB: 2118 { 2119 char_u *r; 2120 char_u numbuf[NUMBUFLEN]; 2121 char_u *tofree; 2122 2123 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf); 2124 smsg("%4d PUSHBLOB %s", current, r); 2125 vim_free(tofree); 2126 } 2127 break; 2128 case ISN_PUSHFUNC: 2129 { 2130 char *name = (char *)iptr->isn_arg.string; 2131 2132 smsg("%4d PUSHFUNC \"%s\"", current, 2133 name == NULL ? "[none]" : name); 2134 } 2135 break; 2136 case ISN_PUSHCHANNEL: 2137 #ifdef FEAT_JOB_CHANNEL 2138 { 2139 channel_T *channel = iptr->isn_arg.channel; 2140 2141 smsg("%4d PUSHCHANNEL %d", current, 2142 channel == NULL ? 0 : channel->ch_id); 2143 } 2144 #endif 2145 break; 2146 case ISN_PUSHJOB: 2147 #ifdef FEAT_JOB_CHANNEL 2148 { 2149 typval_T tv; 2150 char_u *name; 2151 2152 tv.v_type = VAR_JOB; 2153 tv.vval.v_job = iptr->isn_arg.job; 2154 name = tv_get_string(&tv); 2155 smsg("%4d PUSHJOB \"%s\"", current, name); 2156 } 2157 #endif 2158 break; 2159 case ISN_PUSHEXC: 2160 smsg("%4d PUSH v:exception", current); 2161 break; 2162 case ISN_UNLET: 2163 smsg("%4d UNLET%s %s", current, 2164 iptr->isn_arg.unlet.ul_forceit ? "!" : "", 2165 iptr->isn_arg.unlet.ul_name); 2166 break; 2167 case ISN_UNLETENV: 2168 smsg("%4d UNLETENV%s $%s", current, 2169 iptr->isn_arg.unlet.ul_forceit ? "!" : "", 2170 iptr->isn_arg.unlet.ul_name); 2171 break; 2172 case ISN_NEWLIST: 2173 smsg("%4d NEWLIST size %lld", current, 2174 (long long)(iptr->isn_arg.number)); 2175 break; 2176 case ISN_NEWDICT: 2177 smsg("%4d NEWDICT size %lld", current, 2178 (long long)(iptr->isn_arg.number)); 2179 break; 2180 2181 // function call 2182 case ISN_BCALL: 2183 { 2184 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc; 2185 2186 smsg("%4d BCALL %s(argc %d)", current, 2187 internal_func_name(cbfunc->cbf_idx), 2188 cbfunc->cbf_argcount); 2189 } 2190 break; 2191 case ISN_DCALL: 2192 { 2193 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc; 2194 dfunc_T *df = ((dfunc_T *)def_functions.ga_data) 2195 + cdfunc->cdf_idx; 2196 2197 smsg("%4d DCALL %s(argc %d)", current, 2198 df->df_ufunc->uf_name_exp != NULL 2199 ? df->df_ufunc->uf_name_exp 2200 : df->df_ufunc->uf_name, cdfunc->cdf_argcount); 2201 } 2202 break; 2203 case ISN_UCALL: 2204 { 2205 cufunc_T *cufunc = &iptr->isn_arg.ufunc; 2206 2207 smsg("%4d UCALL %s(argc %d)", current, 2208 cufunc->cuf_name, cufunc->cuf_argcount); 2209 } 2210 break; 2211 case ISN_PCALL: 2212 { 2213 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc; 2214 2215 smsg("%4d PCALL%s (argc %d)", current, 2216 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount); 2217 } 2218 break; 2219 case ISN_PCALL_END: 2220 smsg("%4d PCALL end", current); 2221 break; 2222 case ISN_RETURN: 2223 smsg("%4d RETURN", current); 2224 break; 2225 case ISN_FUNCREF: 2226 { 2227 dfunc_T *df = ((dfunc_T *)def_functions.ga_data) 2228 + iptr->isn_arg.number; 2229 2230 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name); 2231 } 2232 break; 2233 2234 case ISN_JUMP: 2235 { 2236 char *when = "?"; 2237 2238 switch (iptr->isn_arg.jump.jump_when) 2239 { 2240 case JUMP_ALWAYS: 2241 when = "JUMP"; 2242 break; 2243 case JUMP_AND_KEEP_IF_TRUE: 2244 when = "JUMP_AND_KEEP_IF_TRUE"; 2245 break; 2246 case JUMP_IF_FALSE: 2247 when = "JUMP_IF_FALSE"; 2248 break; 2249 case JUMP_AND_KEEP_IF_FALSE: 2250 when = "JUMP_AND_KEEP_IF_FALSE"; 2251 break; 2252 } 2253 smsg("%4d %s -> %d", current, when, 2254 iptr->isn_arg.jump.jump_where); 2255 } 2256 break; 2257 2258 case ISN_FOR: 2259 { 2260 forloop_T *forloop = &iptr->isn_arg.forloop; 2261 2262 smsg("%4d FOR $%d -> %d", current, 2263 forloop->for_idx, forloop->for_end); 2264 } 2265 break; 2266 2267 case ISN_TRY: 2268 { 2269 try_T *try = &iptr->isn_arg.try; 2270 2271 smsg("%4d TRY catch -> %d, finally -> %d", current, 2272 try->try_catch, try->try_finally); 2273 } 2274 break; 2275 case ISN_CATCH: 2276 // TODO 2277 smsg("%4d CATCH", current); 2278 break; 2279 case ISN_ENDTRY: 2280 smsg("%4d ENDTRY", current); 2281 break; 2282 case ISN_THROW: 2283 smsg("%4d THROW", current); 2284 break; 2285 2286 // expression operations on number 2287 case ISN_OPNR: 2288 case ISN_OPFLOAT: 2289 case ISN_OPANY: 2290 { 2291 char *what; 2292 char *ins; 2293 2294 switch (iptr->isn_arg.op.op_type) 2295 { 2296 case EXPR_MULT: what = "*"; break; 2297 case EXPR_DIV: what = "/"; break; 2298 case EXPR_REM: what = "%"; break; 2299 case EXPR_SUB: what = "-"; break; 2300 case EXPR_ADD: what = "+"; break; 2301 default: what = "???"; break; 2302 } 2303 switch (iptr->isn_type) 2304 { 2305 case ISN_OPNR: ins = "OPNR"; break; 2306 case ISN_OPFLOAT: ins = "OPFLOAT"; break; 2307 case ISN_OPANY: ins = "OPANY"; break; 2308 default: ins = "???"; break; 2309 } 2310 smsg("%4d %s %s", current, ins, what); 2311 } 2312 break; 2313 2314 case ISN_COMPAREBOOL: 2315 case ISN_COMPARESPECIAL: 2316 case ISN_COMPARENR: 2317 case ISN_COMPAREFLOAT: 2318 case ISN_COMPARESTRING: 2319 case ISN_COMPAREBLOB: 2320 case ISN_COMPARELIST: 2321 case ISN_COMPAREDICT: 2322 case ISN_COMPAREFUNC: 2323 case ISN_COMPAREANY: 2324 { 2325 char *p; 2326 char buf[10]; 2327 char *type; 2328 2329 switch (iptr->isn_arg.op.op_type) 2330 { 2331 case EXPR_EQUAL: p = "=="; break; 2332 case EXPR_NEQUAL: p = "!="; break; 2333 case EXPR_GREATER: p = ">"; break; 2334 case EXPR_GEQUAL: p = ">="; break; 2335 case EXPR_SMALLER: p = "<"; break; 2336 case EXPR_SEQUAL: p = "<="; break; 2337 case EXPR_MATCH: p = "=~"; break; 2338 case EXPR_IS: p = "is"; break; 2339 case EXPR_ISNOT: p = "isnot"; break; 2340 case EXPR_NOMATCH: p = "!~"; break; 2341 default: p = "???"; break; 2342 } 2343 STRCPY(buf, p); 2344 if (iptr->isn_arg.op.op_ic == TRUE) 2345 strcat(buf, "?"); 2346 switch(iptr->isn_type) 2347 { 2348 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break; 2349 case ISN_COMPARESPECIAL: 2350 type = "COMPARESPECIAL"; break; 2351 case ISN_COMPARENR: type = "COMPARENR"; break; 2352 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break; 2353 case ISN_COMPARESTRING: 2354 type = "COMPARESTRING"; break; 2355 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break; 2356 case ISN_COMPARELIST: type = "COMPARELIST"; break; 2357 case ISN_COMPAREDICT: type = "COMPAREDICT"; break; 2358 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break; 2359 case ISN_COMPAREANY: type = "COMPAREANY"; break; 2360 default: type = "???"; break; 2361 } 2362 2363 smsg("%4d %s %s", current, type, buf); 2364 } 2365 break; 2366 2367 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break; 2368 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break; 2369 2370 // expression operations 2371 case ISN_CONCAT: smsg("%4d CONCAT", current); break; 2372 case ISN_INDEX: smsg("%4d INDEX", current); break; 2373 case ISN_MEMBER: smsg("%4d MEMBER %s", current, 2374 iptr->isn_arg.string); break; 2375 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break; 2376 2377 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break; 2378 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current, 2379 vartype_name(iptr->isn_arg.type.ct_type), 2380 iptr->isn_arg.type.ct_off); 2381 break; 2382 case ISN_2BOOL: if (iptr->isn_arg.number) 2383 smsg("%4d INVERT (!val)", current); 2384 else 2385 smsg("%4d 2BOOL (!!val)", current); 2386 break; 2387 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current, 2388 (long long)(iptr->isn_arg.number)); 2389 break; 2390 2391 case ISN_DROP: smsg("%4d DROP", current); break; 2392 } 2393 } 2394 } 2395 2396 /* 2397 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty 2398 * list, etc. Mostly like what JavaScript does, except that empty list and 2399 * empty dictionary are FALSE. 2400 */ 2401 int 2402 tv2bool(typval_T *tv) 2403 { 2404 switch (tv->v_type) 2405 { 2406 case VAR_NUMBER: 2407 return tv->vval.v_number != 0; 2408 case VAR_FLOAT: 2409 #ifdef FEAT_FLOAT 2410 return tv->vval.v_float != 0.0; 2411 #else 2412 break; 2413 #endif 2414 case VAR_PARTIAL: 2415 return tv->vval.v_partial != NULL; 2416 case VAR_FUNC: 2417 case VAR_STRING: 2418 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL; 2419 case VAR_LIST: 2420 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0; 2421 case VAR_DICT: 2422 return tv->vval.v_dict != NULL 2423 && tv->vval.v_dict->dv_hashtab.ht_used > 0; 2424 case VAR_BOOL: 2425 case VAR_SPECIAL: 2426 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE; 2427 case VAR_JOB: 2428 #ifdef FEAT_JOB_CHANNEL 2429 return tv->vval.v_job != NULL; 2430 #else 2431 break; 2432 #endif 2433 case VAR_CHANNEL: 2434 #ifdef FEAT_JOB_CHANNEL 2435 return tv->vval.v_channel != NULL; 2436 #else 2437 break; 2438 #endif 2439 case VAR_BLOB: 2440 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0; 2441 case VAR_UNKNOWN: 2442 case VAR_ANY: 2443 case VAR_VOID: 2444 break; 2445 } 2446 return FALSE; 2447 } 2448 2449 /* 2450 * If "tv" is a string give an error and return FAIL. 2451 */ 2452 int 2453 check_not_string(typval_T *tv) 2454 { 2455 if (tv->v_type == VAR_STRING) 2456 { 2457 emsg(_("E1030: Using a String as a Number")); 2458 clear_tv(tv); 2459 return FAIL; 2460 } 2461 return OK; 2462 } 2463 2464 2465 #endif // FEAT_EVAL 2466