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