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