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 when ISN_TRY was encountered 28 int tcd_catch_idx; // instruction of the first catch 29 int tcd_finally_idx; // instruction of the finally block 30 int tcd_caught; // catch block entered 31 int tcd_return; // when TRUE return from end of :finally 32 } trycmd_T; 33 34 35 // A stack is used to store: 36 // - arguments passed to a :def function 37 // - info about the calling function, to use when returning 38 // - local variables 39 // - temporary values 40 // 41 // In detail (FP == Frame Pointer): 42 // arg1 first argument from caller (if present) 43 // arg2 second argument from caller (if present) 44 // extra_arg1 any missing optional argument default value 45 // FP -> cur_func calling function 46 // current previous instruction pointer 47 // frame_ptr previous Frame Pointer 48 // var1 space for local variable 49 // var2 space for local variable 50 // .... fixed space for max. number of local variables 51 // temp temporary values 52 // .... flexible space for temporary values (can grow big) 53 54 /* 55 * Execution context. 56 */ 57 typedef struct { 58 garray_T ec_stack; // stack of typval_T values 59 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx 60 61 garray_T *ec_outer_stack; // stack used for closures 62 int ec_outer_frame; // stack frame in ec_outer_stack 63 64 garray_T ec_trystack; // stack of trycmd_T values 65 int ec_in_catch; // when TRUE in catch or finally block 66 67 int ec_dfunc_idx; // current function index 68 isn_T *ec_instr; // array with instructions 69 int ec_iidx; // index in ec_instr: instruction to execute 70 } ectx_T; 71 72 // Get pointer to item relative to the bottom of the stack, -1 is the last one. 73 #define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx) 74 75 /* 76 * Return the number of arguments, including optional arguments and any vararg. 77 */ 78 static int 79 ufunc_argcount(ufunc_T *ufunc) 80 { 81 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0); 82 } 83 84 /* 85 * Set the instruction index, depending on omitted arguments, where the default 86 * values are to be computed. If all optional arguments are present, start 87 * with the function body. 88 * The expression evaluation is at the start of the instructions: 89 * 0 -> EVAL default1 90 * STORE arg[-2] 91 * 1 -> EVAL default2 92 * STORE arg[-1] 93 * 2 -> function body 94 */ 95 static void 96 init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx) 97 { 98 if (ufunc->uf_def_args.ga_len == 0) 99 ectx->ec_iidx = 0; 100 else 101 { 102 int defcount = ufunc->uf_args.ga_len - argcount; 103 104 // If there is a varargs argument defcount can be negative, no defaults 105 // to evaluate then. 106 if (defcount < 0) 107 defcount = 0; 108 ectx->ec_iidx = ufunc->uf_def_arg_idx[ 109 ufunc->uf_def_args.ga_len - defcount]; 110 } 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 * 145 * Stack has: 146 * - current arguments (already there) 147 * - omitted optional argument (default values) added here 148 * - stack frame: 149 * - pointer to calling function 150 * - Index of next instruction in calling function 151 * - previous frame pointer 152 * - reserved space for local variables 153 */ 154 static int 155 call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx) 156 { 157 int argcount = argcount_arg; 158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx; 159 ufunc_T *ufunc = dfunc->df_ufunc; 160 int arg_to_add; 161 int vararg_count = 0; 162 int idx; 163 estack_T *entry; 164 165 if (dfunc->df_deleted) 166 { 167 emsg_funcname(e_func_deleted, ufunc->uf_name); 168 return FAIL; 169 } 170 171 if (ufunc->uf_va_name != NULL) 172 { 173 // Need to make a list out of the vararg arguments. 174 // Stack at time of call with 2 varargs: 175 // normal_arg 176 // optional_arg 177 // vararg_1 178 // vararg_2 179 // After creating the list: 180 // normal_arg 181 // optional_arg 182 // vararg-list 183 // With missing optional arguments we get: 184 // normal_arg 185 // After creating the list 186 // normal_arg 187 // (space for optional_arg) 188 // vararg-list 189 vararg_count = argcount - ufunc->uf_args.ga_len; 190 if (vararg_count < 0) 191 vararg_count = 0; 192 else 193 argcount -= vararg_count; 194 if (exe_newlist(vararg_count, ectx) == FAIL) 195 return FAIL; 196 197 vararg_count = 1; 198 } 199 200 arg_to_add = ufunc->uf_args.ga_len - argcount; 201 if (arg_to_add < 0) 202 { 203 iemsg("Argument count wrong?"); 204 return FAIL; 205 } 206 if (ga_grow(&ectx->ec_stack, arg_to_add + 3 207 + dfunc->df_varcount + dfunc->df_closure_count) == FAIL) 208 return FAIL; 209 210 // Move the vararg-list to below the missing optional arguments. 211 if (vararg_count > 0 && arg_to_add > 0) 212 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1); 213 214 // Reserve space for omitted optional arguments, filled in soon. 215 for (idx = 0; idx < arg_to_add; ++idx) 216 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN; 217 ectx->ec_stack.ga_len += arg_to_add; 218 219 // Store current execution state in stack frame for ISN_RETURN. 220 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx; 221 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx; 222 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx; 223 ectx->ec_frame_idx = ectx->ec_stack.ga_len; 224 225 // Initialize local variables 226 for (idx = 0; idx < dfunc->df_varcount + dfunc->df_closure_count; ++idx) 227 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN; 228 ectx->ec_stack.ga_len += STACK_FRAME_SIZE 229 + dfunc->df_varcount + dfunc->df_closure_count; 230 231 // Set execution state to the start of the called function. 232 ectx->ec_dfunc_idx = cdf_idx; 233 ectx->ec_instr = dfunc->df_instr; 234 entry = estack_push_ufunc(dfunc->df_ufunc, 1); 235 if (entry != NULL) 236 { 237 // Set the script context to the script where the function was defined. 238 // TODO: save more than the SID? 239 entry->es_save_sid = current_sctx.sc_sid; 240 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid; 241 } 242 243 // Decide where to start execution, handles optional arguments. 244 init_instr_idx(ufunc, argcount, ectx); 245 246 return OK; 247 } 248 249 // Get pointer to item in the stack. 250 #define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx) 251 252 /* 253 * Used when returning from a function: Check if any closure is still 254 * referenced. If so then move the arguments and variables to a separate piece 255 * of stack to be used when the closure is called. 256 * When "free_arguments" is TRUE the arguments are to be freed. 257 * Returns FAIL when out of memory. 258 */ 259 static int 260 handle_closure_in_use(ectx_T *ectx, int free_arguments) 261 { 262 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 263 + ectx->ec_dfunc_idx; 264 int argcount = ufunc_argcount(dfunc->df_ufunc); 265 int top = ectx->ec_frame_idx - argcount; 266 int idx; 267 typval_T *tv; 268 int closure_in_use = FALSE; 269 270 // Check if any created closure is still in use. 271 for (idx = 0; idx < dfunc->df_closure_count; ++idx) 272 { 273 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE 274 + dfunc->df_varcount + idx); 275 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL 276 && tv->vval.v_partial->pt_refcount > 1) 277 { 278 int refcount = tv->vval.v_partial->pt_refcount; 279 int i; 280 281 // A Reference in a local variables doesn't count, it gets 282 // unreferenced on return. 283 for (i = 0; i < dfunc->df_varcount; ++i) 284 { 285 typval_T *stv = STACK_TV(ectx->ec_frame_idx 286 + STACK_FRAME_SIZE + i); 287 if (stv->v_type == VAR_PARTIAL 288 && tv->vval.v_partial == stv->vval.v_partial) 289 --refcount; 290 } 291 if (refcount > 1) 292 { 293 closure_in_use = TRUE; 294 break; 295 } 296 } 297 } 298 299 if (closure_in_use) 300 { 301 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T); 302 typval_T *stack; 303 304 // A closure is using the arguments and/or local variables. 305 // Move them to the called function. 306 if (funcstack == NULL) 307 return FAIL; 308 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE 309 + dfunc->df_varcount; 310 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len); 311 funcstack->fs_ga.ga_data = stack; 312 if (stack == NULL) 313 { 314 vim_free(funcstack); 315 return FAIL; 316 } 317 318 // Move or copy the arguments. 319 for (idx = 0; idx < argcount; ++idx) 320 { 321 tv = STACK_TV(top + idx); 322 if (free_arguments) 323 { 324 *(stack + idx) = *tv; 325 tv->v_type = VAR_UNKNOWN; 326 } 327 else 328 copy_tv(tv, stack + idx); 329 } 330 // Move the local variables. 331 for (idx = 0; idx < dfunc->df_varcount; ++idx) 332 { 333 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx); 334 335 // Do not copy a partial created for a local function. 336 // TODO: this won't work if the closure actually uses it. But when 337 // keeping it it gets complicated: it will create a reference cycle 338 // inside the partial, thus needs special handling for garbage 339 // collection. 340 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL) 341 { 342 int i; 343 typval_T *ctv; 344 345 for (i = 0; i < dfunc->df_closure_count; ++i) 346 { 347 ctv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE 348 + dfunc->df_varcount + i); 349 if (tv->vval.v_partial == ctv->vval.v_partial) 350 break; 351 } 352 if (i < dfunc->df_closure_count) 353 { 354 (stack + argcount + STACK_FRAME_SIZE + idx)->v_type = 355 VAR_UNKNOWN; 356 continue; 357 } 358 } 359 360 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv; 361 tv->v_type = VAR_UNKNOWN; 362 } 363 364 for (idx = 0; idx < dfunc->df_closure_count; ++idx) 365 { 366 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE 367 + dfunc->df_varcount + idx); 368 if (tv->v_type == VAR_PARTIAL) 369 { 370 partial_T *partial = tv->vval.v_partial; 371 372 if (partial->pt_refcount > 1) 373 { 374 ++funcstack->fs_refcount; 375 partial->pt_funcstack = funcstack; 376 partial->pt_ectx_stack = &funcstack->fs_ga; 377 partial->pt_ectx_frame = ectx->ec_frame_idx - top; 378 } 379 } 380 } 381 } 382 383 return OK; 384 } 385 386 /* 387 * Return from the current function. 388 */ 389 static int 390 func_return(ectx_T *ectx) 391 { 392 int idx; 393 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 394 + ectx->ec_dfunc_idx; 395 int argcount = ufunc_argcount(dfunc->df_ufunc); 396 int top = ectx->ec_frame_idx - argcount; 397 estack_T *entry; 398 399 // execution context goes one level up 400 entry = estack_pop(); 401 if (entry != NULL) 402 current_sctx.sc_sid = entry->es_save_sid; 403 404 if (handle_closure_in_use(ectx, TRUE) == FAIL) 405 return FAIL; 406 407 // Clear the arguments. 408 for (idx = top; idx < ectx->ec_frame_idx; ++idx) 409 clear_tv(STACK_TV(idx)); 410 411 // Clear local variables and temp values, but not the return value. 412 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE; 413 idx < ectx->ec_stack.ga_len - 1; ++idx) 414 clear_tv(STACK_TV(idx)); 415 416 // Restore the previous frame. 417 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number; 418 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number; 419 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number; 420 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx; 421 ectx->ec_instr = dfunc->df_instr; 422 423 // Reset the stack to the position before the call, move the return value 424 // to the top of the stack. 425 idx = ectx->ec_stack.ga_len - 1; 426 ectx->ec_stack.ga_len = top + 1; 427 *STACK_TV_BOT(-1) = *STACK_TV(idx); 428 429 return OK; 430 } 431 432 #undef STACK_TV 433 434 /* 435 * Prepare arguments and rettv for calling a builtin or user function. 436 */ 437 static int 438 call_prepare(int argcount, typval_T *argvars, ectx_T *ectx) 439 { 440 int idx; 441 typval_T *tv; 442 443 // Move arguments from bottom of the stack to argvars[] and add terminator. 444 for (idx = 0; idx < argcount; ++idx) 445 argvars[idx] = *STACK_TV_BOT(idx - argcount); 446 argvars[argcount].v_type = VAR_UNKNOWN; 447 448 // Result replaces the arguments on the stack. 449 if (argcount > 0) 450 ectx->ec_stack.ga_len -= argcount - 1; 451 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL) 452 return FAIL; 453 else 454 ++ectx->ec_stack.ga_len; 455 456 // Default return value is zero. 457 tv = STACK_TV_BOT(-1); 458 tv->v_type = VAR_NUMBER; 459 tv->vval.v_number = 0; 460 461 return OK; 462 } 463 464 // Ugly global to avoid passing the execution context around through many 465 // layers. 466 static ectx_T *current_ectx = NULL; 467 468 /* 469 * Call a builtin function by index. 470 */ 471 static int 472 call_bfunc(int func_idx, int argcount, ectx_T *ectx) 473 { 474 typval_T argvars[MAX_FUNC_ARGS]; 475 int idx; 476 int did_emsg_before = did_emsg; 477 ectx_T *prev_ectx = current_ectx; 478 479 if (call_prepare(argcount, argvars, ectx) == FAIL) 480 return FAIL; 481 482 // Call the builtin function. Set "current_ectx" so that when it 483 // recursively invokes call_def_function() a closure context can be set. 484 current_ectx = ectx; 485 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1)); 486 current_ectx = prev_ectx; 487 488 // Clear the arguments. 489 for (idx = 0; idx < argcount; ++idx) 490 clear_tv(&argvars[idx]); 491 492 if (did_emsg != did_emsg_before) 493 return FAIL; 494 return OK; 495 } 496 497 /* 498 * Execute a user defined function. 499 * "iptr" can be used to replace the instruction with a more efficient one. 500 */ 501 static int 502 call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr) 503 { 504 typval_T argvars[MAX_FUNC_ARGS]; 505 funcexe_T funcexe; 506 int error; 507 int idx; 508 509 if (ufunc->uf_def_status == UF_TO_BE_COMPILED 510 && compile_def_function(ufunc, FALSE, NULL) == FAIL) 511 return FAIL; 512 if (ufunc->uf_def_status == UF_COMPILED) 513 { 514 // The function has been compiled, can call it quickly. For a function 515 // that was defined later: we can call it directly next time. 516 if (iptr != NULL) 517 { 518 delete_instr(iptr); 519 iptr->isn_type = ISN_DCALL; 520 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; 521 iptr->isn_arg.dfunc.cdf_argcount = argcount; 522 } 523 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx); 524 } 525 526 if (call_prepare(argcount, argvars, ectx) == FAIL) 527 return FAIL; 528 CLEAR_FIELD(funcexe); 529 funcexe.evaluate = TRUE; 530 531 // Call the user function. Result goes in last position on the stack. 532 // TODO: add selfdict if there is one 533 error = call_user_func_check(ufunc, argcount, argvars, 534 STACK_TV_BOT(-1), &funcexe, NULL); 535 536 // Clear the arguments. 537 for (idx = 0; idx < argcount; ++idx) 538 clear_tv(&argvars[idx]); 539 540 if (error != FCERR_NONE) 541 { 542 user_func_error(error, ufunc->uf_name); 543 return FAIL; 544 } 545 return OK; 546 } 547 548 /* 549 * Execute a function by "name". 550 * This can be a builtin function or a user function. 551 * "iptr" can be used to replace the instruction with a more efficient one. 552 * Returns FAIL if not found without an error message. 553 */ 554 static int 555 call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr) 556 { 557 ufunc_T *ufunc; 558 559 if (builtin_function(name, -1)) 560 { 561 int func_idx = find_internal_func(name); 562 563 if (func_idx < 0) 564 return FAIL; 565 if (check_internal_func(func_idx, argcount) < 0) 566 return FAIL; 567 return call_bfunc(func_idx, argcount, ectx); 568 } 569 570 ufunc = find_func(name, FALSE, NULL); 571 if (ufunc != NULL) 572 return call_ufunc(ufunc, argcount, ectx, iptr); 573 574 return FAIL; 575 } 576 577 static int 578 call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx) 579 { 580 int argcount = argcount_arg; 581 char_u *name = NULL; 582 int called_emsg_before = called_emsg; 583 584 if (tv->v_type == VAR_PARTIAL) 585 { 586 partial_T *pt = tv->vval.v_partial; 587 int i; 588 589 if (pt->pt_argc > 0) 590 { 591 // Make space for arguments from the partial, shift the "argcount" 592 // arguments up. 593 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL) 594 return FAIL; 595 for (i = 1; i <= argcount; ++i) 596 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i); 597 ectx->ec_stack.ga_len += pt->pt_argc; 598 argcount += pt->pt_argc; 599 600 // copy the arguments from the partial onto the stack 601 for (i = 0; i < pt->pt_argc; ++i) 602 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i)); 603 } 604 605 if (pt->pt_func != NULL) 606 { 607 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL); 608 609 // closure may need the function context where it was defined 610 ectx->ec_outer_stack = pt->pt_ectx_stack; 611 ectx->ec_outer_frame = pt->pt_ectx_frame; 612 613 return ret; 614 } 615 name = pt->pt_name; 616 } 617 else if (tv->v_type == VAR_FUNC) 618 name = tv->vval.v_string; 619 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL) 620 { 621 if (called_emsg == called_emsg_before) 622 semsg(_(e_unknownfunc), 623 name == NULL ? (char_u *)"[unknown]" : name); 624 return FAIL; 625 } 626 return OK; 627 } 628 629 /* 630 * Store "tv" in variable "name". 631 * This is for s: and g: variables. 632 */ 633 static void 634 store_var(char_u *name, typval_T *tv) 635 { 636 funccal_entry_T entry; 637 638 save_funccal(&entry); 639 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND); 640 restore_funccal(); 641 } 642 643 644 /* 645 * Execute a function by "name". 646 * This can be a builtin function, user function or a funcref. 647 * "iptr" can be used to replace the instruction with a more efficient one. 648 */ 649 static int 650 call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr) 651 { 652 int called_emsg_before = called_emsg; 653 654 if (call_by_name(name, argcount, ectx, iptr) == FAIL 655 && called_emsg == called_emsg_before) 656 { 657 dictitem_T *v; 658 659 v = find_var(name, NULL, FALSE); 660 if (v == NULL) 661 { 662 semsg(_(e_unknownfunc), name); 663 return FAIL; 664 } 665 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC) 666 { 667 semsg(_(e_unknownfunc), name); 668 return FAIL; 669 } 670 return call_partial(&v->di_tv, argcount, ectx); 671 } 672 return OK; 673 } 674 675 /* 676 * Call a "def" function from old Vim script. 677 * Return OK or FAIL. 678 */ 679 int 680 call_def_function( 681 ufunc_T *ufunc, 682 int argc_arg, // nr of arguments 683 typval_T *argv, // arguments 684 partial_T *partial, // optional partial for context 685 typval_T *rettv) // return value 686 { 687 ectx_T ectx; // execution context 688 int argc = argc_arg; 689 int initial_frame_idx; 690 typval_T *tv; 691 int idx; 692 int ret = FAIL; 693 int defcount = ufunc->uf_args.ga_len - argc; 694 int save_sc_version = current_sctx.sc_version; 695 int breakcheck_count = 0; 696 int called_emsg_before = called_emsg; 697 698 // Get pointer to item in the stack. 699 #define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx) 700 701 // Get pointer to item at the bottom of the stack, -1 is the bottom. 702 #undef STACK_TV_BOT 703 #define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx) 704 705 // Get pointer to a local variable on the stack. Negative for arguments. 706 #define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx) 707 708 // Like STACK_TV_VAR but use the outer scope 709 #define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx) 710 711 if (ufunc->uf_def_status == UF_NOT_COMPILED 712 || (ufunc->uf_def_status == UF_TO_BE_COMPILED 713 && compile_def_function(ufunc, FALSE, NULL) == FAIL)) 714 { 715 if (called_emsg == called_emsg_before) 716 semsg(_("E1091: Function is not compiled: %s"), 717 printable_func_name(ufunc)); 718 return FAIL; 719 } 720 721 { 722 // Check the function was really compiled. 723 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 724 + ufunc->uf_dfunc_idx; 725 if (dfunc->df_instr == NULL) 726 { 727 iemsg("using call_def_function() on not compiled function"); 728 return FAIL; 729 } 730 } 731 732 CLEAR_FIELD(ectx); 733 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx; 734 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500); 735 if (ga_grow(&ectx.ec_stack, 20) == FAIL) 736 return FAIL; 737 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10); 738 739 // Put arguments on the stack. 740 for (idx = 0; idx < argc; ++idx) 741 { 742 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len 743 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx]) 744 == FAIL) 745 goto failed_early; 746 copy_tv(&argv[idx], STACK_TV_BOT(0)); 747 ++ectx.ec_stack.ga_len; 748 } 749 750 // Turn varargs into a list. Empty list if no args. 751 if (ufunc->uf_va_name != NULL) 752 { 753 int vararg_count = argc - ufunc->uf_args.ga_len; 754 755 if (vararg_count < 0) 756 vararg_count = 0; 757 else 758 argc -= vararg_count; 759 if (exe_newlist(vararg_count, &ectx) == FAIL) 760 goto failed_early; 761 762 // Check the type of the list items. 763 tv = STACK_TV_BOT(-1); 764 if (ufunc->uf_va_type != NULL 765 && ufunc->uf_va_type->tt_member != &t_any 766 && tv->vval.v_list != NULL) 767 { 768 type_T *expected = ufunc->uf_va_type->tt_member; 769 listitem_T *li = tv->vval.v_list->lv_first; 770 771 for (idx = 0; idx < vararg_count; ++idx) 772 { 773 if (check_typval_type(expected, &li->li_tv) == FAIL) 774 goto failed_early; 775 li = li->li_next; 776 } 777 } 778 779 if (defcount > 0) 780 // Move varargs list to below missing default arguments. 781 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1); 782 --ectx.ec_stack.ga_len; 783 } 784 785 // Make space for omitted arguments, will store default value below. 786 // Any varargs list goes after them. 787 if (defcount > 0) 788 for (idx = 0; idx < defcount; ++idx) 789 { 790 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN; 791 ++ectx.ec_stack.ga_len; 792 } 793 if (ufunc->uf_va_name != NULL) 794 ++ectx.ec_stack.ga_len; 795 796 // Frame pointer points to just after arguments. 797 ectx.ec_frame_idx = ectx.ec_stack.ga_len; 798 initial_frame_idx = ectx.ec_frame_idx; 799 800 if (partial != NULL) 801 { 802 if (partial->pt_ectx_stack == NULL && current_ectx != NULL) 803 { 804 // TODO: is this always the right way? 805 ectx.ec_outer_stack = ¤t_ectx->ec_stack; 806 ectx.ec_outer_frame = current_ectx->ec_frame_idx; 807 } 808 else 809 { 810 ectx.ec_outer_stack = partial->pt_ectx_stack; 811 ectx.ec_outer_frame = partial->pt_ectx_frame; 812 } 813 } 814 815 // dummy frame entries 816 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx) 817 { 818 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN; 819 ++ectx.ec_stack.ga_len; 820 } 821 822 { 823 // Reserve space for local variables and closure references. 824 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 825 + ufunc->uf_dfunc_idx; 826 int count = dfunc->df_varcount + dfunc->df_closure_count; 827 828 for (idx = 0; idx < count; ++idx) 829 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN; 830 ectx.ec_stack.ga_len += count; 831 832 ectx.ec_instr = dfunc->df_instr; 833 } 834 835 // Commands behave like vim9script. 836 current_sctx.sc_version = SCRIPT_VERSION_VIM9; 837 838 // Decide where to start execution, handles optional arguments. 839 init_instr_idx(ufunc, argc, &ectx); 840 841 for (;;) 842 { 843 isn_T *iptr; 844 845 if (++breakcheck_count >= 100) 846 { 847 line_breakcheck(); 848 breakcheck_count = 0; 849 } 850 if (got_int) 851 { 852 // Turn CTRL-C into an exception. 853 got_int = FALSE; 854 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL) 855 goto failed; 856 did_throw = TRUE; 857 } 858 859 if (did_emsg && msg_list != NULL && *msg_list != NULL) 860 { 861 // Turn an error message into an exception. 862 did_emsg = FALSE; 863 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL) 864 goto failed; 865 did_throw = TRUE; 866 *msg_list = NULL; 867 } 868 869 if (did_throw && !ectx.ec_in_catch) 870 { 871 garray_T *trystack = &ectx.ec_trystack; 872 trycmd_T *trycmd = NULL; 873 874 // An exception jumps to the first catch, finally, or returns from 875 // the current function. 876 if (trystack->ga_len > 0) 877 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1; 878 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx) 879 { 880 // jump to ":catch" or ":finally" 881 ectx.ec_in_catch = TRUE; 882 ectx.ec_iidx = trycmd->tcd_catch_idx; 883 } 884 else 885 { 886 // not inside try or need to return from current functions. 887 if (ectx.ec_frame_idx == initial_frame_idx) 888 { 889 // At the toplevel we are done. Push a dummy return value. 890 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 891 goto failed; 892 tv = STACK_TV_BOT(0); 893 tv->v_type = VAR_NUMBER; 894 tv->vval.v_number = 0; 895 ++ectx.ec_stack.ga_len; 896 need_rethrow = TRUE; 897 if (handle_closure_in_use(&ectx, FALSE) == FAIL) 898 goto failed; 899 goto done; 900 } 901 902 if (func_return(&ectx) == FAIL) 903 goto failed; 904 } 905 continue; 906 } 907 908 iptr = &ectx.ec_instr[ectx.ec_iidx++]; 909 switch (iptr->isn_type) 910 { 911 // execute Ex command line 912 case ISN_EXEC: 913 SOURCING_LNUM = iptr->isn_lnum; 914 do_cmdline_cmd(iptr->isn_arg.string); 915 break; 916 917 // execute Ex command from pieces on the stack 918 case ISN_EXECCONCAT: 919 { 920 int count = iptr->isn_arg.number; 921 size_t len = 0; 922 int pass; 923 int i; 924 char_u *cmd = NULL; 925 char_u *str; 926 927 for (pass = 1; pass <= 2; ++pass) 928 { 929 for (i = 0; i < count; ++i) 930 { 931 tv = STACK_TV_BOT(i - count); 932 str = tv->vval.v_string; 933 if (str != NULL && *str != NUL) 934 { 935 if (pass == 2) 936 STRCPY(cmd + len, str); 937 len += STRLEN(str); 938 } 939 if (pass == 2) 940 clear_tv(tv); 941 } 942 if (pass == 1) 943 { 944 cmd = alloc(len + 1); 945 if (cmd == NULL) 946 goto failed; 947 len = 0; 948 } 949 } 950 951 SOURCING_LNUM = iptr->isn_lnum; 952 do_cmdline_cmd(cmd); 953 vim_free(cmd); 954 } 955 break; 956 957 // execute :echo {string} ... 958 case ISN_ECHO: 959 { 960 int count = iptr->isn_arg.echo.echo_count; 961 int atstart = TRUE; 962 int needclr = TRUE; 963 964 for (idx = 0; idx < count; ++idx) 965 { 966 tv = STACK_TV_BOT(idx - count); 967 echo_one(tv, iptr->isn_arg.echo.echo_with_white, 968 &atstart, &needclr); 969 clear_tv(tv); 970 } 971 if (needclr) 972 msg_clr_eos(); 973 ectx.ec_stack.ga_len -= count; 974 } 975 break; 976 977 // :execute {string} ... 978 // :echomsg {string} ... 979 // :echoerr {string} ... 980 case ISN_EXECUTE: 981 case ISN_ECHOMSG: 982 case ISN_ECHOERR: 983 { 984 int count = iptr->isn_arg.number; 985 garray_T ga; 986 char_u buf[NUMBUFLEN]; 987 char_u *p; 988 int len; 989 int failed = FALSE; 990 991 ga_init2(&ga, 1, 80); 992 for (idx = 0; idx < count; ++idx) 993 { 994 tv = STACK_TV_BOT(idx - count); 995 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB) 996 { 997 emsg(_(e_inval_string)); 998 break; 999 } 1000 else 1001 p = tv_get_string_buf(tv, buf); 1002 1003 len = (int)STRLEN(p); 1004 if (ga_grow(&ga, len + 2) == FAIL) 1005 failed = TRUE; 1006 else 1007 { 1008 if (ga.ga_len > 0) 1009 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; 1010 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); 1011 ga.ga_len += len; 1012 } 1013 clear_tv(tv); 1014 } 1015 ectx.ec_stack.ga_len -= count; 1016 1017 if (!failed && ga.ga_data != NULL) 1018 { 1019 if (iptr->isn_type == ISN_EXECUTE) 1020 do_cmdline_cmd((char_u *)ga.ga_data); 1021 else 1022 { 1023 msg_sb_eol(); 1024 if (iptr->isn_type == ISN_ECHOMSG) 1025 { 1026 msg_attr(ga.ga_data, echo_attr); 1027 out_flush(); 1028 } 1029 else 1030 { 1031 SOURCING_LNUM = iptr->isn_lnum; 1032 emsg(ga.ga_data); 1033 } 1034 } 1035 } 1036 ga_clear(&ga); 1037 } 1038 break; 1039 1040 // load local variable or argument 1041 case ISN_LOAD: 1042 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1043 goto failed; 1044 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0)); 1045 ++ectx.ec_stack.ga_len; 1046 break; 1047 1048 // load variable or argument from outer scope 1049 case ISN_LOADOUTER: 1050 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1051 goto failed; 1052 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number), 1053 STACK_TV_BOT(0)); 1054 ++ectx.ec_stack.ga_len; 1055 break; 1056 1057 // load v: variable 1058 case ISN_LOADV: 1059 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1060 goto failed; 1061 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0)); 1062 ++ectx.ec_stack.ga_len; 1063 break; 1064 1065 // load s: variable in Vim9 script 1066 case ISN_LOADSCRIPT: 1067 { 1068 scriptitem_T *si = 1069 SCRIPT_ITEM(iptr->isn_arg.script.script_sid); 1070 svar_T *sv; 1071 1072 sv = ((svar_T *)si->sn_var_vals.ga_data) 1073 + iptr->isn_arg.script.script_idx; 1074 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1075 goto failed; 1076 copy_tv(sv->sv_tv, STACK_TV_BOT(0)); 1077 ++ectx.ec_stack.ga_len; 1078 } 1079 break; 1080 1081 // load s: variable in old script 1082 case ISN_LOADS: 1083 { 1084 hashtab_T *ht = &SCRIPT_VARS( 1085 iptr->isn_arg.loadstore.ls_sid); 1086 char_u *name = iptr->isn_arg.loadstore.ls_name; 1087 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE); 1088 1089 if (di == NULL) 1090 { 1091 semsg(_(e_undefvar), name); 1092 goto on_error; 1093 } 1094 else 1095 { 1096 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1097 goto failed; 1098 copy_tv(&di->di_tv, STACK_TV_BOT(0)); 1099 ++ectx.ec_stack.ga_len; 1100 } 1101 } 1102 break; 1103 1104 // load g:/b:/w:/t: variable 1105 case ISN_LOADG: 1106 case ISN_LOADB: 1107 case ISN_LOADW: 1108 case ISN_LOADT: 1109 { 1110 dictitem_T *di = NULL; 1111 hashtab_T *ht = NULL; 1112 char namespace; 1113 1114 switch (iptr->isn_type) 1115 { 1116 case ISN_LOADG: 1117 ht = get_globvar_ht(); 1118 namespace = 'g'; 1119 break; 1120 case ISN_LOADB: 1121 ht = &curbuf->b_vars->dv_hashtab; 1122 namespace = 'b'; 1123 break; 1124 case ISN_LOADW: 1125 ht = &curwin->w_vars->dv_hashtab; 1126 namespace = 'w'; 1127 break; 1128 case ISN_LOADT: 1129 ht = &curtab->tp_vars->dv_hashtab; 1130 namespace = 't'; 1131 break; 1132 default: // Cannot reach here 1133 goto failed; 1134 } 1135 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE); 1136 1137 if (di == NULL) 1138 { 1139 semsg(_("E121: Undefined variable: %c:%s"), 1140 namespace, iptr->isn_arg.string); 1141 goto on_error; 1142 } 1143 else 1144 { 1145 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1146 goto failed; 1147 copy_tv(&di->di_tv, STACK_TV_BOT(0)); 1148 ++ectx.ec_stack.ga_len; 1149 } 1150 } 1151 break; 1152 1153 // load g:/b:/w:/t: namespace 1154 case ISN_LOADGDICT: 1155 case ISN_LOADBDICT: 1156 case ISN_LOADWDICT: 1157 case ISN_LOADTDICT: 1158 { 1159 dict_T *d = NULL; 1160 1161 switch (iptr->isn_type) 1162 { 1163 case ISN_LOADGDICT: d = get_globvar_dict(); break; 1164 case ISN_LOADBDICT: d = curbuf->b_vars; break; 1165 case ISN_LOADWDICT: d = curwin->w_vars; break; 1166 case ISN_LOADTDICT: d = curtab->tp_vars; break; 1167 default: // Cannot reach here 1168 goto failed; 1169 } 1170 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1171 goto failed; 1172 tv = STACK_TV_BOT(0); 1173 tv->v_type = VAR_DICT; 1174 tv->v_lock = 0; 1175 tv->vval.v_dict = d; 1176 ++ectx.ec_stack.ga_len; 1177 } 1178 break; 1179 1180 // load &option 1181 case ISN_LOADOPT: 1182 { 1183 typval_T optval; 1184 char_u *name = iptr->isn_arg.string; 1185 1186 // This is not expected to fail, name is checked during 1187 // compilation: don't set SOURCING_LNUM. 1188 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1189 goto failed; 1190 if (eval_option(&name, &optval, TRUE) == FAIL) 1191 goto failed; 1192 *STACK_TV_BOT(0) = optval; 1193 ++ectx.ec_stack.ga_len; 1194 } 1195 break; 1196 1197 // load $ENV 1198 case ISN_LOADENV: 1199 { 1200 typval_T optval; 1201 char_u *name = iptr->isn_arg.string; 1202 1203 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1204 goto failed; 1205 // name is always valid, checked when compiling 1206 (void)eval_env_var(&name, &optval, TRUE); 1207 *STACK_TV_BOT(0) = optval; 1208 ++ectx.ec_stack.ga_len; 1209 } 1210 break; 1211 1212 // load @register 1213 case ISN_LOADREG: 1214 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1215 goto failed; 1216 tv = STACK_TV_BOT(0); 1217 tv->v_type = VAR_STRING; 1218 tv->v_lock = 0; 1219 tv->vval.v_string = get_reg_contents( 1220 iptr->isn_arg.number, GREG_EXPR_SRC); 1221 ++ectx.ec_stack.ga_len; 1222 break; 1223 1224 // store local variable 1225 case ISN_STORE: 1226 --ectx.ec_stack.ga_len; 1227 tv = STACK_TV_VAR(iptr->isn_arg.number); 1228 clear_tv(tv); 1229 *tv = *STACK_TV_BOT(0); 1230 break; 1231 1232 // store variable or argument in outer scope 1233 case ISN_STOREOUTER: 1234 --ectx.ec_stack.ga_len; 1235 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number); 1236 clear_tv(tv); 1237 *tv = *STACK_TV_BOT(0); 1238 break; 1239 1240 // store s: variable in old script 1241 case ISN_STORES: 1242 { 1243 hashtab_T *ht = &SCRIPT_VARS( 1244 iptr->isn_arg.loadstore.ls_sid); 1245 char_u *name = iptr->isn_arg.loadstore.ls_name; 1246 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE); 1247 1248 --ectx.ec_stack.ga_len; 1249 if (di == NULL) 1250 store_var(name, STACK_TV_BOT(0)); 1251 else 1252 { 1253 clear_tv(&di->di_tv); 1254 di->di_tv = *STACK_TV_BOT(0); 1255 } 1256 } 1257 break; 1258 1259 // store script-local variable in Vim9 script 1260 case ISN_STORESCRIPT: 1261 { 1262 scriptitem_T *si = SCRIPT_ITEM( 1263 iptr->isn_arg.script.script_sid); 1264 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 1265 + iptr->isn_arg.script.script_idx; 1266 1267 --ectx.ec_stack.ga_len; 1268 clear_tv(sv->sv_tv); 1269 *sv->sv_tv = *STACK_TV_BOT(0); 1270 } 1271 break; 1272 1273 // store option 1274 case ISN_STOREOPT: 1275 { 1276 long n = 0; 1277 char_u *s = NULL; 1278 char *msg; 1279 1280 --ectx.ec_stack.ga_len; 1281 tv = STACK_TV_BOT(0); 1282 if (tv->v_type == VAR_STRING) 1283 { 1284 s = tv->vval.v_string; 1285 if (s == NULL) 1286 s = (char_u *)""; 1287 } 1288 else 1289 // must be VAR_NUMBER, CHECKTYPE makes sure 1290 n = tv->vval.v_number; 1291 msg = set_option_value(iptr->isn_arg.storeopt.so_name, 1292 n, s, iptr->isn_arg.storeopt.so_flags); 1293 clear_tv(tv); 1294 if (msg != NULL) 1295 { 1296 emsg(_(msg)); 1297 goto on_error; 1298 } 1299 } 1300 break; 1301 1302 // store $ENV 1303 case ISN_STOREENV: 1304 --ectx.ec_stack.ga_len; 1305 tv = STACK_TV_BOT(0); 1306 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv)); 1307 clear_tv(tv); 1308 break; 1309 1310 // store @r 1311 case ISN_STOREREG: 1312 { 1313 int reg = iptr->isn_arg.number; 1314 1315 --ectx.ec_stack.ga_len; 1316 tv = STACK_TV_BOT(0); 1317 write_reg_contents(reg == '@' ? '"' : reg, 1318 tv_get_string(tv), -1, FALSE); 1319 clear_tv(tv); 1320 } 1321 break; 1322 1323 // store v: variable 1324 case ISN_STOREV: 1325 --ectx.ec_stack.ga_len; 1326 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0)) 1327 == FAIL) 1328 // should not happen, type is checked when compiling 1329 goto on_error; 1330 break; 1331 1332 // store g:/b:/w:/t: variable 1333 case ISN_STOREG: 1334 case ISN_STOREB: 1335 case ISN_STOREW: 1336 case ISN_STORET: 1337 { 1338 dictitem_T *di; 1339 hashtab_T *ht; 1340 switch (iptr->isn_type) 1341 { 1342 case ISN_STOREG: 1343 ht = get_globvar_ht(); 1344 break; 1345 case ISN_STOREB: 1346 ht = &curbuf->b_vars->dv_hashtab; 1347 break; 1348 case ISN_STOREW: 1349 ht = &curwin->w_vars->dv_hashtab; 1350 break; 1351 case ISN_STORET: 1352 ht = &curtab->tp_vars->dv_hashtab; 1353 break; 1354 default: // Cannot reach here 1355 goto failed; 1356 } 1357 1358 --ectx.ec_stack.ga_len; 1359 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE); 1360 if (di == NULL) 1361 store_var(iptr->isn_arg.string, STACK_TV_BOT(0)); 1362 else 1363 { 1364 clear_tv(&di->di_tv); 1365 di->di_tv = *STACK_TV_BOT(0); 1366 } 1367 } 1368 break; 1369 1370 // store number in local variable 1371 case ISN_STORENR: 1372 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx); 1373 clear_tv(tv); 1374 tv->v_type = VAR_NUMBER; 1375 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val; 1376 break; 1377 1378 // store value in list variable 1379 case ISN_STORELIST: 1380 { 1381 typval_T *tv_idx = STACK_TV_BOT(-2); 1382 varnumber_T lidx = tv_idx->vval.v_number; 1383 typval_T *tv_list = STACK_TV_BOT(-1); 1384 list_T *list = tv_list->vval.v_list; 1385 1386 if (lidx < 0 && list->lv_len + lidx >= 0) 1387 // negative index is relative to the end 1388 lidx = list->lv_len + lidx; 1389 if (lidx < 0 || lidx > list->lv_len) 1390 { 1391 semsg(_(e_listidx), lidx); 1392 goto on_error; 1393 } 1394 tv = STACK_TV_BOT(-3); 1395 if (lidx < list->lv_len) 1396 { 1397 listitem_T *li = list_find(list, lidx); 1398 1399 // overwrite existing list item 1400 clear_tv(&li->li_tv); 1401 li->li_tv = *tv; 1402 } 1403 else 1404 { 1405 // append to list, only fails when out of memory 1406 if (list_append_tv(list, tv) == FAIL) 1407 goto failed; 1408 clear_tv(tv); 1409 } 1410 clear_tv(tv_idx); 1411 clear_tv(tv_list); 1412 ectx.ec_stack.ga_len -= 3; 1413 } 1414 break; 1415 1416 // store value in dict variable 1417 case ISN_STOREDICT: 1418 { 1419 typval_T *tv_key = STACK_TV_BOT(-2); 1420 char_u *key = tv_key->vval.v_string; 1421 typval_T *tv_dict = STACK_TV_BOT(-1); 1422 dict_T *dict = tv_dict->vval.v_dict; 1423 dictitem_T *di; 1424 1425 if (dict == NULL) 1426 { 1427 emsg(_(e_dictnull)); 1428 goto on_error; 1429 } 1430 if (key == NULL) 1431 key = (char_u *)""; 1432 tv = STACK_TV_BOT(-3); 1433 di = dict_find(dict, key, -1); 1434 if (di != NULL) 1435 { 1436 // overwrite existing value 1437 clear_tv(&di->di_tv); 1438 di->di_tv = *tv; 1439 } 1440 else 1441 { 1442 // add to dict, only fails when out of memory 1443 if (dict_add_tv(dict, (char *)key, tv) == FAIL) 1444 goto failed; 1445 clear_tv(tv); 1446 } 1447 clear_tv(tv_key); 1448 clear_tv(tv_dict); 1449 ectx.ec_stack.ga_len -= 3; 1450 } 1451 break; 1452 1453 // push constant 1454 case ISN_PUSHNR: 1455 case ISN_PUSHBOOL: 1456 case ISN_PUSHSPEC: 1457 case ISN_PUSHF: 1458 case ISN_PUSHS: 1459 case ISN_PUSHBLOB: 1460 case ISN_PUSHFUNC: 1461 case ISN_PUSHCHANNEL: 1462 case ISN_PUSHJOB: 1463 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1464 goto failed; 1465 tv = STACK_TV_BOT(0); 1466 tv->v_lock = 0; 1467 ++ectx.ec_stack.ga_len; 1468 switch (iptr->isn_type) 1469 { 1470 case ISN_PUSHNR: 1471 tv->v_type = VAR_NUMBER; 1472 tv->vval.v_number = iptr->isn_arg.number; 1473 break; 1474 case ISN_PUSHBOOL: 1475 tv->v_type = VAR_BOOL; 1476 tv->vval.v_number = iptr->isn_arg.number; 1477 break; 1478 case ISN_PUSHSPEC: 1479 tv->v_type = VAR_SPECIAL; 1480 tv->vval.v_number = iptr->isn_arg.number; 1481 break; 1482 #ifdef FEAT_FLOAT 1483 case ISN_PUSHF: 1484 tv->v_type = VAR_FLOAT; 1485 tv->vval.v_float = iptr->isn_arg.fnumber; 1486 break; 1487 #endif 1488 case ISN_PUSHBLOB: 1489 blob_copy(iptr->isn_arg.blob, tv); 1490 break; 1491 case ISN_PUSHFUNC: 1492 tv->v_type = VAR_FUNC; 1493 if (iptr->isn_arg.string == NULL) 1494 tv->vval.v_string = NULL; 1495 else 1496 tv->vval.v_string = 1497 vim_strsave(iptr->isn_arg.string); 1498 break; 1499 case ISN_PUSHCHANNEL: 1500 #ifdef FEAT_JOB_CHANNEL 1501 tv->v_type = VAR_CHANNEL; 1502 tv->vval.v_channel = iptr->isn_arg.channel; 1503 if (tv->vval.v_channel != NULL) 1504 ++tv->vval.v_channel->ch_refcount; 1505 #endif 1506 break; 1507 case ISN_PUSHJOB: 1508 #ifdef FEAT_JOB_CHANNEL 1509 tv->v_type = VAR_JOB; 1510 tv->vval.v_job = iptr->isn_arg.job; 1511 if (tv->vval.v_job != NULL) 1512 ++tv->vval.v_job->jv_refcount; 1513 #endif 1514 break; 1515 default: 1516 tv->v_type = VAR_STRING; 1517 tv->vval.v_string = vim_strsave( 1518 iptr->isn_arg.string == NULL 1519 ? (char_u *)"" : iptr->isn_arg.string); 1520 } 1521 break; 1522 1523 case ISN_UNLET: 1524 if (do_unlet(iptr->isn_arg.unlet.ul_name, 1525 iptr->isn_arg.unlet.ul_forceit) == FAIL) 1526 goto on_error; 1527 break; 1528 case ISN_UNLETENV: 1529 vim_unsetenv(iptr->isn_arg.unlet.ul_name); 1530 break; 1531 1532 // create a list from items on the stack; uses a single allocation 1533 // for the list header and the items 1534 case ISN_NEWLIST: 1535 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL) 1536 goto failed; 1537 break; 1538 1539 // create a dict from items on the stack 1540 case ISN_NEWDICT: 1541 { 1542 int count = iptr->isn_arg.number; 1543 dict_T *dict = dict_alloc(); 1544 dictitem_T *item; 1545 1546 if (dict == NULL) 1547 goto failed; 1548 for (idx = 0; idx < count; ++idx) 1549 { 1550 // have already checked key type is VAR_STRING 1551 tv = STACK_TV_BOT(2 * (idx - count)); 1552 // check key is unique 1553 item = dict_find(dict, tv->vval.v_string, -1); 1554 if (item != NULL) 1555 { 1556 semsg(_(e_duplicate_key), tv->vval.v_string); 1557 dict_unref(dict); 1558 goto on_error; 1559 } 1560 item = dictitem_alloc(tv->vval.v_string); 1561 clear_tv(tv); 1562 if (item == NULL) 1563 { 1564 dict_unref(dict); 1565 goto failed; 1566 } 1567 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1); 1568 item->di_tv.v_lock = 0; 1569 if (dict_add(dict, item) == FAIL) 1570 { 1571 // can this ever happen? 1572 dict_unref(dict); 1573 goto failed; 1574 } 1575 } 1576 1577 if (count > 0) 1578 ectx.ec_stack.ga_len -= 2 * count - 1; 1579 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1580 goto failed; 1581 else 1582 ++ectx.ec_stack.ga_len; 1583 tv = STACK_TV_BOT(-1); 1584 tv->v_type = VAR_DICT; 1585 tv->v_lock = 0; 1586 tv->vval.v_dict = dict; 1587 ++dict->dv_refcount; 1588 } 1589 break; 1590 1591 // call a :def function 1592 case ISN_DCALL: 1593 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, 1594 iptr->isn_arg.dfunc.cdf_argcount, 1595 &ectx) == FAIL) 1596 goto on_error; 1597 break; 1598 1599 // call a builtin function 1600 case ISN_BCALL: 1601 SOURCING_LNUM = iptr->isn_lnum; 1602 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx, 1603 iptr->isn_arg.bfunc.cbf_argcount, 1604 &ectx) == FAIL) 1605 goto on_error; 1606 break; 1607 1608 // call a funcref or partial 1609 case ISN_PCALL: 1610 { 1611 cpfunc_T *pfunc = &iptr->isn_arg.pfunc; 1612 int r; 1613 typval_T partial_tv; 1614 1615 SOURCING_LNUM = iptr->isn_lnum; 1616 if (pfunc->cpf_top) 1617 { 1618 // funcref is above the arguments 1619 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1); 1620 } 1621 else 1622 { 1623 // Get the funcref from the stack. 1624 --ectx.ec_stack.ga_len; 1625 partial_tv = *STACK_TV_BOT(0); 1626 tv = &partial_tv; 1627 } 1628 r = call_partial(tv, pfunc->cpf_argcount, &ectx); 1629 if (tv == &partial_tv) 1630 clear_tv(&partial_tv); 1631 if (r == FAIL) 1632 goto on_error; 1633 } 1634 break; 1635 1636 case ISN_PCALL_END: 1637 // PCALL finished, arguments have been consumed and replaced by 1638 // the return value. Now clear the funcref from the stack, 1639 // and move the return value in its place. 1640 --ectx.ec_stack.ga_len; 1641 clear_tv(STACK_TV_BOT(-1)); 1642 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0); 1643 break; 1644 1645 // call a user defined function or funcref/partial 1646 case ISN_UCALL: 1647 { 1648 cufunc_T *cufunc = &iptr->isn_arg.ufunc; 1649 1650 SOURCING_LNUM = iptr->isn_lnum; 1651 if (call_eval_func(cufunc->cuf_name, 1652 cufunc->cuf_argcount, &ectx, iptr) == FAIL) 1653 goto on_error; 1654 } 1655 break; 1656 1657 // return from a :def function call 1658 case ISN_RETURN: 1659 { 1660 garray_T *trystack = &ectx.ec_trystack; 1661 trycmd_T *trycmd = NULL; 1662 1663 if (trystack->ga_len > 0) 1664 trycmd = ((trycmd_T *)trystack->ga_data) 1665 + trystack->ga_len - 1; 1666 if (trycmd != NULL 1667 && trycmd->tcd_frame_idx == ectx.ec_frame_idx 1668 && trycmd->tcd_finally_idx != 0) 1669 { 1670 // jump to ":finally" 1671 ectx.ec_iidx = trycmd->tcd_finally_idx; 1672 trycmd->tcd_return = TRUE; 1673 } 1674 else 1675 goto func_return; 1676 } 1677 break; 1678 1679 // push a function reference to a compiled function 1680 case ISN_FUNCREF: 1681 { 1682 partial_T *pt = NULL; 1683 dfunc_T *pt_dfunc; 1684 1685 pt = ALLOC_CLEAR_ONE(partial_T); 1686 if (pt == NULL) 1687 goto failed; 1688 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1689 { 1690 vim_free(pt); 1691 goto failed; 1692 } 1693 pt_dfunc = ((dfunc_T *)def_functions.ga_data) 1694 + iptr->isn_arg.funcref.fr_func; 1695 pt->pt_func = pt_dfunc->df_ufunc; 1696 pt->pt_refcount = 1; 1697 ++pt_dfunc->df_ufunc->uf_refcount; 1698 1699 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE) 1700 { 1701 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 1702 + ectx.ec_dfunc_idx; 1703 1704 // The closure needs to find arguments and local 1705 // variables in the current stack. 1706 pt->pt_ectx_stack = &ectx.ec_stack; 1707 pt->pt_ectx_frame = ectx.ec_frame_idx; 1708 1709 // If this function returns and the closure is still 1710 // used, we need to make a copy of the context 1711 // (arguments and local variables). Store a reference 1712 // to the partial so we can handle that. 1713 ++pt->pt_refcount; 1714 tv = STACK_TV_VAR(dfunc->df_varcount 1715 + iptr->isn_arg.funcref.fr_var_idx); 1716 if (tv->v_type == VAR_PARTIAL) 1717 { 1718 // TODO: use a garray_T on ectx. 1719 emsg("Multiple closures not supported yet"); 1720 goto failed; 1721 } 1722 tv->v_type = VAR_PARTIAL; 1723 tv->vval.v_partial = pt; 1724 } 1725 1726 tv = STACK_TV_BOT(0); 1727 ++ectx.ec_stack.ga_len; 1728 tv->vval.v_partial = pt; 1729 tv->v_type = VAR_PARTIAL; 1730 tv->v_lock = 0; 1731 } 1732 break; 1733 1734 // Create a global function from a lambda. 1735 case ISN_NEWFUNC: 1736 { 1737 newfunc_T *newfunc = &iptr->isn_arg.newfunc; 1738 1739 copy_func(newfunc->nf_lambda, newfunc->nf_global); 1740 } 1741 break; 1742 1743 // jump if a condition is met 1744 case ISN_JUMP: 1745 { 1746 jumpwhen_T when = iptr->isn_arg.jump.jump_when; 1747 int jump = TRUE; 1748 1749 if (when != JUMP_ALWAYS) 1750 { 1751 tv = STACK_TV_BOT(-1); 1752 jump = tv2bool(tv); 1753 if (when == JUMP_IF_FALSE 1754 || when == JUMP_AND_KEEP_IF_FALSE) 1755 jump = !jump; 1756 if (when == JUMP_IF_FALSE || !jump) 1757 { 1758 // drop the value from the stack 1759 clear_tv(tv); 1760 --ectx.ec_stack.ga_len; 1761 } 1762 } 1763 if (jump) 1764 ectx.ec_iidx = iptr->isn_arg.jump.jump_where; 1765 } 1766 break; 1767 1768 // top of a for loop 1769 case ISN_FOR: 1770 { 1771 list_T *list = STACK_TV_BOT(-1)->vval.v_list; 1772 typval_T *idxtv = 1773 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx); 1774 1775 // push the next item from the list 1776 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1777 goto failed; 1778 if (++idxtv->vval.v_number >= list->lv_len) 1779 // past the end of the list, jump to "endfor" 1780 ectx.ec_iidx = iptr->isn_arg.forloop.for_end; 1781 else if (list->lv_first == &range_list_item) 1782 { 1783 // non-materialized range() list 1784 tv = STACK_TV_BOT(0); 1785 tv->v_type = VAR_NUMBER; 1786 tv->v_lock = 0; 1787 tv->vval.v_number = list_find_nr( 1788 list, idxtv->vval.v_number, NULL); 1789 ++ectx.ec_stack.ga_len; 1790 } 1791 else 1792 { 1793 listitem_T *li = list_find(list, idxtv->vval.v_number); 1794 1795 copy_tv(&li->li_tv, STACK_TV_BOT(0)); 1796 ++ectx.ec_stack.ga_len; 1797 } 1798 } 1799 break; 1800 1801 // start of ":try" block 1802 case ISN_TRY: 1803 { 1804 trycmd_T *trycmd = NULL; 1805 1806 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL) 1807 goto failed; 1808 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data) 1809 + ectx.ec_trystack.ga_len; 1810 ++ectx.ec_trystack.ga_len; 1811 ++trylevel; 1812 trycmd->tcd_frame_idx = ectx.ec_frame_idx; 1813 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch; 1814 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally; 1815 trycmd->tcd_caught = FALSE; 1816 } 1817 break; 1818 1819 case ISN_PUSHEXC: 1820 if (current_exception == NULL) 1821 { 1822 iemsg("Evaluating catch while current_exception is NULL"); 1823 goto failed; 1824 } 1825 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1826 goto failed; 1827 tv = STACK_TV_BOT(0); 1828 ++ectx.ec_stack.ga_len; 1829 tv->v_type = VAR_STRING; 1830 tv->v_lock = 0; 1831 tv->vval.v_string = vim_strsave( 1832 (char_u *)current_exception->value); 1833 break; 1834 1835 case ISN_CATCH: 1836 { 1837 garray_T *trystack = &ectx.ec_trystack; 1838 1839 if (trystack->ga_len > 0) 1840 { 1841 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) 1842 + trystack->ga_len - 1; 1843 trycmd->tcd_caught = TRUE; 1844 } 1845 did_emsg = got_int = did_throw = FALSE; 1846 catch_exception(current_exception); 1847 } 1848 break; 1849 1850 // end of ":try" block 1851 case ISN_ENDTRY: 1852 { 1853 garray_T *trystack = &ectx.ec_trystack; 1854 1855 if (trystack->ga_len > 0) 1856 { 1857 trycmd_T *trycmd = NULL; 1858 1859 --trystack->ga_len; 1860 --trylevel; 1861 ectx.ec_in_catch = FALSE; 1862 trycmd = ((trycmd_T *)trystack->ga_data) 1863 + trystack->ga_len; 1864 if (trycmd->tcd_caught && current_exception != NULL) 1865 { 1866 // discard the exception 1867 if (caught_stack == current_exception) 1868 caught_stack = caught_stack->caught; 1869 discard_current_exception(); 1870 } 1871 1872 if (trycmd->tcd_return) 1873 goto func_return; 1874 } 1875 } 1876 break; 1877 1878 case ISN_THROW: 1879 --ectx.ec_stack.ga_len; 1880 tv = STACK_TV_BOT(0); 1881 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL) 1882 { 1883 vim_free(tv->vval.v_string); 1884 goto failed; 1885 } 1886 did_throw = TRUE; 1887 break; 1888 1889 // compare with special values 1890 case ISN_COMPAREBOOL: 1891 case ISN_COMPARESPECIAL: 1892 { 1893 typval_T *tv1 = STACK_TV_BOT(-2); 1894 typval_T *tv2 = STACK_TV_BOT(-1); 1895 varnumber_T arg1 = tv1->vval.v_number; 1896 varnumber_T arg2 = tv2->vval.v_number; 1897 int res; 1898 1899 switch (iptr->isn_arg.op.op_type) 1900 { 1901 case EXPR_EQUAL: res = arg1 == arg2; break; 1902 case EXPR_NEQUAL: res = arg1 != arg2; break; 1903 default: res = 0; break; 1904 } 1905 1906 --ectx.ec_stack.ga_len; 1907 tv1->v_type = VAR_BOOL; 1908 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; 1909 } 1910 break; 1911 1912 // Operation with two number arguments 1913 case ISN_OPNR: 1914 case ISN_COMPARENR: 1915 { 1916 typval_T *tv1 = STACK_TV_BOT(-2); 1917 typval_T *tv2 = STACK_TV_BOT(-1); 1918 varnumber_T arg1 = tv1->vval.v_number; 1919 varnumber_T arg2 = tv2->vval.v_number; 1920 varnumber_T res; 1921 1922 switch (iptr->isn_arg.op.op_type) 1923 { 1924 case EXPR_MULT: res = arg1 * arg2; break; 1925 case EXPR_DIV: res = arg1 / arg2; break; 1926 case EXPR_REM: res = arg1 % arg2; break; 1927 case EXPR_SUB: res = arg1 - arg2; break; 1928 case EXPR_ADD: res = arg1 + arg2; break; 1929 1930 case EXPR_EQUAL: res = arg1 == arg2; break; 1931 case EXPR_NEQUAL: res = arg1 != arg2; break; 1932 case EXPR_GREATER: res = arg1 > arg2; break; 1933 case EXPR_GEQUAL: res = arg1 >= arg2; break; 1934 case EXPR_SMALLER: res = arg1 < arg2; break; 1935 case EXPR_SEQUAL: res = arg1 <= arg2; break; 1936 default: res = 0; break; 1937 } 1938 1939 --ectx.ec_stack.ga_len; 1940 if (iptr->isn_type == ISN_COMPARENR) 1941 { 1942 tv1->v_type = VAR_BOOL; 1943 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; 1944 } 1945 else 1946 tv1->vval.v_number = res; 1947 } 1948 break; 1949 1950 // Computation with two float arguments 1951 case ISN_OPFLOAT: 1952 case ISN_COMPAREFLOAT: 1953 #ifdef FEAT_FLOAT 1954 { 1955 typval_T *tv1 = STACK_TV_BOT(-2); 1956 typval_T *tv2 = STACK_TV_BOT(-1); 1957 float_T arg1 = tv1->vval.v_float; 1958 float_T arg2 = tv2->vval.v_float; 1959 float_T res = 0; 1960 int cmp = FALSE; 1961 1962 switch (iptr->isn_arg.op.op_type) 1963 { 1964 case EXPR_MULT: res = arg1 * arg2; break; 1965 case EXPR_DIV: res = arg1 / arg2; break; 1966 case EXPR_SUB: res = arg1 - arg2; break; 1967 case EXPR_ADD: res = arg1 + arg2; break; 1968 1969 case EXPR_EQUAL: cmp = arg1 == arg2; break; 1970 case EXPR_NEQUAL: cmp = arg1 != arg2; break; 1971 case EXPR_GREATER: cmp = arg1 > arg2; break; 1972 case EXPR_GEQUAL: cmp = arg1 >= arg2; break; 1973 case EXPR_SMALLER: cmp = arg1 < arg2; break; 1974 case EXPR_SEQUAL: cmp = arg1 <= arg2; break; 1975 default: cmp = 0; break; 1976 } 1977 --ectx.ec_stack.ga_len; 1978 if (iptr->isn_type == ISN_COMPAREFLOAT) 1979 { 1980 tv1->v_type = VAR_BOOL; 1981 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 1982 } 1983 else 1984 tv1->vval.v_float = res; 1985 } 1986 #endif 1987 break; 1988 1989 case ISN_COMPARELIST: 1990 { 1991 typval_T *tv1 = STACK_TV_BOT(-2); 1992 typval_T *tv2 = STACK_TV_BOT(-1); 1993 list_T *arg1 = tv1->vval.v_list; 1994 list_T *arg2 = tv2->vval.v_list; 1995 int cmp = FALSE; 1996 int ic = iptr->isn_arg.op.op_ic; 1997 1998 switch (iptr->isn_arg.op.op_type) 1999 { 2000 case EXPR_EQUAL: cmp = 2001 list_equal(arg1, arg2, ic, FALSE); break; 2002 case EXPR_NEQUAL: cmp = 2003 !list_equal(arg1, arg2, ic, FALSE); break; 2004 case EXPR_IS: cmp = arg1 == arg2; break; 2005 case EXPR_ISNOT: cmp = arg1 != arg2; break; 2006 default: cmp = 0; break; 2007 } 2008 --ectx.ec_stack.ga_len; 2009 clear_tv(tv1); 2010 clear_tv(tv2); 2011 tv1->v_type = VAR_BOOL; 2012 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 2013 } 2014 break; 2015 2016 case ISN_COMPAREBLOB: 2017 { 2018 typval_T *tv1 = STACK_TV_BOT(-2); 2019 typval_T *tv2 = STACK_TV_BOT(-1); 2020 blob_T *arg1 = tv1->vval.v_blob; 2021 blob_T *arg2 = tv2->vval.v_blob; 2022 int cmp = FALSE; 2023 2024 switch (iptr->isn_arg.op.op_type) 2025 { 2026 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break; 2027 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break; 2028 case EXPR_IS: cmp = arg1 == arg2; break; 2029 case EXPR_ISNOT: cmp = arg1 != arg2; break; 2030 default: cmp = 0; break; 2031 } 2032 --ectx.ec_stack.ga_len; 2033 clear_tv(tv1); 2034 clear_tv(tv2); 2035 tv1->v_type = VAR_BOOL; 2036 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 2037 } 2038 break; 2039 2040 // TODO: handle separately 2041 case ISN_COMPARESTRING: 2042 case ISN_COMPAREDICT: 2043 case ISN_COMPAREFUNC: 2044 case ISN_COMPAREANY: 2045 { 2046 typval_T *tv1 = STACK_TV_BOT(-2); 2047 typval_T *tv2 = STACK_TV_BOT(-1); 2048 exptype_T exptype = iptr->isn_arg.op.op_type; 2049 int ic = iptr->isn_arg.op.op_ic; 2050 2051 typval_compare(tv1, tv2, exptype, ic); 2052 clear_tv(tv2); 2053 --ectx.ec_stack.ga_len; 2054 } 2055 break; 2056 2057 case ISN_ADDLIST: 2058 case ISN_ADDBLOB: 2059 { 2060 typval_T *tv1 = STACK_TV_BOT(-2); 2061 typval_T *tv2 = STACK_TV_BOT(-1); 2062 2063 if (iptr->isn_type == ISN_ADDLIST) 2064 eval_addlist(tv1, tv2); 2065 else 2066 eval_addblob(tv1, tv2); 2067 clear_tv(tv2); 2068 --ectx.ec_stack.ga_len; 2069 } 2070 break; 2071 2072 // Computation with two arguments of unknown type 2073 case ISN_OPANY: 2074 { 2075 typval_T *tv1 = STACK_TV_BOT(-2); 2076 typval_T *tv2 = STACK_TV_BOT(-1); 2077 varnumber_T n1, n2; 2078 #ifdef FEAT_FLOAT 2079 float_T f1 = 0, f2 = 0; 2080 #endif 2081 int error = FALSE; 2082 2083 if (iptr->isn_arg.op.op_type == EXPR_ADD) 2084 { 2085 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST) 2086 { 2087 eval_addlist(tv1, tv2); 2088 clear_tv(tv2); 2089 --ectx.ec_stack.ga_len; 2090 break; 2091 } 2092 else if (tv1->v_type == VAR_BLOB 2093 && tv2->v_type == VAR_BLOB) 2094 { 2095 eval_addblob(tv1, tv2); 2096 clear_tv(tv2); 2097 --ectx.ec_stack.ga_len; 2098 break; 2099 } 2100 } 2101 #ifdef FEAT_FLOAT 2102 if (tv1->v_type == VAR_FLOAT) 2103 { 2104 f1 = tv1->vval.v_float; 2105 n1 = 0; 2106 } 2107 else 2108 #endif 2109 { 2110 n1 = tv_get_number_chk(tv1, &error); 2111 if (error) 2112 goto on_error; 2113 #ifdef FEAT_FLOAT 2114 if (tv2->v_type == VAR_FLOAT) 2115 f1 = n1; 2116 #endif 2117 } 2118 #ifdef FEAT_FLOAT 2119 if (tv2->v_type == VAR_FLOAT) 2120 { 2121 f2 = tv2->vval.v_float; 2122 n2 = 0; 2123 } 2124 else 2125 #endif 2126 { 2127 n2 = tv_get_number_chk(tv2, &error); 2128 if (error) 2129 goto on_error; 2130 #ifdef FEAT_FLOAT 2131 if (tv1->v_type == VAR_FLOAT) 2132 f2 = n2; 2133 #endif 2134 } 2135 #ifdef FEAT_FLOAT 2136 // if there is a float on either side the result is a float 2137 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT) 2138 { 2139 switch (iptr->isn_arg.op.op_type) 2140 { 2141 case EXPR_MULT: f1 = f1 * f2; break; 2142 case EXPR_DIV: f1 = f1 / f2; break; 2143 case EXPR_SUB: f1 = f1 - f2; break; 2144 case EXPR_ADD: f1 = f1 + f2; break; 2145 default: emsg(_(e_modulus)); 2146 goto on_error; 2147 } 2148 clear_tv(tv1); 2149 clear_tv(tv2); 2150 tv1->v_type = VAR_FLOAT; 2151 tv1->vval.v_float = f1; 2152 --ectx.ec_stack.ga_len; 2153 } 2154 else 2155 #endif 2156 { 2157 switch (iptr->isn_arg.op.op_type) 2158 { 2159 case EXPR_MULT: n1 = n1 * n2; break; 2160 case EXPR_DIV: n1 = num_divide(n1, n2); break; 2161 case EXPR_SUB: n1 = n1 - n2; break; 2162 case EXPR_ADD: n1 = n1 + n2; break; 2163 default: n1 = num_modulus(n1, n2); break; 2164 } 2165 clear_tv(tv1); 2166 clear_tv(tv2); 2167 tv1->v_type = VAR_NUMBER; 2168 tv1->vval.v_number = n1; 2169 --ectx.ec_stack.ga_len; 2170 } 2171 } 2172 break; 2173 2174 case ISN_CONCAT: 2175 { 2176 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string; 2177 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string; 2178 char_u *res; 2179 2180 res = concat_str(str1, str2); 2181 clear_tv(STACK_TV_BOT(-2)); 2182 clear_tv(STACK_TV_BOT(-1)); 2183 --ectx.ec_stack.ga_len; 2184 STACK_TV_BOT(-1)->vval.v_string = res; 2185 } 2186 break; 2187 2188 case ISN_STRINDEX: 2189 { 2190 char_u *s; 2191 varnumber_T n; 2192 char_u *res; 2193 2194 // string index: string is at stack-2, index at stack-1 2195 tv = STACK_TV_BOT(-2); 2196 if (tv->v_type != VAR_STRING) 2197 { 2198 emsg(_(e_stringreq)); 2199 goto on_error; 2200 } 2201 s = tv->vval.v_string; 2202 2203 tv = STACK_TV_BOT(-1); 2204 if (tv->v_type != VAR_NUMBER) 2205 { 2206 emsg(_(e_number_exp)); 2207 goto on_error; 2208 } 2209 n = tv->vval.v_number; 2210 2211 // The resulting variable is a string of a single 2212 // character. If the index is too big or negative the 2213 // result is empty. 2214 if (n < 0 || n >= (varnumber_T)STRLEN(s)) 2215 res = NULL; 2216 else 2217 res = vim_strnsave(s + n, 1); 2218 --ectx.ec_stack.ga_len; 2219 tv = STACK_TV_BOT(-1); 2220 vim_free(tv->vval.v_string); 2221 tv->vval.v_string = res; 2222 } 2223 break; 2224 2225 case ISN_LISTINDEX: 2226 { 2227 list_T *list; 2228 varnumber_T n; 2229 listitem_T *li; 2230 typval_T temp_tv; 2231 2232 // list index: list is at stack-2, index at stack-1 2233 tv = STACK_TV_BOT(-2); 2234 if (tv->v_type != VAR_LIST) 2235 { 2236 emsg(_(e_listreq)); 2237 goto on_error; 2238 } 2239 list = tv->vval.v_list; 2240 2241 tv = STACK_TV_BOT(-1); 2242 if (tv->v_type != VAR_NUMBER) 2243 { 2244 emsg(_(e_number_exp)); 2245 goto on_error; 2246 } 2247 n = tv->vval.v_number; 2248 clear_tv(tv); 2249 if ((li = list_find(list, n)) == NULL) 2250 { 2251 semsg(_(e_listidx), n); 2252 goto on_error; 2253 } 2254 --ectx.ec_stack.ga_len; 2255 // Clear the list after getting the item, to avoid that it 2256 // makes the item invalid. 2257 tv = STACK_TV_BOT(-1); 2258 temp_tv = *tv; 2259 copy_tv(&li->li_tv, tv); 2260 clear_tv(&temp_tv); 2261 } 2262 break; 2263 2264 case ISN_SLICE: 2265 { 2266 list_T *list; 2267 int count = iptr->isn_arg.number; 2268 2269 // type will have been checked to be a list 2270 tv = STACK_TV_BOT(-1); 2271 list = tv->vval.v_list; 2272 2273 // no error for short list, expect it to be checked earlier 2274 if (list != NULL && list->lv_len >= count) 2275 { 2276 list_T *newlist = list_slice(list, 2277 count, list->lv_len - 1); 2278 2279 if (newlist != NULL) 2280 { 2281 list_unref(list); 2282 tv->vval.v_list = newlist; 2283 ++newlist->lv_refcount; 2284 } 2285 } 2286 } 2287 break; 2288 2289 case ISN_GETITEM: 2290 { 2291 listitem_T *li; 2292 int index = iptr->isn_arg.number; 2293 2294 // Get list item: list is at stack-1, push item. 2295 // List type and length is checked for when compiling. 2296 tv = STACK_TV_BOT(-1); 2297 li = list_find(tv->vval.v_list, index); 2298 2299 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2300 goto failed; 2301 ++ectx.ec_stack.ga_len; 2302 copy_tv(&li->li_tv, STACK_TV_BOT(-1)); 2303 } 2304 break; 2305 2306 case ISN_MEMBER: 2307 { 2308 dict_T *dict; 2309 char_u *key; 2310 dictitem_T *di; 2311 typval_T temp_tv; 2312 2313 // dict member: dict is at stack-2, key at stack-1 2314 tv = STACK_TV_BOT(-2); 2315 // no need to check for VAR_DICT, CHECKTYPE will check. 2316 dict = tv->vval.v_dict; 2317 2318 tv = STACK_TV_BOT(-1); 2319 // no need to check for VAR_STRING, 2STRING will check. 2320 key = tv->vval.v_string; 2321 2322 if ((di = dict_find(dict, key, -1)) == NULL) 2323 { 2324 semsg(_(e_dictkey), key); 2325 goto on_error; 2326 } 2327 clear_tv(tv); 2328 --ectx.ec_stack.ga_len; 2329 // Clear the dict after getting the item, to avoid that it 2330 // make the item invalid. 2331 tv = STACK_TV_BOT(-1); 2332 temp_tv = *tv; 2333 copy_tv(&di->di_tv, tv); 2334 clear_tv(&temp_tv); 2335 } 2336 break; 2337 2338 // dict member with string key 2339 case ISN_STRINGMEMBER: 2340 { 2341 dict_T *dict; 2342 dictitem_T *di; 2343 typval_T temp_tv; 2344 2345 tv = STACK_TV_BOT(-1); 2346 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL) 2347 { 2348 emsg(_(e_dictreq)); 2349 goto on_error; 2350 } 2351 dict = tv->vval.v_dict; 2352 2353 if ((di = dict_find(dict, iptr->isn_arg.string, -1)) 2354 == NULL) 2355 { 2356 semsg(_(e_dictkey), iptr->isn_arg.string); 2357 goto on_error; 2358 } 2359 // Clear the dict after getting the item, to avoid that it 2360 // make the item invalid. 2361 temp_tv = *tv; 2362 copy_tv(&di->di_tv, tv); 2363 clear_tv(&temp_tv); 2364 } 2365 break; 2366 2367 case ISN_NEGATENR: 2368 tv = STACK_TV_BOT(-1); 2369 if (tv->v_type != VAR_NUMBER 2370 #ifdef FEAT_FLOAT 2371 && tv->v_type != VAR_FLOAT 2372 #endif 2373 ) 2374 { 2375 emsg(_(e_number_exp)); 2376 goto on_error; 2377 } 2378 #ifdef FEAT_FLOAT 2379 if (tv->v_type == VAR_FLOAT) 2380 tv->vval.v_float = -tv->vval.v_float; 2381 else 2382 #endif 2383 tv->vval.v_number = -tv->vval.v_number; 2384 break; 2385 2386 case ISN_CHECKNR: 2387 { 2388 int error = FALSE; 2389 2390 tv = STACK_TV_BOT(-1); 2391 if (check_not_string(tv) == FAIL) 2392 goto on_error; 2393 (void)tv_get_number_chk(tv, &error); 2394 if (error) 2395 goto on_error; 2396 } 2397 break; 2398 2399 case ISN_CHECKTYPE: 2400 { 2401 checktype_T *ct = &iptr->isn_arg.type; 2402 2403 tv = STACK_TV_BOT(ct->ct_off); 2404 // TODO: better type comparison 2405 if (tv->v_type != ct->ct_type 2406 && !((tv->v_type == VAR_PARTIAL 2407 && ct->ct_type == VAR_FUNC) 2408 || (tv->v_type == VAR_FUNC 2409 && ct->ct_type == VAR_PARTIAL))) 2410 { 2411 semsg(_("E1029: Expected %s but got %s"), 2412 vartype_name(ct->ct_type), 2413 vartype_name(tv->v_type)); 2414 goto on_error; 2415 } 2416 } 2417 break; 2418 2419 case ISN_CHECKLEN: 2420 { 2421 int min_len = iptr->isn_arg.checklen.cl_min_len; 2422 list_T *list = NULL; 2423 2424 tv = STACK_TV_BOT(-1); 2425 if (tv->v_type == VAR_LIST) 2426 list = tv->vval.v_list; 2427 if (list == NULL || list->lv_len < min_len 2428 || (list->lv_len > min_len 2429 && !iptr->isn_arg.checklen.cl_more_OK)) 2430 { 2431 semsg(_("E1093: Expected %d items but got %d"), 2432 min_len, list == NULL ? 0 : list->lv_len); 2433 goto on_error; 2434 } 2435 } 2436 break; 2437 2438 case ISN_2BOOL: 2439 { 2440 int n; 2441 2442 tv = STACK_TV_BOT(-1); 2443 n = tv2bool(tv); 2444 if (iptr->isn_arg.number) // invert 2445 n = !n; 2446 clear_tv(tv); 2447 tv->v_type = VAR_BOOL; 2448 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE; 2449 } 2450 break; 2451 2452 case ISN_2STRING: 2453 { 2454 char_u *str; 2455 2456 tv = STACK_TV_BOT(iptr->isn_arg.number); 2457 if (tv->v_type != VAR_STRING) 2458 { 2459 str = typval_tostring(tv); 2460 clear_tv(tv); 2461 tv->v_type = VAR_STRING; 2462 tv->vval.v_string = str; 2463 } 2464 } 2465 break; 2466 2467 case ISN_SHUFFLE: 2468 { 2469 typval_T tmp_tv; 2470 int item = iptr->isn_arg.shuffle.shfl_item; 2471 int up = iptr->isn_arg.shuffle.shfl_up; 2472 2473 tmp_tv = *STACK_TV_BOT(-item); 2474 for ( ; up > 0 && item > 1; --up) 2475 { 2476 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1); 2477 --item; 2478 } 2479 *STACK_TV_BOT(-item) = tmp_tv; 2480 } 2481 break; 2482 2483 case ISN_DROP: 2484 --ectx.ec_stack.ga_len; 2485 clear_tv(STACK_TV_BOT(0)); 2486 break; 2487 } 2488 continue; 2489 2490 func_return: 2491 // Restore previous function. If the frame pointer is zero then there 2492 // is none and we are done. 2493 if (ectx.ec_frame_idx == initial_frame_idx) 2494 { 2495 if (handle_closure_in_use(&ectx, FALSE) == FAIL) 2496 // only fails when out of memory 2497 goto failed; 2498 goto done; 2499 } 2500 if (func_return(&ectx) == FAIL) 2501 // only fails when out of memory 2502 goto failed; 2503 continue; 2504 2505 on_error: 2506 if (trylevel == 0) 2507 goto failed; 2508 } 2509 2510 done: 2511 // function finished, get result from the stack. 2512 tv = STACK_TV_BOT(-1); 2513 *rettv = *tv; 2514 tv->v_type = VAR_UNKNOWN; 2515 ret = OK; 2516 2517 failed: 2518 // When failed need to unwind the call stack. 2519 while (ectx.ec_frame_idx != initial_frame_idx) 2520 func_return(&ectx); 2521 failed_early: 2522 current_sctx.sc_version = save_sc_version; 2523 2524 // Free all local variables, but not arguments. 2525 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx) 2526 clear_tv(STACK_TV(idx)); 2527 2528 vim_free(ectx.ec_stack.ga_data); 2529 vim_free(ectx.ec_trystack.ga_data); 2530 2531 if (ret != OK && called_emsg == called_emsg_before) 2532 semsg(_("E1099: Unknown error while executing %s"), 2533 printable_func_name(ufunc)); 2534 return ret; 2535 } 2536 2537 /* 2538 * ":dissassemble". 2539 * We don't really need this at runtime, but we do have tests that require it, 2540 * so always include this. 2541 */ 2542 void 2543 ex_disassemble(exarg_T *eap) 2544 { 2545 char_u *arg = eap->arg; 2546 char_u *fname; 2547 ufunc_T *ufunc; 2548 dfunc_T *dfunc; 2549 isn_T *instr; 2550 int current; 2551 int line_idx = 0; 2552 int prev_current = 0; 2553 int is_global = FALSE; 2554 2555 if (STRNCMP(arg, "<lambda>", 8) == 0) 2556 { 2557 arg += 8; 2558 (void)getdigits(&arg); 2559 fname = vim_strnsave(eap->arg, arg - eap->arg); 2560 } 2561 else 2562 fname = trans_function_name(&arg, &is_global, FALSE, 2563 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL); 2564 if (fname == NULL) 2565 { 2566 semsg(_(e_invarg2), eap->arg); 2567 return; 2568 } 2569 2570 ufunc = find_func(fname, is_global, NULL); 2571 if (ufunc == NULL) 2572 { 2573 char_u *p = untrans_function_name(fname); 2574 2575 if (p != NULL) 2576 // Try again without making it script-local. 2577 ufunc = find_func(p, FALSE, NULL); 2578 } 2579 vim_free(fname); 2580 if (ufunc == NULL) 2581 { 2582 semsg(_("E1061: Cannot find function %s"), eap->arg); 2583 return; 2584 } 2585 if (ufunc->uf_def_status == UF_TO_BE_COMPILED 2586 && compile_def_function(ufunc, FALSE, NULL) == FAIL) 2587 return; 2588 if (ufunc->uf_def_status != UF_COMPILED) 2589 { 2590 semsg(_("E1062: Function %s is not compiled"), eap->arg); 2591 return; 2592 } 2593 if (ufunc->uf_name_exp != NULL) 2594 msg((char *)ufunc->uf_name_exp); 2595 else 2596 msg((char *)ufunc->uf_name); 2597 2598 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; 2599 instr = dfunc->df_instr; 2600 for (current = 0; current < dfunc->df_instr_count; ++current) 2601 { 2602 isn_T *iptr = &instr[current]; 2603 char *line; 2604 2605 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len) 2606 { 2607 if (current > prev_current) 2608 { 2609 msg_puts("\n\n"); 2610 prev_current = current; 2611 } 2612 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++]; 2613 if (line != NULL) 2614 msg(line); 2615 } 2616 2617 switch (iptr->isn_type) 2618 { 2619 case ISN_EXEC: 2620 smsg("%4d EXEC %s", current, iptr->isn_arg.string); 2621 break; 2622 case ISN_EXECCONCAT: 2623 smsg("%4d EXECCONCAT %lld", current, 2624 (long long)iptr->isn_arg.number); 2625 break; 2626 case ISN_ECHO: 2627 { 2628 echo_T *echo = &iptr->isn_arg.echo; 2629 2630 smsg("%4d %s %d", current, 2631 echo->echo_with_white ? "ECHO" : "ECHON", 2632 echo->echo_count); 2633 } 2634 break; 2635 case ISN_EXECUTE: 2636 smsg("%4d EXECUTE %lld", current, 2637 (long long)(iptr->isn_arg.number)); 2638 break; 2639 case ISN_ECHOMSG: 2640 smsg("%4d ECHOMSG %lld", current, 2641 (long long)(iptr->isn_arg.number)); 2642 break; 2643 case ISN_ECHOERR: 2644 smsg("%4d ECHOERR %lld", current, 2645 (long long)(iptr->isn_arg.number)); 2646 break; 2647 case ISN_LOAD: 2648 case ISN_LOADOUTER: 2649 { 2650 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER"; 2651 2652 if (iptr->isn_arg.number < 0) 2653 smsg("%4d LOAD%s arg[%lld]", current, add, 2654 (long long)(iptr->isn_arg.number 2655 + STACK_FRAME_SIZE)); 2656 else 2657 smsg("%4d LOAD%s $%lld", current, add, 2658 (long long)(iptr->isn_arg.number)); 2659 } 2660 break; 2661 case ISN_LOADV: 2662 smsg("%4d LOADV v:%s", current, 2663 get_vim_var_name(iptr->isn_arg.number)); 2664 break; 2665 case ISN_LOADSCRIPT: 2666 { 2667 scriptitem_T *si = 2668 SCRIPT_ITEM(iptr->isn_arg.script.script_sid); 2669 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 2670 + iptr->isn_arg.script.script_idx; 2671 2672 smsg("%4d LOADSCRIPT %s from %s", current, 2673 sv->sv_name, si->sn_name); 2674 } 2675 break; 2676 case ISN_LOADS: 2677 { 2678 scriptitem_T *si = SCRIPT_ITEM( 2679 iptr->isn_arg.loadstore.ls_sid); 2680 2681 smsg("%4d LOADS s:%s from %s", current, 2682 iptr->isn_arg.loadstore.ls_name, si->sn_name); 2683 } 2684 break; 2685 case ISN_LOADG: 2686 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string); 2687 break; 2688 case ISN_LOADB: 2689 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string); 2690 break; 2691 case ISN_LOADW: 2692 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string); 2693 break; 2694 case ISN_LOADT: 2695 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string); 2696 break; 2697 case ISN_LOADGDICT: 2698 smsg("%4d LOAD g:", current); 2699 break; 2700 case ISN_LOADBDICT: 2701 smsg("%4d LOAD b:", current); 2702 break; 2703 case ISN_LOADWDICT: 2704 smsg("%4d LOAD w:", current); 2705 break; 2706 case ISN_LOADTDICT: 2707 smsg("%4d LOAD t:", current); 2708 break; 2709 case ISN_LOADOPT: 2710 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string); 2711 break; 2712 case ISN_LOADENV: 2713 smsg("%4d LOADENV %s", current, iptr->isn_arg.string); 2714 break; 2715 case ISN_LOADREG: 2716 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number)); 2717 break; 2718 2719 case ISN_STORE: 2720 case ISN_STOREOUTER: 2721 { 2722 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER"; 2723 2724 if (iptr->isn_arg.number < 0) 2725 smsg("%4d STORE%s arg[%lld]", current, add, 2726 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE)); 2727 else 2728 smsg("%4d STORE%s $%lld", current, add, 2729 (long long)(iptr->isn_arg.number)); 2730 } 2731 break; 2732 case ISN_STOREV: 2733 smsg("%4d STOREV v:%s", current, 2734 get_vim_var_name(iptr->isn_arg.number)); 2735 break; 2736 case ISN_STOREG: 2737 smsg("%4d STOREG %s", current, iptr->isn_arg.string); 2738 break; 2739 case ISN_STOREB: 2740 smsg("%4d STOREB %s", current, iptr->isn_arg.string); 2741 break; 2742 case ISN_STOREW: 2743 smsg("%4d STOREW %s", current, iptr->isn_arg.string); 2744 break; 2745 case ISN_STORET: 2746 smsg("%4d STORET %s", current, iptr->isn_arg.string); 2747 break; 2748 case ISN_STORES: 2749 { 2750 scriptitem_T *si = SCRIPT_ITEM( 2751 iptr->isn_arg.loadstore.ls_sid); 2752 2753 smsg("%4d STORES %s in %s", current, 2754 iptr->isn_arg.loadstore.ls_name, si->sn_name); 2755 } 2756 break; 2757 case ISN_STORESCRIPT: 2758 { 2759 scriptitem_T *si = 2760 SCRIPT_ITEM(iptr->isn_arg.script.script_sid); 2761 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 2762 + iptr->isn_arg.script.script_idx; 2763 2764 smsg("%4d STORESCRIPT %s in %s", current, 2765 sv->sv_name, si->sn_name); 2766 } 2767 break; 2768 case ISN_STOREOPT: 2769 smsg("%4d STOREOPT &%s", current, 2770 iptr->isn_arg.storeopt.so_name); 2771 break; 2772 case ISN_STOREENV: 2773 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string); 2774 break; 2775 case ISN_STOREREG: 2776 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number); 2777 break; 2778 case ISN_STORENR: 2779 smsg("%4d STORE %lld in $%d", current, 2780 iptr->isn_arg.storenr.stnr_val, 2781 iptr->isn_arg.storenr.stnr_idx); 2782 break; 2783 2784 case ISN_STORELIST: 2785 smsg("%4d STORELIST", current); 2786 break; 2787 2788 case ISN_STOREDICT: 2789 smsg("%4d STOREDICT", current); 2790 break; 2791 2792 // constants 2793 case ISN_PUSHNR: 2794 smsg("%4d PUSHNR %lld", current, 2795 (long long)(iptr->isn_arg.number)); 2796 break; 2797 case ISN_PUSHBOOL: 2798 case ISN_PUSHSPEC: 2799 smsg("%4d PUSH %s", current, 2800 get_var_special_name(iptr->isn_arg.number)); 2801 break; 2802 case ISN_PUSHF: 2803 #ifdef FEAT_FLOAT 2804 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber); 2805 #endif 2806 break; 2807 case ISN_PUSHS: 2808 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string); 2809 break; 2810 case ISN_PUSHBLOB: 2811 { 2812 char_u *r; 2813 char_u numbuf[NUMBUFLEN]; 2814 char_u *tofree; 2815 2816 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf); 2817 smsg("%4d PUSHBLOB %s", current, r); 2818 vim_free(tofree); 2819 } 2820 break; 2821 case ISN_PUSHFUNC: 2822 { 2823 char *name = (char *)iptr->isn_arg.string; 2824 2825 smsg("%4d PUSHFUNC \"%s\"", current, 2826 name == NULL ? "[none]" : name); 2827 } 2828 break; 2829 case ISN_PUSHCHANNEL: 2830 #ifdef FEAT_JOB_CHANNEL 2831 { 2832 channel_T *channel = iptr->isn_arg.channel; 2833 2834 smsg("%4d PUSHCHANNEL %d", current, 2835 channel == NULL ? 0 : channel->ch_id); 2836 } 2837 #endif 2838 break; 2839 case ISN_PUSHJOB: 2840 #ifdef FEAT_JOB_CHANNEL 2841 { 2842 typval_T tv; 2843 char_u *name; 2844 2845 tv.v_type = VAR_JOB; 2846 tv.vval.v_job = iptr->isn_arg.job; 2847 name = tv_get_string(&tv); 2848 smsg("%4d PUSHJOB \"%s\"", current, name); 2849 } 2850 #endif 2851 break; 2852 case ISN_PUSHEXC: 2853 smsg("%4d PUSH v:exception", current); 2854 break; 2855 case ISN_UNLET: 2856 smsg("%4d UNLET%s %s", current, 2857 iptr->isn_arg.unlet.ul_forceit ? "!" : "", 2858 iptr->isn_arg.unlet.ul_name); 2859 break; 2860 case ISN_UNLETENV: 2861 smsg("%4d UNLETENV%s $%s", current, 2862 iptr->isn_arg.unlet.ul_forceit ? "!" : "", 2863 iptr->isn_arg.unlet.ul_name); 2864 break; 2865 case ISN_NEWLIST: 2866 smsg("%4d NEWLIST size %lld", current, 2867 (long long)(iptr->isn_arg.number)); 2868 break; 2869 case ISN_NEWDICT: 2870 smsg("%4d NEWDICT size %lld", current, 2871 (long long)(iptr->isn_arg.number)); 2872 break; 2873 2874 // function call 2875 case ISN_BCALL: 2876 { 2877 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc; 2878 2879 smsg("%4d BCALL %s(argc %d)", current, 2880 internal_func_name(cbfunc->cbf_idx), 2881 cbfunc->cbf_argcount); 2882 } 2883 break; 2884 case ISN_DCALL: 2885 { 2886 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc; 2887 dfunc_T *df = ((dfunc_T *)def_functions.ga_data) 2888 + cdfunc->cdf_idx; 2889 2890 smsg("%4d DCALL %s(argc %d)", current, 2891 df->df_ufunc->uf_name_exp != NULL 2892 ? df->df_ufunc->uf_name_exp 2893 : df->df_ufunc->uf_name, cdfunc->cdf_argcount); 2894 } 2895 break; 2896 case ISN_UCALL: 2897 { 2898 cufunc_T *cufunc = &iptr->isn_arg.ufunc; 2899 2900 smsg("%4d UCALL %s(argc %d)", current, 2901 cufunc->cuf_name, cufunc->cuf_argcount); 2902 } 2903 break; 2904 case ISN_PCALL: 2905 { 2906 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc; 2907 2908 smsg("%4d PCALL%s (argc %d)", current, 2909 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount); 2910 } 2911 break; 2912 case ISN_PCALL_END: 2913 smsg("%4d PCALL end", current); 2914 break; 2915 case ISN_RETURN: 2916 smsg("%4d RETURN", current); 2917 break; 2918 case ISN_FUNCREF: 2919 { 2920 funcref_T *funcref = &iptr->isn_arg.funcref; 2921 dfunc_T *df = ((dfunc_T *)def_functions.ga_data) 2922 + funcref->fr_func; 2923 2924 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name, 2925 funcref->fr_var_idx + dfunc->df_varcount); 2926 } 2927 break; 2928 2929 case ISN_NEWFUNC: 2930 { 2931 newfunc_T *newfunc = &iptr->isn_arg.newfunc; 2932 2933 smsg("%4d NEWFUNC %s %s", current, 2934 newfunc->nf_lambda, newfunc->nf_global); 2935 } 2936 break; 2937 2938 case ISN_JUMP: 2939 { 2940 char *when = "?"; 2941 2942 switch (iptr->isn_arg.jump.jump_when) 2943 { 2944 case JUMP_ALWAYS: 2945 when = "JUMP"; 2946 break; 2947 case JUMP_AND_KEEP_IF_TRUE: 2948 when = "JUMP_AND_KEEP_IF_TRUE"; 2949 break; 2950 case JUMP_IF_FALSE: 2951 when = "JUMP_IF_FALSE"; 2952 break; 2953 case JUMP_AND_KEEP_IF_FALSE: 2954 when = "JUMP_AND_KEEP_IF_FALSE"; 2955 break; 2956 } 2957 smsg("%4d %s -> %d", current, when, 2958 iptr->isn_arg.jump.jump_where); 2959 } 2960 break; 2961 2962 case ISN_FOR: 2963 { 2964 forloop_T *forloop = &iptr->isn_arg.forloop; 2965 2966 smsg("%4d FOR $%d -> %d", current, 2967 forloop->for_idx, forloop->for_end); 2968 } 2969 break; 2970 2971 case ISN_TRY: 2972 { 2973 try_T *try = &iptr->isn_arg.try; 2974 2975 smsg("%4d TRY catch -> %d, finally -> %d", current, 2976 try->try_catch, try->try_finally); 2977 } 2978 break; 2979 case ISN_CATCH: 2980 // TODO 2981 smsg("%4d CATCH", current); 2982 break; 2983 case ISN_ENDTRY: 2984 smsg("%4d ENDTRY", current); 2985 break; 2986 case ISN_THROW: 2987 smsg("%4d THROW", current); 2988 break; 2989 2990 // expression operations on number 2991 case ISN_OPNR: 2992 case ISN_OPFLOAT: 2993 case ISN_OPANY: 2994 { 2995 char *what; 2996 char *ins; 2997 2998 switch (iptr->isn_arg.op.op_type) 2999 { 3000 case EXPR_MULT: what = "*"; break; 3001 case EXPR_DIV: what = "/"; break; 3002 case EXPR_REM: what = "%"; break; 3003 case EXPR_SUB: what = "-"; break; 3004 case EXPR_ADD: what = "+"; break; 3005 default: what = "???"; break; 3006 } 3007 switch (iptr->isn_type) 3008 { 3009 case ISN_OPNR: ins = "OPNR"; break; 3010 case ISN_OPFLOAT: ins = "OPFLOAT"; break; 3011 case ISN_OPANY: ins = "OPANY"; break; 3012 default: ins = "???"; break; 3013 } 3014 smsg("%4d %s %s", current, ins, what); 3015 } 3016 break; 3017 3018 case ISN_COMPAREBOOL: 3019 case ISN_COMPARESPECIAL: 3020 case ISN_COMPARENR: 3021 case ISN_COMPAREFLOAT: 3022 case ISN_COMPARESTRING: 3023 case ISN_COMPAREBLOB: 3024 case ISN_COMPARELIST: 3025 case ISN_COMPAREDICT: 3026 case ISN_COMPAREFUNC: 3027 case ISN_COMPAREANY: 3028 { 3029 char *p; 3030 char buf[10]; 3031 char *type; 3032 3033 switch (iptr->isn_arg.op.op_type) 3034 { 3035 case EXPR_EQUAL: p = "=="; break; 3036 case EXPR_NEQUAL: p = "!="; break; 3037 case EXPR_GREATER: p = ">"; break; 3038 case EXPR_GEQUAL: p = ">="; break; 3039 case EXPR_SMALLER: p = "<"; break; 3040 case EXPR_SEQUAL: p = "<="; break; 3041 case EXPR_MATCH: p = "=~"; break; 3042 case EXPR_IS: p = "is"; break; 3043 case EXPR_ISNOT: p = "isnot"; break; 3044 case EXPR_NOMATCH: p = "!~"; break; 3045 default: p = "???"; break; 3046 } 3047 STRCPY(buf, p); 3048 if (iptr->isn_arg.op.op_ic == TRUE) 3049 strcat(buf, "?"); 3050 switch(iptr->isn_type) 3051 { 3052 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break; 3053 case ISN_COMPARESPECIAL: 3054 type = "COMPARESPECIAL"; break; 3055 case ISN_COMPARENR: type = "COMPARENR"; break; 3056 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break; 3057 case ISN_COMPARESTRING: 3058 type = "COMPARESTRING"; break; 3059 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break; 3060 case ISN_COMPARELIST: type = "COMPARELIST"; break; 3061 case ISN_COMPAREDICT: type = "COMPAREDICT"; break; 3062 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break; 3063 case ISN_COMPAREANY: type = "COMPAREANY"; break; 3064 default: type = "???"; break; 3065 } 3066 3067 smsg("%4d %s %s", current, type, buf); 3068 } 3069 break; 3070 3071 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break; 3072 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break; 3073 3074 // expression operations 3075 case ISN_CONCAT: smsg("%4d CONCAT", current); break; 3076 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break; 3077 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break; 3078 case ISN_SLICE: smsg("%4d SLICE %lld", 3079 current, iptr->isn_arg.number); break; 3080 case ISN_GETITEM: smsg("%4d ITEM %lld", 3081 current, iptr->isn_arg.number); break; 3082 case ISN_MEMBER: smsg("%4d MEMBER", current); break; 3083 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current, 3084 iptr->isn_arg.string); break; 3085 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break; 3086 3087 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break; 3088 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current, 3089 vartype_name(iptr->isn_arg.type.ct_type), 3090 iptr->isn_arg.type.ct_off); 3091 break; 3092 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current, 3093 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "", 3094 iptr->isn_arg.checklen.cl_min_len); 3095 break; 3096 case ISN_2BOOL: if (iptr->isn_arg.number) 3097 smsg("%4d INVERT (!val)", current); 3098 else 3099 smsg("%4d 2BOOL (!!val)", current); 3100 break; 3101 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current, 3102 (long long)(iptr->isn_arg.number)); 3103 break; 3104 3105 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current, 3106 iptr->isn_arg.shuffle.shfl_item, 3107 iptr->isn_arg.shuffle.shfl_up); 3108 break; 3109 case ISN_DROP: smsg("%4d DROP", current); break; 3110 } 3111 } 3112 } 3113 3114 /* 3115 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty 3116 * list, etc. Mostly like what JavaScript does, except that empty list and 3117 * empty dictionary are FALSE. 3118 */ 3119 int 3120 tv2bool(typval_T *tv) 3121 { 3122 switch (tv->v_type) 3123 { 3124 case VAR_NUMBER: 3125 return tv->vval.v_number != 0; 3126 case VAR_FLOAT: 3127 #ifdef FEAT_FLOAT 3128 return tv->vval.v_float != 0.0; 3129 #else 3130 break; 3131 #endif 3132 case VAR_PARTIAL: 3133 return tv->vval.v_partial != NULL; 3134 case VAR_FUNC: 3135 case VAR_STRING: 3136 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL; 3137 case VAR_LIST: 3138 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0; 3139 case VAR_DICT: 3140 return tv->vval.v_dict != NULL 3141 && tv->vval.v_dict->dv_hashtab.ht_used > 0; 3142 case VAR_BOOL: 3143 case VAR_SPECIAL: 3144 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE; 3145 case VAR_JOB: 3146 #ifdef FEAT_JOB_CHANNEL 3147 return tv->vval.v_job != NULL; 3148 #else 3149 break; 3150 #endif 3151 case VAR_CHANNEL: 3152 #ifdef FEAT_JOB_CHANNEL 3153 return tv->vval.v_channel != NULL; 3154 #else 3155 break; 3156 #endif 3157 case VAR_BLOB: 3158 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0; 3159 case VAR_UNKNOWN: 3160 case VAR_ANY: 3161 case VAR_VOID: 3162 break; 3163 } 3164 return FALSE; 3165 } 3166 3167 /* 3168 * If "tv" is a string give an error and return FAIL. 3169 */ 3170 int 3171 check_not_string(typval_T *tv) 3172 { 3173 if (tv->v_type == VAR_STRING) 3174 { 3175 emsg(_("E1030: Using a String as a Number")); 3176 clear_tv(tv); 3177 return FAIL; 3178 } 3179 return OK; 3180 } 3181 3182 3183 #endif // FEAT_EVAL 3184