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