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