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