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