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