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