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