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