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