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