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