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