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