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