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