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