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