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