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