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 called_emsg_before = called_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 (called_emsg != called_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 msglist_T **saved_msg_list = NULL; 834 msglist_T *private_msg_list = NULL; 835 836 // Get pointer to item in the stack. 837 #define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx) 838 839 // Get pointer to item at the bottom of the stack, -1 is the bottom. 840 #undef STACK_TV_BOT 841 #define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx) 842 843 // Get pointer to a local variable on the stack. Negative for arguments. 844 #define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx) 845 846 // Like STACK_TV_VAR but use the outer scope 847 #define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx) 848 849 if (ufunc->uf_def_status == UF_NOT_COMPILED 850 || (ufunc->uf_def_status == UF_TO_BE_COMPILED 851 && compile_def_function(ufunc, FALSE, NULL) == FAIL)) 852 { 853 if (called_emsg == called_emsg_before) 854 semsg(_(e_function_is_not_compiled_str), 855 printable_func_name(ufunc)); 856 return FAIL; 857 } 858 859 { 860 // Check the function was really compiled. 861 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 862 + ufunc->uf_dfunc_idx; 863 if (dfunc->df_instr == NULL) 864 { 865 iemsg("using call_def_function() on not compiled function"); 866 return FAIL; 867 } 868 } 869 870 CLEAR_FIELD(ectx); 871 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx; 872 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500); 873 if (ga_grow(&ectx.ec_stack, 20) == FAIL) 874 return FAIL; 875 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10); 876 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10); 877 878 // Put arguments on the stack. 879 for (idx = 0; idx < argc; ++idx) 880 { 881 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len 882 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx], 883 idx + 1) == FAIL) 884 goto failed_early; 885 copy_tv(&argv[idx], STACK_TV_BOT(0)); 886 ++ectx.ec_stack.ga_len; 887 } 888 889 // Turn varargs into a list. Empty list if no args. 890 if (ufunc->uf_va_name != NULL) 891 { 892 int vararg_count = argc - ufunc->uf_args.ga_len; 893 894 if (vararg_count < 0) 895 vararg_count = 0; 896 else 897 argc -= vararg_count; 898 if (exe_newlist(vararg_count, &ectx) == FAIL) 899 goto failed_early; 900 901 // Check the type of the list items. 902 tv = STACK_TV_BOT(-1); 903 if (ufunc->uf_va_type != NULL 904 && ufunc->uf_va_type != &t_any 905 && ufunc->uf_va_type->tt_member != &t_any 906 && tv->vval.v_list != NULL) 907 { 908 type_T *expected = ufunc->uf_va_type->tt_member; 909 listitem_T *li = tv->vval.v_list->lv_first; 910 911 for (idx = 0; idx < vararg_count; ++idx) 912 { 913 if (check_typval_type(expected, &li->li_tv, 914 argc + idx + 1) == FAIL) 915 goto failed_early; 916 li = li->li_next; 917 } 918 } 919 920 if (defcount > 0) 921 // Move varargs list to below missing default arguments. 922 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1); 923 --ectx.ec_stack.ga_len; 924 } 925 926 // Make space for omitted arguments, will store default value below. 927 // Any varargs list goes after them. 928 if (defcount > 0) 929 for (idx = 0; idx < defcount; ++idx) 930 { 931 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN; 932 ++ectx.ec_stack.ga_len; 933 } 934 if (ufunc->uf_va_name != NULL) 935 ++ectx.ec_stack.ga_len; 936 937 // Frame pointer points to just after arguments. 938 ectx.ec_frame_idx = ectx.ec_stack.ga_len; 939 initial_frame_idx = ectx.ec_frame_idx; 940 941 if (partial != NULL) 942 { 943 if (partial->pt_ectx_stack == NULL && current_ectx != NULL) 944 { 945 // TODO: is this always the right way? 946 ectx.ec_outer_stack = ¤t_ectx->ec_stack; 947 ectx.ec_outer_frame = current_ectx->ec_frame_idx; 948 } 949 else 950 { 951 ectx.ec_outer_stack = partial->pt_ectx_stack; 952 ectx.ec_outer_frame = partial->pt_ectx_frame; 953 } 954 } 955 956 // dummy frame entries 957 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx) 958 { 959 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN; 960 ++ectx.ec_stack.ga_len; 961 } 962 963 { 964 // Reserve space for local variables and any closure reference count. 965 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 966 + ufunc->uf_dfunc_idx; 967 968 for (idx = 0; idx < dfunc->df_varcount; ++idx) 969 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN; 970 ectx.ec_stack.ga_len += dfunc->df_varcount; 971 if (dfunc->df_has_closure) 972 { 973 STACK_TV_VAR(idx)->v_type = VAR_NUMBER; 974 STACK_TV_VAR(idx)->vval.v_number = 0; 975 ++ectx.ec_stack.ga_len; 976 } 977 978 ectx.ec_instr = dfunc->df_instr; 979 } 980 981 // Following errors are in the function, not the caller. 982 // Commands behave like vim9script. 983 estack_push_ufunc(ufunc, 1); 984 current_sctx = ufunc->uf_script_ctx; 985 current_sctx.sc_version = SCRIPT_VERSION_VIM9; 986 987 // Use a specific location for storing error messages to be converted to an 988 // exception. 989 saved_msg_list = msg_list; 990 msg_list = &private_msg_list; 991 992 // Do turn errors into exceptions. 993 suppress_errthrow = FALSE; 994 995 // Decide where to start execution, handles optional arguments. 996 init_instr_idx(ufunc, argc, &ectx); 997 998 for (;;) 999 { 1000 isn_T *iptr; 1001 1002 if (++breakcheck_count >= 100) 1003 { 1004 line_breakcheck(); 1005 breakcheck_count = 0; 1006 } 1007 if (got_int) 1008 { 1009 // Turn CTRL-C into an exception. 1010 got_int = FALSE; 1011 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL) 1012 goto failed; 1013 did_throw = TRUE; 1014 } 1015 1016 if (did_emsg && msg_list != NULL && *msg_list != NULL) 1017 { 1018 // Turn an error message into an exception. 1019 did_emsg = FALSE; 1020 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL) 1021 goto failed; 1022 did_throw = TRUE; 1023 *msg_list = NULL; 1024 } 1025 1026 if (did_throw && !ectx.ec_in_catch) 1027 { 1028 garray_T *trystack = &ectx.ec_trystack; 1029 trycmd_T *trycmd = NULL; 1030 1031 // An exception jumps to the first catch, finally, or returns from 1032 // the current function. 1033 if (trystack->ga_len > 0) 1034 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1; 1035 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx) 1036 { 1037 // jump to ":catch" or ":finally" 1038 ectx.ec_in_catch = TRUE; 1039 ectx.ec_iidx = trycmd->tcd_catch_idx; 1040 } 1041 else 1042 { 1043 // Not inside try or need to return from current functions. 1044 // Push a dummy return value. 1045 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1046 goto failed; 1047 tv = STACK_TV_BOT(0); 1048 tv->v_type = VAR_NUMBER; 1049 tv->vval.v_number = 0; 1050 ++ectx.ec_stack.ga_len; 1051 if (ectx.ec_frame_idx == initial_frame_idx) 1052 { 1053 // At the toplevel we are done. 1054 need_rethrow = TRUE; 1055 if (handle_closure_in_use(&ectx, FALSE) == FAIL) 1056 goto failed; 1057 goto done; 1058 } 1059 1060 if (func_return(&ectx) == FAIL) 1061 goto failed; 1062 } 1063 continue; 1064 } 1065 1066 iptr = &ectx.ec_instr[ectx.ec_iidx++]; 1067 switch (iptr->isn_type) 1068 { 1069 // execute Ex command line 1070 case ISN_EXEC: 1071 SOURCING_LNUM = iptr->isn_lnum; 1072 do_cmdline_cmd(iptr->isn_arg.string); 1073 break; 1074 1075 // execute Ex command from pieces on the stack 1076 case ISN_EXECCONCAT: 1077 { 1078 int count = iptr->isn_arg.number; 1079 size_t len = 0; 1080 int pass; 1081 int i; 1082 char_u *cmd = NULL; 1083 char_u *str; 1084 1085 for (pass = 1; pass <= 2; ++pass) 1086 { 1087 for (i = 0; i < count; ++i) 1088 { 1089 tv = STACK_TV_BOT(i - count); 1090 str = tv->vval.v_string; 1091 if (str != NULL && *str != NUL) 1092 { 1093 if (pass == 2) 1094 STRCPY(cmd + len, str); 1095 len += STRLEN(str); 1096 } 1097 if (pass == 2) 1098 clear_tv(tv); 1099 } 1100 if (pass == 1) 1101 { 1102 cmd = alloc(len + 1); 1103 if (cmd == NULL) 1104 goto failed; 1105 len = 0; 1106 } 1107 } 1108 1109 SOURCING_LNUM = iptr->isn_lnum; 1110 do_cmdline_cmd(cmd); 1111 vim_free(cmd); 1112 } 1113 break; 1114 1115 // execute :echo {string} ... 1116 case ISN_ECHO: 1117 { 1118 int count = iptr->isn_arg.echo.echo_count; 1119 int atstart = TRUE; 1120 int needclr = TRUE; 1121 1122 for (idx = 0; idx < count; ++idx) 1123 { 1124 tv = STACK_TV_BOT(idx - count); 1125 echo_one(tv, iptr->isn_arg.echo.echo_with_white, 1126 &atstart, &needclr); 1127 clear_tv(tv); 1128 } 1129 if (needclr) 1130 msg_clr_eos(); 1131 ectx.ec_stack.ga_len -= count; 1132 } 1133 break; 1134 1135 // :execute {string} ... 1136 // :echomsg {string} ... 1137 // :echoerr {string} ... 1138 case ISN_EXECUTE: 1139 case ISN_ECHOMSG: 1140 case ISN_ECHOERR: 1141 { 1142 int count = iptr->isn_arg.number; 1143 garray_T ga; 1144 char_u buf[NUMBUFLEN]; 1145 char_u *p; 1146 int len; 1147 int failed = FALSE; 1148 1149 ga_init2(&ga, 1, 80); 1150 for (idx = 0; idx < count; ++idx) 1151 { 1152 tv = STACK_TV_BOT(idx - count); 1153 if (iptr->isn_type == ISN_EXECUTE) 1154 { 1155 if (tv->v_type == VAR_CHANNEL 1156 || tv->v_type == VAR_JOB) 1157 { 1158 SOURCING_LNUM = iptr->isn_lnum; 1159 emsg(_(e_inval_string)); 1160 break; 1161 } 1162 else 1163 p = tv_get_string_buf(tv, buf); 1164 } 1165 else 1166 p = tv_stringify(tv, buf); 1167 1168 len = (int)STRLEN(p); 1169 if (ga_grow(&ga, len + 2) == FAIL) 1170 failed = TRUE; 1171 else 1172 { 1173 if (ga.ga_len > 0) 1174 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; 1175 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); 1176 ga.ga_len += len; 1177 } 1178 clear_tv(tv); 1179 } 1180 ectx.ec_stack.ga_len -= count; 1181 if (failed) 1182 goto on_error; 1183 1184 if (ga.ga_data != NULL) 1185 { 1186 if (iptr->isn_type == ISN_EXECUTE) 1187 { 1188 SOURCING_LNUM = iptr->isn_lnum; 1189 do_cmdline_cmd((char_u *)ga.ga_data); 1190 } 1191 else 1192 { 1193 msg_sb_eol(); 1194 if (iptr->isn_type == ISN_ECHOMSG) 1195 { 1196 msg_attr(ga.ga_data, echo_attr); 1197 out_flush(); 1198 } 1199 else 1200 { 1201 SOURCING_LNUM = iptr->isn_lnum; 1202 emsg(ga.ga_data); 1203 } 1204 } 1205 } 1206 ga_clear(&ga); 1207 } 1208 break; 1209 1210 // load local variable or argument 1211 case ISN_LOAD: 1212 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1213 goto failed; 1214 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0)); 1215 ++ectx.ec_stack.ga_len; 1216 break; 1217 1218 // load variable or argument from outer scope 1219 case ISN_LOADOUTER: 1220 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1221 goto failed; 1222 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number), 1223 STACK_TV_BOT(0)); 1224 ++ectx.ec_stack.ga_len; 1225 break; 1226 1227 // load v: variable 1228 case ISN_LOADV: 1229 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1230 goto failed; 1231 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0)); 1232 ++ectx.ec_stack.ga_len; 1233 break; 1234 1235 // load s: variable in Vim9 script 1236 case ISN_LOADSCRIPT: 1237 { 1238 scriptitem_T *si = 1239 SCRIPT_ITEM(iptr->isn_arg.script.script_sid); 1240 svar_T *sv; 1241 1242 sv = ((svar_T *)si->sn_var_vals.ga_data) 1243 + iptr->isn_arg.script.script_idx; 1244 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1245 goto failed; 1246 copy_tv(sv->sv_tv, STACK_TV_BOT(0)); 1247 ++ectx.ec_stack.ga_len; 1248 } 1249 break; 1250 1251 // load s: variable in old script 1252 case ISN_LOADS: 1253 { 1254 hashtab_T *ht = &SCRIPT_VARS( 1255 iptr->isn_arg.loadstore.ls_sid); 1256 char_u *name = iptr->isn_arg.loadstore.ls_name; 1257 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE); 1258 1259 if (di == NULL) 1260 { 1261 SOURCING_LNUM = iptr->isn_lnum; 1262 semsg(_(e_undefined_variable_str), name); 1263 goto on_error; 1264 } 1265 else 1266 { 1267 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1268 goto failed; 1269 copy_tv(&di->di_tv, STACK_TV_BOT(0)); 1270 ++ectx.ec_stack.ga_len; 1271 } 1272 } 1273 break; 1274 1275 // load g:/b:/w:/t: variable 1276 case ISN_LOADG: 1277 case ISN_LOADB: 1278 case ISN_LOADW: 1279 case ISN_LOADT: 1280 { 1281 dictitem_T *di = NULL; 1282 hashtab_T *ht = NULL; 1283 char namespace; 1284 1285 switch (iptr->isn_type) 1286 { 1287 case ISN_LOADG: 1288 ht = get_globvar_ht(); 1289 namespace = 'g'; 1290 break; 1291 case ISN_LOADB: 1292 ht = &curbuf->b_vars->dv_hashtab; 1293 namespace = 'b'; 1294 break; 1295 case ISN_LOADW: 1296 ht = &curwin->w_vars->dv_hashtab; 1297 namespace = 'w'; 1298 break; 1299 case ISN_LOADT: 1300 ht = &curtab->tp_vars->dv_hashtab; 1301 namespace = 't'; 1302 break; 1303 default: // Cannot reach here 1304 goto failed; 1305 } 1306 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE); 1307 1308 if (di == NULL) 1309 { 1310 SOURCING_LNUM = iptr->isn_lnum; 1311 semsg(_(e_undefined_variable_char_str), 1312 namespace, iptr->isn_arg.string); 1313 goto on_error; 1314 } 1315 else 1316 { 1317 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1318 goto failed; 1319 copy_tv(&di->di_tv, STACK_TV_BOT(0)); 1320 ++ectx.ec_stack.ga_len; 1321 } 1322 } 1323 break; 1324 1325 // load g:/b:/w:/t: namespace 1326 case ISN_LOADGDICT: 1327 case ISN_LOADBDICT: 1328 case ISN_LOADWDICT: 1329 case ISN_LOADTDICT: 1330 { 1331 dict_T *d = NULL; 1332 1333 switch (iptr->isn_type) 1334 { 1335 case ISN_LOADGDICT: d = get_globvar_dict(); break; 1336 case ISN_LOADBDICT: d = curbuf->b_vars; break; 1337 case ISN_LOADWDICT: d = curwin->w_vars; break; 1338 case ISN_LOADTDICT: d = curtab->tp_vars; break; 1339 default: // Cannot reach here 1340 goto failed; 1341 } 1342 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1343 goto failed; 1344 tv = STACK_TV_BOT(0); 1345 tv->v_type = VAR_DICT; 1346 tv->v_lock = 0; 1347 tv->vval.v_dict = d; 1348 ++ectx.ec_stack.ga_len; 1349 } 1350 break; 1351 1352 // load &option 1353 case ISN_LOADOPT: 1354 { 1355 typval_T optval; 1356 char_u *name = iptr->isn_arg.string; 1357 1358 // This is not expected to fail, name is checked during 1359 // compilation: don't set SOURCING_LNUM. 1360 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1361 goto failed; 1362 if (eval_option(&name, &optval, TRUE) == FAIL) 1363 goto failed; 1364 *STACK_TV_BOT(0) = optval; 1365 ++ectx.ec_stack.ga_len; 1366 } 1367 break; 1368 1369 // load $ENV 1370 case ISN_LOADENV: 1371 { 1372 typval_T optval; 1373 char_u *name = iptr->isn_arg.string; 1374 1375 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1376 goto failed; 1377 // name is always valid, checked when compiling 1378 (void)eval_env_var(&name, &optval, TRUE); 1379 *STACK_TV_BOT(0) = optval; 1380 ++ectx.ec_stack.ga_len; 1381 } 1382 break; 1383 1384 // load @register 1385 case ISN_LOADREG: 1386 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1387 goto failed; 1388 tv = STACK_TV_BOT(0); 1389 tv->v_type = VAR_STRING; 1390 tv->v_lock = 0; 1391 // This may result in NULL, which should be equivalent to an 1392 // empty string. 1393 tv->vval.v_string = get_reg_contents( 1394 iptr->isn_arg.number, GREG_EXPR_SRC); 1395 ++ectx.ec_stack.ga_len; 1396 break; 1397 1398 // store local variable 1399 case ISN_STORE: 1400 --ectx.ec_stack.ga_len; 1401 tv = STACK_TV_VAR(iptr->isn_arg.number); 1402 clear_tv(tv); 1403 *tv = *STACK_TV_BOT(0); 1404 break; 1405 1406 // store variable or argument in outer scope 1407 case ISN_STOREOUTER: 1408 --ectx.ec_stack.ga_len; 1409 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number); 1410 clear_tv(tv); 1411 *tv = *STACK_TV_BOT(0); 1412 break; 1413 1414 // store s: variable in old script 1415 case ISN_STORES: 1416 { 1417 hashtab_T *ht = &SCRIPT_VARS( 1418 iptr->isn_arg.loadstore.ls_sid); 1419 char_u *name = iptr->isn_arg.loadstore.ls_name; 1420 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE); 1421 1422 --ectx.ec_stack.ga_len; 1423 if (di == NULL) 1424 store_var(name, STACK_TV_BOT(0)); 1425 else 1426 { 1427 clear_tv(&di->di_tv); 1428 di->di_tv = *STACK_TV_BOT(0); 1429 } 1430 } 1431 break; 1432 1433 // store script-local variable in Vim9 script 1434 case ISN_STORESCRIPT: 1435 { 1436 scriptitem_T *si = SCRIPT_ITEM( 1437 iptr->isn_arg.script.script_sid); 1438 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 1439 + iptr->isn_arg.script.script_idx; 1440 1441 --ectx.ec_stack.ga_len; 1442 clear_tv(sv->sv_tv); 1443 *sv->sv_tv = *STACK_TV_BOT(0); 1444 } 1445 break; 1446 1447 // store option 1448 case ISN_STOREOPT: 1449 { 1450 long n = 0; 1451 char_u *s = NULL; 1452 char *msg; 1453 1454 --ectx.ec_stack.ga_len; 1455 tv = STACK_TV_BOT(0); 1456 if (tv->v_type == VAR_STRING) 1457 { 1458 s = tv->vval.v_string; 1459 if (s == NULL) 1460 s = (char_u *)""; 1461 } 1462 else 1463 // must be VAR_NUMBER, CHECKTYPE makes sure 1464 n = tv->vval.v_number; 1465 msg = set_option_value(iptr->isn_arg.storeopt.so_name, 1466 n, s, iptr->isn_arg.storeopt.so_flags); 1467 clear_tv(tv); 1468 if (msg != NULL) 1469 { 1470 SOURCING_LNUM = iptr->isn_lnum; 1471 emsg(_(msg)); 1472 goto on_error; 1473 } 1474 } 1475 break; 1476 1477 // store $ENV 1478 case ISN_STOREENV: 1479 --ectx.ec_stack.ga_len; 1480 tv = STACK_TV_BOT(0); 1481 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv)); 1482 clear_tv(tv); 1483 break; 1484 1485 // store @r 1486 case ISN_STOREREG: 1487 { 1488 int reg = iptr->isn_arg.number; 1489 1490 --ectx.ec_stack.ga_len; 1491 tv = STACK_TV_BOT(0); 1492 write_reg_contents(reg == '@' ? '"' : reg, 1493 tv_get_string(tv), -1, FALSE); 1494 clear_tv(tv); 1495 } 1496 break; 1497 1498 // store v: variable 1499 case ISN_STOREV: 1500 --ectx.ec_stack.ga_len; 1501 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0)) 1502 == FAIL) 1503 // should not happen, type is checked when compiling 1504 goto on_error; 1505 break; 1506 1507 // store g:/b:/w:/t: variable 1508 case ISN_STOREG: 1509 case ISN_STOREB: 1510 case ISN_STOREW: 1511 case ISN_STORET: 1512 { 1513 dictitem_T *di; 1514 hashtab_T *ht; 1515 switch (iptr->isn_type) 1516 { 1517 case ISN_STOREG: 1518 ht = get_globvar_ht(); 1519 break; 1520 case ISN_STOREB: 1521 ht = &curbuf->b_vars->dv_hashtab; 1522 break; 1523 case ISN_STOREW: 1524 ht = &curwin->w_vars->dv_hashtab; 1525 break; 1526 case ISN_STORET: 1527 ht = &curtab->tp_vars->dv_hashtab; 1528 break; 1529 default: // Cannot reach here 1530 goto failed; 1531 } 1532 1533 --ectx.ec_stack.ga_len; 1534 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE); 1535 if (di == NULL) 1536 store_var(iptr->isn_arg.string, STACK_TV_BOT(0)); 1537 else 1538 { 1539 clear_tv(&di->di_tv); 1540 di->di_tv = *STACK_TV_BOT(0); 1541 } 1542 } 1543 break; 1544 1545 // store number in local variable 1546 case ISN_STORENR: 1547 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx); 1548 clear_tv(tv); 1549 tv->v_type = VAR_NUMBER; 1550 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val; 1551 break; 1552 1553 // store value in list variable 1554 case ISN_STORELIST: 1555 { 1556 typval_T *tv_idx = STACK_TV_BOT(-2); 1557 varnumber_T lidx = tv_idx->vval.v_number; 1558 typval_T *tv_list = STACK_TV_BOT(-1); 1559 list_T *list = tv_list->vval.v_list; 1560 1561 SOURCING_LNUM = iptr->isn_lnum; 1562 if (lidx < 0 && list->lv_len + lidx >= 0) 1563 // negative index is relative to the end 1564 lidx = list->lv_len + lidx; 1565 if (lidx < 0 || lidx > list->lv_len) 1566 { 1567 semsg(_(e_listidx), lidx); 1568 goto on_error; 1569 } 1570 tv = STACK_TV_BOT(-3); 1571 if (lidx < list->lv_len) 1572 { 1573 listitem_T *li = list_find(list, lidx); 1574 1575 if (error_if_locked(li->li_tv.v_lock, 1576 e_cannot_change_list_item)) 1577 goto failed; 1578 // overwrite existing list item 1579 clear_tv(&li->li_tv); 1580 li->li_tv = *tv; 1581 } 1582 else 1583 { 1584 if (error_if_locked(list->lv_lock, 1585 e_cannot_change_list)) 1586 goto failed; 1587 // append to list, only fails when out of memory 1588 if (list_append_tv(list, tv) == FAIL) 1589 goto failed; 1590 clear_tv(tv); 1591 } 1592 clear_tv(tv_idx); 1593 clear_tv(tv_list); 1594 ectx.ec_stack.ga_len -= 3; 1595 } 1596 break; 1597 1598 // store value in dict variable 1599 case ISN_STOREDICT: 1600 { 1601 typval_T *tv_key = STACK_TV_BOT(-2); 1602 char_u *key = tv_key->vval.v_string; 1603 typval_T *tv_dict = STACK_TV_BOT(-1); 1604 dict_T *dict = tv_dict->vval.v_dict; 1605 dictitem_T *di; 1606 1607 SOURCING_LNUM = iptr->isn_lnum; 1608 if (dict == NULL) 1609 { 1610 emsg(_(e_dictionary_not_set)); 1611 goto on_error; 1612 } 1613 if (key == NULL) 1614 key = (char_u *)""; 1615 tv = STACK_TV_BOT(-3); 1616 di = dict_find(dict, key, -1); 1617 if (di != NULL) 1618 { 1619 if (error_if_locked(di->di_tv.v_lock, 1620 e_cannot_change_dict_item)) 1621 goto failed; 1622 // overwrite existing value 1623 clear_tv(&di->di_tv); 1624 di->di_tv = *tv; 1625 } 1626 else 1627 { 1628 if (error_if_locked(dict->dv_lock, 1629 e_cannot_change_dict)) 1630 goto failed; 1631 // add to dict, only fails when out of memory 1632 if (dict_add_tv(dict, (char *)key, tv) == FAIL) 1633 goto failed; 1634 clear_tv(tv); 1635 } 1636 clear_tv(tv_key); 1637 clear_tv(tv_dict); 1638 ectx.ec_stack.ga_len -= 3; 1639 } 1640 break; 1641 1642 // push constant 1643 case ISN_PUSHNR: 1644 case ISN_PUSHBOOL: 1645 case ISN_PUSHSPEC: 1646 case ISN_PUSHF: 1647 case ISN_PUSHS: 1648 case ISN_PUSHBLOB: 1649 case ISN_PUSHFUNC: 1650 case ISN_PUSHCHANNEL: 1651 case ISN_PUSHJOB: 1652 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1653 goto failed; 1654 tv = STACK_TV_BOT(0); 1655 tv->v_lock = 0; 1656 ++ectx.ec_stack.ga_len; 1657 switch (iptr->isn_type) 1658 { 1659 case ISN_PUSHNR: 1660 tv->v_type = VAR_NUMBER; 1661 tv->vval.v_number = iptr->isn_arg.number; 1662 break; 1663 case ISN_PUSHBOOL: 1664 tv->v_type = VAR_BOOL; 1665 tv->vval.v_number = iptr->isn_arg.number; 1666 break; 1667 case ISN_PUSHSPEC: 1668 tv->v_type = VAR_SPECIAL; 1669 tv->vval.v_number = iptr->isn_arg.number; 1670 break; 1671 #ifdef FEAT_FLOAT 1672 case ISN_PUSHF: 1673 tv->v_type = VAR_FLOAT; 1674 tv->vval.v_float = iptr->isn_arg.fnumber; 1675 break; 1676 #endif 1677 case ISN_PUSHBLOB: 1678 blob_copy(iptr->isn_arg.blob, tv); 1679 break; 1680 case ISN_PUSHFUNC: 1681 tv->v_type = VAR_FUNC; 1682 if (iptr->isn_arg.string == NULL) 1683 tv->vval.v_string = NULL; 1684 else 1685 tv->vval.v_string = 1686 vim_strsave(iptr->isn_arg.string); 1687 break; 1688 case ISN_PUSHCHANNEL: 1689 #ifdef FEAT_JOB_CHANNEL 1690 tv->v_type = VAR_CHANNEL; 1691 tv->vval.v_channel = iptr->isn_arg.channel; 1692 if (tv->vval.v_channel != NULL) 1693 ++tv->vval.v_channel->ch_refcount; 1694 #endif 1695 break; 1696 case ISN_PUSHJOB: 1697 #ifdef FEAT_JOB_CHANNEL 1698 tv->v_type = VAR_JOB; 1699 tv->vval.v_job = iptr->isn_arg.job; 1700 if (tv->vval.v_job != NULL) 1701 ++tv->vval.v_job->jv_refcount; 1702 #endif 1703 break; 1704 default: 1705 tv->v_type = VAR_STRING; 1706 tv->vval.v_string = vim_strsave( 1707 iptr->isn_arg.string == NULL 1708 ? (char_u *)"" : iptr->isn_arg.string); 1709 } 1710 break; 1711 1712 case ISN_UNLET: 1713 if (do_unlet(iptr->isn_arg.unlet.ul_name, 1714 iptr->isn_arg.unlet.ul_forceit) == FAIL) 1715 goto on_error; 1716 break; 1717 case ISN_UNLETENV: 1718 vim_unsetenv(iptr->isn_arg.unlet.ul_name); 1719 break; 1720 1721 case ISN_LOCKCONST: 1722 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE); 1723 break; 1724 1725 // create a list from items on the stack; uses a single allocation 1726 // for the list header and the items 1727 case ISN_NEWLIST: 1728 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL) 1729 goto failed; 1730 break; 1731 1732 // create a dict from items on the stack 1733 case ISN_NEWDICT: 1734 { 1735 int count = iptr->isn_arg.number; 1736 dict_T *dict = dict_alloc(); 1737 dictitem_T *item; 1738 1739 if (dict == NULL) 1740 goto failed; 1741 for (idx = 0; idx < count; ++idx) 1742 { 1743 // have already checked key type is VAR_STRING 1744 tv = STACK_TV_BOT(2 * (idx - count)); 1745 // check key is unique 1746 item = dict_find(dict, tv->vval.v_string, -1); 1747 if (item != NULL) 1748 { 1749 SOURCING_LNUM = iptr->isn_lnum; 1750 semsg(_(e_duplicate_key), tv->vval.v_string); 1751 dict_unref(dict); 1752 goto on_error; 1753 } 1754 item = dictitem_alloc(tv->vval.v_string); 1755 clear_tv(tv); 1756 if (item == NULL) 1757 { 1758 dict_unref(dict); 1759 goto failed; 1760 } 1761 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1); 1762 item->di_tv.v_lock = 0; 1763 if (dict_add(dict, item) == FAIL) 1764 { 1765 // can this ever happen? 1766 dict_unref(dict); 1767 goto failed; 1768 } 1769 } 1770 1771 if (count > 0) 1772 ectx.ec_stack.ga_len -= 2 * count - 1; 1773 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1774 goto failed; 1775 else 1776 ++ectx.ec_stack.ga_len; 1777 tv = STACK_TV_BOT(-1); 1778 tv->v_type = VAR_DICT; 1779 tv->v_lock = 0; 1780 tv->vval.v_dict = dict; 1781 ++dict->dv_refcount; 1782 } 1783 break; 1784 1785 // call a :def function 1786 case ISN_DCALL: 1787 SOURCING_LNUM = iptr->isn_lnum; 1788 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, 1789 iptr->isn_arg.dfunc.cdf_argcount, 1790 &ectx) == FAIL) 1791 goto on_error; 1792 break; 1793 1794 // call a builtin function 1795 case ISN_BCALL: 1796 SOURCING_LNUM = iptr->isn_lnum; 1797 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx, 1798 iptr->isn_arg.bfunc.cbf_argcount, 1799 &ectx) == FAIL) 1800 goto on_error; 1801 break; 1802 1803 // call a funcref or partial 1804 case ISN_PCALL: 1805 { 1806 cpfunc_T *pfunc = &iptr->isn_arg.pfunc; 1807 int r; 1808 typval_T partial_tv; 1809 1810 SOURCING_LNUM = iptr->isn_lnum; 1811 if (pfunc->cpf_top) 1812 { 1813 // funcref is above the arguments 1814 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1); 1815 } 1816 else 1817 { 1818 // Get the funcref from the stack. 1819 --ectx.ec_stack.ga_len; 1820 partial_tv = *STACK_TV_BOT(0); 1821 tv = &partial_tv; 1822 } 1823 r = call_partial(tv, pfunc->cpf_argcount, &ectx); 1824 if (tv == &partial_tv) 1825 clear_tv(&partial_tv); 1826 if (r == FAIL) 1827 goto on_error; 1828 } 1829 break; 1830 1831 case ISN_PCALL_END: 1832 // PCALL finished, arguments have been consumed and replaced by 1833 // the return value. Now clear the funcref from the stack, 1834 // and move the return value in its place. 1835 --ectx.ec_stack.ga_len; 1836 clear_tv(STACK_TV_BOT(-1)); 1837 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0); 1838 break; 1839 1840 // call a user defined function or funcref/partial 1841 case ISN_UCALL: 1842 { 1843 cufunc_T *cufunc = &iptr->isn_arg.ufunc; 1844 1845 SOURCING_LNUM = iptr->isn_lnum; 1846 if (call_eval_func(cufunc->cuf_name, 1847 cufunc->cuf_argcount, &ectx, iptr) == FAIL) 1848 goto on_error; 1849 } 1850 break; 1851 1852 // return from a :def function call 1853 case ISN_RETURN: 1854 { 1855 garray_T *trystack = &ectx.ec_trystack; 1856 trycmd_T *trycmd = NULL; 1857 1858 if (trystack->ga_len > 0) 1859 trycmd = ((trycmd_T *)trystack->ga_data) 1860 + trystack->ga_len - 1; 1861 if (trycmd != NULL 1862 && trycmd->tcd_frame_idx == ectx.ec_frame_idx 1863 && trycmd->tcd_finally_idx != 0) 1864 { 1865 // jump to ":finally" 1866 ectx.ec_iidx = trycmd->tcd_finally_idx; 1867 trycmd->tcd_return = TRUE; 1868 } 1869 else 1870 goto func_return; 1871 } 1872 break; 1873 1874 // push a function reference to a compiled function 1875 case ISN_FUNCREF: 1876 { 1877 partial_T *pt = NULL; 1878 dfunc_T *pt_dfunc; 1879 1880 pt = ALLOC_CLEAR_ONE(partial_T); 1881 if (pt == NULL) 1882 goto failed; 1883 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1884 { 1885 vim_free(pt); 1886 goto failed; 1887 } 1888 pt_dfunc = ((dfunc_T *)def_functions.ga_data) 1889 + iptr->isn_arg.funcref.fr_func; 1890 pt->pt_func = pt_dfunc->df_ufunc; 1891 pt->pt_refcount = 1; 1892 1893 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE) 1894 { 1895 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 1896 + ectx.ec_dfunc_idx; 1897 1898 // The closure needs to find arguments and local 1899 // variables in the current stack. 1900 pt->pt_ectx_stack = &ectx.ec_stack; 1901 pt->pt_ectx_frame = ectx.ec_frame_idx; 1902 1903 // If this function returns and the closure is still 1904 // being used, we need to make a copy of the context 1905 // (arguments and local variables). Store a reference 1906 // to the partial so we can handle that. 1907 if (ga_grow(&ectx.ec_funcrefs, 1) == FAIL) 1908 { 1909 vim_free(pt); 1910 goto failed; 1911 } 1912 // Extra variable keeps the count of closures created 1913 // in the current function call. 1914 tv = STACK_TV_VAR(dfunc->df_varcount); 1915 ++tv->vval.v_number; 1916 1917 ((partial_T **)ectx.ec_funcrefs.ga_data) 1918 [ectx.ec_funcrefs.ga_len] = pt; 1919 ++pt->pt_refcount; 1920 ++ectx.ec_funcrefs.ga_len; 1921 } 1922 ++pt_dfunc->df_ufunc->uf_refcount; 1923 1924 tv = STACK_TV_BOT(0); 1925 ++ectx.ec_stack.ga_len; 1926 tv->vval.v_partial = pt; 1927 tv->v_type = VAR_PARTIAL; 1928 tv->v_lock = 0; 1929 } 1930 break; 1931 1932 // Create a global function from a lambda. 1933 case ISN_NEWFUNC: 1934 { 1935 newfunc_T *newfunc = &iptr->isn_arg.newfunc; 1936 1937 copy_func(newfunc->nf_lambda, newfunc->nf_global); 1938 } 1939 break; 1940 1941 // jump if a condition is met 1942 case ISN_JUMP: 1943 { 1944 jumpwhen_T when = iptr->isn_arg.jump.jump_when; 1945 int error = FALSE; 1946 int jump = TRUE; 1947 1948 if (when != JUMP_ALWAYS) 1949 { 1950 tv = STACK_TV_BOT(-1); 1951 if (when == JUMP_IF_COND_FALSE 1952 || when == JUMP_IF_FALSE 1953 || when == JUMP_IF_COND_TRUE) 1954 { 1955 SOURCING_LNUM = iptr->isn_lnum; 1956 jump = tv_get_bool_chk(tv, &error); 1957 if (error) 1958 goto on_error; 1959 } 1960 else 1961 jump = tv2bool(tv); 1962 if (when == JUMP_IF_FALSE 1963 || when == JUMP_AND_KEEP_IF_FALSE 1964 || when == JUMP_IF_COND_FALSE) 1965 jump = !jump; 1966 if (when == JUMP_IF_FALSE || !jump) 1967 { 1968 // drop the value from the stack 1969 clear_tv(tv); 1970 --ectx.ec_stack.ga_len; 1971 } 1972 } 1973 if (jump) 1974 ectx.ec_iidx = iptr->isn_arg.jump.jump_where; 1975 } 1976 break; 1977 1978 // top of a for loop 1979 case ISN_FOR: 1980 { 1981 list_T *list = STACK_TV_BOT(-1)->vval.v_list; 1982 typval_T *idxtv = 1983 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx); 1984 1985 // push the next item from the list 1986 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1987 goto failed; 1988 ++idxtv->vval.v_number; 1989 if (list == NULL || idxtv->vval.v_number >= list->lv_len) 1990 // past the end of the list, jump to "endfor" 1991 ectx.ec_iidx = iptr->isn_arg.forloop.for_end; 1992 else if (list->lv_first == &range_list_item) 1993 { 1994 // non-materialized range() list 1995 tv = STACK_TV_BOT(0); 1996 tv->v_type = VAR_NUMBER; 1997 tv->v_lock = 0; 1998 tv->vval.v_number = list_find_nr( 1999 list, idxtv->vval.v_number, NULL); 2000 ++ectx.ec_stack.ga_len; 2001 } 2002 else 2003 { 2004 listitem_T *li = list_find(list, idxtv->vval.v_number); 2005 2006 copy_tv(&li->li_tv, STACK_TV_BOT(0)); 2007 ++ectx.ec_stack.ga_len; 2008 } 2009 } 2010 break; 2011 2012 // start of ":try" block 2013 case ISN_TRY: 2014 { 2015 trycmd_T *trycmd = NULL; 2016 2017 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL) 2018 goto failed; 2019 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data) 2020 + ectx.ec_trystack.ga_len; 2021 ++ectx.ec_trystack.ga_len; 2022 ++trylevel; 2023 trycmd->tcd_frame_idx = ectx.ec_frame_idx; 2024 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch; 2025 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally; 2026 trycmd->tcd_caught = FALSE; 2027 trycmd->tcd_return = FALSE; 2028 } 2029 break; 2030 2031 case ISN_PUSHEXC: 2032 if (current_exception == NULL) 2033 { 2034 SOURCING_LNUM = iptr->isn_lnum; 2035 iemsg("Evaluating catch while current_exception is NULL"); 2036 goto failed; 2037 } 2038 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2039 goto failed; 2040 tv = STACK_TV_BOT(0); 2041 ++ectx.ec_stack.ga_len; 2042 tv->v_type = VAR_STRING; 2043 tv->v_lock = 0; 2044 tv->vval.v_string = vim_strsave( 2045 (char_u *)current_exception->value); 2046 break; 2047 2048 case ISN_CATCH: 2049 { 2050 garray_T *trystack = &ectx.ec_trystack; 2051 2052 if (trystack->ga_len > 0) 2053 { 2054 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) 2055 + trystack->ga_len - 1; 2056 trycmd->tcd_caught = TRUE; 2057 } 2058 did_emsg = got_int = did_throw = FALSE; 2059 catch_exception(current_exception); 2060 } 2061 break; 2062 2063 // end of ":try" block 2064 case ISN_ENDTRY: 2065 { 2066 garray_T *trystack = &ectx.ec_trystack; 2067 2068 if (trystack->ga_len > 0) 2069 { 2070 trycmd_T *trycmd = NULL; 2071 2072 --trystack->ga_len; 2073 --trylevel; 2074 ectx.ec_in_catch = FALSE; 2075 trycmd = ((trycmd_T *)trystack->ga_data) 2076 + trystack->ga_len; 2077 if (trycmd->tcd_caught && current_exception != NULL) 2078 { 2079 // discard the exception 2080 if (caught_stack == current_exception) 2081 caught_stack = caught_stack->caught; 2082 discard_current_exception(); 2083 } 2084 2085 if (trycmd->tcd_return) 2086 goto func_return; 2087 } 2088 } 2089 break; 2090 2091 case ISN_THROW: 2092 --ectx.ec_stack.ga_len; 2093 tv = STACK_TV_BOT(0); 2094 if (tv->vval.v_string == NULL 2095 || *skipwhite(tv->vval.v_string) == NUL) 2096 { 2097 vim_free(tv->vval.v_string); 2098 SOURCING_LNUM = iptr->isn_lnum; 2099 emsg(_(e_throw_with_empty_string)); 2100 goto failed; 2101 } 2102 2103 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL) 2104 { 2105 vim_free(tv->vval.v_string); 2106 goto failed; 2107 } 2108 did_throw = TRUE; 2109 break; 2110 2111 // compare with special values 2112 case ISN_COMPAREBOOL: 2113 case ISN_COMPARESPECIAL: 2114 { 2115 typval_T *tv1 = STACK_TV_BOT(-2); 2116 typval_T *tv2 = STACK_TV_BOT(-1); 2117 varnumber_T arg1 = tv1->vval.v_number; 2118 varnumber_T arg2 = tv2->vval.v_number; 2119 int res; 2120 2121 switch (iptr->isn_arg.op.op_type) 2122 { 2123 case EXPR_EQUAL: res = arg1 == arg2; break; 2124 case EXPR_NEQUAL: res = arg1 != arg2; break; 2125 default: res = 0; break; 2126 } 2127 2128 --ectx.ec_stack.ga_len; 2129 tv1->v_type = VAR_BOOL; 2130 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; 2131 } 2132 break; 2133 2134 // Operation with two number arguments 2135 case ISN_OPNR: 2136 case ISN_COMPARENR: 2137 { 2138 typval_T *tv1 = STACK_TV_BOT(-2); 2139 typval_T *tv2 = STACK_TV_BOT(-1); 2140 varnumber_T arg1 = tv1->vval.v_number; 2141 varnumber_T arg2 = tv2->vval.v_number; 2142 varnumber_T res; 2143 2144 switch (iptr->isn_arg.op.op_type) 2145 { 2146 case EXPR_MULT: res = arg1 * arg2; break; 2147 case EXPR_DIV: res = arg1 / arg2; break; 2148 case EXPR_REM: res = arg1 % arg2; break; 2149 case EXPR_SUB: res = arg1 - arg2; break; 2150 case EXPR_ADD: res = arg1 + arg2; break; 2151 2152 case EXPR_EQUAL: res = arg1 == arg2; break; 2153 case EXPR_NEQUAL: res = arg1 != arg2; break; 2154 case EXPR_GREATER: res = arg1 > arg2; break; 2155 case EXPR_GEQUAL: res = arg1 >= arg2; break; 2156 case EXPR_SMALLER: res = arg1 < arg2; break; 2157 case EXPR_SEQUAL: res = arg1 <= arg2; break; 2158 default: res = 0; break; 2159 } 2160 2161 --ectx.ec_stack.ga_len; 2162 if (iptr->isn_type == ISN_COMPARENR) 2163 { 2164 tv1->v_type = VAR_BOOL; 2165 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; 2166 } 2167 else 2168 tv1->vval.v_number = res; 2169 } 2170 break; 2171 2172 // Computation with two float arguments 2173 case ISN_OPFLOAT: 2174 case ISN_COMPAREFLOAT: 2175 #ifdef FEAT_FLOAT 2176 { 2177 typval_T *tv1 = STACK_TV_BOT(-2); 2178 typval_T *tv2 = STACK_TV_BOT(-1); 2179 float_T arg1 = tv1->vval.v_float; 2180 float_T arg2 = tv2->vval.v_float; 2181 float_T res = 0; 2182 int cmp = FALSE; 2183 2184 switch (iptr->isn_arg.op.op_type) 2185 { 2186 case EXPR_MULT: res = arg1 * arg2; break; 2187 case EXPR_DIV: res = arg1 / arg2; break; 2188 case EXPR_SUB: res = arg1 - arg2; break; 2189 case EXPR_ADD: res = arg1 + arg2; break; 2190 2191 case EXPR_EQUAL: cmp = arg1 == arg2; break; 2192 case EXPR_NEQUAL: cmp = arg1 != arg2; break; 2193 case EXPR_GREATER: cmp = arg1 > arg2; break; 2194 case EXPR_GEQUAL: cmp = arg1 >= arg2; break; 2195 case EXPR_SMALLER: cmp = arg1 < arg2; break; 2196 case EXPR_SEQUAL: cmp = arg1 <= arg2; break; 2197 default: cmp = 0; break; 2198 } 2199 --ectx.ec_stack.ga_len; 2200 if (iptr->isn_type == ISN_COMPAREFLOAT) 2201 { 2202 tv1->v_type = VAR_BOOL; 2203 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 2204 } 2205 else 2206 tv1->vval.v_float = res; 2207 } 2208 #endif 2209 break; 2210 2211 case ISN_COMPARELIST: 2212 { 2213 typval_T *tv1 = STACK_TV_BOT(-2); 2214 typval_T *tv2 = STACK_TV_BOT(-1); 2215 list_T *arg1 = tv1->vval.v_list; 2216 list_T *arg2 = tv2->vval.v_list; 2217 int cmp = FALSE; 2218 int ic = iptr->isn_arg.op.op_ic; 2219 2220 switch (iptr->isn_arg.op.op_type) 2221 { 2222 case EXPR_EQUAL: cmp = 2223 list_equal(arg1, arg2, ic, FALSE); break; 2224 case EXPR_NEQUAL: cmp = 2225 !list_equal(arg1, arg2, ic, FALSE); break; 2226 case EXPR_IS: cmp = arg1 == arg2; break; 2227 case EXPR_ISNOT: cmp = arg1 != arg2; break; 2228 default: cmp = 0; break; 2229 } 2230 --ectx.ec_stack.ga_len; 2231 clear_tv(tv1); 2232 clear_tv(tv2); 2233 tv1->v_type = VAR_BOOL; 2234 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 2235 } 2236 break; 2237 2238 case ISN_COMPAREBLOB: 2239 { 2240 typval_T *tv1 = STACK_TV_BOT(-2); 2241 typval_T *tv2 = STACK_TV_BOT(-1); 2242 blob_T *arg1 = tv1->vval.v_blob; 2243 blob_T *arg2 = tv2->vval.v_blob; 2244 int cmp = FALSE; 2245 2246 switch (iptr->isn_arg.op.op_type) 2247 { 2248 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break; 2249 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break; 2250 case EXPR_IS: cmp = arg1 == arg2; break; 2251 case EXPR_ISNOT: cmp = arg1 != arg2; break; 2252 default: cmp = 0; break; 2253 } 2254 --ectx.ec_stack.ga_len; 2255 clear_tv(tv1); 2256 clear_tv(tv2); 2257 tv1->v_type = VAR_BOOL; 2258 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 2259 } 2260 break; 2261 2262 // TODO: handle separately 2263 case ISN_COMPARESTRING: 2264 case ISN_COMPAREDICT: 2265 case ISN_COMPAREFUNC: 2266 case ISN_COMPAREANY: 2267 { 2268 typval_T *tv1 = STACK_TV_BOT(-2); 2269 typval_T *tv2 = STACK_TV_BOT(-1); 2270 exptype_T exptype = iptr->isn_arg.op.op_type; 2271 int ic = iptr->isn_arg.op.op_ic; 2272 2273 SOURCING_LNUM = iptr->isn_lnum; 2274 typval_compare(tv1, tv2, exptype, ic); 2275 clear_tv(tv2); 2276 --ectx.ec_stack.ga_len; 2277 } 2278 break; 2279 2280 case ISN_ADDLIST: 2281 case ISN_ADDBLOB: 2282 { 2283 typval_T *tv1 = STACK_TV_BOT(-2); 2284 typval_T *tv2 = STACK_TV_BOT(-1); 2285 2286 // add two lists or blobs 2287 if (iptr->isn_type == ISN_ADDLIST) 2288 eval_addlist(tv1, tv2); 2289 else 2290 eval_addblob(tv1, tv2); 2291 clear_tv(tv2); 2292 --ectx.ec_stack.ga_len; 2293 } 2294 break; 2295 2296 case ISN_LISTAPPEND: 2297 { 2298 typval_T *tv1 = STACK_TV_BOT(-2); 2299 typval_T *tv2 = STACK_TV_BOT(-1); 2300 list_T *l = tv1->vval.v_list; 2301 2302 // add an item to a list 2303 if (l == NULL) 2304 { 2305 SOURCING_LNUM = iptr->isn_lnum; 2306 emsg(_(e_cannot_add_to_null_list)); 2307 goto on_error; 2308 } 2309 if (list_append_tv(l, tv2) == FAIL) 2310 goto failed; 2311 clear_tv(tv2); 2312 --ectx.ec_stack.ga_len; 2313 } 2314 break; 2315 2316 case ISN_BLOBAPPEND: 2317 { 2318 typval_T *tv1 = STACK_TV_BOT(-2); 2319 typval_T *tv2 = STACK_TV_BOT(-1); 2320 blob_T *b = tv1->vval.v_blob; 2321 int error = FALSE; 2322 varnumber_T n; 2323 2324 // add a number to a blob 2325 if (b == NULL) 2326 { 2327 SOURCING_LNUM = iptr->isn_lnum; 2328 emsg(_(e_cannot_add_to_null_blob)); 2329 goto on_error; 2330 } 2331 n = tv_get_number_chk(tv2, &error); 2332 if (error) 2333 goto on_error; 2334 ga_append(&b->bv_ga, (int)n); 2335 --ectx.ec_stack.ga_len; 2336 } 2337 break; 2338 2339 // Computation with two arguments of unknown type 2340 case ISN_OPANY: 2341 { 2342 typval_T *tv1 = STACK_TV_BOT(-2); 2343 typval_T *tv2 = STACK_TV_BOT(-1); 2344 varnumber_T n1, n2; 2345 #ifdef FEAT_FLOAT 2346 float_T f1 = 0, f2 = 0; 2347 #endif 2348 int error = FALSE; 2349 2350 if (iptr->isn_arg.op.op_type == EXPR_ADD) 2351 { 2352 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST) 2353 { 2354 eval_addlist(tv1, tv2); 2355 clear_tv(tv2); 2356 --ectx.ec_stack.ga_len; 2357 break; 2358 } 2359 else if (tv1->v_type == VAR_BLOB 2360 && tv2->v_type == VAR_BLOB) 2361 { 2362 eval_addblob(tv1, tv2); 2363 clear_tv(tv2); 2364 --ectx.ec_stack.ga_len; 2365 break; 2366 } 2367 } 2368 #ifdef FEAT_FLOAT 2369 if (tv1->v_type == VAR_FLOAT) 2370 { 2371 f1 = tv1->vval.v_float; 2372 n1 = 0; 2373 } 2374 else 2375 #endif 2376 { 2377 n1 = tv_get_number_chk(tv1, &error); 2378 if (error) 2379 goto on_error; 2380 #ifdef FEAT_FLOAT 2381 if (tv2->v_type == VAR_FLOAT) 2382 f1 = n1; 2383 #endif 2384 } 2385 #ifdef FEAT_FLOAT 2386 if (tv2->v_type == VAR_FLOAT) 2387 { 2388 f2 = tv2->vval.v_float; 2389 n2 = 0; 2390 } 2391 else 2392 #endif 2393 { 2394 n2 = tv_get_number_chk(tv2, &error); 2395 if (error) 2396 goto on_error; 2397 #ifdef FEAT_FLOAT 2398 if (tv1->v_type == VAR_FLOAT) 2399 f2 = n2; 2400 #endif 2401 } 2402 #ifdef FEAT_FLOAT 2403 // if there is a float on either side the result is a float 2404 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT) 2405 { 2406 switch (iptr->isn_arg.op.op_type) 2407 { 2408 case EXPR_MULT: f1 = f1 * f2; break; 2409 case EXPR_DIV: f1 = f1 / f2; break; 2410 case EXPR_SUB: f1 = f1 - f2; break; 2411 case EXPR_ADD: f1 = f1 + f2; break; 2412 default: SOURCING_LNUM = iptr->isn_lnum; 2413 emsg(_(e_modulus)); 2414 goto on_error; 2415 } 2416 clear_tv(tv1); 2417 clear_tv(tv2); 2418 tv1->v_type = VAR_FLOAT; 2419 tv1->vval.v_float = f1; 2420 --ectx.ec_stack.ga_len; 2421 } 2422 else 2423 #endif 2424 { 2425 switch (iptr->isn_arg.op.op_type) 2426 { 2427 case EXPR_MULT: n1 = n1 * n2; break; 2428 case EXPR_DIV: n1 = num_divide(n1, n2); break; 2429 case EXPR_SUB: n1 = n1 - n2; break; 2430 case EXPR_ADD: n1 = n1 + n2; break; 2431 default: n1 = num_modulus(n1, n2); break; 2432 } 2433 clear_tv(tv1); 2434 clear_tv(tv2); 2435 tv1->v_type = VAR_NUMBER; 2436 tv1->vval.v_number = n1; 2437 --ectx.ec_stack.ga_len; 2438 } 2439 } 2440 break; 2441 2442 case ISN_CONCAT: 2443 { 2444 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string; 2445 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string; 2446 char_u *res; 2447 2448 res = concat_str(str1, str2); 2449 clear_tv(STACK_TV_BOT(-2)); 2450 clear_tv(STACK_TV_BOT(-1)); 2451 --ectx.ec_stack.ga_len; 2452 STACK_TV_BOT(-1)->vval.v_string = res; 2453 } 2454 break; 2455 2456 case ISN_STRINDEX: 2457 case ISN_STRSLICE: 2458 { 2459 int is_slice = iptr->isn_type == ISN_STRSLICE; 2460 varnumber_T n1 = 0, n2; 2461 char_u *res; 2462 2463 // string index: string is at stack-2, index at stack-1 2464 // string slice: string is at stack-3, first index at 2465 // stack-2, second index at stack-1 2466 if (is_slice) 2467 { 2468 tv = STACK_TV_BOT(-2); 2469 n1 = tv->vval.v_number; 2470 } 2471 2472 tv = STACK_TV_BOT(-1); 2473 n2 = tv->vval.v_number; 2474 2475 ectx.ec_stack.ga_len -= is_slice ? 2 : 1; 2476 tv = STACK_TV_BOT(-1); 2477 if (is_slice) 2478 // Slice: Select the characters from the string 2479 res = string_slice(tv->vval.v_string, n1, n2); 2480 else 2481 // Index: The resulting variable is a string of a 2482 // single character. If the index is too big or 2483 // negative the result is empty. 2484 res = char_from_string(tv->vval.v_string, n2); 2485 vim_free(tv->vval.v_string); 2486 tv->vval.v_string = res; 2487 } 2488 break; 2489 2490 case ISN_LISTINDEX: 2491 case ISN_LISTSLICE: 2492 { 2493 int is_slice = iptr->isn_type == ISN_LISTSLICE; 2494 list_T *list; 2495 varnumber_T n1, n2; 2496 2497 // list index: list is at stack-2, index at stack-1 2498 // list slice: list is at stack-3, indexes at stack-2 and 2499 // stack-1 2500 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2); 2501 list = tv->vval.v_list; 2502 2503 tv = STACK_TV_BOT(-1); 2504 n1 = n2 = tv->vval.v_number; 2505 clear_tv(tv); 2506 2507 if (is_slice) 2508 { 2509 tv = STACK_TV_BOT(-2); 2510 n1 = tv->vval.v_number; 2511 clear_tv(tv); 2512 } 2513 2514 ectx.ec_stack.ga_len -= is_slice ? 2 : 1; 2515 tv = STACK_TV_BOT(-1); 2516 SOURCING_LNUM = iptr->isn_lnum; 2517 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE) 2518 == FAIL) 2519 goto on_error; 2520 } 2521 break; 2522 2523 case ISN_ANYINDEX: 2524 case ISN_ANYSLICE: 2525 { 2526 int is_slice = iptr->isn_type == ISN_ANYSLICE; 2527 typval_T *var1, *var2; 2528 int res; 2529 2530 // index: composite is at stack-2, index at stack-1 2531 // slice: composite is at stack-3, indexes at stack-2 and 2532 // stack-1 2533 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2); 2534 SOURCING_LNUM = iptr->isn_lnum; 2535 if (check_can_index(tv, TRUE, TRUE) == FAIL) 2536 goto on_error; 2537 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1); 2538 var2 = is_slice ? STACK_TV_BOT(-1) : NULL; 2539 res = eval_index_inner(tv, is_slice, 2540 var1, var2, NULL, -1, TRUE); 2541 clear_tv(var1); 2542 if (is_slice) 2543 clear_tv(var2); 2544 ectx.ec_stack.ga_len -= is_slice ? 2 : 1; 2545 if (res == FAIL) 2546 goto on_error; 2547 } 2548 break; 2549 2550 case ISN_SLICE: 2551 { 2552 list_T *list; 2553 int count = iptr->isn_arg.number; 2554 2555 // type will have been checked to be a list 2556 tv = STACK_TV_BOT(-1); 2557 list = tv->vval.v_list; 2558 2559 // no error for short list, expect it to be checked earlier 2560 if (list != NULL && list->lv_len >= count) 2561 { 2562 list_T *newlist = list_slice(list, 2563 count, list->lv_len - 1); 2564 2565 if (newlist != NULL) 2566 { 2567 list_unref(list); 2568 tv->vval.v_list = newlist; 2569 ++newlist->lv_refcount; 2570 } 2571 } 2572 } 2573 break; 2574 2575 case ISN_GETITEM: 2576 { 2577 listitem_T *li; 2578 int index = iptr->isn_arg.number; 2579 2580 // Get list item: list is at stack-1, push item. 2581 // List type and length is checked for when compiling. 2582 tv = STACK_TV_BOT(-1); 2583 li = list_find(tv->vval.v_list, index); 2584 2585 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2586 goto failed; 2587 ++ectx.ec_stack.ga_len; 2588 copy_tv(&li->li_tv, STACK_TV_BOT(-1)); 2589 } 2590 break; 2591 2592 case ISN_MEMBER: 2593 { 2594 dict_T *dict; 2595 char_u *key; 2596 dictitem_T *di; 2597 typval_T temp_tv; 2598 2599 // dict member: dict is at stack-2, key at stack-1 2600 tv = STACK_TV_BOT(-2); 2601 // no need to check for VAR_DICT, CHECKTYPE will check. 2602 dict = tv->vval.v_dict; 2603 2604 tv = STACK_TV_BOT(-1); 2605 // no need to check for VAR_STRING, 2STRING will check. 2606 key = tv->vval.v_string; 2607 2608 if ((di = dict_find(dict, key, -1)) == NULL) 2609 { 2610 SOURCING_LNUM = iptr->isn_lnum; 2611 semsg(_(e_dictkey), key); 2612 goto on_error; 2613 } 2614 clear_tv(tv); 2615 --ectx.ec_stack.ga_len; 2616 // Clear the dict after getting the item, to avoid that it 2617 // make the item invalid. 2618 tv = STACK_TV_BOT(-1); 2619 temp_tv = *tv; 2620 copy_tv(&di->di_tv, tv); 2621 clear_tv(&temp_tv); 2622 } 2623 break; 2624 2625 // dict member with string key 2626 case ISN_STRINGMEMBER: 2627 { 2628 dict_T *dict; 2629 dictitem_T *di; 2630 typval_T temp_tv; 2631 2632 tv = STACK_TV_BOT(-1); 2633 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL) 2634 { 2635 SOURCING_LNUM = iptr->isn_lnum; 2636 emsg(_(e_dictreq)); 2637 goto on_error; 2638 } 2639 dict = tv->vval.v_dict; 2640 2641 if ((di = dict_find(dict, iptr->isn_arg.string, -1)) 2642 == NULL) 2643 { 2644 SOURCING_LNUM = iptr->isn_lnum; 2645 semsg(_(e_dictkey), iptr->isn_arg.string); 2646 goto on_error; 2647 } 2648 // Clear the dict after getting the item, to avoid that it 2649 // make the item invalid. 2650 temp_tv = *tv; 2651 copy_tv(&di->di_tv, tv); 2652 clear_tv(&temp_tv); 2653 } 2654 break; 2655 2656 case ISN_NEGATENR: 2657 tv = STACK_TV_BOT(-1); 2658 if (tv->v_type != VAR_NUMBER 2659 #ifdef FEAT_FLOAT 2660 && tv->v_type != VAR_FLOAT 2661 #endif 2662 ) 2663 { 2664 SOURCING_LNUM = iptr->isn_lnum; 2665 emsg(_(e_number_exp)); 2666 goto on_error; 2667 } 2668 #ifdef FEAT_FLOAT 2669 if (tv->v_type == VAR_FLOAT) 2670 tv->vval.v_float = -tv->vval.v_float; 2671 else 2672 #endif 2673 tv->vval.v_number = -tv->vval.v_number; 2674 break; 2675 2676 case ISN_CHECKNR: 2677 { 2678 int error = FALSE; 2679 2680 tv = STACK_TV_BOT(-1); 2681 SOURCING_LNUM = iptr->isn_lnum; 2682 if (check_not_string(tv) == FAIL) 2683 goto on_error; 2684 (void)tv_get_number_chk(tv, &error); 2685 if (error) 2686 goto on_error; 2687 } 2688 break; 2689 2690 case ISN_CHECKTYPE: 2691 { 2692 checktype_T *ct = &iptr->isn_arg.type; 2693 2694 tv = STACK_TV_BOT(ct->ct_off); 2695 SOURCING_LNUM = iptr->isn_lnum; 2696 if (check_typval_type(ct->ct_type, tv, 0) == FAIL) 2697 goto on_error; 2698 2699 // number 0 is FALSE, number 1 is TRUE 2700 if (tv->v_type == VAR_NUMBER 2701 && ct->ct_type->tt_type == VAR_BOOL 2702 && (tv->vval.v_number == 0 2703 || tv->vval.v_number == 1)) 2704 { 2705 tv->v_type = VAR_BOOL; 2706 tv->vval.v_number = tv->vval.v_number 2707 ? VVAL_TRUE : VVAL_FALSE; 2708 } 2709 } 2710 break; 2711 2712 case ISN_CHECKLEN: 2713 { 2714 int min_len = iptr->isn_arg.checklen.cl_min_len; 2715 list_T *list = NULL; 2716 2717 tv = STACK_TV_BOT(-1); 2718 if (tv->v_type == VAR_LIST) 2719 list = tv->vval.v_list; 2720 if (list == NULL || list->lv_len < min_len 2721 || (list->lv_len > min_len 2722 && !iptr->isn_arg.checklen.cl_more_OK)) 2723 { 2724 SOURCING_LNUM = iptr->isn_lnum; 2725 semsg(_(e_expected_nr_items_but_got_nr), 2726 min_len, list == NULL ? 0 : list->lv_len); 2727 goto on_error; 2728 } 2729 } 2730 break; 2731 2732 case ISN_2BOOL: 2733 case ISN_COND2BOOL: 2734 { 2735 int n; 2736 int error = FALSE; 2737 2738 tv = STACK_TV_BOT(-1); 2739 if (iptr->isn_type == ISN_2BOOL) 2740 { 2741 n = tv2bool(tv); 2742 if (iptr->isn_arg.number) // invert 2743 n = !n; 2744 } 2745 else 2746 { 2747 SOURCING_LNUM = iptr->isn_lnum; 2748 n = tv_get_bool_chk(tv, &error); 2749 if (error) 2750 goto on_error; 2751 } 2752 clear_tv(tv); 2753 tv->v_type = VAR_BOOL; 2754 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE; 2755 } 2756 break; 2757 2758 case ISN_2STRING: 2759 case ISN_2STRING_ANY: 2760 { 2761 char_u *str; 2762 2763 tv = STACK_TV_BOT(iptr->isn_arg.number); 2764 if (tv->v_type != VAR_STRING) 2765 { 2766 if (iptr->isn_type == ISN_2STRING_ANY) 2767 { 2768 switch (tv->v_type) 2769 { 2770 case VAR_SPECIAL: 2771 case VAR_BOOL: 2772 case VAR_NUMBER: 2773 case VAR_FLOAT: 2774 case VAR_BLOB: break; 2775 default: to_string_error(tv->v_type); 2776 goto on_error; 2777 } 2778 } 2779 str = typval_tostring(tv); 2780 clear_tv(tv); 2781 tv->v_type = VAR_STRING; 2782 tv->vval.v_string = str; 2783 } 2784 } 2785 break; 2786 2787 case ISN_PUT: 2788 { 2789 int regname = iptr->isn_arg.put.put_regname; 2790 linenr_T lnum = iptr->isn_arg.put.put_lnum; 2791 char_u *expr = NULL; 2792 int dir = FORWARD; 2793 2794 if (regname == '=') 2795 { 2796 tv = STACK_TV_BOT(-1); 2797 if (tv->v_type == VAR_STRING) 2798 expr = tv->vval.v_string; 2799 else 2800 { 2801 expr = typval_tostring(tv); // allocates value 2802 clear_tv(tv); 2803 } 2804 --ectx.ec_stack.ga_len; 2805 } 2806 if (lnum == -2) 2807 // :put! above cursor 2808 dir = BACKWARD; 2809 else if (lnum >= 0) 2810 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum; 2811 check_cursor(); 2812 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE); 2813 vim_free(expr); 2814 } 2815 break; 2816 2817 case ISN_SHUFFLE: 2818 { 2819 typval_T tmp_tv; 2820 int item = iptr->isn_arg.shuffle.shfl_item; 2821 int up = iptr->isn_arg.shuffle.shfl_up; 2822 2823 tmp_tv = *STACK_TV_BOT(-item); 2824 for ( ; up > 0 && item > 1; --up) 2825 { 2826 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1); 2827 --item; 2828 } 2829 *STACK_TV_BOT(-item) = tmp_tv; 2830 } 2831 break; 2832 2833 case ISN_DROP: 2834 --ectx.ec_stack.ga_len; 2835 clear_tv(STACK_TV_BOT(0)); 2836 break; 2837 } 2838 continue; 2839 2840 func_return: 2841 // Restore previous function. If the frame pointer is where we started 2842 // then there is none and we are done. 2843 if (ectx.ec_frame_idx == initial_frame_idx) 2844 goto done; 2845 2846 if (func_return(&ectx) == FAIL) 2847 // only fails when out of memory 2848 goto failed; 2849 continue; 2850 2851 on_error: 2852 if (trylevel == 0) 2853 goto failed; 2854 } 2855 2856 done: 2857 // function finished, get result from the stack. 2858 tv = STACK_TV_BOT(-1); 2859 *rettv = *tv; 2860 tv->v_type = VAR_UNKNOWN; 2861 ret = OK; 2862 2863 failed: 2864 // When failed need to unwind the call stack. 2865 while (ectx.ec_frame_idx != initial_frame_idx) 2866 func_return(&ectx); 2867 2868 // Deal with any remaining closures, they may be in use somewhere. 2869 if (ectx.ec_funcrefs.ga_len > 0) 2870 handle_closure_in_use(&ectx, FALSE); 2871 2872 estack_pop(); 2873 current_sctx = save_current_sctx; 2874 2875 if (*msg_list != NULL && saved_msg_list != NULL) 2876 { 2877 msglist_T **plist = saved_msg_list; 2878 2879 // Append entries from the current msg_list (uncaught exceptions) to 2880 // the saved msg_list. 2881 while (*plist != NULL) 2882 plist = &(*plist)->next; 2883 2884 *plist = *msg_list; 2885 } 2886 msg_list = saved_msg_list; 2887 2888 failed_early: 2889 // Free all local variables, but not arguments. 2890 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx) 2891 clear_tv(STACK_TV(idx)); 2892 2893 vim_free(ectx.ec_stack.ga_data); 2894 vim_free(ectx.ec_trystack.ga_data); 2895 2896 // Not sure if this is necessary. 2897 suppress_errthrow = save_suppress_errthrow; 2898 2899 if (ret != OK && called_emsg == called_emsg_before) 2900 semsg(_(e_unknown_error_while_executing_str), 2901 printable_func_name(ufunc)); 2902 return ret; 2903 } 2904 2905 /* 2906 * ":dissassemble". 2907 * We don't really need this at runtime, but we do have tests that require it, 2908 * so always include this. 2909 */ 2910 void 2911 ex_disassemble(exarg_T *eap) 2912 { 2913 char_u *arg = eap->arg; 2914 char_u *fname; 2915 ufunc_T *ufunc; 2916 dfunc_T *dfunc; 2917 isn_T *instr; 2918 int current; 2919 int line_idx = 0; 2920 int prev_current = 0; 2921 int is_global = FALSE; 2922 2923 if (STRNCMP(arg, "<lambda>", 8) == 0) 2924 { 2925 arg += 8; 2926 (void)getdigits(&arg); 2927 fname = vim_strnsave(eap->arg, arg - eap->arg); 2928 } 2929 else 2930 fname = trans_function_name(&arg, &is_global, FALSE, 2931 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL); 2932 if (fname == NULL) 2933 { 2934 semsg(_(e_invarg2), eap->arg); 2935 return; 2936 } 2937 2938 ufunc = find_func(fname, is_global, NULL); 2939 if (ufunc == NULL) 2940 { 2941 char_u *p = untrans_function_name(fname); 2942 2943 if (p != NULL) 2944 // Try again without making it script-local. 2945 ufunc = find_func(p, FALSE, NULL); 2946 } 2947 vim_free(fname); 2948 if (ufunc == NULL) 2949 { 2950 semsg(_(e_cannot_find_function_str), eap->arg); 2951 return; 2952 } 2953 if (ufunc->uf_def_status == UF_TO_BE_COMPILED 2954 && compile_def_function(ufunc, FALSE, NULL) == FAIL) 2955 return; 2956 if (ufunc->uf_def_status != UF_COMPILED) 2957 { 2958 semsg(_(e_function_is_not_compiled_str), eap->arg); 2959 return; 2960 } 2961 if (ufunc->uf_name_exp != NULL) 2962 msg((char *)ufunc->uf_name_exp); 2963 else 2964 msg((char *)ufunc->uf_name); 2965 2966 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; 2967 instr = dfunc->df_instr; 2968 for (current = 0; current < dfunc->df_instr_count; ++current) 2969 { 2970 isn_T *iptr = &instr[current]; 2971 char *line; 2972 2973 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len) 2974 { 2975 if (current > prev_current) 2976 { 2977 msg_puts("\n\n"); 2978 prev_current = current; 2979 } 2980 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++]; 2981 if (line != NULL) 2982 msg(line); 2983 } 2984 2985 switch (iptr->isn_type) 2986 { 2987 case ISN_EXEC: 2988 smsg("%4d EXEC %s", current, iptr->isn_arg.string); 2989 break; 2990 case ISN_EXECCONCAT: 2991 smsg("%4d EXECCONCAT %lld", current, 2992 (long long)iptr->isn_arg.number); 2993 break; 2994 case ISN_ECHO: 2995 { 2996 echo_T *echo = &iptr->isn_arg.echo; 2997 2998 smsg("%4d %s %d", current, 2999 echo->echo_with_white ? "ECHO" : "ECHON", 3000 echo->echo_count); 3001 } 3002 break; 3003 case ISN_EXECUTE: 3004 smsg("%4d EXECUTE %lld", current, 3005 (long long)(iptr->isn_arg.number)); 3006 break; 3007 case ISN_ECHOMSG: 3008 smsg("%4d ECHOMSG %lld", current, 3009 (long long)(iptr->isn_arg.number)); 3010 break; 3011 case ISN_ECHOERR: 3012 smsg("%4d ECHOERR %lld", current, 3013 (long long)(iptr->isn_arg.number)); 3014 break; 3015 case ISN_LOAD: 3016 case ISN_LOADOUTER: 3017 { 3018 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER"; 3019 3020 if (iptr->isn_arg.number < 0) 3021 smsg("%4d LOAD%s arg[%lld]", current, add, 3022 (long long)(iptr->isn_arg.number 3023 + STACK_FRAME_SIZE)); 3024 else 3025 smsg("%4d LOAD%s $%lld", current, add, 3026 (long long)(iptr->isn_arg.number)); 3027 } 3028 break; 3029 case ISN_LOADV: 3030 smsg("%4d LOADV v:%s", current, 3031 get_vim_var_name(iptr->isn_arg.number)); 3032 break; 3033 case ISN_LOADSCRIPT: 3034 { 3035 scriptitem_T *si = 3036 SCRIPT_ITEM(iptr->isn_arg.script.script_sid); 3037 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 3038 + iptr->isn_arg.script.script_idx; 3039 3040 smsg("%4d LOADSCRIPT %s-%d from %s", current, 3041 sv->sv_name, 3042 iptr->isn_arg.script.script_idx, 3043 si->sn_name); 3044 } 3045 break; 3046 case ISN_LOADS: 3047 { 3048 scriptitem_T *si = SCRIPT_ITEM( 3049 iptr->isn_arg.loadstore.ls_sid); 3050 3051 smsg("%4d LOADS s:%s from %s", current, 3052 iptr->isn_arg.loadstore.ls_name, si->sn_name); 3053 } 3054 break; 3055 case ISN_LOADG: 3056 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string); 3057 break; 3058 case ISN_LOADB: 3059 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string); 3060 break; 3061 case ISN_LOADW: 3062 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string); 3063 break; 3064 case ISN_LOADT: 3065 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string); 3066 break; 3067 case ISN_LOADGDICT: 3068 smsg("%4d LOAD g:", current); 3069 break; 3070 case ISN_LOADBDICT: 3071 smsg("%4d LOAD b:", current); 3072 break; 3073 case ISN_LOADWDICT: 3074 smsg("%4d LOAD w:", current); 3075 break; 3076 case ISN_LOADTDICT: 3077 smsg("%4d LOAD t:", current); 3078 break; 3079 case ISN_LOADOPT: 3080 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string); 3081 break; 3082 case ISN_LOADENV: 3083 smsg("%4d LOADENV %s", current, iptr->isn_arg.string); 3084 break; 3085 case ISN_LOADREG: 3086 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number)); 3087 break; 3088 3089 case ISN_STORE: 3090 case ISN_STOREOUTER: 3091 { 3092 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER"; 3093 3094 if (iptr->isn_arg.number < 0) 3095 smsg("%4d STORE%s arg[%lld]", current, add, 3096 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE)); 3097 else 3098 smsg("%4d STORE%s $%lld", current, add, 3099 (long long)(iptr->isn_arg.number)); 3100 } 3101 break; 3102 case ISN_STOREV: 3103 smsg("%4d STOREV v:%s", current, 3104 get_vim_var_name(iptr->isn_arg.number)); 3105 break; 3106 case ISN_STOREG: 3107 smsg("%4d STOREG %s", current, iptr->isn_arg.string); 3108 break; 3109 case ISN_STOREB: 3110 smsg("%4d STOREB %s", current, iptr->isn_arg.string); 3111 break; 3112 case ISN_STOREW: 3113 smsg("%4d STOREW %s", current, iptr->isn_arg.string); 3114 break; 3115 case ISN_STORET: 3116 smsg("%4d STORET %s", current, iptr->isn_arg.string); 3117 break; 3118 case ISN_STORES: 3119 { 3120 scriptitem_T *si = SCRIPT_ITEM( 3121 iptr->isn_arg.loadstore.ls_sid); 3122 3123 smsg("%4d STORES %s in %s", current, 3124 iptr->isn_arg.loadstore.ls_name, si->sn_name); 3125 } 3126 break; 3127 case ISN_STORESCRIPT: 3128 { 3129 scriptitem_T *si = 3130 SCRIPT_ITEM(iptr->isn_arg.script.script_sid); 3131 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 3132 + iptr->isn_arg.script.script_idx; 3133 3134 smsg("%4d STORESCRIPT %s-%d in %s", current, 3135 sv->sv_name, 3136 iptr->isn_arg.script.script_idx, 3137 si->sn_name); 3138 } 3139 break; 3140 case ISN_STOREOPT: 3141 smsg("%4d STOREOPT &%s", current, 3142 iptr->isn_arg.storeopt.so_name); 3143 break; 3144 case ISN_STOREENV: 3145 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string); 3146 break; 3147 case ISN_STOREREG: 3148 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number); 3149 break; 3150 case ISN_STORENR: 3151 smsg("%4d STORE %lld in $%d", current, 3152 iptr->isn_arg.storenr.stnr_val, 3153 iptr->isn_arg.storenr.stnr_idx); 3154 break; 3155 3156 case ISN_STORELIST: 3157 smsg("%4d STORELIST", current); 3158 break; 3159 3160 case ISN_STOREDICT: 3161 smsg("%4d STOREDICT", current); 3162 break; 3163 3164 // constants 3165 case ISN_PUSHNR: 3166 smsg("%4d PUSHNR %lld", current, 3167 (long long)(iptr->isn_arg.number)); 3168 break; 3169 case ISN_PUSHBOOL: 3170 case ISN_PUSHSPEC: 3171 smsg("%4d PUSH %s", current, 3172 get_var_special_name(iptr->isn_arg.number)); 3173 break; 3174 case ISN_PUSHF: 3175 #ifdef FEAT_FLOAT 3176 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber); 3177 #endif 3178 break; 3179 case ISN_PUSHS: 3180 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string); 3181 break; 3182 case ISN_PUSHBLOB: 3183 { 3184 char_u *r; 3185 char_u numbuf[NUMBUFLEN]; 3186 char_u *tofree; 3187 3188 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf); 3189 smsg("%4d PUSHBLOB %s", current, r); 3190 vim_free(tofree); 3191 } 3192 break; 3193 case ISN_PUSHFUNC: 3194 { 3195 char *name = (char *)iptr->isn_arg.string; 3196 3197 smsg("%4d PUSHFUNC \"%s\"", current, 3198 name == NULL ? "[none]" : name); 3199 } 3200 break; 3201 case ISN_PUSHCHANNEL: 3202 #ifdef FEAT_JOB_CHANNEL 3203 { 3204 channel_T *channel = iptr->isn_arg.channel; 3205 3206 smsg("%4d PUSHCHANNEL %d", current, 3207 channel == NULL ? 0 : channel->ch_id); 3208 } 3209 #endif 3210 break; 3211 case ISN_PUSHJOB: 3212 #ifdef FEAT_JOB_CHANNEL 3213 { 3214 typval_T tv; 3215 char_u *name; 3216 3217 tv.v_type = VAR_JOB; 3218 tv.vval.v_job = iptr->isn_arg.job; 3219 name = tv_get_string(&tv); 3220 smsg("%4d PUSHJOB \"%s\"", current, name); 3221 } 3222 #endif 3223 break; 3224 case ISN_PUSHEXC: 3225 smsg("%4d PUSH v:exception", current); 3226 break; 3227 case ISN_UNLET: 3228 smsg("%4d UNLET%s %s", current, 3229 iptr->isn_arg.unlet.ul_forceit ? "!" : "", 3230 iptr->isn_arg.unlet.ul_name); 3231 break; 3232 case ISN_UNLETENV: 3233 smsg("%4d UNLETENV%s $%s", current, 3234 iptr->isn_arg.unlet.ul_forceit ? "!" : "", 3235 iptr->isn_arg.unlet.ul_name); 3236 break; 3237 case ISN_LOCKCONST: 3238 smsg("%4d LOCKCONST", current); 3239 break; 3240 case ISN_NEWLIST: 3241 smsg("%4d NEWLIST size %lld", current, 3242 (long long)(iptr->isn_arg.number)); 3243 break; 3244 case ISN_NEWDICT: 3245 smsg("%4d NEWDICT size %lld", current, 3246 (long long)(iptr->isn_arg.number)); 3247 break; 3248 3249 // function call 3250 case ISN_BCALL: 3251 { 3252 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc; 3253 3254 smsg("%4d BCALL %s(argc %d)", current, 3255 internal_func_name(cbfunc->cbf_idx), 3256 cbfunc->cbf_argcount); 3257 } 3258 break; 3259 case ISN_DCALL: 3260 { 3261 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc; 3262 dfunc_T *df = ((dfunc_T *)def_functions.ga_data) 3263 + cdfunc->cdf_idx; 3264 3265 smsg("%4d DCALL %s(argc %d)", current, 3266 df->df_ufunc->uf_name_exp != NULL 3267 ? df->df_ufunc->uf_name_exp 3268 : df->df_ufunc->uf_name, cdfunc->cdf_argcount); 3269 } 3270 break; 3271 case ISN_UCALL: 3272 { 3273 cufunc_T *cufunc = &iptr->isn_arg.ufunc; 3274 3275 smsg("%4d UCALL %s(argc %d)", current, 3276 cufunc->cuf_name, cufunc->cuf_argcount); 3277 } 3278 break; 3279 case ISN_PCALL: 3280 { 3281 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc; 3282 3283 smsg("%4d PCALL%s (argc %d)", current, 3284 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount); 3285 } 3286 break; 3287 case ISN_PCALL_END: 3288 smsg("%4d PCALL end", current); 3289 break; 3290 case ISN_RETURN: 3291 smsg("%4d RETURN", current); 3292 break; 3293 case ISN_FUNCREF: 3294 { 3295 funcref_T *funcref = &iptr->isn_arg.funcref; 3296 dfunc_T *df = ((dfunc_T *)def_functions.ga_data) 3297 + funcref->fr_func; 3298 3299 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name); 3300 } 3301 break; 3302 3303 case ISN_NEWFUNC: 3304 { 3305 newfunc_T *newfunc = &iptr->isn_arg.newfunc; 3306 3307 smsg("%4d NEWFUNC %s %s", current, 3308 newfunc->nf_lambda, newfunc->nf_global); 3309 } 3310 break; 3311 3312 case ISN_JUMP: 3313 { 3314 char *when = "?"; 3315 3316 switch (iptr->isn_arg.jump.jump_when) 3317 { 3318 case JUMP_ALWAYS: 3319 when = "JUMP"; 3320 break; 3321 case JUMP_AND_KEEP_IF_TRUE: 3322 when = "JUMP_AND_KEEP_IF_TRUE"; 3323 break; 3324 case JUMP_IF_FALSE: 3325 when = "JUMP_IF_FALSE"; 3326 break; 3327 case JUMP_AND_KEEP_IF_FALSE: 3328 when = "JUMP_AND_KEEP_IF_FALSE"; 3329 break; 3330 case JUMP_IF_COND_FALSE: 3331 when = "JUMP_IF_COND_FALSE"; 3332 break; 3333 case JUMP_IF_COND_TRUE: 3334 when = "JUMP_IF_COND_TRUE"; 3335 break; 3336 } 3337 smsg("%4d %s -> %d", current, when, 3338 iptr->isn_arg.jump.jump_where); 3339 } 3340 break; 3341 3342 case ISN_FOR: 3343 { 3344 forloop_T *forloop = &iptr->isn_arg.forloop; 3345 3346 smsg("%4d FOR $%d -> %d", current, 3347 forloop->for_idx, forloop->for_end); 3348 } 3349 break; 3350 3351 case ISN_TRY: 3352 { 3353 try_T *try = &iptr->isn_arg.try; 3354 3355 smsg("%4d TRY catch -> %d, finally -> %d", current, 3356 try->try_catch, try->try_finally); 3357 } 3358 break; 3359 case ISN_CATCH: 3360 // TODO 3361 smsg("%4d CATCH", current); 3362 break; 3363 case ISN_ENDTRY: 3364 smsg("%4d ENDTRY", current); 3365 break; 3366 case ISN_THROW: 3367 smsg("%4d THROW", current); 3368 break; 3369 3370 // expression operations on number 3371 case ISN_OPNR: 3372 case ISN_OPFLOAT: 3373 case ISN_OPANY: 3374 { 3375 char *what; 3376 char *ins; 3377 3378 switch (iptr->isn_arg.op.op_type) 3379 { 3380 case EXPR_MULT: what = "*"; break; 3381 case EXPR_DIV: what = "/"; break; 3382 case EXPR_REM: what = "%"; break; 3383 case EXPR_SUB: what = "-"; break; 3384 case EXPR_ADD: what = "+"; break; 3385 default: what = "???"; break; 3386 } 3387 switch (iptr->isn_type) 3388 { 3389 case ISN_OPNR: ins = "OPNR"; break; 3390 case ISN_OPFLOAT: ins = "OPFLOAT"; break; 3391 case ISN_OPANY: ins = "OPANY"; break; 3392 default: ins = "???"; break; 3393 } 3394 smsg("%4d %s %s", current, ins, what); 3395 } 3396 break; 3397 3398 case ISN_COMPAREBOOL: 3399 case ISN_COMPARESPECIAL: 3400 case ISN_COMPARENR: 3401 case ISN_COMPAREFLOAT: 3402 case ISN_COMPARESTRING: 3403 case ISN_COMPAREBLOB: 3404 case ISN_COMPARELIST: 3405 case ISN_COMPAREDICT: 3406 case ISN_COMPAREFUNC: 3407 case ISN_COMPAREANY: 3408 { 3409 char *p; 3410 char buf[10]; 3411 char *type; 3412 3413 switch (iptr->isn_arg.op.op_type) 3414 { 3415 case EXPR_EQUAL: p = "=="; break; 3416 case EXPR_NEQUAL: p = "!="; break; 3417 case EXPR_GREATER: p = ">"; break; 3418 case EXPR_GEQUAL: p = ">="; break; 3419 case EXPR_SMALLER: p = "<"; break; 3420 case EXPR_SEQUAL: p = "<="; break; 3421 case EXPR_MATCH: p = "=~"; break; 3422 case EXPR_IS: p = "is"; break; 3423 case EXPR_ISNOT: p = "isnot"; break; 3424 case EXPR_NOMATCH: p = "!~"; break; 3425 default: p = "???"; break; 3426 } 3427 STRCPY(buf, p); 3428 if (iptr->isn_arg.op.op_ic == TRUE) 3429 strcat(buf, "?"); 3430 switch(iptr->isn_type) 3431 { 3432 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break; 3433 case ISN_COMPARESPECIAL: 3434 type = "COMPARESPECIAL"; break; 3435 case ISN_COMPARENR: type = "COMPARENR"; break; 3436 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break; 3437 case ISN_COMPARESTRING: 3438 type = "COMPARESTRING"; break; 3439 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break; 3440 case ISN_COMPARELIST: type = "COMPARELIST"; break; 3441 case ISN_COMPAREDICT: type = "COMPAREDICT"; break; 3442 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break; 3443 case ISN_COMPAREANY: type = "COMPAREANY"; break; 3444 default: type = "???"; break; 3445 } 3446 3447 smsg("%4d %s %s", current, type, buf); 3448 } 3449 break; 3450 3451 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break; 3452 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break; 3453 3454 // expression operations 3455 case ISN_CONCAT: smsg("%4d CONCAT", current); break; 3456 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break; 3457 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break; 3458 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break; 3459 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break; 3460 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break; 3461 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break; 3462 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break; 3463 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break; 3464 case ISN_SLICE: smsg("%4d SLICE %lld", 3465 current, iptr->isn_arg.number); break; 3466 case ISN_GETITEM: smsg("%4d ITEM %lld", 3467 current, iptr->isn_arg.number); break; 3468 case ISN_MEMBER: smsg("%4d MEMBER", current); break; 3469 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current, 3470 iptr->isn_arg.string); break; 3471 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break; 3472 3473 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break; 3474 case ISN_CHECKTYPE: 3475 { 3476 char *tofree; 3477 3478 smsg("%4d CHECKTYPE %s stack[%d]", current, 3479 type_name(iptr->isn_arg.type.ct_type, &tofree), 3480 iptr->isn_arg.type.ct_off); 3481 vim_free(tofree); 3482 break; 3483 } 3484 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current, 3485 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "", 3486 iptr->isn_arg.checklen.cl_min_len); 3487 break; 3488 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break; 3489 case ISN_2BOOL: if (iptr->isn_arg.number) 3490 smsg("%4d INVERT (!val)", current); 3491 else 3492 smsg("%4d 2BOOL (!!val)", current); 3493 break; 3494 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current, 3495 (long long)(iptr->isn_arg.number)); 3496 break; 3497 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current, 3498 (long long)(iptr->isn_arg.number)); 3499 break; 3500 case ISN_PUT: 3501 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname, 3502 (long)iptr->isn_arg.put.put_lnum); 3503 break; 3504 3505 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current, 3506 iptr->isn_arg.shuffle.shfl_item, 3507 iptr->isn_arg.shuffle.shfl_up); 3508 break; 3509 case ISN_DROP: smsg("%4d DROP", current); break; 3510 } 3511 3512 out_flush(); // output one line at a time 3513 ui_breakcheck(); 3514 if (got_int) 3515 break; 3516 } 3517 } 3518 3519 /* 3520 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty 3521 * list, etc. Mostly like what JavaScript does, except that empty list and 3522 * empty dictionary are FALSE. 3523 */ 3524 int 3525 tv2bool(typval_T *tv) 3526 { 3527 switch (tv->v_type) 3528 { 3529 case VAR_NUMBER: 3530 return tv->vval.v_number != 0; 3531 case VAR_FLOAT: 3532 #ifdef FEAT_FLOAT 3533 return tv->vval.v_float != 0.0; 3534 #else 3535 break; 3536 #endif 3537 case VAR_PARTIAL: 3538 return tv->vval.v_partial != NULL; 3539 case VAR_FUNC: 3540 case VAR_STRING: 3541 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL; 3542 case VAR_LIST: 3543 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0; 3544 case VAR_DICT: 3545 return tv->vval.v_dict != NULL 3546 && tv->vval.v_dict->dv_hashtab.ht_used > 0; 3547 case VAR_BOOL: 3548 case VAR_SPECIAL: 3549 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE; 3550 case VAR_JOB: 3551 #ifdef FEAT_JOB_CHANNEL 3552 return tv->vval.v_job != NULL; 3553 #else 3554 break; 3555 #endif 3556 case VAR_CHANNEL: 3557 #ifdef FEAT_JOB_CHANNEL 3558 return tv->vval.v_channel != NULL; 3559 #else 3560 break; 3561 #endif 3562 case VAR_BLOB: 3563 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0; 3564 case VAR_UNKNOWN: 3565 case VAR_ANY: 3566 case VAR_VOID: 3567 break; 3568 } 3569 return FALSE; 3570 } 3571 3572 /* 3573 * If "tv" is a string give an error and return FAIL. 3574 */ 3575 int 3576 check_not_string(typval_T *tv) 3577 { 3578 if (tv->v_type == VAR_STRING) 3579 { 3580 emsg(_(e_using_string_as_number)); 3581 clear_tv(tv); 3582 return FAIL; 3583 } 3584 return OK; 3585 } 3586 3587 3588 #endif // FEAT_EVAL 3589