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