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