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