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