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