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