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