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 return FAIL; 727 } 728 729 CLEAR_FIELD(ectx); 730 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx; 731 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500); 732 if (ga_grow(&ectx.ec_stack, 20) == FAIL) 733 return FAIL; 734 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10); 735 736 // Put arguments on the stack. 737 for (idx = 0; idx < argc; ++idx) 738 { 739 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len 740 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx]) 741 == FAIL) 742 goto failed_early; 743 copy_tv(&argv[idx], STACK_TV_BOT(0)); 744 ++ectx.ec_stack.ga_len; 745 } 746 747 // Turn varargs into a list. Empty list if no args. 748 if (ufunc->uf_va_name != NULL) 749 { 750 int vararg_count = argc - ufunc->uf_args.ga_len; 751 752 if (vararg_count < 0) 753 vararg_count = 0; 754 else 755 argc -= vararg_count; 756 if (exe_newlist(vararg_count, &ectx) == FAIL) 757 goto failed_early; 758 759 // Check the type of the list items. 760 tv = STACK_TV_BOT(-1); 761 if (ufunc->uf_va_type != NULL 762 && ufunc->uf_va_type->tt_member != &t_any 763 && tv->vval.v_list != NULL) 764 { 765 type_T *expected = ufunc->uf_va_type->tt_member; 766 listitem_T *li = tv->vval.v_list->lv_first; 767 768 for (idx = 0; idx < vararg_count; ++idx) 769 { 770 if (check_typval_type(expected, &li->li_tv) == FAIL) 771 goto failed_early; 772 li = li->li_next; 773 } 774 } 775 776 if (defcount > 0) 777 // Move varargs list to below missing default arguments. 778 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1); 779 --ectx.ec_stack.ga_len; 780 } 781 782 // Make space for omitted arguments, will store default value below. 783 // Any varargs list goes after them. 784 if (defcount > 0) 785 for (idx = 0; idx < defcount; ++idx) 786 { 787 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN; 788 ++ectx.ec_stack.ga_len; 789 } 790 if (ufunc->uf_va_name != NULL) 791 ++ectx.ec_stack.ga_len; 792 793 // Frame pointer points to just after arguments. 794 ectx.ec_frame_idx = ectx.ec_stack.ga_len; 795 initial_frame_idx = ectx.ec_frame_idx; 796 797 if (partial != NULL) 798 { 799 if (partial->pt_ectx_stack == NULL && current_ectx != NULL) 800 { 801 // TODO: is this always the right way? 802 ectx.ec_outer_stack = ¤t_ectx->ec_stack; 803 ectx.ec_outer_frame = current_ectx->ec_frame_idx; 804 } 805 else 806 { 807 ectx.ec_outer_stack = partial->pt_ectx_stack; 808 ectx.ec_outer_frame = partial->pt_ectx_frame; 809 } 810 } 811 812 // dummy frame entries 813 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx) 814 { 815 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN; 816 ++ectx.ec_stack.ga_len; 817 } 818 819 { 820 // Reserve space for local variables and closure references. 821 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 822 + ufunc->uf_dfunc_idx; 823 int count = dfunc->df_varcount + dfunc->df_closure_count; 824 825 for (idx = 0; idx < count; ++idx) 826 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN; 827 ectx.ec_stack.ga_len += count; 828 829 ectx.ec_instr = dfunc->df_instr; 830 } 831 832 // Commands behave like vim9script. 833 current_sctx.sc_version = SCRIPT_VERSION_VIM9; 834 835 // Decide where to start execution, handles optional arguments. 836 init_instr_idx(ufunc, argc, &ectx); 837 838 for (;;) 839 { 840 isn_T *iptr; 841 842 if (++breakcheck_count >= 100) 843 { 844 line_breakcheck(); 845 breakcheck_count = 0; 846 } 847 if (got_int) 848 { 849 // Turn CTRL-C into an exception. 850 got_int = FALSE; 851 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL) 852 goto failed; 853 did_throw = TRUE; 854 } 855 856 if (did_emsg && msg_list != NULL && *msg_list != NULL) 857 { 858 // Turn an error message into an exception. 859 did_emsg = FALSE; 860 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL) 861 goto failed; 862 did_throw = TRUE; 863 *msg_list = NULL; 864 } 865 866 if (did_throw && !ectx.ec_in_catch) 867 { 868 garray_T *trystack = &ectx.ec_trystack; 869 trycmd_T *trycmd = NULL; 870 871 // An exception jumps to the first catch, finally, or returns from 872 // the current function. 873 if (trystack->ga_len > 0) 874 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1; 875 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx) 876 { 877 // jump to ":catch" or ":finally" 878 ectx.ec_in_catch = TRUE; 879 ectx.ec_iidx = trycmd->tcd_catch_idx; 880 } 881 else 882 { 883 // not inside try or need to return from current functions. 884 if (ectx.ec_frame_idx == initial_frame_idx) 885 { 886 // At the toplevel we are done. Push a dummy return value. 887 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 888 goto failed; 889 tv = STACK_TV_BOT(0); 890 tv->v_type = VAR_NUMBER; 891 tv->vval.v_number = 0; 892 ++ectx.ec_stack.ga_len; 893 need_rethrow = TRUE; 894 if (handle_closure_in_use(&ectx, FALSE) == FAIL) 895 goto failed; 896 goto done; 897 } 898 899 if (func_return(&ectx) == FAIL) 900 goto failed; 901 } 902 continue; 903 } 904 905 iptr = &ectx.ec_instr[ectx.ec_iidx++]; 906 switch (iptr->isn_type) 907 { 908 // execute Ex command line 909 case ISN_EXEC: 910 SOURCING_LNUM = iptr->isn_lnum; 911 do_cmdline_cmd(iptr->isn_arg.string); 912 break; 913 914 // execute Ex command from pieces on the stack 915 case ISN_EXECCONCAT: 916 { 917 int count = iptr->isn_arg.number; 918 size_t len = 0; 919 int pass; 920 int i; 921 char_u *cmd = NULL; 922 char_u *str; 923 924 for (pass = 1; pass <= 2; ++pass) 925 { 926 for (i = 0; i < count; ++i) 927 { 928 tv = STACK_TV_BOT(i - count); 929 str = tv->vval.v_string; 930 if (str != NULL && *str != NUL) 931 { 932 if (pass == 2) 933 STRCPY(cmd + len, str); 934 len += STRLEN(str); 935 } 936 if (pass == 2) 937 clear_tv(tv); 938 } 939 if (pass == 1) 940 { 941 cmd = alloc(len + 1); 942 if (cmd == NULL) 943 goto failed; 944 len = 0; 945 } 946 } 947 948 SOURCING_LNUM = iptr->isn_lnum; 949 do_cmdline_cmd(cmd); 950 vim_free(cmd); 951 } 952 break; 953 954 // execute :echo {string} ... 955 case ISN_ECHO: 956 { 957 int count = iptr->isn_arg.echo.echo_count; 958 int atstart = TRUE; 959 int needclr = TRUE; 960 961 for (idx = 0; idx < count; ++idx) 962 { 963 tv = STACK_TV_BOT(idx - count); 964 echo_one(tv, iptr->isn_arg.echo.echo_with_white, 965 &atstart, &needclr); 966 clear_tv(tv); 967 } 968 if (needclr) 969 msg_clr_eos(); 970 ectx.ec_stack.ga_len -= count; 971 } 972 break; 973 974 // :execute {string} ... 975 // :echomsg {string} ... 976 // :echoerr {string} ... 977 case ISN_EXECUTE: 978 case ISN_ECHOMSG: 979 case ISN_ECHOERR: 980 { 981 int count = iptr->isn_arg.number; 982 garray_T ga; 983 char_u buf[NUMBUFLEN]; 984 char_u *p; 985 int len; 986 int failed = FALSE; 987 988 ga_init2(&ga, 1, 80); 989 for (idx = 0; idx < count; ++idx) 990 { 991 tv = STACK_TV_BOT(idx - count); 992 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB) 993 { 994 emsg(_(e_inval_string)); 995 break; 996 } 997 else 998 p = tv_get_string_buf(tv, buf); 999 1000 len = (int)STRLEN(p); 1001 if (ga_grow(&ga, len + 2) == FAIL) 1002 failed = TRUE; 1003 else 1004 { 1005 if (ga.ga_len > 0) 1006 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; 1007 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); 1008 ga.ga_len += len; 1009 } 1010 clear_tv(tv); 1011 } 1012 ectx.ec_stack.ga_len -= count; 1013 1014 if (!failed && ga.ga_data != NULL) 1015 { 1016 if (iptr->isn_type == ISN_EXECUTE) 1017 do_cmdline_cmd((char_u *)ga.ga_data); 1018 else 1019 { 1020 msg_sb_eol(); 1021 if (iptr->isn_type == ISN_ECHOMSG) 1022 { 1023 msg_attr(ga.ga_data, echo_attr); 1024 out_flush(); 1025 } 1026 else 1027 { 1028 SOURCING_LNUM = iptr->isn_lnum; 1029 emsg(ga.ga_data); 1030 } 1031 } 1032 } 1033 ga_clear(&ga); 1034 } 1035 break; 1036 1037 // load local variable or argument 1038 case ISN_LOAD: 1039 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1040 goto failed; 1041 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0)); 1042 ++ectx.ec_stack.ga_len; 1043 break; 1044 1045 // load variable or argument from outer scope 1046 case ISN_LOADOUTER: 1047 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1048 goto failed; 1049 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number), 1050 STACK_TV_BOT(0)); 1051 ++ectx.ec_stack.ga_len; 1052 break; 1053 1054 // load v: variable 1055 case ISN_LOADV: 1056 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1057 goto failed; 1058 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0)); 1059 ++ectx.ec_stack.ga_len; 1060 break; 1061 1062 // load s: variable in Vim9 script 1063 case ISN_LOADSCRIPT: 1064 { 1065 scriptitem_T *si = 1066 SCRIPT_ITEM(iptr->isn_arg.script.script_sid); 1067 svar_T *sv; 1068 1069 sv = ((svar_T *)si->sn_var_vals.ga_data) 1070 + iptr->isn_arg.script.script_idx; 1071 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1072 goto failed; 1073 copy_tv(sv->sv_tv, STACK_TV_BOT(0)); 1074 ++ectx.ec_stack.ga_len; 1075 } 1076 break; 1077 1078 // load s: variable in old script 1079 case ISN_LOADS: 1080 { 1081 hashtab_T *ht = &SCRIPT_VARS( 1082 iptr->isn_arg.loadstore.ls_sid); 1083 char_u *name = iptr->isn_arg.loadstore.ls_name; 1084 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE); 1085 1086 if (di == NULL) 1087 { 1088 semsg(_(e_undefvar), name); 1089 goto on_error; 1090 } 1091 else 1092 { 1093 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1094 goto failed; 1095 copy_tv(&di->di_tv, STACK_TV_BOT(0)); 1096 ++ectx.ec_stack.ga_len; 1097 } 1098 } 1099 break; 1100 1101 // load g:/b:/w:/t: variable 1102 case ISN_LOADG: 1103 case ISN_LOADB: 1104 case ISN_LOADW: 1105 case ISN_LOADT: 1106 { 1107 dictitem_T *di = NULL; 1108 hashtab_T *ht = NULL; 1109 char namespace; 1110 1111 switch (iptr->isn_type) 1112 { 1113 case ISN_LOADG: 1114 ht = get_globvar_ht(); 1115 namespace = 'g'; 1116 break; 1117 case ISN_LOADB: 1118 ht = &curbuf->b_vars->dv_hashtab; 1119 namespace = 'b'; 1120 break; 1121 case ISN_LOADW: 1122 ht = &curwin->w_vars->dv_hashtab; 1123 namespace = 'w'; 1124 break; 1125 case ISN_LOADT: 1126 ht = &curtab->tp_vars->dv_hashtab; 1127 namespace = 't'; 1128 break; 1129 default: // Cannot reach here 1130 goto failed; 1131 } 1132 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE); 1133 1134 if (di == NULL) 1135 { 1136 semsg(_("E121: Undefined variable: %c:%s"), 1137 namespace, iptr->isn_arg.string); 1138 goto on_error; 1139 } 1140 else 1141 { 1142 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1143 goto failed; 1144 copy_tv(&di->di_tv, STACK_TV_BOT(0)); 1145 ++ectx.ec_stack.ga_len; 1146 } 1147 } 1148 break; 1149 1150 // load g:/b:/w:/t: namespace 1151 case ISN_LOADGDICT: 1152 case ISN_LOADBDICT: 1153 case ISN_LOADWDICT: 1154 case ISN_LOADTDICT: 1155 { 1156 dict_T *d = NULL; 1157 1158 switch (iptr->isn_type) 1159 { 1160 case ISN_LOADGDICT: d = get_globvar_dict(); break; 1161 case ISN_LOADBDICT: d = curbuf->b_vars; break; 1162 case ISN_LOADWDICT: d = curwin->w_vars; break; 1163 case ISN_LOADTDICT: d = curtab->tp_vars; break; 1164 default: // Cannot reach here 1165 goto failed; 1166 } 1167 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1168 goto failed; 1169 tv = STACK_TV_BOT(0); 1170 tv->v_type = VAR_DICT; 1171 tv->v_lock = 0; 1172 tv->vval.v_dict = d; 1173 ++ectx.ec_stack.ga_len; 1174 } 1175 break; 1176 1177 // load &option 1178 case ISN_LOADOPT: 1179 { 1180 typval_T optval; 1181 char_u *name = iptr->isn_arg.string; 1182 1183 // This is not expected to fail, name is checked during 1184 // compilation: don't set SOURCING_LNUM. 1185 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1186 goto failed; 1187 if (eval_option(&name, &optval, TRUE) == FAIL) 1188 goto failed; 1189 *STACK_TV_BOT(0) = optval; 1190 ++ectx.ec_stack.ga_len; 1191 } 1192 break; 1193 1194 // load $ENV 1195 case ISN_LOADENV: 1196 { 1197 typval_T optval; 1198 char_u *name = iptr->isn_arg.string; 1199 1200 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1201 goto failed; 1202 // name is always valid, checked when compiling 1203 (void)eval_env_var(&name, &optval, TRUE); 1204 *STACK_TV_BOT(0) = optval; 1205 ++ectx.ec_stack.ga_len; 1206 } 1207 break; 1208 1209 // load @register 1210 case ISN_LOADREG: 1211 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1212 goto failed; 1213 tv = STACK_TV_BOT(0); 1214 tv->v_type = VAR_STRING; 1215 tv->v_lock = 0; 1216 tv->vval.v_string = get_reg_contents( 1217 iptr->isn_arg.number, GREG_EXPR_SRC); 1218 ++ectx.ec_stack.ga_len; 1219 break; 1220 1221 // store local variable 1222 case ISN_STORE: 1223 --ectx.ec_stack.ga_len; 1224 tv = STACK_TV_VAR(iptr->isn_arg.number); 1225 clear_tv(tv); 1226 *tv = *STACK_TV_BOT(0); 1227 break; 1228 1229 // store variable or argument in outer scope 1230 case ISN_STOREOUTER: 1231 --ectx.ec_stack.ga_len; 1232 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number); 1233 clear_tv(tv); 1234 *tv = *STACK_TV_BOT(0); 1235 break; 1236 1237 // store s: variable in old script 1238 case ISN_STORES: 1239 { 1240 hashtab_T *ht = &SCRIPT_VARS( 1241 iptr->isn_arg.loadstore.ls_sid); 1242 char_u *name = iptr->isn_arg.loadstore.ls_name; 1243 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE); 1244 1245 --ectx.ec_stack.ga_len; 1246 if (di == NULL) 1247 store_var(name, STACK_TV_BOT(0)); 1248 else 1249 { 1250 clear_tv(&di->di_tv); 1251 di->di_tv = *STACK_TV_BOT(0); 1252 } 1253 } 1254 break; 1255 1256 // store script-local variable in Vim9 script 1257 case ISN_STORESCRIPT: 1258 { 1259 scriptitem_T *si = SCRIPT_ITEM( 1260 iptr->isn_arg.script.script_sid); 1261 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 1262 + iptr->isn_arg.script.script_idx; 1263 1264 --ectx.ec_stack.ga_len; 1265 clear_tv(sv->sv_tv); 1266 *sv->sv_tv = *STACK_TV_BOT(0); 1267 } 1268 break; 1269 1270 // store option 1271 case ISN_STOREOPT: 1272 { 1273 long n = 0; 1274 char_u *s = NULL; 1275 char *msg; 1276 1277 --ectx.ec_stack.ga_len; 1278 tv = STACK_TV_BOT(0); 1279 if (tv->v_type == VAR_STRING) 1280 { 1281 s = tv->vval.v_string; 1282 if (s == NULL) 1283 s = (char_u *)""; 1284 } 1285 else 1286 // must be VAR_NUMBER, CHECKTYPE makes sure 1287 n = tv->vval.v_number; 1288 msg = set_option_value(iptr->isn_arg.storeopt.so_name, 1289 n, s, iptr->isn_arg.storeopt.so_flags); 1290 clear_tv(tv); 1291 if (msg != NULL) 1292 { 1293 emsg(_(msg)); 1294 goto on_error; 1295 } 1296 } 1297 break; 1298 1299 // store $ENV 1300 case ISN_STOREENV: 1301 --ectx.ec_stack.ga_len; 1302 tv = STACK_TV_BOT(0); 1303 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv)); 1304 clear_tv(tv); 1305 break; 1306 1307 // store @r 1308 case ISN_STOREREG: 1309 { 1310 int reg = iptr->isn_arg.number; 1311 1312 --ectx.ec_stack.ga_len; 1313 tv = STACK_TV_BOT(0); 1314 write_reg_contents(reg == '@' ? '"' : reg, 1315 tv_get_string(tv), -1, FALSE); 1316 clear_tv(tv); 1317 } 1318 break; 1319 1320 // store v: variable 1321 case ISN_STOREV: 1322 --ectx.ec_stack.ga_len; 1323 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0)) 1324 == FAIL) 1325 // should not happen, type is checked when compiling 1326 goto on_error; 1327 break; 1328 1329 // store g:/b:/w:/t: variable 1330 case ISN_STOREG: 1331 case ISN_STOREB: 1332 case ISN_STOREW: 1333 case ISN_STORET: 1334 { 1335 dictitem_T *di; 1336 hashtab_T *ht; 1337 switch (iptr->isn_type) 1338 { 1339 case ISN_STOREG: 1340 ht = get_globvar_ht(); 1341 break; 1342 case ISN_STOREB: 1343 ht = &curbuf->b_vars->dv_hashtab; 1344 break; 1345 case ISN_STOREW: 1346 ht = &curwin->w_vars->dv_hashtab; 1347 break; 1348 case ISN_STORET: 1349 ht = &curtab->tp_vars->dv_hashtab; 1350 break; 1351 default: // Cannot reach here 1352 goto failed; 1353 } 1354 1355 --ectx.ec_stack.ga_len; 1356 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE); 1357 if (di == NULL) 1358 store_var(iptr->isn_arg.string, STACK_TV_BOT(0)); 1359 else 1360 { 1361 clear_tv(&di->di_tv); 1362 di->di_tv = *STACK_TV_BOT(0); 1363 } 1364 } 1365 break; 1366 1367 // store number in local variable 1368 case ISN_STORENR: 1369 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx); 1370 clear_tv(tv); 1371 tv->v_type = VAR_NUMBER; 1372 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val; 1373 break; 1374 1375 // store value in list variable 1376 case ISN_STORELIST: 1377 { 1378 typval_T *tv_idx = STACK_TV_BOT(-2); 1379 varnumber_T lidx = tv_idx->vval.v_number; 1380 typval_T *tv_list = STACK_TV_BOT(-1); 1381 list_T *list = tv_list->vval.v_list; 1382 1383 if (lidx < 0 && list->lv_len + lidx >= 0) 1384 // negative index is relative to the end 1385 lidx = list->lv_len + lidx; 1386 if (lidx < 0 || lidx > list->lv_len) 1387 { 1388 semsg(_(e_listidx), lidx); 1389 goto on_error; 1390 } 1391 tv = STACK_TV_BOT(-3); 1392 if (lidx < list->lv_len) 1393 { 1394 listitem_T *li = list_find(list, lidx); 1395 1396 // overwrite existing list item 1397 clear_tv(&li->li_tv); 1398 li->li_tv = *tv; 1399 } 1400 else 1401 { 1402 // append to list, only fails when out of memory 1403 if (list_append_tv(list, tv) == FAIL) 1404 goto failed; 1405 clear_tv(tv); 1406 } 1407 clear_tv(tv_idx); 1408 clear_tv(tv_list); 1409 ectx.ec_stack.ga_len -= 3; 1410 } 1411 break; 1412 1413 // store value in dict variable 1414 case ISN_STOREDICT: 1415 { 1416 typval_T *tv_key = STACK_TV_BOT(-2); 1417 char_u *key = tv_key->vval.v_string; 1418 typval_T *tv_dict = STACK_TV_BOT(-1); 1419 dict_T *dict = tv_dict->vval.v_dict; 1420 dictitem_T *di; 1421 1422 if (key == NULL || *key == NUL) 1423 { 1424 emsg(_(e_emptykey)); 1425 goto on_error; 1426 } 1427 tv = STACK_TV_BOT(-3); 1428 di = dict_find(dict, key, -1); 1429 if (di != NULL) 1430 { 1431 // overwrite existing value 1432 clear_tv(&di->di_tv); 1433 di->di_tv = *tv; 1434 } 1435 else 1436 { 1437 // add to dict, only fails when out of memory 1438 if (dict_add_tv(dict, (char *)key, tv) == FAIL) 1439 goto failed; 1440 clear_tv(tv); 1441 } 1442 clear_tv(tv_key); 1443 clear_tv(tv_dict); 1444 ectx.ec_stack.ga_len -= 3; 1445 } 1446 break; 1447 1448 // push constant 1449 case ISN_PUSHNR: 1450 case ISN_PUSHBOOL: 1451 case ISN_PUSHSPEC: 1452 case ISN_PUSHF: 1453 case ISN_PUSHS: 1454 case ISN_PUSHBLOB: 1455 case ISN_PUSHFUNC: 1456 case ISN_PUSHCHANNEL: 1457 case ISN_PUSHJOB: 1458 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1459 goto failed; 1460 tv = STACK_TV_BOT(0); 1461 tv->v_lock = 0; 1462 ++ectx.ec_stack.ga_len; 1463 switch (iptr->isn_type) 1464 { 1465 case ISN_PUSHNR: 1466 tv->v_type = VAR_NUMBER; 1467 tv->vval.v_number = iptr->isn_arg.number; 1468 break; 1469 case ISN_PUSHBOOL: 1470 tv->v_type = VAR_BOOL; 1471 tv->vval.v_number = iptr->isn_arg.number; 1472 break; 1473 case ISN_PUSHSPEC: 1474 tv->v_type = VAR_SPECIAL; 1475 tv->vval.v_number = iptr->isn_arg.number; 1476 break; 1477 #ifdef FEAT_FLOAT 1478 case ISN_PUSHF: 1479 tv->v_type = VAR_FLOAT; 1480 tv->vval.v_float = iptr->isn_arg.fnumber; 1481 break; 1482 #endif 1483 case ISN_PUSHBLOB: 1484 blob_copy(iptr->isn_arg.blob, tv); 1485 break; 1486 case ISN_PUSHFUNC: 1487 tv->v_type = VAR_FUNC; 1488 if (iptr->isn_arg.string == NULL) 1489 tv->vval.v_string = NULL; 1490 else 1491 tv->vval.v_string = 1492 vim_strsave(iptr->isn_arg.string); 1493 break; 1494 case ISN_PUSHCHANNEL: 1495 #ifdef FEAT_JOB_CHANNEL 1496 tv->v_type = VAR_CHANNEL; 1497 tv->vval.v_channel = iptr->isn_arg.channel; 1498 if (tv->vval.v_channel != NULL) 1499 ++tv->vval.v_channel->ch_refcount; 1500 #endif 1501 break; 1502 case ISN_PUSHJOB: 1503 #ifdef FEAT_JOB_CHANNEL 1504 tv->v_type = VAR_JOB; 1505 tv->vval.v_job = iptr->isn_arg.job; 1506 if (tv->vval.v_job != NULL) 1507 ++tv->vval.v_job->jv_refcount; 1508 #endif 1509 break; 1510 default: 1511 tv->v_type = VAR_STRING; 1512 tv->vval.v_string = vim_strsave( 1513 iptr->isn_arg.string == NULL 1514 ? (char_u *)"" : iptr->isn_arg.string); 1515 } 1516 break; 1517 1518 case ISN_UNLET: 1519 if (do_unlet(iptr->isn_arg.unlet.ul_name, 1520 iptr->isn_arg.unlet.ul_forceit) == FAIL) 1521 goto on_error; 1522 break; 1523 case ISN_UNLETENV: 1524 vim_unsetenv(iptr->isn_arg.unlet.ul_name); 1525 break; 1526 1527 // create a list from items on the stack; uses a single allocation 1528 // for the list header and the items 1529 case ISN_NEWLIST: 1530 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL) 1531 goto failed; 1532 break; 1533 1534 // create a dict from items on the stack 1535 case ISN_NEWDICT: 1536 { 1537 int count = iptr->isn_arg.number; 1538 dict_T *dict = dict_alloc(); 1539 dictitem_T *item; 1540 1541 if (dict == NULL) 1542 goto failed; 1543 for (idx = 0; idx < count; ++idx) 1544 { 1545 // have already checked key type is VAR_STRING 1546 tv = STACK_TV_BOT(2 * (idx - count)); 1547 // check key is unique 1548 item = dict_find(dict, tv->vval.v_string, -1); 1549 if (item != NULL) 1550 { 1551 semsg(_(e_duplicate_key), tv->vval.v_string); 1552 dict_unref(dict); 1553 goto on_error; 1554 } 1555 item = dictitem_alloc(tv->vval.v_string); 1556 clear_tv(tv); 1557 if (item == NULL) 1558 { 1559 dict_unref(dict); 1560 goto failed; 1561 } 1562 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1); 1563 item->di_tv.v_lock = 0; 1564 if (dict_add(dict, item) == FAIL) 1565 { 1566 // can this ever happen? 1567 dict_unref(dict); 1568 goto failed; 1569 } 1570 } 1571 1572 if (count > 0) 1573 ectx.ec_stack.ga_len -= 2 * count - 1; 1574 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1575 goto failed; 1576 else 1577 ++ectx.ec_stack.ga_len; 1578 tv = STACK_TV_BOT(-1); 1579 tv->v_type = VAR_DICT; 1580 tv->v_lock = 0; 1581 tv->vval.v_dict = dict; 1582 ++dict->dv_refcount; 1583 } 1584 break; 1585 1586 // call a :def function 1587 case ISN_DCALL: 1588 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, 1589 iptr->isn_arg.dfunc.cdf_argcount, 1590 &ectx) == FAIL) 1591 goto on_error; 1592 break; 1593 1594 // call a builtin function 1595 case ISN_BCALL: 1596 SOURCING_LNUM = iptr->isn_lnum; 1597 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx, 1598 iptr->isn_arg.bfunc.cbf_argcount, 1599 &ectx) == FAIL) 1600 goto on_error; 1601 break; 1602 1603 // call a funcref or partial 1604 case ISN_PCALL: 1605 { 1606 cpfunc_T *pfunc = &iptr->isn_arg.pfunc; 1607 int r; 1608 typval_T partial_tv; 1609 1610 SOURCING_LNUM = iptr->isn_lnum; 1611 if (pfunc->cpf_top) 1612 { 1613 // funcref is above the arguments 1614 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1); 1615 } 1616 else 1617 { 1618 // Get the funcref from the stack. 1619 --ectx.ec_stack.ga_len; 1620 partial_tv = *STACK_TV_BOT(0); 1621 tv = &partial_tv; 1622 } 1623 r = call_partial(tv, pfunc->cpf_argcount, &ectx); 1624 if (tv == &partial_tv) 1625 clear_tv(&partial_tv); 1626 if (r == FAIL) 1627 goto on_error; 1628 } 1629 break; 1630 1631 case ISN_PCALL_END: 1632 // PCALL finished, arguments have been consumed and replaced by 1633 // the return value. Now clear the funcref from the stack, 1634 // and move the return value in its place. 1635 --ectx.ec_stack.ga_len; 1636 clear_tv(STACK_TV_BOT(-1)); 1637 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0); 1638 break; 1639 1640 // call a user defined function or funcref/partial 1641 case ISN_UCALL: 1642 { 1643 cufunc_T *cufunc = &iptr->isn_arg.ufunc; 1644 1645 SOURCING_LNUM = iptr->isn_lnum; 1646 if (call_eval_func(cufunc->cuf_name, 1647 cufunc->cuf_argcount, &ectx, iptr) == FAIL) 1648 goto on_error; 1649 } 1650 break; 1651 1652 // return from a :def function call 1653 case ISN_RETURN: 1654 { 1655 garray_T *trystack = &ectx.ec_trystack; 1656 trycmd_T *trycmd = NULL; 1657 1658 if (trystack->ga_len > 0) 1659 trycmd = ((trycmd_T *)trystack->ga_data) 1660 + trystack->ga_len - 1; 1661 if (trycmd != NULL 1662 && trycmd->tcd_frame_idx == ectx.ec_frame_idx 1663 && trycmd->tcd_finally_idx != 0) 1664 { 1665 // jump to ":finally" 1666 ectx.ec_iidx = trycmd->tcd_finally_idx; 1667 trycmd->tcd_return = TRUE; 1668 } 1669 else 1670 goto func_return; 1671 } 1672 break; 1673 1674 // push a function reference to a compiled function 1675 case ISN_FUNCREF: 1676 { 1677 partial_T *pt = NULL; 1678 dfunc_T *pt_dfunc; 1679 1680 pt = ALLOC_CLEAR_ONE(partial_T); 1681 if (pt == NULL) 1682 goto failed; 1683 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1684 { 1685 vim_free(pt); 1686 goto failed; 1687 } 1688 pt_dfunc = ((dfunc_T *)def_functions.ga_data) 1689 + iptr->isn_arg.funcref.fr_func; 1690 pt->pt_func = pt_dfunc->df_ufunc; 1691 pt->pt_refcount = 1; 1692 ++pt_dfunc->df_ufunc->uf_refcount; 1693 1694 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE) 1695 { 1696 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 1697 + ectx.ec_dfunc_idx; 1698 1699 // The closure needs to find arguments and local 1700 // variables in the current stack. 1701 pt->pt_ectx_stack = &ectx.ec_stack; 1702 pt->pt_ectx_frame = ectx.ec_frame_idx; 1703 1704 // If this function returns and the closure is still 1705 // used, we need to make a copy of the context 1706 // (arguments and local variables). Store a reference 1707 // to the partial so we can handle that. 1708 ++pt->pt_refcount; 1709 tv = STACK_TV_VAR(dfunc->df_varcount 1710 + iptr->isn_arg.funcref.fr_var_idx); 1711 if (tv->v_type == VAR_PARTIAL) 1712 { 1713 // TODO: use a garray_T on ectx. 1714 emsg("Multiple closures not supported yet"); 1715 goto failed; 1716 } 1717 tv->v_type = VAR_PARTIAL; 1718 tv->vval.v_partial = pt; 1719 } 1720 1721 tv = STACK_TV_BOT(0); 1722 ++ectx.ec_stack.ga_len; 1723 tv->vval.v_partial = pt; 1724 tv->v_type = VAR_PARTIAL; 1725 tv->v_lock = 0; 1726 } 1727 break; 1728 1729 // jump if a condition is met 1730 case ISN_JUMP: 1731 { 1732 jumpwhen_T when = iptr->isn_arg.jump.jump_when; 1733 int jump = TRUE; 1734 1735 if (when != JUMP_ALWAYS) 1736 { 1737 tv = STACK_TV_BOT(-1); 1738 jump = tv2bool(tv); 1739 if (when == JUMP_IF_FALSE 1740 || when == JUMP_AND_KEEP_IF_FALSE) 1741 jump = !jump; 1742 if (when == JUMP_IF_FALSE || !jump) 1743 { 1744 // drop the value from the stack 1745 clear_tv(tv); 1746 --ectx.ec_stack.ga_len; 1747 } 1748 } 1749 if (jump) 1750 ectx.ec_iidx = iptr->isn_arg.jump.jump_where; 1751 } 1752 break; 1753 1754 // top of a for loop 1755 case ISN_FOR: 1756 { 1757 list_T *list = STACK_TV_BOT(-1)->vval.v_list; 1758 typval_T *idxtv = 1759 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx); 1760 1761 // push the next item from the list 1762 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1763 goto failed; 1764 if (++idxtv->vval.v_number >= list->lv_len) 1765 // past the end of the list, jump to "endfor" 1766 ectx.ec_iidx = iptr->isn_arg.forloop.for_end; 1767 else if (list->lv_first == &range_list_item) 1768 { 1769 // non-materialized range() list 1770 tv = STACK_TV_BOT(0); 1771 tv->v_type = VAR_NUMBER; 1772 tv->v_lock = 0; 1773 tv->vval.v_number = list_find_nr( 1774 list, idxtv->vval.v_number, NULL); 1775 ++ectx.ec_stack.ga_len; 1776 } 1777 else 1778 { 1779 listitem_T *li = list_find(list, idxtv->vval.v_number); 1780 1781 copy_tv(&li->li_tv, STACK_TV_BOT(0)); 1782 ++ectx.ec_stack.ga_len; 1783 } 1784 } 1785 break; 1786 1787 // start of ":try" block 1788 case ISN_TRY: 1789 { 1790 trycmd_T *trycmd = NULL; 1791 1792 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL) 1793 goto failed; 1794 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data) 1795 + ectx.ec_trystack.ga_len; 1796 ++ectx.ec_trystack.ga_len; 1797 ++trylevel; 1798 trycmd->tcd_frame_idx = ectx.ec_frame_idx; 1799 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch; 1800 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally; 1801 trycmd->tcd_caught = FALSE; 1802 } 1803 break; 1804 1805 case ISN_PUSHEXC: 1806 if (current_exception == NULL) 1807 { 1808 iemsg("Evaluating catch while current_exception is NULL"); 1809 goto failed; 1810 } 1811 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 1812 goto failed; 1813 tv = STACK_TV_BOT(0); 1814 ++ectx.ec_stack.ga_len; 1815 tv->v_type = VAR_STRING; 1816 tv->v_lock = 0; 1817 tv->vval.v_string = vim_strsave( 1818 (char_u *)current_exception->value); 1819 break; 1820 1821 case ISN_CATCH: 1822 { 1823 garray_T *trystack = &ectx.ec_trystack; 1824 1825 if (trystack->ga_len > 0) 1826 { 1827 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) 1828 + trystack->ga_len - 1; 1829 trycmd->tcd_caught = TRUE; 1830 } 1831 did_emsg = got_int = did_throw = FALSE; 1832 catch_exception(current_exception); 1833 } 1834 break; 1835 1836 // end of ":try" block 1837 case ISN_ENDTRY: 1838 { 1839 garray_T *trystack = &ectx.ec_trystack; 1840 1841 if (trystack->ga_len > 0) 1842 { 1843 trycmd_T *trycmd = NULL; 1844 1845 --trystack->ga_len; 1846 --trylevel; 1847 ectx.ec_in_catch = FALSE; 1848 trycmd = ((trycmd_T *)trystack->ga_data) 1849 + trystack->ga_len; 1850 if (trycmd->tcd_caught && current_exception != NULL) 1851 { 1852 // discard the exception 1853 if (caught_stack == current_exception) 1854 caught_stack = caught_stack->caught; 1855 discard_current_exception(); 1856 } 1857 1858 if (trycmd->tcd_return) 1859 goto func_return; 1860 } 1861 } 1862 break; 1863 1864 case ISN_THROW: 1865 --ectx.ec_stack.ga_len; 1866 tv = STACK_TV_BOT(0); 1867 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL) 1868 { 1869 vim_free(tv->vval.v_string); 1870 goto failed; 1871 } 1872 did_throw = TRUE; 1873 break; 1874 1875 // compare with special values 1876 case ISN_COMPAREBOOL: 1877 case ISN_COMPARESPECIAL: 1878 { 1879 typval_T *tv1 = STACK_TV_BOT(-2); 1880 typval_T *tv2 = STACK_TV_BOT(-1); 1881 varnumber_T arg1 = tv1->vval.v_number; 1882 varnumber_T arg2 = tv2->vval.v_number; 1883 int res; 1884 1885 switch (iptr->isn_arg.op.op_type) 1886 { 1887 case EXPR_EQUAL: res = arg1 == arg2; break; 1888 case EXPR_NEQUAL: res = arg1 != arg2; break; 1889 default: res = 0; break; 1890 } 1891 1892 --ectx.ec_stack.ga_len; 1893 tv1->v_type = VAR_BOOL; 1894 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; 1895 } 1896 break; 1897 1898 // Operation with two number arguments 1899 case ISN_OPNR: 1900 case ISN_COMPARENR: 1901 { 1902 typval_T *tv1 = STACK_TV_BOT(-2); 1903 typval_T *tv2 = STACK_TV_BOT(-1); 1904 varnumber_T arg1 = tv1->vval.v_number; 1905 varnumber_T arg2 = tv2->vval.v_number; 1906 varnumber_T res; 1907 1908 switch (iptr->isn_arg.op.op_type) 1909 { 1910 case EXPR_MULT: res = arg1 * arg2; break; 1911 case EXPR_DIV: res = arg1 / arg2; break; 1912 case EXPR_REM: res = arg1 % arg2; break; 1913 case EXPR_SUB: res = arg1 - arg2; break; 1914 case EXPR_ADD: res = arg1 + arg2; break; 1915 1916 case EXPR_EQUAL: res = arg1 == arg2; break; 1917 case EXPR_NEQUAL: res = arg1 != arg2; break; 1918 case EXPR_GREATER: res = arg1 > arg2; break; 1919 case EXPR_GEQUAL: res = arg1 >= arg2; break; 1920 case EXPR_SMALLER: res = arg1 < arg2; break; 1921 case EXPR_SEQUAL: res = arg1 <= arg2; break; 1922 default: res = 0; break; 1923 } 1924 1925 --ectx.ec_stack.ga_len; 1926 if (iptr->isn_type == ISN_COMPARENR) 1927 { 1928 tv1->v_type = VAR_BOOL; 1929 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; 1930 } 1931 else 1932 tv1->vval.v_number = res; 1933 } 1934 break; 1935 1936 // Computation with two float arguments 1937 case ISN_OPFLOAT: 1938 case ISN_COMPAREFLOAT: 1939 #ifdef FEAT_FLOAT 1940 { 1941 typval_T *tv1 = STACK_TV_BOT(-2); 1942 typval_T *tv2 = STACK_TV_BOT(-1); 1943 float_T arg1 = tv1->vval.v_float; 1944 float_T arg2 = tv2->vval.v_float; 1945 float_T res = 0; 1946 int cmp = FALSE; 1947 1948 switch (iptr->isn_arg.op.op_type) 1949 { 1950 case EXPR_MULT: res = arg1 * arg2; break; 1951 case EXPR_DIV: res = arg1 / arg2; break; 1952 case EXPR_SUB: res = arg1 - arg2; break; 1953 case EXPR_ADD: res = arg1 + arg2; break; 1954 1955 case EXPR_EQUAL: cmp = arg1 == arg2; break; 1956 case EXPR_NEQUAL: cmp = arg1 != arg2; break; 1957 case EXPR_GREATER: cmp = arg1 > arg2; break; 1958 case EXPR_GEQUAL: cmp = arg1 >= arg2; break; 1959 case EXPR_SMALLER: cmp = arg1 < arg2; break; 1960 case EXPR_SEQUAL: cmp = arg1 <= arg2; break; 1961 default: cmp = 0; break; 1962 } 1963 --ectx.ec_stack.ga_len; 1964 if (iptr->isn_type == ISN_COMPAREFLOAT) 1965 { 1966 tv1->v_type = VAR_BOOL; 1967 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 1968 } 1969 else 1970 tv1->vval.v_float = res; 1971 } 1972 #endif 1973 break; 1974 1975 case ISN_COMPARELIST: 1976 { 1977 typval_T *tv1 = STACK_TV_BOT(-2); 1978 typval_T *tv2 = STACK_TV_BOT(-1); 1979 list_T *arg1 = tv1->vval.v_list; 1980 list_T *arg2 = tv2->vval.v_list; 1981 int cmp = FALSE; 1982 int ic = iptr->isn_arg.op.op_ic; 1983 1984 switch (iptr->isn_arg.op.op_type) 1985 { 1986 case EXPR_EQUAL: cmp = 1987 list_equal(arg1, arg2, ic, FALSE); break; 1988 case EXPR_NEQUAL: cmp = 1989 !list_equal(arg1, arg2, ic, FALSE); break; 1990 case EXPR_IS: cmp = arg1 == arg2; break; 1991 case EXPR_ISNOT: cmp = arg1 != arg2; break; 1992 default: cmp = 0; break; 1993 } 1994 --ectx.ec_stack.ga_len; 1995 clear_tv(tv1); 1996 clear_tv(tv2); 1997 tv1->v_type = VAR_BOOL; 1998 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 1999 } 2000 break; 2001 2002 case ISN_COMPAREBLOB: 2003 { 2004 typval_T *tv1 = STACK_TV_BOT(-2); 2005 typval_T *tv2 = STACK_TV_BOT(-1); 2006 blob_T *arg1 = tv1->vval.v_blob; 2007 blob_T *arg2 = tv2->vval.v_blob; 2008 int cmp = FALSE; 2009 2010 switch (iptr->isn_arg.op.op_type) 2011 { 2012 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break; 2013 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break; 2014 case EXPR_IS: cmp = arg1 == arg2; break; 2015 case EXPR_ISNOT: cmp = arg1 != arg2; break; 2016 default: cmp = 0; break; 2017 } 2018 --ectx.ec_stack.ga_len; 2019 clear_tv(tv1); 2020 clear_tv(tv2); 2021 tv1->v_type = VAR_BOOL; 2022 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; 2023 } 2024 break; 2025 2026 // TODO: handle separately 2027 case ISN_COMPARESTRING: 2028 case ISN_COMPAREDICT: 2029 case ISN_COMPAREFUNC: 2030 case ISN_COMPAREANY: 2031 { 2032 typval_T *tv1 = STACK_TV_BOT(-2); 2033 typval_T *tv2 = STACK_TV_BOT(-1); 2034 exptype_T exptype = iptr->isn_arg.op.op_type; 2035 int ic = iptr->isn_arg.op.op_ic; 2036 2037 typval_compare(tv1, tv2, exptype, ic); 2038 clear_tv(tv2); 2039 --ectx.ec_stack.ga_len; 2040 } 2041 break; 2042 2043 case ISN_ADDLIST: 2044 case ISN_ADDBLOB: 2045 { 2046 typval_T *tv1 = STACK_TV_BOT(-2); 2047 typval_T *tv2 = STACK_TV_BOT(-1); 2048 2049 if (iptr->isn_type == ISN_ADDLIST) 2050 eval_addlist(tv1, tv2); 2051 else 2052 eval_addblob(tv1, tv2); 2053 clear_tv(tv2); 2054 --ectx.ec_stack.ga_len; 2055 } 2056 break; 2057 2058 // Computation with two arguments of unknown type 2059 case ISN_OPANY: 2060 { 2061 typval_T *tv1 = STACK_TV_BOT(-2); 2062 typval_T *tv2 = STACK_TV_BOT(-1); 2063 varnumber_T n1, n2; 2064 #ifdef FEAT_FLOAT 2065 float_T f1 = 0, f2 = 0; 2066 #endif 2067 int error = FALSE; 2068 2069 if (iptr->isn_arg.op.op_type == EXPR_ADD) 2070 { 2071 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST) 2072 { 2073 eval_addlist(tv1, tv2); 2074 clear_tv(tv2); 2075 --ectx.ec_stack.ga_len; 2076 break; 2077 } 2078 else if (tv1->v_type == VAR_BLOB 2079 && tv2->v_type == VAR_BLOB) 2080 { 2081 eval_addblob(tv1, tv2); 2082 clear_tv(tv2); 2083 --ectx.ec_stack.ga_len; 2084 break; 2085 } 2086 } 2087 #ifdef FEAT_FLOAT 2088 if (tv1->v_type == VAR_FLOAT) 2089 { 2090 f1 = tv1->vval.v_float; 2091 n1 = 0; 2092 } 2093 else 2094 #endif 2095 { 2096 n1 = tv_get_number_chk(tv1, &error); 2097 if (error) 2098 goto on_error; 2099 #ifdef FEAT_FLOAT 2100 if (tv2->v_type == VAR_FLOAT) 2101 f1 = n1; 2102 #endif 2103 } 2104 #ifdef FEAT_FLOAT 2105 if (tv2->v_type == VAR_FLOAT) 2106 { 2107 f2 = tv2->vval.v_float; 2108 n2 = 0; 2109 } 2110 else 2111 #endif 2112 { 2113 n2 = tv_get_number_chk(tv2, &error); 2114 if (error) 2115 goto on_error; 2116 #ifdef FEAT_FLOAT 2117 if (tv1->v_type == VAR_FLOAT) 2118 f2 = n2; 2119 #endif 2120 } 2121 #ifdef FEAT_FLOAT 2122 // if there is a float on either side the result is a float 2123 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT) 2124 { 2125 switch (iptr->isn_arg.op.op_type) 2126 { 2127 case EXPR_MULT: f1 = f1 * f2; break; 2128 case EXPR_DIV: f1 = f1 / f2; break; 2129 case EXPR_SUB: f1 = f1 - f2; break; 2130 case EXPR_ADD: f1 = f1 + f2; break; 2131 default: emsg(_(e_modulus)); 2132 goto on_error; 2133 } 2134 clear_tv(tv1); 2135 clear_tv(tv2); 2136 tv1->v_type = VAR_FLOAT; 2137 tv1->vval.v_float = f1; 2138 --ectx.ec_stack.ga_len; 2139 } 2140 else 2141 #endif 2142 { 2143 switch (iptr->isn_arg.op.op_type) 2144 { 2145 case EXPR_MULT: n1 = n1 * n2; break; 2146 case EXPR_DIV: n1 = num_divide(n1, n2); break; 2147 case EXPR_SUB: n1 = n1 - n2; break; 2148 case EXPR_ADD: n1 = n1 + n2; break; 2149 default: n1 = num_modulus(n1, n2); break; 2150 } 2151 clear_tv(tv1); 2152 clear_tv(tv2); 2153 tv1->v_type = VAR_NUMBER; 2154 tv1->vval.v_number = n1; 2155 --ectx.ec_stack.ga_len; 2156 } 2157 } 2158 break; 2159 2160 case ISN_CONCAT: 2161 { 2162 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string; 2163 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string; 2164 char_u *res; 2165 2166 res = concat_str(str1, str2); 2167 clear_tv(STACK_TV_BOT(-2)); 2168 clear_tv(STACK_TV_BOT(-1)); 2169 --ectx.ec_stack.ga_len; 2170 STACK_TV_BOT(-1)->vval.v_string = res; 2171 } 2172 break; 2173 2174 case ISN_STRINDEX: 2175 { 2176 char_u *s; 2177 varnumber_T n; 2178 char_u *res; 2179 2180 // string index: string is at stack-2, index at stack-1 2181 tv = STACK_TV_BOT(-2); 2182 if (tv->v_type != VAR_STRING) 2183 { 2184 emsg(_(e_stringreq)); 2185 goto on_error; 2186 } 2187 s = tv->vval.v_string; 2188 2189 tv = STACK_TV_BOT(-1); 2190 if (tv->v_type != VAR_NUMBER) 2191 { 2192 emsg(_(e_number_exp)); 2193 goto on_error; 2194 } 2195 n = tv->vval.v_number; 2196 2197 // The resulting variable is a string of a single 2198 // character. If the index is too big or negative the 2199 // result is empty. 2200 if (n < 0 || n >= (varnumber_T)STRLEN(s)) 2201 res = NULL; 2202 else 2203 res = vim_strnsave(s + n, 1); 2204 --ectx.ec_stack.ga_len; 2205 tv = STACK_TV_BOT(-1); 2206 vim_free(tv->vval.v_string); 2207 tv->vval.v_string = res; 2208 } 2209 break; 2210 2211 case ISN_LISTINDEX: 2212 { 2213 list_T *list; 2214 varnumber_T n; 2215 listitem_T *li; 2216 typval_T temp_tv; 2217 2218 // list index: list is at stack-2, index at stack-1 2219 tv = STACK_TV_BOT(-2); 2220 if (tv->v_type != VAR_LIST) 2221 { 2222 emsg(_(e_listreq)); 2223 goto on_error; 2224 } 2225 list = tv->vval.v_list; 2226 2227 tv = STACK_TV_BOT(-1); 2228 if (tv->v_type != VAR_NUMBER) 2229 { 2230 emsg(_(e_number_exp)); 2231 goto on_error; 2232 } 2233 n = tv->vval.v_number; 2234 clear_tv(tv); 2235 if ((li = list_find(list, n)) == NULL) 2236 { 2237 semsg(_(e_listidx), n); 2238 goto on_error; 2239 } 2240 --ectx.ec_stack.ga_len; 2241 // Clear the list after getting the item, to avoid that it 2242 // makes the item invalid. 2243 tv = STACK_TV_BOT(-1); 2244 temp_tv = *tv; 2245 copy_tv(&li->li_tv, tv); 2246 clear_tv(&temp_tv); 2247 } 2248 break; 2249 2250 case ISN_SLICE: 2251 { 2252 list_T *list; 2253 int count = iptr->isn_arg.number; 2254 2255 // type will have been checked to be a list 2256 tv = STACK_TV_BOT(-1); 2257 list = tv->vval.v_list; 2258 2259 // no error for short list, expect it to be checked earlier 2260 if (list != NULL && list->lv_len >= count) 2261 { 2262 list_T *newlist = list_slice(list, 2263 count, list->lv_len - 1); 2264 2265 if (newlist != NULL) 2266 { 2267 list_unref(list); 2268 tv->vval.v_list = newlist; 2269 ++newlist->lv_refcount; 2270 } 2271 } 2272 } 2273 break; 2274 2275 case ISN_GETITEM: 2276 { 2277 listitem_T *li; 2278 int index = iptr->isn_arg.number; 2279 2280 // Get list item: list is at stack-1, push item. 2281 // List type and length is checked for when compiling. 2282 tv = STACK_TV_BOT(-1); 2283 li = list_find(tv->vval.v_list, index); 2284 2285 if (GA_GROW(&ectx.ec_stack, 1) == FAIL) 2286 goto failed; 2287 ++ectx.ec_stack.ga_len; 2288 copy_tv(&li->li_tv, STACK_TV_BOT(-1)); 2289 } 2290 break; 2291 2292 case ISN_MEMBER: 2293 { 2294 dict_T *dict; 2295 char_u *key; 2296 dictitem_T *di; 2297 typval_T temp_tv; 2298 2299 // dict member: dict is at stack-2, key at stack-1 2300 tv = STACK_TV_BOT(-2); 2301 // no need to check for VAR_DICT, CHECKTYPE will check. 2302 dict = tv->vval.v_dict; 2303 2304 tv = STACK_TV_BOT(-1); 2305 // no need to check for VAR_STRING, 2STRING will check. 2306 key = tv->vval.v_string; 2307 2308 if ((di = dict_find(dict, key, -1)) == NULL) 2309 { 2310 semsg(_(e_dictkey), key); 2311 goto on_error; 2312 } 2313 clear_tv(tv); 2314 --ectx.ec_stack.ga_len; 2315 // Clear the dict after getting the item, to avoid that it 2316 // make the item invalid. 2317 tv = STACK_TV_BOT(-1); 2318 temp_tv = *tv; 2319 copy_tv(&di->di_tv, tv); 2320 clear_tv(&temp_tv); 2321 } 2322 break; 2323 2324 // dict member with string key 2325 case ISN_STRINGMEMBER: 2326 { 2327 dict_T *dict; 2328 dictitem_T *di; 2329 typval_T temp_tv; 2330 2331 tv = STACK_TV_BOT(-1); 2332 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL) 2333 { 2334 emsg(_(e_dictreq)); 2335 goto on_error; 2336 } 2337 dict = tv->vval.v_dict; 2338 2339 if ((di = dict_find(dict, iptr->isn_arg.string, -1)) 2340 == NULL) 2341 { 2342 semsg(_(e_dictkey), iptr->isn_arg.string); 2343 goto on_error; 2344 } 2345 // Clear the dict after getting the item, to avoid that it 2346 // make the item invalid. 2347 temp_tv = *tv; 2348 copy_tv(&di->di_tv, tv); 2349 clear_tv(&temp_tv); 2350 } 2351 break; 2352 2353 case ISN_NEGATENR: 2354 tv = STACK_TV_BOT(-1); 2355 if (tv->v_type != VAR_NUMBER 2356 #ifdef FEAT_FLOAT 2357 && tv->v_type != VAR_FLOAT 2358 #endif 2359 ) 2360 { 2361 emsg(_(e_number_exp)); 2362 goto on_error; 2363 } 2364 #ifdef FEAT_FLOAT 2365 if (tv->v_type == VAR_FLOAT) 2366 tv->vval.v_float = -tv->vval.v_float; 2367 else 2368 #endif 2369 tv->vval.v_number = -tv->vval.v_number; 2370 break; 2371 2372 case ISN_CHECKNR: 2373 { 2374 int error = FALSE; 2375 2376 tv = STACK_TV_BOT(-1); 2377 if (check_not_string(tv) == FAIL) 2378 goto on_error; 2379 (void)tv_get_number_chk(tv, &error); 2380 if (error) 2381 goto on_error; 2382 } 2383 break; 2384 2385 case ISN_CHECKTYPE: 2386 { 2387 checktype_T *ct = &iptr->isn_arg.type; 2388 2389 tv = STACK_TV_BOT(ct->ct_off); 2390 // TODO: better type comparison 2391 if (tv->v_type != ct->ct_type 2392 && !((tv->v_type == VAR_PARTIAL 2393 && ct->ct_type == VAR_FUNC) 2394 || (tv->v_type == VAR_FUNC 2395 && ct->ct_type == VAR_PARTIAL))) 2396 { 2397 semsg(_("E1029: Expected %s but got %s"), 2398 vartype_name(ct->ct_type), 2399 vartype_name(tv->v_type)); 2400 goto on_error; 2401 } 2402 } 2403 break; 2404 2405 case ISN_CHECKLEN: 2406 { 2407 int min_len = iptr->isn_arg.checklen.cl_min_len; 2408 list_T *list = NULL; 2409 2410 tv = STACK_TV_BOT(-1); 2411 if (tv->v_type == VAR_LIST) 2412 list = tv->vval.v_list; 2413 if (list == NULL || list->lv_len < min_len 2414 || (list->lv_len > min_len 2415 && !iptr->isn_arg.checklen.cl_more_OK)) 2416 { 2417 semsg(_("E1093: Expected %d items but got %d"), 2418 min_len, list == NULL ? 0 : list->lv_len); 2419 goto on_error; 2420 } 2421 } 2422 break; 2423 2424 case ISN_2BOOL: 2425 { 2426 int n; 2427 2428 tv = STACK_TV_BOT(-1); 2429 n = tv2bool(tv); 2430 if (iptr->isn_arg.number) // invert 2431 n = !n; 2432 clear_tv(tv); 2433 tv->v_type = VAR_BOOL; 2434 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE; 2435 } 2436 break; 2437 2438 case ISN_2STRING: 2439 { 2440 char_u *str; 2441 2442 tv = STACK_TV_BOT(iptr->isn_arg.number); 2443 if (tv->v_type != VAR_STRING) 2444 { 2445 str = typval_tostring(tv); 2446 clear_tv(tv); 2447 tv->v_type = VAR_STRING; 2448 tv->vval.v_string = str; 2449 } 2450 } 2451 break; 2452 2453 case ISN_SHUFFLE: 2454 { 2455 typval_T tmp_tv; 2456 int item = iptr->isn_arg.shuffle.shfl_item; 2457 int up = iptr->isn_arg.shuffle.shfl_up; 2458 2459 tmp_tv = *STACK_TV_BOT(-item); 2460 for ( ; up > 0 && item > 1; --up) 2461 { 2462 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1); 2463 --item; 2464 } 2465 *STACK_TV_BOT(-item) = tmp_tv; 2466 } 2467 break; 2468 2469 case ISN_DROP: 2470 --ectx.ec_stack.ga_len; 2471 clear_tv(STACK_TV_BOT(0)); 2472 break; 2473 } 2474 continue; 2475 2476 func_return: 2477 // Restore previous function. If the frame pointer is zero then there 2478 // is none and we are done. 2479 if (ectx.ec_frame_idx == initial_frame_idx) 2480 { 2481 if (handle_closure_in_use(&ectx, FALSE) == FAIL) 2482 // only fails when out of memory 2483 goto failed; 2484 goto done; 2485 } 2486 if (func_return(&ectx) == FAIL) 2487 // only fails when out of memory 2488 goto failed; 2489 continue; 2490 2491 on_error: 2492 if (trylevel == 0) 2493 goto failed; 2494 } 2495 2496 done: 2497 // function finished, get result from the stack. 2498 tv = STACK_TV_BOT(-1); 2499 *rettv = *tv; 2500 tv->v_type = VAR_UNKNOWN; 2501 ret = OK; 2502 2503 failed: 2504 // When failed need to unwind the call stack. 2505 while (ectx.ec_frame_idx != initial_frame_idx) 2506 func_return(&ectx); 2507 failed_early: 2508 current_sctx.sc_version = save_sc_version; 2509 2510 // Free all local variables, but not arguments. 2511 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx) 2512 clear_tv(STACK_TV(idx)); 2513 2514 vim_free(ectx.ec_stack.ga_data); 2515 vim_free(ectx.ec_trystack.ga_data); 2516 2517 if (ret != OK && called_emsg == called_emsg_before) 2518 semsg(_("E1099: Unknown error while executing %s"), 2519 printable_func_name(ufunc)); 2520 return ret; 2521 } 2522 2523 /* 2524 * ":dissassemble". 2525 * We don't really need this at runtime, but we do have tests that require it, 2526 * so always include this. 2527 */ 2528 void 2529 ex_disassemble(exarg_T *eap) 2530 { 2531 char_u *arg = eap->arg; 2532 char_u *fname; 2533 ufunc_T *ufunc; 2534 dfunc_T *dfunc; 2535 isn_T *instr; 2536 int current; 2537 int line_idx = 0; 2538 int prev_current = 0; 2539 int is_global = FALSE; 2540 2541 if (STRNCMP(arg, "<lambda>", 8) == 0) 2542 { 2543 arg += 8; 2544 (void)getdigits(&arg); 2545 fname = vim_strnsave(eap->arg, arg - eap->arg); 2546 } 2547 else 2548 fname = trans_function_name(&arg, &is_global, FALSE, 2549 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL); 2550 if (fname == NULL) 2551 { 2552 semsg(_(e_invarg2), eap->arg); 2553 return; 2554 } 2555 2556 ufunc = find_func(fname, is_global, NULL); 2557 if (ufunc == NULL) 2558 { 2559 char_u *p = untrans_function_name(fname); 2560 2561 if (p != NULL) 2562 // Try again without making it script-local. 2563 ufunc = find_func(p, FALSE, NULL); 2564 } 2565 vim_free(fname); 2566 if (ufunc == NULL) 2567 { 2568 semsg(_("E1061: Cannot find function %s"), eap->arg); 2569 return; 2570 } 2571 if (ufunc->uf_def_status == UF_TO_BE_COMPILED 2572 && compile_def_function(ufunc, FALSE, NULL) == FAIL) 2573 return; 2574 if (ufunc->uf_def_status != UF_COMPILED) 2575 { 2576 semsg(_("E1062: Function %s is not compiled"), eap->arg); 2577 return; 2578 } 2579 if (ufunc->uf_name_exp != NULL) 2580 msg((char *)ufunc->uf_name_exp); 2581 else 2582 msg((char *)ufunc->uf_name); 2583 2584 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; 2585 instr = dfunc->df_instr; 2586 for (current = 0; current < dfunc->df_instr_count; ++current) 2587 { 2588 isn_T *iptr = &instr[current]; 2589 char *line; 2590 2591 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len) 2592 { 2593 if (current > prev_current) 2594 { 2595 msg_puts("\n\n"); 2596 prev_current = current; 2597 } 2598 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++]; 2599 if (line != NULL) 2600 msg(line); 2601 } 2602 2603 switch (iptr->isn_type) 2604 { 2605 case ISN_EXEC: 2606 smsg("%4d EXEC %s", current, iptr->isn_arg.string); 2607 break; 2608 case ISN_EXECCONCAT: 2609 smsg("%4d EXECCONCAT %lld", current, 2610 (long long)iptr->isn_arg.number); 2611 break; 2612 case ISN_ECHO: 2613 { 2614 echo_T *echo = &iptr->isn_arg.echo; 2615 2616 smsg("%4d %s %d", current, 2617 echo->echo_with_white ? "ECHO" : "ECHON", 2618 echo->echo_count); 2619 } 2620 break; 2621 case ISN_EXECUTE: 2622 smsg("%4d EXECUTE %lld", current, 2623 (long long)(iptr->isn_arg.number)); 2624 break; 2625 case ISN_ECHOMSG: 2626 smsg("%4d ECHOMSG %lld", current, 2627 (long long)(iptr->isn_arg.number)); 2628 break; 2629 case ISN_ECHOERR: 2630 smsg("%4d ECHOERR %lld", current, 2631 (long long)(iptr->isn_arg.number)); 2632 break; 2633 case ISN_LOAD: 2634 case ISN_LOADOUTER: 2635 { 2636 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER"; 2637 2638 if (iptr->isn_arg.number < 0) 2639 smsg("%4d LOAD%s arg[%lld]", current, add, 2640 (long long)(iptr->isn_arg.number 2641 + STACK_FRAME_SIZE)); 2642 else 2643 smsg("%4d LOAD%s $%lld", current, add, 2644 (long long)(iptr->isn_arg.number)); 2645 } 2646 break; 2647 case ISN_LOADV: 2648 smsg("%4d LOADV v:%s", current, 2649 get_vim_var_name(iptr->isn_arg.number)); 2650 break; 2651 case ISN_LOADSCRIPT: 2652 { 2653 scriptitem_T *si = 2654 SCRIPT_ITEM(iptr->isn_arg.script.script_sid); 2655 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 2656 + iptr->isn_arg.script.script_idx; 2657 2658 smsg("%4d LOADSCRIPT %s from %s", current, 2659 sv->sv_name, si->sn_name); 2660 } 2661 break; 2662 case ISN_LOADS: 2663 { 2664 scriptitem_T *si = SCRIPT_ITEM( 2665 iptr->isn_arg.loadstore.ls_sid); 2666 2667 smsg("%4d LOADS s:%s from %s", current, 2668 iptr->isn_arg.loadstore.ls_name, si->sn_name); 2669 } 2670 break; 2671 case ISN_LOADG: 2672 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string); 2673 break; 2674 case ISN_LOADB: 2675 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string); 2676 break; 2677 case ISN_LOADW: 2678 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string); 2679 break; 2680 case ISN_LOADT: 2681 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string); 2682 break; 2683 case ISN_LOADGDICT: 2684 smsg("%4d LOAD g:", current); 2685 break; 2686 case ISN_LOADBDICT: 2687 smsg("%4d LOAD b:", current); 2688 break; 2689 case ISN_LOADWDICT: 2690 smsg("%4d LOAD w:", current); 2691 break; 2692 case ISN_LOADTDICT: 2693 smsg("%4d LOAD t:", current); 2694 break; 2695 case ISN_LOADOPT: 2696 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string); 2697 break; 2698 case ISN_LOADENV: 2699 smsg("%4d LOADENV %s", current, iptr->isn_arg.string); 2700 break; 2701 case ISN_LOADREG: 2702 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number)); 2703 break; 2704 2705 case ISN_STORE: 2706 case ISN_STOREOUTER: 2707 { 2708 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER"; 2709 2710 if (iptr->isn_arg.number < 0) 2711 smsg("%4d STORE%s arg[%lld]", current, add, 2712 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE)); 2713 else 2714 smsg("%4d STORE%s $%lld", current, add, 2715 (long long)(iptr->isn_arg.number)); 2716 } 2717 break; 2718 case ISN_STOREV: 2719 smsg("%4d STOREV v:%s", current, 2720 get_vim_var_name(iptr->isn_arg.number)); 2721 break; 2722 case ISN_STOREG: 2723 smsg("%4d STOREG %s", current, iptr->isn_arg.string); 2724 break; 2725 case ISN_STOREB: 2726 smsg("%4d STOREB %s", current, iptr->isn_arg.string); 2727 break; 2728 case ISN_STOREW: 2729 smsg("%4d STOREW %s", current, iptr->isn_arg.string); 2730 break; 2731 case ISN_STORET: 2732 smsg("%4d STORET %s", current, iptr->isn_arg.string); 2733 break; 2734 case ISN_STORES: 2735 { 2736 scriptitem_T *si = SCRIPT_ITEM( 2737 iptr->isn_arg.loadstore.ls_sid); 2738 2739 smsg("%4d STORES %s in %s", current, 2740 iptr->isn_arg.loadstore.ls_name, si->sn_name); 2741 } 2742 break; 2743 case ISN_STORESCRIPT: 2744 { 2745 scriptitem_T *si = 2746 SCRIPT_ITEM(iptr->isn_arg.script.script_sid); 2747 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) 2748 + iptr->isn_arg.script.script_idx; 2749 2750 smsg("%4d STORESCRIPT %s in %s", current, 2751 sv->sv_name, si->sn_name); 2752 } 2753 break; 2754 case ISN_STOREOPT: 2755 smsg("%4d STOREOPT &%s", current, 2756 iptr->isn_arg.storeopt.so_name); 2757 break; 2758 case ISN_STOREENV: 2759 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string); 2760 break; 2761 case ISN_STOREREG: 2762 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number); 2763 break; 2764 case ISN_STORENR: 2765 smsg("%4d STORE %lld in $%d", current, 2766 iptr->isn_arg.storenr.stnr_val, 2767 iptr->isn_arg.storenr.stnr_idx); 2768 break; 2769 2770 case ISN_STORELIST: 2771 smsg("%4d STORELIST", current); 2772 break; 2773 2774 case ISN_STOREDICT: 2775 smsg("%4d STOREDICT", current); 2776 break; 2777 2778 // constants 2779 case ISN_PUSHNR: 2780 smsg("%4d PUSHNR %lld", current, 2781 (long long)(iptr->isn_arg.number)); 2782 break; 2783 case ISN_PUSHBOOL: 2784 case ISN_PUSHSPEC: 2785 smsg("%4d PUSH %s", current, 2786 get_var_special_name(iptr->isn_arg.number)); 2787 break; 2788 case ISN_PUSHF: 2789 #ifdef FEAT_FLOAT 2790 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber); 2791 #endif 2792 break; 2793 case ISN_PUSHS: 2794 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string); 2795 break; 2796 case ISN_PUSHBLOB: 2797 { 2798 char_u *r; 2799 char_u numbuf[NUMBUFLEN]; 2800 char_u *tofree; 2801 2802 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf); 2803 smsg("%4d PUSHBLOB %s", current, r); 2804 vim_free(tofree); 2805 } 2806 break; 2807 case ISN_PUSHFUNC: 2808 { 2809 char *name = (char *)iptr->isn_arg.string; 2810 2811 smsg("%4d PUSHFUNC \"%s\"", current, 2812 name == NULL ? "[none]" : name); 2813 } 2814 break; 2815 case ISN_PUSHCHANNEL: 2816 #ifdef FEAT_JOB_CHANNEL 2817 { 2818 channel_T *channel = iptr->isn_arg.channel; 2819 2820 smsg("%4d PUSHCHANNEL %d", current, 2821 channel == NULL ? 0 : channel->ch_id); 2822 } 2823 #endif 2824 break; 2825 case ISN_PUSHJOB: 2826 #ifdef FEAT_JOB_CHANNEL 2827 { 2828 typval_T tv; 2829 char_u *name; 2830 2831 tv.v_type = VAR_JOB; 2832 tv.vval.v_job = iptr->isn_arg.job; 2833 name = tv_get_string(&tv); 2834 smsg("%4d PUSHJOB \"%s\"", current, name); 2835 } 2836 #endif 2837 break; 2838 case ISN_PUSHEXC: 2839 smsg("%4d PUSH v:exception", current); 2840 break; 2841 case ISN_UNLET: 2842 smsg("%4d UNLET%s %s", current, 2843 iptr->isn_arg.unlet.ul_forceit ? "!" : "", 2844 iptr->isn_arg.unlet.ul_name); 2845 break; 2846 case ISN_UNLETENV: 2847 smsg("%4d UNLETENV%s $%s", current, 2848 iptr->isn_arg.unlet.ul_forceit ? "!" : "", 2849 iptr->isn_arg.unlet.ul_name); 2850 break; 2851 case ISN_NEWLIST: 2852 smsg("%4d NEWLIST size %lld", current, 2853 (long long)(iptr->isn_arg.number)); 2854 break; 2855 case ISN_NEWDICT: 2856 smsg("%4d NEWDICT size %lld", current, 2857 (long long)(iptr->isn_arg.number)); 2858 break; 2859 2860 // function call 2861 case ISN_BCALL: 2862 { 2863 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc; 2864 2865 smsg("%4d BCALL %s(argc %d)", current, 2866 internal_func_name(cbfunc->cbf_idx), 2867 cbfunc->cbf_argcount); 2868 } 2869 break; 2870 case ISN_DCALL: 2871 { 2872 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc; 2873 dfunc_T *df = ((dfunc_T *)def_functions.ga_data) 2874 + cdfunc->cdf_idx; 2875 2876 smsg("%4d DCALL %s(argc %d)", current, 2877 df->df_ufunc->uf_name_exp != NULL 2878 ? df->df_ufunc->uf_name_exp 2879 : df->df_ufunc->uf_name, cdfunc->cdf_argcount); 2880 } 2881 break; 2882 case ISN_UCALL: 2883 { 2884 cufunc_T *cufunc = &iptr->isn_arg.ufunc; 2885 2886 smsg("%4d UCALL %s(argc %d)", current, 2887 cufunc->cuf_name, cufunc->cuf_argcount); 2888 } 2889 break; 2890 case ISN_PCALL: 2891 { 2892 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc; 2893 2894 smsg("%4d PCALL%s (argc %d)", current, 2895 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount); 2896 } 2897 break; 2898 case ISN_PCALL_END: 2899 smsg("%4d PCALL end", current); 2900 break; 2901 case ISN_RETURN: 2902 smsg("%4d RETURN", current); 2903 break; 2904 case ISN_FUNCREF: 2905 { 2906 funcref_T *funcref = &iptr->isn_arg.funcref; 2907 dfunc_T *df = ((dfunc_T *)def_functions.ga_data) 2908 + funcref->fr_func; 2909 2910 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name, 2911 funcref->fr_var_idx + dfunc->df_varcount); 2912 } 2913 break; 2914 2915 case ISN_JUMP: 2916 { 2917 char *when = "?"; 2918 2919 switch (iptr->isn_arg.jump.jump_when) 2920 { 2921 case JUMP_ALWAYS: 2922 when = "JUMP"; 2923 break; 2924 case JUMP_AND_KEEP_IF_TRUE: 2925 when = "JUMP_AND_KEEP_IF_TRUE"; 2926 break; 2927 case JUMP_IF_FALSE: 2928 when = "JUMP_IF_FALSE"; 2929 break; 2930 case JUMP_AND_KEEP_IF_FALSE: 2931 when = "JUMP_AND_KEEP_IF_FALSE"; 2932 break; 2933 } 2934 smsg("%4d %s -> %d", current, when, 2935 iptr->isn_arg.jump.jump_where); 2936 } 2937 break; 2938 2939 case ISN_FOR: 2940 { 2941 forloop_T *forloop = &iptr->isn_arg.forloop; 2942 2943 smsg("%4d FOR $%d -> %d", current, 2944 forloop->for_idx, forloop->for_end); 2945 } 2946 break; 2947 2948 case ISN_TRY: 2949 { 2950 try_T *try = &iptr->isn_arg.try; 2951 2952 smsg("%4d TRY catch -> %d, finally -> %d", current, 2953 try->try_catch, try->try_finally); 2954 } 2955 break; 2956 case ISN_CATCH: 2957 // TODO 2958 smsg("%4d CATCH", current); 2959 break; 2960 case ISN_ENDTRY: 2961 smsg("%4d ENDTRY", current); 2962 break; 2963 case ISN_THROW: 2964 smsg("%4d THROW", current); 2965 break; 2966 2967 // expression operations on number 2968 case ISN_OPNR: 2969 case ISN_OPFLOAT: 2970 case ISN_OPANY: 2971 { 2972 char *what; 2973 char *ins; 2974 2975 switch (iptr->isn_arg.op.op_type) 2976 { 2977 case EXPR_MULT: what = "*"; break; 2978 case EXPR_DIV: what = "/"; break; 2979 case EXPR_REM: what = "%"; break; 2980 case EXPR_SUB: what = "-"; break; 2981 case EXPR_ADD: what = "+"; break; 2982 default: what = "???"; break; 2983 } 2984 switch (iptr->isn_type) 2985 { 2986 case ISN_OPNR: ins = "OPNR"; break; 2987 case ISN_OPFLOAT: ins = "OPFLOAT"; break; 2988 case ISN_OPANY: ins = "OPANY"; break; 2989 default: ins = "???"; break; 2990 } 2991 smsg("%4d %s %s", current, ins, what); 2992 } 2993 break; 2994 2995 case ISN_COMPAREBOOL: 2996 case ISN_COMPARESPECIAL: 2997 case ISN_COMPARENR: 2998 case ISN_COMPAREFLOAT: 2999 case ISN_COMPARESTRING: 3000 case ISN_COMPAREBLOB: 3001 case ISN_COMPARELIST: 3002 case ISN_COMPAREDICT: 3003 case ISN_COMPAREFUNC: 3004 case ISN_COMPAREANY: 3005 { 3006 char *p; 3007 char buf[10]; 3008 char *type; 3009 3010 switch (iptr->isn_arg.op.op_type) 3011 { 3012 case EXPR_EQUAL: p = "=="; break; 3013 case EXPR_NEQUAL: p = "!="; break; 3014 case EXPR_GREATER: p = ">"; break; 3015 case EXPR_GEQUAL: p = ">="; break; 3016 case EXPR_SMALLER: p = "<"; break; 3017 case EXPR_SEQUAL: p = "<="; break; 3018 case EXPR_MATCH: p = "=~"; break; 3019 case EXPR_IS: p = "is"; break; 3020 case EXPR_ISNOT: p = "isnot"; break; 3021 case EXPR_NOMATCH: p = "!~"; break; 3022 default: p = "???"; break; 3023 } 3024 STRCPY(buf, p); 3025 if (iptr->isn_arg.op.op_ic == TRUE) 3026 strcat(buf, "?"); 3027 switch(iptr->isn_type) 3028 { 3029 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break; 3030 case ISN_COMPARESPECIAL: 3031 type = "COMPARESPECIAL"; break; 3032 case ISN_COMPARENR: type = "COMPARENR"; break; 3033 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break; 3034 case ISN_COMPARESTRING: 3035 type = "COMPARESTRING"; break; 3036 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break; 3037 case ISN_COMPARELIST: type = "COMPARELIST"; break; 3038 case ISN_COMPAREDICT: type = "COMPAREDICT"; break; 3039 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break; 3040 case ISN_COMPAREANY: type = "COMPAREANY"; break; 3041 default: type = "???"; break; 3042 } 3043 3044 smsg("%4d %s %s", current, type, buf); 3045 } 3046 break; 3047 3048 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break; 3049 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break; 3050 3051 // expression operations 3052 case ISN_CONCAT: smsg("%4d CONCAT", current); break; 3053 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break; 3054 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break; 3055 case ISN_SLICE: smsg("%4d SLICE %lld", 3056 current, iptr->isn_arg.number); break; 3057 case ISN_GETITEM: smsg("%4d ITEM %lld", 3058 current, iptr->isn_arg.number); break; 3059 case ISN_MEMBER: smsg("%4d MEMBER", current); break; 3060 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current, 3061 iptr->isn_arg.string); break; 3062 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break; 3063 3064 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break; 3065 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current, 3066 vartype_name(iptr->isn_arg.type.ct_type), 3067 iptr->isn_arg.type.ct_off); 3068 break; 3069 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current, 3070 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "", 3071 iptr->isn_arg.checklen.cl_min_len); 3072 break; 3073 case ISN_2BOOL: if (iptr->isn_arg.number) 3074 smsg("%4d INVERT (!val)", current); 3075 else 3076 smsg("%4d 2BOOL (!!val)", current); 3077 break; 3078 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current, 3079 (long long)(iptr->isn_arg.number)); 3080 break; 3081 3082 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current, 3083 iptr->isn_arg.shuffle.shfl_item, 3084 iptr->isn_arg.shuffle.shfl_up); 3085 break; 3086 case ISN_DROP: smsg("%4d DROP", current); break; 3087 } 3088 } 3089 } 3090 3091 /* 3092 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty 3093 * list, etc. Mostly like what JavaScript does, except that empty list and 3094 * empty dictionary are FALSE. 3095 */ 3096 int 3097 tv2bool(typval_T *tv) 3098 { 3099 switch (tv->v_type) 3100 { 3101 case VAR_NUMBER: 3102 return tv->vval.v_number != 0; 3103 case VAR_FLOAT: 3104 #ifdef FEAT_FLOAT 3105 return tv->vval.v_float != 0.0; 3106 #else 3107 break; 3108 #endif 3109 case VAR_PARTIAL: 3110 return tv->vval.v_partial != NULL; 3111 case VAR_FUNC: 3112 case VAR_STRING: 3113 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL; 3114 case VAR_LIST: 3115 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0; 3116 case VAR_DICT: 3117 return tv->vval.v_dict != NULL 3118 && tv->vval.v_dict->dv_hashtab.ht_used > 0; 3119 case VAR_BOOL: 3120 case VAR_SPECIAL: 3121 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE; 3122 case VAR_JOB: 3123 #ifdef FEAT_JOB_CHANNEL 3124 return tv->vval.v_job != NULL; 3125 #else 3126 break; 3127 #endif 3128 case VAR_CHANNEL: 3129 #ifdef FEAT_JOB_CHANNEL 3130 return tv->vval.v_channel != NULL; 3131 #else 3132 break; 3133 #endif 3134 case VAR_BLOB: 3135 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0; 3136 case VAR_UNKNOWN: 3137 case VAR_ANY: 3138 case VAR_VOID: 3139 break; 3140 } 3141 return FALSE; 3142 } 3143 3144 /* 3145 * If "tv" is a string give an error and return FAIL. 3146 */ 3147 int 3148 check_not_string(typval_T *tv) 3149 { 3150 if (tv->v_type == VAR_STRING) 3151 { 3152 emsg(_("E1030: Using a String as a Number")); 3153 clear_tv(tv); 3154 return FAIL; 3155 } 3156 return OK; 3157 } 3158 3159 3160 #endif // FEAT_EVAL 3161