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