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