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