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