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