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 71 garray_T ec_funcrefs; // partials that might be a closure 72 } ectx_T; 73 74 // Get pointer to item relative to the bottom of the stack, -1 is the last one. 75 #define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx)) 76 77 void 78 to_string_error(vartype_T vartype) 79 { 80 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype)); 81 } 82 83 /* 84 * Return the number of arguments, including optional arguments and any vararg. 85 */ 86 static int 87 ufunc_argcount(ufunc_T *ufunc) 88 { 89 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0); 90 } 91 92 /* 93 * Set the instruction index, depending on omitted arguments, where the default 94 * values are to be computed. If all optional arguments are present, start 95 * with the function body. 96 * The expression evaluation is at the start of the instructions: 97 * 0 -> EVAL default1 98 * STORE arg[-2] 99 * 1 -> EVAL default2 100 * STORE arg[-1] 101 * 2 -> function body 102 */ 103 static void 104 init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx) 105 { 106 if (ufunc->uf_def_args.ga_len == 0) 107 ectx->ec_iidx = 0; 108 else 109 { 110 int defcount = ufunc->uf_args.ga_len - argcount; 111 112 // If there is a varargs argument defcount can be negative, no defaults 113 // to evaluate then. 114 if (defcount < 0) 115 defcount = 0; 116 ectx->ec_iidx = ufunc->uf_def_arg_idx[ 117 ufunc->uf_def_args.ga_len - defcount]; 118 } 119 } 120 121 /* 122 * Create a new list from "count" items at the bottom of the stack. 123 * When "count" is zero an empty list is added to the stack. 124 */ 125 static int 126 exe_newlist(int count, ectx_T *ectx) 127 { 128 list_T *list = list_alloc_with_items(count); 129 int idx; 130 typval_T *tv; 131 132 if (list == NULL) 133 return FAIL; 134 for (idx = 0; idx < count; ++idx) 135 list_set_item(list, idx, STACK_TV_BOT(idx - count)); 136 137 if (count > 0) 138 ectx->ec_stack.ga_len -= count - 1; 139 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL) 140 return FAIL; 141 else 142 ++ectx->ec_stack.ga_len; 143 tv = STACK_TV_BOT(-1); 144 tv->v_type = VAR_LIST; 145 tv->vval.v_list = list; 146 ++list->lv_refcount; 147 return OK; 148 } 149 150 /* 151 * Call compiled function "cdf_idx" from compiled code. 152 * 153 * Stack has: 154 * - current arguments (already there) 155 * - omitted optional argument (default values) added here 156 * - stack frame: 157 * - pointer to calling function 158 * - Index of next instruction in calling function 159 * - previous frame pointer 160 * - reserved space for local variables 161 */ 162 static int 163 call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx) 164 { 165 int argcount = argcount_arg; 166 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx; 167 ufunc_T *ufunc = dfunc->df_ufunc; 168 int arg_to_add; 169 int vararg_count = 0; 170 int varcount; 171 int idx; 172 estack_T *entry; 173 174 if (dfunc->df_deleted) 175 { 176 emsg_funcname(e_func_deleted, ufunc->uf_name); 177 return FAIL; 178 } 179 180 if (ufunc->uf_va_name != NULL) 181 { 182 // Need to make a list out of the vararg arguments. 183 // Stack at time of call with 2 varargs: 184 // normal_arg 185 // optional_arg 186 // vararg_1 187 // vararg_2 188 // After creating the list: 189 // normal_arg 190 // optional_arg 191 // vararg-list 192 // With missing optional arguments we get: 193 // normal_arg 194 // After creating the list 195 // normal_arg 196 // (space for optional_arg) 197 // vararg-list 198 vararg_count = argcount - ufunc->uf_args.ga_len; 199 if (vararg_count < 0) 200 vararg_count = 0; 201 else 202 argcount -= vararg_count; 203 if (exe_newlist(vararg_count, ectx) == FAIL) 204 return FAIL; 205 206 vararg_count = 1; 207 } 208 209 arg_to_add = ufunc->uf_args.ga_len - argcount; 210 if (arg_to_add < 0) 211 { 212 if (arg_to_add == -1) 213 emsg(_(e_one_argument_too_many)); 214 else 215 semsg(_(e_nr_arguments_too_many), -arg_to_add); 216 return FAIL; 217 } 218 219 // Reserve space for: 220 // - missing arguments 221 // - stack frame 222 // - local variables 223 // - if needed: a counter for number of closures created in 224 // ectx->ec_funcrefs. 225 varcount = dfunc->df_varcount + dfunc->df_has_closure; 226 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount) 227 == FAIL) 228 return FAIL; 229 230 // Move the vararg-list to below the missing optional arguments. 231 if (vararg_count > 0 && arg_to_add > 0) 232 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1); 233 234 // Reserve space for omitted optional arguments, filled in soon. 235 for (idx = 0; idx < arg_to_add; ++idx) 236 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN; 237 ectx->ec_stack.ga_len += arg_to_add; 238 239 // Store current execution state in stack frame for ISN_RETURN. 240 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx; 241 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx; 242 STACK_TV_BOT(2)->vval.v_string = (void *)ectx->ec_outer_stack; 243 STACK_TV_BOT(3)->vval.v_number = ectx->ec_outer_frame; 244 STACK_TV_BOT(4)->vval.v_number = ectx->ec_frame_idx; 245 ectx->ec_frame_idx = ectx->ec_stack.ga_len; 246 247 // Initialize local variables 248 for (idx = 0; idx < dfunc->df_varcount; ++idx) 249 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN; 250 if (dfunc->df_has_closure) 251 { 252 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount); 253 254 tv->v_type = VAR_NUMBER; 255 tv->vval.v_number = 0; 256 } 257 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount; 258 259 // Set execution state to the start of the called function. 260 ectx->ec_dfunc_idx = cdf_idx; 261 ectx->ec_instr = dfunc->df_instr; 262 entry = estack_push_ufunc(dfunc->df_ufunc, 1); 263 if (entry != NULL) 264 { 265 // Set the script context to the script where the function was defined. 266 // TODO: save more than the SID? 267 entry->es_save_sid = current_sctx.sc_sid; 268 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid; 269 } 270 271 // Decide where to start execution, handles optional arguments. 272 init_instr_idx(ufunc, argcount, ectx); 273 274 return OK; 275 } 276 277 // Get pointer to item in the stack. 278 #define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx) 279 280 /* 281 * Used when returning from a function: Check if any closure is still 282 * referenced. If so then move the arguments and variables to a separate piece 283 * of stack to be used when the closure is called. 284 * When "free_arguments" is TRUE the arguments are to be freed. 285 * Returns FAIL when out of memory. 286 */ 287 static int 288 handle_closure_in_use(ectx_T *ectx, int free_arguments) 289 { 290 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 291 + ectx->ec_dfunc_idx; 292 int argcount; 293 int top; 294 int idx; 295 typval_T *tv; 296 int closure_in_use = FALSE; 297 garray_T *gap = &ectx->ec_funcrefs; 298 varnumber_T closure_count; 299 300 if (dfunc->df_ufunc == NULL) 301 return OK; // function was freed 302 if (dfunc->df_has_closure == 0) 303 return OK; // no closures 304 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount); 305 closure_count = tv->vval.v_number; 306 if (closure_count == 0) 307 return OK; // no funcrefs created 308 309 argcount = ufunc_argcount(dfunc->df_ufunc); 310 top = ectx->ec_frame_idx - argcount; 311 312 // Check if any created closure is still in use. 313 for (idx = 0; idx < closure_count; ++idx) 314 { 315 partial_T *pt; 316 int off = gap->ga_len - closure_count + idx; 317 318 if (off < 0) 319 continue; // count is off or already done 320 pt = ((partial_T **)gap->ga_data)[off]; 321 if (pt->pt_refcount > 1) 322 { 323 int refcount = pt->pt_refcount; 324 int i; 325 326 // A Reference in a local variables doesn't count, it gets 327 // unreferenced on return. 328 for (i = 0; i < dfunc->df_varcount; ++i) 329 { 330 typval_T *stv = STACK_TV(ectx->ec_frame_idx 331 + STACK_FRAME_SIZE + i); 332 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial) 333 --refcount; 334 } 335 if (refcount > 1) 336 { 337 closure_in_use = TRUE; 338 break; 339 } 340 } 341 } 342 343 if (closure_in_use) 344 { 345 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T); 346 typval_T *stack; 347 348 // A closure is using the arguments and/or local variables. 349 // Move them to the called function. 350 if (funcstack == NULL) 351 return FAIL; 352 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE; 353 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount; 354 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len); 355 funcstack->fs_ga.ga_data = stack; 356 if (stack == NULL) 357 { 358 vim_free(funcstack); 359 return FAIL; 360 } 361 362 // Move or copy the arguments. 363 for (idx = 0; idx < argcount; ++idx) 364 { 365 tv = STACK_TV(top + idx); 366 if (free_arguments) 367 { 368 *(stack + idx) = *tv; 369 tv->v_type = VAR_UNKNOWN; 370 } 371 else 372 copy_tv(tv, stack + idx); 373 } 374 // Move the local variables. 375 for (idx = 0; idx < dfunc->df_varcount; ++idx) 376 { 377 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx); 378 379 // A partial created for a local function, that is also used as a 380 // local variable, has a reference count for the variable, thus 381 // will never go down to zero. When all these refcounts are one 382 // then the funcstack is unused. We need to count how many we have 383 // so we need when to check. 384 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL) 385 { 386 int i; 387 388 for (i = 0; i < closure_count; ++i) 389 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[ 390 gap->ga_len - closure_count + i]) 391 ++funcstack->fs_min_refcount; 392 } 393 394 *(stack + funcstack->fs_var_offset + idx) = *tv; 395 tv->v_type = VAR_UNKNOWN; 396 } 397 398 for (idx = 0; idx < closure_count; ++idx) 399 { 400 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len 401 - closure_count + idx]; 402 if (pt->pt_refcount > 1) 403 { 404 ++funcstack->fs_refcount; 405 pt->pt_funcstack = funcstack; 406 pt->pt_ectx_stack = &funcstack->fs_ga; 407 pt->pt_ectx_frame = ectx->ec_frame_idx - top; 408 } 409 } 410 } 411 412 for (idx = 0; idx < closure_count; ++idx) 413 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len 414 - closure_count + idx]); 415 gap->ga_len -= closure_count; 416 if (gap->ga_len == 0) 417 ga_clear(gap); 418 419 return OK; 420 } 421 422 /* 423 * Called when a partial is freed or its reference count goes down to one. The 424 * funcstack may be the only reference to the partials in the local variables. 425 * Go over all of them, the funcref and can be freed if all partials 426 * referencing the funcstack have a reference count of one. 427 */ 428 void 429 funcstack_check_refcount(funcstack_T *funcstack) 430 { 431 int i; 432 garray_T *gap = &funcstack->fs_ga; 433 int done = 0; 434 435 if (funcstack->fs_refcount > funcstack->fs_min_refcount) 436 return; 437 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i) 438 { 439 typval_T *tv = ((typval_T *)gap->ga_data) + i; 440 441 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL 442 && tv->vval.v_partial->pt_funcstack == funcstack 443 && tv->vval.v_partial->pt_refcount == 1) 444 ++done; 445 } 446 if (done == funcstack->fs_min_refcount) 447 { 448 typval_T *stack = gap->ga_data; 449 450 // All partials referencing the funcstack have a reference count of 451 // one, thus the funcstack is no longer of use. 452 for (i = 0; i < gap->ga_len; ++i) 453 clear_tv(stack + i); 454 vim_free(stack); 455 vim_free(funcstack); 456 } 457 } 458 459 /* 460 * Return from the current function. 461 */ 462 static int 463 func_return(ectx_T *ectx) 464 { 465 int idx; 466 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 467 + ectx->ec_dfunc_idx; 468 int argcount = ufunc_argcount(dfunc->df_ufunc); 469 int top = ectx->ec_frame_idx - argcount; 470 estack_T *entry; 471 472 // execution context goes one level up 473 entry = estack_pop(); 474 if (entry != NULL) 475 current_sctx.sc_sid = entry->es_save_sid; 476 477 if (handle_closure_in_use(ectx, TRUE) == FAIL) 478 return FAIL; 479 480 // Clear the arguments. 481 for (idx = top; idx < ectx->ec_frame_idx; ++idx) 482 clear_tv(STACK_TV(idx)); 483 484 // Clear local variables and temp values, but not the return value. 485 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE; 486 idx < ectx->ec_stack.ga_len - 1; ++idx) 487 clear_tv(STACK_TV(idx)); 488 489 // Restore the previous frame. 490 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number; 491 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number; 492 ectx->ec_outer_stack = 493 (void *)STACK_TV(ectx->ec_frame_idx + 2)->vval.v_string; 494 ectx->ec_outer_frame = STACK_TV(ectx->ec_frame_idx + 3)->vval.v_number; 495 // restoring ec_frame_idx must be last 496 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 4)->vval.v_number; 497 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx; 498 ectx->ec_instr = dfunc->df_instr; 499 500 // Reset the stack to the position before the call, move the return value 501 // to the top of the stack. 502 idx = ectx->ec_stack.ga_len - 1; 503 ectx->ec_stack.ga_len = top + 1; 504 *STACK_TV_BOT(-1) = *STACK_TV(idx); 505 506 return OK; 507 } 508 509 #undef STACK_TV 510 511 /* 512 * Prepare arguments and rettv for calling a builtin or user function. 513 */ 514 static int 515 call_prepare(int argcount, typval_T *argvars, ectx_T *ectx) 516 { 517 int idx; 518 typval_T *tv; 519 520 // Move arguments from bottom of the stack to argvars[] and add terminator. 521 for (idx = 0; idx < argcount; ++idx) 522 argvars[idx] = *STACK_TV_BOT(idx - argcount); 523 argvars[argcount].v_type = VAR_UNKNOWN; 524 525 // Result replaces the arguments on the stack. 526 if (argcount > 0) 527 ectx->ec_stack.ga_len -= argcount - 1; 528 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL) 529 return FAIL; 530 else 531 ++ectx->ec_stack.ga_len; 532 533 // Default return value is zero. 534 tv = STACK_TV_BOT(-1); 535 tv->v_type = VAR_NUMBER; 536 tv->vval.v_number = 0; 537 538 return OK; 539 } 540 541 // Ugly global to avoid passing the execution context around through many 542 // layers. 543 static ectx_T *current_ectx = NULL; 544 545 /* 546 * Call a builtin function by index. 547 */ 548 static int 549 call_bfunc(int func_idx, int argcount, ectx_T *ectx) 550 { 551 typval_T argvars[MAX_FUNC_ARGS]; 552 int idx; 553 int did_emsg_before = did_emsg; 554 ectx_T *prev_ectx = current_ectx; 555 556 if (call_prepare(argcount, argvars, ectx) == FAIL) 557 return FAIL; 558 559 // Call the builtin function. Set "current_ectx" so that when it 560 // recursively invokes call_def_function() a closure context can be set. 561 current_ectx = ectx; 562 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1)); 563 current_ectx = prev_ectx; 564 565 // Clear the arguments. 566 for (idx = 0; idx < argcount; ++idx) 567 clear_tv(&argvars[idx]); 568 569 if (did_emsg != did_emsg_before) 570 return FAIL; 571 return OK; 572 } 573 574 /* 575 * Execute a user defined function. 576 * "iptr" can be used to replace the instruction with a more efficient one. 577 */ 578 static int 579 call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr) 580 { 581 typval_T argvars[MAX_FUNC_ARGS]; 582 funcexe_T funcexe; 583 int error; 584 int idx; 585 int called_emsg_before = called_emsg; 586 587 if (ufunc->uf_def_status == UF_TO_BE_COMPILED 588 && compile_def_function(ufunc, FALSE, NULL) == FAIL) 589 return FAIL; 590 if (ufunc->uf_def_status == UF_COMPILED) 591 { 592 // The function has been compiled, can call it quickly. For a function 593 // that was defined later: we can call it directly next time. 594 if (iptr != NULL) 595 { 596 delete_instr(iptr); 597 iptr->isn_type = ISN_DCALL; 598 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; 599 iptr->isn_arg.dfunc.cdf_argcount = argcount; 600 } 601 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx); 602 } 603 604 if (call_prepare(argcount, argvars, ectx) == FAIL) 605 return FAIL; 606 CLEAR_FIELD(funcexe); 607 funcexe.evaluate = TRUE; 608 609 // Call the user function. Result goes in last position on the stack. 610 // TODO: add selfdict if there is one 611 error = call_user_func_check(ufunc, argcount, argvars, 612 STACK_TV_BOT(-1), &funcexe, NULL); 613 614 // Clear the arguments. 615 for (idx = 0; idx < argcount; ++idx) 616 clear_tv(&argvars[idx]); 617 618 if (error != FCERR_NONE) 619 { 620 user_func_error(error, ufunc->uf_name); 621 return FAIL; 622 } 623 if (called_emsg > called_emsg_before) 624 // Error other than from calling the function itself. 625 return FAIL; 626 return OK; 627 } 628 629 /* 630 * Return TRUE if an error was given or CTRL-C was pressed. 631 */ 632 static int 633 vim9_aborting(int prev_called_emsg) 634 { 635 return called_emsg > prev_called_emsg || got_int || did_throw; 636 } 637 638 /* 639 * Execute a function by "name". 640 * This can be a builtin function or a user function. 641 * "iptr" can be used to replace the instruction with a more efficient one. 642 * Returns FAIL if not found without an error message. 643 */ 644 static int 645 call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr) 646 { 647 ufunc_T *ufunc; 648 649 if (builtin_function(name, -1)) 650 { 651 int func_idx = find_internal_func(name); 652 653 if (func_idx < 0) 654 return FAIL; 655 if (check_internal_func(func_idx, argcount) < 0) 656 return FAIL; 657 return call_bfunc(func_idx, argcount, ectx); 658 } 659 660 ufunc = find_func(name, FALSE, NULL); 661 662 if (ufunc == NULL) 663 { 664 int called_emsg_before = called_emsg; 665 666 if (script_autoload(name, TRUE)) 667 // loaded a package, search for the function again 668 ufunc = find_func(name, FALSE, NULL); 669 if (vim9_aborting(called_emsg_before)) 670 return FAIL; // bail out if loading the script caused an error 671 } 672 673 if (ufunc != NULL) 674 return call_ufunc(ufunc, argcount, ectx, iptr); 675 676 return FAIL; 677 } 678 679 static int 680 call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx) 681 { 682 int argcount = argcount_arg; 683 char_u *name = NULL; 684 int called_emsg_before = called_emsg; 685 int res; 686 687 if (tv->v_type == VAR_PARTIAL) 688 { 689 partial_T *pt = tv->vval.v_partial; 690 int i; 691 692 if (pt->pt_argc > 0) 693 { 694 // Make space for arguments from the partial, shift the "argcount" 695 // arguments up. 696 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL) 697 return FAIL; 698 for (i = 1; i <= argcount; ++i) 699 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i); 700 ectx->ec_stack.ga_len += pt->pt_argc; 701 argcount += pt->pt_argc; 702 703 // copy the arguments from the partial onto the stack 704 for (i = 0; i < pt->pt_argc; ++i) 705 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i)); 706 } 707 708 if (pt->pt_func != NULL) 709 { 710 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL); 711 712 // closure may need the function context where it was defined 713 ectx->ec_outer_stack = pt->pt_ectx_stack; 714 ectx->ec_outer_frame = pt->pt_ectx_frame; 715 716 return ret; 717 } 718 name = pt->pt_name; 719 } 720 else if (tv->v_type == VAR_FUNC) 721 name = tv->vval.v_string; 722 if (name != NULL) 723 { 724 char_u fname_buf[FLEN_FIXED + 1]; 725 char_u *tofree = NULL; 726 int error = FCERR_NONE; 727 char_u *fname; 728 729 // May need to translate <SNR>123_ to K_SNR. 730 fname = fname_trans_sid(name, fname_buf, &tofree, &error); 731 if (error != FCERR_NONE) 732 res = FAIL; 733 else 734 res = call_by_name(fname, argcount, ectx, NULL); 735 vim_free(tofree); 736 } 737 738 if (name == NULL || res == FAIL) 739 { 740 if (called_emsg == called_emsg_before) 741 semsg(_(e_unknownfunc), 742 name == NULL ? (char_u *)"[unknown]" : name); 743 return FAIL; 744 } 745 return OK; 746 } 747 748 /* 749 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return 750 * TRUE. 751 */ 752 static int 753 error_if_locked(int lock, char *error) 754 { 755 if (lock & (VAR_LOCKED | VAR_FIXED)) 756 { 757 emsg(_(error)); 758 return TRUE; 759 } 760 return FALSE; 761 } 762 763 /* 764 * Store "tv" in variable "name". 765 * This is for s: and g: variables. 766 */ 767 static void 768 store_var(char_u *name, typval_T *tv) 769 { 770 funccal_entry_T entry; 771 772 save_funccal(&entry); 773 set_var_const(name, NULL, tv, FALSE, ASSIGN_NO_DECL); 774 restore_funccal(); 775 } 776 777 778 /* 779 * Execute a function by "name". 780 * This can be a builtin function, user function or a funcref. 781 * "iptr" can be used to replace the instruction with a more efficient one. 782 */ 783 static int 784 call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr) 785 { 786 int called_emsg_before = called_emsg; 787 int res; 788 789 res = call_by_name(name, argcount, ectx, iptr); 790 if (res == FAIL && called_emsg == called_emsg_before) 791 { 792 dictitem_T *v; 793 794 v = find_var(name, NULL, FALSE); 795 if (v == NULL) 796 { 797 semsg(_(e_unknownfunc), name); 798 return FAIL; 799 } 800 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC) 801 { 802 semsg(_(e_unknownfunc), name); 803 return FAIL; 804 } 805 return call_partial(&v->di_tv, argcount, ectx); 806 } 807 return res; 808 } 809 810 /* 811 * Call a "def" function from old Vim script. 812 * Return OK or FAIL. 813 */ 814 int 815 call_def_function( 816 ufunc_T *ufunc, 817 int argc_arg, // nr of arguments 818 typval_T *argv, // arguments 819 partial_T *partial, // optional partial for context 820 typval_T *rettv) // return value 821 { 822 ectx_T ectx; // execution context 823 int argc = argc_arg; 824 int initial_frame_idx; 825 typval_T *tv; 826 int idx; 827 int ret = FAIL; 828 int defcount = ufunc->uf_args.ga_len - argc; 829 sctx_T save_current_sctx = current_sctx; 830 int breakcheck_count = 0; 831 int called_emsg_before = called_emsg; 832 int save_suppress_errthrow = suppress_errthrow; 833 834 // Get pointer to item in the stack. 835 #define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx) 836 837 // Get pointer to item at the bottom of the stack, -1 is the bottom. 838 #undef STACK_TV_BOT 839 #define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx) 840 841 // Get pointer to a local variable on the stack. Negative for arguments. 842 #define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx) 843 844 // Like STACK_TV_VAR but use the outer scope 845 #define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx) 846 847 if (ufunc->uf_def_status == UF_NOT_COMPILED 848 || (ufunc->uf_def_status == UF_TO_BE_COMPILED 849 && compile_def_function(ufunc, FALSE, NULL) == FAIL)) 850 { 851 if (called_emsg == called_emsg_before) 852 semsg(_(e_function_is_not_compiled_str), 853 printable_func_name(ufunc)); 854 return FAIL; 855 } 856 857 { 858 // Check the function was really compiled. 859 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 860 + ufunc->uf_dfunc_idx; 861 if (dfunc->df_instr == NULL) 862 { 863 iemsg("using call_def_function() on not compiled function"); 864 return FAIL; 865 } 866 } 867 868 CLEAR_FIELD(ectx); 869 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx; 870 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500); 871 if (ga_grow(&ectx.ec_stack, 20) == FAIL) 872 return FAIL; 873 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10); 874 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10); 875 876 // Put arguments on the stack. 877 for (idx = 0; idx < argc; ++idx) 878 { 879 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len 880 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx], 881 idx + 1) == FAIL) 882 goto failed_early; 883 copy_tv(&argv[idx], STACK_TV_BOT(0)); 884 ++ectx.ec_stack.ga_len; 885 } 886 887 // Turn varargs into a list. Empty list if no args. 888 if (ufunc->uf_va_name != NULL) 889 { 890 int vararg_count = argc - ufunc->uf_args.ga_len; 891 892 if (vararg_count < 0) 893 vararg_count = 0; 894 else 895 argc -= vararg_count; 896 if (exe_newlist(vararg_count, &ectx) == FAIL) 897 goto failed_early; 898 899 // Check the type of the list items. 900 tv = STACK_TV_BOT(-1); 901 if (ufunc->uf_va_type != NULL 902 && ufunc->uf_va_type != &t_any 903 && ufunc->uf_va_type->tt_member != &t_any 904 && tv->vval.v_list != NULL) 905 { 906 type_T *expected = ufunc->uf_va_type->tt_member; 907 listitem_T *li = tv->vval.v_list->lv_first; 908 909 for (idx = 0; idx < vararg_count; ++idx) 910 { 911 if (check_typval_type(expected, &li->li_tv, 912 argc + idx + 1) == FAIL) 913 goto failed_early; 914 li = li->li_next; 915 } 916 } 917 918 if (defcount > 0) 919 // Move varargs list to below missing default arguments. 920 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1); 921 --ectx.ec_stack.ga_len; 922 } 923 924 // Make space for omitted arguments, will store default value below. 925 // Any varargs list goes after them. 926 if (defcount > 0) 927 for (idx = 0; idx < defcount; ++idx) 928 { 929 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN; 930 ++ectx.ec_stack.ga_len; 931 } 932 if (ufunc->uf_va_name != NULL) 933 ++ectx.ec_stack.ga_len; 934 935 // Frame pointer points to just after arguments. 936 ectx.ec_frame_idx = ectx.ec_stack.ga_len; 937 initial_frame_idx = ectx.ec_frame_idx; 938 939 if (partial != NULL) 940 { 941 if (partial->pt_ectx_stack == NULL && current_ectx != NULL) 942 { 943 // TODO: is this always the right way? 944 ectx.ec_outer_stack = ¤t_ectx->ec_stack; 945 ectx.ec_outer_frame = current_ectx->ec_frame_idx; 946 } 947 else 948 { 949 ectx.ec_outer_stack = partial->pt_ectx_stack; 950 ectx.ec_outer_frame = partial->pt_ectx_frame; 951 } 952 } 953 954 // dummy frame entries 955 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx) 956 { 957 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN; 958 ++ectx.ec_stack.ga_len; 959 } 960 961 { 962 // Reserve space for local variables and any closure reference count. 963 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 964 + ufunc->uf_dfunc_idx; 965 966 for (idx = 0; idx < dfunc->df_varcount; ++idx) 967 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN; 968 ectx.ec_stack.ga_len += dfunc->df_varcount; 969 if (dfunc->df_has_closure) 970 { 971 STACK_TV_VAR(idx)->v_type = VAR_NUMBER; 972 STACK_TV_VAR(idx)->vval.v_number = 0; 973 ++ectx.ec_stack.ga_len; 974 } 975 976 ectx.ec_instr = dfunc->df_instr; 977 } 978 979 // Following errors are in the function, not the caller. 980 // Commands behave like vim9script. 981 estack_push_ufunc(ufunc, 1); 982 current_sctx = ufunc->uf_script_ctx; 983 current_sctx.sc_version = SCRIPT_VERSION_VIM9; 984 985 // Do turn errors into exceptions. 986 suppress_errthrow = FALSE; 987 988 // Decide where to start execution, handles optional arguments. 989 init_instr_idx(ufunc, argc, &ectx); 990 991 for (;;) 992 { 993 isn_T *iptr; 994 995 if (++breakcheck_count >= 100) 996 { 997 line_breakcheck(); 998 breakcheck_count = 0; 999 } 1000 if (got_int) 1001 { 1002 // Turn CTRL-C into an exception. 1003 got_int = FALSE; 1004 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL) 1005 goto failed; 1006 did_throw = TRUE; 1007 } 1008 1009 if (did_emsg && msg_list != NULL && *msg_list != NULL) 1010 { 1011 // Turn an error message into an exception. 1012 did_emsg = FALSE; 1013 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL) 1014 goto failed; 1015 did_throw = TRUE; 1016 *msg_list = NULL; 1017 } 1018 1019 if (did_throw && !ectx.ec_in_catch) 1020 { 1021 garray_T *trystack = &ectx.ec_trystack; 1022 trycmd_T *trycmd = NULL; 1023 1024 // An exception jumps to the first catch, finally, or returns from 1025 // the current function. 1026 if (trystack->ga_len > 0) 1027 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1; 1028 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx) 1029 { 1030 // jump to ":catch" or ":finally" 1031 ectx.ec_in_catch = TRUE; 1032 ectx.ec_iidx = trycmd->tcd_catch_idx; 1033 } 1034 else 1035 { 1036 // Not inside try or need to return from current functions. 1037 // Push a dummy return value. 1038 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1039 goto failed; 1040 tv = STACK_TV_BOT(0); 1041 tv->v_type = VAR_NUMBER; 1042 tv->vval.v_number = 0; 1043 ++ectx.ec_stack.ga_len; 1044 if (ectx.ec_frame_idx == initial_frame_idx) 1045 { 1046 // At the toplevel we are done. 1047 need_rethrow = TRUE; 1048 if (handle_closure_in_use(&ectx, FALSE) == FAIL) 1049 goto failed; 1050 goto done; 1051 } 1052 1053 if (func_return(&ectx) == FAIL) 1054 goto failed; 1055 } 1056 continue; 1057 } 1058 1059 iptr = &ectx.ec_instr[ectx.ec_iidx++]; 1060 switch (iptr->isn_type) 1061 { 1062 // execute Ex command line 1063 case ISN_EXEC: 1064 SOURCING_LNUM = iptr->isn_lnum; 1065 do_cmdline_cmd(iptr->isn_arg.string); 1066 break; 1067 1068 // execute Ex command from pieces on the stack 1069 case ISN_EXECCONCAT: 1070 { 1071 int count = iptr->isn_arg.number; 1072 size_t len = 0; 1073 int pass; 1074 int i; 1075 char_u *cmd = NULL; 1076 char_u *str; 1077 1078 for (pass = 1; pass <= 2; ++pass) 1079 { 1080 for (i = 0; i < count; ++i) 1081 { 1082 tv = STACK_TV_BOT(i - count); 1083 str = tv->vval.v_string; 1084 if (str != NULL && *str != NUL) 1085 { 1086 if (pass == 2) 1087 STRCPY(cmd + len, str); 1088 len += STRLEN(str); 1089 } 1090 if (pass == 2) 1091 clear_tv(tv); 1092 } 1093 if (pass == 1) 1094 { 1095 cmd = alloc(len + 1); 1096 if (cmd == NULL) 1097 goto failed; 1098 len = 0; 1099 } 1100 } 1101 1102 SOURCING_LNUM = iptr->isn_lnum; 1103 do_cmdline_cmd(cmd); 1104 vim_free(cmd); 1105 } 1106 break; 1107 1108 // execute :echo {string} ... 1109 case ISN_ECHO: 1110 { 1111 int count = iptr->isn_arg.echo.echo_count; 1112 int atstart = TRUE; 1113 int needclr = TRUE; 1114 1115 for (idx = 0; idx < count; ++idx) 1116 { 1117 tv = STACK_TV_BOT(idx - count); 1118 echo_one(tv, iptr->isn_arg.echo.echo_with_white, 1119 &atstart, &needclr); 1120 clear_tv(tv); 1121 } 1122 if (needclr) 1123 msg_clr_eos(); 1124 ectx.ec_stack.ga_len -= count; 1125 } 1126 break; 1127 1128 // :execute {string} ... 1129 // :echomsg {string} ... 1130 // :echoerr {string} ... 1131 case ISN_EXECUTE: 1132 case ISN_ECHOMSG: 1133 case ISN_ECHOERR: 1134 { 1135 int count = iptr->isn_arg.number; 1136 garray_T ga; 1137 char_u buf[NUMBUFLEN]; 1138 char_u *p; 1139 int len; 1140 int failed = FALSE; 1141 1142 ga_init2(&ga, 1, 80); 1143 for (idx = 0; idx < count; ++idx) 1144 { 1145 tv = STACK_TV_BOT(idx - count); 1146 if (iptr->isn_type == ISN_EXECUTE) 1147 { 1148 if (tv->v_type == VAR_CHANNEL 1149 || tv->v_type == VAR_JOB) 1150 { 1151 SOURCING_LNUM = iptr->isn_lnum; 1152 emsg(_(e_inval_string)); 1153 break; 1154 } 1155 else 1156 p = tv_get_string_buf(tv, buf); 1157 } 1158 else 1159 p = tv_stringify(tv, buf); 1160 1161 len = (int)STRLEN(p); 1162 if (ga_grow(&ga, len + 2) == FAIL) 1163 failed = TRUE; 1164 else 1165 { 1166 if (ga.ga_len > 0) 1167 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; 1168 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); 1169 ga.ga_len += len; 1170 } 1171 clear_tv(tv); 1172 } 1173 ectx.ec_stack.ga_len -= count; 1174 if (failed) 1175 goto on_error; 1176 1177 if (ga.ga_data != NULL) 1178 { 1179 if (iptr->isn_type == ISN_EXECUTE) 1180 { 1181 SOURCING_LNUM = iptr->isn_lnum; 1182 do_cmdline_cmd((char_u *)ga.ga_data); 1183 } 1184 else 1185 { 1186 msg_sb_eol(); 1187 if (iptr->isn_type == ISN_ECHOMSG) 1188 { 1189 msg_attr(ga.ga_data, echo_attr); 1190 out_flush(); 1191 } 1192 else 1193 { 1194 SOURCING_LNUM = iptr->isn_lnum; 1195 emsg(ga.ga_data); 1196 } 1197 } 1198 } 1199 ga_clear(&ga); 1200 } 1201 break; 1202 1203 // load local variable or argument 1204 case ISN_LOAD: 1205 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1206 goto failed; 1207 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0)); 1208 ++ectx.ec_stack.ga_len; 1209 break; 1210 1211 // load variable or argument from outer scope 1212 case ISN_LOADOUTER: 1213 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1214 goto failed; 1215 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number), 1216 STACK_TV_BOT(0)); 1217 ++ectx.ec_stack.ga_len; 1218 break; 1219 1220 // load v: variable 1221 case ISN_LOADV: 1222 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1223 goto failed; 1224 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0)); 1225 ++ectx.ec_stack.ga_len; 1226 break; 1227 1228 // load s: variable in Vim9 script 1229 case ISN_LOADSCRIPT: 1230 { 1231 scriptitem_T *si = 1232 SCRIPT_ITEM(iptr->isn_arg.script.script_sid); 1233 svar_T *sv; 1234 1235 sv = ((svar_T *)si->sn_var_vals.ga_data) 1236 + iptr->isn_arg.script.script_idx; 1237 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1238 goto failed; 1239 copy_tv(sv->sv_tv, STACK_TV_BOT(0)); 1240 ++ectx.ec_stack.ga_len; 1241 } 1242 break; 1243 1244 // load s: variable in old script 1245 case ISN_LOADS: 1246 { 1247 hashtab_T *ht = &SCRIPT_VARS( 1248 iptr->isn_arg.loadstore.ls_sid); 1249 char_u *name = iptr->isn_arg.loadstore.ls_name; 1250 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE); 1251 1252 if (di == NULL) 1253 { 1254 SOURCING_LNUM = iptr->isn_lnum; 1255 semsg(_(e_undefined_variable_str), name); 1256 goto on_error; 1257 } 1258 else 1259 { 1260 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1261 goto failed; 1262 copy_tv(&di->di_tv, STACK_TV_BOT(0)); 1263 ++ectx.ec_stack.ga_len; 1264 } 1265 } 1266 break; 1267 1268 // load g:/b:/w:/t: variable 1269 case ISN_LOADG: 1270 case ISN_LOADB: 1271 case ISN_LOADW: 1272 case ISN_LOADT: 1273 { 1274 dictitem_T *di = NULL; 1275 hashtab_T *ht = NULL; 1276 char namespace; 1277 1278 switch (iptr->isn_type) 1279 { 1280 case ISN_LOADG: 1281 ht = get_globvar_ht(); 1282 namespace = 'g'; 1283 break; 1284 case ISN_LOADB: 1285 ht = &curbuf->b_vars->dv_hashtab; 1286 namespace = 'b'; 1287 break; 1288 case ISN_LOADW: 1289 ht = &curwin->w_vars->dv_hashtab; 1290 namespace = 'w'; 1291 break; 1292 case ISN_LOADT: 1293 ht = &curtab->tp_vars->dv_hashtab; 1294 namespace = 't'; 1295 break; 1296 default: // Cannot reach here 1297 goto failed; 1298 } 1299 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE); 1300 1301 if (di == NULL) 1302 { 1303 SOURCING_LNUM = iptr->isn_lnum; 1304 semsg(_(e_undefined_variable_char_str), 1305 namespace, iptr->isn_arg.string); 1306 goto on_error; 1307 } 1308 else 1309 { 1310 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1311 goto failed; 1312 copy_tv(&di->di_tv, STACK_TV_BOT(0)); 1313 ++ectx.ec_stack.ga_len; 1314 } 1315 } 1316 break; 1317 1318 // load g:/b:/w:/t: namespace 1319 case ISN_LOADGDICT: 1320 case ISN_LOADBDICT: 1321 case ISN_LOADWDICT: 1322 case ISN_LOADTDICT: 1323 { 1324 dict_T *d = NULL; 1325 1326 switch (iptr->isn_type) 1327 { 1328 case ISN_LOADGDICT: d = get_globvar_dict(); break; 1329 case ISN_LOADBDICT: d = curbuf->b_vars; break; 1330 case ISN_LOADWDICT: d = curwin->w_vars; break; 1331 case ISN_LOADTDICT: d = curtab->tp_vars; break; 1332 default: // Cannot reach here 1333 goto failed; 1334 } 1335 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1336 goto failed; 1337 tv = STACK_TV_BOT(0); 1338 tv->v_type = VAR_DICT; 1339 tv->v_lock = 0; 1340 tv->vval.v_dict = d; 1341 ++ectx.ec_stack.ga_len; 1342 } 1343 break; 1344 1345 // load &option 1346 case ISN_LOADOPT: 1347 { 1348 typval_T optval; 1349 char_u *name = iptr->isn_arg.string; 1350 1351 // This is not expected to fail, name is checked during 1352 // compilation: don't set SOURCING_LNUM. 1353 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1354 goto failed; 1355 if (eval_option(&name, &optval, TRUE) == FAIL) 1356 goto failed; 1357 *STACK_TV_BOT(0) = optval; 1358 ++ectx.ec_stack.ga_len; 1359 } 1360 break; 1361 1362 // load $ENV 1363 case ISN_LOADENV: 1364 { 1365 typval_T optval; 1366 char_u *name = iptr->isn_arg.string; 1367 1368 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1369 goto failed; 1370 // name is always valid, checked when compiling 1371 (void)eval_env_var(&name, &optval, TRUE); 1372 *STACK_TV_BOT(0) = optval; 1373 ++ectx.ec_stack.ga_len; 1374 } 1375 break; 1376 1377 // load @register 1378 case ISN_LOADREG: 1379 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1380 goto failed; 1381 tv = STACK_TV_BOT(0); 1382 tv->v_type = VAR_STRING; 1383 tv->v_lock = 0; 1384 tv->vval.v_string = get_reg_contents( 1385 iptr->isn_arg.number, GREG_EXPR_SRC); 1386 ++ectx.ec_stack.ga_len; 1387 break; 1388 1389 // store local variable 1390 case ISN_STORE: 1391 --ectx.ec_stack.ga_len; 1392 tv = STACK_TV_VAR(iptr->isn_arg.number); 1393 clear_tv(tv); 1394 *tv = *STACK_TV_BOT(0); 1395 break; 1396 1397 // store variable or argument in outer scope 1398 case ISN_STOREOUTER: 1399 --ectx.ec_stack.ga_len; 1400 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number); 1401 clear_tv(tv); 1402 *tv = *STACK_TV_BOT(0); 1403 break; 1404 1405 // store s: variable in old script 1406 case ISN_STORES: 1407 { 1408 hashtab_T *ht = &SCRIPT_VARS( 1409 iptr->isn_arg.loadstore.ls_sid); 1410 char_u *name = iptr->isn_arg.loadstore.ls_name; 1411 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE); 1412 1413 --ectx.ec_stack.ga_len; 1414 if (di == NULL) 1415 store_var(name, STACK_TV_BOT(0)); 1416 else 1417 { 1418 clear_tv(&di->di_tv); 1419 di->di_tv = *STACK_TV_BOT(0); 1420 } 1421 } 1422 break; 1423 1424 // store script-local variable in Vim9 script 1425 case ISN_STORESCRIPT: 1426 { 1427 scriptitem_T *si = SCRIPT_ITEM( 1428 iptr->isn_arg.script.script_sid); 1429 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 1430 + iptr->isn_arg.script.script_idx; 1431 1432 --ectx.ec_stack.ga_len; 1433 clear_tv(sv->sv_tv); 1434 *sv->sv_tv = *STACK_TV_BOT(0); 1435 } 1436 break; 1437 1438 // store option 1439 case ISN_STOREOPT: 1440 { 1441 long n = 0; 1442 char_u *s = NULL; 1443 char *msg; 1444 1445 --ectx.ec_stack.ga_len; 1446 tv = STACK_TV_BOT(0); 1447 if (tv->v_type == VAR_STRING) 1448 { 1449 s = tv->vval.v_string; 1450 if (s == NULL) 1451 s = (char_u *)""; 1452 } 1453 else 1454 // must be VAR_NUMBER, CHECKTYPE makes sure 1455 n = tv->vval.v_number; 1456 msg = set_option_value(iptr->isn_arg.storeopt.so_name, 1457 n, s, iptr->isn_arg.storeopt.so_flags); 1458 clear_tv(tv); 1459 if (msg != NULL) 1460 { 1461 SOURCING_LNUM = iptr->isn_lnum; 1462 emsg(_(msg)); 1463 goto on_error; 1464 } 1465 } 1466 break; 1467 1468 // store $ENV 1469 case ISN_STOREENV: 1470 --ectx.ec_stack.ga_len; 1471 tv = STACK_TV_BOT(0); 1472 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv)); 1473 clear_tv(tv); 1474 break; 1475 1476 // store @r 1477 case ISN_STOREREG: 1478 { 1479 int reg = iptr->isn_arg.number; 1480 1481 --ectx.ec_stack.ga_len; 1482 tv = STACK_TV_BOT(0); 1483 write_reg_contents(reg == '@' ? '"' : reg, 1484 tv_get_string(tv), -1, FALSE); 1485 clear_tv(tv); 1486 } 1487 break; 1488 1489 // store v: variable 1490 case ISN_STOREV: 1491 --ectx.ec_stack.ga_len; 1492 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0)) 1493 == FAIL) 1494 // should not happen, type is checked when compiling 1495 goto on_error; 1496 break; 1497 1498 // store g:/b:/w:/t: variable 1499 case ISN_STOREG: 1500 case ISN_STOREB: 1501 case ISN_STOREW: 1502 case ISN_STORET: 1503 { 1504 dictitem_T *di; 1505 hashtab_T *ht; 1506 switch (iptr->isn_type) 1507 { 1508 case ISN_STOREG: 1509 ht = get_globvar_ht(); 1510 break; 1511 case ISN_STOREB: 1512 ht = &curbuf->b_vars->dv_hashtab; 1513 break; 1514 case ISN_STOREW: 1515 ht = &curwin->w_vars->dv_hashtab; 1516 break; 1517 case ISN_STORET: 1518 ht = &curtab->tp_vars->dv_hashtab; 1519 break; 1520 default: // Cannot reach here 1521 goto failed; 1522 } 1523 1524 --ectx.ec_stack.ga_len; 1525 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE); 1526 if (di == NULL) 1527 store_var(iptr->isn_arg.string, STACK_TV_BOT(0)); 1528 else 1529 { 1530 clear_tv(&di->di_tv); 1531 di->di_tv = *STACK_TV_BOT(0); 1532 } 1533 } 1534 break; 1535 1536 // store number in local variable 1537 case ISN_STORENR: 1538 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx); 1539 clear_tv(tv); 1540 tv->v_type = VAR_NUMBER; 1541 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val; 1542 break; 1543 1544 // store value in list variable 1545 case ISN_STORELIST: 1546 { 1547 typval_T *tv_idx = STACK_TV_BOT(-2); 1548 varnumber_T lidx = tv_idx->vval.v_number; 1549 typval_T *tv_list = STACK_TV_BOT(-1); 1550 list_T *list = tv_list->vval.v_list; 1551 1552 SOURCING_LNUM = iptr->isn_lnum; 1553 if (lidx < 0 && list->lv_len + lidx >= 0) 1554 // negative index is relative to the end 1555 lidx = list->lv_len + lidx; 1556 if (lidx < 0 || lidx > list->lv_len) 1557 { 1558 semsg(_(e_listidx), lidx); 1559 goto on_error; 1560 } 1561 tv = STACK_TV_BOT(-3); 1562 if (lidx < list->lv_len) 1563 { 1564 listitem_T *li = list_find(list, lidx); 1565 1566 if (error_if_locked(li->li_tv.v_lock, 1567 e_cannot_change_list_item)) 1568 goto failed; 1569 // overwrite existing list item 1570 clear_tv(&li->li_tv); 1571 li->li_tv = *tv; 1572 } 1573 else 1574 { 1575 if (error_if_locked(list->lv_lock, 1576 e_cannot_change_list)) 1577 goto failed; 1578 // append to list, only fails when out of memory 1579 if (list_append_tv(list, tv) == FAIL) 1580 goto failed; 1581 clear_tv(tv); 1582 } 1583 clear_tv(tv_idx); 1584 clear_tv(tv_list); 1585 ectx.ec_stack.ga_len -= 3; 1586 } 1587 break; 1588 1589 // store value in dict variable 1590 case ISN_STOREDICT: 1591 { 1592 typval_T *tv_key = STACK_TV_BOT(-2); 1593 char_u *key = tv_key->vval.v_string; 1594 typval_T *tv_dict = STACK_TV_BOT(-1); 1595 dict_T *dict = tv_dict->vval.v_dict; 1596 dictitem_T *di; 1597 1598 SOURCING_LNUM = iptr->isn_lnum; 1599 if (dict == NULL) 1600 { 1601 emsg(_(e_dictionary_not_set)); 1602 goto on_error; 1603 } 1604 if (key == NULL) 1605 key = (char_u *)""; 1606 tv = STACK_TV_BOT(-3); 1607 di = dict_find(dict, key, -1); 1608 if (di != NULL) 1609 { 1610 if (error_if_locked(di->di_tv.v_lock, 1611 e_cannot_change_dict_item)) 1612 goto failed; 1613 // overwrite existing value 1614 clear_tv(&di->di_tv); 1615 di->di_tv = *tv; 1616 } 1617 else 1618 { 1619 if (error_if_locked(dict->dv_lock, 1620 e_cannot_change_dict)) 1621 goto failed; 1622 // add to dict, only fails when out of memory 1623 if (dict_add_tv(dict, (char *)key, tv) == FAIL) 1624 goto failed; 1625 clear_tv(tv); 1626 } 1627 clear_tv(tv_key); 1628 clear_tv(tv_dict); 1629 ectx.ec_stack.ga_len -= 3; 1630 } 1631 break; 1632 1633 // push constant 1634 case ISN_PUSHNR: 1635 case ISN_PUSHBOOL: 1636 case ISN_PUSHSPEC: 1637 case ISN_PUSHF: 1638 case ISN_PUSHS: 1639 case ISN_PUSHBLOB: 1640 case ISN_PUSHFUNC: 1641 case ISN_PUSHCHANNEL: 1642 case ISN_PUSHJOB: 1643 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1644 goto failed; 1645 tv = STACK_TV_BOT(0); 1646 tv->v_lock = 0; 1647 ++ectx.ec_stack.ga_len; 1648 switch (iptr->isn_type) 1649 { 1650 case ISN_PUSHNR: 1651 tv->v_type = VAR_NUMBER; 1652 tv->vval.v_number = iptr->isn_arg.number; 1653 break; 1654 case ISN_PUSHBOOL: 1655 tv->v_type = VAR_BOOL; 1656 tv->vval.v_number = iptr->isn_arg.number; 1657 break; 1658 case ISN_PUSHSPEC: 1659 tv->v_type = VAR_SPECIAL; 1660 tv->vval.v_number = iptr->isn_arg.number; 1661 break; 1662 #ifdef FEAT_FLOAT 1663 case ISN_PUSHF: 1664 tv->v_type = VAR_FLOAT; 1665 tv->vval.v_float = iptr->isn_arg.fnumber; 1666 break; 1667 #endif 1668 case ISN_PUSHBLOB: 1669 blob_copy(iptr->isn_arg.blob, tv); 1670 break; 1671 case ISN_PUSHFUNC: 1672 tv->v_type = VAR_FUNC; 1673 if (iptr->isn_arg.string == NULL) 1674 tv->vval.v_string = NULL; 1675 else 1676 tv->vval.v_string = 1677 vim_strsave(iptr->isn_arg.string); 1678 break; 1679 case ISN_PUSHCHANNEL: 1680 #ifdef FEAT_JOB_CHANNEL 1681 tv->v_type = VAR_CHANNEL; 1682 tv->vval.v_channel = iptr->isn_arg.channel; 1683 if (tv->vval.v_channel != NULL) 1684 ++tv->vval.v_channel->ch_refcount; 1685 #endif 1686 break; 1687 case ISN_PUSHJOB: 1688 #ifdef FEAT_JOB_CHANNEL 1689 tv->v_type = VAR_JOB; 1690 tv->vval.v_job = iptr->isn_arg.job; 1691 if (tv->vval.v_job != NULL) 1692 ++tv->vval.v_job->jv_refcount; 1693 #endif 1694 break; 1695 default: 1696 tv->v_type = VAR_STRING; 1697 tv->vval.v_string = vim_strsave( 1698 iptr->isn_arg.string == NULL 1699 ? (char_u *)"" : iptr->isn_arg.string); 1700 } 1701 break; 1702 1703 case ISN_UNLET: 1704 if (do_unlet(iptr->isn_arg.unlet.ul_name, 1705 iptr->isn_arg.unlet.ul_forceit) == FAIL) 1706 goto on_error; 1707 break; 1708 case ISN_UNLETENV: 1709 vim_unsetenv(iptr->isn_arg.unlet.ul_name); 1710 break; 1711 1712 case ISN_LOCKCONST: 1713 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE); 1714 break; 1715 1716 // create a list from items on the stack; uses a single allocation 1717 // for the list header and the items 1718 case ISN_NEWLIST: 1719 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL) 1720 goto failed; 1721 break; 1722 1723 // create a dict from items on the stack 1724 case ISN_NEWDICT: 1725 { 1726 int count = iptr->isn_arg.number; 1727 dict_T *dict = dict_alloc(); 1728 dictitem_T *item; 1729 1730 if (dict == NULL) 1731 goto failed; 1732 for (idx = 0; idx < count; ++idx) 1733 { 1734 // have already checked key type is VAR_STRING 1735 tv = STACK_TV_BOT(2 * (idx - count)); 1736 // check key is unique 1737 item = dict_find(dict, tv->vval.v_string, -1); 1738 if (item != NULL) 1739 { 1740 SOURCING_LNUM = iptr->isn_lnum; 1741 semsg(_(e_duplicate_key), tv->vval.v_string); 1742 dict_unref(dict); 1743 goto on_error; 1744 } 1745 item = dictitem_alloc(tv->vval.v_string); 1746 clear_tv(tv); 1747 if (item == NULL) 1748 { 1749 dict_unref(dict); 1750 goto failed; 1751 } 1752 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1); 1753 item->di_tv.v_lock = 0; 1754 if (dict_add(dict, item) == FAIL) 1755 { 1756 // can this ever happen? 1757 dict_unref(dict); 1758 goto failed; 1759 } 1760 } 1761 1762 if (count > 0) 1763 ectx.ec_stack.ga_len -= 2 * count - 1; 1764 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1765 goto failed; 1766 else 1767 ++ectx.ec_stack.ga_len; 1768 tv = STACK_TV_BOT(-1); 1769 tv->v_type = VAR_DICT; 1770 tv->v_lock = 0; 1771 tv->vval.v_dict = dict; 1772 ++dict->dv_refcount; 1773 } 1774 break; 1775 1776 // call a :def function 1777 case ISN_DCALL: 1778 SOURCING_LNUM = iptr->isn_lnum; 1779 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, 1780 iptr->isn_arg.dfunc.cdf_argcount, 1781 &ectx) == FAIL) 1782 goto on_error; 1783 break; 1784 1785 // call a builtin function 1786 case ISN_BCALL: 1787 SOURCING_LNUM = iptr->isn_lnum; 1788 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx, 1789 iptr->isn_arg.bfunc.cbf_argcount, 1790 &ectx) == FAIL) 1791 goto on_error; 1792 break; 1793 1794 // call a funcref or partial 1795 case ISN_PCALL: 1796 { 1797 cpfunc_T *pfunc = &iptr->isn_arg.pfunc; 1798 int r; 1799 typval_T partial_tv; 1800 1801 SOURCING_LNUM = iptr->isn_lnum; 1802 if (pfunc->cpf_top) 1803 { 1804 // funcref is above the arguments 1805 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1); 1806 } 1807 else 1808 { 1809 // Get the funcref from the stack. 1810 --ectx.ec_stack.ga_len; 1811 partial_tv = *STACK_TV_BOT(0); 1812 tv = &partial_tv; 1813 } 1814 r = call_partial(tv, pfunc->cpf_argcount, &ectx); 1815 if (tv == &partial_tv) 1816 clear_tv(&partial_tv); 1817 if (r == FAIL) 1818 goto on_error; 1819 } 1820 break; 1821 1822 case ISN_PCALL_END: 1823 // PCALL finished, arguments have been consumed and replaced by 1824 // the return value. Now clear the funcref from the stack, 1825 // and move the return value in its place. 1826 --ectx.ec_stack.ga_len; 1827 clear_tv(STACK_TV_BOT(-1)); 1828 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0); 1829 break; 1830 1831 // call a user defined function or funcref/partial 1832 case ISN_UCALL: 1833 { 1834 cufunc_T *cufunc = &iptr->isn_arg.ufunc; 1835 1836 SOURCING_LNUM = iptr->isn_lnum; 1837 if (call_eval_func(cufunc->cuf_name, 1838 cufunc->cuf_argcount, &ectx, iptr) == FAIL) 1839 goto on_error; 1840 } 1841 break; 1842 1843 // return from a :def function call 1844 case ISN_RETURN: 1845 { 1846 garray_T *trystack = &ectx.ec_trystack; 1847 trycmd_T *trycmd = NULL; 1848 1849 if (trystack->ga_len > 0) 1850 trycmd = ((trycmd_T *)trystack->ga_data) 1851 + trystack->ga_len - 1; 1852 if (trycmd != NULL 1853 && trycmd->tcd_frame_idx == ectx.ec_frame_idx 1854 && trycmd->tcd_finally_idx != 0) 1855 { 1856 // jump to ":finally" 1857 ectx.ec_iidx = trycmd->tcd_finally_idx; 1858 trycmd->tcd_return = TRUE; 1859 } 1860 else 1861 goto func_return; 1862 } 1863 break; 1864 1865 // push a function reference to a compiled function 1866 case ISN_FUNCREF: 1867 { 1868 partial_T *pt = NULL; 1869 dfunc_T *pt_dfunc; 1870 1871 pt = ALLOC_CLEAR_ONE(partial_T); 1872 if (pt == NULL) 1873 goto failed; 1874 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1875 { 1876 vim_free(pt); 1877 goto failed; 1878 } 1879 pt_dfunc = ((dfunc_T *)def_functions.ga_data) 1880 + iptr->isn_arg.funcref.fr_func; 1881 pt->pt_func = pt_dfunc->df_ufunc; 1882 pt->pt_refcount = 1; 1883 1884 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE) 1885 { 1886 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 1887 + ectx.ec_dfunc_idx; 1888 1889 // The closure needs to find arguments and local 1890 // variables in the current stack. 1891 pt->pt_ectx_stack = &ectx.ec_stack; 1892 pt->pt_ectx_frame = ectx.ec_frame_idx; 1893 1894 // If this function returns and the closure is still 1895 // being used, we need to make a copy of the context 1896 // (arguments and local variables). Store a reference 1897 // to the partial so we can handle that. 1898 if (ga_grow(&ectx.ec_funcrefs, 1) == FAIL) 1899 { 1900 vim_free(pt); 1901 goto failed; 1902 } 1903 // Extra variable keeps the count of closures created 1904 // in the current function call. 1905 tv = STACK_TV_VAR(dfunc->df_varcount); 1906 ++tv->vval.v_number; 1907 1908 ((partial_T **)ectx.ec_funcrefs.ga_data) 1909 [ectx.ec_funcrefs.ga_len] = pt; 1910 ++pt->pt_refcount; 1911 ++ectx.ec_funcrefs.ga_len; 1912 } 1913 ++pt_dfunc->df_ufunc->uf_refcount; 1914 1915 tv = STACK_TV_BOT(0); 1916 ++ectx.ec_stack.ga_len; 1917 tv->vval.v_partial = pt; 1918 tv->v_type = VAR_PARTIAL; 1919 tv->v_lock = 0; 1920 } 1921 break; 1922 1923 // Create a global function from a lambda. 1924 case ISN_NEWFUNC: 1925 { 1926 newfunc_T *newfunc = &iptr->isn_arg.newfunc; 1927 1928 copy_func(newfunc->nf_lambda, newfunc->nf_global); 1929 } 1930 break; 1931 1932 // jump if a condition is met 1933 case ISN_JUMP: 1934 { 1935 jumpwhen_T when = iptr->isn_arg.jump.jump_when; 1936 int error = FALSE; 1937 int jump = TRUE; 1938 1939 if (when != JUMP_ALWAYS) 1940 { 1941 tv = STACK_TV_BOT(-1); 1942 if (when == JUMP_IF_COND_FALSE 1943 || when == JUMP_IF_FALSE 1944 || when == JUMP_IF_COND_TRUE) 1945 { 1946 SOURCING_LNUM = iptr->isn_lnum; 1947 jump = tv_get_bool_chk(tv, &error); 1948 if (error) 1949 goto on_error; 1950 } 1951 else 1952 jump = tv2bool(tv); 1953 if (when == JUMP_IF_FALSE 1954 || when == JUMP_AND_KEEP_IF_FALSE 1955 || when == JUMP_IF_COND_FALSE) 1956 jump = !jump; 1957 if (when == JUMP_IF_FALSE || !jump) 1958 { 1959 // drop the value from the stack 1960 clear_tv(tv); 1961 --ectx.ec_stack.ga_len; 1962 } 1963 } 1964 if (jump) 1965 ectx.ec_iidx = iptr->isn_arg.jump.jump_where; 1966 } 1967 break; 1968 1969 // top of a for loop 1970 case ISN_FOR: 1971 { 1972 list_T *list = STACK_TV_BOT(-1)->vval.v_list; 1973 typval_T *idxtv = 1974 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx); 1975 1976 // push the next item from the list 1977 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1978 goto failed; 1979 ++idxtv->vval.v_number; 1980 if (list == NULL || idxtv->vval.v_number >= list->lv_len) 1981 // past the end of the list, jump to "endfor" 1982 ectx.ec_iidx = iptr->isn_arg.forloop.for_end; 1983 else if (list->lv_first == &range_list_item) 1984 { 1985 // non-materialized range() list 1986 tv = STACK_TV_BOT(0); 1987 tv->v_type = VAR_NUMBER; 1988 tv->v_lock = 0; 1989 tv->vval.v_number = list_find_nr( 1990 list, idxtv->vval.v_number, NULL); 1991 ++ectx.ec_stack.ga_len; 1992 } 1993 else 1994 { 1995 listitem_T *li = list_find(list, idxtv->vval.v_number); 1996 1997 copy_tv(&li->li_tv, STACK_TV_BOT(0)); 1998 ++ectx.ec_stack.ga_len; 1999 } 2000 } 2001 break; 2002 2003 // start of ":try" block 2004 case ISN_TRY: 2005 { 2006 trycmd_T *trycmd = NULL; 2007 2008 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL) 2009 goto failed; 2010 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data) 2011 + ectx.ec_trystack.ga_len; 2012 ++ectx.ec_trystack.ga_len; 2013 ++trylevel; 2014 trycmd->tcd_frame_idx = ectx.ec_frame_idx; 2015 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch; 2016 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally; 2017 trycmd->tcd_caught = FALSE; 2018 trycmd->tcd_return = FALSE; 2019 } 2020 break; 2021 2022 case ISN_PUSHEXC: 2023 if (current_exception == NULL) 2024 { 2025 SOURCING_LNUM = iptr->isn_lnum; 2026 iemsg("Evaluating catch while current_exception is NULL"); 2027 goto failed; 2028 } 2029 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2030 goto failed; 2031 tv = STACK_TV_BOT(0); 2032 ++ectx.ec_stack.ga_len; 2033 tv->v_type = VAR_STRING; 2034 tv->v_lock = 0; 2035 tv->vval.v_string = vim_strsave( 2036 (char_u *)current_exception->value); 2037 break; 2038 2039 case ISN_CATCH: 2040 { 2041 garray_T *trystack = &ectx.ec_trystack; 2042 2043 if (trystack->ga_len > 0) 2044 { 2045 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) 2046 + trystack->ga_len - 1; 2047 trycmd->tcd_caught = TRUE; 2048 } 2049 did_emsg = got_int = did_throw = FALSE; 2050 catch_exception(current_exception); 2051 } 2052 break; 2053 2054 // end of ":try" block 2055 case ISN_ENDTRY: 2056 { 2057 garray_T *trystack = &ectx.ec_trystack; 2058 2059 if (trystack->ga_len > 0) 2060 { 2061 trycmd_T *trycmd = NULL; 2062 2063 --trystack->ga_len; 2064 --trylevel; 2065 ectx.ec_in_catch = FALSE; 2066 trycmd = ((trycmd_T *)trystack->ga_data) 2067 + trystack->ga_len; 2068 if (trycmd->tcd_caught && current_exception != NULL) 2069 { 2070 // discard the exception 2071 if (caught_stack == current_exception) 2072 caught_stack = caught_stack->caught; 2073 discard_current_exception(); 2074 } 2075 2076 if (trycmd->tcd_return) 2077 goto func_return; 2078 } 2079 } 2080 break; 2081 2082 case ISN_THROW: 2083 --ectx.ec_stack.ga_len; 2084 tv = STACK_TV_BOT(0); 2085 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL) 2086 { 2087 vim_free(tv->vval.v_string); 2088 goto failed; 2089 } 2090 did_throw = TRUE; 2091 break; 2092 2093 // compare with special values 2094 case ISN_COMPAREBOOL: 2095 case ISN_COMPARESPECIAL: 2096 { 2097 typval_T *tv1 = STACK_TV_BOT(-2); 2098 typval_T *tv2 = STACK_TV_BOT(-1); 2099 varnumber_T arg1 = tv1->vval.v_number; 2100 varnumber_T arg2 = tv2->vval.v_number; 2101 int res; 2102 2103 switch (iptr->isn_arg.op.op_type) 2104 { 2105 case EXPR_EQUAL: res = arg1 == arg2; break; 2106 case EXPR_NEQUAL: res = arg1 != arg2; break; 2107 default: res = 0; break; 2108 } 2109 2110 --ectx.ec_stack.ga_len; 2111 tv1->v_type = VAR_BOOL; 2112 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; 2113 } 2114 break; 2115 2116 // Operation with two number arguments 2117 case ISN_OPNR: 2118 case ISN_COMPARENR: 2119 { 2120 typval_T *tv1 = STACK_TV_BOT(-2); 2121 typval_T *tv2 = STACK_TV_BOT(-1); 2122 varnumber_T arg1 = tv1->vval.v_number; 2123 varnumber_T arg2 = tv2->vval.v_number; 2124 varnumber_T res; 2125 2126 switch (iptr->isn_arg.op.op_type) 2127 { 2128 case EXPR_MULT: res = arg1 * arg2; break; 2129 case EXPR_DIV: res = arg1 / arg2; break; 2130 case EXPR_REM: res = arg1 % arg2; break; 2131 case EXPR_SUB: res = arg1 - arg2; break; 2132 case EXPR_ADD: res = arg1 + arg2; break; 2133 2134 case EXPR_EQUAL: res = arg1 == arg2; break; 2135 case EXPR_NEQUAL: res = arg1 != arg2; break; 2136 case EXPR_GREATER: res = arg1 > arg2; break; 2137 case EXPR_GEQUAL: res = arg1 >= arg2; break; 2138 case EXPR_SMALLER: res = arg1 < arg2; break; 2139 case EXPR_SEQUAL: res = arg1 <= arg2; break; 2140 default: res = 0; break; 2141 } 2142 2143 --ectx.ec_stack.ga_len; 2144 if (iptr->isn_type == ISN_COMPARENR) 2145 { 2146 tv1->v_type = VAR_BOOL; 2147 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; 2148 } 2149 else 2150 tv1->vval.v_number = res; 2151 } 2152 break; 2153 2154 // Computation with two float arguments 2155 case ISN_OPFLOAT: 2156 case ISN_COMPAREFLOAT: 2157 #ifdef FEAT_FLOAT 2158 { 2159 typval_T *tv1 = STACK_TV_BOT(-2); 2160 typval_T *tv2 = STACK_TV_BOT(-1); 2161 float_T arg1 = tv1->vval.v_float; 2162 float_T arg2 = tv2->vval.v_float; 2163 float_T res = 0; 2164 int cmp = FALSE; 2165 2166 switch (iptr->isn_arg.op.op_type) 2167 { 2168 case EXPR_MULT: res = arg1 * arg2; break; 2169 case EXPR_DIV: res = arg1 / arg2; break; 2170 case EXPR_SUB: res = arg1 - arg2; break; 2171 case EXPR_ADD: res = arg1 + arg2; break; 2172 2173 case EXPR_EQUAL: cmp = arg1 == arg2; break; 2174 case EXPR_NEQUAL: cmp = arg1 != arg2; break; 2175 case EXPR_GREATER: cmp = arg1 > arg2; break; 2176 case EXPR_GEQUAL: cmp = arg1 >= arg2; break; 2177 case EXPR_SMALLER: cmp = arg1 < arg2; break; 2178 case EXPR_SEQUAL: cmp = arg1 <= arg2; break; 2179 default: cmp = 0; break; 2180 } 2181 --ectx.ec_stack.ga_len; 2182 if (iptr->isn_type == ISN_COMPAREFLOAT) 2183 { 2184 tv1->v_type = VAR_BOOL; 2185 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 2186 } 2187 else 2188 tv1->vval.v_float = res; 2189 } 2190 #endif 2191 break; 2192 2193 case ISN_COMPARELIST: 2194 { 2195 typval_T *tv1 = STACK_TV_BOT(-2); 2196 typval_T *tv2 = STACK_TV_BOT(-1); 2197 list_T *arg1 = tv1->vval.v_list; 2198 list_T *arg2 = tv2->vval.v_list; 2199 int cmp = FALSE; 2200 int ic = iptr->isn_arg.op.op_ic; 2201 2202 switch (iptr->isn_arg.op.op_type) 2203 { 2204 case EXPR_EQUAL: cmp = 2205 list_equal(arg1, arg2, ic, FALSE); break; 2206 case EXPR_NEQUAL: cmp = 2207 !list_equal(arg1, arg2, ic, FALSE); break; 2208 case EXPR_IS: cmp = arg1 == arg2; break; 2209 case EXPR_ISNOT: cmp = arg1 != arg2; break; 2210 default: cmp = 0; break; 2211 } 2212 --ectx.ec_stack.ga_len; 2213 clear_tv(tv1); 2214 clear_tv(tv2); 2215 tv1->v_type = VAR_BOOL; 2216 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 2217 } 2218 break; 2219 2220 case ISN_COMPAREBLOB: 2221 { 2222 typval_T *tv1 = STACK_TV_BOT(-2); 2223 typval_T *tv2 = STACK_TV_BOT(-1); 2224 blob_T *arg1 = tv1->vval.v_blob; 2225 blob_T *arg2 = tv2->vval.v_blob; 2226 int cmp = FALSE; 2227 2228 switch (iptr->isn_arg.op.op_type) 2229 { 2230 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break; 2231 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break; 2232 case EXPR_IS: cmp = arg1 == arg2; break; 2233 case EXPR_ISNOT: cmp = arg1 != arg2; break; 2234 default: cmp = 0; break; 2235 } 2236 --ectx.ec_stack.ga_len; 2237 clear_tv(tv1); 2238 clear_tv(tv2); 2239 tv1->v_type = VAR_BOOL; 2240 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 2241 } 2242 break; 2243 2244 // TODO: handle separately 2245 case ISN_COMPARESTRING: 2246 case ISN_COMPAREDICT: 2247 case ISN_COMPAREFUNC: 2248 case ISN_COMPAREANY: 2249 { 2250 typval_T *tv1 = STACK_TV_BOT(-2); 2251 typval_T *tv2 = STACK_TV_BOT(-1); 2252 exptype_T exptype = iptr->isn_arg.op.op_type; 2253 int ic = iptr->isn_arg.op.op_ic; 2254 2255 SOURCING_LNUM = iptr->isn_lnum; 2256 typval_compare(tv1, tv2, exptype, ic); 2257 clear_tv(tv2); 2258 --ectx.ec_stack.ga_len; 2259 } 2260 break; 2261 2262 case ISN_ADDLIST: 2263 case ISN_ADDBLOB: 2264 { 2265 typval_T *tv1 = STACK_TV_BOT(-2); 2266 typval_T *tv2 = STACK_TV_BOT(-1); 2267 2268 if (iptr->isn_type == ISN_ADDLIST) 2269 eval_addlist(tv1, tv2); 2270 else 2271 eval_addblob(tv1, tv2); 2272 clear_tv(tv2); 2273 --ectx.ec_stack.ga_len; 2274 } 2275 break; 2276 2277 // Computation with two arguments of unknown type 2278 case ISN_OPANY: 2279 { 2280 typval_T *tv1 = STACK_TV_BOT(-2); 2281 typval_T *tv2 = STACK_TV_BOT(-1); 2282 varnumber_T n1, n2; 2283 #ifdef FEAT_FLOAT 2284 float_T f1 = 0, f2 = 0; 2285 #endif 2286 int error = FALSE; 2287 2288 if (iptr->isn_arg.op.op_type == EXPR_ADD) 2289 { 2290 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST) 2291 { 2292 eval_addlist(tv1, tv2); 2293 clear_tv(tv2); 2294 --ectx.ec_stack.ga_len; 2295 break; 2296 } 2297 else if (tv1->v_type == VAR_BLOB 2298 && tv2->v_type == VAR_BLOB) 2299 { 2300 eval_addblob(tv1, tv2); 2301 clear_tv(tv2); 2302 --ectx.ec_stack.ga_len; 2303 break; 2304 } 2305 } 2306 #ifdef FEAT_FLOAT 2307 if (tv1->v_type == VAR_FLOAT) 2308 { 2309 f1 = tv1->vval.v_float; 2310 n1 = 0; 2311 } 2312 else 2313 #endif 2314 { 2315 n1 = tv_get_number_chk(tv1, &error); 2316 if (error) 2317 goto on_error; 2318 #ifdef FEAT_FLOAT 2319 if (tv2->v_type == VAR_FLOAT) 2320 f1 = n1; 2321 #endif 2322 } 2323 #ifdef FEAT_FLOAT 2324 if (tv2->v_type == VAR_FLOAT) 2325 { 2326 f2 = tv2->vval.v_float; 2327 n2 = 0; 2328 } 2329 else 2330 #endif 2331 { 2332 n2 = tv_get_number_chk(tv2, &error); 2333 if (error) 2334 goto on_error; 2335 #ifdef FEAT_FLOAT 2336 if (tv1->v_type == VAR_FLOAT) 2337 f2 = n2; 2338 #endif 2339 } 2340 #ifdef FEAT_FLOAT 2341 // if there is a float on either side the result is a float 2342 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT) 2343 { 2344 switch (iptr->isn_arg.op.op_type) 2345 { 2346 case EXPR_MULT: f1 = f1 * f2; break; 2347 case EXPR_DIV: f1 = f1 / f2; break; 2348 case EXPR_SUB: f1 = f1 - f2; break; 2349 case EXPR_ADD: f1 = f1 + f2; break; 2350 default: SOURCING_LNUM = iptr->isn_lnum; 2351 emsg(_(e_modulus)); 2352 goto on_error; 2353 } 2354 clear_tv(tv1); 2355 clear_tv(tv2); 2356 tv1->v_type = VAR_FLOAT; 2357 tv1->vval.v_float = f1; 2358 --ectx.ec_stack.ga_len; 2359 } 2360 else 2361 #endif 2362 { 2363 switch (iptr->isn_arg.op.op_type) 2364 { 2365 case EXPR_MULT: n1 = n1 * n2; break; 2366 case EXPR_DIV: n1 = num_divide(n1, n2); break; 2367 case EXPR_SUB: n1 = n1 - n2; break; 2368 case EXPR_ADD: n1 = n1 + n2; break; 2369 default: n1 = num_modulus(n1, n2); break; 2370 } 2371 clear_tv(tv1); 2372 clear_tv(tv2); 2373 tv1->v_type = VAR_NUMBER; 2374 tv1->vval.v_number = n1; 2375 --ectx.ec_stack.ga_len; 2376 } 2377 } 2378 break; 2379 2380 case ISN_CONCAT: 2381 { 2382 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string; 2383 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string; 2384 char_u *res; 2385 2386 res = concat_str(str1, str2); 2387 clear_tv(STACK_TV_BOT(-2)); 2388 clear_tv(STACK_TV_BOT(-1)); 2389 --ectx.ec_stack.ga_len; 2390 STACK_TV_BOT(-1)->vval.v_string = res; 2391 } 2392 break; 2393 2394 case ISN_STRINDEX: 2395 case ISN_STRSLICE: 2396 { 2397 int is_slice = iptr->isn_type == ISN_STRSLICE; 2398 varnumber_T n1 = 0, n2; 2399 char_u *res; 2400 2401 // string index: string is at stack-2, index at stack-1 2402 // string slice: string is at stack-3, first index at 2403 // stack-2, second index at stack-1 2404 if (is_slice) 2405 { 2406 tv = STACK_TV_BOT(-2); 2407 n1 = tv->vval.v_number; 2408 } 2409 2410 tv = STACK_TV_BOT(-1); 2411 n2 = tv->vval.v_number; 2412 2413 ectx.ec_stack.ga_len -= is_slice ? 2 : 1; 2414 tv = STACK_TV_BOT(-1); 2415 if (is_slice) 2416 // Slice: Select the characters from the string 2417 res = string_slice(tv->vval.v_string, n1, n2); 2418 else 2419 // Index: The resulting variable is a string of a 2420 // single character. If the index is too big or 2421 // negative the result is empty. 2422 res = char_from_string(tv->vval.v_string, n2); 2423 vim_free(tv->vval.v_string); 2424 tv->vval.v_string = res; 2425 } 2426 break; 2427 2428 case ISN_LISTINDEX: 2429 case ISN_LISTSLICE: 2430 { 2431 int is_slice = iptr->isn_type == ISN_LISTSLICE; 2432 list_T *list; 2433 varnumber_T n1, n2; 2434 2435 // list index: list is at stack-2, index at stack-1 2436 // list slice: list is at stack-3, indexes at stack-2 and 2437 // stack-1 2438 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2); 2439 list = tv->vval.v_list; 2440 2441 tv = STACK_TV_BOT(-1); 2442 n1 = n2 = tv->vval.v_number; 2443 clear_tv(tv); 2444 2445 if (is_slice) 2446 { 2447 tv = STACK_TV_BOT(-2); 2448 n1 = tv->vval.v_number; 2449 clear_tv(tv); 2450 } 2451 2452 ectx.ec_stack.ga_len -= is_slice ? 2 : 1; 2453 tv = STACK_TV_BOT(-1); 2454 SOURCING_LNUM = iptr->isn_lnum; 2455 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE) 2456 == FAIL) 2457 goto on_error; 2458 } 2459 break; 2460 2461 case ISN_ANYINDEX: 2462 case ISN_ANYSLICE: 2463 { 2464 int is_slice = iptr->isn_type == ISN_ANYSLICE; 2465 typval_T *var1, *var2; 2466 int res; 2467 2468 // index: composite is at stack-2, index at stack-1 2469 // slice: composite is at stack-3, indexes at stack-2 and 2470 // stack-1 2471 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2); 2472 SOURCING_LNUM = iptr->isn_lnum; 2473 if (check_can_index(tv, TRUE, TRUE) == FAIL) 2474 goto on_error; 2475 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1); 2476 var2 = is_slice ? STACK_TV_BOT(-1) : NULL; 2477 res = eval_index_inner(tv, is_slice, 2478 var1, var2, NULL, -1, TRUE); 2479 clear_tv(var1); 2480 if (is_slice) 2481 clear_tv(var2); 2482 ectx.ec_stack.ga_len -= is_slice ? 2 : 1; 2483 if (res == FAIL) 2484 goto on_error; 2485 } 2486 break; 2487 2488 case ISN_SLICE: 2489 { 2490 list_T *list; 2491 int count = iptr->isn_arg.number; 2492 2493 // type will have been checked to be a list 2494 tv = STACK_TV_BOT(-1); 2495 list = tv->vval.v_list; 2496 2497 // no error for short list, expect it to be checked earlier 2498 if (list != NULL && list->lv_len >= count) 2499 { 2500 list_T *newlist = list_slice(list, 2501 count, list->lv_len - 1); 2502 2503 if (newlist != NULL) 2504 { 2505 list_unref(list); 2506 tv->vval.v_list = newlist; 2507 ++newlist->lv_refcount; 2508 } 2509 } 2510 } 2511 break; 2512 2513 case ISN_GETITEM: 2514 { 2515 listitem_T *li; 2516 int index = iptr->isn_arg.number; 2517 2518 // Get list item: list is at stack-1, push item. 2519 // List type and length is checked for when compiling. 2520 tv = STACK_TV_BOT(-1); 2521 li = list_find(tv->vval.v_list, index); 2522 2523 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2524 goto failed; 2525 ++ectx.ec_stack.ga_len; 2526 copy_tv(&li->li_tv, STACK_TV_BOT(-1)); 2527 } 2528 break; 2529 2530 case ISN_MEMBER: 2531 { 2532 dict_T *dict; 2533 char_u *key; 2534 dictitem_T *di; 2535 typval_T temp_tv; 2536 2537 // dict member: dict is at stack-2, key at stack-1 2538 tv = STACK_TV_BOT(-2); 2539 // no need to check for VAR_DICT, CHECKTYPE will check. 2540 dict = tv->vval.v_dict; 2541 2542 tv = STACK_TV_BOT(-1); 2543 // no need to check for VAR_STRING, 2STRING will check. 2544 key = tv->vval.v_string; 2545 2546 if ((di = dict_find(dict, key, -1)) == NULL) 2547 { 2548 SOURCING_LNUM = iptr->isn_lnum; 2549 semsg(_(e_dictkey), key); 2550 goto on_error; 2551 } 2552 clear_tv(tv); 2553 --ectx.ec_stack.ga_len; 2554 // Clear the dict after getting the item, to avoid that it 2555 // make the item invalid. 2556 tv = STACK_TV_BOT(-1); 2557 temp_tv = *tv; 2558 copy_tv(&di->di_tv, tv); 2559 clear_tv(&temp_tv); 2560 } 2561 break; 2562 2563 // dict member with string key 2564 case ISN_STRINGMEMBER: 2565 { 2566 dict_T *dict; 2567 dictitem_T *di; 2568 typval_T temp_tv; 2569 2570 tv = STACK_TV_BOT(-1); 2571 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL) 2572 { 2573 SOURCING_LNUM = iptr->isn_lnum; 2574 emsg(_(e_dictreq)); 2575 goto on_error; 2576 } 2577 dict = tv->vval.v_dict; 2578 2579 if ((di = dict_find(dict, iptr->isn_arg.string, -1)) 2580 == NULL) 2581 { 2582 SOURCING_LNUM = iptr->isn_lnum; 2583 semsg(_(e_dictkey), iptr->isn_arg.string); 2584 goto on_error; 2585 } 2586 // Clear the dict after getting the item, to avoid that it 2587 // make the item invalid. 2588 temp_tv = *tv; 2589 copy_tv(&di->di_tv, tv); 2590 clear_tv(&temp_tv); 2591 } 2592 break; 2593 2594 case ISN_NEGATENR: 2595 tv = STACK_TV_BOT(-1); 2596 if (tv->v_type != VAR_NUMBER 2597 #ifdef FEAT_FLOAT 2598 && tv->v_type != VAR_FLOAT 2599 #endif 2600 ) 2601 { 2602 SOURCING_LNUM = iptr->isn_lnum; 2603 emsg(_(e_number_exp)); 2604 goto on_error; 2605 } 2606 #ifdef FEAT_FLOAT 2607 if (tv->v_type == VAR_FLOAT) 2608 tv->vval.v_float = -tv->vval.v_float; 2609 else 2610 #endif 2611 tv->vval.v_number = -tv->vval.v_number; 2612 break; 2613 2614 case ISN_CHECKNR: 2615 { 2616 int error = FALSE; 2617 2618 tv = STACK_TV_BOT(-1); 2619 SOURCING_LNUM = iptr->isn_lnum; 2620 if (check_not_string(tv) == FAIL) 2621 goto on_error; 2622 (void)tv_get_number_chk(tv, &error); 2623 if (error) 2624 goto on_error; 2625 } 2626 break; 2627 2628 case ISN_CHECKTYPE: 2629 { 2630 checktype_T *ct = &iptr->isn_arg.type; 2631 2632 tv = STACK_TV_BOT(ct->ct_off); 2633 SOURCING_LNUM = iptr->isn_lnum; 2634 if (check_typval_type(ct->ct_type, tv, 0) == FAIL) 2635 goto on_error; 2636 2637 // number 0 is FALSE, number 1 is TRUE 2638 if (tv->v_type == VAR_NUMBER 2639 && ct->ct_type->tt_type == VAR_BOOL 2640 && (tv->vval.v_number == 0 2641 || tv->vval.v_number == 1)) 2642 { 2643 tv->v_type = VAR_BOOL; 2644 tv->vval.v_number = tv->vval.v_number 2645 ? VVAL_TRUE : VVAL_FALSE; 2646 } 2647 } 2648 break; 2649 2650 case ISN_CHECKLEN: 2651 { 2652 int min_len = iptr->isn_arg.checklen.cl_min_len; 2653 list_T *list = NULL; 2654 2655 tv = STACK_TV_BOT(-1); 2656 if (tv->v_type == VAR_LIST) 2657 list = tv->vval.v_list; 2658 if (list == NULL || list->lv_len < min_len 2659 || (list->lv_len > min_len 2660 && !iptr->isn_arg.checklen.cl_more_OK)) 2661 { 2662 SOURCING_LNUM = iptr->isn_lnum; 2663 semsg(_(e_expected_nr_items_but_got_nr), 2664 min_len, list == NULL ? 0 : list->lv_len); 2665 goto on_error; 2666 } 2667 } 2668 break; 2669 2670 case ISN_2BOOL: 2671 case ISN_COND2BOOL: 2672 { 2673 int n; 2674 int error = FALSE; 2675 2676 tv = STACK_TV_BOT(-1); 2677 if (iptr->isn_type == ISN_2BOOL) 2678 { 2679 n = tv2bool(tv); 2680 if (iptr->isn_arg.number) // invert 2681 n = !n; 2682 } 2683 else 2684 { 2685 SOURCING_LNUM = iptr->isn_lnum; 2686 n = tv_get_bool_chk(tv, &error); 2687 if (error) 2688 goto on_error; 2689 } 2690 clear_tv(tv); 2691 tv->v_type = VAR_BOOL; 2692 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE; 2693 } 2694 break; 2695 2696 case ISN_2STRING: 2697 case ISN_2STRING_ANY: 2698 { 2699 char_u *str; 2700 2701 tv = STACK_TV_BOT(iptr->isn_arg.number); 2702 if (tv->v_type != VAR_STRING) 2703 { 2704 if (iptr->isn_type == ISN_2STRING_ANY) 2705 { 2706 switch (tv->v_type) 2707 { 2708 case VAR_SPECIAL: 2709 case VAR_BOOL: 2710 case VAR_NUMBER: 2711 case VAR_FLOAT: 2712 case VAR_BLOB: break; 2713 default: to_string_error(tv->v_type); 2714 goto on_error; 2715 } 2716 } 2717 str = typval_tostring(tv); 2718 clear_tv(tv); 2719 tv->v_type = VAR_STRING; 2720 tv->vval.v_string = str; 2721 } 2722 } 2723 break; 2724 2725 case ISN_PUT: 2726 { 2727 int regname = iptr->isn_arg.put.put_regname; 2728 linenr_T lnum = iptr->isn_arg.put.put_lnum; 2729 char_u *expr = NULL; 2730 int dir = FORWARD; 2731 2732 if (regname == '=') 2733 { 2734 tv = STACK_TV_BOT(-1); 2735 if (tv->v_type == VAR_STRING) 2736 expr = tv->vval.v_string; 2737 else 2738 { 2739 expr = typval_tostring(tv); // allocates value 2740 clear_tv(tv); 2741 } 2742 --ectx.ec_stack.ga_len; 2743 } 2744 if (lnum == -2) 2745 // :put! above cursor 2746 dir = BACKWARD; 2747 else if (lnum >= 0) 2748 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum; 2749 check_cursor(); 2750 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE); 2751 vim_free(expr); 2752 } 2753 break; 2754 2755 case ISN_SHUFFLE: 2756 { 2757 typval_T tmp_tv; 2758 int item = iptr->isn_arg.shuffle.shfl_item; 2759 int up = iptr->isn_arg.shuffle.shfl_up; 2760 2761 tmp_tv = *STACK_TV_BOT(-item); 2762 for ( ; up > 0 && item > 1; --up) 2763 { 2764 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1); 2765 --item; 2766 } 2767 *STACK_TV_BOT(-item) = tmp_tv; 2768 } 2769 break; 2770 2771 case ISN_DROP: 2772 --ectx.ec_stack.ga_len; 2773 clear_tv(STACK_TV_BOT(0)); 2774 break; 2775 } 2776 continue; 2777 2778 func_return: 2779 // Restore previous function. If the frame pointer is where we started 2780 // then there is none and we are done. 2781 if (ectx.ec_frame_idx == initial_frame_idx) 2782 goto done; 2783 2784 if (func_return(&ectx) == FAIL) 2785 // only fails when out of memory 2786 goto failed; 2787 continue; 2788 2789 on_error: 2790 if (trylevel == 0) 2791 goto failed; 2792 } 2793 2794 done: 2795 // function finished, get result from the stack. 2796 tv = STACK_TV_BOT(-1); 2797 *rettv = *tv; 2798 tv->v_type = VAR_UNKNOWN; 2799 ret = OK; 2800 2801 failed: 2802 // When failed need to unwind the call stack. 2803 while (ectx.ec_frame_idx != initial_frame_idx) 2804 func_return(&ectx); 2805 2806 // Deal with any remaining closures, they may be in use somewhere. 2807 if (ectx.ec_funcrefs.ga_len > 0) 2808 handle_closure_in_use(&ectx, FALSE); 2809 2810 estack_pop(); 2811 current_sctx = save_current_sctx; 2812 2813 failed_early: 2814 // Free all local variables, but not arguments. 2815 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx) 2816 clear_tv(STACK_TV(idx)); 2817 2818 vim_free(ectx.ec_stack.ga_data); 2819 vim_free(ectx.ec_trystack.ga_data); 2820 2821 // Not sure if this is necessary. 2822 suppress_errthrow = save_suppress_errthrow; 2823 2824 if (ret != OK && called_emsg == called_emsg_before) 2825 semsg(_(e_unknown_error_while_executing_str), 2826 printable_func_name(ufunc)); 2827 return ret; 2828 } 2829 2830 /* 2831 * ":dissassemble". 2832 * We don't really need this at runtime, but we do have tests that require it, 2833 * so always include this. 2834 */ 2835 void 2836 ex_disassemble(exarg_T *eap) 2837 { 2838 char_u *arg = eap->arg; 2839 char_u *fname; 2840 ufunc_T *ufunc; 2841 dfunc_T *dfunc; 2842 isn_T *instr; 2843 int current; 2844 int line_idx = 0; 2845 int prev_current = 0; 2846 int is_global = FALSE; 2847 2848 if (STRNCMP(arg, "<lambda>", 8) == 0) 2849 { 2850 arg += 8; 2851 (void)getdigits(&arg); 2852 fname = vim_strnsave(eap->arg, arg - eap->arg); 2853 } 2854 else 2855 fname = trans_function_name(&arg, &is_global, FALSE, 2856 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL); 2857 if (fname == NULL) 2858 { 2859 semsg(_(e_invarg2), eap->arg); 2860 return; 2861 } 2862 2863 ufunc = find_func(fname, is_global, NULL); 2864 if (ufunc == NULL) 2865 { 2866 char_u *p = untrans_function_name(fname); 2867 2868 if (p != NULL) 2869 // Try again without making it script-local. 2870 ufunc = find_func(p, FALSE, NULL); 2871 } 2872 vim_free(fname); 2873 if (ufunc == NULL) 2874 { 2875 semsg(_(e_cannot_find_function_str), eap->arg); 2876 return; 2877 } 2878 if (ufunc->uf_def_status == UF_TO_BE_COMPILED 2879 && compile_def_function(ufunc, FALSE, NULL) == FAIL) 2880 return; 2881 if (ufunc->uf_def_status != UF_COMPILED) 2882 { 2883 semsg(_(e_function_is_not_compiled_str), eap->arg); 2884 return; 2885 } 2886 if (ufunc->uf_name_exp != NULL) 2887 msg((char *)ufunc->uf_name_exp); 2888 else 2889 msg((char *)ufunc->uf_name); 2890 2891 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; 2892 instr = dfunc->df_instr; 2893 for (current = 0; current < dfunc->df_instr_count; ++current) 2894 { 2895 isn_T *iptr = &instr[current]; 2896 char *line; 2897 2898 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len) 2899 { 2900 if (current > prev_current) 2901 { 2902 msg_puts("\n\n"); 2903 prev_current = current; 2904 } 2905 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++]; 2906 if (line != NULL) 2907 msg(line); 2908 } 2909 2910 switch (iptr->isn_type) 2911 { 2912 case ISN_EXEC: 2913 smsg("%4d EXEC %s", current, iptr->isn_arg.string); 2914 break; 2915 case ISN_EXECCONCAT: 2916 smsg("%4d EXECCONCAT %lld", current, 2917 (long long)iptr->isn_arg.number); 2918 break; 2919 case ISN_ECHO: 2920 { 2921 echo_T *echo = &iptr->isn_arg.echo; 2922 2923 smsg("%4d %s %d", current, 2924 echo->echo_with_white ? "ECHO" : "ECHON", 2925 echo->echo_count); 2926 } 2927 break; 2928 case ISN_EXECUTE: 2929 smsg("%4d EXECUTE %lld", current, 2930 (long long)(iptr->isn_arg.number)); 2931 break; 2932 case ISN_ECHOMSG: 2933 smsg("%4d ECHOMSG %lld", current, 2934 (long long)(iptr->isn_arg.number)); 2935 break; 2936 case ISN_ECHOERR: 2937 smsg("%4d ECHOERR %lld", current, 2938 (long long)(iptr->isn_arg.number)); 2939 break; 2940 case ISN_LOAD: 2941 case ISN_LOADOUTER: 2942 { 2943 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER"; 2944 2945 if (iptr->isn_arg.number < 0) 2946 smsg("%4d LOAD%s arg[%lld]", current, add, 2947 (long long)(iptr->isn_arg.number 2948 + STACK_FRAME_SIZE)); 2949 else 2950 smsg("%4d LOAD%s $%lld", current, add, 2951 (long long)(iptr->isn_arg.number)); 2952 } 2953 break; 2954 case ISN_LOADV: 2955 smsg("%4d LOADV v:%s", current, 2956 get_vim_var_name(iptr->isn_arg.number)); 2957 break; 2958 case ISN_LOADSCRIPT: 2959 { 2960 scriptitem_T *si = 2961 SCRIPT_ITEM(iptr->isn_arg.script.script_sid); 2962 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 2963 + iptr->isn_arg.script.script_idx; 2964 2965 smsg("%4d LOADSCRIPT %s from %s", current, 2966 sv->sv_name, si->sn_name); 2967 } 2968 break; 2969 case ISN_LOADS: 2970 { 2971 scriptitem_T *si = SCRIPT_ITEM( 2972 iptr->isn_arg.loadstore.ls_sid); 2973 2974 smsg("%4d LOADS s:%s from %s", current, 2975 iptr->isn_arg.loadstore.ls_name, si->sn_name); 2976 } 2977 break; 2978 case ISN_LOADG: 2979 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string); 2980 break; 2981 case ISN_LOADB: 2982 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string); 2983 break; 2984 case ISN_LOADW: 2985 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string); 2986 break; 2987 case ISN_LOADT: 2988 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string); 2989 break; 2990 case ISN_LOADGDICT: 2991 smsg("%4d LOAD g:", current); 2992 break; 2993 case ISN_LOADBDICT: 2994 smsg("%4d LOAD b:", current); 2995 break; 2996 case ISN_LOADWDICT: 2997 smsg("%4d LOAD w:", current); 2998 break; 2999 case ISN_LOADTDICT: 3000 smsg("%4d LOAD t:", current); 3001 break; 3002 case ISN_LOADOPT: 3003 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string); 3004 break; 3005 case ISN_LOADENV: 3006 smsg("%4d LOADENV %s", current, iptr->isn_arg.string); 3007 break; 3008 case ISN_LOADREG: 3009 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number)); 3010 break; 3011 3012 case ISN_STORE: 3013 case ISN_STOREOUTER: 3014 { 3015 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER"; 3016 3017 if (iptr->isn_arg.number < 0) 3018 smsg("%4d STORE%s arg[%lld]", current, add, 3019 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE)); 3020 else 3021 smsg("%4d STORE%s $%lld", current, add, 3022 (long long)(iptr->isn_arg.number)); 3023 } 3024 break; 3025 case ISN_STOREV: 3026 smsg("%4d STOREV v:%s", current, 3027 get_vim_var_name(iptr->isn_arg.number)); 3028 break; 3029 case ISN_STOREG: 3030 smsg("%4d STOREG %s", current, iptr->isn_arg.string); 3031 break; 3032 case ISN_STOREB: 3033 smsg("%4d STOREB %s", current, iptr->isn_arg.string); 3034 break; 3035 case ISN_STOREW: 3036 smsg("%4d STOREW %s", current, iptr->isn_arg.string); 3037 break; 3038 case ISN_STORET: 3039 smsg("%4d STORET %s", current, iptr->isn_arg.string); 3040 break; 3041 case ISN_STORES: 3042 { 3043 scriptitem_T *si = SCRIPT_ITEM( 3044 iptr->isn_arg.loadstore.ls_sid); 3045 3046 smsg("%4d STORES %s in %s", current, 3047 iptr->isn_arg.loadstore.ls_name, si->sn_name); 3048 } 3049 break; 3050 case ISN_STORESCRIPT: 3051 { 3052 scriptitem_T *si = 3053 SCRIPT_ITEM(iptr->isn_arg.script.script_sid); 3054 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 3055 + iptr->isn_arg.script.script_idx; 3056 3057 smsg("%4d STORESCRIPT %s in %s", current, 3058 sv->sv_name, si->sn_name); 3059 } 3060 break; 3061 case ISN_STOREOPT: 3062 smsg("%4d STOREOPT &%s", current, 3063 iptr->isn_arg.storeopt.so_name); 3064 break; 3065 case ISN_STOREENV: 3066 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string); 3067 break; 3068 case ISN_STOREREG: 3069 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number); 3070 break; 3071 case ISN_STORENR: 3072 smsg("%4d STORE %lld in $%d", current, 3073 iptr->isn_arg.storenr.stnr_val, 3074 iptr->isn_arg.storenr.stnr_idx); 3075 break; 3076 3077 case ISN_STORELIST: 3078 smsg("%4d STORELIST", current); 3079 break; 3080 3081 case ISN_STOREDICT: 3082 smsg("%4d STOREDICT", current); 3083 break; 3084 3085 // constants 3086 case ISN_PUSHNR: 3087 smsg("%4d PUSHNR %lld", current, 3088 (long long)(iptr->isn_arg.number)); 3089 break; 3090 case ISN_PUSHBOOL: 3091 case ISN_PUSHSPEC: 3092 smsg("%4d PUSH %s", current, 3093 get_var_special_name(iptr->isn_arg.number)); 3094 break; 3095 case ISN_PUSHF: 3096 #ifdef FEAT_FLOAT 3097 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber); 3098 #endif 3099 break; 3100 case ISN_PUSHS: 3101 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string); 3102 break; 3103 case ISN_PUSHBLOB: 3104 { 3105 char_u *r; 3106 char_u numbuf[NUMBUFLEN]; 3107 char_u *tofree; 3108 3109 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf); 3110 smsg("%4d PUSHBLOB %s", current, r); 3111 vim_free(tofree); 3112 } 3113 break; 3114 case ISN_PUSHFUNC: 3115 { 3116 char *name = (char *)iptr->isn_arg.string; 3117 3118 smsg("%4d PUSHFUNC \"%s\"", current, 3119 name == NULL ? "[none]" : name); 3120 } 3121 break; 3122 case ISN_PUSHCHANNEL: 3123 #ifdef FEAT_JOB_CHANNEL 3124 { 3125 channel_T *channel = iptr->isn_arg.channel; 3126 3127 smsg("%4d PUSHCHANNEL %d", current, 3128 channel == NULL ? 0 : channel->ch_id); 3129 } 3130 #endif 3131 break; 3132 case ISN_PUSHJOB: 3133 #ifdef FEAT_JOB_CHANNEL 3134 { 3135 typval_T tv; 3136 char_u *name; 3137 3138 tv.v_type = VAR_JOB; 3139 tv.vval.v_job = iptr->isn_arg.job; 3140 name = tv_get_string(&tv); 3141 smsg("%4d PUSHJOB \"%s\"", current, name); 3142 } 3143 #endif 3144 break; 3145 case ISN_PUSHEXC: 3146 smsg("%4d PUSH v:exception", current); 3147 break; 3148 case ISN_UNLET: 3149 smsg("%4d UNLET%s %s", current, 3150 iptr->isn_arg.unlet.ul_forceit ? "!" : "", 3151 iptr->isn_arg.unlet.ul_name); 3152 break; 3153 case ISN_UNLETENV: 3154 smsg("%4d UNLETENV%s $%s", current, 3155 iptr->isn_arg.unlet.ul_forceit ? "!" : "", 3156 iptr->isn_arg.unlet.ul_name); 3157 break; 3158 case ISN_LOCKCONST: 3159 smsg("%4d LOCKCONST", current); 3160 break; 3161 case ISN_NEWLIST: 3162 smsg("%4d NEWLIST size %lld", current, 3163 (long long)(iptr->isn_arg.number)); 3164 break; 3165 case ISN_NEWDICT: 3166 smsg("%4d NEWDICT size %lld", current, 3167 (long long)(iptr->isn_arg.number)); 3168 break; 3169 3170 // function call 3171 case ISN_BCALL: 3172 { 3173 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc; 3174 3175 smsg("%4d BCALL %s(argc %d)", current, 3176 internal_func_name(cbfunc->cbf_idx), 3177 cbfunc->cbf_argcount); 3178 } 3179 break; 3180 case ISN_DCALL: 3181 { 3182 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc; 3183 dfunc_T *df = ((dfunc_T *)def_functions.ga_data) 3184 + cdfunc->cdf_idx; 3185 3186 smsg("%4d DCALL %s(argc %d)", current, 3187 df->df_ufunc->uf_name_exp != NULL 3188 ? df->df_ufunc->uf_name_exp 3189 : df->df_ufunc->uf_name, cdfunc->cdf_argcount); 3190 } 3191 break; 3192 case ISN_UCALL: 3193 { 3194 cufunc_T *cufunc = &iptr->isn_arg.ufunc; 3195 3196 smsg("%4d UCALL %s(argc %d)", current, 3197 cufunc->cuf_name, cufunc->cuf_argcount); 3198 } 3199 break; 3200 case ISN_PCALL: 3201 { 3202 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc; 3203 3204 smsg("%4d PCALL%s (argc %d)", current, 3205 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount); 3206 } 3207 break; 3208 case ISN_PCALL_END: 3209 smsg("%4d PCALL end", current); 3210 break; 3211 case ISN_RETURN: 3212 smsg("%4d RETURN", current); 3213 break; 3214 case ISN_FUNCREF: 3215 { 3216 funcref_T *funcref = &iptr->isn_arg.funcref; 3217 dfunc_T *df = ((dfunc_T *)def_functions.ga_data) 3218 + funcref->fr_func; 3219 3220 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name); 3221 } 3222 break; 3223 3224 case ISN_NEWFUNC: 3225 { 3226 newfunc_T *newfunc = &iptr->isn_arg.newfunc; 3227 3228 smsg("%4d NEWFUNC %s %s", current, 3229 newfunc->nf_lambda, newfunc->nf_global); 3230 } 3231 break; 3232 3233 case ISN_JUMP: 3234 { 3235 char *when = "?"; 3236 3237 switch (iptr->isn_arg.jump.jump_when) 3238 { 3239 case JUMP_ALWAYS: 3240 when = "JUMP"; 3241 break; 3242 case JUMP_AND_KEEP_IF_TRUE: 3243 when = "JUMP_AND_KEEP_IF_TRUE"; 3244 break; 3245 case JUMP_IF_FALSE: 3246 when = "JUMP_IF_FALSE"; 3247 break; 3248 case JUMP_AND_KEEP_IF_FALSE: 3249 when = "JUMP_AND_KEEP_IF_FALSE"; 3250 break; 3251 case JUMP_IF_COND_FALSE: 3252 when = "JUMP_IF_COND_FALSE"; 3253 break; 3254 case JUMP_IF_COND_TRUE: 3255 when = "JUMP_IF_COND_TRUE"; 3256 break; 3257 } 3258 smsg("%4d %s -> %d", current, when, 3259 iptr->isn_arg.jump.jump_where); 3260 } 3261 break; 3262 3263 case ISN_FOR: 3264 { 3265 forloop_T *forloop = &iptr->isn_arg.forloop; 3266 3267 smsg("%4d FOR $%d -> %d", current, 3268 forloop->for_idx, forloop->for_end); 3269 } 3270 break; 3271 3272 case ISN_TRY: 3273 { 3274 try_T *try = &iptr->isn_arg.try; 3275 3276 smsg("%4d TRY catch -> %d, finally -> %d", current, 3277 try->try_catch, try->try_finally); 3278 } 3279 break; 3280 case ISN_CATCH: 3281 // TODO 3282 smsg("%4d CATCH", current); 3283 break; 3284 case ISN_ENDTRY: 3285 smsg("%4d ENDTRY", current); 3286 break; 3287 case ISN_THROW: 3288 smsg("%4d THROW", current); 3289 break; 3290 3291 // expression operations on number 3292 case ISN_OPNR: 3293 case ISN_OPFLOAT: 3294 case ISN_OPANY: 3295 { 3296 char *what; 3297 char *ins; 3298 3299 switch (iptr->isn_arg.op.op_type) 3300 { 3301 case EXPR_MULT: what = "*"; break; 3302 case EXPR_DIV: what = "/"; break; 3303 case EXPR_REM: what = "%"; break; 3304 case EXPR_SUB: what = "-"; break; 3305 case EXPR_ADD: what = "+"; break; 3306 default: what = "???"; break; 3307 } 3308 switch (iptr->isn_type) 3309 { 3310 case ISN_OPNR: ins = "OPNR"; break; 3311 case ISN_OPFLOAT: ins = "OPFLOAT"; break; 3312 case ISN_OPANY: ins = "OPANY"; break; 3313 default: ins = "???"; break; 3314 } 3315 smsg("%4d %s %s", current, ins, what); 3316 } 3317 break; 3318 3319 case ISN_COMPAREBOOL: 3320 case ISN_COMPARESPECIAL: 3321 case ISN_COMPARENR: 3322 case ISN_COMPAREFLOAT: 3323 case ISN_COMPARESTRING: 3324 case ISN_COMPAREBLOB: 3325 case ISN_COMPARELIST: 3326 case ISN_COMPAREDICT: 3327 case ISN_COMPAREFUNC: 3328 case ISN_COMPAREANY: 3329 { 3330 char *p; 3331 char buf[10]; 3332 char *type; 3333 3334 switch (iptr->isn_arg.op.op_type) 3335 { 3336 case EXPR_EQUAL: p = "=="; break; 3337 case EXPR_NEQUAL: p = "!="; break; 3338 case EXPR_GREATER: p = ">"; break; 3339 case EXPR_GEQUAL: p = ">="; break; 3340 case EXPR_SMALLER: p = "<"; break; 3341 case EXPR_SEQUAL: p = "<="; break; 3342 case EXPR_MATCH: p = "=~"; break; 3343 case EXPR_IS: p = "is"; break; 3344 case EXPR_ISNOT: p = "isnot"; break; 3345 case EXPR_NOMATCH: p = "!~"; break; 3346 default: p = "???"; break; 3347 } 3348 STRCPY(buf, p); 3349 if (iptr->isn_arg.op.op_ic == TRUE) 3350 strcat(buf, "?"); 3351 switch(iptr->isn_type) 3352 { 3353 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break; 3354 case ISN_COMPARESPECIAL: 3355 type = "COMPARESPECIAL"; break; 3356 case ISN_COMPARENR: type = "COMPARENR"; break; 3357 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break; 3358 case ISN_COMPARESTRING: 3359 type = "COMPARESTRING"; break; 3360 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break; 3361 case ISN_COMPARELIST: type = "COMPARELIST"; break; 3362 case ISN_COMPAREDICT: type = "COMPAREDICT"; break; 3363 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break; 3364 case ISN_COMPAREANY: type = "COMPAREANY"; break; 3365 default: type = "???"; break; 3366 } 3367 3368 smsg("%4d %s %s", current, type, buf); 3369 } 3370 break; 3371 3372 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break; 3373 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break; 3374 3375 // expression operations 3376 case ISN_CONCAT: smsg("%4d CONCAT", current); break; 3377 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break; 3378 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break; 3379 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break; 3380 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break; 3381 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break; 3382 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break; 3383 case ISN_SLICE: smsg("%4d SLICE %lld", 3384 current, iptr->isn_arg.number); break; 3385 case ISN_GETITEM: smsg("%4d ITEM %lld", 3386 current, iptr->isn_arg.number); break; 3387 case ISN_MEMBER: smsg("%4d MEMBER", current); break; 3388 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current, 3389 iptr->isn_arg.string); break; 3390 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break; 3391 3392 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break; 3393 case ISN_CHECKTYPE: 3394 { 3395 char *tofree; 3396 3397 smsg("%4d CHECKTYPE %s stack[%d]", current, 3398 type_name(iptr->isn_arg.type.ct_type, &tofree), 3399 iptr->isn_arg.type.ct_off); 3400 vim_free(tofree); 3401 break; 3402 } 3403 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current, 3404 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "", 3405 iptr->isn_arg.checklen.cl_min_len); 3406 break; 3407 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break; 3408 case ISN_2BOOL: if (iptr->isn_arg.number) 3409 smsg("%4d INVERT (!val)", current); 3410 else 3411 smsg("%4d 2BOOL (!!val)", current); 3412 break; 3413 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current, 3414 (long long)(iptr->isn_arg.number)); 3415 break; 3416 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current, 3417 (long long)(iptr->isn_arg.number)); 3418 break; 3419 case ISN_PUT: 3420 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname, 3421 (long)iptr->isn_arg.put.put_lnum); 3422 break; 3423 3424 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current, 3425 iptr->isn_arg.shuffle.shfl_item, 3426 iptr->isn_arg.shuffle.shfl_up); 3427 break; 3428 case ISN_DROP: smsg("%4d DROP", current); break; 3429 } 3430 3431 out_flush(); // output one line at a time 3432 ui_breakcheck(); 3433 if (got_int) 3434 break; 3435 } 3436 } 3437 3438 /* 3439 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty 3440 * list, etc. Mostly like what JavaScript does, except that empty list and 3441 * empty dictionary are FALSE. 3442 */ 3443 int 3444 tv2bool(typval_T *tv) 3445 { 3446 switch (tv->v_type) 3447 { 3448 case VAR_NUMBER: 3449 return tv->vval.v_number != 0; 3450 case VAR_FLOAT: 3451 #ifdef FEAT_FLOAT 3452 return tv->vval.v_float != 0.0; 3453 #else 3454 break; 3455 #endif 3456 case VAR_PARTIAL: 3457 return tv->vval.v_partial != NULL; 3458 case VAR_FUNC: 3459 case VAR_STRING: 3460 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL; 3461 case VAR_LIST: 3462 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0; 3463 case VAR_DICT: 3464 return tv->vval.v_dict != NULL 3465 && tv->vval.v_dict->dv_hashtab.ht_used > 0; 3466 case VAR_BOOL: 3467 case VAR_SPECIAL: 3468 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE; 3469 case VAR_JOB: 3470 #ifdef FEAT_JOB_CHANNEL 3471 return tv->vval.v_job != NULL; 3472 #else 3473 break; 3474 #endif 3475 case VAR_CHANNEL: 3476 #ifdef FEAT_JOB_CHANNEL 3477 return tv->vval.v_channel != NULL; 3478 #else 3479 break; 3480 #endif 3481 case VAR_BLOB: 3482 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0; 3483 case VAR_UNKNOWN: 3484 case VAR_ANY: 3485 case VAR_VOID: 3486 break; 3487 } 3488 return FALSE; 3489 } 3490 3491 /* 3492 * If "tv" is a string give an error and return FAIL. 3493 */ 3494 int 3495 check_not_string(typval_T *tv) 3496 { 3497 if (tv->v_type == VAR_STRING) 3498 { 3499 emsg(_(e_using_string_as_number)); 3500 clear_tv(tv); 3501 return FAIL; 3502 } 3503 return OK; 3504 } 3505 3506 3507 #endif // FEAT_EVAL 3508