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