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