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