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