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