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