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 struct ectx_S { 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 outer_T *ec_outer; // outer scope used for closures, allocated 62 63 garray_T ec_trystack; // stack of trycmd_T values 64 int ec_in_catch; // when TRUE in catch or finally block 65 66 int ec_dfunc_idx; // current function index 67 isn_T *ec_instr; // array with instructions 68 int ec_iidx; // index in ec_instr: instruction to execute 69 70 garray_T ec_funcrefs; // partials that might be a closure 71 }; 72 73 // Get pointer to item relative to the bottom of the stack, -1 is the last one. 74 #define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx)) 75 76 void 77 to_string_error(vartype_T vartype) 78 { 79 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype)); 80 } 81 82 /* 83 * Return the number of arguments, including optional arguments and any vararg. 84 */ 85 static int 86 ufunc_argcount(ufunc_T *ufunc) 87 { 88 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0); 89 } 90 91 /* 92 * Set the instruction index, depending on omitted arguments, where the default 93 * values are to be computed. If all optional arguments are present, start 94 * with the function body. 95 * The expression evaluation is at the start of the instructions: 96 * 0 -> EVAL default1 97 * STORE arg[-2] 98 * 1 -> EVAL default2 99 * STORE arg[-1] 100 * 2 -> function body 101 */ 102 static void 103 init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx) 104 { 105 if (ufunc->uf_def_args.ga_len == 0) 106 ectx->ec_iidx = 0; 107 else 108 { 109 int defcount = ufunc->uf_args.ga_len - argcount; 110 111 // If there is a varargs argument defcount can be negative, no defaults 112 // to evaluate then. 113 if (defcount < 0) 114 defcount = 0; 115 ectx->ec_iidx = ufunc->uf_def_arg_idx[ 116 ufunc->uf_def_args.ga_len - defcount]; 117 } 118 } 119 120 /* 121 * Create a new list from "count" items at the bottom of the stack. 122 * When "count" is zero an empty list is added to the stack. 123 */ 124 static int 125 exe_newlist(int count, ectx_T *ectx) 126 { 127 list_T *list = list_alloc_with_items(count); 128 int idx; 129 typval_T *tv; 130 131 if (list == NULL) 132 return FAIL; 133 for (idx = 0; idx < count; ++idx) 134 list_set_item(list, idx, STACK_TV_BOT(idx - count)); 135 136 if (count > 0) 137 ectx->ec_stack.ga_len -= count - 1; 138 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL) 139 return FAIL; 140 else 141 ++ectx->ec_stack.ga_len; 142 tv = STACK_TV_BOT(-1); 143 tv->v_type = VAR_LIST; 144 tv->vval.v_list = list; 145 ++list->lv_refcount; 146 return OK; 147 } 148 149 /* 150 * Call compiled function "cdf_idx" from compiled code. 151 * This adds a stack frame and sets the instruction pointer to the start of the 152 * called function. 153 * If "pt" is not null use "pt->pt_outer" for ec_outer. 154 * 155 * Stack has: 156 * - current arguments (already there) 157 * - omitted optional argument (default values) added here 158 * - stack frame: 159 * - pointer to calling function 160 * - Index of next instruction in calling function 161 * - previous frame pointer 162 * - reserved space for local variables 163 */ 164 static int 165 call_dfunc(int cdf_idx, partial_T *pt, int argcount_arg, ectx_T *ectx) 166 { 167 int argcount = argcount_arg; 168 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx; 169 ufunc_T *ufunc = dfunc->df_ufunc; 170 int arg_to_add; 171 int vararg_count = 0; 172 int varcount; 173 int idx; 174 estack_T *entry; 175 176 if (dfunc->df_deleted) 177 { 178 // don't use ufunc->uf_name, it may have been freed 179 emsg_funcname(e_func_deleted, 180 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name); 181 return FAIL; 182 } 183 184 #ifdef FEAT_PROFILE 185 // Profiling might be enabled/disabled along the way. This should not 186 // fail, since the function was compiled before and toggling profiling 187 // doesn't change any errors. 188 if (func_needs_compiling(ufunc, PROFILING(ufunc)) 189 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL) 190 == FAIL) 191 return FAIL; 192 #endif 193 194 if (ufunc->uf_va_name != NULL) 195 { 196 // Need to make a list out of the vararg arguments. 197 // Stack at time of call with 2 varargs: 198 // normal_arg 199 // optional_arg 200 // vararg_1 201 // vararg_2 202 // After creating the list: 203 // normal_arg 204 // optional_arg 205 // vararg-list 206 // With missing optional arguments we get: 207 // normal_arg 208 // After creating the list 209 // normal_arg 210 // (space for optional_arg) 211 // vararg-list 212 vararg_count = argcount - ufunc->uf_args.ga_len; 213 if (vararg_count < 0) 214 vararg_count = 0; 215 else 216 argcount -= vararg_count; 217 if (exe_newlist(vararg_count, ectx) == FAIL) 218 return FAIL; 219 220 vararg_count = 1; 221 } 222 223 arg_to_add = ufunc->uf_args.ga_len - argcount; 224 if (arg_to_add < 0) 225 { 226 if (arg_to_add == -1) 227 emsg(_(e_one_argument_too_many)); 228 else 229 semsg(_(e_nr_arguments_too_many), -arg_to_add); 230 return FAIL; 231 } 232 233 // Reserve space for: 234 // - missing arguments 235 // - stack frame 236 // - local variables 237 // - if needed: a counter for number of closures created in 238 // ectx->ec_funcrefs. 239 varcount = dfunc->df_varcount + dfunc->df_has_closure; 240 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount) 241 == FAIL) 242 return FAIL; 243 244 // If depth of calling is getting too high, don't execute the function. 245 if (funcdepth_increment() == FAIL) 246 return FAIL; 247 248 // Move the vararg-list to below the missing optional arguments. 249 if (vararg_count > 0 && arg_to_add > 0) 250 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1); 251 252 // Reserve space for omitted optional arguments, filled in soon. 253 for (idx = 0; idx < arg_to_add; ++idx) 254 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN; 255 ectx->ec_stack.ga_len += arg_to_add; 256 257 // Store current execution state in stack frame for ISN_RETURN. 258 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx; 259 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx; 260 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer; 261 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx; 262 ectx->ec_frame_idx = ectx->ec_stack.ga_len; 263 264 // Initialize local variables 265 for (idx = 0; idx < dfunc->df_varcount; ++idx) 266 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN; 267 if (dfunc->df_has_closure) 268 { 269 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount); 270 271 tv->v_type = VAR_NUMBER; 272 tv->vval.v_number = 0; 273 } 274 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount; 275 276 if (pt != NULL || ufunc->uf_partial != NULL 277 || (ufunc->uf_flags & FC_CLOSURE)) 278 { 279 outer_T *outer = ALLOC_CLEAR_ONE(outer_T); 280 281 if (outer == NULL) 282 return FAIL; 283 if (pt != NULL) 284 { 285 *outer = pt->pt_outer; 286 outer->out_up_is_copy = TRUE; 287 } 288 else if (ufunc->uf_partial != NULL) 289 { 290 *outer = ufunc->uf_partial->pt_outer; 291 outer->out_up_is_copy = TRUE; 292 } 293 else 294 { 295 outer->out_stack = &ectx->ec_stack; 296 outer->out_frame_idx = ectx->ec_frame_idx; 297 outer->out_up = ectx->ec_outer; 298 } 299 ectx->ec_outer = outer; 300 } 301 else 302 ectx->ec_outer = NULL; 303 304 // Set execution state to the start of the called function. 305 ectx->ec_dfunc_idx = cdf_idx; 306 ectx->ec_instr = INSTRUCTIONS(dfunc); 307 entry = estack_push_ufunc(ufunc, 1); 308 if (entry != NULL) 309 { 310 // Set the script context to the script where the function was defined. 311 // TODO: save more than the SID? 312 entry->es_save_sid = current_sctx.sc_sid; 313 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid; 314 } 315 316 // Decide where to start execution, handles optional arguments. 317 init_instr_idx(ufunc, argcount, ectx); 318 319 return OK; 320 } 321 322 // Get pointer to item in the stack. 323 #define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx) 324 325 /* 326 * Used when returning from a function: Check if any closure is still 327 * referenced. If so then move the arguments and variables to a separate piece 328 * of stack to be used when the closure is called. 329 * When "free_arguments" is TRUE the arguments are to be freed. 330 * Returns FAIL when out of memory. 331 */ 332 static int 333 handle_closure_in_use(ectx_T *ectx, int free_arguments) 334 { 335 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 336 + ectx->ec_dfunc_idx; 337 int argcount; 338 int top; 339 int idx; 340 typval_T *tv; 341 int closure_in_use = FALSE; 342 garray_T *gap = &ectx->ec_funcrefs; 343 varnumber_T closure_count; 344 345 if (dfunc->df_ufunc == NULL) 346 return OK; // function was freed 347 if (dfunc->df_has_closure == 0) 348 return OK; // no closures 349 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount); 350 closure_count = tv->vval.v_number; 351 if (closure_count == 0) 352 return OK; // no funcrefs created 353 354 argcount = ufunc_argcount(dfunc->df_ufunc); 355 top = ectx->ec_frame_idx - argcount; 356 357 // Check if any created closure is still in use. 358 for (idx = 0; idx < closure_count; ++idx) 359 { 360 partial_T *pt; 361 int off = gap->ga_len - closure_count + idx; 362 363 if (off < 0) 364 continue; // count is off or already done 365 pt = ((partial_T **)gap->ga_data)[off]; 366 if (pt->pt_refcount > 1) 367 { 368 int refcount = pt->pt_refcount; 369 int i; 370 371 // A Reference in a local variables doesn't count, it gets 372 // unreferenced on return. 373 for (i = 0; i < dfunc->df_varcount; ++i) 374 { 375 typval_T *stv = STACK_TV(ectx->ec_frame_idx 376 + STACK_FRAME_SIZE + i); 377 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial) 378 --refcount; 379 } 380 if (refcount > 1) 381 { 382 closure_in_use = TRUE; 383 break; 384 } 385 } 386 } 387 388 if (closure_in_use) 389 { 390 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T); 391 typval_T *stack; 392 393 // A closure is using the arguments and/or local variables. 394 // Move them to the called function. 395 if (funcstack == NULL) 396 return FAIL; 397 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE; 398 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount; 399 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len); 400 funcstack->fs_ga.ga_data = stack; 401 if (stack == NULL) 402 { 403 vim_free(funcstack); 404 return FAIL; 405 } 406 407 // Move or copy the arguments. 408 for (idx = 0; idx < argcount; ++idx) 409 { 410 tv = STACK_TV(top + idx); 411 if (free_arguments) 412 { 413 *(stack + idx) = *tv; 414 tv->v_type = VAR_UNKNOWN; 415 } 416 else 417 copy_tv(tv, stack + idx); 418 } 419 // Move the local variables. 420 for (idx = 0; idx < dfunc->df_varcount; ++idx) 421 { 422 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx); 423 424 // A partial created for a local function, that is also used as a 425 // local variable, has a reference count for the variable, thus 426 // will never go down to zero. When all these refcounts are one 427 // then the funcstack is unused. We need to count how many we have 428 // so we need when to check. 429 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL) 430 { 431 int i; 432 433 for (i = 0; i < closure_count; ++i) 434 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[ 435 gap->ga_len - closure_count + i]) 436 ++funcstack->fs_min_refcount; 437 } 438 439 *(stack + funcstack->fs_var_offset + idx) = *tv; 440 tv->v_type = VAR_UNKNOWN; 441 } 442 443 for (idx = 0; idx < closure_count; ++idx) 444 { 445 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len 446 - closure_count + idx]; 447 if (pt->pt_refcount > 1) 448 { 449 ++funcstack->fs_refcount; 450 pt->pt_funcstack = funcstack; 451 pt->pt_outer.out_stack = &funcstack->fs_ga; 452 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top; 453 pt->pt_outer.out_up = ectx->ec_outer; 454 } 455 } 456 } 457 458 for (idx = 0; idx < closure_count; ++idx) 459 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len 460 - closure_count + idx]); 461 gap->ga_len -= closure_count; 462 if (gap->ga_len == 0) 463 ga_clear(gap); 464 465 return OK; 466 } 467 468 /* 469 * Called when a partial is freed or its reference count goes down to one. The 470 * funcstack may be the only reference to the partials in the local variables. 471 * Go over all of them, the funcref and can be freed if all partials 472 * referencing the funcstack have a reference count of one. 473 */ 474 void 475 funcstack_check_refcount(funcstack_T *funcstack) 476 { 477 int i; 478 garray_T *gap = &funcstack->fs_ga; 479 int done = 0; 480 481 if (funcstack->fs_refcount > funcstack->fs_min_refcount) 482 return; 483 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i) 484 { 485 typval_T *tv = ((typval_T *)gap->ga_data) + i; 486 487 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL 488 && tv->vval.v_partial->pt_funcstack == funcstack 489 && tv->vval.v_partial->pt_refcount == 1) 490 ++done; 491 } 492 if (done == funcstack->fs_min_refcount) 493 { 494 typval_T *stack = gap->ga_data; 495 496 // All partials referencing the funcstack have a reference count of 497 // one, thus the funcstack is no longer of use. 498 for (i = 0; i < gap->ga_len; ++i) 499 clear_tv(stack + i); 500 vim_free(stack); 501 vim_free(funcstack); 502 } 503 } 504 505 /* 506 * Return from the current function. 507 */ 508 static int 509 func_return(ectx_T *ectx) 510 { 511 int idx; 512 int ret_idx; 513 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 514 + ectx->ec_dfunc_idx; 515 int argcount = ufunc_argcount(dfunc->df_ufunc); 516 int top = ectx->ec_frame_idx - argcount; 517 estack_T *entry; 518 519 // execution context goes one level up 520 entry = estack_pop(); 521 if (entry != NULL) 522 current_sctx.sc_sid = entry->es_save_sid; 523 524 if (handle_closure_in_use(ectx, TRUE) == FAIL) 525 return FAIL; 526 527 // Clear the arguments. 528 for (idx = top; idx < ectx->ec_frame_idx; ++idx) 529 clear_tv(STACK_TV(idx)); 530 531 // Clear local variables and temp values, but not the return value. 532 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE; 533 idx < ectx->ec_stack.ga_len - 1; ++idx) 534 clear_tv(STACK_TV(idx)); 535 536 // The return value should be on top of the stack. However, when aborting 537 // it may not be there and ec_frame_idx is the top of the stack. 538 ret_idx = ectx->ec_stack.ga_len - 1; 539 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF) 540 ret_idx = 0; 541 542 vim_free(ectx->ec_outer); 543 544 // Restore the previous frame. 545 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx 546 + STACK_FRAME_FUNC_OFF)->vval.v_number; 547 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx 548 + STACK_FRAME_IIDX_OFF)->vval.v_number; 549 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx 550 + STACK_FRAME_OUTER_OFF)->vval.v_string; 551 // restoring ec_frame_idx must be last 552 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx 553 + STACK_FRAME_IDX_OFF)->vval.v_number; 554 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx; 555 ectx->ec_instr = INSTRUCTIONS(dfunc); 556 557 if (ret_idx > 0) 558 { 559 // Reset the stack to the position before the call, with a spot for the 560 // return value, moved there from above the frame. 561 ectx->ec_stack.ga_len = top + 1; 562 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx); 563 } 564 else 565 // Reset the stack to the position before the call. 566 ectx->ec_stack.ga_len = top; 567 568 funcdepth_decrement(); 569 return OK; 570 } 571 572 #undef STACK_TV 573 574 /* 575 * Prepare arguments and rettv for calling a builtin or user function. 576 */ 577 static int 578 call_prepare(int argcount, typval_T *argvars, ectx_T *ectx) 579 { 580 int idx; 581 typval_T *tv; 582 583 // Move arguments from bottom of the stack to argvars[] and add terminator. 584 for (idx = 0; idx < argcount; ++idx) 585 argvars[idx] = *STACK_TV_BOT(idx - argcount); 586 argvars[argcount].v_type = VAR_UNKNOWN; 587 588 // Result replaces the arguments on the stack. 589 if (argcount > 0) 590 ectx->ec_stack.ga_len -= argcount - 1; 591 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL) 592 return FAIL; 593 else 594 ++ectx->ec_stack.ga_len; 595 596 // Default return value is zero. 597 tv = STACK_TV_BOT(-1); 598 tv->v_type = VAR_NUMBER; 599 tv->vval.v_number = 0; 600 601 return OK; 602 } 603 604 // Ugly global to avoid passing the execution context around through many 605 // layers. 606 static ectx_T *current_ectx = NULL; 607 608 /* 609 * Call a builtin function by index. 610 */ 611 static int 612 call_bfunc(int func_idx, int argcount, ectx_T *ectx) 613 { 614 typval_T argvars[MAX_FUNC_ARGS]; 615 int idx; 616 int did_emsg_before = did_emsg; 617 ectx_T *prev_ectx = current_ectx; 618 619 if (call_prepare(argcount, argvars, ectx) == FAIL) 620 return FAIL; 621 622 // Call the builtin function. Set "current_ectx" so that when it 623 // recursively invokes call_def_function() a closure context can be set. 624 current_ectx = ectx; 625 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1)); 626 current_ectx = prev_ectx; 627 628 // Clear the arguments. 629 for (idx = 0; idx < argcount; ++idx) 630 clear_tv(&argvars[idx]); 631 632 if (did_emsg > did_emsg_before) 633 return FAIL; 634 return OK; 635 } 636 637 /* 638 * Execute a user defined function. 639 * If the function is compiled this will add a stack frame and set the 640 * instruction pointer at the start of the function. 641 * Otherwise the function is called here. 642 * If "pt" is not null use "pt->pt_outer" for ec_outer. 643 * "iptr" can be used to replace the instruction with a more efficient one. 644 */ 645 static int 646 call_ufunc( 647 ufunc_T *ufunc, 648 partial_T *pt, 649 int argcount, 650 ectx_T *ectx, 651 isn_T *iptr) 652 { 653 typval_T argvars[MAX_FUNC_ARGS]; 654 funcexe_T funcexe; 655 int error; 656 int idx; 657 int did_emsg_before = did_emsg; 658 #ifdef FEAT_PROFILE 659 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling; 660 #else 661 # define profiling FALSE 662 #endif 663 664 if (func_needs_compiling(ufunc, profiling) 665 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL) 666 return FAIL; 667 if (ufunc->uf_def_status == UF_COMPILED) 668 { 669 error = check_user_func_argcount(ufunc, argcount); 670 if (error != FCERR_UNKNOWN) 671 { 672 if (error == FCERR_TOOMANY) 673 semsg(_(e_toomanyarg), ufunc->uf_name); 674 else 675 semsg(_(e_toofewarg), ufunc->uf_name); 676 return FAIL; 677 } 678 679 // The function has been compiled, can call it quickly. For a function 680 // that was defined later: we can call it directly next time. 681 // TODO: what if the function was deleted and then defined again? 682 if (iptr != NULL) 683 { 684 delete_instr(iptr); 685 iptr->isn_type = ISN_DCALL; 686 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; 687 iptr->isn_arg.dfunc.cdf_argcount = argcount; 688 } 689 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx); 690 } 691 692 if (call_prepare(argcount, argvars, ectx) == FAIL) 693 return FAIL; 694 CLEAR_FIELD(funcexe); 695 funcexe.evaluate = TRUE; 696 697 // Call the user function. Result goes in last position on the stack. 698 // TODO: add selfdict if there is one 699 error = call_user_func_check(ufunc, argcount, argvars, 700 STACK_TV_BOT(-1), &funcexe, NULL); 701 702 // Clear the arguments. 703 for (idx = 0; idx < argcount; ++idx) 704 clear_tv(&argvars[idx]); 705 706 if (error != FCERR_NONE) 707 { 708 user_func_error(error, ufunc->uf_name); 709 return FAIL; 710 } 711 if (did_emsg > did_emsg_before) 712 // Error other than from calling the function itself. 713 return FAIL; 714 return OK; 715 } 716 717 /* 718 * Return TRUE if an error was given or CTRL-C was pressed. 719 */ 720 static int 721 vim9_aborting(int prev_called_emsg) 722 { 723 return called_emsg > prev_called_emsg || got_int || did_throw; 724 } 725 726 /* 727 * Execute a function by "name". 728 * This can be a builtin function or a user function. 729 * "iptr" can be used to replace the instruction with a more efficient one. 730 * Returns FAIL if not found without an error message. 731 */ 732 static int 733 call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr) 734 { 735 ufunc_T *ufunc; 736 737 if (builtin_function(name, -1)) 738 { 739 int func_idx = find_internal_func(name); 740 741 if (func_idx < 0) 742 return FAIL; 743 if (check_internal_func(func_idx, argcount) < 0) 744 return FAIL; 745 return call_bfunc(func_idx, argcount, ectx); 746 } 747 748 ufunc = find_func(name, FALSE, NULL); 749 750 if (ufunc == NULL) 751 { 752 int called_emsg_before = called_emsg; 753 754 if (script_autoload(name, TRUE)) 755 // loaded a package, search for the function again 756 ufunc = find_func(name, FALSE, NULL); 757 if (vim9_aborting(called_emsg_before)) 758 return FAIL; // bail out if loading the script caused an error 759 } 760 761 if (ufunc != NULL) 762 return call_ufunc(ufunc, NULL, argcount, ectx, iptr); 763 764 return FAIL; 765 } 766 767 static int 768 call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx) 769 { 770 int argcount = argcount_arg; 771 char_u *name = NULL; 772 int called_emsg_before = called_emsg; 773 int res = FAIL; 774 775 if (tv->v_type == VAR_PARTIAL) 776 { 777 partial_T *pt = tv->vval.v_partial; 778 int i; 779 780 if (pt->pt_argc > 0) 781 { 782 // Make space for arguments from the partial, shift the "argcount" 783 // arguments up. 784 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL) 785 return FAIL; 786 for (i = 1; i <= argcount; ++i) 787 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i); 788 ectx->ec_stack.ga_len += pt->pt_argc; 789 argcount += pt->pt_argc; 790 791 // copy the arguments from the partial onto the stack 792 for (i = 0; i < pt->pt_argc; ++i) 793 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i)); 794 } 795 796 if (pt->pt_func != NULL) 797 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL); 798 799 name = pt->pt_name; 800 } 801 else if (tv->v_type == VAR_FUNC) 802 name = tv->vval.v_string; 803 if (name != NULL) 804 { 805 char_u fname_buf[FLEN_FIXED + 1]; 806 char_u *tofree = NULL; 807 int error = FCERR_NONE; 808 char_u *fname; 809 810 // May need to translate <SNR>123_ to K_SNR. 811 fname = fname_trans_sid(name, fname_buf, &tofree, &error); 812 if (error != FCERR_NONE) 813 res = FAIL; 814 else 815 res = call_by_name(fname, argcount, ectx, NULL); 816 vim_free(tofree); 817 } 818 819 if (res == FAIL) 820 { 821 if (called_emsg == called_emsg_before) 822 semsg(_(e_unknownfunc), 823 name == NULL ? (char_u *)"[unknown]" : name); 824 return FAIL; 825 } 826 return OK; 827 } 828 829 /* 830 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return 831 * TRUE. 832 */ 833 static int 834 error_if_locked(int lock, char *error) 835 { 836 if (lock & (VAR_LOCKED | VAR_FIXED)) 837 { 838 emsg(_(error)); 839 return TRUE; 840 } 841 return FALSE; 842 } 843 844 /* 845 * Store "tv" in variable "name". 846 * This is for s: and g: variables. 847 */ 848 static void 849 store_var(char_u *name, typval_T *tv) 850 { 851 funccal_entry_T entry; 852 853 save_funccal(&entry); 854 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL, 0); 855 restore_funccal(); 856 } 857 858 /* 859 * Convert "tv" to a string. 860 * Return FAIL if not allowed. 861 */ 862 static int 863 do_2string(typval_T *tv, int is_2string_any) 864 { 865 if (tv->v_type != VAR_STRING) 866 { 867 char_u *str; 868 869 if (is_2string_any) 870 { 871 switch (tv->v_type) 872 { 873 case VAR_SPECIAL: 874 case VAR_BOOL: 875 case VAR_NUMBER: 876 case VAR_FLOAT: 877 case VAR_BLOB: break; 878 default: to_string_error(tv->v_type); 879 return FAIL; 880 } 881 } 882 str = typval_tostring(tv, TRUE); 883 clear_tv(tv); 884 tv->v_type = VAR_STRING; 885 tv->vval.v_string = str; 886 } 887 return OK; 888 } 889 890 /* 891 * When the value of "sv" is a null list of dict, allocate it. 892 */ 893 static void 894 allocate_if_null(typval_T *tv) 895 { 896 switch (tv->v_type) 897 { 898 case VAR_LIST: 899 if (tv->vval.v_list == NULL) 900 (void)rettv_list_alloc(tv); 901 break; 902 case VAR_DICT: 903 if (tv->vval.v_dict == NULL) 904 (void)rettv_dict_alloc(tv); 905 break; 906 default: 907 break; 908 } 909 } 910 911 /* 912 * Return the character "str[index]" where "index" is the character index. If 913 * "index" is out of range NULL is returned. 914 */ 915 char_u * 916 char_from_string(char_u *str, varnumber_T index) 917 { 918 size_t nbyte = 0; 919 varnumber_T nchar = index; 920 size_t slen; 921 922 if (str == NULL) 923 return NULL; 924 slen = STRLEN(str); 925 926 // do the same as for a list: a negative index counts from the end 927 if (index < 0) 928 { 929 int clen = 0; 930 931 for (nbyte = 0; nbyte < slen; ++clen) 932 nbyte += MB_CPTR2LEN(str + nbyte); 933 nchar = clen + index; 934 if (nchar < 0) 935 // unlike list: index out of range results in empty string 936 return NULL; 937 } 938 939 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar) 940 nbyte += MB_CPTR2LEN(str + nbyte); 941 if (nbyte >= slen) 942 return NULL; 943 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte)); 944 } 945 946 /* 947 * Get the byte index for character index "idx" in string "str" with length 948 * "str_len". 949 * If going over the end return "str_len". 950 * If "idx" is negative count from the end, -1 is the last character. 951 * When going over the start return -1. 952 */ 953 static long 954 char_idx2byte(char_u *str, size_t str_len, varnumber_T idx) 955 { 956 varnumber_T nchar = idx; 957 size_t nbyte = 0; 958 959 if (nchar >= 0) 960 { 961 while (nchar > 0 && nbyte < str_len) 962 { 963 nbyte += MB_CPTR2LEN(str + nbyte); 964 --nchar; 965 } 966 } 967 else 968 { 969 nbyte = str_len; 970 while (nchar < 0 && nbyte > 0) 971 { 972 --nbyte; 973 nbyte -= mb_head_off(str, str + nbyte); 974 ++nchar; 975 } 976 if (nchar < 0) 977 return -1; 978 } 979 return (long)nbyte; 980 } 981 982 /* 983 * Return the slice "str[first:last]" using character indexes. 984 * "exclusive" is TRUE for slice(). 985 * Return NULL when the result is empty. 986 */ 987 char_u * 988 string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive) 989 { 990 long start_byte, end_byte; 991 size_t slen; 992 993 if (str == NULL) 994 return NULL; 995 slen = STRLEN(str); 996 start_byte = char_idx2byte(str, slen, first); 997 if (start_byte < 0) 998 start_byte = 0; // first index very negative: use zero 999 if ((last == -1 && !exclusive) || last == VARNUM_MAX) 1000 end_byte = (long)slen; 1001 else 1002 { 1003 end_byte = char_idx2byte(str, slen, last); 1004 if (!exclusive && end_byte >= 0 && end_byte < (long)slen) 1005 // end index is inclusive 1006 end_byte += MB_CPTR2LEN(str + end_byte); 1007 } 1008 1009 if (start_byte >= (long)slen || end_byte <= start_byte) 1010 return NULL; 1011 return vim_strnsave(str + start_byte, end_byte - start_byte); 1012 } 1013 1014 static svar_T * 1015 get_script_svar(scriptref_T *sref, ectx_T *ectx) 1016 { 1017 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); 1018 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 1019 + ectx->ec_dfunc_idx; 1020 svar_T *sv; 1021 1022 if (sref->sref_seq != si->sn_script_seq) 1023 { 1024 // The script was reloaded after the function was 1025 // compiled, the script_idx may not be valid. 1026 semsg(_(e_script_variable_invalid_after_reload_in_function_str), 1027 dfunc->df_ufunc->uf_name_exp); 1028 return NULL; 1029 } 1030 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx; 1031 if (!equal_type(sv->sv_type, sref->sref_type)) 1032 { 1033 emsg(_(e_script_variable_type_changed)); 1034 return NULL; 1035 } 1036 return sv; 1037 } 1038 1039 /* 1040 * Execute a function by "name". 1041 * This can be a builtin function, user function or a funcref. 1042 * "iptr" can be used to replace the instruction with a more efficient one. 1043 */ 1044 static int 1045 call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr) 1046 { 1047 int called_emsg_before = called_emsg; 1048 int res; 1049 1050 res = call_by_name(name, argcount, ectx, iptr); 1051 if (res == FAIL && called_emsg == called_emsg_before) 1052 { 1053 dictitem_T *v; 1054 1055 v = find_var(name, NULL, FALSE); 1056 if (v == NULL) 1057 { 1058 semsg(_(e_unknownfunc), name); 1059 return FAIL; 1060 } 1061 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC) 1062 { 1063 semsg(_(e_unknownfunc), name); 1064 return FAIL; 1065 } 1066 return call_partial(&v->di_tv, argcount, ectx); 1067 } 1068 return res; 1069 } 1070 1071 /* 1072 * When a function reference is used, fill a partial with the information 1073 * needed, especially when it is used as a closure. 1074 */ 1075 int 1076 fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx) 1077 { 1078 pt->pt_func = ufunc; 1079 pt->pt_refcount = 1; 1080 1081 if (ufunc->uf_flags & FC_CLOSURE) 1082 { 1083 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 1084 + ectx->ec_dfunc_idx; 1085 1086 // The closure needs to find arguments and local 1087 // variables in the current stack. 1088 pt->pt_outer.out_stack = &ectx->ec_stack; 1089 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx; 1090 pt->pt_outer.out_up = ectx->ec_outer; 1091 pt->pt_outer.out_up_is_copy = TRUE; 1092 1093 // If this function returns and the closure is still 1094 // being used, we need to make a copy of the context 1095 // (arguments and local variables). Store a reference 1096 // to the partial so we can handle that. 1097 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL) 1098 { 1099 vim_free(pt); 1100 return FAIL; 1101 } 1102 // Extra variable keeps the count of closures created 1103 // in the current function call. 1104 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx 1105 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number; 1106 1107 ((partial_T **)ectx->ec_funcrefs.ga_data) 1108 [ectx->ec_funcrefs.ga_len] = pt; 1109 ++pt->pt_refcount; 1110 ++ectx->ec_funcrefs.ga_len; 1111 } 1112 ++ufunc->uf_refcount; 1113 return OK; 1114 } 1115 1116 1117 /* 1118 * Call a "def" function from old Vim script. 1119 * Return OK or FAIL. 1120 */ 1121 int 1122 call_def_function( 1123 ufunc_T *ufunc, 1124 int argc_arg, // nr of arguments 1125 typval_T *argv, // arguments 1126 partial_T *partial, // optional partial for context 1127 typval_T *rettv) // return value 1128 { 1129 ectx_T ectx; // execution context 1130 int argc = argc_arg; 1131 int initial_frame_idx; 1132 typval_T *tv; 1133 int idx; 1134 int ret = FAIL; 1135 int defcount = ufunc->uf_args.ga_len - argc; 1136 sctx_T save_current_sctx = current_sctx; 1137 int breakcheck_count = 0; 1138 int did_emsg_before = did_emsg_cumul + did_emsg; 1139 int save_suppress_errthrow = suppress_errthrow; 1140 msglist_T **saved_msg_list = NULL; 1141 msglist_T *private_msg_list = NULL; 1142 cmdmod_T save_cmdmod; 1143 int restore_cmdmod = FALSE; 1144 int restore_cmdmod_stacklen = 0; 1145 int save_emsg_silent_def = emsg_silent_def; 1146 int save_did_emsg_def = did_emsg_def; 1147 int trylevel_at_start = trylevel; 1148 int orig_funcdepth; 1149 where_T where; 1150 1151 // Get pointer to item in the stack. 1152 #define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx) 1153 1154 // Get pointer to item at the bottom of the stack, -1 is the bottom. 1155 #undef STACK_TV_BOT 1156 #define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx) 1157 1158 // Get pointer to a local variable on the stack. Negative for arguments. 1159 #define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx) 1160 1161 if (ufunc->uf_def_status == UF_NOT_COMPILED 1162 || (func_needs_compiling(ufunc, PROFILING(ufunc)) 1163 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL) 1164 == FAIL)) 1165 { 1166 if (did_emsg_cumul + did_emsg == did_emsg_before) 1167 semsg(_(e_function_is_not_compiled_str), 1168 printable_func_name(ufunc)); 1169 return FAIL; 1170 } 1171 1172 { 1173 // Check the function was really compiled. 1174 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 1175 + ufunc->uf_dfunc_idx; 1176 if (INSTRUCTIONS(dfunc) == NULL) 1177 { 1178 iemsg("using call_def_function() on not compiled function"); 1179 return FAIL; 1180 } 1181 } 1182 1183 // If depth of calling is getting too high, don't execute the function. 1184 orig_funcdepth = funcdepth_get(); 1185 if (funcdepth_increment() == FAIL) 1186 return FAIL; 1187 1188 CLEAR_FIELD(ectx); 1189 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx; 1190 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500); 1191 if (ga_grow(&ectx.ec_stack, 20) == FAIL) 1192 { 1193 funcdepth_decrement(); 1194 return FAIL; 1195 } 1196 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10); 1197 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10); 1198 1199 // Put arguments on the stack, but no more than what the function expects. 1200 // A lambda can be called with more arguments than it uses. 1201 for (idx = 0; idx < argc 1202 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len); 1203 ++idx) 1204 { 1205 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len 1206 && check_typval_arg_type(ufunc->uf_arg_types[idx], &argv[idx], 1207 idx + 1) == FAIL) 1208 goto failed_early; 1209 copy_tv(&argv[idx], STACK_TV_BOT(0)); 1210 ++ectx.ec_stack.ga_len; 1211 } 1212 1213 // Turn varargs into a list. Empty list if no args. 1214 if (ufunc->uf_va_name != NULL) 1215 { 1216 int vararg_count = argc - ufunc->uf_args.ga_len; 1217 1218 if (vararg_count < 0) 1219 vararg_count = 0; 1220 else 1221 argc -= vararg_count; 1222 if (exe_newlist(vararg_count, &ectx) == FAIL) 1223 goto failed_early; 1224 1225 // Check the type of the list items. 1226 tv = STACK_TV_BOT(-1); 1227 if (ufunc->uf_va_type != NULL 1228 && ufunc->uf_va_type != &t_any 1229 && ufunc->uf_va_type->tt_member != &t_any 1230 && tv->vval.v_list != NULL) 1231 { 1232 type_T *expected = ufunc->uf_va_type->tt_member; 1233 listitem_T *li = tv->vval.v_list->lv_first; 1234 1235 for (idx = 0; idx < vararg_count; ++idx) 1236 { 1237 if (check_typval_arg_type(expected, &li->li_tv, 1238 argc + idx + 1) == FAIL) 1239 goto failed_early; 1240 li = li->li_next; 1241 } 1242 } 1243 1244 if (defcount > 0) 1245 // Move varargs list to below missing default arguments. 1246 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1); 1247 --ectx.ec_stack.ga_len; 1248 } 1249 1250 // Make space for omitted arguments, will store default value below. 1251 // Any varargs list goes after them. 1252 if (defcount > 0) 1253 for (idx = 0; idx < defcount; ++idx) 1254 { 1255 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN; 1256 ++ectx.ec_stack.ga_len; 1257 } 1258 if (ufunc->uf_va_name != NULL) 1259 ++ectx.ec_stack.ga_len; 1260 1261 // Frame pointer points to just after arguments. 1262 ectx.ec_frame_idx = ectx.ec_stack.ga_len; 1263 initial_frame_idx = ectx.ec_frame_idx; 1264 1265 { 1266 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 1267 + ufunc->uf_dfunc_idx; 1268 ufunc_T *base_ufunc = dfunc->df_ufunc; 1269 1270 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done 1271 // by copy_func(). 1272 if (partial != NULL || base_ufunc->uf_partial != NULL) 1273 { 1274 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T); 1275 if (ectx.ec_outer == NULL) 1276 goto failed_early; 1277 if (partial != NULL) 1278 { 1279 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL) 1280 { 1281 if (current_ectx->ec_outer != NULL) 1282 *ectx.ec_outer = *current_ectx->ec_outer; 1283 } 1284 else 1285 *ectx.ec_outer = partial->pt_outer; 1286 } 1287 else 1288 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer; 1289 ectx.ec_outer->out_up_is_copy = TRUE; 1290 } 1291 } 1292 1293 // dummy frame entries 1294 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx) 1295 { 1296 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN; 1297 ++ectx.ec_stack.ga_len; 1298 } 1299 1300 { 1301 // Reserve space for local variables and any closure reference count. 1302 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 1303 + ufunc->uf_dfunc_idx; 1304 1305 for (idx = 0; idx < dfunc->df_varcount; ++idx) 1306 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN; 1307 ectx.ec_stack.ga_len += dfunc->df_varcount; 1308 if (dfunc->df_has_closure) 1309 { 1310 STACK_TV_VAR(idx)->v_type = VAR_NUMBER; 1311 STACK_TV_VAR(idx)->vval.v_number = 0; 1312 ++ectx.ec_stack.ga_len; 1313 } 1314 1315 ectx.ec_instr = INSTRUCTIONS(dfunc); 1316 } 1317 1318 // Following errors are in the function, not the caller. 1319 // Commands behave like vim9script. 1320 estack_push_ufunc(ufunc, 1); 1321 current_sctx = ufunc->uf_script_ctx; 1322 current_sctx.sc_version = SCRIPT_VERSION_VIM9; 1323 1324 // Use a specific location for storing error messages to be converted to an 1325 // exception. 1326 saved_msg_list = msg_list; 1327 msg_list = &private_msg_list; 1328 1329 // Do turn errors into exceptions. 1330 suppress_errthrow = FALSE; 1331 1332 // When ":silent!" was used before calling then we still abort the 1333 // function. If ":silent!" is used in the function then we don't. 1334 emsg_silent_def = emsg_silent; 1335 did_emsg_def = 0; 1336 1337 where.wt_index = 0; 1338 where.wt_variable = FALSE; 1339 1340 // Decide where to start execution, handles optional arguments. 1341 init_instr_idx(ufunc, argc, &ectx); 1342 1343 for (;;) 1344 { 1345 isn_T *iptr; 1346 1347 if (++breakcheck_count >= 100) 1348 { 1349 line_breakcheck(); 1350 breakcheck_count = 0; 1351 } 1352 if (got_int) 1353 { 1354 // Turn CTRL-C into an exception. 1355 got_int = FALSE; 1356 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL) 1357 goto failed; 1358 did_throw = TRUE; 1359 } 1360 1361 if (did_emsg && msg_list != NULL && *msg_list != NULL) 1362 { 1363 // Turn an error message into an exception. 1364 did_emsg = FALSE; 1365 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL) 1366 goto failed; 1367 did_throw = TRUE; 1368 *msg_list = NULL; 1369 } 1370 1371 if (did_throw && !ectx.ec_in_catch) 1372 { 1373 garray_T *trystack = &ectx.ec_trystack; 1374 trycmd_T *trycmd = NULL; 1375 1376 // An exception jumps to the first catch, finally, or returns from 1377 // the current function. 1378 if (trystack->ga_len > 0) 1379 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1; 1380 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx) 1381 { 1382 // jump to ":catch" or ":finally" 1383 ectx.ec_in_catch = TRUE; 1384 ectx.ec_iidx = trycmd->tcd_catch_idx; 1385 } 1386 else 1387 { 1388 // Not inside try or need to return from current functions. 1389 // Push a dummy return value. 1390 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1391 goto failed; 1392 tv = STACK_TV_BOT(0); 1393 tv->v_type = VAR_NUMBER; 1394 tv->vval.v_number = 0; 1395 ++ectx.ec_stack.ga_len; 1396 if (ectx.ec_frame_idx == initial_frame_idx) 1397 { 1398 // At the toplevel we are done. 1399 need_rethrow = TRUE; 1400 if (handle_closure_in_use(&ectx, FALSE) == FAIL) 1401 goto failed; 1402 goto done; 1403 } 1404 1405 if (func_return(&ectx) == FAIL) 1406 goto failed; 1407 } 1408 continue; 1409 } 1410 1411 iptr = &ectx.ec_instr[ectx.ec_iidx++]; 1412 switch (iptr->isn_type) 1413 { 1414 // execute Ex command line 1415 case ISN_EXEC: 1416 { 1417 source_cookie_T cookie; 1418 1419 SOURCING_LNUM = iptr->isn_lnum; 1420 // Pass getsourceline to get an error for a missing ":end" 1421 // command. 1422 CLEAR_FIELD(cookie); 1423 cookie.sourcing_lnum = iptr->isn_lnum - 1; 1424 if (do_cmdline(iptr->isn_arg.string, 1425 getsourceline, &cookie, 1426 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) 1427 == FAIL 1428 || did_emsg) 1429 goto on_error; 1430 } 1431 break; 1432 1433 // execute Ex command from pieces on the stack 1434 case ISN_EXECCONCAT: 1435 { 1436 int count = iptr->isn_arg.number; 1437 size_t len = 0; 1438 int pass; 1439 int i; 1440 char_u *cmd = NULL; 1441 char_u *str; 1442 1443 for (pass = 1; pass <= 2; ++pass) 1444 { 1445 for (i = 0; i < count; ++i) 1446 { 1447 tv = STACK_TV_BOT(i - count); 1448 str = tv->vval.v_string; 1449 if (str != NULL && *str != NUL) 1450 { 1451 if (pass == 2) 1452 STRCPY(cmd + len, str); 1453 len += STRLEN(str); 1454 } 1455 if (pass == 2) 1456 clear_tv(tv); 1457 } 1458 if (pass == 1) 1459 { 1460 cmd = alloc(len + 1); 1461 if (cmd == NULL) 1462 goto failed; 1463 len = 0; 1464 } 1465 } 1466 1467 SOURCING_LNUM = iptr->isn_lnum; 1468 do_cmdline_cmd(cmd); 1469 vim_free(cmd); 1470 } 1471 break; 1472 1473 // execute :echo {string} ... 1474 case ISN_ECHO: 1475 { 1476 int count = iptr->isn_arg.echo.echo_count; 1477 int atstart = TRUE; 1478 int needclr = TRUE; 1479 1480 for (idx = 0; idx < count; ++idx) 1481 { 1482 tv = STACK_TV_BOT(idx - count); 1483 echo_one(tv, iptr->isn_arg.echo.echo_with_white, 1484 &atstart, &needclr); 1485 clear_tv(tv); 1486 } 1487 if (needclr) 1488 msg_clr_eos(); 1489 ectx.ec_stack.ga_len -= count; 1490 } 1491 break; 1492 1493 // :execute {string} ... 1494 // :echomsg {string} ... 1495 // :echoerr {string} ... 1496 case ISN_EXECUTE: 1497 case ISN_ECHOMSG: 1498 case ISN_ECHOERR: 1499 { 1500 int count = iptr->isn_arg.number; 1501 garray_T ga; 1502 char_u buf[NUMBUFLEN]; 1503 char_u *p; 1504 int len; 1505 int failed = FALSE; 1506 1507 ga_init2(&ga, 1, 80); 1508 for (idx = 0; idx < count; ++idx) 1509 { 1510 tv = STACK_TV_BOT(idx - count); 1511 if (iptr->isn_type == ISN_EXECUTE) 1512 { 1513 if (tv->v_type == VAR_CHANNEL 1514 || tv->v_type == VAR_JOB) 1515 { 1516 SOURCING_LNUM = iptr->isn_lnum; 1517 emsg(_(e_inval_string)); 1518 break; 1519 } 1520 else 1521 p = tv_get_string_buf(tv, buf); 1522 } 1523 else 1524 p = tv_stringify(tv, buf); 1525 1526 len = (int)STRLEN(p); 1527 if (ga_grow(&ga, len + 2) == FAIL) 1528 failed = TRUE; 1529 else 1530 { 1531 if (ga.ga_len > 0) 1532 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; 1533 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); 1534 ga.ga_len += len; 1535 } 1536 clear_tv(tv); 1537 } 1538 ectx.ec_stack.ga_len -= count; 1539 if (failed) 1540 { 1541 ga_clear(&ga); 1542 goto on_error; 1543 } 1544 1545 if (ga.ga_data != NULL) 1546 { 1547 if (iptr->isn_type == ISN_EXECUTE) 1548 { 1549 SOURCING_LNUM = iptr->isn_lnum; 1550 do_cmdline_cmd((char_u *)ga.ga_data); 1551 if (did_emsg) 1552 { 1553 ga_clear(&ga); 1554 goto on_error; 1555 } 1556 } 1557 else 1558 { 1559 msg_sb_eol(); 1560 if (iptr->isn_type == ISN_ECHOMSG) 1561 { 1562 msg_attr(ga.ga_data, echo_attr); 1563 out_flush(); 1564 } 1565 else 1566 { 1567 SOURCING_LNUM = iptr->isn_lnum; 1568 emsg(ga.ga_data); 1569 } 1570 } 1571 } 1572 ga_clear(&ga); 1573 } 1574 break; 1575 1576 // load local variable or argument 1577 case ISN_LOAD: 1578 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1579 goto failed; 1580 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0)); 1581 ++ectx.ec_stack.ga_len; 1582 break; 1583 1584 // load v: variable 1585 case ISN_LOADV: 1586 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1587 goto failed; 1588 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0)); 1589 ++ectx.ec_stack.ga_len; 1590 break; 1591 1592 // load s: variable in Vim9 script 1593 case ISN_LOADSCRIPT: 1594 { 1595 scriptref_T *sref = iptr->isn_arg.script.scriptref; 1596 svar_T *sv; 1597 1598 sv = get_script_svar(sref, &ectx); 1599 if (sv == NULL) 1600 goto failed; 1601 allocate_if_null(sv->sv_tv); 1602 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1603 goto failed; 1604 copy_tv(sv->sv_tv, STACK_TV_BOT(0)); 1605 ++ectx.ec_stack.ga_len; 1606 } 1607 break; 1608 1609 // load s: variable in old script 1610 case ISN_LOADS: 1611 { 1612 hashtab_T *ht = &SCRIPT_VARS( 1613 iptr->isn_arg.loadstore.ls_sid); 1614 char_u *name = iptr->isn_arg.loadstore.ls_name; 1615 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE); 1616 1617 if (di == NULL) 1618 { 1619 SOURCING_LNUM = iptr->isn_lnum; 1620 semsg(_(e_undefined_variable_str), name); 1621 goto on_error; 1622 } 1623 else 1624 { 1625 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1626 goto failed; 1627 copy_tv(&di->di_tv, STACK_TV_BOT(0)); 1628 ++ectx.ec_stack.ga_len; 1629 } 1630 } 1631 break; 1632 1633 // load g:/b:/w:/t: variable 1634 case ISN_LOADG: 1635 case ISN_LOADB: 1636 case ISN_LOADW: 1637 case ISN_LOADT: 1638 { 1639 dictitem_T *di = NULL; 1640 hashtab_T *ht = NULL; 1641 char namespace; 1642 1643 switch (iptr->isn_type) 1644 { 1645 case ISN_LOADG: 1646 ht = get_globvar_ht(); 1647 namespace = 'g'; 1648 break; 1649 case ISN_LOADB: 1650 ht = &curbuf->b_vars->dv_hashtab; 1651 namespace = 'b'; 1652 break; 1653 case ISN_LOADW: 1654 ht = &curwin->w_vars->dv_hashtab; 1655 namespace = 'w'; 1656 break; 1657 case ISN_LOADT: 1658 ht = &curtab->tp_vars->dv_hashtab; 1659 namespace = 't'; 1660 break; 1661 default: // Cannot reach here 1662 goto failed; 1663 } 1664 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE); 1665 1666 if (di == NULL) 1667 { 1668 SOURCING_LNUM = iptr->isn_lnum; 1669 semsg(_(e_undefined_variable_char_str), 1670 namespace, iptr->isn_arg.string); 1671 goto on_error; 1672 } 1673 else 1674 { 1675 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1676 goto failed; 1677 copy_tv(&di->di_tv, STACK_TV_BOT(0)); 1678 ++ectx.ec_stack.ga_len; 1679 } 1680 } 1681 break; 1682 1683 // load autoload variable 1684 case ISN_LOADAUTO: 1685 { 1686 char_u *name = iptr->isn_arg.string; 1687 1688 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1689 goto failed; 1690 SOURCING_LNUM = iptr->isn_lnum; 1691 if (eval_variable(name, (int)STRLEN(name), 1692 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL) 1693 goto on_error; 1694 ++ectx.ec_stack.ga_len; 1695 } 1696 break; 1697 1698 // load g:/b:/w:/t: namespace 1699 case ISN_LOADGDICT: 1700 case ISN_LOADBDICT: 1701 case ISN_LOADWDICT: 1702 case ISN_LOADTDICT: 1703 { 1704 dict_T *d = NULL; 1705 1706 switch (iptr->isn_type) 1707 { 1708 case ISN_LOADGDICT: d = get_globvar_dict(); break; 1709 case ISN_LOADBDICT: d = curbuf->b_vars; break; 1710 case ISN_LOADWDICT: d = curwin->w_vars; break; 1711 case ISN_LOADTDICT: d = curtab->tp_vars; break; 1712 default: // Cannot reach here 1713 goto failed; 1714 } 1715 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1716 goto failed; 1717 tv = STACK_TV_BOT(0); 1718 tv->v_type = VAR_DICT; 1719 tv->v_lock = 0; 1720 tv->vval.v_dict = d; 1721 ++ectx.ec_stack.ga_len; 1722 } 1723 break; 1724 1725 // load &option 1726 case ISN_LOADOPT: 1727 { 1728 typval_T optval; 1729 char_u *name = iptr->isn_arg.string; 1730 1731 // This is not expected to fail, name is checked during 1732 // compilation: don't set SOURCING_LNUM. 1733 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1734 goto failed; 1735 if (eval_option(&name, &optval, TRUE) == FAIL) 1736 goto failed; 1737 *STACK_TV_BOT(0) = optval; 1738 ++ectx.ec_stack.ga_len; 1739 } 1740 break; 1741 1742 // load $ENV 1743 case ISN_LOADENV: 1744 { 1745 typval_T optval; 1746 char_u *name = iptr->isn_arg.string; 1747 1748 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1749 goto failed; 1750 // name is always valid, checked when compiling 1751 (void)eval_env_var(&name, &optval, TRUE); 1752 *STACK_TV_BOT(0) = optval; 1753 ++ectx.ec_stack.ga_len; 1754 } 1755 break; 1756 1757 // load @register 1758 case ISN_LOADREG: 1759 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1760 goto failed; 1761 tv = STACK_TV_BOT(0); 1762 tv->v_type = VAR_STRING; 1763 tv->v_lock = 0; 1764 // This may result in NULL, which should be equivalent to an 1765 // empty string. 1766 tv->vval.v_string = get_reg_contents( 1767 iptr->isn_arg.number, GREG_EXPR_SRC); 1768 ++ectx.ec_stack.ga_len; 1769 break; 1770 1771 // store local variable 1772 case ISN_STORE: 1773 --ectx.ec_stack.ga_len; 1774 tv = STACK_TV_VAR(iptr->isn_arg.number); 1775 clear_tv(tv); 1776 *tv = *STACK_TV_BOT(0); 1777 break; 1778 1779 // store s: variable in old script 1780 case ISN_STORES: 1781 { 1782 hashtab_T *ht = &SCRIPT_VARS( 1783 iptr->isn_arg.loadstore.ls_sid); 1784 char_u *name = iptr->isn_arg.loadstore.ls_name; 1785 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE); 1786 1787 --ectx.ec_stack.ga_len; 1788 if (di == NULL) 1789 store_var(name, STACK_TV_BOT(0)); 1790 else 1791 { 1792 clear_tv(&di->di_tv); 1793 di->di_tv = *STACK_TV_BOT(0); 1794 } 1795 } 1796 break; 1797 1798 // store script-local variable in Vim9 script 1799 case ISN_STORESCRIPT: 1800 { 1801 scriptref_T *sref = iptr->isn_arg.script.scriptref; 1802 svar_T *sv; 1803 1804 sv = get_script_svar(sref, &ectx); 1805 if (sv == NULL) 1806 goto failed; 1807 --ectx.ec_stack.ga_len; 1808 clear_tv(sv->sv_tv); 1809 *sv->sv_tv = *STACK_TV_BOT(0); 1810 } 1811 break; 1812 1813 // store option 1814 case ISN_STOREOPT: 1815 { 1816 long n = 0; 1817 char_u *s = NULL; 1818 char *msg; 1819 1820 --ectx.ec_stack.ga_len; 1821 tv = STACK_TV_BOT(0); 1822 if (tv->v_type == VAR_STRING) 1823 { 1824 s = tv->vval.v_string; 1825 if (s == NULL) 1826 s = (char_u *)""; 1827 } 1828 else 1829 // must be VAR_NUMBER, CHECKTYPE makes sure 1830 n = tv->vval.v_number; 1831 msg = set_option_value(iptr->isn_arg.storeopt.so_name, 1832 n, s, iptr->isn_arg.storeopt.so_flags); 1833 clear_tv(tv); 1834 if (msg != NULL) 1835 { 1836 SOURCING_LNUM = iptr->isn_lnum; 1837 emsg(_(msg)); 1838 goto on_error; 1839 } 1840 } 1841 break; 1842 1843 // store $ENV 1844 case ISN_STOREENV: 1845 --ectx.ec_stack.ga_len; 1846 tv = STACK_TV_BOT(0); 1847 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv)); 1848 clear_tv(tv); 1849 break; 1850 1851 // store @r 1852 case ISN_STOREREG: 1853 { 1854 int reg = iptr->isn_arg.number; 1855 1856 --ectx.ec_stack.ga_len; 1857 tv = STACK_TV_BOT(0); 1858 write_reg_contents(reg == '@' ? '"' : reg, 1859 tv_get_string(tv), -1, FALSE); 1860 clear_tv(tv); 1861 } 1862 break; 1863 1864 // store v: variable 1865 case ISN_STOREV: 1866 --ectx.ec_stack.ga_len; 1867 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0)) 1868 == FAIL) 1869 // should not happen, type is checked when compiling 1870 goto on_error; 1871 break; 1872 1873 // store g:/b:/w:/t: variable 1874 case ISN_STOREG: 1875 case ISN_STOREB: 1876 case ISN_STOREW: 1877 case ISN_STORET: 1878 { 1879 dictitem_T *di; 1880 hashtab_T *ht; 1881 char_u *name = iptr->isn_arg.string + 2; 1882 1883 switch (iptr->isn_type) 1884 { 1885 case ISN_STOREG: 1886 ht = get_globvar_ht(); 1887 break; 1888 case ISN_STOREB: 1889 ht = &curbuf->b_vars->dv_hashtab; 1890 break; 1891 case ISN_STOREW: 1892 ht = &curwin->w_vars->dv_hashtab; 1893 break; 1894 case ISN_STORET: 1895 ht = &curtab->tp_vars->dv_hashtab; 1896 break; 1897 default: // Cannot reach here 1898 goto failed; 1899 } 1900 1901 --ectx.ec_stack.ga_len; 1902 di = find_var_in_ht(ht, 0, name, TRUE); 1903 if (di == NULL) 1904 store_var(iptr->isn_arg.string, STACK_TV_BOT(0)); 1905 else 1906 { 1907 SOURCING_LNUM = iptr->isn_lnum; 1908 if (var_check_permission(di, name) == FAIL) 1909 goto on_error; 1910 clear_tv(&di->di_tv); 1911 di->di_tv = *STACK_TV_BOT(0); 1912 } 1913 } 1914 break; 1915 1916 // store an autoload variable 1917 case ISN_STOREAUTO: 1918 SOURCING_LNUM = iptr->isn_lnum; 1919 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE); 1920 clear_tv(STACK_TV_BOT(-1)); 1921 --ectx.ec_stack.ga_len; 1922 break; 1923 1924 // store number in local variable 1925 case ISN_STORENR: 1926 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx); 1927 clear_tv(tv); 1928 tv->v_type = VAR_NUMBER; 1929 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val; 1930 break; 1931 1932 // store value in list or dict variable 1933 case ISN_STOREINDEX: 1934 { 1935 vartype_T dest_type = iptr->isn_arg.vartype; 1936 typval_T *tv_idx = STACK_TV_BOT(-2); 1937 typval_T *tv_dest = STACK_TV_BOT(-1); 1938 int status = OK; 1939 1940 // Stack contains: 1941 // -3 value to be stored 1942 // -2 index 1943 // -1 dict or list 1944 tv = STACK_TV_BOT(-3); 1945 SOURCING_LNUM = iptr->isn_lnum; 1946 if (dest_type == VAR_ANY) 1947 { 1948 dest_type = tv_dest->v_type; 1949 if (dest_type == VAR_DICT) 1950 status = do_2string(tv_idx, TRUE); 1951 else if (dest_type == VAR_LIST 1952 && tv_idx->v_type != VAR_NUMBER) 1953 { 1954 emsg(_(e_number_exp)); 1955 status = FAIL; 1956 } 1957 } 1958 else if (dest_type != tv_dest->v_type) 1959 { 1960 // just in case, should be OK 1961 semsg(_(e_expected_str_but_got_str), 1962 vartype_name(dest_type), 1963 vartype_name(tv_dest->v_type)); 1964 status = FAIL; 1965 } 1966 1967 if (status == OK && dest_type == VAR_LIST) 1968 { 1969 long lidx = (long)tv_idx->vval.v_number; 1970 list_T *list = tv_dest->vval.v_list; 1971 1972 if (list == NULL) 1973 { 1974 emsg(_(e_list_not_set)); 1975 goto on_error; 1976 } 1977 if (lidx < 0 && list->lv_len + lidx >= 0) 1978 // negative index is relative to the end 1979 lidx = list->lv_len + lidx; 1980 if (lidx < 0 || lidx > list->lv_len) 1981 { 1982 semsg(_(e_listidx), lidx); 1983 goto on_error; 1984 } 1985 if (lidx < list->lv_len) 1986 { 1987 listitem_T *li = list_find(list, lidx); 1988 1989 if (error_if_locked(li->li_tv.v_lock, 1990 e_cannot_change_list_item)) 1991 goto on_error; 1992 // overwrite existing list item 1993 clear_tv(&li->li_tv); 1994 li->li_tv = *tv; 1995 } 1996 else 1997 { 1998 if (error_if_locked(list->lv_lock, 1999 e_cannot_change_list)) 2000 goto on_error; 2001 // append to list, only fails when out of memory 2002 if (list_append_tv(list, tv) == FAIL) 2003 goto failed; 2004 clear_tv(tv); 2005 } 2006 } 2007 else if (status == OK && dest_type == VAR_DICT) 2008 { 2009 char_u *key = tv_idx->vval.v_string; 2010 dict_T *dict = tv_dest->vval.v_dict; 2011 dictitem_T *di; 2012 2013 SOURCING_LNUM = iptr->isn_lnum; 2014 if (dict == NULL) 2015 { 2016 emsg(_(e_dictionary_not_set)); 2017 goto on_error; 2018 } 2019 if (key == NULL) 2020 key = (char_u *)""; 2021 di = dict_find(dict, key, -1); 2022 if (di != NULL) 2023 { 2024 if (error_if_locked(di->di_tv.v_lock, 2025 e_cannot_change_dict_item)) 2026 goto on_error; 2027 // overwrite existing value 2028 clear_tv(&di->di_tv); 2029 di->di_tv = *tv; 2030 } 2031 else 2032 { 2033 if (error_if_locked(dict->dv_lock, 2034 e_cannot_change_dict)) 2035 goto on_error; 2036 // add to dict, only fails when out of memory 2037 if (dict_add_tv(dict, (char *)key, tv) == FAIL) 2038 goto failed; 2039 clear_tv(tv); 2040 } 2041 } 2042 else 2043 { 2044 status = FAIL; 2045 semsg(_(e_cannot_index_str), vartype_name(dest_type)); 2046 } 2047 2048 clear_tv(tv_idx); 2049 clear_tv(tv_dest); 2050 ectx.ec_stack.ga_len -= 3; 2051 if (status == FAIL) 2052 { 2053 clear_tv(tv); 2054 goto on_error; 2055 } 2056 } 2057 break; 2058 2059 // load or store variable or argument from outer scope 2060 case ISN_LOADOUTER: 2061 case ISN_STOREOUTER: 2062 { 2063 int depth = iptr->isn_arg.outer.outer_depth; 2064 outer_T *outer = ectx.ec_outer; 2065 2066 while (depth > 1 && outer != NULL) 2067 { 2068 outer = outer->out_up; 2069 --depth; 2070 } 2071 if (outer == NULL) 2072 { 2073 SOURCING_LNUM = iptr->isn_lnum; 2074 iemsg("LOADOUTER depth more than scope levels"); 2075 goto failed; 2076 } 2077 tv = ((typval_T *)outer->out_stack->ga_data) 2078 + outer->out_frame_idx + STACK_FRAME_SIZE 2079 + iptr->isn_arg.outer.outer_idx; 2080 if (iptr->isn_type == ISN_LOADOUTER) 2081 { 2082 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2083 goto failed; 2084 copy_tv(tv, STACK_TV_BOT(0)); 2085 ++ectx.ec_stack.ga_len; 2086 } 2087 else 2088 { 2089 --ectx.ec_stack.ga_len; 2090 clear_tv(tv); 2091 *tv = *STACK_TV_BOT(0); 2092 } 2093 } 2094 break; 2095 2096 // unlet item in list or dict variable 2097 case ISN_UNLETINDEX: 2098 { 2099 typval_T *tv_idx = STACK_TV_BOT(-2); 2100 typval_T *tv_dest = STACK_TV_BOT(-1); 2101 int status = OK; 2102 2103 // Stack contains: 2104 // -2 index 2105 // -1 dict or list 2106 if (tv_dest->v_type == VAR_DICT) 2107 { 2108 // unlet a dict item, index must be a string 2109 if (tv_idx->v_type != VAR_STRING) 2110 { 2111 SOURCING_LNUM = iptr->isn_lnum; 2112 semsg(_(e_expected_str_but_got_str), 2113 vartype_name(VAR_STRING), 2114 vartype_name(tv_idx->v_type)); 2115 status = FAIL; 2116 } 2117 else 2118 { 2119 dict_T *d = tv_dest->vval.v_dict; 2120 char_u *key = tv_idx->vval.v_string; 2121 dictitem_T *di = NULL; 2122 2123 if (key == NULL) 2124 key = (char_u *)""; 2125 if (d != NULL) 2126 di = dict_find(d, key, (int)STRLEN(key)); 2127 if (di == NULL) 2128 { 2129 // NULL dict is equivalent to empty dict 2130 SOURCING_LNUM = iptr->isn_lnum; 2131 semsg(_(e_dictkey), key); 2132 status = FAIL; 2133 } 2134 else 2135 { 2136 // TODO: check for dict or item locked 2137 dictitem_remove(d, di); 2138 } 2139 } 2140 } 2141 else if (tv_dest->v_type == VAR_LIST) 2142 { 2143 // unlet a List item, index must be a number 2144 if (tv_idx->v_type != VAR_NUMBER) 2145 { 2146 SOURCING_LNUM = iptr->isn_lnum; 2147 semsg(_(e_expected_str_but_got_str), 2148 vartype_name(VAR_NUMBER), 2149 vartype_name(tv_idx->v_type)); 2150 status = FAIL; 2151 } 2152 else 2153 { 2154 list_T *l = tv_dest->vval.v_list; 2155 long n = (long)tv_idx->vval.v_number; 2156 listitem_T *li = NULL; 2157 2158 li = list_find(l, n); 2159 if (li == NULL) 2160 { 2161 SOURCING_LNUM = iptr->isn_lnum; 2162 semsg(_(e_listidx), n); 2163 status = FAIL; 2164 } 2165 else 2166 // TODO: check for list or item locked 2167 listitem_remove(l, li); 2168 } 2169 } 2170 else 2171 { 2172 status = FAIL; 2173 semsg(_(e_cannot_index_str), 2174 vartype_name(tv_dest->v_type)); 2175 } 2176 2177 clear_tv(tv_idx); 2178 clear_tv(tv_dest); 2179 ectx.ec_stack.ga_len -= 2; 2180 if (status == FAIL) 2181 goto on_error; 2182 } 2183 break; 2184 2185 // push constant 2186 case ISN_PUSHNR: 2187 case ISN_PUSHBOOL: 2188 case ISN_PUSHSPEC: 2189 case ISN_PUSHF: 2190 case ISN_PUSHS: 2191 case ISN_PUSHBLOB: 2192 case ISN_PUSHFUNC: 2193 case ISN_PUSHCHANNEL: 2194 case ISN_PUSHJOB: 2195 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2196 goto failed; 2197 tv = STACK_TV_BOT(0); 2198 tv->v_lock = 0; 2199 ++ectx.ec_stack.ga_len; 2200 switch (iptr->isn_type) 2201 { 2202 case ISN_PUSHNR: 2203 tv->v_type = VAR_NUMBER; 2204 tv->vval.v_number = iptr->isn_arg.number; 2205 break; 2206 case ISN_PUSHBOOL: 2207 tv->v_type = VAR_BOOL; 2208 tv->vval.v_number = iptr->isn_arg.number; 2209 break; 2210 case ISN_PUSHSPEC: 2211 tv->v_type = VAR_SPECIAL; 2212 tv->vval.v_number = iptr->isn_arg.number; 2213 break; 2214 #ifdef FEAT_FLOAT 2215 case ISN_PUSHF: 2216 tv->v_type = VAR_FLOAT; 2217 tv->vval.v_float = iptr->isn_arg.fnumber; 2218 break; 2219 #endif 2220 case ISN_PUSHBLOB: 2221 blob_copy(iptr->isn_arg.blob, tv); 2222 break; 2223 case ISN_PUSHFUNC: 2224 tv->v_type = VAR_FUNC; 2225 if (iptr->isn_arg.string == NULL) 2226 tv->vval.v_string = NULL; 2227 else 2228 tv->vval.v_string = 2229 vim_strsave(iptr->isn_arg.string); 2230 break; 2231 case ISN_PUSHCHANNEL: 2232 #ifdef FEAT_JOB_CHANNEL 2233 tv->v_type = VAR_CHANNEL; 2234 tv->vval.v_channel = iptr->isn_arg.channel; 2235 if (tv->vval.v_channel != NULL) 2236 ++tv->vval.v_channel->ch_refcount; 2237 #endif 2238 break; 2239 case ISN_PUSHJOB: 2240 #ifdef FEAT_JOB_CHANNEL 2241 tv->v_type = VAR_JOB; 2242 tv->vval.v_job = iptr->isn_arg.job; 2243 if (tv->vval.v_job != NULL) 2244 ++tv->vval.v_job->jv_refcount; 2245 #endif 2246 break; 2247 default: 2248 tv->v_type = VAR_STRING; 2249 tv->vval.v_string = vim_strsave( 2250 iptr->isn_arg.string == NULL 2251 ? (char_u *)"" : iptr->isn_arg.string); 2252 } 2253 break; 2254 2255 case ISN_UNLET: 2256 if (do_unlet(iptr->isn_arg.unlet.ul_name, 2257 iptr->isn_arg.unlet.ul_forceit) == FAIL) 2258 goto on_error; 2259 break; 2260 case ISN_UNLETENV: 2261 vim_unsetenv(iptr->isn_arg.unlet.ul_name); 2262 break; 2263 2264 case ISN_LOCKCONST: 2265 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE); 2266 break; 2267 2268 // create a list from items on the stack; uses a single allocation 2269 // for the list header and the items 2270 case ISN_NEWLIST: 2271 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL) 2272 goto failed; 2273 break; 2274 2275 // create a dict from items on the stack 2276 case ISN_NEWDICT: 2277 { 2278 int count = iptr->isn_arg.number; 2279 dict_T *dict = dict_alloc(); 2280 dictitem_T *item; 2281 char_u *key; 2282 2283 if (dict == NULL) 2284 goto failed; 2285 for (idx = 0; idx < count; ++idx) 2286 { 2287 // have already checked key type is VAR_STRING 2288 tv = STACK_TV_BOT(2 * (idx - count)); 2289 // check key is unique 2290 key = tv->vval.v_string == NULL 2291 ? (char_u *)"" : tv->vval.v_string; 2292 item = dict_find(dict, key, -1); 2293 if (item != NULL) 2294 { 2295 SOURCING_LNUM = iptr->isn_lnum; 2296 semsg(_(e_duplicate_key), key); 2297 dict_unref(dict); 2298 goto on_error; 2299 } 2300 item = dictitem_alloc(key); 2301 clear_tv(tv); 2302 if (item == NULL) 2303 { 2304 dict_unref(dict); 2305 goto failed; 2306 } 2307 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1); 2308 item->di_tv.v_lock = 0; 2309 if (dict_add(dict, item) == FAIL) 2310 { 2311 // can this ever happen? 2312 dict_unref(dict); 2313 goto failed; 2314 } 2315 } 2316 2317 if (count > 0) 2318 ectx.ec_stack.ga_len -= 2 * count - 1; 2319 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2320 goto failed; 2321 else 2322 ++ectx.ec_stack.ga_len; 2323 tv = STACK_TV_BOT(-1); 2324 tv->v_type = VAR_DICT; 2325 tv->v_lock = 0; 2326 tv->vval.v_dict = dict; 2327 ++dict->dv_refcount; 2328 } 2329 break; 2330 2331 // call a :def function 2332 case ISN_DCALL: 2333 SOURCING_LNUM = iptr->isn_lnum; 2334 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL, 2335 iptr->isn_arg.dfunc.cdf_argcount, 2336 &ectx) == FAIL) 2337 goto on_error; 2338 break; 2339 2340 // call a builtin function 2341 case ISN_BCALL: 2342 SOURCING_LNUM = iptr->isn_lnum; 2343 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx, 2344 iptr->isn_arg.bfunc.cbf_argcount, 2345 &ectx) == FAIL) 2346 goto on_error; 2347 break; 2348 2349 // call a funcref or partial 2350 case ISN_PCALL: 2351 { 2352 cpfunc_T *pfunc = &iptr->isn_arg.pfunc; 2353 int r; 2354 typval_T partial_tv; 2355 2356 SOURCING_LNUM = iptr->isn_lnum; 2357 if (pfunc->cpf_top) 2358 { 2359 // funcref is above the arguments 2360 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1); 2361 } 2362 else 2363 { 2364 // Get the funcref from the stack. 2365 --ectx.ec_stack.ga_len; 2366 partial_tv = *STACK_TV_BOT(0); 2367 tv = &partial_tv; 2368 } 2369 r = call_partial(tv, pfunc->cpf_argcount, &ectx); 2370 if (tv == &partial_tv) 2371 clear_tv(&partial_tv); 2372 if (r == FAIL) 2373 goto on_error; 2374 } 2375 break; 2376 2377 case ISN_PCALL_END: 2378 // PCALL finished, arguments have been consumed and replaced by 2379 // the return value. Now clear the funcref from the stack, 2380 // and move the return value in its place. 2381 --ectx.ec_stack.ga_len; 2382 clear_tv(STACK_TV_BOT(-1)); 2383 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0); 2384 break; 2385 2386 // call a user defined function or funcref/partial 2387 case ISN_UCALL: 2388 { 2389 cufunc_T *cufunc = &iptr->isn_arg.ufunc; 2390 2391 SOURCING_LNUM = iptr->isn_lnum; 2392 if (call_eval_func(cufunc->cuf_name, 2393 cufunc->cuf_argcount, &ectx, iptr) == FAIL) 2394 goto on_error; 2395 } 2396 break; 2397 2398 // return from a :def function call 2399 case ISN_RETURN_ZERO: 2400 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2401 goto failed; 2402 tv = STACK_TV_BOT(0); 2403 ++ectx.ec_stack.ga_len; 2404 tv->v_type = VAR_NUMBER; 2405 tv->vval.v_number = 0; 2406 tv->v_lock = 0; 2407 // FALLTHROUGH 2408 2409 case ISN_RETURN: 2410 { 2411 garray_T *trystack = &ectx.ec_trystack; 2412 trycmd_T *trycmd = NULL; 2413 2414 if (trystack->ga_len > 0) 2415 trycmd = ((trycmd_T *)trystack->ga_data) 2416 + trystack->ga_len - 1; 2417 if (trycmd != NULL 2418 && trycmd->tcd_frame_idx == ectx.ec_frame_idx 2419 && trycmd->tcd_finally_idx != 0) 2420 { 2421 // jump to ":finally" 2422 ectx.ec_iidx = trycmd->tcd_finally_idx; 2423 trycmd->tcd_return = TRUE; 2424 } 2425 else 2426 goto func_return; 2427 } 2428 break; 2429 2430 // push a function reference to a compiled function 2431 case ISN_FUNCREF: 2432 { 2433 partial_T *pt = ALLOC_CLEAR_ONE(partial_T); 2434 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data) 2435 + iptr->isn_arg.funcref.fr_func; 2436 2437 if (pt == NULL) 2438 goto failed; 2439 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2440 { 2441 vim_free(pt); 2442 goto failed; 2443 } 2444 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc, 2445 &ectx) == FAIL) 2446 goto failed; 2447 2448 tv = STACK_TV_BOT(0); 2449 ++ectx.ec_stack.ga_len; 2450 tv->vval.v_partial = pt; 2451 tv->v_type = VAR_PARTIAL; 2452 tv->v_lock = 0; 2453 } 2454 break; 2455 2456 // Create a global function from a lambda. 2457 case ISN_NEWFUNC: 2458 { 2459 newfunc_T *newfunc = &iptr->isn_arg.newfunc; 2460 2461 if (copy_func(newfunc->nf_lambda, newfunc->nf_global, 2462 &ectx) == FAIL) 2463 goto failed; 2464 } 2465 break; 2466 2467 // List functions 2468 case ISN_DEF: 2469 if (iptr->isn_arg.string == NULL) 2470 list_functions(NULL); 2471 else 2472 { 2473 exarg_T ea; 2474 2475 CLEAR_FIELD(ea); 2476 ea.cmd = ea.arg = iptr->isn_arg.string; 2477 define_function(&ea, NULL); 2478 } 2479 break; 2480 2481 // jump if a condition is met 2482 case ISN_JUMP: 2483 { 2484 jumpwhen_T when = iptr->isn_arg.jump.jump_when; 2485 int error = FALSE; 2486 int jump = TRUE; 2487 2488 if (when != JUMP_ALWAYS) 2489 { 2490 tv = STACK_TV_BOT(-1); 2491 if (when == JUMP_IF_COND_FALSE 2492 || when == JUMP_IF_FALSE 2493 || when == JUMP_IF_COND_TRUE) 2494 { 2495 SOURCING_LNUM = iptr->isn_lnum; 2496 jump = tv_get_bool_chk(tv, &error); 2497 if (error) 2498 goto on_error; 2499 } 2500 else 2501 jump = tv2bool(tv); 2502 if (when == JUMP_IF_FALSE 2503 || when == JUMP_AND_KEEP_IF_FALSE 2504 || when == JUMP_IF_COND_FALSE) 2505 jump = !jump; 2506 if (when == JUMP_IF_FALSE || !jump) 2507 { 2508 // drop the value from the stack 2509 clear_tv(tv); 2510 --ectx.ec_stack.ga_len; 2511 } 2512 } 2513 if (jump) 2514 ectx.ec_iidx = iptr->isn_arg.jump.jump_where; 2515 } 2516 break; 2517 2518 // top of a for loop 2519 case ISN_FOR: 2520 { 2521 list_T *list = STACK_TV_BOT(-1)->vval.v_list; 2522 typval_T *idxtv = 2523 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx); 2524 2525 // push the next item from the list 2526 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2527 goto failed; 2528 ++idxtv->vval.v_number; 2529 if (list == NULL || idxtv->vval.v_number >= list->lv_len) 2530 // past the end of the list, jump to "endfor" 2531 ectx.ec_iidx = iptr->isn_arg.forloop.for_end; 2532 else if (list->lv_first == &range_list_item) 2533 { 2534 // non-materialized range() list 2535 tv = STACK_TV_BOT(0); 2536 tv->v_type = VAR_NUMBER; 2537 tv->v_lock = 0; 2538 tv->vval.v_number = list_find_nr( 2539 list, idxtv->vval.v_number, NULL); 2540 ++ectx.ec_stack.ga_len; 2541 } 2542 else 2543 { 2544 listitem_T *li = list_find(list, idxtv->vval.v_number); 2545 2546 copy_tv(&li->li_tv, STACK_TV_BOT(0)); 2547 ++ectx.ec_stack.ga_len; 2548 } 2549 } 2550 break; 2551 2552 // start of ":try" block 2553 case ISN_TRY: 2554 { 2555 trycmd_T *trycmd = NULL; 2556 2557 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL) 2558 goto failed; 2559 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data) 2560 + ectx.ec_trystack.ga_len; 2561 ++ectx.ec_trystack.ga_len; 2562 ++trylevel; 2563 trycmd->tcd_frame_idx = ectx.ec_frame_idx; 2564 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch; 2565 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally; 2566 trycmd->tcd_caught = FALSE; 2567 trycmd->tcd_return = FALSE; 2568 } 2569 break; 2570 2571 case ISN_PUSHEXC: 2572 if (current_exception == NULL) 2573 { 2574 SOURCING_LNUM = iptr->isn_lnum; 2575 iemsg("Evaluating catch while current_exception is NULL"); 2576 goto failed; 2577 } 2578 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2579 goto failed; 2580 tv = STACK_TV_BOT(0); 2581 ++ectx.ec_stack.ga_len; 2582 tv->v_type = VAR_STRING; 2583 tv->v_lock = 0; 2584 tv->vval.v_string = vim_strsave( 2585 (char_u *)current_exception->value); 2586 break; 2587 2588 case ISN_CATCH: 2589 { 2590 garray_T *trystack = &ectx.ec_trystack; 2591 2592 if (restore_cmdmod) 2593 { 2594 cmdmod.cmod_filter_regmatch.regprog = NULL; 2595 undo_cmdmod(&cmdmod); 2596 cmdmod = save_cmdmod; 2597 restore_cmdmod = FALSE; 2598 } 2599 if (trystack->ga_len > 0) 2600 { 2601 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) 2602 + trystack->ga_len - 1; 2603 trycmd->tcd_caught = TRUE; 2604 } 2605 did_emsg = got_int = did_throw = FALSE; 2606 force_abort = need_rethrow = FALSE; 2607 catch_exception(current_exception); 2608 } 2609 break; 2610 2611 // end of ":try" block 2612 case ISN_ENDTRY: 2613 { 2614 garray_T *trystack = &ectx.ec_trystack; 2615 2616 if (trystack->ga_len > 0) 2617 { 2618 trycmd_T *trycmd; 2619 2620 --trystack->ga_len; 2621 --trylevel; 2622 ectx.ec_in_catch = FALSE; 2623 trycmd = ((trycmd_T *)trystack->ga_data) 2624 + trystack->ga_len; 2625 if (trycmd->tcd_caught && current_exception != NULL) 2626 { 2627 // discard the exception 2628 if (caught_stack == current_exception) 2629 caught_stack = caught_stack->caught; 2630 discard_current_exception(); 2631 } 2632 2633 if (trycmd->tcd_return) 2634 goto func_return; 2635 } 2636 } 2637 break; 2638 2639 case ISN_THROW: 2640 { 2641 garray_T *trystack = &ectx.ec_trystack; 2642 2643 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent) 2644 { 2645 // throwing an exception while using "silent!" causes 2646 // the function to abort but not display an error. 2647 tv = STACK_TV_BOT(-1); 2648 clear_tv(tv); 2649 tv->v_type = VAR_NUMBER; 2650 tv->vval.v_number = 0; 2651 goto done; 2652 } 2653 --ectx.ec_stack.ga_len; 2654 tv = STACK_TV_BOT(0); 2655 if (tv->vval.v_string == NULL 2656 || *skipwhite(tv->vval.v_string) == NUL) 2657 { 2658 vim_free(tv->vval.v_string); 2659 SOURCING_LNUM = iptr->isn_lnum; 2660 emsg(_(e_throw_with_empty_string)); 2661 goto failed; 2662 } 2663 2664 // Inside a "catch" we need to first discard the caught 2665 // exception. 2666 if (trystack->ga_len > 0) 2667 { 2668 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) 2669 + trystack->ga_len - 1; 2670 if (trycmd->tcd_caught && current_exception != NULL) 2671 { 2672 // discard the exception 2673 if (caught_stack == current_exception) 2674 caught_stack = caught_stack->caught; 2675 discard_current_exception(); 2676 trycmd->tcd_caught = FALSE; 2677 } 2678 } 2679 2680 if (throw_exception(tv->vval.v_string, ET_USER, NULL) 2681 == FAIL) 2682 { 2683 vim_free(tv->vval.v_string); 2684 goto failed; 2685 } 2686 did_throw = TRUE; 2687 } 2688 break; 2689 2690 // compare with special values 2691 case ISN_COMPAREBOOL: 2692 case ISN_COMPARESPECIAL: 2693 { 2694 typval_T *tv1 = STACK_TV_BOT(-2); 2695 typval_T *tv2 = STACK_TV_BOT(-1); 2696 varnumber_T arg1 = tv1->vval.v_number; 2697 varnumber_T arg2 = tv2->vval.v_number; 2698 int res; 2699 2700 switch (iptr->isn_arg.op.op_type) 2701 { 2702 case EXPR_EQUAL: res = arg1 == arg2; break; 2703 case EXPR_NEQUAL: res = arg1 != arg2; break; 2704 default: res = 0; break; 2705 } 2706 2707 --ectx.ec_stack.ga_len; 2708 tv1->v_type = VAR_BOOL; 2709 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; 2710 } 2711 break; 2712 2713 // Operation with two number arguments 2714 case ISN_OPNR: 2715 case ISN_COMPARENR: 2716 { 2717 typval_T *tv1 = STACK_TV_BOT(-2); 2718 typval_T *tv2 = STACK_TV_BOT(-1); 2719 varnumber_T arg1 = tv1->vval.v_number; 2720 varnumber_T arg2 = tv2->vval.v_number; 2721 varnumber_T res; 2722 2723 switch (iptr->isn_arg.op.op_type) 2724 { 2725 case EXPR_MULT: res = arg1 * arg2; break; 2726 case EXPR_DIV: res = arg1 / arg2; break; 2727 case EXPR_REM: res = arg1 % arg2; break; 2728 case EXPR_SUB: res = arg1 - arg2; break; 2729 case EXPR_ADD: res = arg1 + arg2; break; 2730 2731 case EXPR_EQUAL: res = arg1 == arg2; break; 2732 case EXPR_NEQUAL: res = arg1 != arg2; break; 2733 case EXPR_GREATER: res = arg1 > arg2; break; 2734 case EXPR_GEQUAL: res = arg1 >= arg2; break; 2735 case EXPR_SMALLER: res = arg1 < arg2; break; 2736 case EXPR_SEQUAL: res = arg1 <= arg2; break; 2737 default: res = 0; break; 2738 } 2739 2740 --ectx.ec_stack.ga_len; 2741 if (iptr->isn_type == ISN_COMPARENR) 2742 { 2743 tv1->v_type = VAR_BOOL; 2744 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; 2745 } 2746 else 2747 tv1->vval.v_number = res; 2748 } 2749 break; 2750 2751 // Computation with two float arguments 2752 case ISN_OPFLOAT: 2753 case ISN_COMPAREFLOAT: 2754 #ifdef FEAT_FLOAT 2755 { 2756 typval_T *tv1 = STACK_TV_BOT(-2); 2757 typval_T *tv2 = STACK_TV_BOT(-1); 2758 float_T arg1 = tv1->vval.v_float; 2759 float_T arg2 = tv2->vval.v_float; 2760 float_T res = 0; 2761 int cmp = FALSE; 2762 2763 switch (iptr->isn_arg.op.op_type) 2764 { 2765 case EXPR_MULT: res = arg1 * arg2; break; 2766 case EXPR_DIV: res = arg1 / arg2; break; 2767 case EXPR_SUB: res = arg1 - arg2; break; 2768 case EXPR_ADD: res = arg1 + arg2; break; 2769 2770 case EXPR_EQUAL: cmp = arg1 == arg2; break; 2771 case EXPR_NEQUAL: cmp = arg1 != arg2; break; 2772 case EXPR_GREATER: cmp = arg1 > arg2; break; 2773 case EXPR_GEQUAL: cmp = arg1 >= arg2; break; 2774 case EXPR_SMALLER: cmp = arg1 < arg2; break; 2775 case EXPR_SEQUAL: cmp = arg1 <= arg2; break; 2776 default: cmp = 0; break; 2777 } 2778 --ectx.ec_stack.ga_len; 2779 if (iptr->isn_type == ISN_COMPAREFLOAT) 2780 { 2781 tv1->v_type = VAR_BOOL; 2782 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 2783 } 2784 else 2785 tv1->vval.v_float = res; 2786 } 2787 #endif 2788 break; 2789 2790 case ISN_COMPARELIST: 2791 { 2792 typval_T *tv1 = STACK_TV_BOT(-2); 2793 typval_T *tv2 = STACK_TV_BOT(-1); 2794 list_T *arg1 = tv1->vval.v_list; 2795 list_T *arg2 = tv2->vval.v_list; 2796 int cmp = FALSE; 2797 int ic = iptr->isn_arg.op.op_ic; 2798 2799 switch (iptr->isn_arg.op.op_type) 2800 { 2801 case EXPR_EQUAL: cmp = 2802 list_equal(arg1, arg2, ic, FALSE); break; 2803 case EXPR_NEQUAL: cmp = 2804 !list_equal(arg1, arg2, ic, FALSE); break; 2805 case EXPR_IS: cmp = arg1 == arg2; break; 2806 case EXPR_ISNOT: cmp = arg1 != arg2; break; 2807 default: cmp = 0; break; 2808 } 2809 --ectx.ec_stack.ga_len; 2810 clear_tv(tv1); 2811 clear_tv(tv2); 2812 tv1->v_type = VAR_BOOL; 2813 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 2814 } 2815 break; 2816 2817 case ISN_COMPAREBLOB: 2818 { 2819 typval_T *tv1 = STACK_TV_BOT(-2); 2820 typval_T *tv2 = STACK_TV_BOT(-1); 2821 blob_T *arg1 = tv1->vval.v_blob; 2822 blob_T *arg2 = tv2->vval.v_blob; 2823 int cmp = FALSE; 2824 2825 switch (iptr->isn_arg.op.op_type) 2826 { 2827 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break; 2828 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break; 2829 case EXPR_IS: cmp = arg1 == arg2; break; 2830 case EXPR_ISNOT: cmp = arg1 != arg2; break; 2831 default: cmp = 0; break; 2832 } 2833 --ectx.ec_stack.ga_len; 2834 clear_tv(tv1); 2835 clear_tv(tv2); 2836 tv1->v_type = VAR_BOOL; 2837 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 2838 } 2839 break; 2840 2841 // TODO: handle separately 2842 case ISN_COMPARESTRING: 2843 case ISN_COMPAREDICT: 2844 case ISN_COMPAREFUNC: 2845 case ISN_COMPAREANY: 2846 { 2847 typval_T *tv1 = STACK_TV_BOT(-2); 2848 typval_T *tv2 = STACK_TV_BOT(-1); 2849 exprtype_T exprtype = iptr->isn_arg.op.op_type; 2850 int ic = iptr->isn_arg.op.op_ic; 2851 2852 SOURCING_LNUM = iptr->isn_lnum; 2853 typval_compare(tv1, tv2, exprtype, ic); 2854 clear_tv(tv2); 2855 --ectx.ec_stack.ga_len; 2856 } 2857 break; 2858 2859 case ISN_ADDLIST: 2860 case ISN_ADDBLOB: 2861 { 2862 typval_T *tv1 = STACK_TV_BOT(-2); 2863 typval_T *tv2 = STACK_TV_BOT(-1); 2864 2865 // add two lists or blobs 2866 if (iptr->isn_type == ISN_ADDLIST) 2867 eval_addlist(tv1, tv2); 2868 else 2869 eval_addblob(tv1, tv2); 2870 clear_tv(tv2); 2871 --ectx.ec_stack.ga_len; 2872 } 2873 break; 2874 2875 case ISN_LISTAPPEND: 2876 { 2877 typval_T *tv1 = STACK_TV_BOT(-2); 2878 typval_T *tv2 = STACK_TV_BOT(-1); 2879 list_T *l = tv1->vval.v_list; 2880 2881 // add an item to a list 2882 if (l == NULL) 2883 { 2884 SOURCING_LNUM = iptr->isn_lnum; 2885 emsg(_(e_cannot_add_to_null_list)); 2886 goto on_error; 2887 } 2888 if (list_append_tv(l, tv2) == FAIL) 2889 goto failed; 2890 clear_tv(tv2); 2891 --ectx.ec_stack.ga_len; 2892 } 2893 break; 2894 2895 case ISN_BLOBAPPEND: 2896 { 2897 typval_T *tv1 = STACK_TV_BOT(-2); 2898 typval_T *tv2 = STACK_TV_BOT(-1); 2899 blob_T *b = tv1->vval.v_blob; 2900 int error = FALSE; 2901 varnumber_T n; 2902 2903 // add a number to a blob 2904 if (b == NULL) 2905 { 2906 SOURCING_LNUM = iptr->isn_lnum; 2907 emsg(_(e_cannot_add_to_null_blob)); 2908 goto on_error; 2909 } 2910 n = tv_get_number_chk(tv2, &error); 2911 if (error) 2912 goto on_error; 2913 ga_append(&b->bv_ga, (int)n); 2914 --ectx.ec_stack.ga_len; 2915 } 2916 break; 2917 2918 // Computation with two arguments of unknown type 2919 case ISN_OPANY: 2920 { 2921 typval_T *tv1 = STACK_TV_BOT(-2); 2922 typval_T *tv2 = STACK_TV_BOT(-1); 2923 varnumber_T n1, n2; 2924 #ifdef FEAT_FLOAT 2925 float_T f1 = 0, f2 = 0; 2926 #endif 2927 int error = FALSE; 2928 2929 if (iptr->isn_arg.op.op_type == EXPR_ADD) 2930 { 2931 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST) 2932 { 2933 eval_addlist(tv1, tv2); 2934 clear_tv(tv2); 2935 --ectx.ec_stack.ga_len; 2936 break; 2937 } 2938 else if (tv1->v_type == VAR_BLOB 2939 && tv2->v_type == VAR_BLOB) 2940 { 2941 eval_addblob(tv1, tv2); 2942 clear_tv(tv2); 2943 --ectx.ec_stack.ga_len; 2944 break; 2945 } 2946 } 2947 #ifdef FEAT_FLOAT 2948 if (tv1->v_type == VAR_FLOAT) 2949 { 2950 f1 = tv1->vval.v_float; 2951 n1 = 0; 2952 } 2953 else 2954 #endif 2955 { 2956 SOURCING_LNUM = iptr->isn_lnum; 2957 n1 = tv_get_number_chk(tv1, &error); 2958 if (error) 2959 goto on_error; 2960 #ifdef FEAT_FLOAT 2961 if (tv2->v_type == VAR_FLOAT) 2962 f1 = n1; 2963 #endif 2964 } 2965 #ifdef FEAT_FLOAT 2966 if (tv2->v_type == VAR_FLOAT) 2967 { 2968 f2 = tv2->vval.v_float; 2969 n2 = 0; 2970 } 2971 else 2972 #endif 2973 { 2974 n2 = tv_get_number_chk(tv2, &error); 2975 if (error) 2976 goto on_error; 2977 #ifdef FEAT_FLOAT 2978 if (tv1->v_type == VAR_FLOAT) 2979 f2 = n2; 2980 #endif 2981 } 2982 #ifdef FEAT_FLOAT 2983 // if there is a float on either side the result is a float 2984 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT) 2985 { 2986 switch (iptr->isn_arg.op.op_type) 2987 { 2988 case EXPR_MULT: f1 = f1 * f2; break; 2989 case EXPR_DIV: f1 = f1 / f2; break; 2990 case EXPR_SUB: f1 = f1 - f2; break; 2991 case EXPR_ADD: f1 = f1 + f2; break; 2992 default: SOURCING_LNUM = iptr->isn_lnum; 2993 emsg(_(e_modulus)); 2994 goto on_error; 2995 } 2996 clear_tv(tv1); 2997 clear_tv(tv2); 2998 tv1->v_type = VAR_FLOAT; 2999 tv1->vval.v_float = f1; 3000 --ectx.ec_stack.ga_len; 3001 } 3002 else 3003 #endif 3004 { 3005 int failed = FALSE; 3006 3007 switch (iptr->isn_arg.op.op_type) 3008 { 3009 case EXPR_MULT: n1 = n1 * n2; break; 3010 case EXPR_DIV: n1 = num_divide(n1, n2, &failed); 3011 if (failed) 3012 goto on_error; 3013 break; 3014 case EXPR_SUB: n1 = n1 - n2; break; 3015 case EXPR_ADD: n1 = n1 + n2; break; 3016 default: n1 = num_modulus(n1, n2, &failed); 3017 if (failed) 3018 goto on_error; 3019 break; 3020 } 3021 clear_tv(tv1); 3022 clear_tv(tv2); 3023 tv1->v_type = VAR_NUMBER; 3024 tv1->vval.v_number = n1; 3025 --ectx.ec_stack.ga_len; 3026 } 3027 } 3028 break; 3029 3030 case ISN_CONCAT: 3031 { 3032 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string; 3033 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string; 3034 char_u *res; 3035 3036 res = concat_str(str1, str2); 3037 clear_tv(STACK_TV_BOT(-2)); 3038 clear_tv(STACK_TV_BOT(-1)); 3039 --ectx.ec_stack.ga_len; 3040 STACK_TV_BOT(-1)->vval.v_string = res; 3041 } 3042 break; 3043 3044 case ISN_STRINDEX: 3045 case ISN_STRSLICE: 3046 { 3047 int is_slice = iptr->isn_type == ISN_STRSLICE; 3048 varnumber_T n1 = 0, n2; 3049 char_u *res; 3050 3051 // string index: string is at stack-2, index at stack-1 3052 // string slice: string is at stack-3, first index at 3053 // stack-2, second index at stack-1 3054 if (is_slice) 3055 { 3056 tv = STACK_TV_BOT(-2); 3057 n1 = tv->vval.v_number; 3058 } 3059 3060 tv = STACK_TV_BOT(-1); 3061 n2 = tv->vval.v_number; 3062 3063 ectx.ec_stack.ga_len -= is_slice ? 2 : 1; 3064 tv = STACK_TV_BOT(-1); 3065 if (is_slice) 3066 // Slice: Select the characters from the string 3067 res = string_slice(tv->vval.v_string, n1, n2, FALSE); 3068 else 3069 // Index: The resulting variable is a string of a 3070 // single character. If the index is too big or 3071 // negative the result is empty. 3072 res = char_from_string(tv->vval.v_string, n2); 3073 vim_free(tv->vval.v_string); 3074 tv->vval.v_string = res; 3075 } 3076 break; 3077 3078 case ISN_LISTINDEX: 3079 case ISN_LISTSLICE: 3080 { 3081 int is_slice = iptr->isn_type == ISN_LISTSLICE; 3082 list_T *list; 3083 varnumber_T n1, n2; 3084 3085 // list index: list is at stack-2, index at stack-1 3086 // list slice: list is at stack-3, indexes at stack-2 and 3087 // stack-1 3088 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2); 3089 list = tv->vval.v_list; 3090 3091 tv = STACK_TV_BOT(-1); 3092 n1 = n2 = tv->vval.v_number; 3093 clear_tv(tv); 3094 3095 if (is_slice) 3096 { 3097 tv = STACK_TV_BOT(-2); 3098 n1 = tv->vval.v_number; 3099 clear_tv(tv); 3100 } 3101 3102 ectx.ec_stack.ga_len -= is_slice ? 2 : 1; 3103 tv = STACK_TV_BOT(-1); 3104 SOURCING_LNUM = iptr->isn_lnum; 3105 if (list_slice_or_index(list, is_slice, n1, n2, FALSE, 3106 tv, TRUE) == FAIL) 3107 goto on_error; 3108 } 3109 break; 3110 3111 case ISN_ANYINDEX: 3112 case ISN_ANYSLICE: 3113 { 3114 int is_slice = iptr->isn_type == ISN_ANYSLICE; 3115 typval_T *var1, *var2; 3116 int res; 3117 3118 // index: composite is at stack-2, index at stack-1 3119 // slice: composite is at stack-3, indexes at stack-2 and 3120 // stack-1 3121 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2); 3122 SOURCING_LNUM = iptr->isn_lnum; 3123 if (check_can_index(tv, TRUE, TRUE) == FAIL) 3124 goto on_error; 3125 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1); 3126 var2 = is_slice ? STACK_TV_BOT(-1) : NULL; 3127 res = eval_index_inner(tv, is_slice, var1, var2, 3128 FALSE, NULL, -1, TRUE); 3129 clear_tv(var1); 3130 if (is_slice) 3131 clear_tv(var2); 3132 ectx.ec_stack.ga_len -= is_slice ? 2 : 1; 3133 if (res == FAIL) 3134 goto on_error; 3135 } 3136 break; 3137 3138 case ISN_SLICE: 3139 { 3140 list_T *list; 3141 int count = iptr->isn_arg.number; 3142 3143 // type will have been checked to be a list 3144 tv = STACK_TV_BOT(-1); 3145 list = tv->vval.v_list; 3146 3147 // no error for short list, expect it to be checked earlier 3148 if (list != NULL && list->lv_len >= count) 3149 { 3150 list_T *newlist = list_slice(list, 3151 count, list->lv_len - 1); 3152 3153 if (newlist != NULL) 3154 { 3155 list_unref(list); 3156 tv->vval.v_list = newlist; 3157 ++newlist->lv_refcount; 3158 } 3159 } 3160 } 3161 break; 3162 3163 case ISN_GETITEM: 3164 { 3165 listitem_T *li; 3166 int index = iptr->isn_arg.number; 3167 3168 // Get list item: list is at stack-1, push item. 3169 // List type and length is checked for when compiling. 3170 tv = STACK_TV_BOT(-1); 3171 li = list_find(tv->vval.v_list, index); 3172 3173 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 3174 goto failed; 3175 ++ectx.ec_stack.ga_len; 3176 copy_tv(&li->li_tv, STACK_TV_BOT(-1)); 3177 3178 // Useful when used in unpack assignment. Reset at 3179 // ISN_DROP. 3180 where.wt_index = index + 1; 3181 where.wt_variable = TRUE; 3182 } 3183 break; 3184 3185 case ISN_MEMBER: 3186 { 3187 dict_T *dict; 3188 char_u *key; 3189 dictitem_T *di; 3190 typval_T temp_tv; 3191 3192 // dict member: dict is at stack-2, key at stack-1 3193 tv = STACK_TV_BOT(-2); 3194 // no need to check for VAR_DICT, CHECKTYPE will check. 3195 dict = tv->vval.v_dict; 3196 3197 tv = STACK_TV_BOT(-1); 3198 // no need to check for VAR_STRING, 2STRING will check. 3199 key = tv->vval.v_string; 3200 if (key == NULL) 3201 key = (char_u *)""; 3202 3203 if ((di = dict_find(dict, key, -1)) == NULL) 3204 { 3205 SOURCING_LNUM = iptr->isn_lnum; 3206 semsg(_(e_dictkey), key); 3207 3208 // If :silent! is used we will continue, make sure the 3209 // stack contents makes sense. 3210 clear_tv(tv); 3211 --ectx.ec_stack.ga_len; 3212 tv = STACK_TV_BOT(-1); 3213 clear_tv(tv); 3214 tv->v_type = VAR_NUMBER; 3215 tv->vval.v_number = 0; 3216 goto on_fatal_error; 3217 } 3218 clear_tv(tv); 3219 --ectx.ec_stack.ga_len; 3220 // Clear the dict only after getting the item, to avoid 3221 // that it makes the item invalid. 3222 tv = STACK_TV_BOT(-1); 3223 temp_tv = *tv; 3224 copy_tv(&di->di_tv, tv); 3225 clear_tv(&temp_tv); 3226 } 3227 break; 3228 3229 // dict member with string key 3230 case ISN_STRINGMEMBER: 3231 { 3232 dict_T *dict; 3233 dictitem_T *di; 3234 typval_T temp_tv; 3235 3236 tv = STACK_TV_BOT(-1); 3237 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL) 3238 { 3239 SOURCING_LNUM = iptr->isn_lnum; 3240 emsg(_(e_dictreq)); 3241 goto on_error; 3242 } 3243 dict = tv->vval.v_dict; 3244 3245 if ((di = dict_find(dict, iptr->isn_arg.string, -1)) 3246 == NULL) 3247 { 3248 SOURCING_LNUM = iptr->isn_lnum; 3249 semsg(_(e_dictkey), iptr->isn_arg.string); 3250 goto on_error; 3251 } 3252 // Clear the dict after getting the item, to avoid that it 3253 // make the item invalid. 3254 temp_tv = *tv; 3255 copy_tv(&di->di_tv, tv); 3256 clear_tv(&temp_tv); 3257 } 3258 break; 3259 3260 case ISN_NEGATENR: 3261 tv = STACK_TV_BOT(-1); 3262 if (tv->v_type != VAR_NUMBER 3263 #ifdef FEAT_FLOAT 3264 && tv->v_type != VAR_FLOAT 3265 #endif 3266 ) 3267 { 3268 SOURCING_LNUM = iptr->isn_lnum; 3269 emsg(_(e_number_exp)); 3270 goto on_error; 3271 } 3272 #ifdef FEAT_FLOAT 3273 if (tv->v_type == VAR_FLOAT) 3274 tv->vval.v_float = -tv->vval.v_float; 3275 else 3276 #endif 3277 tv->vval.v_number = -tv->vval.v_number; 3278 break; 3279 3280 case ISN_CHECKNR: 3281 { 3282 int error = FALSE; 3283 3284 tv = STACK_TV_BOT(-1); 3285 SOURCING_LNUM = iptr->isn_lnum; 3286 if (check_not_string(tv) == FAIL) 3287 goto on_error; 3288 (void)tv_get_number_chk(tv, &error); 3289 if (error) 3290 goto on_error; 3291 } 3292 break; 3293 3294 case ISN_CHECKTYPE: 3295 { 3296 checktype_T *ct = &iptr->isn_arg.type; 3297 3298 tv = STACK_TV_BOT((int)ct->ct_off); 3299 SOURCING_LNUM = iptr->isn_lnum; 3300 if (!where.wt_variable) 3301 where.wt_index = ct->ct_arg_idx; 3302 if (check_typval_type(ct->ct_type, tv, where) == FAIL) 3303 goto on_error; 3304 if (!where.wt_variable) 3305 where.wt_index = 0; 3306 3307 // number 0 is FALSE, number 1 is TRUE 3308 if (tv->v_type == VAR_NUMBER 3309 && ct->ct_type->tt_type == VAR_BOOL 3310 && (tv->vval.v_number == 0 3311 || tv->vval.v_number == 1)) 3312 { 3313 tv->v_type = VAR_BOOL; 3314 tv->vval.v_number = tv->vval.v_number 3315 ? VVAL_TRUE : VVAL_FALSE; 3316 } 3317 } 3318 break; 3319 3320 case ISN_CHECKLEN: 3321 { 3322 int min_len = iptr->isn_arg.checklen.cl_min_len; 3323 list_T *list = NULL; 3324 3325 tv = STACK_TV_BOT(-1); 3326 if (tv->v_type == VAR_LIST) 3327 list = tv->vval.v_list; 3328 if (list == NULL || list->lv_len < min_len 3329 || (list->lv_len > min_len 3330 && !iptr->isn_arg.checklen.cl_more_OK)) 3331 { 3332 SOURCING_LNUM = iptr->isn_lnum; 3333 semsg(_(e_expected_nr_items_but_got_nr), 3334 min_len, list == NULL ? 0 : list->lv_len); 3335 goto on_error; 3336 } 3337 } 3338 break; 3339 3340 case ISN_SETTYPE: 3341 { 3342 checktype_T *ct = &iptr->isn_arg.type; 3343 3344 tv = STACK_TV_BOT(-1); 3345 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) 3346 { 3347 free_type(tv->vval.v_dict->dv_type); 3348 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type); 3349 } 3350 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL) 3351 { 3352 free_type(tv->vval.v_list->lv_type); 3353 tv->vval.v_list->lv_type = alloc_type(ct->ct_type); 3354 } 3355 } 3356 break; 3357 3358 case ISN_2BOOL: 3359 case ISN_COND2BOOL: 3360 { 3361 int n; 3362 int error = FALSE; 3363 3364 tv = STACK_TV_BOT(-1); 3365 if (iptr->isn_type == ISN_2BOOL) 3366 { 3367 n = tv2bool(tv); 3368 if (iptr->isn_arg.number) // invert 3369 n = !n; 3370 } 3371 else 3372 { 3373 SOURCING_LNUM = iptr->isn_lnum; 3374 n = tv_get_bool_chk(tv, &error); 3375 if (error) 3376 goto on_error; 3377 } 3378 clear_tv(tv); 3379 tv->v_type = VAR_BOOL; 3380 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE; 3381 } 3382 break; 3383 3384 case ISN_2STRING: 3385 case ISN_2STRING_ANY: 3386 SOURCING_LNUM = iptr->isn_lnum; 3387 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number), 3388 iptr->isn_type == ISN_2STRING_ANY) == FAIL) 3389 goto on_error; 3390 break; 3391 3392 case ISN_RANGE: 3393 { 3394 exarg_T ea; 3395 char *errormsg; 3396 3397 ea.line2 = 0; 3398 ea.addr_count = 0; 3399 ea.addr_type = ADDR_LINES; 3400 ea.cmd = iptr->isn_arg.string; 3401 ea.skip = FALSE; 3402 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL) 3403 goto on_error; 3404 3405 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 3406 goto failed; 3407 ++ectx.ec_stack.ga_len; 3408 tv = STACK_TV_BOT(-1); 3409 tv->v_type = VAR_NUMBER; 3410 tv->v_lock = 0; 3411 if (ea.addr_count == 0) 3412 tv->vval.v_number = curwin->w_cursor.lnum; 3413 else 3414 tv->vval.v_number = ea.line2; 3415 } 3416 break; 3417 3418 case ISN_PUT: 3419 { 3420 int regname = iptr->isn_arg.put.put_regname; 3421 linenr_T lnum = iptr->isn_arg.put.put_lnum; 3422 char_u *expr = NULL; 3423 int dir = FORWARD; 3424 3425 if (lnum < -2) 3426 { 3427 // line number was put on the stack by ISN_RANGE 3428 tv = STACK_TV_BOT(-1); 3429 curwin->w_cursor.lnum = tv->vval.v_number; 3430 if (lnum == LNUM_VARIABLE_RANGE_ABOVE) 3431 dir = BACKWARD; 3432 --ectx.ec_stack.ga_len; 3433 } 3434 else if (lnum == -2) 3435 // :put! above cursor 3436 dir = BACKWARD; 3437 else if (lnum >= 0) 3438 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum; 3439 3440 if (regname == '=') 3441 { 3442 tv = STACK_TV_BOT(-1); 3443 if (tv->v_type == VAR_STRING) 3444 expr = tv->vval.v_string; 3445 else 3446 { 3447 expr = typval2string(tv, TRUE); // allocates value 3448 clear_tv(tv); 3449 } 3450 --ectx.ec_stack.ga_len; 3451 } 3452 check_cursor(); 3453 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE); 3454 vim_free(expr); 3455 } 3456 break; 3457 3458 case ISN_CMDMOD: 3459 save_cmdmod = cmdmod; 3460 restore_cmdmod = TRUE; 3461 restore_cmdmod_stacklen = ectx.ec_stack.ga_len; 3462 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod; 3463 apply_cmdmod(&cmdmod); 3464 break; 3465 3466 case ISN_CMDMOD_REV: 3467 // filter regprog is owned by the instruction, don't free it 3468 cmdmod.cmod_filter_regmatch.regprog = NULL; 3469 undo_cmdmod(&cmdmod); 3470 cmdmod = save_cmdmod; 3471 restore_cmdmod = FALSE; 3472 break; 3473 3474 case ISN_UNPACK: 3475 { 3476 int count = iptr->isn_arg.unpack.unp_count; 3477 int semicolon = iptr->isn_arg.unpack.unp_semicolon; 3478 list_T *l; 3479 listitem_T *li; 3480 int i; 3481 3482 // Check there is a valid list to unpack. 3483 tv = STACK_TV_BOT(-1); 3484 if (tv->v_type != VAR_LIST) 3485 { 3486 SOURCING_LNUM = iptr->isn_lnum; 3487 emsg(_(e_for_argument_must_be_sequence_of_lists)); 3488 goto on_error; 3489 } 3490 l = tv->vval.v_list; 3491 if (l == NULL 3492 || l->lv_len < (semicolon ? count - 1 : count)) 3493 { 3494 SOURCING_LNUM = iptr->isn_lnum; 3495 emsg(_(e_list_value_does_not_have_enough_items)); 3496 goto on_error; 3497 } 3498 else if (!semicolon && l->lv_len > count) 3499 { 3500 SOURCING_LNUM = iptr->isn_lnum; 3501 emsg(_(e_list_value_has_more_items_than_targets)); 3502 goto on_error; 3503 } 3504 3505 CHECK_LIST_MATERIALIZE(l); 3506 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL) 3507 goto failed; 3508 ectx.ec_stack.ga_len += count - 1; 3509 3510 // Variable after semicolon gets a list with the remaining 3511 // items. 3512 if (semicolon) 3513 { 3514 list_T *rem_list = 3515 list_alloc_with_items(l->lv_len - count + 1); 3516 3517 if (rem_list == NULL) 3518 goto failed; 3519 tv = STACK_TV_BOT(-count); 3520 tv->vval.v_list = rem_list; 3521 ++rem_list->lv_refcount; 3522 tv->v_lock = 0; 3523 li = l->lv_first; 3524 for (i = 0; i < count - 1; ++i) 3525 li = li->li_next; 3526 for (i = 0; li != NULL; ++i) 3527 { 3528 list_set_item(rem_list, i, &li->li_tv); 3529 li = li->li_next; 3530 } 3531 --count; 3532 } 3533 3534 // Produce the values in reverse order, first item last. 3535 li = l->lv_first; 3536 for (i = 0; i < count; ++i) 3537 { 3538 tv = STACK_TV_BOT(-i - 1); 3539 copy_tv(&li->li_tv, tv); 3540 li = li->li_next; 3541 } 3542 3543 list_unref(l); 3544 } 3545 break; 3546 3547 case ISN_PROF_START: 3548 case ISN_PROF_END: 3549 { 3550 #ifdef FEAT_PROFILE 3551 funccall_T cookie; 3552 ufunc_T *cur_ufunc = 3553 (((dfunc_T *)def_functions.ga_data) 3554 + ectx.ec_dfunc_idx)->df_ufunc; 3555 3556 cookie.func = cur_ufunc; 3557 if (iptr->isn_type == ISN_PROF_START) 3558 { 3559 func_line_start(&cookie, iptr->isn_lnum); 3560 // if we get here the instruction is executed 3561 func_line_exec(&cookie); 3562 } 3563 else 3564 func_line_end(&cookie); 3565 #endif 3566 } 3567 break; 3568 3569 case ISN_SHUFFLE: 3570 { 3571 typval_T tmp_tv; 3572 int item = iptr->isn_arg.shuffle.shfl_item; 3573 int up = iptr->isn_arg.shuffle.shfl_up; 3574 3575 tmp_tv = *STACK_TV_BOT(-item); 3576 for ( ; up > 0 && item > 1; --up) 3577 { 3578 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1); 3579 --item; 3580 } 3581 *STACK_TV_BOT(-item) = tmp_tv; 3582 } 3583 break; 3584 3585 case ISN_DROP: 3586 --ectx.ec_stack.ga_len; 3587 clear_tv(STACK_TV_BOT(0)); 3588 where.wt_index = 0; 3589 where.wt_variable = FALSE; 3590 break; 3591 } 3592 continue; 3593 3594 func_return: 3595 // Restore previous function. If the frame pointer is where we started 3596 // then there is none and we are done. 3597 if (ectx.ec_frame_idx == initial_frame_idx) 3598 goto done; 3599 3600 if (func_return(&ectx) == FAIL) 3601 // only fails when out of memory 3602 goto failed; 3603 continue; 3604 3605 on_error: 3606 // Jump here for an error that does not require aborting execution. 3607 // If "emsg_silent" is set then ignore the error, unless it was set 3608 // when calling the function. 3609 if (did_emsg_cumul + did_emsg == did_emsg_before 3610 && emsg_silent && did_emsg_def == 0) 3611 { 3612 // If a sequence of instructions causes an error while ":silent!" 3613 // was used, restore the stack length and jump ahead to restoring 3614 // the cmdmod. 3615 if (restore_cmdmod) 3616 { 3617 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen) 3618 { 3619 --ectx.ec_stack.ga_len; 3620 clear_tv(STACK_TV_BOT(0)); 3621 } 3622 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV) 3623 ++ectx.ec_iidx; 3624 } 3625 continue; 3626 } 3627 on_fatal_error: 3628 // Jump here for an error that messes up the stack. 3629 // If we are not inside a try-catch started here, abort execution. 3630 if (trylevel <= trylevel_at_start) 3631 goto failed; 3632 } 3633 3634 done: 3635 // function finished, get result from the stack. 3636 tv = STACK_TV_BOT(-1); 3637 *rettv = *tv; 3638 tv->v_type = VAR_UNKNOWN; 3639 ret = OK; 3640 3641 failed: 3642 // When failed need to unwind the call stack. 3643 while (ectx.ec_frame_idx != initial_frame_idx) 3644 func_return(&ectx); 3645 3646 // Deal with any remaining closures, they may be in use somewhere. 3647 if (ectx.ec_funcrefs.ga_len > 0) 3648 { 3649 handle_closure_in_use(&ectx, FALSE); 3650 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed? 3651 } 3652 3653 estack_pop(); 3654 current_sctx = save_current_sctx; 3655 3656 if (*msg_list != NULL && saved_msg_list != NULL) 3657 { 3658 msglist_T **plist = saved_msg_list; 3659 3660 // Append entries from the current msg_list (uncaught exceptions) to 3661 // the saved msg_list. 3662 while (*plist != NULL) 3663 plist = &(*plist)->next; 3664 3665 *plist = *msg_list; 3666 } 3667 msg_list = saved_msg_list; 3668 3669 if (restore_cmdmod) 3670 { 3671 cmdmod.cmod_filter_regmatch.regprog = NULL; 3672 undo_cmdmod(&cmdmod); 3673 cmdmod = save_cmdmod; 3674 } 3675 emsg_silent_def = save_emsg_silent_def; 3676 did_emsg_def += save_did_emsg_def; 3677 3678 failed_early: 3679 // Free all local variables, but not arguments. 3680 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx) 3681 clear_tv(STACK_TV(idx)); 3682 3683 vim_free(ectx.ec_stack.ga_data); 3684 vim_free(ectx.ec_trystack.ga_data); 3685 3686 while (ectx.ec_outer != NULL) 3687 { 3688 outer_T *up = ectx.ec_outer->out_up_is_copy 3689 ? NULL : ectx.ec_outer->out_up; 3690 3691 vim_free(ectx.ec_outer); 3692 ectx.ec_outer = up; 3693 } 3694 3695 // Not sure if this is necessary. 3696 suppress_errthrow = save_suppress_errthrow; 3697 3698 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before) 3699 semsg(_(e_unknown_error_while_executing_str), 3700 printable_func_name(ufunc)); 3701 funcdepth_restore(orig_funcdepth); 3702 return ret; 3703 } 3704 3705 /* 3706 * ":disassemble". 3707 * We don't really need this at runtime, but we do have tests that require it, 3708 * so always include this. 3709 */ 3710 void 3711 ex_disassemble(exarg_T *eap) 3712 { 3713 char_u *arg = eap->arg; 3714 char_u *fname; 3715 ufunc_T *ufunc; 3716 dfunc_T *dfunc; 3717 isn_T *instr; 3718 int instr_count; 3719 int current; 3720 int line_idx = 0; 3721 int prev_current = 0; 3722 int is_global = FALSE; 3723 3724 if (STRNCMP(arg, "<lambda>", 8) == 0) 3725 { 3726 arg += 8; 3727 (void)getdigits(&arg); 3728 fname = vim_strnsave(eap->arg, arg - eap->arg); 3729 } 3730 else 3731 fname = trans_function_name(&arg, &is_global, FALSE, 3732 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL); 3733 if (fname == NULL) 3734 { 3735 semsg(_(e_invarg2), eap->arg); 3736 return; 3737 } 3738 3739 ufunc = find_func(fname, is_global, NULL); 3740 if (ufunc == NULL) 3741 { 3742 char_u *p = untrans_function_name(fname); 3743 3744 if (p != NULL) 3745 // Try again without making it script-local. 3746 ufunc = find_func(p, FALSE, NULL); 3747 } 3748 vim_free(fname); 3749 if (ufunc == NULL) 3750 { 3751 semsg(_(e_cannot_find_function_str), eap->arg); 3752 return; 3753 } 3754 if (func_needs_compiling(ufunc, eap->forceit) 3755 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL) 3756 return; 3757 if (ufunc->uf_def_status != UF_COMPILED) 3758 { 3759 semsg(_(e_function_is_not_compiled_str), eap->arg); 3760 return; 3761 } 3762 if (ufunc->uf_name_exp != NULL) 3763 msg((char *)ufunc->uf_name_exp); 3764 else 3765 msg((char *)ufunc->uf_name); 3766 3767 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; 3768 #ifdef FEAT_PROFILE 3769 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr; 3770 instr_count = eap->forceit ? dfunc->df_instr_prof_count 3771 : dfunc->df_instr_count; 3772 #else 3773 instr = dfunc->df_instr; 3774 instr_count = dfunc->df_instr_count; 3775 #endif 3776 for (current = 0; current < instr_count; ++current) 3777 { 3778 isn_T *iptr = &instr[current]; 3779 char *line; 3780 3781 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len) 3782 { 3783 if (current > prev_current) 3784 { 3785 msg_puts("\n\n"); 3786 prev_current = current; 3787 } 3788 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++]; 3789 if (line != NULL) 3790 msg(line); 3791 } 3792 3793 switch (iptr->isn_type) 3794 { 3795 case ISN_EXEC: 3796 smsg("%4d EXEC %s", current, iptr->isn_arg.string); 3797 break; 3798 case ISN_EXECCONCAT: 3799 smsg("%4d EXECCONCAT %lld", current, 3800 (varnumber_T)iptr->isn_arg.number); 3801 break; 3802 case ISN_ECHO: 3803 { 3804 echo_T *echo = &iptr->isn_arg.echo; 3805 3806 smsg("%4d %s %d", current, 3807 echo->echo_with_white ? "ECHO" : "ECHON", 3808 echo->echo_count); 3809 } 3810 break; 3811 case ISN_EXECUTE: 3812 smsg("%4d EXECUTE %lld", current, 3813 (varnumber_T)(iptr->isn_arg.number)); 3814 break; 3815 case ISN_ECHOMSG: 3816 smsg("%4d ECHOMSG %lld", current, 3817 (varnumber_T)(iptr->isn_arg.number)); 3818 break; 3819 case ISN_ECHOERR: 3820 smsg("%4d ECHOERR %lld", current, 3821 (varnumber_T)(iptr->isn_arg.number)); 3822 break; 3823 case ISN_LOAD: 3824 { 3825 if (iptr->isn_arg.number < 0) 3826 smsg("%4d LOAD arg[%lld]", current, 3827 (varnumber_T)(iptr->isn_arg.number 3828 + STACK_FRAME_SIZE)); 3829 else 3830 smsg("%4d LOAD $%lld", current, 3831 (varnumber_T)(iptr->isn_arg.number)); 3832 } 3833 break; 3834 case ISN_LOADOUTER: 3835 { 3836 if (iptr->isn_arg.number < 0) 3837 smsg("%4d LOADOUTER level %d arg[%d]", current, 3838 iptr->isn_arg.outer.outer_depth, 3839 iptr->isn_arg.outer.outer_idx 3840 + STACK_FRAME_SIZE); 3841 else 3842 smsg("%4d LOADOUTER level %d $%d", current, 3843 iptr->isn_arg.outer.outer_depth, 3844 iptr->isn_arg.outer.outer_idx); 3845 } 3846 break; 3847 case ISN_LOADV: 3848 smsg("%4d LOADV v:%s", current, 3849 get_vim_var_name(iptr->isn_arg.number)); 3850 break; 3851 case ISN_LOADSCRIPT: 3852 { 3853 scriptref_T *sref = iptr->isn_arg.script.scriptref; 3854 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); 3855 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 3856 + sref->sref_idx; 3857 3858 smsg("%4d LOADSCRIPT %s-%d from %s", current, 3859 sv->sv_name, 3860 sref->sref_idx, 3861 si->sn_name); 3862 } 3863 break; 3864 case ISN_LOADS: 3865 { 3866 scriptitem_T *si = SCRIPT_ITEM( 3867 iptr->isn_arg.loadstore.ls_sid); 3868 3869 smsg("%4d LOADS s:%s from %s", current, 3870 iptr->isn_arg.loadstore.ls_name, si->sn_name); 3871 } 3872 break; 3873 case ISN_LOADAUTO: 3874 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string); 3875 break; 3876 case ISN_LOADG: 3877 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string); 3878 break; 3879 case ISN_LOADB: 3880 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string); 3881 break; 3882 case ISN_LOADW: 3883 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string); 3884 break; 3885 case ISN_LOADT: 3886 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string); 3887 break; 3888 case ISN_LOADGDICT: 3889 smsg("%4d LOAD g:", current); 3890 break; 3891 case ISN_LOADBDICT: 3892 smsg("%4d LOAD b:", current); 3893 break; 3894 case ISN_LOADWDICT: 3895 smsg("%4d LOAD w:", current); 3896 break; 3897 case ISN_LOADTDICT: 3898 smsg("%4d LOAD t:", current); 3899 break; 3900 case ISN_LOADOPT: 3901 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string); 3902 break; 3903 case ISN_LOADENV: 3904 smsg("%4d LOADENV %s", current, iptr->isn_arg.string); 3905 break; 3906 case ISN_LOADREG: 3907 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number)); 3908 break; 3909 3910 case ISN_STORE: 3911 if (iptr->isn_arg.number < 0) 3912 smsg("%4d STORE arg[%lld]", current, 3913 iptr->isn_arg.number + STACK_FRAME_SIZE); 3914 else 3915 smsg("%4d STORE $%lld", current, iptr->isn_arg.number); 3916 break; 3917 case ISN_STOREOUTER: 3918 { 3919 if (iptr->isn_arg.number < 0) 3920 smsg("%4d STOREOUTEr level %d arg[%d]", current, 3921 iptr->isn_arg.outer.outer_depth, 3922 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE); 3923 else 3924 smsg("%4d STOREOUTER level %d $%d", current, 3925 iptr->isn_arg.outer.outer_depth, 3926 iptr->isn_arg.outer.outer_idx); 3927 } 3928 break; 3929 case ISN_STOREV: 3930 smsg("%4d STOREV v:%s", current, 3931 get_vim_var_name(iptr->isn_arg.number)); 3932 break; 3933 case ISN_STOREAUTO: 3934 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string); 3935 break; 3936 case ISN_STOREG: 3937 smsg("%4d STOREG %s", current, iptr->isn_arg.string); 3938 break; 3939 case ISN_STOREB: 3940 smsg("%4d STOREB %s", current, iptr->isn_arg.string); 3941 break; 3942 case ISN_STOREW: 3943 smsg("%4d STOREW %s", current, iptr->isn_arg.string); 3944 break; 3945 case ISN_STORET: 3946 smsg("%4d STORET %s", current, iptr->isn_arg.string); 3947 break; 3948 case ISN_STORES: 3949 { 3950 scriptitem_T *si = SCRIPT_ITEM( 3951 iptr->isn_arg.loadstore.ls_sid); 3952 3953 smsg("%4d STORES %s in %s", current, 3954 iptr->isn_arg.loadstore.ls_name, si->sn_name); 3955 } 3956 break; 3957 case ISN_STORESCRIPT: 3958 { 3959 scriptref_T *sref = iptr->isn_arg.script.scriptref; 3960 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); 3961 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 3962 + sref->sref_idx; 3963 3964 smsg("%4d STORESCRIPT %s-%d in %s", current, 3965 sv->sv_name, 3966 sref->sref_idx, 3967 si->sn_name); 3968 } 3969 break; 3970 case ISN_STOREOPT: 3971 smsg("%4d STOREOPT &%s", current, 3972 iptr->isn_arg.storeopt.so_name); 3973 break; 3974 case ISN_STOREENV: 3975 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string); 3976 break; 3977 case ISN_STOREREG: 3978 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number); 3979 break; 3980 case ISN_STORENR: 3981 smsg("%4d STORE %lld in $%d", current, 3982 iptr->isn_arg.storenr.stnr_val, 3983 iptr->isn_arg.storenr.stnr_idx); 3984 break; 3985 3986 case ISN_STOREINDEX: 3987 switch (iptr->isn_arg.vartype) 3988 { 3989 case VAR_LIST: 3990 smsg("%4d STORELIST", current); 3991 break; 3992 case VAR_DICT: 3993 smsg("%4d STOREDICT", current); 3994 break; 3995 case VAR_ANY: 3996 smsg("%4d STOREINDEX", current); 3997 break; 3998 default: break; 3999 } 4000 break; 4001 4002 // constants 4003 case ISN_PUSHNR: 4004 smsg("%4d PUSHNR %lld", current, 4005 (varnumber_T)(iptr->isn_arg.number)); 4006 break; 4007 case ISN_PUSHBOOL: 4008 case ISN_PUSHSPEC: 4009 smsg("%4d PUSH %s", current, 4010 get_var_special_name(iptr->isn_arg.number)); 4011 break; 4012 case ISN_PUSHF: 4013 #ifdef FEAT_FLOAT 4014 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber); 4015 #endif 4016 break; 4017 case ISN_PUSHS: 4018 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string); 4019 break; 4020 case ISN_PUSHBLOB: 4021 { 4022 char_u *r; 4023 char_u numbuf[NUMBUFLEN]; 4024 char_u *tofree; 4025 4026 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf); 4027 smsg("%4d PUSHBLOB %s", current, r); 4028 vim_free(tofree); 4029 } 4030 break; 4031 case ISN_PUSHFUNC: 4032 { 4033 char *name = (char *)iptr->isn_arg.string; 4034 4035 smsg("%4d PUSHFUNC \"%s\"", current, 4036 name == NULL ? "[none]" : name); 4037 } 4038 break; 4039 case ISN_PUSHCHANNEL: 4040 #ifdef FEAT_JOB_CHANNEL 4041 { 4042 channel_T *channel = iptr->isn_arg.channel; 4043 4044 smsg("%4d PUSHCHANNEL %d", current, 4045 channel == NULL ? 0 : channel->ch_id); 4046 } 4047 #endif 4048 break; 4049 case ISN_PUSHJOB: 4050 #ifdef FEAT_JOB_CHANNEL 4051 { 4052 typval_T tv; 4053 char_u *name; 4054 4055 tv.v_type = VAR_JOB; 4056 tv.vval.v_job = iptr->isn_arg.job; 4057 name = tv_get_string(&tv); 4058 smsg("%4d PUSHJOB \"%s\"", current, name); 4059 } 4060 #endif 4061 break; 4062 case ISN_PUSHEXC: 4063 smsg("%4d PUSH v:exception", current); 4064 break; 4065 case ISN_UNLET: 4066 smsg("%4d UNLET%s %s", current, 4067 iptr->isn_arg.unlet.ul_forceit ? "!" : "", 4068 iptr->isn_arg.unlet.ul_name); 4069 break; 4070 case ISN_UNLETENV: 4071 smsg("%4d UNLETENV%s $%s", current, 4072 iptr->isn_arg.unlet.ul_forceit ? "!" : "", 4073 iptr->isn_arg.unlet.ul_name); 4074 break; 4075 case ISN_UNLETINDEX: 4076 smsg("%4d UNLETINDEX", current); 4077 break; 4078 case ISN_LOCKCONST: 4079 smsg("%4d LOCKCONST", current); 4080 break; 4081 case ISN_NEWLIST: 4082 smsg("%4d NEWLIST size %lld", current, 4083 (varnumber_T)(iptr->isn_arg.number)); 4084 break; 4085 case ISN_NEWDICT: 4086 smsg("%4d NEWDICT size %lld", current, 4087 (varnumber_T)(iptr->isn_arg.number)); 4088 break; 4089 4090 // function call 4091 case ISN_BCALL: 4092 { 4093 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc; 4094 4095 smsg("%4d BCALL %s(argc %d)", current, 4096 internal_func_name(cbfunc->cbf_idx), 4097 cbfunc->cbf_argcount); 4098 } 4099 break; 4100 case ISN_DCALL: 4101 { 4102 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc; 4103 dfunc_T *df = ((dfunc_T *)def_functions.ga_data) 4104 + cdfunc->cdf_idx; 4105 4106 smsg("%4d DCALL %s(argc %d)", current, 4107 df->df_ufunc->uf_name_exp != NULL 4108 ? df->df_ufunc->uf_name_exp 4109 : df->df_ufunc->uf_name, cdfunc->cdf_argcount); 4110 } 4111 break; 4112 case ISN_UCALL: 4113 { 4114 cufunc_T *cufunc = &iptr->isn_arg.ufunc; 4115 4116 smsg("%4d UCALL %s(argc %d)", current, 4117 cufunc->cuf_name, cufunc->cuf_argcount); 4118 } 4119 break; 4120 case ISN_PCALL: 4121 { 4122 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc; 4123 4124 smsg("%4d PCALL%s (argc %d)", current, 4125 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount); 4126 } 4127 break; 4128 case ISN_PCALL_END: 4129 smsg("%4d PCALL end", current); 4130 break; 4131 case ISN_RETURN: 4132 smsg("%4d RETURN", current); 4133 break; 4134 case ISN_RETURN_ZERO: 4135 smsg("%4d RETURN 0", current); 4136 break; 4137 case ISN_FUNCREF: 4138 { 4139 funcref_T *funcref = &iptr->isn_arg.funcref; 4140 dfunc_T *df = ((dfunc_T *)def_functions.ga_data) 4141 + funcref->fr_func; 4142 4143 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name); 4144 } 4145 break; 4146 4147 case ISN_NEWFUNC: 4148 { 4149 newfunc_T *newfunc = &iptr->isn_arg.newfunc; 4150 4151 smsg("%4d NEWFUNC %s %s", current, 4152 newfunc->nf_lambda, newfunc->nf_global); 4153 } 4154 break; 4155 4156 case ISN_DEF: 4157 { 4158 char_u *name = iptr->isn_arg.string; 4159 4160 smsg("%4d DEF %s", current, 4161 name == NULL ? (char_u *)"" : name); 4162 } 4163 break; 4164 4165 case ISN_JUMP: 4166 { 4167 char *when = "?"; 4168 4169 switch (iptr->isn_arg.jump.jump_when) 4170 { 4171 case JUMP_ALWAYS: 4172 when = "JUMP"; 4173 break; 4174 case JUMP_AND_KEEP_IF_TRUE: 4175 when = "JUMP_AND_KEEP_IF_TRUE"; 4176 break; 4177 case JUMP_IF_FALSE: 4178 when = "JUMP_IF_FALSE"; 4179 break; 4180 case JUMP_AND_KEEP_IF_FALSE: 4181 when = "JUMP_AND_KEEP_IF_FALSE"; 4182 break; 4183 case JUMP_IF_COND_FALSE: 4184 when = "JUMP_IF_COND_FALSE"; 4185 break; 4186 case JUMP_IF_COND_TRUE: 4187 when = "JUMP_IF_COND_TRUE"; 4188 break; 4189 } 4190 smsg("%4d %s -> %d", current, when, 4191 iptr->isn_arg.jump.jump_where); 4192 } 4193 break; 4194 4195 case ISN_FOR: 4196 { 4197 forloop_T *forloop = &iptr->isn_arg.forloop; 4198 4199 smsg("%4d FOR $%d -> %d", current, 4200 forloop->for_idx, forloop->for_end); 4201 } 4202 break; 4203 4204 case ISN_TRY: 4205 { 4206 try_T *try = &iptr->isn_arg.try; 4207 4208 smsg("%4d TRY catch -> %d, finally -> %d", current, 4209 try->try_catch, try->try_finally); 4210 } 4211 break; 4212 case ISN_CATCH: 4213 // TODO 4214 smsg("%4d CATCH", current); 4215 break; 4216 case ISN_ENDTRY: 4217 smsg("%4d ENDTRY", current); 4218 break; 4219 case ISN_THROW: 4220 smsg("%4d THROW", current); 4221 break; 4222 4223 // expression operations on number 4224 case ISN_OPNR: 4225 case ISN_OPFLOAT: 4226 case ISN_OPANY: 4227 { 4228 char *what; 4229 char *ins; 4230 4231 switch (iptr->isn_arg.op.op_type) 4232 { 4233 case EXPR_MULT: what = "*"; break; 4234 case EXPR_DIV: what = "/"; break; 4235 case EXPR_REM: what = "%"; break; 4236 case EXPR_SUB: what = "-"; break; 4237 case EXPR_ADD: what = "+"; break; 4238 default: what = "???"; break; 4239 } 4240 switch (iptr->isn_type) 4241 { 4242 case ISN_OPNR: ins = "OPNR"; break; 4243 case ISN_OPFLOAT: ins = "OPFLOAT"; break; 4244 case ISN_OPANY: ins = "OPANY"; break; 4245 default: ins = "???"; break; 4246 } 4247 smsg("%4d %s %s", current, ins, what); 4248 } 4249 break; 4250 4251 case ISN_COMPAREBOOL: 4252 case ISN_COMPARESPECIAL: 4253 case ISN_COMPARENR: 4254 case ISN_COMPAREFLOAT: 4255 case ISN_COMPARESTRING: 4256 case ISN_COMPAREBLOB: 4257 case ISN_COMPARELIST: 4258 case ISN_COMPAREDICT: 4259 case ISN_COMPAREFUNC: 4260 case ISN_COMPAREANY: 4261 { 4262 char *p; 4263 char buf[10]; 4264 char *type; 4265 4266 switch (iptr->isn_arg.op.op_type) 4267 { 4268 case EXPR_EQUAL: p = "=="; break; 4269 case EXPR_NEQUAL: p = "!="; break; 4270 case EXPR_GREATER: p = ">"; break; 4271 case EXPR_GEQUAL: p = ">="; break; 4272 case EXPR_SMALLER: p = "<"; break; 4273 case EXPR_SEQUAL: p = "<="; break; 4274 case EXPR_MATCH: p = "=~"; break; 4275 case EXPR_IS: p = "is"; break; 4276 case EXPR_ISNOT: p = "isnot"; break; 4277 case EXPR_NOMATCH: p = "!~"; break; 4278 default: p = "???"; break; 4279 } 4280 STRCPY(buf, p); 4281 if (iptr->isn_arg.op.op_ic == TRUE) 4282 strcat(buf, "?"); 4283 switch(iptr->isn_type) 4284 { 4285 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break; 4286 case ISN_COMPARESPECIAL: 4287 type = "COMPARESPECIAL"; break; 4288 case ISN_COMPARENR: type = "COMPARENR"; break; 4289 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break; 4290 case ISN_COMPARESTRING: 4291 type = "COMPARESTRING"; break; 4292 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break; 4293 case ISN_COMPARELIST: type = "COMPARELIST"; break; 4294 case ISN_COMPAREDICT: type = "COMPAREDICT"; break; 4295 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break; 4296 case ISN_COMPAREANY: type = "COMPAREANY"; break; 4297 default: type = "???"; break; 4298 } 4299 4300 smsg("%4d %s %s", current, type, buf); 4301 } 4302 break; 4303 4304 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break; 4305 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break; 4306 4307 // expression operations 4308 case ISN_CONCAT: smsg("%4d CONCAT", current); break; 4309 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break; 4310 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break; 4311 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break; 4312 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break; 4313 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break; 4314 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break; 4315 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break; 4316 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break; 4317 case ISN_SLICE: smsg("%4d SLICE %lld", 4318 current, iptr->isn_arg.number); break; 4319 case ISN_GETITEM: smsg("%4d ITEM %lld", 4320 current, iptr->isn_arg.number); break; 4321 case ISN_MEMBER: smsg("%4d MEMBER", current); break; 4322 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current, 4323 iptr->isn_arg.string); break; 4324 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break; 4325 4326 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break; 4327 case ISN_CHECKTYPE: 4328 { 4329 checktype_T *ct = &iptr->isn_arg.type; 4330 char *tofree; 4331 4332 if (ct->ct_arg_idx == 0) 4333 smsg("%4d CHECKTYPE %s stack[%d]", current, 4334 type_name(ct->ct_type, &tofree), 4335 (int)ct->ct_off); 4336 else 4337 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current, 4338 type_name(ct->ct_type, &tofree), 4339 (int)ct->ct_off, 4340 (int)ct->ct_arg_idx); 4341 vim_free(tofree); 4342 break; 4343 } 4344 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current, 4345 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "", 4346 iptr->isn_arg.checklen.cl_min_len); 4347 break; 4348 case ISN_SETTYPE: 4349 { 4350 char *tofree; 4351 4352 smsg("%4d SETTYPE %s", current, 4353 type_name(iptr->isn_arg.type.ct_type, &tofree)); 4354 vim_free(tofree); 4355 break; 4356 } 4357 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break; 4358 case ISN_2BOOL: if (iptr->isn_arg.number) 4359 smsg("%4d INVERT (!val)", current); 4360 else 4361 smsg("%4d 2BOOL (!!val)", current); 4362 break; 4363 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current, 4364 (varnumber_T)(iptr->isn_arg.number)); 4365 break; 4366 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current, 4367 (varnumber_T)(iptr->isn_arg.number)); 4368 break; 4369 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string); 4370 break; 4371 case ISN_PUT: 4372 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE) 4373 smsg("%4d PUT %c above range", 4374 current, iptr->isn_arg.put.put_regname); 4375 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE) 4376 smsg("%4d PUT %c range", 4377 current, iptr->isn_arg.put.put_regname); 4378 else 4379 smsg("%4d PUT %c %ld", current, 4380 iptr->isn_arg.put.put_regname, 4381 (long)iptr->isn_arg.put.put_lnum); 4382 break; 4383 4384 // TODO: summarize modifiers 4385 case ISN_CMDMOD: 4386 { 4387 char_u *buf; 4388 size_t len = produce_cmdmods( 4389 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE); 4390 4391 buf = alloc(len + 1); 4392 if (buf != NULL) 4393 { 4394 (void)produce_cmdmods( 4395 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE); 4396 smsg("%4d CMDMOD %s", current, buf); 4397 vim_free(buf); 4398 } 4399 break; 4400 } 4401 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break; 4402 4403 case ISN_PROF_START: 4404 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum); 4405 break; 4406 4407 case ISN_PROF_END: 4408 smsg("%4d PROFILE END", current); 4409 break; 4410 4411 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current, 4412 iptr->isn_arg.unpack.unp_count, 4413 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : ""); 4414 break; 4415 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current, 4416 iptr->isn_arg.shuffle.shfl_item, 4417 iptr->isn_arg.shuffle.shfl_up); 4418 break; 4419 case ISN_DROP: smsg("%4d DROP", current); break; 4420 } 4421 4422 out_flush(); // output one line at a time 4423 ui_breakcheck(); 4424 if (got_int) 4425 break; 4426 } 4427 } 4428 4429 /* 4430 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty 4431 * list, etc. Mostly like what JavaScript does, except that empty list and 4432 * empty dictionary are FALSE. 4433 */ 4434 int 4435 tv2bool(typval_T *tv) 4436 { 4437 switch (tv->v_type) 4438 { 4439 case VAR_NUMBER: 4440 return tv->vval.v_number != 0; 4441 case VAR_FLOAT: 4442 #ifdef FEAT_FLOAT 4443 return tv->vval.v_float != 0.0; 4444 #else 4445 break; 4446 #endif 4447 case VAR_PARTIAL: 4448 return tv->vval.v_partial != NULL; 4449 case VAR_FUNC: 4450 case VAR_STRING: 4451 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL; 4452 case VAR_LIST: 4453 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0; 4454 case VAR_DICT: 4455 return tv->vval.v_dict != NULL 4456 && tv->vval.v_dict->dv_hashtab.ht_used > 0; 4457 case VAR_BOOL: 4458 case VAR_SPECIAL: 4459 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE; 4460 case VAR_JOB: 4461 #ifdef FEAT_JOB_CHANNEL 4462 return tv->vval.v_job != NULL; 4463 #else 4464 break; 4465 #endif 4466 case VAR_CHANNEL: 4467 #ifdef FEAT_JOB_CHANNEL 4468 return tv->vval.v_channel != NULL; 4469 #else 4470 break; 4471 #endif 4472 case VAR_BLOB: 4473 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0; 4474 case VAR_UNKNOWN: 4475 case VAR_ANY: 4476 case VAR_VOID: 4477 break; 4478 } 4479 return FALSE; 4480 } 4481 4482 void 4483 emsg_using_string_as(typval_T *tv, int as_number) 4484 { 4485 semsg(_(as_number ? e_using_string_as_number_str 4486 : e_using_string_as_bool_str), 4487 tv->vval.v_string == NULL 4488 ? (char_u *)"" : tv->vval.v_string); 4489 } 4490 4491 /* 4492 * If "tv" is a string give an error and return FAIL. 4493 */ 4494 int 4495 check_not_string(typval_T *tv) 4496 { 4497 if (tv->v_type == VAR_STRING) 4498 { 4499 emsg_using_string_as(tv, TRUE); 4500 clear_tv(tv); 4501 return FAIL; 4502 } 4503 return OK; 4504 } 4505 4506 4507 #endif // FEAT_EVAL 4508