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