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