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 || (func_needs_compiling(ufunc, PROFILING(ufunc)) 1301 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL) 1302 == FAIL)) 1303 { 1304 if (did_emsg_cumul + did_emsg == did_emsg_before) 1305 semsg(_(e_function_is_not_compiled_str), 1306 printable_func_name(ufunc)); 1307 return FAIL; 1308 } 1309 1310 { 1311 // Check the function was really compiled. 1312 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 1313 + ufunc->uf_dfunc_idx; 1314 if (INSTRUCTIONS(dfunc) == NULL) 1315 { 1316 iemsg("using call_def_function() on not compiled function"); 1317 return FAIL; 1318 } 1319 } 1320 1321 // If depth of calling is getting too high, don't execute the function. 1322 orig_funcdepth = funcdepth_get(); 1323 if (funcdepth_increment() == FAIL) 1324 return FAIL; 1325 1326 CLEAR_FIELD(funclocal); 1327 CLEAR_FIELD(ectx); 1328 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx; 1329 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500); 1330 if (ga_grow(&ectx.ec_stack, 20) == FAIL) 1331 { 1332 funcdepth_decrement(); 1333 return FAIL; 1334 } 1335 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10); 1336 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10); 1337 1338 // Put arguments on the stack, but no more than what the function expects. 1339 // A lambda can be called with more arguments than it uses. 1340 for (idx = 0; idx < argc 1341 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len); 1342 ++idx) 1343 { 1344 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len 1345 && argv[idx].v_type == VAR_SPECIAL 1346 && argv[idx].vval.v_number == VVAL_NONE) 1347 { 1348 // Use the default value. 1349 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN; 1350 } 1351 else 1352 { 1353 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len 1354 && check_typval_arg_type( 1355 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL) 1356 goto failed_early; 1357 copy_tv(&argv[idx], STACK_TV_BOT(0)); 1358 } 1359 ++ectx.ec_stack.ga_len; 1360 } 1361 1362 // Turn varargs into a list. Empty list if no args. 1363 if (ufunc->uf_va_name != NULL) 1364 { 1365 int vararg_count = argc - ufunc->uf_args.ga_len; 1366 1367 if (vararg_count < 0) 1368 vararg_count = 0; 1369 else 1370 argc -= vararg_count; 1371 if (exe_newlist(vararg_count, &ectx) == FAIL) 1372 goto failed_early; 1373 1374 // Check the type of the list items. 1375 tv = STACK_TV_BOT(-1); 1376 if (ufunc->uf_va_type != NULL 1377 && ufunc->uf_va_type != &t_any 1378 && ufunc->uf_va_type->tt_member != &t_any 1379 && tv->vval.v_list != NULL) 1380 { 1381 type_T *expected = ufunc->uf_va_type->tt_member; 1382 listitem_T *li = tv->vval.v_list->lv_first; 1383 1384 for (idx = 0; idx < vararg_count; ++idx) 1385 { 1386 if (check_typval_arg_type(expected, &li->li_tv, 1387 argc + idx + 1) == FAIL) 1388 goto failed_early; 1389 li = li->li_next; 1390 } 1391 } 1392 1393 if (defcount > 0) 1394 // Move varargs list to below missing default arguments. 1395 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1); 1396 --ectx.ec_stack.ga_len; 1397 } 1398 1399 // Make space for omitted arguments, will store default value below. 1400 // Any varargs list goes after them. 1401 if (defcount > 0) 1402 for (idx = 0; idx < defcount; ++idx) 1403 { 1404 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN; 1405 ++ectx.ec_stack.ga_len; 1406 } 1407 if (ufunc->uf_va_name != NULL) 1408 ++ectx.ec_stack.ga_len; 1409 1410 // Frame pointer points to just after arguments. 1411 ectx.ec_frame_idx = ectx.ec_stack.ga_len; 1412 initial_frame_idx = ectx.ec_frame_idx; 1413 1414 { 1415 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 1416 + ufunc->uf_dfunc_idx; 1417 ufunc_T *base_ufunc = dfunc->df_ufunc; 1418 1419 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done 1420 // by copy_func(). 1421 if (partial != NULL || base_ufunc->uf_partial != NULL) 1422 { 1423 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T); 1424 if (ectx.ec_outer == NULL) 1425 goto failed_early; 1426 if (partial != NULL) 1427 { 1428 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL) 1429 { 1430 if (current_ectx->ec_outer != NULL) 1431 *ectx.ec_outer = *current_ectx->ec_outer; 1432 } 1433 else 1434 *ectx.ec_outer = partial->pt_outer; 1435 } 1436 else 1437 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer; 1438 ectx.ec_outer->out_up_is_copy = TRUE; 1439 } 1440 } 1441 1442 // dummy frame entries 1443 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx) 1444 { 1445 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN; 1446 ++ectx.ec_stack.ga_len; 1447 } 1448 1449 { 1450 // Reserve space for local variables and any closure reference count. 1451 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 1452 + ufunc->uf_dfunc_idx; 1453 1454 for (idx = 0; idx < dfunc->df_varcount; ++idx) 1455 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN; 1456 ectx.ec_stack.ga_len += dfunc->df_varcount; 1457 if (dfunc->df_has_closure) 1458 { 1459 STACK_TV_VAR(idx)->v_type = VAR_NUMBER; 1460 STACK_TV_VAR(idx)->vval.v_number = 0; 1461 ++ectx.ec_stack.ga_len; 1462 } 1463 1464 ectx.ec_instr = INSTRUCTIONS(dfunc); 1465 } 1466 1467 // Following errors are in the function, not the caller. 1468 // Commands behave like vim9script. 1469 estack_push_ufunc(ufunc, 1); 1470 current_sctx = ufunc->uf_script_ctx; 1471 current_sctx.sc_version = SCRIPT_VERSION_VIM9; 1472 1473 // Use a specific location for storing error messages to be converted to an 1474 // exception. 1475 saved_msg_list = msg_list; 1476 msg_list = &private_msg_list; 1477 1478 // Do turn errors into exceptions. 1479 suppress_errthrow = FALSE; 1480 1481 // Do not delete the function while executing it. 1482 ++ufunc->uf_calls; 1483 1484 // When ":silent!" was used before calling then we still abort the 1485 // function. If ":silent!" is used in the function then we don't. 1486 emsg_silent_def = emsg_silent; 1487 did_emsg_def = 0; 1488 1489 where.wt_index = 0; 1490 where.wt_variable = FALSE; 1491 1492 // Start execution at the first instruction. 1493 ectx.ec_iidx = 0; 1494 1495 for (;;) 1496 { 1497 isn_T *iptr; 1498 1499 if (++breakcheck_count >= 100) 1500 { 1501 line_breakcheck(); 1502 breakcheck_count = 0; 1503 } 1504 if (got_int) 1505 { 1506 // Turn CTRL-C into an exception. 1507 got_int = FALSE; 1508 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL) 1509 goto failed; 1510 did_throw = TRUE; 1511 } 1512 1513 if (did_emsg && msg_list != NULL && *msg_list != NULL) 1514 { 1515 // Turn an error message into an exception. 1516 did_emsg = FALSE; 1517 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL) 1518 goto failed; 1519 did_throw = TRUE; 1520 *msg_list = NULL; 1521 } 1522 1523 if (did_throw && !ectx.ec_in_catch) 1524 { 1525 garray_T *trystack = &ectx.ec_trystack; 1526 trycmd_T *trycmd = NULL; 1527 1528 // An exception jumps to the first catch, finally, or returns from 1529 // the current function. 1530 if (trystack->ga_len > 0) 1531 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1; 1532 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx) 1533 { 1534 // jump to ":catch" or ":finally" 1535 ectx.ec_in_catch = TRUE; 1536 ectx.ec_iidx = trycmd->tcd_catch_idx; 1537 } 1538 else 1539 { 1540 // Not inside try or need to return from current functions. 1541 // Push a dummy return value. 1542 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1543 goto failed; 1544 tv = STACK_TV_BOT(0); 1545 tv->v_type = VAR_NUMBER; 1546 tv->vval.v_number = 0; 1547 ++ectx.ec_stack.ga_len; 1548 if (ectx.ec_frame_idx == initial_frame_idx) 1549 { 1550 // At the toplevel we are done. 1551 need_rethrow = TRUE; 1552 if (handle_closure_in_use(&ectx, FALSE) == FAIL) 1553 goto failed; 1554 goto done; 1555 } 1556 1557 if (func_return(&funclocal, &ectx) == FAIL) 1558 goto failed; 1559 } 1560 continue; 1561 } 1562 1563 iptr = &ectx.ec_instr[ectx.ec_iidx++]; 1564 switch (iptr->isn_type) 1565 { 1566 // execute Ex command line 1567 case ISN_EXEC: 1568 { 1569 source_cookie_T cookie; 1570 1571 SOURCING_LNUM = iptr->isn_lnum; 1572 // Pass getsourceline to get an error for a missing ":end" 1573 // command. 1574 CLEAR_FIELD(cookie); 1575 cookie.sourcing_lnum = iptr->isn_lnum - 1; 1576 if (do_cmdline(iptr->isn_arg.string, 1577 getsourceline, &cookie, 1578 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) 1579 == FAIL 1580 || did_emsg) 1581 goto on_error; 1582 } 1583 break; 1584 1585 // execute Ex command from pieces on the stack 1586 case ISN_EXECCONCAT: 1587 { 1588 int count = iptr->isn_arg.number; 1589 size_t len = 0; 1590 int pass; 1591 int i; 1592 char_u *cmd = NULL; 1593 char_u *str; 1594 1595 for (pass = 1; pass <= 2; ++pass) 1596 { 1597 for (i = 0; i < count; ++i) 1598 { 1599 tv = STACK_TV_BOT(i - count); 1600 str = tv->vval.v_string; 1601 if (str != NULL && *str != NUL) 1602 { 1603 if (pass == 2) 1604 STRCPY(cmd + len, str); 1605 len += STRLEN(str); 1606 } 1607 if (pass == 2) 1608 clear_tv(tv); 1609 } 1610 if (pass == 1) 1611 { 1612 cmd = alloc(len + 1); 1613 if (cmd == NULL) 1614 goto failed; 1615 len = 0; 1616 } 1617 } 1618 1619 SOURCING_LNUM = iptr->isn_lnum; 1620 do_cmdline_cmd(cmd); 1621 vim_free(cmd); 1622 } 1623 break; 1624 1625 // execute :echo {string} ... 1626 case ISN_ECHO: 1627 { 1628 int count = iptr->isn_arg.echo.echo_count; 1629 int atstart = TRUE; 1630 int needclr = TRUE; 1631 1632 for (idx = 0; idx < count; ++idx) 1633 { 1634 tv = STACK_TV_BOT(idx - count); 1635 echo_one(tv, iptr->isn_arg.echo.echo_with_white, 1636 &atstart, &needclr); 1637 clear_tv(tv); 1638 } 1639 if (needclr) 1640 msg_clr_eos(); 1641 ectx.ec_stack.ga_len -= count; 1642 } 1643 break; 1644 1645 // :execute {string} ... 1646 // :echomsg {string} ... 1647 // :echoerr {string} ... 1648 case ISN_EXECUTE: 1649 case ISN_ECHOMSG: 1650 case ISN_ECHOERR: 1651 { 1652 int count = iptr->isn_arg.number; 1653 garray_T ga; 1654 char_u buf[NUMBUFLEN]; 1655 char_u *p; 1656 int len; 1657 int failed = FALSE; 1658 1659 ga_init2(&ga, 1, 80); 1660 for (idx = 0; idx < count; ++idx) 1661 { 1662 tv = STACK_TV_BOT(idx - count); 1663 if (iptr->isn_type == ISN_EXECUTE) 1664 { 1665 if (tv->v_type == VAR_CHANNEL 1666 || tv->v_type == VAR_JOB) 1667 { 1668 SOURCING_LNUM = iptr->isn_lnum; 1669 emsg(_(e_inval_string)); 1670 break; 1671 } 1672 else 1673 p = tv_get_string_buf(tv, buf); 1674 } 1675 else 1676 p = tv_stringify(tv, buf); 1677 1678 len = (int)STRLEN(p); 1679 if (ga_grow(&ga, len + 2) == FAIL) 1680 failed = TRUE; 1681 else 1682 { 1683 if (ga.ga_len > 0) 1684 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; 1685 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); 1686 ga.ga_len += len; 1687 } 1688 clear_tv(tv); 1689 } 1690 ectx.ec_stack.ga_len -= count; 1691 if (failed) 1692 { 1693 ga_clear(&ga); 1694 goto on_error; 1695 } 1696 1697 if (ga.ga_data != NULL) 1698 { 1699 if (iptr->isn_type == ISN_EXECUTE) 1700 { 1701 SOURCING_LNUM = iptr->isn_lnum; 1702 do_cmdline_cmd((char_u *)ga.ga_data); 1703 if (did_emsg) 1704 { 1705 ga_clear(&ga); 1706 goto on_error; 1707 } 1708 } 1709 else 1710 { 1711 msg_sb_eol(); 1712 if (iptr->isn_type == ISN_ECHOMSG) 1713 { 1714 msg_attr(ga.ga_data, echo_attr); 1715 out_flush(); 1716 } 1717 else 1718 { 1719 SOURCING_LNUM = iptr->isn_lnum; 1720 emsg(ga.ga_data); 1721 } 1722 } 1723 } 1724 ga_clear(&ga); 1725 } 1726 break; 1727 1728 // load local variable or argument 1729 case ISN_LOAD: 1730 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1731 goto failed; 1732 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0)); 1733 ++ectx.ec_stack.ga_len; 1734 break; 1735 1736 // load v: variable 1737 case ISN_LOADV: 1738 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1739 goto failed; 1740 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0)); 1741 ++ectx.ec_stack.ga_len; 1742 break; 1743 1744 // load s: variable in Vim9 script 1745 case ISN_LOADSCRIPT: 1746 { 1747 scriptref_T *sref = iptr->isn_arg.script.scriptref; 1748 svar_T *sv; 1749 1750 sv = get_script_svar(sref, &ectx); 1751 if (sv == NULL) 1752 goto failed; 1753 allocate_if_null(sv->sv_tv); 1754 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1755 goto failed; 1756 copy_tv(sv->sv_tv, STACK_TV_BOT(0)); 1757 ++ectx.ec_stack.ga_len; 1758 } 1759 break; 1760 1761 // load s: variable in old script 1762 case ISN_LOADS: 1763 { 1764 hashtab_T *ht = &SCRIPT_VARS( 1765 iptr->isn_arg.loadstore.ls_sid); 1766 char_u *name = iptr->isn_arg.loadstore.ls_name; 1767 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE); 1768 1769 if (di == NULL) 1770 { 1771 SOURCING_LNUM = iptr->isn_lnum; 1772 semsg(_(e_undefined_variable_str), name); 1773 goto on_error; 1774 } 1775 else 1776 { 1777 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1778 goto failed; 1779 copy_tv(&di->di_tv, STACK_TV_BOT(0)); 1780 ++ectx.ec_stack.ga_len; 1781 } 1782 } 1783 break; 1784 1785 // load g:/b:/w:/t: variable 1786 case ISN_LOADG: 1787 case ISN_LOADB: 1788 case ISN_LOADW: 1789 case ISN_LOADT: 1790 { 1791 dictitem_T *di = NULL; 1792 hashtab_T *ht = NULL; 1793 char namespace; 1794 1795 switch (iptr->isn_type) 1796 { 1797 case ISN_LOADG: 1798 ht = get_globvar_ht(); 1799 namespace = 'g'; 1800 break; 1801 case ISN_LOADB: 1802 ht = &curbuf->b_vars->dv_hashtab; 1803 namespace = 'b'; 1804 break; 1805 case ISN_LOADW: 1806 ht = &curwin->w_vars->dv_hashtab; 1807 namespace = 'w'; 1808 break; 1809 case ISN_LOADT: 1810 ht = &curtab->tp_vars->dv_hashtab; 1811 namespace = 't'; 1812 break; 1813 default: // Cannot reach here 1814 goto failed; 1815 } 1816 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE); 1817 1818 if (di == NULL) 1819 { 1820 SOURCING_LNUM = iptr->isn_lnum; 1821 semsg(_(e_undefined_variable_char_str), 1822 namespace, iptr->isn_arg.string); 1823 goto on_error; 1824 } 1825 else 1826 { 1827 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1828 goto failed; 1829 copy_tv(&di->di_tv, STACK_TV_BOT(0)); 1830 ++ectx.ec_stack.ga_len; 1831 } 1832 } 1833 break; 1834 1835 // load autoload variable 1836 case ISN_LOADAUTO: 1837 { 1838 char_u *name = iptr->isn_arg.string; 1839 1840 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1841 goto failed; 1842 SOURCING_LNUM = iptr->isn_lnum; 1843 if (eval_variable(name, (int)STRLEN(name), 1844 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL) 1845 goto on_error; 1846 ++ectx.ec_stack.ga_len; 1847 } 1848 break; 1849 1850 // load g:/b:/w:/t: namespace 1851 case ISN_LOADGDICT: 1852 case ISN_LOADBDICT: 1853 case ISN_LOADWDICT: 1854 case ISN_LOADTDICT: 1855 { 1856 dict_T *d = NULL; 1857 1858 switch (iptr->isn_type) 1859 { 1860 case ISN_LOADGDICT: d = get_globvar_dict(); break; 1861 case ISN_LOADBDICT: d = curbuf->b_vars; break; 1862 case ISN_LOADWDICT: d = curwin->w_vars; break; 1863 case ISN_LOADTDICT: d = curtab->tp_vars; break; 1864 default: // Cannot reach here 1865 goto failed; 1866 } 1867 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1868 goto failed; 1869 tv = STACK_TV_BOT(0); 1870 tv->v_type = VAR_DICT; 1871 tv->v_lock = 0; 1872 tv->vval.v_dict = d; 1873 ++d->dv_refcount; 1874 ++ectx.ec_stack.ga_len; 1875 } 1876 break; 1877 1878 // load &option 1879 case ISN_LOADOPT: 1880 { 1881 typval_T optval; 1882 char_u *name = iptr->isn_arg.string; 1883 1884 // This is not expected to fail, name is checked during 1885 // compilation: don't set SOURCING_LNUM. 1886 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1887 goto failed; 1888 if (eval_option(&name, &optval, TRUE) == FAIL) 1889 goto failed; 1890 *STACK_TV_BOT(0) = optval; 1891 ++ectx.ec_stack.ga_len; 1892 } 1893 break; 1894 1895 // load $ENV 1896 case ISN_LOADENV: 1897 { 1898 typval_T optval; 1899 char_u *name = iptr->isn_arg.string; 1900 1901 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1902 goto failed; 1903 // name is always valid, checked when compiling 1904 (void)eval_env_var(&name, &optval, TRUE); 1905 *STACK_TV_BOT(0) = optval; 1906 ++ectx.ec_stack.ga_len; 1907 } 1908 break; 1909 1910 // load @register 1911 case ISN_LOADREG: 1912 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1913 goto failed; 1914 tv = STACK_TV_BOT(0); 1915 tv->v_type = VAR_STRING; 1916 tv->v_lock = 0; 1917 // This may result in NULL, which should be equivalent to an 1918 // empty string. 1919 tv->vval.v_string = get_reg_contents( 1920 iptr->isn_arg.number, GREG_EXPR_SRC); 1921 ++ectx.ec_stack.ga_len; 1922 break; 1923 1924 // store local variable 1925 case ISN_STORE: 1926 --ectx.ec_stack.ga_len; 1927 tv = STACK_TV_VAR(iptr->isn_arg.number); 1928 clear_tv(tv); 1929 *tv = *STACK_TV_BOT(0); 1930 break; 1931 1932 // store s: variable in old script 1933 case ISN_STORES: 1934 { 1935 hashtab_T *ht = &SCRIPT_VARS( 1936 iptr->isn_arg.loadstore.ls_sid); 1937 char_u *name = iptr->isn_arg.loadstore.ls_name; 1938 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE); 1939 1940 --ectx.ec_stack.ga_len; 1941 if (di == NULL) 1942 store_var(name, STACK_TV_BOT(0)); 1943 else 1944 { 1945 SOURCING_LNUM = iptr->isn_lnum; 1946 if (var_check_permission(di, name) == FAIL) 1947 { 1948 clear_tv(STACK_TV_BOT(0)); 1949 goto on_error; 1950 } 1951 clear_tv(&di->di_tv); 1952 di->di_tv = *STACK_TV_BOT(0); 1953 } 1954 } 1955 break; 1956 1957 // store script-local variable in Vim9 script 1958 case ISN_STORESCRIPT: 1959 { 1960 scriptref_T *sref = iptr->isn_arg.script.scriptref; 1961 svar_T *sv; 1962 1963 sv = get_script_svar(sref, &ectx); 1964 if (sv == NULL) 1965 goto failed; 1966 --ectx.ec_stack.ga_len; 1967 1968 // "const" and "final" are checked at compile time, locking 1969 // the value needs to be checked here. 1970 SOURCING_LNUM = iptr->isn_lnum; 1971 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE)) 1972 { 1973 clear_tv(STACK_TV_BOT(0)); 1974 goto on_error; 1975 } 1976 1977 clear_tv(sv->sv_tv); 1978 *sv->sv_tv = *STACK_TV_BOT(0); 1979 } 1980 break; 1981 1982 // store option 1983 case ISN_STOREOPT: 1984 { 1985 long n = 0; 1986 char_u *s = NULL; 1987 char *msg; 1988 1989 --ectx.ec_stack.ga_len; 1990 tv = STACK_TV_BOT(0); 1991 if (tv->v_type == VAR_STRING) 1992 { 1993 s = tv->vval.v_string; 1994 if (s == NULL) 1995 s = (char_u *)""; 1996 } 1997 else 1998 // must be VAR_NUMBER, CHECKTYPE makes sure 1999 n = tv->vval.v_number; 2000 msg = set_option_value(iptr->isn_arg.storeopt.so_name, 2001 n, s, iptr->isn_arg.storeopt.so_flags); 2002 clear_tv(tv); 2003 if (msg != NULL) 2004 { 2005 SOURCING_LNUM = iptr->isn_lnum; 2006 emsg(_(msg)); 2007 goto on_error; 2008 } 2009 } 2010 break; 2011 2012 // store $ENV 2013 case ISN_STOREENV: 2014 --ectx.ec_stack.ga_len; 2015 tv = STACK_TV_BOT(0); 2016 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv)); 2017 clear_tv(tv); 2018 break; 2019 2020 // store @r 2021 case ISN_STOREREG: 2022 { 2023 int reg = iptr->isn_arg.number; 2024 2025 --ectx.ec_stack.ga_len; 2026 tv = STACK_TV_BOT(0); 2027 write_reg_contents(reg == '@' ? '"' : reg, 2028 tv_get_string(tv), -1, FALSE); 2029 clear_tv(tv); 2030 } 2031 break; 2032 2033 // store v: variable 2034 case ISN_STOREV: 2035 --ectx.ec_stack.ga_len; 2036 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0)) 2037 == FAIL) 2038 // should not happen, type is checked when compiling 2039 goto on_error; 2040 break; 2041 2042 // store g:/b:/w:/t: variable 2043 case ISN_STOREG: 2044 case ISN_STOREB: 2045 case ISN_STOREW: 2046 case ISN_STORET: 2047 { 2048 dictitem_T *di; 2049 hashtab_T *ht; 2050 char_u *name = iptr->isn_arg.string + 2; 2051 2052 switch (iptr->isn_type) 2053 { 2054 case ISN_STOREG: 2055 ht = get_globvar_ht(); 2056 break; 2057 case ISN_STOREB: 2058 ht = &curbuf->b_vars->dv_hashtab; 2059 break; 2060 case ISN_STOREW: 2061 ht = &curwin->w_vars->dv_hashtab; 2062 break; 2063 case ISN_STORET: 2064 ht = &curtab->tp_vars->dv_hashtab; 2065 break; 2066 default: // Cannot reach here 2067 goto failed; 2068 } 2069 2070 --ectx.ec_stack.ga_len; 2071 di = find_var_in_ht(ht, 0, name, TRUE); 2072 if (di == NULL) 2073 store_var(iptr->isn_arg.string, STACK_TV_BOT(0)); 2074 else 2075 { 2076 SOURCING_LNUM = iptr->isn_lnum; 2077 if (var_check_permission(di, name) == FAIL) 2078 goto on_error; 2079 clear_tv(&di->di_tv); 2080 di->di_tv = *STACK_TV_BOT(0); 2081 } 2082 } 2083 break; 2084 2085 // store an autoload variable 2086 case ISN_STOREAUTO: 2087 SOURCING_LNUM = iptr->isn_lnum; 2088 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE); 2089 clear_tv(STACK_TV_BOT(-1)); 2090 --ectx.ec_stack.ga_len; 2091 break; 2092 2093 // store number in local variable 2094 case ISN_STORENR: 2095 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx); 2096 clear_tv(tv); 2097 tv->v_type = VAR_NUMBER; 2098 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val; 2099 break; 2100 2101 // store value in list or dict variable 2102 case ISN_STOREINDEX: 2103 { 2104 vartype_T dest_type = iptr->isn_arg.vartype; 2105 typval_T *tv_idx = STACK_TV_BOT(-2); 2106 typval_T *tv_dest = STACK_TV_BOT(-1); 2107 int status = OK; 2108 2109 // Stack contains: 2110 // -3 value to be stored 2111 // -2 index 2112 // -1 dict or list 2113 tv = STACK_TV_BOT(-3); 2114 SOURCING_LNUM = iptr->isn_lnum; 2115 if (dest_type == VAR_ANY) 2116 { 2117 dest_type = tv_dest->v_type; 2118 if (dest_type == VAR_DICT) 2119 status = do_2string(tv_idx, TRUE); 2120 else if (dest_type == VAR_LIST 2121 && tv_idx->v_type != VAR_NUMBER) 2122 { 2123 emsg(_(e_number_exp)); 2124 status = FAIL; 2125 } 2126 } 2127 else if (dest_type != tv_dest->v_type) 2128 { 2129 // just in case, should be OK 2130 semsg(_(e_expected_str_but_got_str), 2131 vartype_name(dest_type), 2132 vartype_name(tv_dest->v_type)); 2133 status = FAIL; 2134 } 2135 2136 if (status == OK && dest_type == VAR_LIST) 2137 { 2138 long lidx = (long)tv_idx->vval.v_number; 2139 list_T *list = tv_dest->vval.v_list; 2140 2141 if (list == NULL) 2142 { 2143 emsg(_(e_list_not_set)); 2144 goto on_error; 2145 } 2146 if (lidx < 0 && list->lv_len + lidx >= 0) 2147 // negative index is relative to the end 2148 lidx = list->lv_len + lidx; 2149 if (lidx < 0 || lidx > list->lv_len) 2150 { 2151 semsg(_(e_listidx), lidx); 2152 goto on_error; 2153 } 2154 if (lidx < list->lv_len) 2155 { 2156 listitem_T *li = list_find(list, lidx); 2157 2158 if (error_if_locked(li->li_tv.v_lock, 2159 e_cannot_change_list_item)) 2160 goto on_error; 2161 // overwrite existing list item 2162 clear_tv(&li->li_tv); 2163 li->li_tv = *tv; 2164 } 2165 else 2166 { 2167 if (error_if_locked(list->lv_lock, 2168 e_cannot_change_list)) 2169 goto on_error; 2170 // append to list, only fails when out of memory 2171 if (list_append_tv(list, tv) == FAIL) 2172 goto failed; 2173 clear_tv(tv); 2174 } 2175 } 2176 else if (status == OK && dest_type == VAR_DICT) 2177 { 2178 char_u *key = tv_idx->vval.v_string; 2179 dict_T *dict = tv_dest->vval.v_dict; 2180 dictitem_T *di; 2181 2182 SOURCING_LNUM = iptr->isn_lnum; 2183 if (dict == NULL) 2184 { 2185 emsg(_(e_dictionary_not_set)); 2186 goto on_error; 2187 } 2188 if (key == NULL) 2189 key = (char_u *)""; 2190 di = dict_find(dict, key, -1); 2191 if (di != NULL) 2192 { 2193 if (error_if_locked(di->di_tv.v_lock, 2194 e_cannot_change_dict_item)) 2195 goto on_error; 2196 // overwrite existing value 2197 clear_tv(&di->di_tv); 2198 di->di_tv = *tv; 2199 } 2200 else 2201 { 2202 if (error_if_locked(dict->dv_lock, 2203 e_cannot_change_dict)) 2204 goto on_error; 2205 // add to dict, only fails when out of memory 2206 if (dict_add_tv(dict, (char *)key, tv) == FAIL) 2207 goto failed; 2208 clear_tv(tv); 2209 } 2210 } 2211 else 2212 { 2213 status = FAIL; 2214 semsg(_(e_cannot_index_str), vartype_name(dest_type)); 2215 } 2216 2217 clear_tv(tv_idx); 2218 clear_tv(tv_dest); 2219 ectx.ec_stack.ga_len -= 3; 2220 if (status == FAIL) 2221 { 2222 clear_tv(tv); 2223 goto on_error; 2224 } 2225 } 2226 break; 2227 2228 // load or store variable or argument from outer scope 2229 case ISN_LOADOUTER: 2230 case ISN_STOREOUTER: 2231 { 2232 int depth = iptr->isn_arg.outer.outer_depth; 2233 outer_T *outer = ectx.ec_outer; 2234 2235 while (depth > 1 && outer != NULL) 2236 { 2237 outer = outer->out_up; 2238 --depth; 2239 } 2240 if (outer == NULL) 2241 { 2242 SOURCING_LNUM = iptr->isn_lnum; 2243 iemsg("LOADOUTER depth more than scope levels"); 2244 goto failed; 2245 } 2246 tv = ((typval_T *)outer->out_stack->ga_data) 2247 + outer->out_frame_idx + STACK_FRAME_SIZE 2248 + iptr->isn_arg.outer.outer_idx; 2249 if (iptr->isn_type == ISN_LOADOUTER) 2250 { 2251 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2252 goto failed; 2253 copy_tv(tv, STACK_TV_BOT(0)); 2254 ++ectx.ec_stack.ga_len; 2255 } 2256 else 2257 { 2258 --ectx.ec_stack.ga_len; 2259 clear_tv(tv); 2260 *tv = *STACK_TV_BOT(0); 2261 } 2262 } 2263 break; 2264 2265 // unlet item in list or dict variable 2266 case ISN_UNLETINDEX: 2267 { 2268 typval_T *tv_idx = STACK_TV_BOT(-2); 2269 typval_T *tv_dest = STACK_TV_BOT(-1); 2270 int status = OK; 2271 2272 // Stack contains: 2273 // -2 index 2274 // -1 dict or list 2275 if (tv_dest->v_type == VAR_DICT) 2276 { 2277 // unlet a dict item, index must be a string 2278 if (tv_idx->v_type != VAR_STRING) 2279 { 2280 SOURCING_LNUM = iptr->isn_lnum; 2281 semsg(_(e_expected_str_but_got_str), 2282 vartype_name(VAR_STRING), 2283 vartype_name(tv_idx->v_type)); 2284 status = FAIL; 2285 } 2286 else 2287 { 2288 dict_T *d = tv_dest->vval.v_dict; 2289 char_u *key = tv_idx->vval.v_string; 2290 dictitem_T *di = NULL; 2291 2292 if (key == NULL) 2293 key = (char_u *)""; 2294 if (d != NULL) 2295 di = dict_find(d, key, (int)STRLEN(key)); 2296 if (di == NULL) 2297 { 2298 // NULL dict is equivalent to empty dict 2299 SOURCING_LNUM = iptr->isn_lnum; 2300 semsg(_(e_dictkey), key); 2301 status = FAIL; 2302 } 2303 else 2304 { 2305 // TODO: check for dict or item locked 2306 dictitem_remove(d, di); 2307 } 2308 } 2309 } 2310 else if (tv_dest->v_type == VAR_LIST) 2311 { 2312 // unlet a List item, index must be a number 2313 SOURCING_LNUM = iptr->isn_lnum; 2314 if (check_for_number(tv_idx) == FAIL) 2315 { 2316 status = FAIL; 2317 } 2318 else 2319 { 2320 list_T *l = tv_dest->vval.v_list; 2321 long n = (long)tv_idx->vval.v_number; 2322 listitem_T *li = NULL; 2323 2324 li = list_find(l, n); 2325 if (li == NULL) 2326 { 2327 SOURCING_LNUM = iptr->isn_lnum; 2328 semsg(_(e_listidx), n); 2329 status = FAIL; 2330 } 2331 else 2332 // TODO: check for list or item locked 2333 listitem_remove(l, li); 2334 } 2335 } 2336 else 2337 { 2338 status = FAIL; 2339 semsg(_(e_cannot_index_str), 2340 vartype_name(tv_dest->v_type)); 2341 } 2342 2343 clear_tv(tv_idx); 2344 clear_tv(tv_dest); 2345 ectx.ec_stack.ga_len -= 2; 2346 if (status == FAIL) 2347 goto on_error; 2348 } 2349 break; 2350 2351 // unlet range of items in list variable 2352 case ISN_UNLETRANGE: 2353 { 2354 // Stack contains: 2355 // -3 index1 2356 // -2 index2 2357 // -1 dict or list 2358 typval_T *tv_idx1 = STACK_TV_BOT(-3); 2359 typval_T *tv_idx2 = STACK_TV_BOT(-2); 2360 typval_T *tv_dest = STACK_TV_BOT(-1); 2361 int status = OK; 2362 2363 if (tv_dest->v_type == VAR_LIST) 2364 { 2365 // indexes must be a number 2366 SOURCING_LNUM = iptr->isn_lnum; 2367 if (check_for_number(tv_idx1) == FAIL 2368 || check_for_number(tv_idx2) == FAIL) 2369 { 2370 status = FAIL; 2371 } 2372 else 2373 { 2374 list_T *l = tv_dest->vval.v_list; 2375 long n1 = (long)tv_idx1->vval.v_number; 2376 long n2 = (long)tv_idx2->vval.v_number; 2377 listitem_T *li; 2378 2379 li = list_find_index(l, &n1); 2380 if (li == NULL 2381 || list_unlet_range(l, li, NULL, n1, 2382 TRUE, n2) == FAIL) 2383 status = FAIL; 2384 } 2385 } 2386 else 2387 { 2388 status = FAIL; 2389 SOURCING_LNUM = iptr->isn_lnum; 2390 semsg(_(e_cannot_index_str), 2391 vartype_name(tv_dest->v_type)); 2392 } 2393 2394 clear_tv(tv_idx1); 2395 clear_tv(tv_idx2); 2396 clear_tv(tv_dest); 2397 ectx.ec_stack.ga_len -= 3; 2398 if (status == FAIL) 2399 goto on_error; 2400 } 2401 break; 2402 2403 // push constant 2404 case ISN_PUSHNR: 2405 case ISN_PUSHBOOL: 2406 case ISN_PUSHSPEC: 2407 case ISN_PUSHF: 2408 case ISN_PUSHS: 2409 case ISN_PUSHBLOB: 2410 case ISN_PUSHFUNC: 2411 case ISN_PUSHCHANNEL: 2412 case ISN_PUSHJOB: 2413 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2414 goto failed; 2415 tv = STACK_TV_BOT(0); 2416 tv->v_lock = 0; 2417 ++ectx.ec_stack.ga_len; 2418 switch (iptr->isn_type) 2419 { 2420 case ISN_PUSHNR: 2421 tv->v_type = VAR_NUMBER; 2422 tv->vval.v_number = iptr->isn_arg.number; 2423 break; 2424 case ISN_PUSHBOOL: 2425 tv->v_type = VAR_BOOL; 2426 tv->vval.v_number = iptr->isn_arg.number; 2427 break; 2428 case ISN_PUSHSPEC: 2429 tv->v_type = VAR_SPECIAL; 2430 tv->vval.v_number = iptr->isn_arg.number; 2431 break; 2432 #ifdef FEAT_FLOAT 2433 case ISN_PUSHF: 2434 tv->v_type = VAR_FLOAT; 2435 tv->vval.v_float = iptr->isn_arg.fnumber; 2436 break; 2437 #endif 2438 case ISN_PUSHBLOB: 2439 blob_copy(iptr->isn_arg.blob, tv); 2440 break; 2441 case ISN_PUSHFUNC: 2442 tv->v_type = VAR_FUNC; 2443 if (iptr->isn_arg.string == NULL) 2444 tv->vval.v_string = NULL; 2445 else 2446 tv->vval.v_string = 2447 vim_strsave(iptr->isn_arg.string); 2448 break; 2449 case ISN_PUSHCHANNEL: 2450 #ifdef FEAT_JOB_CHANNEL 2451 tv->v_type = VAR_CHANNEL; 2452 tv->vval.v_channel = iptr->isn_arg.channel; 2453 if (tv->vval.v_channel != NULL) 2454 ++tv->vval.v_channel->ch_refcount; 2455 #endif 2456 break; 2457 case ISN_PUSHJOB: 2458 #ifdef FEAT_JOB_CHANNEL 2459 tv->v_type = VAR_JOB; 2460 tv->vval.v_job = iptr->isn_arg.job; 2461 if (tv->vval.v_job != NULL) 2462 ++tv->vval.v_job->jv_refcount; 2463 #endif 2464 break; 2465 default: 2466 tv->v_type = VAR_STRING; 2467 tv->vval.v_string = vim_strsave( 2468 iptr->isn_arg.string == NULL 2469 ? (char_u *)"" : iptr->isn_arg.string); 2470 } 2471 break; 2472 2473 case ISN_UNLET: 2474 if (do_unlet(iptr->isn_arg.unlet.ul_name, 2475 iptr->isn_arg.unlet.ul_forceit) == FAIL) 2476 goto on_error; 2477 break; 2478 case ISN_UNLETENV: 2479 vim_unsetenv(iptr->isn_arg.unlet.ul_name); 2480 break; 2481 2482 case ISN_LOCKCONST: 2483 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE); 2484 break; 2485 2486 // create a list from items on the stack; uses a single allocation 2487 // for the list header and the items 2488 case ISN_NEWLIST: 2489 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL) 2490 goto failed; 2491 break; 2492 2493 // create a dict from items on the stack 2494 case ISN_NEWDICT: 2495 { 2496 int count = iptr->isn_arg.number; 2497 dict_T *dict = dict_alloc(); 2498 dictitem_T *item; 2499 char_u *key; 2500 2501 if (dict == NULL) 2502 goto failed; 2503 for (idx = 0; idx < count; ++idx) 2504 { 2505 // have already checked key type is VAR_STRING 2506 tv = STACK_TV_BOT(2 * (idx - count)); 2507 // check key is unique 2508 key = tv->vval.v_string == NULL 2509 ? (char_u *)"" : tv->vval.v_string; 2510 item = dict_find(dict, key, -1); 2511 if (item != NULL) 2512 { 2513 SOURCING_LNUM = iptr->isn_lnum; 2514 semsg(_(e_duplicate_key), key); 2515 dict_unref(dict); 2516 goto on_error; 2517 } 2518 item = dictitem_alloc(key); 2519 clear_tv(tv); 2520 if (item == NULL) 2521 { 2522 dict_unref(dict); 2523 goto failed; 2524 } 2525 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1); 2526 item->di_tv.v_lock = 0; 2527 if (dict_add(dict, item) == FAIL) 2528 { 2529 // can this ever happen? 2530 dict_unref(dict); 2531 goto failed; 2532 } 2533 } 2534 2535 if (count > 0) 2536 ectx.ec_stack.ga_len -= 2 * count - 1; 2537 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2538 goto failed; 2539 else 2540 ++ectx.ec_stack.ga_len; 2541 tv = STACK_TV_BOT(-1); 2542 tv->v_type = VAR_DICT; 2543 tv->v_lock = 0; 2544 tv->vval.v_dict = dict; 2545 ++dict->dv_refcount; 2546 } 2547 break; 2548 2549 // call a :def function 2550 case ISN_DCALL: 2551 SOURCING_LNUM = iptr->isn_lnum; 2552 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, 2553 NULL, 2554 iptr->isn_arg.dfunc.cdf_argcount, 2555 &funclocal, 2556 &ectx) == FAIL) 2557 goto on_error; 2558 break; 2559 2560 // call a builtin function 2561 case ISN_BCALL: 2562 SOURCING_LNUM = iptr->isn_lnum; 2563 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx, 2564 iptr->isn_arg.bfunc.cbf_argcount, 2565 &ectx) == FAIL) 2566 goto on_error; 2567 break; 2568 2569 // call a funcref or partial 2570 case ISN_PCALL: 2571 { 2572 cpfunc_T *pfunc = &iptr->isn_arg.pfunc; 2573 int r; 2574 typval_T partial_tv; 2575 2576 SOURCING_LNUM = iptr->isn_lnum; 2577 if (pfunc->cpf_top) 2578 { 2579 // funcref is above the arguments 2580 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1); 2581 } 2582 else 2583 { 2584 // Get the funcref from the stack. 2585 --ectx.ec_stack.ga_len; 2586 partial_tv = *STACK_TV_BOT(0); 2587 tv = &partial_tv; 2588 } 2589 r = call_partial(tv, pfunc->cpf_argcount, 2590 &funclocal, &ectx); 2591 if (tv == &partial_tv) 2592 clear_tv(&partial_tv); 2593 if (r == FAIL) 2594 goto on_error; 2595 } 2596 break; 2597 2598 case ISN_PCALL_END: 2599 // PCALL finished, arguments have been consumed and replaced by 2600 // the return value. Now clear the funcref from the stack, 2601 // and move the return value in its place. 2602 --ectx.ec_stack.ga_len; 2603 clear_tv(STACK_TV_BOT(-1)); 2604 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0); 2605 break; 2606 2607 // call a user defined function or funcref/partial 2608 case ISN_UCALL: 2609 { 2610 cufunc_T *cufunc = &iptr->isn_arg.ufunc; 2611 2612 SOURCING_LNUM = iptr->isn_lnum; 2613 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount, 2614 &funclocal, &ectx, iptr) == FAIL) 2615 goto on_error; 2616 } 2617 break; 2618 2619 // return from a :def function call 2620 case ISN_RETURN_ZERO: 2621 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2622 goto failed; 2623 tv = STACK_TV_BOT(0); 2624 ++ectx.ec_stack.ga_len; 2625 tv->v_type = VAR_NUMBER; 2626 tv->vval.v_number = 0; 2627 tv->v_lock = 0; 2628 // FALLTHROUGH 2629 2630 case ISN_RETURN: 2631 { 2632 garray_T *trystack = &ectx.ec_trystack; 2633 trycmd_T *trycmd = NULL; 2634 2635 if (trystack->ga_len > 0) 2636 trycmd = ((trycmd_T *)trystack->ga_data) 2637 + trystack->ga_len - 1; 2638 if (trycmd != NULL 2639 && trycmd->tcd_frame_idx == ectx.ec_frame_idx) 2640 { 2641 // jump to ":finally" or ":endtry" 2642 if (trycmd->tcd_finally_idx != 0) 2643 ectx.ec_iidx = trycmd->tcd_finally_idx; 2644 else 2645 ectx.ec_iidx = trycmd->tcd_endtry_idx; 2646 trycmd->tcd_return = TRUE; 2647 } 2648 else 2649 goto func_return; 2650 } 2651 break; 2652 2653 // push a function reference to a compiled function 2654 case ISN_FUNCREF: 2655 { 2656 partial_T *pt = ALLOC_CLEAR_ONE(partial_T); 2657 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data) 2658 + iptr->isn_arg.funcref.fr_func; 2659 2660 if (pt == NULL) 2661 goto failed; 2662 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2663 { 2664 vim_free(pt); 2665 goto failed; 2666 } 2667 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc, 2668 &ectx) == FAIL) 2669 goto failed; 2670 2671 tv = STACK_TV_BOT(0); 2672 ++ectx.ec_stack.ga_len; 2673 tv->vval.v_partial = pt; 2674 tv->v_type = VAR_PARTIAL; 2675 tv->v_lock = 0; 2676 } 2677 break; 2678 2679 // Create a global function from a lambda. 2680 case ISN_NEWFUNC: 2681 { 2682 newfunc_T *newfunc = &iptr->isn_arg.newfunc; 2683 2684 if (copy_func(newfunc->nf_lambda, newfunc->nf_global, 2685 &ectx) == FAIL) 2686 goto failed; 2687 } 2688 break; 2689 2690 // List functions 2691 case ISN_DEF: 2692 if (iptr->isn_arg.string == NULL) 2693 list_functions(NULL); 2694 else 2695 { 2696 exarg_T ea; 2697 2698 CLEAR_FIELD(ea); 2699 ea.cmd = ea.arg = iptr->isn_arg.string; 2700 define_function(&ea, NULL); 2701 } 2702 break; 2703 2704 // jump if a condition is met 2705 case ISN_JUMP: 2706 { 2707 jumpwhen_T when = iptr->isn_arg.jump.jump_when; 2708 int error = FALSE; 2709 int jump = TRUE; 2710 2711 if (when != JUMP_ALWAYS) 2712 { 2713 tv = STACK_TV_BOT(-1); 2714 if (when == JUMP_IF_COND_FALSE 2715 || when == JUMP_IF_FALSE 2716 || when == JUMP_IF_COND_TRUE) 2717 { 2718 SOURCING_LNUM = iptr->isn_lnum; 2719 jump = tv_get_bool_chk(tv, &error); 2720 if (error) 2721 goto on_error; 2722 } 2723 else 2724 jump = tv2bool(tv); 2725 if (when == JUMP_IF_FALSE 2726 || when == JUMP_AND_KEEP_IF_FALSE 2727 || when == JUMP_IF_COND_FALSE) 2728 jump = !jump; 2729 if (when == JUMP_IF_FALSE || !jump) 2730 { 2731 // drop the value from the stack 2732 clear_tv(tv); 2733 --ectx.ec_stack.ga_len; 2734 } 2735 } 2736 if (jump) 2737 ectx.ec_iidx = iptr->isn_arg.jump.jump_where; 2738 } 2739 break; 2740 2741 // Jump if an argument with a default value was already set and not 2742 // v:none. 2743 case ISN_JUMP_IF_ARG_SET: 2744 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off); 2745 if (tv->v_type != VAR_UNKNOWN 2746 && !(tv->v_type == VAR_SPECIAL 2747 && tv->vval.v_number == VVAL_NONE)) 2748 ectx.ec_iidx = iptr->isn_arg.jumparg.jump_where; 2749 break; 2750 2751 // top of a for loop 2752 case ISN_FOR: 2753 { 2754 typval_T *ltv = STACK_TV_BOT(-1); 2755 typval_T *idxtv = 2756 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx); 2757 2758 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2759 goto failed; 2760 if (ltv->v_type == VAR_LIST) 2761 { 2762 list_T *list = ltv->vval.v_list; 2763 2764 // push the next item from the list 2765 ++idxtv->vval.v_number; 2766 if (list == NULL 2767 || idxtv->vval.v_number >= list->lv_len) 2768 { 2769 // past the end of the list, jump to "endfor" 2770 ectx.ec_iidx = iptr->isn_arg.forloop.for_end; 2771 may_restore_cmdmod(&funclocal); 2772 } 2773 else if (list->lv_first == &range_list_item) 2774 { 2775 // non-materialized range() list 2776 tv = STACK_TV_BOT(0); 2777 tv->v_type = VAR_NUMBER; 2778 tv->v_lock = 0; 2779 tv->vval.v_number = list_find_nr( 2780 list, idxtv->vval.v_number, NULL); 2781 ++ectx.ec_stack.ga_len; 2782 } 2783 else 2784 { 2785 listitem_T *li = list_find(list, 2786 idxtv->vval.v_number); 2787 2788 copy_tv(&li->li_tv, STACK_TV_BOT(0)); 2789 ++ectx.ec_stack.ga_len; 2790 } 2791 } 2792 else if (ltv->v_type == VAR_STRING) 2793 { 2794 char_u *str = ltv->vval.v_string; 2795 2796 // Push the next character from the string. The index 2797 // is for the last byte of the previous character. 2798 ++idxtv->vval.v_number; 2799 if (str == NULL || str[idxtv->vval.v_number] == NUL) 2800 { 2801 // past the end of the string, jump to "endfor" 2802 ectx.ec_iidx = iptr->isn_arg.forloop.for_end; 2803 may_restore_cmdmod(&funclocal); 2804 } 2805 else 2806 { 2807 int clen = mb_ptr2len(str + idxtv->vval.v_number); 2808 2809 tv = STACK_TV_BOT(0); 2810 tv->v_type = VAR_STRING; 2811 tv->vval.v_string = vim_strnsave( 2812 str + idxtv->vval.v_number, clen); 2813 ++ectx.ec_stack.ga_len; 2814 idxtv->vval.v_number += clen - 1; 2815 } 2816 } 2817 else 2818 { 2819 // TODO: support Blob 2820 semsg(_(e_for_loop_on_str_not_supported), 2821 vartype_name(ltv->v_type)); 2822 goto failed; 2823 } 2824 } 2825 break; 2826 2827 // start of ":try" block 2828 case ISN_TRY: 2829 { 2830 trycmd_T *trycmd = NULL; 2831 2832 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL) 2833 goto failed; 2834 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data) 2835 + ectx.ec_trystack.ga_len; 2836 ++ectx.ec_trystack.ga_len; 2837 ++trylevel; 2838 CLEAR_POINTER(trycmd); 2839 trycmd->tcd_frame_idx = ectx.ec_frame_idx; 2840 trycmd->tcd_stack_len = ectx.ec_stack.ga_len; 2841 trycmd->tcd_catch_idx = 2842 iptr->isn_arg.try.try_ref->try_catch; 2843 trycmd->tcd_finally_idx = 2844 iptr->isn_arg.try.try_ref->try_finally; 2845 trycmd->tcd_endtry_idx = 2846 iptr->isn_arg.try.try_ref->try_endtry; 2847 } 2848 break; 2849 2850 case ISN_PUSHEXC: 2851 if (current_exception == NULL) 2852 { 2853 SOURCING_LNUM = iptr->isn_lnum; 2854 iemsg("Evaluating catch while current_exception is NULL"); 2855 goto failed; 2856 } 2857 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2858 goto failed; 2859 tv = STACK_TV_BOT(0); 2860 ++ectx.ec_stack.ga_len; 2861 tv->v_type = VAR_STRING; 2862 tv->v_lock = 0; 2863 tv->vval.v_string = vim_strsave( 2864 (char_u *)current_exception->value); 2865 break; 2866 2867 case ISN_CATCH: 2868 { 2869 garray_T *trystack = &ectx.ec_trystack; 2870 2871 may_restore_cmdmod(&funclocal); 2872 if (trystack->ga_len > 0) 2873 { 2874 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) 2875 + trystack->ga_len - 1; 2876 trycmd->tcd_caught = TRUE; 2877 } 2878 did_emsg = got_int = did_throw = FALSE; 2879 force_abort = need_rethrow = FALSE; 2880 catch_exception(current_exception); 2881 } 2882 break; 2883 2884 case ISN_TRYCONT: 2885 { 2886 garray_T *trystack = &ectx.ec_trystack; 2887 trycont_T *trycont = &iptr->isn_arg.trycont; 2888 int i; 2889 trycmd_T *trycmd; 2890 int iidx = trycont->tct_where; 2891 2892 if (trystack->ga_len < trycont->tct_levels) 2893 { 2894 siemsg("TRYCONT: expected %d levels, found %d", 2895 trycont->tct_levels, trystack->ga_len); 2896 goto failed; 2897 } 2898 // Make :endtry jump to any outer try block and the last 2899 // :endtry inside the loop to the loop start. 2900 for (i = trycont->tct_levels; i > 0; --i) 2901 { 2902 trycmd = ((trycmd_T *)trystack->ga_data) 2903 + trystack->ga_len - i; 2904 // Add one to tcd_cont to be able to jump to 2905 // instruction with index zero. 2906 trycmd->tcd_cont = iidx + 1; 2907 iidx = trycmd->tcd_finally_idx == 0 2908 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx; 2909 } 2910 // jump to :finally or :endtry of current try statement 2911 ectx.ec_iidx = iidx; 2912 } 2913 break; 2914 2915 case ISN_FINALLY: 2916 { 2917 garray_T *trystack = &ectx.ec_trystack; 2918 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) 2919 + trystack->ga_len - 1; 2920 2921 // Reset the index to avoid a return statement jumps here 2922 // again. 2923 trycmd->tcd_finally_idx = 0; 2924 break; 2925 } 2926 2927 // end of ":try" block 2928 case ISN_ENDTRY: 2929 { 2930 garray_T *trystack = &ectx.ec_trystack; 2931 2932 if (trystack->ga_len > 0) 2933 { 2934 trycmd_T *trycmd; 2935 2936 --trystack->ga_len; 2937 --trylevel; 2938 ectx.ec_in_catch = FALSE; 2939 trycmd = ((trycmd_T *)trystack->ga_data) 2940 + trystack->ga_len; 2941 if (trycmd->tcd_caught && current_exception != NULL) 2942 { 2943 // discard the exception 2944 if (caught_stack == current_exception) 2945 caught_stack = caught_stack->caught; 2946 discard_current_exception(); 2947 } 2948 2949 if (trycmd->tcd_return) 2950 goto func_return; 2951 2952 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len) 2953 { 2954 --ectx.ec_stack.ga_len; 2955 clear_tv(STACK_TV_BOT(0)); 2956 } 2957 if (trycmd->tcd_cont != 0) 2958 // handling :continue: jump to outer try block or 2959 // start of the loop 2960 ectx.ec_iidx = trycmd->tcd_cont - 1; 2961 } 2962 } 2963 break; 2964 2965 case ISN_THROW: 2966 { 2967 garray_T *trystack = &ectx.ec_trystack; 2968 2969 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent) 2970 { 2971 // throwing an exception while using "silent!" causes 2972 // the function to abort but not display an error. 2973 tv = STACK_TV_BOT(-1); 2974 clear_tv(tv); 2975 tv->v_type = VAR_NUMBER; 2976 tv->vval.v_number = 0; 2977 goto done; 2978 } 2979 --ectx.ec_stack.ga_len; 2980 tv = STACK_TV_BOT(0); 2981 if (tv->vval.v_string == NULL 2982 || *skipwhite(tv->vval.v_string) == NUL) 2983 { 2984 vim_free(tv->vval.v_string); 2985 SOURCING_LNUM = iptr->isn_lnum; 2986 emsg(_(e_throw_with_empty_string)); 2987 goto failed; 2988 } 2989 2990 // Inside a "catch" we need to first discard the caught 2991 // exception. 2992 if (trystack->ga_len > 0) 2993 { 2994 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) 2995 + trystack->ga_len - 1; 2996 if (trycmd->tcd_caught && current_exception != NULL) 2997 { 2998 // discard the exception 2999 if (caught_stack == current_exception) 3000 caught_stack = caught_stack->caught; 3001 discard_current_exception(); 3002 trycmd->tcd_caught = FALSE; 3003 } 3004 } 3005 3006 if (throw_exception(tv->vval.v_string, ET_USER, NULL) 3007 == FAIL) 3008 { 3009 vim_free(tv->vval.v_string); 3010 goto failed; 3011 } 3012 did_throw = TRUE; 3013 } 3014 break; 3015 3016 // compare with special values 3017 case ISN_COMPAREBOOL: 3018 case ISN_COMPARESPECIAL: 3019 { 3020 typval_T *tv1 = STACK_TV_BOT(-2); 3021 typval_T *tv2 = STACK_TV_BOT(-1); 3022 varnumber_T arg1 = tv1->vval.v_number; 3023 varnumber_T arg2 = tv2->vval.v_number; 3024 int res; 3025 3026 switch (iptr->isn_arg.op.op_type) 3027 { 3028 case EXPR_EQUAL: res = arg1 == arg2; break; 3029 case EXPR_NEQUAL: res = arg1 != arg2; break; 3030 default: res = 0; break; 3031 } 3032 3033 --ectx.ec_stack.ga_len; 3034 tv1->v_type = VAR_BOOL; 3035 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; 3036 } 3037 break; 3038 3039 // Operation with two number arguments 3040 case ISN_OPNR: 3041 case ISN_COMPARENR: 3042 { 3043 typval_T *tv1 = STACK_TV_BOT(-2); 3044 typval_T *tv2 = STACK_TV_BOT(-1); 3045 varnumber_T arg1 = tv1->vval.v_number; 3046 varnumber_T arg2 = tv2->vval.v_number; 3047 varnumber_T res; 3048 3049 switch (iptr->isn_arg.op.op_type) 3050 { 3051 case EXPR_MULT: res = arg1 * arg2; break; 3052 case EXPR_DIV: res = arg1 / arg2; break; 3053 case EXPR_REM: res = arg1 % arg2; break; 3054 case EXPR_SUB: res = arg1 - arg2; break; 3055 case EXPR_ADD: res = arg1 + arg2; break; 3056 3057 case EXPR_EQUAL: res = arg1 == arg2; break; 3058 case EXPR_NEQUAL: res = arg1 != arg2; break; 3059 case EXPR_GREATER: res = arg1 > arg2; break; 3060 case EXPR_GEQUAL: res = arg1 >= arg2; break; 3061 case EXPR_SMALLER: res = arg1 < arg2; break; 3062 case EXPR_SEQUAL: res = arg1 <= arg2; break; 3063 default: res = 0; break; 3064 } 3065 3066 --ectx.ec_stack.ga_len; 3067 if (iptr->isn_type == ISN_COMPARENR) 3068 { 3069 tv1->v_type = VAR_BOOL; 3070 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; 3071 } 3072 else 3073 tv1->vval.v_number = res; 3074 } 3075 break; 3076 3077 // Computation with two float arguments 3078 case ISN_OPFLOAT: 3079 case ISN_COMPAREFLOAT: 3080 #ifdef FEAT_FLOAT 3081 { 3082 typval_T *tv1 = STACK_TV_BOT(-2); 3083 typval_T *tv2 = STACK_TV_BOT(-1); 3084 float_T arg1 = tv1->vval.v_float; 3085 float_T arg2 = tv2->vval.v_float; 3086 float_T res = 0; 3087 int cmp = FALSE; 3088 3089 switch (iptr->isn_arg.op.op_type) 3090 { 3091 case EXPR_MULT: res = arg1 * arg2; break; 3092 case EXPR_DIV: res = arg1 / arg2; break; 3093 case EXPR_SUB: res = arg1 - arg2; break; 3094 case EXPR_ADD: res = arg1 + arg2; break; 3095 3096 case EXPR_EQUAL: cmp = arg1 == arg2; break; 3097 case EXPR_NEQUAL: cmp = arg1 != arg2; break; 3098 case EXPR_GREATER: cmp = arg1 > arg2; break; 3099 case EXPR_GEQUAL: cmp = arg1 >= arg2; break; 3100 case EXPR_SMALLER: cmp = arg1 < arg2; break; 3101 case EXPR_SEQUAL: cmp = arg1 <= arg2; break; 3102 default: cmp = 0; break; 3103 } 3104 --ectx.ec_stack.ga_len; 3105 if (iptr->isn_type == ISN_COMPAREFLOAT) 3106 { 3107 tv1->v_type = VAR_BOOL; 3108 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 3109 } 3110 else 3111 tv1->vval.v_float = res; 3112 } 3113 #endif 3114 break; 3115 3116 case ISN_COMPARELIST: 3117 { 3118 typval_T *tv1 = STACK_TV_BOT(-2); 3119 typval_T *tv2 = STACK_TV_BOT(-1); 3120 list_T *arg1 = tv1->vval.v_list; 3121 list_T *arg2 = tv2->vval.v_list; 3122 int cmp = FALSE; 3123 int ic = iptr->isn_arg.op.op_ic; 3124 3125 switch (iptr->isn_arg.op.op_type) 3126 { 3127 case EXPR_EQUAL: cmp = 3128 list_equal(arg1, arg2, ic, FALSE); break; 3129 case EXPR_NEQUAL: cmp = 3130 !list_equal(arg1, arg2, ic, FALSE); break; 3131 case EXPR_IS: cmp = arg1 == arg2; break; 3132 case EXPR_ISNOT: cmp = arg1 != arg2; break; 3133 default: cmp = 0; break; 3134 } 3135 --ectx.ec_stack.ga_len; 3136 clear_tv(tv1); 3137 clear_tv(tv2); 3138 tv1->v_type = VAR_BOOL; 3139 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 3140 } 3141 break; 3142 3143 case ISN_COMPAREBLOB: 3144 { 3145 typval_T *tv1 = STACK_TV_BOT(-2); 3146 typval_T *tv2 = STACK_TV_BOT(-1); 3147 blob_T *arg1 = tv1->vval.v_blob; 3148 blob_T *arg2 = tv2->vval.v_blob; 3149 int cmp = FALSE; 3150 3151 switch (iptr->isn_arg.op.op_type) 3152 { 3153 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break; 3154 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break; 3155 case EXPR_IS: cmp = arg1 == arg2; break; 3156 case EXPR_ISNOT: cmp = arg1 != arg2; break; 3157 default: cmp = 0; break; 3158 } 3159 --ectx.ec_stack.ga_len; 3160 clear_tv(tv1); 3161 clear_tv(tv2); 3162 tv1->v_type = VAR_BOOL; 3163 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 3164 } 3165 break; 3166 3167 // TODO: handle separately 3168 case ISN_COMPARESTRING: 3169 case ISN_COMPAREDICT: 3170 case ISN_COMPAREFUNC: 3171 case ISN_COMPAREANY: 3172 { 3173 typval_T *tv1 = STACK_TV_BOT(-2); 3174 typval_T *tv2 = STACK_TV_BOT(-1); 3175 exprtype_T exprtype = iptr->isn_arg.op.op_type; 3176 int ic = iptr->isn_arg.op.op_ic; 3177 3178 SOURCING_LNUM = iptr->isn_lnum; 3179 typval_compare(tv1, tv2, exprtype, ic); 3180 clear_tv(tv2); 3181 --ectx.ec_stack.ga_len; 3182 } 3183 break; 3184 3185 case ISN_ADDLIST: 3186 case ISN_ADDBLOB: 3187 { 3188 typval_T *tv1 = STACK_TV_BOT(-2); 3189 typval_T *tv2 = STACK_TV_BOT(-1); 3190 3191 // add two lists or blobs 3192 if (iptr->isn_type == ISN_ADDLIST) 3193 eval_addlist(tv1, tv2); 3194 else 3195 eval_addblob(tv1, tv2); 3196 clear_tv(tv2); 3197 --ectx.ec_stack.ga_len; 3198 } 3199 break; 3200 3201 case ISN_LISTAPPEND: 3202 { 3203 typval_T *tv1 = STACK_TV_BOT(-2); 3204 typval_T *tv2 = STACK_TV_BOT(-1); 3205 list_T *l = tv1->vval.v_list; 3206 3207 // add an item to a list 3208 if (l == NULL) 3209 { 3210 SOURCING_LNUM = iptr->isn_lnum; 3211 emsg(_(e_cannot_add_to_null_list)); 3212 goto on_error; 3213 } 3214 if (list_append_tv(l, tv2) == FAIL) 3215 goto failed; 3216 clear_tv(tv2); 3217 --ectx.ec_stack.ga_len; 3218 } 3219 break; 3220 3221 case ISN_BLOBAPPEND: 3222 { 3223 typval_T *tv1 = STACK_TV_BOT(-2); 3224 typval_T *tv2 = STACK_TV_BOT(-1); 3225 blob_T *b = tv1->vval.v_blob; 3226 int error = FALSE; 3227 varnumber_T n; 3228 3229 // add a number to a blob 3230 if (b == NULL) 3231 { 3232 SOURCING_LNUM = iptr->isn_lnum; 3233 emsg(_(e_cannot_add_to_null_blob)); 3234 goto on_error; 3235 } 3236 n = tv_get_number_chk(tv2, &error); 3237 if (error) 3238 goto on_error; 3239 ga_append(&b->bv_ga, (int)n); 3240 --ectx.ec_stack.ga_len; 3241 } 3242 break; 3243 3244 // Computation with two arguments of unknown type 3245 case ISN_OPANY: 3246 { 3247 typval_T *tv1 = STACK_TV_BOT(-2); 3248 typval_T *tv2 = STACK_TV_BOT(-1); 3249 varnumber_T n1, n2; 3250 #ifdef FEAT_FLOAT 3251 float_T f1 = 0, f2 = 0; 3252 #endif 3253 int error = FALSE; 3254 3255 if (iptr->isn_arg.op.op_type == EXPR_ADD) 3256 { 3257 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST) 3258 { 3259 eval_addlist(tv1, tv2); 3260 clear_tv(tv2); 3261 --ectx.ec_stack.ga_len; 3262 break; 3263 } 3264 else if (tv1->v_type == VAR_BLOB 3265 && tv2->v_type == VAR_BLOB) 3266 { 3267 eval_addblob(tv1, tv2); 3268 clear_tv(tv2); 3269 --ectx.ec_stack.ga_len; 3270 break; 3271 } 3272 } 3273 #ifdef FEAT_FLOAT 3274 if (tv1->v_type == VAR_FLOAT) 3275 { 3276 f1 = tv1->vval.v_float; 3277 n1 = 0; 3278 } 3279 else 3280 #endif 3281 { 3282 SOURCING_LNUM = iptr->isn_lnum; 3283 n1 = tv_get_number_chk(tv1, &error); 3284 if (error) 3285 goto on_error; 3286 #ifdef FEAT_FLOAT 3287 if (tv2->v_type == VAR_FLOAT) 3288 f1 = n1; 3289 #endif 3290 } 3291 #ifdef FEAT_FLOAT 3292 if (tv2->v_type == VAR_FLOAT) 3293 { 3294 f2 = tv2->vval.v_float; 3295 n2 = 0; 3296 } 3297 else 3298 #endif 3299 { 3300 n2 = tv_get_number_chk(tv2, &error); 3301 if (error) 3302 goto on_error; 3303 #ifdef FEAT_FLOAT 3304 if (tv1->v_type == VAR_FLOAT) 3305 f2 = n2; 3306 #endif 3307 } 3308 #ifdef FEAT_FLOAT 3309 // if there is a float on either side the result is a float 3310 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT) 3311 { 3312 switch (iptr->isn_arg.op.op_type) 3313 { 3314 case EXPR_MULT: f1 = f1 * f2; break; 3315 case EXPR_DIV: f1 = f1 / f2; break; 3316 case EXPR_SUB: f1 = f1 - f2; break; 3317 case EXPR_ADD: f1 = f1 + f2; break; 3318 default: SOURCING_LNUM = iptr->isn_lnum; 3319 emsg(_(e_modulus)); 3320 goto on_error; 3321 } 3322 clear_tv(tv1); 3323 clear_tv(tv2); 3324 tv1->v_type = VAR_FLOAT; 3325 tv1->vval.v_float = f1; 3326 --ectx.ec_stack.ga_len; 3327 } 3328 else 3329 #endif 3330 { 3331 int failed = FALSE; 3332 3333 switch (iptr->isn_arg.op.op_type) 3334 { 3335 case EXPR_MULT: n1 = n1 * n2; break; 3336 case EXPR_DIV: n1 = num_divide(n1, n2, &failed); 3337 if (failed) 3338 goto on_error; 3339 break; 3340 case EXPR_SUB: n1 = n1 - n2; break; 3341 case EXPR_ADD: n1 = n1 + n2; break; 3342 default: n1 = num_modulus(n1, n2, &failed); 3343 if (failed) 3344 goto on_error; 3345 break; 3346 } 3347 clear_tv(tv1); 3348 clear_tv(tv2); 3349 tv1->v_type = VAR_NUMBER; 3350 tv1->vval.v_number = n1; 3351 --ectx.ec_stack.ga_len; 3352 } 3353 } 3354 break; 3355 3356 case ISN_CONCAT: 3357 { 3358 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string; 3359 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string; 3360 char_u *res; 3361 3362 res = concat_str(str1, str2); 3363 clear_tv(STACK_TV_BOT(-2)); 3364 clear_tv(STACK_TV_BOT(-1)); 3365 --ectx.ec_stack.ga_len; 3366 STACK_TV_BOT(-1)->vval.v_string = res; 3367 } 3368 break; 3369 3370 case ISN_STRINDEX: 3371 case ISN_STRSLICE: 3372 { 3373 int is_slice = iptr->isn_type == ISN_STRSLICE; 3374 varnumber_T n1 = 0, n2; 3375 char_u *res; 3376 3377 // string index: string is at stack-2, index at stack-1 3378 // string slice: string is at stack-3, first index at 3379 // stack-2, second index at stack-1 3380 if (is_slice) 3381 { 3382 tv = STACK_TV_BOT(-2); 3383 n1 = tv->vval.v_number; 3384 } 3385 3386 tv = STACK_TV_BOT(-1); 3387 n2 = tv->vval.v_number; 3388 3389 ectx.ec_stack.ga_len -= is_slice ? 2 : 1; 3390 tv = STACK_TV_BOT(-1); 3391 if (is_slice) 3392 // Slice: Select the characters from the string 3393 res = string_slice(tv->vval.v_string, n1, n2, FALSE); 3394 else 3395 // Index: The resulting variable is a string of a 3396 // single character (including composing characters). 3397 // If the index is too big or negative the result is 3398 // empty. 3399 res = char_from_string(tv->vval.v_string, n2); 3400 vim_free(tv->vval.v_string); 3401 tv->vval.v_string = res; 3402 } 3403 break; 3404 3405 case ISN_LISTINDEX: 3406 case ISN_LISTSLICE: 3407 { 3408 int is_slice = iptr->isn_type == ISN_LISTSLICE; 3409 list_T *list; 3410 varnumber_T n1, n2; 3411 3412 // list index: list is at stack-2, index at stack-1 3413 // list slice: list is at stack-3, indexes at stack-2 and 3414 // stack-1 3415 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2); 3416 list = tv->vval.v_list; 3417 3418 tv = STACK_TV_BOT(-1); 3419 n1 = n2 = tv->vval.v_number; 3420 clear_tv(tv); 3421 3422 if (is_slice) 3423 { 3424 tv = STACK_TV_BOT(-2); 3425 n1 = tv->vval.v_number; 3426 clear_tv(tv); 3427 } 3428 3429 ectx.ec_stack.ga_len -= is_slice ? 2 : 1; 3430 tv = STACK_TV_BOT(-1); 3431 SOURCING_LNUM = iptr->isn_lnum; 3432 if (list_slice_or_index(list, is_slice, n1, n2, FALSE, 3433 tv, TRUE) == FAIL) 3434 goto on_error; 3435 } 3436 break; 3437 3438 case ISN_ANYINDEX: 3439 case ISN_ANYSLICE: 3440 { 3441 int is_slice = iptr->isn_type == ISN_ANYSLICE; 3442 typval_T *var1, *var2; 3443 int res; 3444 3445 // index: composite is at stack-2, index at stack-1 3446 // slice: composite is at stack-3, indexes at stack-2 and 3447 // stack-1 3448 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2); 3449 SOURCING_LNUM = iptr->isn_lnum; 3450 if (check_can_index(tv, TRUE, TRUE) == FAIL) 3451 goto on_error; 3452 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1); 3453 var2 = is_slice ? STACK_TV_BOT(-1) : NULL; 3454 res = eval_index_inner(tv, is_slice, var1, var2, 3455 FALSE, NULL, -1, TRUE); 3456 clear_tv(var1); 3457 if (is_slice) 3458 clear_tv(var2); 3459 ectx.ec_stack.ga_len -= is_slice ? 2 : 1; 3460 if (res == FAIL) 3461 goto on_error; 3462 } 3463 break; 3464 3465 case ISN_SLICE: 3466 { 3467 list_T *list; 3468 int count = iptr->isn_arg.number; 3469 3470 // type will have been checked to be a list 3471 tv = STACK_TV_BOT(-1); 3472 list = tv->vval.v_list; 3473 3474 // no error for short list, expect it to be checked earlier 3475 if (list != NULL && list->lv_len >= count) 3476 { 3477 list_T *newlist = list_slice(list, 3478 count, list->lv_len - 1); 3479 3480 if (newlist != NULL) 3481 { 3482 list_unref(list); 3483 tv->vval.v_list = newlist; 3484 ++newlist->lv_refcount; 3485 } 3486 } 3487 } 3488 break; 3489 3490 case ISN_GETITEM: 3491 { 3492 listitem_T *li; 3493 int index = iptr->isn_arg.number; 3494 3495 // Get list item: list is at stack-1, push item. 3496 // List type and length is checked for when compiling. 3497 tv = STACK_TV_BOT(-1); 3498 li = list_find(tv->vval.v_list, index); 3499 3500 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 3501 goto failed; 3502 ++ectx.ec_stack.ga_len; 3503 copy_tv(&li->li_tv, STACK_TV_BOT(-1)); 3504 3505 // Useful when used in unpack assignment. Reset at 3506 // ISN_DROP. 3507 where.wt_index = index + 1; 3508 where.wt_variable = TRUE; 3509 } 3510 break; 3511 3512 case ISN_MEMBER: 3513 { 3514 dict_T *dict; 3515 char_u *key; 3516 dictitem_T *di; 3517 typval_T temp_tv; 3518 3519 // dict member: dict is at stack-2, key at stack-1 3520 tv = STACK_TV_BOT(-2); 3521 // no need to check for VAR_DICT, CHECKTYPE will check. 3522 dict = tv->vval.v_dict; 3523 3524 tv = STACK_TV_BOT(-1); 3525 // no need to check for VAR_STRING, 2STRING will check. 3526 key = tv->vval.v_string; 3527 if (key == NULL) 3528 key = (char_u *)""; 3529 3530 if ((di = dict_find(dict, key, -1)) == NULL) 3531 { 3532 SOURCING_LNUM = iptr->isn_lnum; 3533 semsg(_(e_dictkey), key); 3534 3535 // If :silent! is used we will continue, make sure the 3536 // stack contents makes sense. 3537 clear_tv(tv); 3538 --ectx.ec_stack.ga_len; 3539 tv = STACK_TV_BOT(-1); 3540 clear_tv(tv); 3541 tv->v_type = VAR_NUMBER; 3542 tv->vval.v_number = 0; 3543 goto on_fatal_error; 3544 } 3545 clear_tv(tv); 3546 --ectx.ec_stack.ga_len; 3547 // Clear the dict only after getting the item, to avoid 3548 // that it makes the item invalid. 3549 tv = STACK_TV_BOT(-1); 3550 temp_tv = *tv; 3551 copy_tv(&di->di_tv, tv); 3552 clear_tv(&temp_tv); 3553 } 3554 break; 3555 3556 // dict member with string key 3557 case ISN_STRINGMEMBER: 3558 { 3559 dict_T *dict; 3560 dictitem_T *di; 3561 typval_T temp_tv; 3562 3563 tv = STACK_TV_BOT(-1); 3564 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL) 3565 { 3566 SOURCING_LNUM = iptr->isn_lnum; 3567 emsg(_(e_dictreq)); 3568 goto on_error; 3569 } 3570 dict = tv->vval.v_dict; 3571 3572 if ((di = dict_find(dict, iptr->isn_arg.string, -1)) 3573 == NULL) 3574 { 3575 SOURCING_LNUM = iptr->isn_lnum; 3576 semsg(_(e_dictkey), iptr->isn_arg.string); 3577 goto on_error; 3578 } 3579 // Clear the dict after getting the item, to avoid that it 3580 // make the item invalid. 3581 temp_tv = *tv; 3582 copy_tv(&di->di_tv, tv); 3583 clear_tv(&temp_tv); 3584 } 3585 break; 3586 3587 case ISN_NEGATENR: 3588 tv = STACK_TV_BOT(-1); 3589 if (tv->v_type != VAR_NUMBER 3590 #ifdef FEAT_FLOAT 3591 && tv->v_type != VAR_FLOAT 3592 #endif 3593 ) 3594 { 3595 SOURCING_LNUM = iptr->isn_lnum; 3596 emsg(_(e_number_exp)); 3597 goto on_error; 3598 } 3599 #ifdef FEAT_FLOAT 3600 if (tv->v_type == VAR_FLOAT) 3601 tv->vval.v_float = -tv->vval.v_float; 3602 else 3603 #endif 3604 tv->vval.v_number = -tv->vval.v_number; 3605 break; 3606 3607 case ISN_CHECKNR: 3608 { 3609 int error = FALSE; 3610 3611 tv = STACK_TV_BOT(-1); 3612 SOURCING_LNUM = iptr->isn_lnum; 3613 if (check_not_string(tv) == FAIL) 3614 goto on_error; 3615 (void)tv_get_number_chk(tv, &error); 3616 if (error) 3617 goto on_error; 3618 } 3619 break; 3620 3621 case ISN_CHECKTYPE: 3622 { 3623 checktype_T *ct = &iptr->isn_arg.type; 3624 3625 tv = STACK_TV_BOT((int)ct->ct_off); 3626 SOURCING_LNUM = iptr->isn_lnum; 3627 if (!where.wt_variable) 3628 where.wt_index = ct->ct_arg_idx; 3629 if (check_typval_type(ct->ct_type, tv, where) == FAIL) 3630 goto on_error; 3631 if (!where.wt_variable) 3632 where.wt_index = 0; 3633 3634 // number 0 is FALSE, number 1 is TRUE 3635 if (tv->v_type == VAR_NUMBER 3636 && ct->ct_type->tt_type == VAR_BOOL 3637 && (tv->vval.v_number == 0 3638 || tv->vval.v_number == 1)) 3639 { 3640 tv->v_type = VAR_BOOL; 3641 tv->vval.v_number = tv->vval.v_number 3642 ? VVAL_TRUE : VVAL_FALSE; 3643 } 3644 } 3645 break; 3646 3647 case ISN_CHECKLEN: 3648 { 3649 int min_len = iptr->isn_arg.checklen.cl_min_len; 3650 list_T *list = NULL; 3651 3652 tv = STACK_TV_BOT(-1); 3653 if (tv->v_type == VAR_LIST) 3654 list = tv->vval.v_list; 3655 if (list == NULL || list->lv_len < min_len 3656 || (list->lv_len > min_len 3657 && !iptr->isn_arg.checklen.cl_more_OK)) 3658 { 3659 SOURCING_LNUM = iptr->isn_lnum; 3660 semsg(_(e_expected_nr_items_but_got_nr), 3661 min_len, list == NULL ? 0 : list->lv_len); 3662 goto on_error; 3663 } 3664 } 3665 break; 3666 3667 case ISN_SETTYPE: 3668 { 3669 checktype_T *ct = &iptr->isn_arg.type; 3670 3671 tv = STACK_TV_BOT(-1); 3672 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) 3673 { 3674 free_type(tv->vval.v_dict->dv_type); 3675 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type); 3676 } 3677 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL) 3678 { 3679 free_type(tv->vval.v_list->lv_type); 3680 tv->vval.v_list->lv_type = alloc_type(ct->ct_type); 3681 } 3682 } 3683 break; 3684 3685 case ISN_2BOOL: 3686 case ISN_COND2BOOL: 3687 { 3688 int n; 3689 int error = FALSE; 3690 3691 tv = STACK_TV_BOT(-1); 3692 if (iptr->isn_type == ISN_2BOOL) 3693 { 3694 n = tv2bool(tv); 3695 if (iptr->isn_arg.number) // invert 3696 n = !n; 3697 } 3698 else 3699 { 3700 SOURCING_LNUM = iptr->isn_lnum; 3701 n = tv_get_bool_chk(tv, &error); 3702 if (error) 3703 goto on_error; 3704 } 3705 clear_tv(tv); 3706 tv->v_type = VAR_BOOL; 3707 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE; 3708 } 3709 break; 3710 3711 case ISN_2STRING: 3712 case ISN_2STRING_ANY: 3713 SOURCING_LNUM = iptr->isn_lnum; 3714 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number), 3715 iptr->isn_type == ISN_2STRING_ANY) == FAIL) 3716 goto on_error; 3717 break; 3718 3719 case ISN_RANGE: 3720 { 3721 exarg_T ea; 3722 char *errormsg; 3723 3724 ea.line2 = 0; 3725 ea.addr_count = 0; 3726 ea.addr_type = ADDR_LINES; 3727 ea.cmd = iptr->isn_arg.string; 3728 ea.skip = FALSE; 3729 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL) 3730 goto on_error; 3731 3732 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 3733 goto failed; 3734 ++ectx.ec_stack.ga_len; 3735 tv = STACK_TV_BOT(-1); 3736 tv->v_type = VAR_NUMBER; 3737 tv->v_lock = 0; 3738 if (ea.addr_count == 0) 3739 tv->vval.v_number = curwin->w_cursor.lnum; 3740 else 3741 tv->vval.v_number = ea.line2; 3742 } 3743 break; 3744 3745 case ISN_PUT: 3746 { 3747 int regname = iptr->isn_arg.put.put_regname; 3748 linenr_T lnum = iptr->isn_arg.put.put_lnum; 3749 char_u *expr = NULL; 3750 int dir = FORWARD; 3751 3752 if (lnum < -2) 3753 { 3754 // line number was put on the stack by ISN_RANGE 3755 tv = STACK_TV_BOT(-1); 3756 curwin->w_cursor.lnum = tv->vval.v_number; 3757 if (lnum == LNUM_VARIABLE_RANGE_ABOVE) 3758 dir = BACKWARD; 3759 --ectx.ec_stack.ga_len; 3760 } 3761 else if (lnum == -2) 3762 // :put! above cursor 3763 dir = BACKWARD; 3764 else if (lnum >= 0) 3765 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum; 3766 3767 if (regname == '=') 3768 { 3769 tv = STACK_TV_BOT(-1); 3770 if (tv->v_type == VAR_STRING) 3771 expr = tv->vval.v_string; 3772 else 3773 { 3774 expr = typval2string(tv, TRUE); // allocates value 3775 clear_tv(tv); 3776 } 3777 --ectx.ec_stack.ga_len; 3778 } 3779 check_cursor(); 3780 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE); 3781 vim_free(expr); 3782 } 3783 break; 3784 3785 case ISN_CMDMOD: 3786 funclocal.floc_save_cmdmod = cmdmod; 3787 funclocal.floc_restore_cmdmod = TRUE; 3788 funclocal.floc_restore_cmdmod_stacklen = ectx.ec_stack.ga_len; 3789 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod; 3790 apply_cmdmod(&cmdmod); 3791 break; 3792 3793 case ISN_CMDMOD_REV: 3794 // filter regprog is owned by the instruction, don't free it 3795 cmdmod.cmod_filter_regmatch.regprog = NULL; 3796 undo_cmdmod(&cmdmod); 3797 cmdmod = funclocal.floc_save_cmdmod; 3798 funclocal.floc_restore_cmdmod = FALSE; 3799 break; 3800 3801 case ISN_UNPACK: 3802 { 3803 int count = iptr->isn_arg.unpack.unp_count; 3804 int semicolon = iptr->isn_arg.unpack.unp_semicolon; 3805 list_T *l; 3806 listitem_T *li; 3807 int i; 3808 3809 // Check there is a valid list to unpack. 3810 tv = STACK_TV_BOT(-1); 3811 if (tv->v_type != VAR_LIST) 3812 { 3813 SOURCING_LNUM = iptr->isn_lnum; 3814 emsg(_(e_for_argument_must_be_sequence_of_lists)); 3815 goto on_error; 3816 } 3817 l = tv->vval.v_list; 3818 if (l == NULL 3819 || l->lv_len < (semicolon ? count - 1 : count)) 3820 { 3821 SOURCING_LNUM = iptr->isn_lnum; 3822 emsg(_(e_list_value_does_not_have_enough_items)); 3823 goto on_error; 3824 } 3825 else if (!semicolon && l->lv_len > count) 3826 { 3827 SOURCING_LNUM = iptr->isn_lnum; 3828 emsg(_(e_list_value_has_more_items_than_targets)); 3829 goto on_error; 3830 } 3831 3832 CHECK_LIST_MATERIALIZE(l); 3833 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL) 3834 goto failed; 3835 ectx.ec_stack.ga_len += count - 1; 3836 3837 // Variable after semicolon gets a list with the remaining 3838 // items. 3839 if (semicolon) 3840 { 3841 list_T *rem_list = 3842 list_alloc_with_items(l->lv_len - count + 1); 3843 3844 if (rem_list == NULL) 3845 goto failed; 3846 tv = STACK_TV_BOT(-count); 3847 tv->vval.v_list = rem_list; 3848 ++rem_list->lv_refcount; 3849 tv->v_lock = 0; 3850 li = l->lv_first; 3851 for (i = 0; i < count - 1; ++i) 3852 li = li->li_next; 3853 for (i = 0; li != NULL; ++i) 3854 { 3855 list_set_item(rem_list, i, &li->li_tv); 3856 li = li->li_next; 3857 } 3858 --count; 3859 } 3860 3861 // Produce the values in reverse order, first item last. 3862 li = l->lv_first; 3863 for (i = 0; i < count; ++i) 3864 { 3865 tv = STACK_TV_BOT(-i - 1); 3866 copy_tv(&li->li_tv, tv); 3867 li = li->li_next; 3868 } 3869 3870 list_unref(l); 3871 } 3872 break; 3873 3874 case ISN_PROF_START: 3875 case ISN_PROF_END: 3876 { 3877 #ifdef FEAT_PROFILE 3878 funccall_T cookie; 3879 ufunc_T *cur_ufunc = 3880 (((dfunc_T *)def_functions.ga_data) 3881 + ectx.ec_dfunc_idx)->df_ufunc; 3882 3883 cookie.func = cur_ufunc; 3884 if (iptr->isn_type == ISN_PROF_START) 3885 { 3886 func_line_start(&cookie, iptr->isn_lnum); 3887 // if we get here the instruction is executed 3888 func_line_exec(&cookie); 3889 } 3890 else 3891 func_line_end(&cookie); 3892 #endif 3893 } 3894 break; 3895 3896 case ISN_SHUFFLE: 3897 { 3898 typval_T tmp_tv; 3899 int item = iptr->isn_arg.shuffle.shfl_item; 3900 int up = iptr->isn_arg.shuffle.shfl_up; 3901 3902 tmp_tv = *STACK_TV_BOT(-item); 3903 for ( ; up > 0 && item > 1; --up) 3904 { 3905 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1); 3906 --item; 3907 } 3908 *STACK_TV_BOT(-item) = tmp_tv; 3909 } 3910 break; 3911 3912 case ISN_DROP: 3913 --ectx.ec_stack.ga_len; 3914 clear_tv(STACK_TV_BOT(0)); 3915 where.wt_index = 0; 3916 where.wt_variable = FALSE; 3917 break; 3918 } 3919 continue; 3920 3921 func_return: 3922 // Restore previous function. If the frame pointer is where we started 3923 // then there is none and we are done. 3924 if (ectx.ec_frame_idx == initial_frame_idx) 3925 goto done; 3926 3927 if (func_return(&funclocal, &ectx) == FAIL) 3928 // only fails when out of memory 3929 goto failed; 3930 continue; 3931 3932 on_error: 3933 // Jump here for an error that does not require aborting execution. 3934 // If "emsg_silent" is set then ignore the error, unless it was set 3935 // when calling the function. 3936 if (did_emsg_cumul + did_emsg == did_emsg_before 3937 && emsg_silent && did_emsg_def == 0) 3938 { 3939 // If a sequence of instructions causes an error while ":silent!" 3940 // was used, restore the stack length and jump ahead to restoring 3941 // the cmdmod. 3942 if (funclocal.floc_restore_cmdmod) 3943 { 3944 while (ectx.ec_stack.ga_len 3945 > funclocal.floc_restore_cmdmod_stacklen) 3946 { 3947 --ectx.ec_stack.ga_len; 3948 clear_tv(STACK_TV_BOT(0)); 3949 } 3950 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV) 3951 ++ectx.ec_iidx; 3952 } 3953 continue; 3954 } 3955 on_fatal_error: 3956 // Jump here for an error that messes up the stack. 3957 // If we are not inside a try-catch started here, abort execution. 3958 if (trylevel <= trylevel_at_start) 3959 goto failed; 3960 } 3961 3962 done: 3963 // function finished, get result from the stack. 3964 if (ufunc->uf_ret_type == &t_void) 3965 { 3966 rettv->v_type = VAR_VOID; 3967 } 3968 else 3969 { 3970 tv = STACK_TV_BOT(-1); 3971 *rettv = *tv; 3972 tv->v_type = VAR_UNKNOWN; 3973 } 3974 ret = OK; 3975 3976 failed: 3977 // When failed need to unwind the call stack. 3978 while (ectx.ec_frame_idx != initial_frame_idx) 3979 func_return(&funclocal, &ectx); 3980 3981 // Deal with any remaining closures, they may be in use somewhere. 3982 if (ectx.ec_funcrefs.ga_len > 0) 3983 { 3984 handle_closure_in_use(&ectx, FALSE); 3985 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed? 3986 } 3987 3988 estack_pop(); 3989 current_sctx = save_current_sctx; 3990 3991 // TODO: when is it safe to delete the function if it is no longer used? 3992 --ufunc->uf_calls; 3993 3994 if (*msg_list != NULL && saved_msg_list != NULL) 3995 { 3996 msglist_T **plist = saved_msg_list; 3997 3998 // Append entries from the current msg_list (uncaught exceptions) to 3999 // the saved msg_list. 4000 while (*plist != NULL) 4001 plist = &(*plist)->next; 4002 4003 *plist = *msg_list; 4004 } 4005 msg_list = saved_msg_list; 4006 4007 if (funclocal.floc_restore_cmdmod) 4008 { 4009 cmdmod.cmod_filter_regmatch.regprog = NULL; 4010 undo_cmdmod(&cmdmod); 4011 cmdmod = funclocal.floc_save_cmdmod; 4012 } 4013 emsg_silent_def = save_emsg_silent_def; 4014 did_emsg_def += save_did_emsg_def; 4015 4016 failed_early: 4017 // Free all local variables, but not arguments. 4018 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx) 4019 clear_tv(STACK_TV(idx)); 4020 4021 vim_free(ectx.ec_stack.ga_data); 4022 vim_free(ectx.ec_trystack.ga_data); 4023 4024 while (ectx.ec_outer != NULL) 4025 { 4026 outer_T *up = ectx.ec_outer->out_up_is_copy 4027 ? NULL : ectx.ec_outer->out_up; 4028 4029 vim_free(ectx.ec_outer); 4030 ectx.ec_outer = up; 4031 } 4032 4033 // Not sure if this is necessary. 4034 suppress_errthrow = save_suppress_errthrow; 4035 4036 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before) 4037 semsg(_(e_unknown_error_while_executing_str), 4038 printable_func_name(ufunc)); 4039 funcdepth_restore(orig_funcdepth); 4040 return ret; 4041 } 4042 4043 /* 4044 * ":disassemble". 4045 * We don't really need this at runtime, but we do have tests that require it, 4046 * so always include this. 4047 */ 4048 void 4049 ex_disassemble(exarg_T *eap) 4050 { 4051 char_u *arg = eap->arg; 4052 char_u *fname; 4053 ufunc_T *ufunc; 4054 dfunc_T *dfunc; 4055 isn_T *instr; 4056 int instr_count; 4057 int current; 4058 int line_idx = 0; 4059 int prev_current = 0; 4060 int is_global = FALSE; 4061 4062 if (STRNCMP(arg, "<lambda>", 8) == 0) 4063 { 4064 arg += 8; 4065 (void)getdigits(&arg); 4066 fname = vim_strnsave(eap->arg, arg - eap->arg); 4067 } 4068 else 4069 fname = trans_function_name(&arg, &is_global, FALSE, 4070 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL); 4071 if (fname == NULL) 4072 { 4073 semsg(_(e_invarg2), eap->arg); 4074 return; 4075 } 4076 4077 ufunc = find_func(fname, is_global, NULL); 4078 if (ufunc == NULL) 4079 { 4080 char_u *p = untrans_function_name(fname); 4081 4082 if (p != NULL) 4083 // Try again without making it script-local. 4084 ufunc = find_func(p, FALSE, NULL); 4085 } 4086 vim_free(fname); 4087 if (ufunc == NULL) 4088 { 4089 semsg(_(e_cannot_find_function_str), eap->arg); 4090 return; 4091 } 4092 if (func_needs_compiling(ufunc, eap->forceit) 4093 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL) 4094 return; 4095 if (ufunc->uf_def_status != UF_COMPILED) 4096 { 4097 semsg(_(e_function_is_not_compiled_str), eap->arg); 4098 return; 4099 } 4100 if (ufunc->uf_name_exp != NULL) 4101 msg((char *)ufunc->uf_name_exp); 4102 else 4103 msg((char *)ufunc->uf_name); 4104 4105 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; 4106 #ifdef FEAT_PROFILE 4107 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr; 4108 instr_count = eap->forceit ? dfunc->df_instr_prof_count 4109 : dfunc->df_instr_count; 4110 #else 4111 instr = dfunc->df_instr; 4112 instr_count = dfunc->df_instr_count; 4113 #endif 4114 for (current = 0; current < instr_count; ++current) 4115 { 4116 isn_T *iptr = &instr[current]; 4117 char *line; 4118 4119 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len) 4120 { 4121 if (current > prev_current) 4122 { 4123 msg_puts("\n\n"); 4124 prev_current = current; 4125 } 4126 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++]; 4127 if (line != NULL) 4128 msg(line); 4129 } 4130 4131 switch (iptr->isn_type) 4132 { 4133 case ISN_EXEC: 4134 smsg("%4d EXEC %s", current, iptr->isn_arg.string); 4135 break; 4136 case ISN_EXECCONCAT: 4137 smsg("%4d EXECCONCAT %lld", current, 4138 (varnumber_T)iptr->isn_arg.number); 4139 break; 4140 case ISN_ECHO: 4141 { 4142 echo_T *echo = &iptr->isn_arg.echo; 4143 4144 smsg("%4d %s %d", current, 4145 echo->echo_with_white ? "ECHO" : "ECHON", 4146 echo->echo_count); 4147 } 4148 break; 4149 case ISN_EXECUTE: 4150 smsg("%4d EXECUTE %lld", current, 4151 (varnumber_T)(iptr->isn_arg.number)); 4152 break; 4153 case ISN_ECHOMSG: 4154 smsg("%4d ECHOMSG %lld", current, 4155 (varnumber_T)(iptr->isn_arg.number)); 4156 break; 4157 case ISN_ECHOERR: 4158 smsg("%4d ECHOERR %lld", current, 4159 (varnumber_T)(iptr->isn_arg.number)); 4160 break; 4161 case ISN_LOAD: 4162 { 4163 if (iptr->isn_arg.number < 0) 4164 smsg("%4d LOAD arg[%lld]", current, 4165 (varnumber_T)(iptr->isn_arg.number 4166 + STACK_FRAME_SIZE)); 4167 else 4168 smsg("%4d LOAD $%lld", current, 4169 (varnumber_T)(iptr->isn_arg.number)); 4170 } 4171 break; 4172 case ISN_LOADOUTER: 4173 { 4174 if (iptr->isn_arg.number < 0) 4175 smsg("%4d LOADOUTER level %d arg[%d]", current, 4176 iptr->isn_arg.outer.outer_depth, 4177 iptr->isn_arg.outer.outer_idx 4178 + STACK_FRAME_SIZE); 4179 else 4180 smsg("%4d LOADOUTER level %d $%d", current, 4181 iptr->isn_arg.outer.outer_depth, 4182 iptr->isn_arg.outer.outer_idx); 4183 } 4184 break; 4185 case ISN_LOADV: 4186 smsg("%4d LOADV v:%s", current, 4187 get_vim_var_name(iptr->isn_arg.number)); 4188 break; 4189 case ISN_LOADSCRIPT: 4190 { 4191 scriptref_T *sref = iptr->isn_arg.script.scriptref; 4192 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); 4193 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 4194 + sref->sref_idx; 4195 4196 smsg("%4d LOADSCRIPT %s-%d from %s", current, 4197 sv->sv_name, 4198 sref->sref_idx, 4199 si->sn_name); 4200 } 4201 break; 4202 case ISN_LOADS: 4203 { 4204 scriptitem_T *si = SCRIPT_ITEM( 4205 iptr->isn_arg.loadstore.ls_sid); 4206 4207 smsg("%4d LOADS s:%s from %s", current, 4208 iptr->isn_arg.loadstore.ls_name, si->sn_name); 4209 } 4210 break; 4211 case ISN_LOADAUTO: 4212 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string); 4213 break; 4214 case ISN_LOADG: 4215 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string); 4216 break; 4217 case ISN_LOADB: 4218 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string); 4219 break; 4220 case ISN_LOADW: 4221 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string); 4222 break; 4223 case ISN_LOADT: 4224 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string); 4225 break; 4226 case ISN_LOADGDICT: 4227 smsg("%4d LOAD g:", current); 4228 break; 4229 case ISN_LOADBDICT: 4230 smsg("%4d LOAD b:", current); 4231 break; 4232 case ISN_LOADWDICT: 4233 smsg("%4d LOAD w:", current); 4234 break; 4235 case ISN_LOADTDICT: 4236 smsg("%4d LOAD t:", current); 4237 break; 4238 case ISN_LOADOPT: 4239 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string); 4240 break; 4241 case ISN_LOADENV: 4242 smsg("%4d LOADENV %s", current, iptr->isn_arg.string); 4243 break; 4244 case ISN_LOADREG: 4245 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number)); 4246 break; 4247 4248 case ISN_STORE: 4249 if (iptr->isn_arg.number < 0) 4250 smsg("%4d STORE arg[%lld]", current, 4251 iptr->isn_arg.number + STACK_FRAME_SIZE); 4252 else 4253 smsg("%4d STORE $%lld", current, iptr->isn_arg.number); 4254 break; 4255 case ISN_STOREOUTER: 4256 { 4257 if (iptr->isn_arg.number < 0) 4258 smsg("%4d STOREOUTEr level %d arg[%d]", current, 4259 iptr->isn_arg.outer.outer_depth, 4260 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE); 4261 else 4262 smsg("%4d STOREOUTER level %d $%d", current, 4263 iptr->isn_arg.outer.outer_depth, 4264 iptr->isn_arg.outer.outer_idx); 4265 } 4266 break; 4267 case ISN_STOREV: 4268 smsg("%4d STOREV v:%s", current, 4269 get_vim_var_name(iptr->isn_arg.number)); 4270 break; 4271 case ISN_STOREAUTO: 4272 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string); 4273 break; 4274 case ISN_STOREG: 4275 smsg("%4d STOREG %s", current, iptr->isn_arg.string); 4276 break; 4277 case ISN_STOREB: 4278 smsg("%4d STOREB %s", current, iptr->isn_arg.string); 4279 break; 4280 case ISN_STOREW: 4281 smsg("%4d STOREW %s", current, iptr->isn_arg.string); 4282 break; 4283 case ISN_STORET: 4284 smsg("%4d STORET %s", current, iptr->isn_arg.string); 4285 break; 4286 case ISN_STORES: 4287 { 4288 scriptitem_T *si = SCRIPT_ITEM( 4289 iptr->isn_arg.loadstore.ls_sid); 4290 4291 smsg("%4d STORES %s in %s", current, 4292 iptr->isn_arg.loadstore.ls_name, si->sn_name); 4293 } 4294 break; 4295 case ISN_STORESCRIPT: 4296 { 4297 scriptref_T *sref = iptr->isn_arg.script.scriptref; 4298 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); 4299 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 4300 + sref->sref_idx; 4301 4302 smsg("%4d STORESCRIPT %s-%d in %s", current, 4303 sv->sv_name, 4304 sref->sref_idx, 4305 si->sn_name); 4306 } 4307 break; 4308 case ISN_STOREOPT: 4309 smsg("%4d STOREOPT &%s", current, 4310 iptr->isn_arg.storeopt.so_name); 4311 break; 4312 case ISN_STOREENV: 4313 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string); 4314 break; 4315 case ISN_STOREREG: 4316 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number); 4317 break; 4318 case ISN_STORENR: 4319 smsg("%4d STORE %lld in $%d", current, 4320 iptr->isn_arg.storenr.stnr_val, 4321 iptr->isn_arg.storenr.stnr_idx); 4322 break; 4323 4324 case ISN_STOREINDEX: 4325 switch (iptr->isn_arg.vartype) 4326 { 4327 case VAR_LIST: 4328 smsg("%4d STORELIST", current); 4329 break; 4330 case VAR_DICT: 4331 smsg("%4d STOREDICT", current); 4332 break; 4333 case VAR_ANY: 4334 smsg("%4d STOREINDEX", current); 4335 break; 4336 default: break; 4337 } 4338 break; 4339 4340 // constants 4341 case ISN_PUSHNR: 4342 smsg("%4d PUSHNR %lld", current, 4343 (varnumber_T)(iptr->isn_arg.number)); 4344 break; 4345 case ISN_PUSHBOOL: 4346 case ISN_PUSHSPEC: 4347 smsg("%4d PUSH %s", current, 4348 get_var_special_name(iptr->isn_arg.number)); 4349 break; 4350 case ISN_PUSHF: 4351 #ifdef FEAT_FLOAT 4352 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber); 4353 #endif 4354 break; 4355 case ISN_PUSHS: 4356 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string); 4357 break; 4358 case ISN_PUSHBLOB: 4359 { 4360 char_u *r; 4361 char_u numbuf[NUMBUFLEN]; 4362 char_u *tofree; 4363 4364 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf); 4365 smsg("%4d PUSHBLOB %s", current, r); 4366 vim_free(tofree); 4367 } 4368 break; 4369 case ISN_PUSHFUNC: 4370 { 4371 char *name = (char *)iptr->isn_arg.string; 4372 4373 smsg("%4d PUSHFUNC \"%s\"", current, 4374 name == NULL ? "[none]" : name); 4375 } 4376 break; 4377 case ISN_PUSHCHANNEL: 4378 #ifdef FEAT_JOB_CHANNEL 4379 { 4380 channel_T *channel = iptr->isn_arg.channel; 4381 4382 smsg("%4d PUSHCHANNEL %d", current, 4383 channel == NULL ? 0 : channel->ch_id); 4384 } 4385 #endif 4386 break; 4387 case ISN_PUSHJOB: 4388 #ifdef FEAT_JOB_CHANNEL 4389 { 4390 typval_T tv; 4391 char_u *name; 4392 4393 tv.v_type = VAR_JOB; 4394 tv.vval.v_job = iptr->isn_arg.job; 4395 name = tv_get_string(&tv); 4396 smsg("%4d PUSHJOB \"%s\"", current, name); 4397 } 4398 #endif 4399 break; 4400 case ISN_PUSHEXC: 4401 smsg("%4d PUSH v:exception", current); 4402 break; 4403 case ISN_UNLET: 4404 smsg("%4d UNLET%s %s", current, 4405 iptr->isn_arg.unlet.ul_forceit ? "!" : "", 4406 iptr->isn_arg.unlet.ul_name); 4407 break; 4408 case ISN_UNLETENV: 4409 smsg("%4d UNLETENV%s $%s", current, 4410 iptr->isn_arg.unlet.ul_forceit ? "!" : "", 4411 iptr->isn_arg.unlet.ul_name); 4412 break; 4413 case ISN_UNLETINDEX: 4414 smsg("%4d UNLETINDEX", current); 4415 break; 4416 case ISN_UNLETRANGE: 4417 smsg("%4d UNLETRANGE", current); 4418 break; 4419 case ISN_LOCKCONST: 4420 smsg("%4d LOCKCONST", current); 4421 break; 4422 case ISN_NEWLIST: 4423 smsg("%4d NEWLIST size %lld", current, 4424 (varnumber_T)(iptr->isn_arg.number)); 4425 break; 4426 case ISN_NEWDICT: 4427 smsg("%4d NEWDICT size %lld", current, 4428 (varnumber_T)(iptr->isn_arg.number)); 4429 break; 4430 4431 // function call 4432 case ISN_BCALL: 4433 { 4434 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc; 4435 4436 smsg("%4d BCALL %s(argc %d)", current, 4437 internal_func_name(cbfunc->cbf_idx), 4438 cbfunc->cbf_argcount); 4439 } 4440 break; 4441 case ISN_DCALL: 4442 { 4443 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc; 4444 dfunc_T *df = ((dfunc_T *)def_functions.ga_data) 4445 + cdfunc->cdf_idx; 4446 4447 smsg("%4d DCALL %s(argc %d)", current, 4448 df->df_ufunc->uf_name_exp != NULL 4449 ? df->df_ufunc->uf_name_exp 4450 : df->df_ufunc->uf_name, cdfunc->cdf_argcount); 4451 } 4452 break; 4453 case ISN_UCALL: 4454 { 4455 cufunc_T *cufunc = &iptr->isn_arg.ufunc; 4456 4457 smsg("%4d UCALL %s(argc %d)", current, 4458 cufunc->cuf_name, cufunc->cuf_argcount); 4459 } 4460 break; 4461 case ISN_PCALL: 4462 { 4463 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc; 4464 4465 smsg("%4d PCALL%s (argc %d)", current, 4466 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount); 4467 } 4468 break; 4469 case ISN_PCALL_END: 4470 smsg("%4d PCALL end", current); 4471 break; 4472 case ISN_RETURN: 4473 smsg("%4d RETURN", current); 4474 break; 4475 case ISN_RETURN_ZERO: 4476 smsg("%4d RETURN 0", current); 4477 break; 4478 case ISN_FUNCREF: 4479 { 4480 funcref_T *funcref = &iptr->isn_arg.funcref; 4481 dfunc_T *df = ((dfunc_T *)def_functions.ga_data) 4482 + funcref->fr_func; 4483 4484 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name); 4485 } 4486 break; 4487 4488 case ISN_NEWFUNC: 4489 { 4490 newfunc_T *newfunc = &iptr->isn_arg.newfunc; 4491 4492 smsg("%4d NEWFUNC %s %s", current, 4493 newfunc->nf_lambda, newfunc->nf_global); 4494 } 4495 break; 4496 4497 case ISN_DEF: 4498 { 4499 char_u *name = iptr->isn_arg.string; 4500 4501 smsg("%4d DEF %s", current, 4502 name == NULL ? (char_u *)"" : name); 4503 } 4504 break; 4505 4506 case ISN_JUMP: 4507 { 4508 char *when = "?"; 4509 4510 switch (iptr->isn_arg.jump.jump_when) 4511 { 4512 case JUMP_ALWAYS: 4513 when = "JUMP"; 4514 break; 4515 case JUMP_AND_KEEP_IF_TRUE: 4516 when = "JUMP_AND_KEEP_IF_TRUE"; 4517 break; 4518 case JUMP_IF_FALSE: 4519 when = "JUMP_IF_FALSE"; 4520 break; 4521 case JUMP_AND_KEEP_IF_FALSE: 4522 when = "JUMP_AND_KEEP_IF_FALSE"; 4523 break; 4524 case JUMP_IF_COND_FALSE: 4525 when = "JUMP_IF_COND_FALSE"; 4526 break; 4527 case JUMP_IF_COND_TRUE: 4528 when = "JUMP_IF_COND_TRUE"; 4529 break; 4530 } 4531 smsg("%4d %s -> %d", current, when, 4532 iptr->isn_arg.jump.jump_where); 4533 } 4534 break; 4535 4536 case ISN_JUMP_IF_ARG_SET: 4537 smsg("%4d JUMP_IF_ARG_SET arg[%d] -> %d", current, 4538 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE, 4539 iptr->isn_arg.jump.jump_where); 4540 break; 4541 4542 case ISN_FOR: 4543 { 4544 forloop_T *forloop = &iptr->isn_arg.forloop; 4545 4546 smsg("%4d FOR $%d -> %d", current, 4547 forloop->for_idx, forloop->for_end); 4548 } 4549 break; 4550 4551 case ISN_TRY: 4552 { 4553 try_T *try = &iptr->isn_arg.try; 4554 4555 if (try->try_ref->try_finally == 0) 4556 smsg("%4d TRY catch -> %d, endtry -> %d", 4557 current, 4558 try->try_ref->try_catch, 4559 try->try_ref->try_endtry); 4560 else 4561 smsg("%4d TRY catch -> %d, finally -> %d, endtry -> %d", 4562 current, 4563 try->try_ref->try_catch, 4564 try->try_ref->try_finally, 4565 try->try_ref->try_endtry); 4566 } 4567 break; 4568 case ISN_CATCH: 4569 // TODO 4570 smsg("%4d CATCH", current); 4571 break; 4572 case ISN_TRYCONT: 4573 { 4574 trycont_T *trycont = &iptr->isn_arg.trycont; 4575 4576 smsg("%4d TRY-CONTINUE %d level%s -> %d", current, 4577 trycont->tct_levels, 4578 trycont->tct_levels == 1 ? "" : "s", 4579 trycont->tct_where); 4580 } 4581 break; 4582 case ISN_FINALLY: 4583 smsg("%4d FINALLY", current); 4584 break; 4585 case ISN_ENDTRY: 4586 smsg("%4d ENDTRY", current); 4587 break; 4588 case ISN_THROW: 4589 smsg("%4d THROW", current); 4590 break; 4591 4592 // expression operations on number 4593 case ISN_OPNR: 4594 case ISN_OPFLOAT: 4595 case ISN_OPANY: 4596 { 4597 char *what; 4598 char *ins; 4599 4600 switch (iptr->isn_arg.op.op_type) 4601 { 4602 case EXPR_MULT: what = "*"; break; 4603 case EXPR_DIV: what = "/"; break; 4604 case EXPR_REM: what = "%"; break; 4605 case EXPR_SUB: what = "-"; break; 4606 case EXPR_ADD: what = "+"; break; 4607 default: what = "???"; break; 4608 } 4609 switch (iptr->isn_type) 4610 { 4611 case ISN_OPNR: ins = "OPNR"; break; 4612 case ISN_OPFLOAT: ins = "OPFLOAT"; break; 4613 case ISN_OPANY: ins = "OPANY"; break; 4614 default: ins = "???"; break; 4615 } 4616 smsg("%4d %s %s", current, ins, what); 4617 } 4618 break; 4619 4620 case ISN_COMPAREBOOL: 4621 case ISN_COMPARESPECIAL: 4622 case ISN_COMPARENR: 4623 case ISN_COMPAREFLOAT: 4624 case ISN_COMPARESTRING: 4625 case ISN_COMPAREBLOB: 4626 case ISN_COMPARELIST: 4627 case ISN_COMPAREDICT: 4628 case ISN_COMPAREFUNC: 4629 case ISN_COMPAREANY: 4630 { 4631 char *p; 4632 char buf[10]; 4633 char *type; 4634 4635 switch (iptr->isn_arg.op.op_type) 4636 { 4637 case EXPR_EQUAL: p = "=="; break; 4638 case EXPR_NEQUAL: p = "!="; break; 4639 case EXPR_GREATER: p = ">"; break; 4640 case EXPR_GEQUAL: p = ">="; break; 4641 case EXPR_SMALLER: p = "<"; break; 4642 case EXPR_SEQUAL: p = "<="; break; 4643 case EXPR_MATCH: p = "=~"; break; 4644 case EXPR_IS: p = "is"; break; 4645 case EXPR_ISNOT: p = "isnot"; break; 4646 case EXPR_NOMATCH: p = "!~"; break; 4647 default: p = "???"; break; 4648 } 4649 STRCPY(buf, p); 4650 if (iptr->isn_arg.op.op_ic == TRUE) 4651 strcat(buf, "?"); 4652 switch(iptr->isn_type) 4653 { 4654 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break; 4655 case ISN_COMPARESPECIAL: 4656 type = "COMPARESPECIAL"; break; 4657 case ISN_COMPARENR: type = "COMPARENR"; break; 4658 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break; 4659 case ISN_COMPARESTRING: 4660 type = "COMPARESTRING"; break; 4661 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break; 4662 case ISN_COMPARELIST: type = "COMPARELIST"; break; 4663 case ISN_COMPAREDICT: type = "COMPAREDICT"; break; 4664 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break; 4665 case ISN_COMPAREANY: type = "COMPAREANY"; break; 4666 default: type = "???"; break; 4667 } 4668 4669 smsg("%4d %s %s", current, type, buf); 4670 } 4671 break; 4672 4673 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break; 4674 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break; 4675 4676 // expression operations 4677 case ISN_CONCAT: smsg("%4d CONCAT", current); break; 4678 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break; 4679 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break; 4680 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break; 4681 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break; 4682 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break; 4683 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break; 4684 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break; 4685 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break; 4686 case ISN_SLICE: smsg("%4d SLICE %lld", 4687 current, iptr->isn_arg.number); break; 4688 case ISN_GETITEM: smsg("%4d ITEM %lld", 4689 current, iptr->isn_arg.number); break; 4690 case ISN_MEMBER: smsg("%4d MEMBER", current); break; 4691 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current, 4692 iptr->isn_arg.string); break; 4693 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break; 4694 4695 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break; 4696 case ISN_CHECKTYPE: 4697 { 4698 checktype_T *ct = &iptr->isn_arg.type; 4699 char *tofree; 4700 4701 if (ct->ct_arg_idx == 0) 4702 smsg("%4d CHECKTYPE %s stack[%d]", current, 4703 type_name(ct->ct_type, &tofree), 4704 (int)ct->ct_off); 4705 else 4706 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current, 4707 type_name(ct->ct_type, &tofree), 4708 (int)ct->ct_off, 4709 (int)ct->ct_arg_idx); 4710 vim_free(tofree); 4711 break; 4712 } 4713 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current, 4714 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "", 4715 iptr->isn_arg.checklen.cl_min_len); 4716 break; 4717 case ISN_SETTYPE: 4718 { 4719 char *tofree; 4720 4721 smsg("%4d SETTYPE %s", current, 4722 type_name(iptr->isn_arg.type.ct_type, &tofree)); 4723 vim_free(tofree); 4724 break; 4725 } 4726 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break; 4727 case ISN_2BOOL: if (iptr->isn_arg.number) 4728 smsg("%4d INVERT (!val)", current); 4729 else 4730 smsg("%4d 2BOOL (!!val)", current); 4731 break; 4732 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current, 4733 (varnumber_T)(iptr->isn_arg.number)); 4734 break; 4735 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current, 4736 (varnumber_T)(iptr->isn_arg.number)); 4737 break; 4738 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string); 4739 break; 4740 case ISN_PUT: 4741 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE) 4742 smsg("%4d PUT %c above range", 4743 current, iptr->isn_arg.put.put_regname); 4744 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE) 4745 smsg("%4d PUT %c range", 4746 current, iptr->isn_arg.put.put_regname); 4747 else 4748 smsg("%4d PUT %c %ld", current, 4749 iptr->isn_arg.put.put_regname, 4750 (long)iptr->isn_arg.put.put_lnum); 4751 break; 4752 4753 // TODO: summarize modifiers 4754 case ISN_CMDMOD: 4755 { 4756 char_u *buf; 4757 size_t len = produce_cmdmods( 4758 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE); 4759 4760 buf = alloc(len + 1); 4761 if (buf != NULL) 4762 { 4763 (void)produce_cmdmods( 4764 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE); 4765 smsg("%4d CMDMOD %s", current, buf); 4766 vim_free(buf); 4767 } 4768 break; 4769 } 4770 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break; 4771 4772 case ISN_PROF_START: 4773 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum); 4774 break; 4775 4776 case ISN_PROF_END: 4777 smsg("%4d PROFILE END", current); 4778 break; 4779 4780 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current, 4781 iptr->isn_arg.unpack.unp_count, 4782 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : ""); 4783 break; 4784 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current, 4785 iptr->isn_arg.shuffle.shfl_item, 4786 iptr->isn_arg.shuffle.shfl_up); 4787 break; 4788 case ISN_DROP: smsg("%4d DROP", current); break; 4789 } 4790 4791 out_flush(); // output one line at a time 4792 ui_breakcheck(); 4793 if (got_int) 4794 break; 4795 } 4796 } 4797 4798 /* 4799 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty 4800 * list, etc. Mostly like what JavaScript does, except that empty list and 4801 * empty dictionary are FALSE. 4802 */ 4803 int 4804 tv2bool(typval_T *tv) 4805 { 4806 switch (tv->v_type) 4807 { 4808 case VAR_NUMBER: 4809 return tv->vval.v_number != 0; 4810 case VAR_FLOAT: 4811 #ifdef FEAT_FLOAT 4812 return tv->vval.v_float != 0.0; 4813 #else 4814 break; 4815 #endif 4816 case VAR_PARTIAL: 4817 return tv->vval.v_partial != NULL; 4818 case VAR_FUNC: 4819 case VAR_STRING: 4820 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL; 4821 case VAR_LIST: 4822 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0; 4823 case VAR_DICT: 4824 return tv->vval.v_dict != NULL 4825 && tv->vval.v_dict->dv_hashtab.ht_used > 0; 4826 case VAR_BOOL: 4827 case VAR_SPECIAL: 4828 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE; 4829 case VAR_JOB: 4830 #ifdef FEAT_JOB_CHANNEL 4831 return tv->vval.v_job != NULL; 4832 #else 4833 break; 4834 #endif 4835 case VAR_CHANNEL: 4836 #ifdef FEAT_JOB_CHANNEL 4837 return tv->vval.v_channel != NULL; 4838 #else 4839 break; 4840 #endif 4841 case VAR_BLOB: 4842 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0; 4843 case VAR_UNKNOWN: 4844 case VAR_ANY: 4845 case VAR_VOID: 4846 break; 4847 } 4848 return FALSE; 4849 } 4850 4851 void 4852 emsg_using_string_as(typval_T *tv, int as_number) 4853 { 4854 semsg(_(as_number ? e_using_string_as_number_str 4855 : e_using_string_as_bool_str), 4856 tv->vval.v_string == NULL 4857 ? (char_u *)"" : tv->vval.v_string); 4858 } 4859 4860 /* 4861 * If "tv" is a string give an error and return FAIL. 4862 */ 4863 int 4864 check_not_string(typval_T *tv) 4865 { 4866 if (tv->v_type == VAR_STRING) 4867 { 4868 emsg_using_string_as(tv, TRUE); 4869 clear_tv(tv); 4870 return FAIL; 4871 } 4872 return OK; 4873 } 4874 4875 4876 #endif // FEAT_EVAL 4877