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