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