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