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