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 * userfunc.c: User defined function support 12 */ 13 14 #include "vim.h" 15 16 #if defined(FEAT_EVAL) || defined(PROTO) 17 // flags used in uf_flags 18 #define FC_ABORT 0x01 // abort function on error 19 #define FC_RANGE 0x02 // function accepts range 20 #define FC_DICT 0x04 // Dict function, uses "self" 21 #define FC_CLOSURE 0x08 // closure, uses outer scope variables 22 #define FC_DELETED 0x10 // :delfunction used while uf_refcount > 0 23 #define FC_REMOVED 0x20 // function redefined while uf_refcount > 0 24 #define FC_SANDBOX 0x40 // function defined in the sandbox 25 #define FC_DEAD 0x80 // function kept only for reference to dfunc 26 #define FC_EXPORT 0x100 // "export def Func()" 27 #define FC_NOARGS 0x200 // no a: variables in lambda 28 29 /* 30 * All user-defined functions are found in this hashtable. 31 */ 32 static hashtab_T func_hashtab; 33 34 // Used by get_func_tv() 35 static garray_T funcargs = GA_EMPTY; 36 37 // pointer to funccal for currently active function 38 static funccall_T *current_funccal = NULL; 39 40 // Pointer to list of previously used funccal, still around because some 41 // item in it is still being used. 42 static funccall_T *previous_funccal = NULL; 43 44 static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it"); 45 static char *e_funcdict = N_("E717: Dictionary entry already exists"); 46 static char *e_funcref = N_("E718: Funcref required"); 47 static char *e_nofunc = N_("E130: Unknown function: %s"); 48 49 static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force); 50 51 void 52 func_init() 53 { 54 hash_init(&func_hashtab); 55 } 56 57 /* 58 * Return the function hash table 59 */ 60 hashtab_T * 61 func_tbl_get(void) 62 { 63 return &func_hashtab; 64 } 65 66 /* 67 * Get one function argument. 68 * If "argtypes" is not NULL also get the type: "arg: type". 69 * Return a pointer to after the type. 70 * When something is wrong return "arg". 71 */ 72 static char_u * 73 one_function_arg(char_u *arg, garray_T *newargs, garray_T *argtypes, int skip) 74 { 75 char_u *p = arg; 76 char_u *arg_copy = NULL; 77 78 while (ASCII_ISALNUM(*p) || *p == '_') 79 ++p; 80 if (arg == p || isdigit(*arg) 81 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0) 82 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0)) 83 { 84 if (!skip) 85 semsg(_("E125: Illegal argument: %s"), arg); 86 return arg; 87 } 88 if (newargs != NULL && ga_grow(newargs, 1) == FAIL) 89 return arg; 90 if (newargs != NULL) 91 { 92 int c; 93 int i; 94 95 c = *p; 96 *p = NUL; 97 arg_copy = vim_strsave(arg); 98 if (arg_copy == NULL) 99 { 100 *p = c; 101 return arg; 102 } 103 104 // Check for duplicate argument name. 105 for (i = 0; i < newargs->ga_len; ++i) 106 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0) 107 { 108 semsg(_("E853: Duplicate argument name: %s"), arg_copy); 109 vim_free(arg_copy); 110 return arg; 111 } 112 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy; 113 newargs->ga_len++; 114 115 *p = c; 116 } 117 118 // get any type from "arg: type" 119 if (argtypes != NULL && ga_grow(argtypes, 1) == OK) 120 { 121 char_u *type = NULL; 122 123 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':') 124 { 125 semsg(_("E1059: No white space allowed before colon: %s"), 126 arg_copy == NULL ? arg : arg_copy); 127 p = skipwhite(p); 128 } 129 if (*p == ':') 130 { 131 type = skipwhite(p + 1); 132 p = skip_type(type); 133 type = vim_strnsave(type, p - type); 134 } 135 else if (*skipwhite(p) != '=') 136 { 137 semsg(_("E1077: Missing argument type for %s"), 138 arg_copy == NULL ? arg : arg_copy); 139 return arg; 140 } 141 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type; 142 } 143 144 return p; 145 } 146 147 /* 148 * Get function arguments. 149 */ 150 int 151 get_function_args( 152 char_u **argp, 153 char_u endchar, 154 garray_T *newargs, 155 garray_T *argtypes, // NULL unless using :def 156 int *varargs, 157 garray_T *default_args, 158 int skip, 159 exarg_T *eap, 160 char_u **line_to_free) 161 { 162 int mustend = FALSE; 163 char_u *arg = *argp; 164 char_u *p = arg; 165 int c; 166 int any_default = FALSE; 167 char_u *expr; 168 char_u *whitep = arg; 169 170 if (newargs != NULL) 171 ga_init2(newargs, (int)sizeof(char_u *), 3); 172 if (argtypes != NULL) 173 ga_init2(argtypes, (int)sizeof(char_u *), 3); 174 if (default_args != NULL) 175 ga_init2(default_args, (int)sizeof(char_u *), 3); 176 177 if (varargs != NULL) 178 *varargs = FALSE; 179 180 /* 181 * Isolate the arguments: "arg1, arg2, ...)" 182 */ 183 while (*p != endchar) 184 { 185 while (eap != NULL && eap->getline != NULL 186 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#'))) 187 { 188 char_u *theline; 189 190 // End of the line, get the next one. 191 theline = eap->getline(':', eap->cookie, 0, TRUE); 192 if (theline == NULL) 193 break; 194 vim_free(*line_to_free); 195 *line_to_free = theline; 196 whitep = (char_u *)" "; 197 p = skipwhite(theline); 198 } 199 200 if (mustend && *p != endchar) 201 { 202 if (!skip) 203 semsg(_(e_invarg2), *argp); 204 break; 205 } 206 if (*p == endchar) 207 break; 208 209 if (p[0] == '.' && p[1] == '.' && p[2] == '.') 210 { 211 if (varargs != NULL) 212 *varargs = TRUE; 213 p += 3; 214 mustend = TRUE; 215 216 if (argtypes != NULL) 217 { 218 // ...name: list<type> 219 if (!ASCII_ISALPHA(*p)) 220 { 221 emsg(_("E1055: Missing name after ...")); 222 break; 223 } 224 225 arg = p; 226 p = one_function_arg(p, newargs, argtypes, skip); 227 if (p == arg) 228 break; 229 } 230 } 231 else 232 { 233 arg = p; 234 p = one_function_arg(p, newargs, argtypes, skip); 235 if (p == arg) 236 break; 237 238 if (*skipwhite(p) == '=' && default_args != NULL) 239 { 240 typval_T rettv; 241 242 // find the end of the expression (doesn't evaluate it) 243 any_default = TRUE; 244 p = skipwhite(p) + 1; 245 whitep = p; 246 p = skipwhite(p); 247 expr = p; 248 if (eval1(&p, &rettv, FALSE) != FAIL) 249 { 250 if (ga_grow(default_args, 1) == FAIL) 251 goto err_ret; 252 253 // trim trailing whitespace 254 while (p > expr && VIM_ISWHITE(p[-1])) 255 p--; 256 c = *p; 257 *p = NUL; 258 expr = vim_strsave(expr); 259 if (expr == NULL) 260 { 261 *p = c; 262 goto err_ret; 263 } 264 ((char_u **)(default_args->ga_data)) 265 [default_args->ga_len] = expr; 266 default_args->ga_len++; 267 *p = c; 268 } 269 else 270 mustend = TRUE; 271 } 272 else if (any_default) 273 { 274 emsg(_("E989: Non-default argument follows default argument")); 275 mustend = TRUE; 276 } 277 if (*p == ',') 278 ++p; 279 else 280 mustend = TRUE; 281 } 282 whitep = p; 283 p = skipwhite(p); 284 } 285 286 if (*p != endchar) 287 goto err_ret; 288 ++p; // skip "endchar" 289 290 *argp = p; 291 return OK; 292 293 err_ret: 294 if (newargs != NULL) 295 ga_clear_strings(newargs); 296 if (default_args != NULL) 297 ga_clear_strings(default_args); 298 return FAIL; 299 } 300 301 /* 302 * Register function "fp" as using "current_funccal" as its scope. 303 */ 304 static int 305 register_closure(ufunc_T *fp) 306 { 307 if (fp->uf_scoped == current_funccal) 308 // no change 309 return OK; 310 funccal_unref(fp->uf_scoped, fp, FALSE); 311 fp->uf_scoped = current_funccal; 312 current_funccal->fc_refcount++; 313 314 if (ga_grow(¤t_funccal->fc_funcs, 1) == FAIL) 315 return FAIL; 316 ((ufunc_T **)current_funccal->fc_funcs.ga_data) 317 [current_funccal->fc_funcs.ga_len++] = fp; 318 return OK; 319 } 320 321 static void 322 set_ufunc_name(ufunc_T *fp, char_u *name) 323 { 324 STRCPY(fp->uf_name, name); 325 326 if (name[0] == K_SPECIAL) 327 { 328 fp->uf_name_exp = alloc(STRLEN(name) + 3); 329 if (fp->uf_name_exp != NULL) 330 { 331 STRCPY(fp->uf_name_exp, "<SNR>"); 332 STRCAT(fp->uf_name_exp, fp->uf_name + 3); 333 } 334 } 335 } 336 337 /* 338 * Parse a lambda expression and get a Funcref from "*arg". 339 * Return OK or FAIL. Returns NOTDONE for dict or {expr}. 340 */ 341 int 342 get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate) 343 { 344 garray_T newargs; 345 garray_T newlines; 346 garray_T *pnewargs; 347 ufunc_T *fp = NULL; 348 partial_T *pt = NULL; 349 int varargs; 350 int ret; 351 char_u *start = skipwhite(*arg + 1); 352 char_u *s, *e; 353 static int lambda_no = 0; 354 int *old_eval_lavars = eval_lavars_used; 355 int eval_lavars = FALSE; 356 357 ga_init(&newargs); 358 ga_init(&newlines); 359 360 // First, check if this is a lambda expression. "->" must exist. 361 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE, 362 NULL, NULL); 363 if (ret == FAIL || *start != '>') 364 return NOTDONE; 365 366 // Parse the arguments again. 367 if (evaluate) 368 pnewargs = &newargs; 369 else 370 pnewargs = NULL; 371 *arg = skipwhite(*arg + 1); 372 // TODO: argument types 373 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE, 374 NULL, NULL); 375 if (ret == FAIL || **arg != '>') 376 goto errret; 377 378 // Set up a flag for checking local variables and arguments. 379 if (evaluate) 380 eval_lavars_used = &eval_lavars; 381 382 // Get the start and the end of the expression. 383 *arg = skipwhite(*arg + 1); 384 s = *arg; 385 ret = skip_expr(arg); 386 if (ret == FAIL) 387 goto errret; 388 e = *arg; 389 *arg = skipwhite(*arg); 390 if (**arg != '}') 391 { 392 semsg(_("E451: Expected }: %s"), *arg); 393 goto errret; 394 } 395 ++*arg; 396 397 if (evaluate) 398 { 399 int len, flags = 0; 400 char_u *p; 401 char_u name[20]; 402 403 sprintf((char*)name, "<lambda>%d", ++lambda_no); 404 405 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1); 406 if (fp == NULL) 407 goto errret; 408 fp->uf_dfunc_idx = -1; 409 pt = ALLOC_CLEAR_ONE(partial_T); 410 if (pt == NULL) 411 goto errret; 412 413 ga_init2(&newlines, (int)sizeof(char_u *), 1); 414 if (ga_grow(&newlines, 1) == FAIL) 415 goto errret; 416 417 // Add "return " before the expression. 418 len = 7 + e - s + 1; 419 p = alloc(len); 420 if (p == NULL) 421 goto errret; 422 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p; 423 STRCPY(p, "return "); 424 vim_strncpy(p + 7, s, e - s); 425 if (strstr((char *)p + 7, "a:") == NULL) 426 // No a: variables are used for sure. 427 flags |= FC_NOARGS; 428 429 fp->uf_refcount = 1; 430 set_ufunc_name(fp, name); 431 hash_add(&func_hashtab, UF2HIKEY(fp)); 432 fp->uf_args = newargs; 433 ga_init(&fp->uf_def_args); 434 fp->uf_lines = newlines; 435 if (current_funccal != NULL && eval_lavars) 436 { 437 flags |= FC_CLOSURE; 438 if (register_closure(fp) == FAIL) 439 goto errret; 440 } 441 else 442 fp->uf_scoped = NULL; 443 444 #ifdef FEAT_PROFILE 445 if (prof_def_func()) 446 func_do_profile(fp); 447 #endif 448 if (sandbox) 449 flags |= FC_SANDBOX; 450 // can be called with more args than uf_args.ga_len 451 fp->uf_varargs = TRUE; 452 fp->uf_flags = flags; 453 fp->uf_calls = 0; 454 fp->uf_script_ctx = current_sctx; 455 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len; 456 457 pt->pt_func = fp; 458 pt->pt_refcount = 1; 459 rettv->vval.v_partial = pt; 460 rettv->v_type = VAR_PARTIAL; 461 } 462 463 eval_lavars_used = old_eval_lavars; 464 return OK; 465 466 errret: 467 ga_clear_strings(&newargs); 468 ga_clear_strings(&newlines); 469 vim_free(fp); 470 vim_free(pt); 471 eval_lavars_used = old_eval_lavars; 472 return FAIL; 473 } 474 475 /* 476 * Check if "name" is a variable of type VAR_FUNC. If so, return the function 477 * name it contains, otherwise return "name". 478 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set 479 * "partialp". 480 */ 481 char_u * 482 deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload) 483 { 484 dictitem_T *v; 485 int cc; 486 char_u *s; 487 488 if (partialp != NULL) 489 *partialp = NULL; 490 491 cc = name[*lenp]; 492 name[*lenp] = NUL; 493 v = find_var(name, NULL, no_autoload); 494 name[*lenp] = cc; 495 if (v != NULL && v->di_tv.v_type == VAR_FUNC) 496 { 497 if (v->di_tv.vval.v_string == NULL) 498 { 499 *lenp = 0; 500 return (char_u *)""; // just in case 501 } 502 s = v->di_tv.vval.v_string; 503 *lenp = (int)STRLEN(s); 504 return s; 505 } 506 507 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL) 508 { 509 partial_T *pt = v->di_tv.vval.v_partial; 510 511 if (pt == NULL) 512 { 513 *lenp = 0; 514 return (char_u *)""; // just in case 515 } 516 if (partialp != NULL) 517 *partialp = pt; 518 s = partial_name(pt); 519 *lenp = (int)STRLEN(s); 520 return s; 521 } 522 523 return name; 524 } 525 526 /* 527 * Give an error message with a function name. Handle <SNR> things. 528 * "ermsg" is to be passed without translation, use N_() instead of _(). 529 */ 530 void 531 emsg_funcname(char *ermsg, char_u *name) 532 { 533 char_u *p; 534 535 if (*name == K_SPECIAL) 536 p = concat_str((char_u *)"<SNR>", name + 3); 537 else 538 p = name; 539 semsg(_(ermsg), p); 540 if (p != name) 541 vim_free(p); 542 } 543 544 /* 545 * Allocate a variable for the result of a function. 546 * Return OK or FAIL. 547 */ 548 int 549 get_func_tv( 550 char_u *name, // name of the function 551 int len, // length of "name" or -1 to use strlen() 552 typval_T *rettv, 553 char_u **arg, // argument, pointing to the '(' 554 funcexe_T *funcexe) // various values 555 { 556 char_u *argp; 557 int ret = OK; 558 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments 559 int argcount = 0; // number of arguments found 560 561 /* 562 * Get the arguments. 563 */ 564 argp = *arg; 565 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0 566 : funcexe->partial->pt_argc)) 567 { 568 argp = skipwhite(argp + 1); // skip the '(' or ',' 569 if (*argp == ')' || *argp == ',' || *argp == NUL) 570 break; 571 if (eval1(&argp, &argvars[argcount], funcexe->evaluate) == FAIL) 572 { 573 ret = FAIL; 574 break; 575 } 576 ++argcount; 577 if (*argp != ',') 578 break; 579 } 580 if (*argp == ')') 581 ++argp; 582 else 583 ret = FAIL; 584 585 if (ret == OK) 586 { 587 int i = 0; 588 589 if (get_vim_var_nr(VV_TESTING)) 590 { 591 // Prepare for calling test_garbagecollect_now(), need to know 592 // what variables are used on the call stack. 593 if (funcargs.ga_itemsize == 0) 594 ga_init2(&funcargs, (int)sizeof(typval_T *), 50); 595 for (i = 0; i < argcount; ++i) 596 if (ga_grow(&funcargs, 1) == OK) 597 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] = 598 &argvars[i]; 599 } 600 601 ret = call_func(name, len, rettv, argcount, argvars, funcexe); 602 603 funcargs.ga_len -= i; 604 } 605 else if (!aborting()) 606 { 607 if (argcount == MAX_FUNC_ARGS) 608 emsg_funcname(N_("E740: Too many arguments for function %s"), name); 609 else 610 emsg_funcname(N_("E116: Invalid arguments for function %s"), name); 611 } 612 613 while (--argcount >= 0) 614 clear_tv(&argvars[argcount]); 615 616 *arg = skipwhite(argp); 617 return ret; 618 } 619 620 /* 621 * Return TRUE if "p" starts with "<SID>" or "s:". 622 * Only works if eval_fname_script() returned non-zero for "p"! 623 */ 624 static int 625 eval_fname_sid(char_u *p) 626 { 627 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I'); 628 } 629 630 /* 631 * In a script change <SID>name() and s:name() to K_SNR 123_name(). 632 * Change <SNR>123_name() to K_SNR 123_name(). 633 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory 634 * (slow). 635 */ 636 char_u * 637 fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error) 638 { 639 int llen; 640 char_u *fname; 641 int i; 642 643 llen = eval_fname_script(name); 644 if (llen > 0) 645 { 646 fname_buf[0] = K_SPECIAL; 647 fname_buf[1] = KS_EXTRA; 648 fname_buf[2] = (int)KE_SNR; 649 i = 3; 650 if (eval_fname_sid(name)) // "<SID>" or "s:" 651 { 652 if (current_sctx.sc_sid <= 0) 653 *error = FCERR_SCRIPT; 654 else 655 { 656 sprintf((char *)fname_buf + 3, "%ld_", 657 (long)current_sctx.sc_sid); 658 i = (int)STRLEN(fname_buf); 659 } 660 } 661 if (i + STRLEN(name + llen) < FLEN_FIXED) 662 { 663 STRCPY(fname_buf + i, name + llen); 664 fname = fname_buf; 665 } 666 else 667 { 668 fname = alloc(i + STRLEN(name + llen) + 1); 669 if (fname == NULL) 670 *error = FCERR_OTHER; 671 else 672 { 673 *tofree = fname; 674 mch_memmove(fname, fname_buf, (size_t)i); 675 STRCPY(fname + i, name + llen); 676 } 677 } 678 } 679 else 680 fname = name; 681 return fname; 682 } 683 684 /* 685 * Find a function "name" in script "sid". 686 */ 687 static ufunc_T * 688 find_func_with_sid(char_u *name, int sid) 689 { 690 hashitem_T *hi; 691 char_u buffer[200]; 692 693 buffer[0] = K_SPECIAL; 694 buffer[1] = KS_EXTRA; 695 buffer[2] = (int)KE_SNR; 696 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s", 697 (long)sid, name); 698 hi = hash_find(&func_hashtab, buffer); 699 if (!HASHITEM_EMPTY(hi)) 700 return HI2UF(hi); 701 702 return NULL; 703 } 704 705 /* 706 * Find a function by name, return pointer to it in ufuncs. 707 * Return NULL for unknown function. 708 */ 709 static ufunc_T * 710 find_func_even_dead(char_u *name, cctx_T *cctx) 711 { 712 hashitem_T *hi; 713 ufunc_T *func; 714 imported_T *imported; 715 716 if (in_vim9script()) 717 { 718 // Find script-local function before global one. 719 func = find_func_with_sid(name, current_sctx.sc_sid); 720 if (func != NULL) 721 return func; 722 723 // Find imported funcion before global one. 724 imported = find_imported(name, 0, cctx); 725 if (imported != NULL && imported->imp_funcname != NULL) 726 { 727 hi = hash_find(&func_hashtab, imported->imp_funcname); 728 if (!HASHITEM_EMPTY(hi)) 729 return HI2UF(hi); 730 } 731 } 732 733 hi = hash_find(&func_hashtab, 734 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name); 735 if (!HASHITEM_EMPTY(hi)) 736 return HI2UF(hi); 737 738 return NULL; 739 } 740 741 /* 742 * Find a function by name, return pointer to it in ufuncs. 743 * "cctx" is passed in a :def function to find imported functions. 744 * Return NULL for unknown or dead function. 745 */ 746 ufunc_T * 747 find_func(char_u *name, cctx_T *cctx) 748 { 749 ufunc_T *fp = find_func_even_dead(name, cctx); 750 751 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0) 752 return fp; 753 return NULL; 754 } 755 756 /* 757 * Copy the function name of "fp" to buffer "buf". 758 * "buf" must be able to hold the function name plus three bytes. 759 * Takes care of script-local function names. 760 */ 761 static void 762 cat_func_name(char_u *buf, ufunc_T *fp) 763 { 764 if (fp->uf_name[0] == K_SPECIAL) 765 { 766 STRCPY(buf, "<SNR>"); 767 STRCAT(buf, fp->uf_name + 3); 768 } 769 else 770 STRCPY(buf, fp->uf_name); 771 } 772 773 /* 774 * Add a number variable "name" to dict "dp" with value "nr". 775 */ 776 static void 777 add_nr_var( 778 dict_T *dp, 779 dictitem_T *v, 780 char *name, 781 varnumber_T nr) 782 { 783 STRCPY(v->di_key, name); 784 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 785 hash_add(&dp->dv_hashtab, DI2HIKEY(v)); 786 v->di_tv.v_type = VAR_NUMBER; 787 v->di_tv.v_lock = VAR_FIXED; 788 v->di_tv.vval.v_number = nr; 789 } 790 791 /* 792 * Free "fc". 793 */ 794 static void 795 free_funccal(funccall_T *fc) 796 { 797 int i; 798 799 for (i = 0; i < fc->fc_funcs.ga_len; ++i) 800 { 801 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i]; 802 803 // When garbage collecting a funccall_T may be freed before the 804 // function that references it, clear its uf_scoped field. 805 // The function may have been redefined and point to another 806 // funccall_T, don't clear it then. 807 if (fp != NULL && fp->uf_scoped == fc) 808 fp->uf_scoped = NULL; 809 } 810 ga_clear(&fc->fc_funcs); 811 812 func_ptr_unref(fc->func); 813 vim_free(fc); 814 } 815 816 /* 817 * Free "fc" and what it contains. 818 * Can be called only when "fc" is kept beyond the period of it called, 819 * i.e. after cleanup_function_call(fc). 820 */ 821 static void 822 free_funccal_contents(funccall_T *fc) 823 { 824 listitem_T *li; 825 826 // Free all l: variables. 827 vars_clear(&fc->l_vars.dv_hashtab); 828 829 // Free all a: variables. 830 vars_clear(&fc->l_avars.dv_hashtab); 831 832 // Free the a:000 variables. 833 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li) 834 clear_tv(&li->li_tv); 835 836 free_funccal(fc); 837 } 838 839 /* 840 * Handle the last part of returning from a function: free the local hashtable. 841 * Unless it is still in use by a closure. 842 */ 843 static void 844 cleanup_function_call(funccall_T *fc) 845 { 846 int may_free_fc = fc->fc_refcount <= 0; 847 int free_fc = TRUE; 848 849 current_funccal = fc->caller; 850 851 // Free all l: variables if not referred. 852 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT) 853 vars_clear(&fc->l_vars.dv_hashtab); 854 else 855 free_fc = FALSE; 856 857 // If the a:000 list and the l: and a: dicts are not referenced and 858 // there is no closure using it, we can free the funccall_T and what's 859 // in it. 860 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT) 861 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE); 862 else 863 { 864 int todo; 865 hashitem_T *hi; 866 dictitem_T *di; 867 868 free_fc = FALSE; 869 870 // Make a copy of the a: variables, since we didn't do that above. 871 todo = (int)fc->l_avars.dv_hashtab.ht_used; 872 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi) 873 { 874 if (!HASHITEM_EMPTY(hi)) 875 { 876 --todo; 877 di = HI2DI(hi); 878 copy_tv(&di->di_tv, &di->di_tv); 879 } 880 } 881 } 882 883 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT) 884 fc->l_varlist.lv_first = NULL; 885 else 886 { 887 listitem_T *li; 888 889 free_fc = FALSE; 890 891 // Make a copy of the a:000 items, since we didn't do that above. 892 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li) 893 copy_tv(&li->li_tv, &li->li_tv); 894 } 895 896 if (free_fc) 897 free_funccal(fc); 898 else 899 { 900 static int made_copy = 0; 901 902 // "fc" is still in use. This can happen when returning "a:000", 903 // assigning "l:" to a global variable or defining a closure. 904 // Link "fc" in the list for garbage collection later. 905 fc->caller = previous_funccal; 906 previous_funccal = fc; 907 908 if (want_garbage_collect) 909 // If garbage collector is ready, clear count. 910 made_copy = 0; 911 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc))) 912 { 913 // We have made a lot of copies, worth 4 Mbyte. This can happen 914 // when repetitively calling a function that creates a reference to 915 // itself somehow. Call the garbage collector soon to avoid using 916 // too much memory. 917 made_copy = 0; 918 want_garbage_collect = TRUE; 919 } 920 } 921 } 922 /* 923 * Unreference "fc": decrement the reference count and free it when it 924 * becomes zero. "fp" is detached from "fc". 925 * When "force" is TRUE we are exiting. 926 */ 927 static void 928 funccal_unref(funccall_T *fc, ufunc_T *fp, int force) 929 { 930 funccall_T **pfc; 931 int i; 932 933 if (fc == NULL) 934 return; 935 936 if (--fc->fc_refcount <= 0 && (force || ( 937 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT 938 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT 939 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT))) 940 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller) 941 { 942 if (fc == *pfc) 943 { 944 *pfc = fc->caller; 945 free_funccal_contents(fc); 946 return; 947 } 948 } 949 for (i = 0; i < fc->fc_funcs.ga_len; ++i) 950 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp) 951 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL; 952 } 953 954 /* 955 * Remove the function from the function hashtable. If the function was 956 * deleted while it still has references this was already done. 957 * Return TRUE if the entry was deleted, FALSE if it wasn't found. 958 */ 959 static int 960 func_remove(ufunc_T *fp) 961 { 962 hashitem_T *hi; 963 964 // Return if it was already virtually deleted. 965 if (fp->uf_flags & FC_DEAD) 966 return FALSE; 967 968 hi = hash_find(&func_hashtab, UF2HIKEY(fp)); 969 if (!HASHITEM_EMPTY(hi)) 970 { 971 // When there is a def-function index do not actually remove the 972 // function, so we can find the index when defining the function again. 973 if (fp->uf_dfunc_idx >= 0) 974 fp->uf_flags |= FC_DEAD; 975 else 976 hash_remove(&func_hashtab, hi); 977 return TRUE; 978 } 979 return FALSE; 980 } 981 982 static void 983 func_clear_items(ufunc_T *fp) 984 { 985 ga_clear_strings(&(fp->uf_args)); 986 ga_clear_strings(&(fp->uf_def_args)); 987 ga_clear_strings(&(fp->uf_lines)); 988 VIM_CLEAR(fp->uf_name_exp); 989 VIM_CLEAR(fp->uf_arg_types); 990 VIM_CLEAR(fp->uf_def_arg_idx); 991 VIM_CLEAR(fp->uf_va_name); 992 while (fp->uf_type_list.ga_len > 0) 993 vim_free(((type_T **)fp->uf_type_list.ga_data) 994 [--fp->uf_type_list.ga_len]); 995 ga_clear(&fp->uf_type_list); 996 #ifdef FEAT_PROFILE 997 VIM_CLEAR(fp->uf_tml_count); 998 VIM_CLEAR(fp->uf_tml_total); 999 VIM_CLEAR(fp->uf_tml_self); 1000 #endif 1001 } 1002 1003 /* 1004 * Free all things that a function contains. Does not free the function 1005 * itself, use func_free() for that. 1006 * When "force" is TRUE we are exiting. 1007 */ 1008 static void 1009 func_clear(ufunc_T *fp, int force) 1010 { 1011 if (fp->uf_cleared) 1012 return; 1013 fp->uf_cleared = TRUE; 1014 1015 // clear this function 1016 func_clear_items(fp); 1017 funccal_unref(fp->uf_scoped, fp, force); 1018 delete_def_function(fp); 1019 } 1020 1021 /* 1022 * Free a function and remove it from the list of functions. Does not free 1023 * what a function contains, call func_clear() first. 1024 */ 1025 static void 1026 func_free(ufunc_T *fp) 1027 { 1028 // Only remove it when not done already, otherwise we would remove a newer 1029 // version of the function with the same name. 1030 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0) 1031 func_remove(fp); 1032 1033 if ((fp->uf_flags & FC_DEAD) == 0) 1034 vim_free(fp); 1035 } 1036 1037 /* 1038 * Free all things that a function contains and free the function itself. 1039 * When "force" is TRUE we are exiting. 1040 */ 1041 static void 1042 func_clear_free(ufunc_T *fp, int force) 1043 { 1044 func_clear(fp, force); 1045 func_free(fp); 1046 } 1047 1048 1049 /* 1050 * Call a user function. 1051 */ 1052 static void 1053 call_user_func( 1054 ufunc_T *fp, // pointer to function 1055 int argcount, // nr of args 1056 typval_T *argvars, // arguments 1057 typval_T *rettv, // return value 1058 linenr_T firstline, // first line of range 1059 linenr_T lastline, // last line of range 1060 dict_T *selfdict) // Dictionary for "self" 1061 { 1062 sctx_T save_current_sctx; 1063 int using_sandbox = FALSE; 1064 funccall_T *fc; 1065 int save_did_emsg; 1066 int default_arg_err = FALSE; 1067 static int depth = 0; 1068 dictitem_T *v; 1069 int fixvar_idx = 0; // index in fixvar[] 1070 int i; 1071 int ai; 1072 int islambda = FALSE; 1073 char_u numbuf[NUMBUFLEN]; 1074 char_u *name; 1075 #ifdef FEAT_PROFILE 1076 proftime_T wait_start; 1077 proftime_T call_start; 1078 int started_profiling = FALSE; 1079 #endif 1080 ESTACK_CHECK_DECLARATION 1081 1082 // If depth of calling is getting too high, don't execute the function 1083 if (depth >= p_mfd) 1084 { 1085 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'")); 1086 rettv->v_type = VAR_NUMBER; 1087 rettv->vval.v_number = -1; 1088 return; 1089 } 1090 ++depth; 1091 1092 line_breakcheck(); // check for CTRL-C hit 1093 1094 fc = ALLOC_CLEAR_ONE(funccall_T); 1095 if (fc == NULL) 1096 return; 1097 fc->caller = current_funccal; 1098 current_funccal = fc; 1099 fc->func = fp; 1100 fc->rettv = rettv; 1101 fc->level = ex_nesting_level; 1102 // Check if this function has a breakpoint. 1103 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0); 1104 fc->dbg_tick = debug_tick; 1105 // Set up fields for closure. 1106 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1); 1107 func_ptr_ref(fp); 1108 1109 if (fp->uf_dfunc_idx >= 0) 1110 { 1111 estack_push_ufunc(ETYPE_UFUNC, fp, 1); 1112 save_current_sctx = current_sctx; 1113 current_sctx = fp->uf_script_ctx; 1114 1115 // Execute the compiled function. 1116 call_def_function(fp, argcount, argvars, rettv); 1117 --depth; 1118 current_funccal = fc->caller; 1119 1120 estack_pop(); 1121 current_sctx = save_current_sctx; 1122 free_funccal(fc); 1123 return; 1124 } 1125 1126 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0) 1127 islambda = TRUE; 1128 1129 /* 1130 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables 1131 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free 1132 * each argument variable and saves a lot of time. 1133 */ 1134 /* 1135 * Init l: variables. 1136 */ 1137 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE); 1138 if (selfdict != NULL) 1139 { 1140 // Set l:self to "selfdict". Use "name" to avoid a warning from 1141 // some compiler that checks the destination size. 1142 v = &fc->fixvar[fixvar_idx++].var; 1143 name = v->di_key; 1144 STRCPY(name, "self"); 1145 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 1146 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v)); 1147 v->di_tv.v_type = VAR_DICT; 1148 v->di_tv.v_lock = 0; 1149 v->di_tv.vval.v_dict = selfdict; 1150 ++selfdict->dv_refcount; 1151 } 1152 1153 /* 1154 * Init a: variables, unless none found (in lambda). 1155 * Set a:0 to "argcount" less number of named arguments, if >= 0. 1156 * Set a:000 to a list with room for the "..." arguments. 1157 */ 1158 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE); 1159 if ((fp->uf_flags & FC_NOARGS) == 0) 1160 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0", 1161 (varnumber_T)(argcount >= fp->uf_args.ga_len 1162 ? argcount - fp->uf_args.ga_len : 0)); 1163 fc->l_avars.dv_lock = VAR_FIXED; 1164 if ((fp->uf_flags & FC_NOARGS) == 0) 1165 { 1166 // Use "name" to avoid a warning from some compiler that checks the 1167 // destination size. 1168 v = &fc->fixvar[fixvar_idx++].var; 1169 name = v->di_key; 1170 STRCPY(name, "000"); 1171 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 1172 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v)); 1173 v->di_tv.v_type = VAR_LIST; 1174 v->di_tv.v_lock = VAR_FIXED; 1175 v->di_tv.vval.v_list = &fc->l_varlist; 1176 } 1177 CLEAR_FIELD(fc->l_varlist); 1178 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT; 1179 fc->l_varlist.lv_lock = VAR_FIXED; 1180 1181 /* 1182 * Set a:firstline to "firstline" and a:lastline to "lastline". 1183 * Set a:name to named arguments. 1184 * Set a:N to the "..." arguments. 1185 * Skipped when no a: variables used (in lambda). 1186 */ 1187 if ((fp->uf_flags & FC_NOARGS) == 0) 1188 { 1189 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline", 1190 (varnumber_T)firstline); 1191 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline", 1192 (varnumber_T)lastline); 1193 } 1194 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i) 1195 { 1196 int addlocal = FALSE; 1197 typval_T def_rettv; 1198 int isdefault = FALSE; 1199 1200 ai = i - fp->uf_args.ga_len; 1201 if (ai < 0) 1202 { 1203 // named argument a:name 1204 name = FUNCARG(fp, i); 1205 if (islambda) 1206 addlocal = TRUE; 1207 1208 // evaluate named argument default expression 1209 isdefault = ai + fp->uf_def_args.ga_len >= 0 1210 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL 1211 && argvars[i].vval.v_number == VVAL_NONE)); 1212 if (isdefault) 1213 { 1214 char_u *default_expr = NULL; 1215 def_rettv.v_type = VAR_NUMBER; 1216 def_rettv.vval.v_number = -1; 1217 1218 default_expr = ((char_u **)(fp->uf_def_args.ga_data)) 1219 [ai + fp->uf_def_args.ga_len]; 1220 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL) 1221 { 1222 default_arg_err = 1; 1223 break; 1224 } 1225 } 1226 } 1227 else 1228 { 1229 if ((fp->uf_flags & FC_NOARGS) != 0) 1230 // Bail out if no a: arguments used (in lambda). 1231 break; 1232 1233 // "..." argument a:1, a:2, etc. 1234 sprintf((char *)numbuf, "%d", ai + 1); 1235 name = numbuf; 1236 } 1237 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN) 1238 { 1239 v = &fc->fixvar[fixvar_idx++].var; 1240 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 1241 STRCPY(v->di_key, name); 1242 } 1243 else 1244 { 1245 v = dictitem_alloc(name); 1246 if (v == NULL) 1247 break; 1248 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX; 1249 } 1250 1251 // Note: the values are copied directly to avoid alloc/free. 1252 // "argvars" must have VAR_FIXED for v_lock. 1253 v->di_tv = isdefault ? def_rettv : argvars[i]; 1254 v->di_tv.v_lock = VAR_FIXED; 1255 1256 if (addlocal) 1257 { 1258 // Named arguments should be accessed without the "a:" prefix in 1259 // lambda expressions. Add to the l: dict. 1260 copy_tv(&v->di_tv, &v->di_tv); 1261 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v)); 1262 } 1263 else 1264 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v)); 1265 1266 if (ai >= 0 && ai < MAX_FUNC_ARGS) 1267 { 1268 listitem_T *li = &fc->l_listitems[ai]; 1269 1270 li->li_tv = argvars[i]; 1271 li->li_tv.v_lock = VAR_FIXED; 1272 list_append(&fc->l_varlist, li); 1273 } 1274 } 1275 1276 // Don't redraw while executing the function. 1277 ++RedrawingDisabled; 1278 1279 if (fp->uf_flags & FC_SANDBOX) 1280 { 1281 using_sandbox = TRUE; 1282 ++sandbox; 1283 } 1284 1285 estack_push_ufunc(ETYPE_UFUNC, fp, 1); 1286 ESTACK_CHECK_SETUP 1287 if (p_verbose >= 12) 1288 { 1289 ++no_wait_return; 1290 verbose_enter_scroll(); 1291 1292 smsg(_("calling %s"), SOURCING_NAME); 1293 if (p_verbose >= 14) 1294 { 1295 char_u buf[MSG_BUF_LEN]; 1296 char_u numbuf2[NUMBUFLEN]; 1297 char_u *tofree; 1298 char_u *s; 1299 1300 msg_puts("("); 1301 for (i = 0; i < argcount; ++i) 1302 { 1303 if (i > 0) 1304 msg_puts(", "); 1305 if (argvars[i].v_type == VAR_NUMBER) 1306 msg_outnum((long)argvars[i].vval.v_number); 1307 else 1308 { 1309 // Do not want errors such as E724 here. 1310 ++emsg_off; 1311 s = tv2string(&argvars[i], &tofree, numbuf2, 0); 1312 --emsg_off; 1313 if (s != NULL) 1314 { 1315 if (vim_strsize(s) > MSG_BUF_CLEN) 1316 { 1317 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN); 1318 s = buf; 1319 } 1320 msg_puts((char *)s); 1321 vim_free(tofree); 1322 } 1323 } 1324 } 1325 msg_puts(")"); 1326 } 1327 msg_puts("\n"); // don't overwrite this either 1328 1329 verbose_leave_scroll(); 1330 --no_wait_return; 1331 } 1332 #ifdef FEAT_PROFILE 1333 if (do_profiling == PROF_YES) 1334 { 1335 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL)) 1336 { 1337 started_profiling = TRUE; 1338 func_do_profile(fp); 1339 } 1340 if (fp->uf_profiling 1341 || (fc->caller != NULL && fc->caller->func->uf_profiling)) 1342 { 1343 ++fp->uf_tm_count; 1344 profile_start(&call_start); 1345 profile_zero(&fp->uf_tm_children); 1346 } 1347 script_prof_save(&wait_start); 1348 } 1349 #endif 1350 1351 save_current_sctx = current_sctx; 1352 current_sctx = fp->uf_script_ctx; 1353 save_did_emsg = did_emsg; 1354 did_emsg = FALSE; 1355 1356 if (default_arg_err && (fp->uf_flags & FC_ABORT)) 1357 did_emsg = TRUE; 1358 else if (islambda) 1359 { 1360 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7; 1361 1362 // A Lambda always has the command "return {expr}". It is much faster 1363 // to evaluate {expr} directly. 1364 ++ex_nesting_level; 1365 (void)eval1(&p, rettv, TRUE); 1366 --ex_nesting_level; 1367 } 1368 else 1369 // call do_cmdline() to execute the lines 1370 do_cmdline(NULL, get_func_line, (void *)fc, 1371 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); 1372 1373 --RedrawingDisabled; 1374 1375 // when the function was aborted because of an error, return -1 1376 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN) 1377 { 1378 clear_tv(rettv); 1379 rettv->v_type = VAR_NUMBER; 1380 rettv->vval.v_number = -1; 1381 } 1382 1383 #ifdef FEAT_PROFILE 1384 if (do_profiling == PROF_YES && (fp->uf_profiling 1385 || (fc->caller != NULL && fc->caller->func->uf_profiling))) 1386 { 1387 profile_end(&call_start); 1388 profile_sub_wait(&wait_start, &call_start); 1389 profile_add(&fp->uf_tm_total, &call_start); 1390 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children); 1391 if (fc->caller != NULL && fc->caller->func->uf_profiling) 1392 { 1393 profile_add(&fc->caller->func->uf_tm_children, &call_start); 1394 profile_add(&fc->caller->func->uf_tml_children, &call_start); 1395 } 1396 if (started_profiling) 1397 // make a ":profdel func" stop profiling the function 1398 fp->uf_profiling = FALSE; 1399 } 1400 #endif 1401 1402 // when being verbose, mention the return value 1403 if (p_verbose >= 12) 1404 { 1405 ++no_wait_return; 1406 verbose_enter_scroll(); 1407 1408 if (aborting()) 1409 smsg(_("%s aborted"), SOURCING_NAME); 1410 else if (fc->rettv->v_type == VAR_NUMBER) 1411 smsg(_("%s returning #%ld"), SOURCING_NAME, 1412 (long)fc->rettv->vval.v_number); 1413 else 1414 { 1415 char_u buf[MSG_BUF_LEN]; 1416 char_u numbuf2[NUMBUFLEN]; 1417 char_u *tofree; 1418 char_u *s; 1419 1420 // The value may be very long. Skip the middle part, so that we 1421 // have some idea how it starts and ends. smsg() would always 1422 // truncate it at the end. Don't want errors such as E724 here. 1423 ++emsg_off; 1424 s = tv2string(fc->rettv, &tofree, numbuf2, 0); 1425 --emsg_off; 1426 if (s != NULL) 1427 { 1428 if (vim_strsize(s) > MSG_BUF_CLEN) 1429 { 1430 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN); 1431 s = buf; 1432 } 1433 smsg(_("%s returning %s"), SOURCING_NAME, s); 1434 vim_free(tofree); 1435 } 1436 } 1437 msg_puts("\n"); // don't overwrite this either 1438 1439 verbose_leave_scroll(); 1440 --no_wait_return; 1441 } 1442 1443 ESTACK_CHECK_NOW 1444 estack_pop(); 1445 current_sctx = save_current_sctx; 1446 #ifdef FEAT_PROFILE 1447 if (do_profiling == PROF_YES) 1448 script_prof_restore(&wait_start); 1449 #endif 1450 if (using_sandbox) 1451 --sandbox; 1452 1453 if (p_verbose >= 12 && SOURCING_NAME != NULL) 1454 { 1455 ++no_wait_return; 1456 verbose_enter_scroll(); 1457 1458 smsg(_("continuing in %s"), SOURCING_NAME); 1459 msg_puts("\n"); // don't overwrite this either 1460 1461 verbose_leave_scroll(); 1462 --no_wait_return; 1463 } 1464 1465 did_emsg |= save_did_emsg; 1466 --depth; 1467 1468 cleanup_function_call(fc); 1469 } 1470 1471 /* 1472 * Call a user function after checking the arguments. 1473 */ 1474 int 1475 call_user_func_check( 1476 ufunc_T *fp, 1477 int argcount, 1478 typval_T *argvars, 1479 typval_T *rettv, 1480 funcexe_T *funcexe, 1481 dict_T *selfdict) 1482 { 1483 int error; 1484 int regular_args = fp->uf_args.ga_len; 1485 1486 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL) 1487 *funcexe->doesrange = TRUE; 1488 if (argcount < regular_args - fp->uf_def_args.ga_len) 1489 error = FCERR_TOOFEW; 1490 else if (!has_varargs(fp) && argcount > regular_args) 1491 error = FCERR_TOOMANY; 1492 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL) 1493 error = FCERR_DICT; 1494 else 1495 { 1496 int did_save_redo = FALSE; 1497 save_redo_T save_redo; 1498 1499 /* 1500 * Call the user function. 1501 * Save and restore search patterns, script variables and 1502 * redo buffer. 1503 */ 1504 save_search_patterns(); 1505 if (!ins_compl_active()) 1506 { 1507 saveRedobuff(&save_redo); 1508 did_save_redo = TRUE; 1509 } 1510 ++fp->uf_calls; 1511 call_user_func(fp, argcount, argvars, rettv, 1512 funcexe->firstline, funcexe->lastline, 1513 (fp->uf_flags & FC_DICT) ? selfdict : NULL); 1514 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0) 1515 // Function was unreferenced while being used, free it now. 1516 func_clear_free(fp, FALSE); 1517 if (did_save_redo) 1518 restoreRedobuff(&save_redo); 1519 restore_search_patterns(); 1520 error = FCERR_NONE; 1521 } 1522 return error; 1523 } 1524 1525 /* 1526 * There are two kinds of function names: 1527 * 1. ordinary names, function defined with :function 1528 * 2. numbered functions and lambdas 1529 * For the first we only count the name stored in func_hashtab as a reference, 1530 * using function() does not count as a reference, because the function is 1531 * looked up by name. 1532 */ 1533 static int 1534 func_name_refcount(char_u *name) 1535 { 1536 return isdigit(*name) || *name == '<'; 1537 } 1538 1539 static funccal_entry_T *funccal_stack = NULL; 1540 1541 /* 1542 * Save the current function call pointer, and set it to NULL. 1543 * Used when executing autocommands and for ":source". 1544 */ 1545 void 1546 save_funccal(funccal_entry_T *entry) 1547 { 1548 entry->top_funccal = current_funccal; 1549 entry->next = funccal_stack; 1550 funccal_stack = entry; 1551 current_funccal = NULL; 1552 } 1553 1554 void 1555 restore_funccal(void) 1556 { 1557 if (funccal_stack == NULL) 1558 iemsg("INTERNAL: restore_funccal()"); 1559 else 1560 { 1561 current_funccal = funccal_stack->top_funccal; 1562 funccal_stack = funccal_stack->next; 1563 } 1564 } 1565 1566 funccall_T * 1567 get_current_funccal(void) 1568 { 1569 return current_funccal; 1570 } 1571 1572 #if defined(EXITFREE) || defined(PROTO) 1573 void 1574 free_all_functions(void) 1575 { 1576 hashitem_T *hi; 1577 ufunc_T *fp; 1578 long_u skipped = 0; 1579 long_u todo = 1; 1580 long_u used; 1581 1582 // Clean up the current_funccal chain and the funccal stack. 1583 while (current_funccal != NULL) 1584 { 1585 clear_tv(current_funccal->rettv); 1586 cleanup_function_call(current_funccal); 1587 if (current_funccal == NULL && funccal_stack != NULL) 1588 restore_funccal(); 1589 } 1590 1591 // First clear what the functions contain. Since this may lower the 1592 // reference count of a function, it may also free a function and change 1593 // the hash table. Restart if that happens. 1594 while (todo > 0) 1595 { 1596 todo = func_hashtab.ht_used; 1597 for (hi = func_hashtab.ht_array; todo > 0; ++hi) 1598 if (!HASHITEM_EMPTY(hi)) 1599 { 1600 // clear the def function index now 1601 fp = HI2UF(hi); 1602 fp->uf_flags &= ~FC_DEAD; 1603 fp->uf_dfunc_idx = -1; 1604 1605 // Only free functions that are not refcounted, those are 1606 // supposed to be freed when no longer referenced. 1607 if (func_name_refcount(fp->uf_name)) 1608 ++skipped; 1609 else 1610 { 1611 used = func_hashtab.ht_used; 1612 func_clear(fp, TRUE); 1613 if (used != func_hashtab.ht_used) 1614 { 1615 skipped = 0; 1616 break; 1617 } 1618 } 1619 --todo; 1620 } 1621 } 1622 1623 // Now actually free the functions. Need to start all over every time, 1624 // because func_free() may change the hash table. 1625 skipped = 0; 1626 while (func_hashtab.ht_used > skipped) 1627 { 1628 todo = func_hashtab.ht_used; 1629 for (hi = func_hashtab.ht_array; todo > 0; ++hi) 1630 if (!HASHITEM_EMPTY(hi)) 1631 { 1632 --todo; 1633 // Only free functions that are not refcounted, those are 1634 // supposed to be freed when no longer referenced. 1635 fp = HI2UF(hi); 1636 if (func_name_refcount(fp->uf_name)) 1637 ++skipped; 1638 else 1639 { 1640 func_free(fp); 1641 skipped = 0; 1642 break; 1643 } 1644 } 1645 } 1646 if (skipped == 0) 1647 hash_clear(&func_hashtab); 1648 1649 free_def_functions(); 1650 } 1651 #endif 1652 1653 /* 1654 * Return TRUE if "name" looks like a builtin function name: starts with a 1655 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'. 1656 * "len" is the length of "name", or -1 for NUL terminated. 1657 */ 1658 int 1659 builtin_function(char_u *name, int len) 1660 { 1661 char_u *p; 1662 1663 if (!ASCII_ISLOWER(name[0]) || name[1] == ':') 1664 return FALSE; 1665 p = vim_strchr(name, AUTOLOAD_CHAR); 1666 return p == NULL || (len > 0 && p > name + len); 1667 } 1668 1669 int 1670 func_call( 1671 char_u *name, 1672 typval_T *args, 1673 partial_T *partial, 1674 dict_T *selfdict, 1675 typval_T *rettv) 1676 { 1677 list_T *l = args->vval.v_list; 1678 listitem_T *item; 1679 typval_T argv[MAX_FUNC_ARGS + 1]; 1680 int argc = 0; 1681 int r = 0; 1682 1683 range_list_materialize(l); 1684 FOR_ALL_LIST_ITEMS(l, item) 1685 { 1686 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc)) 1687 { 1688 emsg(_("E699: Too many arguments")); 1689 break; 1690 } 1691 // Make a copy of each argument. This is needed to be able to set 1692 // v_lock to VAR_FIXED in the copy without changing the original list. 1693 copy_tv(&item->li_tv, &argv[argc++]); 1694 } 1695 1696 if (item == NULL) 1697 { 1698 funcexe_T funcexe; 1699 1700 CLEAR_FIELD(funcexe); 1701 funcexe.firstline = curwin->w_cursor.lnum; 1702 funcexe.lastline = curwin->w_cursor.lnum; 1703 funcexe.evaluate = TRUE; 1704 funcexe.partial = partial; 1705 funcexe.selfdict = selfdict; 1706 r = call_func(name, -1, rettv, argc, argv, &funcexe); 1707 } 1708 1709 // Free the arguments. 1710 while (argc > 0) 1711 clear_tv(&argv[--argc]); 1712 1713 return r; 1714 } 1715 1716 static int callback_depth = 0; 1717 1718 int 1719 get_callback_depth(void) 1720 { 1721 return callback_depth; 1722 } 1723 1724 /* 1725 * Invoke call_func() with a callback. 1726 */ 1727 int 1728 call_callback( 1729 callback_T *callback, 1730 int len, // length of "name" or -1 to use strlen() 1731 typval_T *rettv, // return value goes here 1732 int argcount, // number of "argvars" 1733 typval_T *argvars) // vars for arguments, must have "argcount" 1734 // PLUS ONE elements! 1735 { 1736 funcexe_T funcexe; 1737 int ret; 1738 1739 CLEAR_FIELD(funcexe); 1740 funcexe.evaluate = TRUE; 1741 funcexe.partial = callback->cb_partial; 1742 ++callback_depth; 1743 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe); 1744 --callback_depth; 1745 return ret; 1746 } 1747 1748 /* 1749 * Give an error message for the result of a function. 1750 * Nothing if "error" is FCERR_NONE. 1751 */ 1752 void 1753 user_func_error(int error, char_u *name) 1754 { 1755 switch (error) 1756 { 1757 case FCERR_UNKNOWN: 1758 emsg_funcname(e_unknownfunc, name); 1759 break; 1760 case FCERR_NOTMETHOD: 1761 emsg_funcname( 1762 N_("E276: Cannot use function as a method: %s"), name); 1763 break; 1764 case FCERR_DELETED: 1765 emsg_funcname(N_(e_func_deleted), name); 1766 break; 1767 case FCERR_TOOMANY: 1768 emsg_funcname((char *)e_toomanyarg, name); 1769 break; 1770 case FCERR_TOOFEW: 1771 emsg_funcname((char *)e_toofewarg, name); 1772 break; 1773 case FCERR_SCRIPT: 1774 emsg_funcname( 1775 N_("E120: Using <SID> not in a script context: %s"), name); 1776 break; 1777 case FCERR_DICT: 1778 emsg_funcname( 1779 N_("E725: Calling dict function without Dictionary: %s"), 1780 name); 1781 break; 1782 } 1783 } 1784 1785 /* 1786 * Call a function with its resolved parameters 1787 * 1788 * Return FAIL when the function can't be called, OK otherwise. 1789 * Also returns OK when an error was encountered while executing the function. 1790 */ 1791 int 1792 call_func( 1793 char_u *funcname, // name of the function 1794 int len, // length of "name" or -1 to use strlen() 1795 typval_T *rettv, // return value goes here 1796 int argcount_in, // number of "argvars" 1797 typval_T *argvars_in, // vars for arguments, must have "argcount" 1798 // PLUS ONE elements! 1799 funcexe_T *funcexe) // more arguments 1800 { 1801 int ret = FAIL; 1802 int error = FCERR_NONE; 1803 int i; 1804 ufunc_T *fp = NULL; 1805 char_u fname_buf[FLEN_FIXED + 1]; 1806 char_u *tofree = NULL; 1807 char_u *fname = NULL; 1808 char_u *name = NULL; 1809 int argcount = argcount_in; 1810 typval_T *argvars = argvars_in; 1811 dict_T *selfdict = funcexe->selfdict; 1812 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or 1813 // "funcexe->basetv" is not NULL 1814 int argv_clear = 0; 1815 int argv_base = 0; 1816 partial_T *partial = funcexe->partial; 1817 1818 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv) 1819 // even when call_func() returns FAIL. 1820 rettv->v_type = VAR_UNKNOWN; 1821 1822 if (partial != NULL) 1823 fp = partial->pt_func; 1824 if (fp == NULL) 1825 { 1826 // Make a copy of the name, if it comes from a funcref variable it 1827 // could be changed or deleted in the called function. 1828 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname); 1829 if (name == NULL) 1830 return ret; 1831 1832 fname = fname_trans_sid(name, fname_buf, &tofree, &error); 1833 } 1834 1835 if (funcexe->doesrange != NULL) 1836 *funcexe->doesrange = FALSE; 1837 1838 if (partial != NULL) 1839 { 1840 // When the function has a partial with a dict and there is a dict 1841 // argument, use the dict argument. That is backwards compatible. 1842 // When the dict was bound explicitly use the one from the partial. 1843 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto)) 1844 selfdict = partial->pt_dict; 1845 if (error == FCERR_NONE && partial->pt_argc > 0) 1846 { 1847 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear) 1848 { 1849 if (argv_clear + argcount_in >= MAX_FUNC_ARGS) 1850 { 1851 error = FCERR_TOOMANY; 1852 goto theend; 1853 } 1854 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]); 1855 } 1856 for (i = 0; i < argcount_in; ++i) 1857 argv[i + argv_clear] = argvars_in[i]; 1858 argvars = argv; 1859 argcount = partial->pt_argc + argcount_in; 1860 } 1861 } 1862 1863 if (error == FCERR_NONE && funcexe->evaluate) 1864 { 1865 char_u *rfname = fname; 1866 1867 // Ignore "g:" before a function name. 1868 if (fp == NULL && fname[0] == 'g' && fname[1] == ':') 1869 rfname = fname + 2; 1870 1871 rettv->v_type = VAR_NUMBER; // default rettv is number zero 1872 rettv->vval.v_number = 0; 1873 error = FCERR_UNKNOWN; 1874 1875 if (fp != NULL || !builtin_function(rfname, -1)) 1876 { 1877 /* 1878 * User defined function. 1879 */ 1880 if (fp == NULL) 1881 fp = find_func(rfname, NULL); 1882 1883 // Trigger FuncUndefined event, may load the function. 1884 if (fp == NULL 1885 && apply_autocmds(EVENT_FUNCUNDEFINED, 1886 rfname, rfname, TRUE, NULL) 1887 && !aborting()) 1888 { 1889 // executed an autocommand, search for the function again 1890 fp = find_func(rfname, NULL); 1891 } 1892 // Try loading a package. 1893 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting()) 1894 { 1895 // loaded a package, search for the function again 1896 fp = find_func(rfname, NULL); 1897 } 1898 if (fp == NULL) 1899 { 1900 char_u *p = untrans_function_name(rfname); 1901 1902 // If using Vim9 script try not local to the script. 1903 // TODO: should not do this if the name started with "s:". 1904 if (p != NULL) 1905 fp = find_func(p, NULL); 1906 } 1907 1908 if (fp != NULL && (fp->uf_flags & FC_DELETED)) 1909 error = FCERR_DELETED; 1910 else if (fp != NULL) 1911 { 1912 if (funcexe->argv_func != NULL) 1913 // postponed filling in the arguments, do it now 1914 argcount = funcexe->argv_func(argcount, argvars, argv_clear, 1915 fp->uf_args.ga_len); 1916 1917 if (funcexe->basetv != NULL) 1918 { 1919 // Method call: base->Method() 1920 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount); 1921 argv[0] = *funcexe->basetv; 1922 argcount++; 1923 argvars = argv; 1924 argv_base = 1; 1925 } 1926 1927 error = call_user_func_check(fp, argcount, argvars, rettv, 1928 funcexe, selfdict); 1929 } 1930 } 1931 else if (funcexe->basetv != NULL) 1932 { 1933 /* 1934 * expr->method(): Find the method name in the table, call its 1935 * implementation with the base as one of the arguments. 1936 */ 1937 error = call_internal_method(fname, argcount, argvars, rettv, 1938 funcexe->basetv); 1939 } 1940 else 1941 { 1942 /* 1943 * Find the function name in the table, call its implementation. 1944 */ 1945 error = call_internal_func(fname, argcount, argvars, rettv); 1946 } 1947 /* 1948 * The function call (or "FuncUndefined" autocommand sequence) might 1949 * have been aborted by an error, an interrupt, or an explicitly thrown 1950 * exception that has not been caught so far. This situation can be 1951 * tested for by calling aborting(). For an error in an internal 1952 * function or for the "E132" error in call_user_func(), however, the 1953 * throw point at which the "force_abort" flag (temporarily reset by 1954 * emsg()) is normally updated has not been reached yet. We need to 1955 * update that flag first to make aborting() reliable. 1956 */ 1957 update_force_abort(); 1958 } 1959 if (error == FCERR_NONE) 1960 ret = OK; 1961 1962 theend: 1963 /* 1964 * Report an error unless the argument evaluation or function call has been 1965 * cancelled due to an aborting error, an interrupt, or an exception. 1966 */ 1967 if (!aborting()) 1968 { 1969 user_func_error(error, (name != NULL) ? name : funcname); 1970 } 1971 1972 // clear the copies made from the partial 1973 while (argv_clear > 0) 1974 clear_tv(&argv[--argv_clear + argv_base]); 1975 1976 vim_free(tofree); 1977 vim_free(name); 1978 1979 return ret; 1980 } 1981 1982 static char_u * 1983 printable_func_name(ufunc_T *fp) 1984 { 1985 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name; 1986 } 1987 1988 /* 1989 * List the head of the function: "function name(arg1, arg2)". 1990 */ 1991 static void 1992 list_func_head(ufunc_T *fp, int indent) 1993 { 1994 int j; 1995 1996 msg_start(); 1997 if (indent) 1998 msg_puts(" "); 1999 if (fp->uf_dfunc_idx >= 0) 2000 msg_puts("def "); 2001 else 2002 msg_puts("function "); 2003 msg_puts((char *)printable_func_name(fp)); 2004 msg_putchar('('); 2005 for (j = 0; j < fp->uf_args.ga_len; ++j) 2006 { 2007 if (j) 2008 msg_puts(", "); 2009 msg_puts((char *)FUNCARG(fp, j)); 2010 if (fp->uf_arg_types != NULL) 2011 { 2012 char *tofree; 2013 2014 msg_puts(": "); 2015 msg_puts(type_name(fp->uf_arg_types[j], &tofree)); 2016 vim_free(tofree); 2017 } 2018 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len) 2019 { 2020 msg_puts(" = "); 2021 msg_puts(((char **)(fp->uf_def_args.ga_data)) 2022 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]); 2023 } 2024 } 2025 if (fp->uf_varargs) 2026 { 2027 if (j) 2028 msg_puts(", "); 2029 msg_puts("..."); 2030 } 2031 if (fp->uf_va_name != NULL) 2032 { 2033 if (j) 2034 msg_puts(", "); 2035 msg_puts("..."); 2036 msg_puts((char *)fp->uf_va_name); 2037 if (fp->uf_va_type) 2038 { 2039 char *tofree; 2040 2041 msg_puts(": "); 2042 msg_puts(type_name(fp->uf_va_type, &tofree)); 2043 vim_free(tofree); 2044 } 2045 } 2046 msg_putchar(')'); 2047 2048 if (fp->uf_dfunc_idx >= 0) 2049 { 2050 if (fp->uf_ret_type != &t_void) 2051 { 2052 char *tofree; 2053 2054 msg_puts(": "); 2055 msg_puts(type_name(fp->uf_ret_type, &tofree)); 2056 vim_free(tofree); 2057 } 2058 } 2059 else if (fp->uf_flags & FC_ABORT) 2060 msg_puts(" abort"); 2061 if (fp->uf_flags & FC_RANGE) 2062 msg_puts(" range"); 2063 if (fp->uf_flags & FC_DICT) 2064 msg_puts(" dict"); 2065 if (fp->uf_flags & FC_CLOSURE) 2066 msg_puts(" closure"); 2067 msg_clr_eos(); 2068 if (p_verbose > 0) 2069 last_set_msg(fp->uf_script_ctx); 2070 } 2071 2072 /* 2073 * Get a function name, translating "<SID>" and "<SNR>". 2074 * Also handles a Funcref in a List or Dictionary. 2075 * Returns the function name in allocated memory, or NULL for failure. 2076 * flags: 2077 * TFN_INT: internal function name OK 2078 * TFN_QUIET: be quiet 2079 * TFN_NO_AUTOLOAD: do not use script autoloading 2080 * TFN_NO_DEREF: do not dereference a Funcref 2081 * Advances "pp" to just after the function name (if no error). 2082 */ 2083 char_u * 2084 trans_function_name( 2085 char_u **pp, 2086 int skip, // only find the end, don't evaluate 2087 int flags, 2088 funcdict_T *fdp, // return: info about dictionary used 2089 partial_T **partial) // return: partial of a FuncRef 2090 { 2091 char_u *name = NULL; 2092 char_u *start; 2093 char_u *end; 2094 int lead; 2095 char_u sid_buf[20]; 2096 int len; 2097 int extra = 0; 2098 lval_T lv; 2099 int vim9script; 2100 2101 if (fdp != NULL) 2102 CLEAR_POINTER(fdp); 2103 start = *pp; 2104 2105 // Check for hard coded <SNR>: already translated function ID (from a user 2106 // command). 2107 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA 2108 && (*pp)[2] == (int)KE_SNR) 2109 { 2110 *pp += 3; 2111 len = get_id_len(pp) + 3; 2112 return vim_strnsave(start, len); 2113 } 2114 2115 // A name starting with "<SID>" or "<SNR>" is local to a script. But 2116 // don't skip over "s:", get_lval() needs it for "s:dict.func". 2117 lead = eval_fname_script(start); 2118 if (lead > 2) 2119 start += lead; 2120 2121 // Note that TFN_ flags use the same values as GLV_ flags. 2122 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY, 2123 lead > 2 ? 0 : FNE_CHECK_START); 2124 if (end == start) 2125 { 2126 if (!skip) 2127 emsg(_("E129: Function name required")); 2128 goto theend; 2129 } 2130 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range))) 2131 { 2132 /* 2133 * Report an invalid expression in braces, unless the expression 2134 * evaluation has been cancelled due to an aborting error, an 2135 * interrupt, or an exception. 2136 */ 2137 if (!aborting()) 2138 { 2139 if (end != NULL) 2140 semsg(_(e_invarg2), start); 2141 } 2142 else 2143 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR); 2144 goto theend; 2145 } 2146 2147 if (lv.ll_tv != NULL) 2148 { 2149 if (fdp != NULL) 2150 { 2151 fdp->fd_dict = lv.ll_dict; 2152 fdp->fd_newkey = lv.ll_newkey; 2153 lv.ll_newkey = NULL; 2154 fdp->fd_di = lv.ll_di; 2155 } 2156 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL) 2157 { 2158 name = vim_strsave(lv.ll_tv->vval.v_string); 2159 *pp = end; 2160 } 2161 else if (lv.ll_tv->v_type == VAR_PARTIAL 2162 && lv.ll_tv->vval.v_partial != NULL) 2163 { 2164 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial)); 2165 *pp = end; 2166 if (partial != NULL) 2167 *partial = lv.ll_tv->vval.v_partial; 2168 } 2169 else 2170 { 2171 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL 2172 || lv.ll_dict == NULL || fdp->fd_newkey == NULL)) 2173 emsg(_(e_funcref)); 2174 else 2175 *pp = end; 2176 name = NULL; 2177 } 2178 goto theend; 2179 } 2180 2181 if (lv.ll_name == NULL) 2182 { 2183 // Error found, but continue after the function name. 2184 *pp = end; 2185 goto theend; 2186 } 2187 2188 // Check if the name is a Funcref. If so, use the value. 2189 if (lv.ll_exp_name != NULL) 2190 { 2191 len = (int)STRLEN(lv.ll_exp_name); 2192 name = deref_func_name(lv.ll_exp_name, &len, partial, 2193 flags & TFN_NO_AUTOLOAD); 2194 if (name == lv.ll_exp_name) 2195 name = NULL; 2196 } 2197 else if (!(flags & TFN_NO_DEREF)) 2198 { 2199 len = (int)(end - *pp); 2200 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD); 2201 if (name == *pp) 2202 name = NULL; 2203 } 2204 if (name != NULL) 2205 { 2206 name = vim_strsave(name); 2207 *pp = end; 2208 if (STRNCMP(name, "<SNR>", 5) == 0) 2209 { 2210 // Change "<SNR>" to the byte sequence. 2211 name[0] = K_SPECIAL; 2212 name[1] = KS_EXTRA; 2213 name[2] = (int)KE_SNR; 2214 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1); 2215 } 2216 goto theend; 2217 } 2218 2219 if (lv.ll_exp_name != NULL) 2220 { 2221 len = (int)STRLEN(lv.ll_exp_name); 2222 if (lead <= 2 && lv.ll_name == lv.ll_exp_name 2223 && STRNCMP(lv.ll_name, "s:", 2) == 0) 2224 { 2225 // When there was "s:" already or the name expanded to get a 2226 // leading "s:" then remove it. 2227 lv.ll_name += 2; 2228 len -= 2; 2229 lead = 2; 2230 } 2231 } 2232 else 2233 { 2234 // skip over "s:" and "g:" 2235 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':')) 2236 lv.ll_name += 2; 2237 len = (int)(end - lv.ll_name); 2238 } 2239 2240 // In Vim9 script a user function is script-local by default. 2241 vim9script = ASCII_ISUPPER(*start) 2242 && current_sctx.sc_version == SCRIPT_VERSION_VIM9; 2243 2244 /* 2245 * Copy the function name to allocated memory. 2246 * Accept <SID>name() inside a script, translate into <SNR>123_name(). 2247 * Accept <SNR>123_name() outside a script. 2248 */ 2249 if (skip) 2250 lead = 0; // do nothing 2251 else if (lead > 0 || vim9script) 2252 { 2253 if (!vim9script) 2254 lead = 3; 2255 if (vim9script || (lv.ll_exp_name != NULL 2256 && eval_fname_sid(lv.ll_exp_name)) 2257 || eval_fname_sid(*pp)) 2258 { 2259 // It's script-local, "s:" or "<SID>" 2260 if (current_sctx.sc_sid <= 0) 2261 { 2262 emsg(_(e_usingsid)); 2263 goto theend; 2264 } 2265 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid); 2266 if (vim9script) 2267 extra = 3 + (int)STRLEN(sid_buf); 2268 else 2269 lead += (int)STRLEN(sid_buf); 2270 } 2271 } 2272 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len)) 2273 { 2274 semsg(_("E128: Function name must start with a capital or \"s:\": %s"), 2275 start); 2276 goto theend; 2277 } 2278 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF)) 2279 { 2280 char_u *cp = vim_strchr(lv.ll_name, ':'); 2281 2282 if (cp != NULL && cp < end) 2283 { 2284 semsg(_("E884: Function name cannot contain a colon: %s"), start); 2285 goto theend; 2286 } 2287 } 2288 2289 name = alloc(len + lead + extra + 1); 2290 if (name != NULL) 2291 { 2292 if (!skip && (lead > 0 || vim9script)) 2293 { 2294 name[0] = K_SPECIAL; 2295 name[1] = KS_EXTRA; 2296 name[2] = (int)KE_SNR; 2297 if (vim9script || lead > 3) // If it's "<SID>" 2298 STRCPY(name + 3, sid_buf); 2299 } 2300 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len); 2301 name[lead + extra + len] = NUL; 2302 } 2303 *pp = end; 2304 2305 theend: 2306 clear_lval(&lv); 2307 return name; 2308 } 2309 2310 /* 2311 * Assuming "name" is the result of trans_function_name() and it was prefixed 2312 * to use the script-local name, return the unmodified name (points into 2313 * "name"). Otherwise return NULL. 2314 * This can be used to first search for a script-local function and fall back 2315 * to the global function if not found. 2316 */ 2317 char_u * 2318 untrans_function_name(char_u *name) 2319 { 2320 char_u *p; 2321 2322 if (*name == K_SPECIAL && current_sctx.sc_version == SCRIPT_VERSION_VIM9) 2323 { 2324 p = vim_strchr(name, '_'); 2325 if (p != NULL) 2326 return p + 1; 2327 } 2328 return NULL; 2329 } 2330 2331 /* 2332 * ":function" 2333 */ 2334 void 2335 ex_function(exarg_T *eap) 2336 { 2337 char_u *theline; 2338 char_u *line_to_free = NULL; 2339 int j; 2340 int c; 2341 int saved_did_emsg; 2342 int saved_wait_return = need_wait_return; 2343 char_u *name = NULL; 2344 char_u *p; 2345 char_u *arg; 2346 char_u *line_arg = NULL; 2347 garray_T newargs; 2348 garray_T argtypes; 2349 garray_T default_args; 2350 garray_T newlines; 2351 int varargs = FALSE; 2352 int flags = 0; 2353 char_u *ret_type = NULL; 2354 ufunc_T *fp; 2355 int overwrite = FALSE; 2356 int indent; 2357 int nesting; 2358 #define MAX_FUNC_NESTING 50 2359 char nesting_def[MAX_FUNC_NESTING]; 2360 dictitem_T *v; 2361 funcdict_T fudi; 2362 static int func_nr = 0; // number for nameless function 2363 int paren; 2364 int todo; 2365 hashitem_T *hi; 2366 int do_concat = TRUE; 2367 linenr_T sourcing_lnum_off; 2368 linenr_T sourcing_lnum_top; 2369 int is_heredoc = FALSE; 2370 char_u *skip_until = NULL; 2371 char_u *heredoc_trimmed = NULL; 2372 2373 /* 2374 * ":function" without argument: list functions. 2375 */ 2376 if (ends_excmd(*eap->arg)) 2377 { 2378 if (!eap->skip) 2379 { 2380 todo = (int)func_hashtab.ht_used; 2381 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi) 2382 { 2383 if (!HASHITEM_EMPTY(hi)) 2384 { 2385 --todo; 2386 fp = HI2UF(hi); 2387 if ((fp->uf_flags & FC_DEAD) 2388 || message_filtered(fp->uf_name)) 2389 continue; 2390 if (!func_name_refcount(fp->uf_name)) 2391 list_func_head(fp, FALSE); 2392 } 2393 } 2394 } 2395 eap->nextcmd = check_nextcmd(eap->arg); 2396 return; 2397 } 2398 2399 /* 2400 * ":function /pat": list functions matching pattern. 2401 */ 2402 if (*eap->arg == '/') 2403 { 2404 p = skip_regexp(eap->arg + 1, '/', TRUE); 2405 if (!eap->skip) 2406 { 2407 regmatch_T regmatch; 2408 2409 c = *p; 2410 *p = NUL; 2411 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC); 2412 *p = c; 2413 if (regmatch.regprog != NULL) 2414 { 2415 regmatch.rm_ic = p_ic; 2416 2417 todo = (int)func_hashtab.ht_used; 2418 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi) 2419 { 2420 if (!HASHITEM_EMPTY(hi)) 2421 { 2422 --todo; 2423 fp = HI2UF(hi); 2424 if ((fp->uf_flags & FC_DEAD) == 0 2425 && !isdigit(*fp->uf_name) 2426 && vim_regexec(®match, fp->uf_name, 0)) 2427 list_func_head(fp, FALSE); 2428 } 2429 } 2430 vim_regfree(regmatch.regprog); 2431 } 2432 } 2433 if (*p == '/') 2434 ++p; 2435 eap->nextcmd = check_nextcmd(p); 2436 return; 2437 } 2438 2439 ga_init(&newargs); 2440 ga_init(&argtypes); 2441 ga_init(&default_args); 2442 2443 /* 2444 * Get the function name. There are these situations: 2445 * func normal function name 2446 * "name" == func, "fudi.fd_dict" == NULL 2447 * dict.func new dictionary entry 2448 * "name" == NULL, "fudi.fd_dict" set, 2449 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func 2450 * dict.func existing dict entry with a Funcref 2451 * "name" == func, "fudi.fd_dict" set, 2452 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL 2453 * dict.func existing dict entry that's not a Funcref 2454 * "name" == NULL, "fudi.fd_dict" set, 2455 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL 2456 * s:func script-local function name 2457 * g:func global function name, same as "func" 2458 */ 2459 p = eap->arg; 2460 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL); 2461 paren = (vim_strchr(p, '(') != NULL); 2462 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip) 2463 { 2464 /* 2465 * Return on an invalid expression in braces, unless the expression 2466 * evaluation has been cancelled due to an aborting error, an 2467 * interrupt, or an exception. 2468 */ 2469 if (!aborting()) 2470 { 2471 if (!eap->skip && fudi.fd_newkey != NULL) 2472 semsg(_(e_dictkey), fudi.fd_newkey); 2473 vim_free(fudi.fd_newkey); 2474 return; 2475 } 2476 else 2477 eap->skip = TRUE; 2478 } 2479 2480 // An error in a function call during evaluation of an expression in magic 2481 // braces should not cause the function not to be defined. 2482 saved_did_emsg = did_emsg; 2483 did_emsg = FALSE; 2484 2485 /* 2486 * ":function func" with only function name: list function. 2487 */ 2488 if (!paren) 2489 { 2490 if (!ends_excmd(*skipwhite(p))) 2491 { 2492 emsg(_(e_trailing)); 2493 goto ret_free; 2494 } 2495 eap->nextcmd = check_nextcmd(p); 2496 if (eap->nextcmd != NULL) 2497 *p = NUL; 2498 if (!eap->skip && !got_int) 2499 { 2500 fp = find_func(name, NULL); 2501 if (fp == NULL && ASCII_ISUPPER(*eap->arg)) 2502 { 2503 char_u *up = untrans_function_name(name); 2504 2505 // With Vim9 script the name was made script-local, if not 2506 // found try again with the original name. 2507 if (up != NULL) 2508 fp = find_func(up, NULL); 2509 } 2510 2511 if (fp != NULL) 2512 { 2513 list_func_head(fp, TRUE); 2514 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j) 2515 { 2516 if (FUNCLINE(fp, j) == NULL) 2517 continue; 2518 msg_putchar('\n'); 2519 msg_outnum((long)(j + 1)); 2520 if (j < 9) 2521 msg_putchar(' '); 2522 if (j < 99) 2523 msg_putchar(' '); 2524 msg_prt_line(FUNCLINE(fp, j), FALSE); 2525 out_flush(); // show a line at a time 2526 ui_breakcheck(); 2527 } 2528 if (!got_int) 2529 { 2530 msg_putchar('\n'); 2531 if (fp->uf_dfunc_idx >= 0) 2532 msg_puts(" enddef"); 2533 else 2534 msg_puts(" endfunction"); 2535 } 2536 } 2537 else 2538 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg); 2539 } 2540 goto ret_free; 2541 } 2542 2543 /* 2544 * ":function name(arg1, arg2)" Define function. 2545 */ 2546 p = skipwhite(p); 2547 if (*p != '(') 2548 { 2549 if (!eap->skip) 2550 { 2551 semsg(_("E124: Missing '(': %s"), eap->arg); 2552 goto ret_free; 2553 } 2554 // attempt to continue by skipping some text 2555 if (vim_strchr(p, '(') != NULL) 2556 p = vim_strchr(p, '('); 2557 } 2558 p = skipwhite(p + 1); 2559 2560 ga_init2(&newlines, (int)sizeof(char_u *), 3); 2561 2562 if (!eap->skip) 2563 { 2564 // Check the name of the function. Unless it's a dictionary function 2565 // (that we are overwriting). 2566 if (name != NULL) 2567 arg = name; 2568 else 2569 arg = fudi.fd_newkey; 2570 if (arg != NULL && (fudi.fd_di == NULL 2571 || (fudi.fd_di->di_tv.v_type != VAR_FUNC 2572 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL))) 2573 { 2574 if (*arg == K_SPECIAL) 2575 j = 3; 2576 else 2577 j = 0; 2578 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j]) 2579 : eval_isnamec(arg[j]))) 2580 ++j; 2581 if (arg[j] != NUL) 2582 emsg_funcname((char *)e_invarg2, arg); 2583 } 2584 // Disallow using the g: dict. 2585 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE) 2586 emsg(_("E862: Cannot use g: here")); 2587 } 2588 2589 // This may get more lines and make the pointers into the first line 2590 // invalid. 2591 if (get_function_args(&p, ')', &newargs, 2592 eap->cmdidx == CMD_def ? &argtypes : NULL, 2593 &varargs, &default_args, eap->skip, 2594 eap, &line_to_free) == FAIL) 2595 goto errret_2; 2596 2597 if (eap->cmdidx == CMD_def) 2598 { 2599 // find the return type: :def Func(): type 2600 if (*p == ':') 2601 { 2602 ret_type = skipwhite(p + 1); 2603 p = skip_type(ret_type); 2604 if (p > ret_type) 2605 { 2606 ret_type = vim_strnsave(ret_type, (int)(p - ret_type)); 2607 p = skipwhite(p); 2608 } 2609 else 2610 { 2611 ret_type = NULL; 2612 semsg(_("E1056: expected a type: %s"), ret_type); 2613 } 2614 } 2615 } 2616 else 2617 // find extra arguments "range", "dict", "abort" and "closure" 2618 for (;;) 2619 { 2620 p = skipwhite(p); 2621 if (STRNCMP(p, "range", 5) == 0) 2622 { 2623 flags |= FC_RANGE; 2624 p += 5; 2625 } 2626 else if (STRNCMP(p, "dict", 4) == 0) 2627 { 2628 flags |= FC_DICT; 2629 p += 4; 2630 } 2631 else if (STRNCMP(p, "abort", 5) == 0) 2632 { 2633 flags |= FC_ABORT; 2634 p += 5; 2635 } 2636 else if (STRNCMP(p, "closure", 7) == 0) 2637 { 2638 flags |= FC_CLOSURE; 2639 p += 7; 2640 if (current_funccal == NULL) 2641 { 2642 emsg_funcname(N_("E932: Closure function should not be at top level: %s"), 2643 name == NULL ? (char_u *)"" : name); 2644 goto erret; 2645 } 2646 } 2647 else 2648 break; 2649 } 2650 2651 // When there is a line break use what follows for the function body. 2652 // Makes 'exe "func Test()\n...\nendfunc"' work. 2653 if (*p == '\n') 2654 line_arg = p + 1; 2655 else if (*p != NUL && *p != '"' && !(eap->cmdidx == CMD_def && *p == '#') 2656 && !eap->skip && !did_emsg) 2657 emsg(_(e_trailing)); 2658 2659 /* 2660 * Read the body of the function, until "}", ":endfunction" or ":enddef" is 2661 * found. 2662 */ 2663 if (KeyTyped) 2664 { 2665 // Check if the function already exists, don't let the user type the 2666 // whole function before telling him it doesn't work! For a script we 2667 // need to skip the body to be able to find what follows. 2668 if (!eap->skip && !eap->forceit) 2669 { 2670 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL) 2671 emsg(_(e_funcdict)); 2672 else if (name != NULL && find_func(name, NULL) != NULL) 2673 emsg_funcname(e_funcexts, name); 2674 } 2675 2676 if (!eap->skip && did_emsg) 2677 goto erret; 2678 2679 msg_putchar('\n'); // don't overwrite the function name 2680 cmdline_row = msg_row; 2681 } 2682 2683 // Save the starting line number. 2684 sourcing_lnum_top = SOURCING_LNUM; 2685 2686 indent = 2; 2687 nesting = 0; 2688 nesting_def[nesting] = (eap->cmdidx == CMD_def); 2689 for (;;) 2690 { 2691 if (KeyTyped) 2692 { 2693 msg_scroll = TRUE; 2694 saved_wait_return = FALSE; 2695 } 2696 need_wait_return = FALSE; 2697 2698 if (line_arg != NULL) 2699 { 2700 // Use eap->arg, split up in parts by line breaks. 2701 theline = line_arg; 2702 p = vim_strchr(theline, '\n'); 2703 if (p == NULL) 2704 line_arg += STRLEN(line_arg); 2705 else 2706 { 2707 *p = NUL; 2708 line_arg = p + 1; 2709 } 2710 } 2711 else 2712 { 2713 vim_free(line_to_free); 2714 if (eap->getline == NULL) 2715 theline = getcmdline(':', 0L, indent, do_concat); 2716 else 2717 theline = eap->getline(':', eap->cookie, indent, do_concat); 2718 line_to_free = theline; 2719 } 2720 if (KeyTyped) 2721 lines_left = Rows - 1; 2722 if (theline == NULL) 2723 { 2724 if (eap->cmdidx == CMD_def) 2725 emsg(_("E1057: Missing :enddef")); 2726 else 2727 emsg(_("E126: Missing :endfunction")); 2728 goto erret; 2729 } 2730 2731 // Detect line continuation: SOURCING_LNUM increased more than one. 2732 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie); 2733 if (SOURCING_LNUM < sourcing_lnum_off) 2734 sourcing_lnum_off -= SOURCING_LNUM; 2735 else 2736 sourcing_lnum_off = 0; 2737 2738 if (skip_until != NULL) 2739 { 2740 // Don't check for ":endfunc"/":enddef" between 2741 // * ":append" and "." 2742 // * ":python <<EOF" and "EOF" 2743 // * ":let {var-name} =<< [trim] {marker}" and "{marker}" 2744 if (heredoc_trimmed == NULL 2745 || (is_heredoc && skipwhite(theline) == theline) 2746 || STRNCMP(theline, heredoc_trimmed, 2747 STRLEN(heredoc_trimmed)) == 0) 2748 { 2749 if (heredoc_trimmed == NULL) 2750 p = theline; 2751 else if (is_heredoc) 2752 p = skipwhite(theline) == theline 2753 ? theline : theline + STRLEN(heredoc_trimmed); 2754 else 2755 p = theline + STRLEN(heredoc_trimmed); 2756 if (STRCMP(p, skip_until) == 0) 2757 { 2758 VIM_CLEAR(skip_until); 2759 VIM_CLEAR(heredoc_trimmed); 2760 do_concat = TRUE; 2761 is_heredoc = FALSE; 2762 } 2763 } 2764 } 2765 else 2766 { 2767 // skip ':' and blanks 2768 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p) 2769 ; 2770 2771 // Check for "endfunction" or "enddef". 2772 if (checkforcmd(&p, nesting_def[nesting] 2773 ? "enddef" : "endfunction", 4) && nesting-- == 0) 2774 { 2775 char_u *nextcmd = NULL; 2776 2777 if (*p == '|') 2778 nextcmd = p + 1; 2779 else if (line_arg != NULL && *skipwhite(line_arg) != NUL) 2780 nextcmd = line_arg; 2781 else if (*p != NUL && *p != '"' && p_verbose > 0) 2782 give_warning2(eap->cmdidx == CMD_def 2783 ? (char_u *)_("W1001: Text found after :enddef: %s") 2784 : (char_u *)_("W22: Text found after :endfunction: %s"), 2785 p, TRUE); 2786 if (nextcmd != NULL) 2787 { 2788 // Another command follows. If the line came from "eap" we 2789 // can simply point into it, otherwise we need to change 2790 // "eap->cmdlinep". 2791 eap->nextcmd = nextcmd; 2792 if (line_to_free != NULL) 2793 { 2794 vim_free(*eap->cmdlinep); 2795 *eap->cmdlinep = line_to_free; 2796 line_to_free = NULL; 2797 } 2798 } 2799 break; 2800 } 2801 2802 // Increase indent inside "if", "while", "for" and "try", decrease 2803 // at "end". 2804 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0)) 2805 indent -= 2; 2806 else if (STRNCMP(p, "if", 2) == 0 2807 || STRNCMP(p, "wh", 2) == 0 2808 || STRNCMP(p, "for", 3) == 0 2809 || STRNCMP(p, "try", 3) == 0) 2810 indent += 2; 2811 2812 // Check for defining a function inside this function. 2813 // Only recognize "def" inside "def", not inside "function", 2814 // For backwards compatibility, see Test_function_python(). 2815 c = *p; 2816 if (checkforcmd(&p, "function", 2) 2817 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3))) 2818 { 2819 if (*p == '!') 2820 p = skipwhite(p + 1); 2821 p += eval_fname_script(p); 2822 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL)); 2823 if (*skipwhite(p) == '(') 2824 { 2825 if (nesting == MAX_FUNC_NESTING - 1) 2826 emsg(_("E1058: function nesting too deep")); 2827 else 2828 { 2829 ++nesting; 2830 nesting_def[nesting] = (c == 'd'); 2831 indent += 2; 2832 } 2833 } 2834 } 2835 2836 // Check for ":append", ":change", ":insert". Not for :def. 2837 p = skip_range(p, NULL); 2838 if (eap->cmdidx != CMD_def 2839 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p')) 2840 || (p[0] == 'c' 2841 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h' 2842 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a' 2843 && (STRNCMP(&p[3], "nge", 3) != 0 2844 || !ASCII_ISALPHA(p[6]))))))) 2845 || (p[0] == 'i' 2846 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n' 2847 && (!ASCII_ISALPHA(p[2]) 2848 || (p[2] == 's' 2849 && (!ASCII_ISALPHA(p[3]) 2850 || p[3] == 'e')))))))) 2851 skip_until = vim_strsave((char_u *)"."); 2852 2853 // Check for ":python <<EOF", ":tcl <<EOF", etc. 2854 arg = skipwhite(skiptowhite(p)); 2855 if (arg[0] == '<' && arg[1] =='<' 2856 && ((p[0] == 'p' && p[1] == 'y' 2857 && (!ASCII_ISALNUM(p[2]) || p[2] == 't' 2858 || ((p[2] == '3' || p[2] == 'x') 2859 && !ASCII_ISALPHA(p[3])))) 2860 || (p[0] == 'p' && p[1] == 'e' 2861 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r')) 2862 || (p[0] == 't' && p[1] == 'c' 2863 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l')) 2864 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a' 2865 && !ASCII_ISALPHA(p[3])) 2866 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b' 2867 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y')) 2868 || (p[0] == 'm' && p[1] == 'z' 2869 && (!ASCII_ISALPHA(p[2]) || p[2] == 's')) 2870 )) 2871 { 2872 // ":python <<" continues until a dot, like ":append" 2873 p = skipwhite(arg + 2); 2874 if (STRNCMP(p, "trim", 4) == 0) 2875 { 2876 // Ignore leading white space. 2877 p = skipwhite(p + 4); 2878 heredoc_trimmed = vim_strnsave(theline, 2879 (int)(skipwhite(theline) - theline)); 2880 } 2881 if (*p == NUL) 2882 skip_until = vim_strsave((char_u *)"."); 2883 else 2884 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p)); 2885 do_concat = FALSE; 2886 is_heredoc = TRUE; 2887 } 2888 2889 // Check for ":let v =<< [trim] EOF" 2890 // and ":let [a, b] =<< [trim] EOF" 2891 arg = skipwhite(skiptowhite(p)); 2892 if (*arg == '[') 2893 arg = vim_strchr(arg, ']'); 2894 if (arg != NULL) 2895 { 2896 arg = skipwhite(skiptowhite(arg)); 2897 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<' 2898 && ((p[0] == 'l' 2899 && p[1] == 'e' 2900 && (!ASCII_ISALNUM(p[2]) 2901 || (p[2] == 't' && !ASCII_ISALNUM(p[3])))))) 2902 { 2903 p = skipwhite(arg + 3); 2904 if (STRNCMP(p, "trim", 4) == 0) 2905 { 2906 // Ignore leading white space. 2907 p = skipwhite(p + 4); 2908 heredoc_trimmed = vim_strnsave(theline, 2909 (int)(skipwhite(theline) - theline)); 2910 } 2911 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p)); 2912 do_concat = FALSE; 2913 is_heredoc = TRUE; 2914 } 2915 } 2916 } 2917 2918 // Add the line to the function. 2919 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL) 2920 goto erret; 2921 2922 // Copy the line to newly allocated memory. get_one_sourceline() 2923 // allocates 250 bytes per line, this saves 80% on average. The cost 2924 // is an extra alloc/free. 2925 p = vim_strsave(theline); 2926 if (p == NULL) 2927 goto erret; 2928 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p; 2929 2930 // Add NULL lines for continuation lines, so that the line count is 2931 // equal to the index in the growarray. 2932 while (sourcing_lnum_off-- > 0) 2933 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL; 2934 2935 // Check for end of eap->arg. 2936 if (line_arg != NULL && *line_arg == NUL) 2937 line_arg = NULL; 2938 } 2939 2940 // Don't define the function when skipping commands or when an error was 2941 // detected. 2942 if (eap->skip || did_emsg) 2943 goto erret; 2944 2945 /* 2946 * If there are no errors, add the function 2947 */ 2948 if (fudi.fd_dict == NULL) 2949 { 2950 hashtab_T *ht; 2951 2952 v = find_var(name, &ht, FALSE); 2953 if (v != NULL && v->di_tv.v_type == VAR_FUNC) 2954 { 2955 emsg_funcname(N_("E707: Function name conflicts with variable: %s"), 2956 name); 2957 goto erret; 2958 } 2959 2960 fp = find_func_even_dead(name, NULL); 2961 if (fp != NULL) 2962 { 2963 int dead = fp->uf_flags & FC_DEAD; 2964 2965 // Function can be replaced with "function!" and when sourcing the 2966 // same script again, but only once. 2967 if (!dead && !eap->forceit 2968 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid 2969 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)) 2970 { 2971 emsg_funcname(e_funcexts, name); 2972 goto erret; 2973 } 2974 if (fp->uf_calls > 0) 2975 { 2976 emsg_funcname( 2977 N_("E127: Cannot redefine function %s: It is in use"), 2978 name); 2979 goto erret; 2980 } 2981 if (fp->uf_refcount > 1) 2982 { 2983 // This function is referenced somewhere, don't redefine it but 2984 // create a new one. 2985 --fp->uf_refcount; 2986 fp->uf_flags |= FC_REMOVED; 2987 fp = NULL; 2988 overwrite = TRUE; 2989 } 2990 else 2991 { 2992 char_u *exp_name = fp->uf_name_exp; 2993 2994 // redefine existing function, keep the expanded name 2995 VIM_CLEAR(name); 2996 fp->uf_name_exp = NULL; 2997 func_clear_items(fp); 2998 fp->uf_name_exp = exp_name; 2999 fp->uf_flags &= ~FC_DEAD; 3000 #ifdef FEAT_PROFILE 3001 fp->uf_profiling = FALSE; 3002 fp->uf_prof_initialized = FALSE; 3003 #endif 3004 } 3005 } 3006 } 3007 else 3008 { 3009 char numbuf[20]; 3010 3011 fp = NULL; 3012 if (fudi.fd_newkey == NULL && !eap->forceit) 3013 { 3014 emsg(_(e_funcdict)); 3015 goto erret; 3016 } 3017 if (fudi.fd_di == NULL) 3018 { 3019 // Can't add a function to a locked dictionary 3020 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE)) 3021 goto erret; 3022 } 3023 // Can't change an existing function if it is locked 3024 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE)) 3025 goto erret; 3026 3027 // Give the function a sequential number. Can only be used with a 3028 // Funcref! 3029 vim_free(name); 3030 sprintf(numbuf, "%d", ++func_nr); 3031 name = vim_strsave((char_u *)numbuf); 3032 if (name == NULL) 3033 goto erret; 3034 } 3035 3036 if (fp == NULL) 3037 { 3038 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL) 3039 { 3040 int slen, plen; 3041 char_u *scriptname; 3042 3043 // Check that the autoload name matches the script name. 3044 j = FAIL; 3045 if (SOURCING_NAME != NULL) 3046 { 3047 scriptname = autoload_name(name); 3048 if (scriptname != NULL) 3049 { 3050 p = vim_strchr(scriptname, '/'); 3051 plen = (int)STRLEN(p); 3052 slen = (int)STRLEN(SOURCING_NAME); 3053 if (slen > plen && fnamecmp(p, 3054 SOURCING_NAME + slen - plen) == 0) 3055 j = OK; 3056 vim_free(scriptname); 3057 } 3058 } 3059 if (j == FAIL) 3060 { 3061 semsg(_("E746: Function name does not match script file name: %s"), name); 3062 goto erret; 3063 } 3064 } 3065 3066 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1); 3067 if (fp == NULL) 3068 goto erret; 3069 fp->uf_dfunc_idx = -1; 3070 3071 if (fudi.fd_dict != NULL) 3072 { 3073 if (fudi.fd_di == NULL) 3074 { 3075 // add new dict entry 3076 fudi.fd_di = dictitem_alloc(fudi.fd_newkey); 3077 if (fudi.fd_di == NULL) 3078 { 3079 vim_free(fp); 3080 goto erret; 3081 } 3082 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL) 3083 { 3084 vim_free(fudi.fd_di); 3085 vim_free(fp); 3086 goto erret; 3087 } 3088 } 3089 else 3090 // overwrite existing dict entry 3091 clear_tv(&fudi.fd_di->di_tv); 3092 fudi.fd_di->di_tv.v_type = VAR_FUNC; 3093 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name); 3094 3095 // behave like "dict" was used 3096 flags |= FC_DICT; 3097 } 3098 3099 // insert the new function in the function list 3100 set_ufunc_name(fp, name); 3101 if (overwrite) 3102 { 3103 hi = hash_find(&func_hashtab, name); 3104 hi->hi_key = UF2HIKEY(fp); 3105 } 3106 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL) 3107 { 3108 vim_free(fp); 3109 goto erret; 3110 } 3111 fp->uf_refcount = 1; 3112 } 3113 fp->uf_args = newargs; 3114 fp->uf_def_args = default_args; 3115 fp->uf_ret_type = &t_any; 3116 fp->uf_func_type = &t_func_any; 3117 3118 if (eap->cmdidx == CMD_def) 3119 { 3120 int lnum_save = SOURCING_LNUM; 3121 3122 // error messages are for the first function line 3123 SOURCING_LNUM = sourcing_lnum_top; 3124 3125 // parse the argument types 3126 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10); 3127 3128 if (argtypes.ga_len > 0) 3129 { 3130 // When "varargs" is set the last name/type goes into uf_va_name 3131 // and uf_va_type. 3132 int len = argtypes.ga_len - (varargs ? 1 : 0); 3133 3134 if (len > 0) 3135 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len); 3136 if (fp->uf_arg_types != NULL) 3137 { 3138 int i; 3139 type_T *type; 3140 3141 for (i = 0; i < len; ++ i) 3142 { 3143 p = ((char_u **)argtypes.ga_data)[i]; 3144 if (p == NULL) 3145 // will get the type from the default value 3146 type = &t_unknown; 3147 else 3148 type = parse_type(&p, &fp->uf_type_list); 3149 if (type == NULL) 3150 { 3151 SOURCING_LNUM = lnum_save; 3152 goto errret_2; 3153 } 3154 fp->uf_arg_types[i] = type; 3155 } 3156 } 3157 if (varargs) 3158 { 3159 // Move the last argument "...name: type" to uf_va_name and 3160 // uf_va_type. 3161 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data) 3162 [fp->uf_args.ga_len - 1]; 3163 --fp->uf_args.ga_len; 3164 p = ((char_u **)argtypes.ga_data)[len]; 3165 if (p == NULL) 3166 // todo: get type from default value 3167 fp->uf_va_type = &t_any; 3168 else 3169 fp->uf_va_type = parse_type(&p, &fp->uf_type_list); 3170 if (fp->uf_va_type == NULL) 3171 { 3172 SOURCING_LNUM = lnum_save; 3173 goto errret_2; 3174 } 3175 } 3176 varargs = FALSE; 3177 } 3178 3179 // parse the return type, if any 3180 if (ret_type == NULL) 3181 fp->uf_ret_type = &t_void; 3182 else 3183 { 3184 p = ret_type; 3185 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list); 3186 } 3187 } 3188 3189 fp->uf_lines = newlines; 3190 if ((flags & FC_CLOSURE) != 0) 3191 { 3192 if (register_closure(fp) == FAIL) 3193 goto erret; 3194 } 3195 else 3196 fp->uf_scoped = NULL; 3197 3198 #ifdef FEAT_PROFILE 3199 if (prof_def_func()) 3200 func_do_profile(fp); 3201 #endif 3202 fp->uf_varargs = varargs; 3203 if (sandbox) 3204 flags |= FC_SANDBOX; 3205 fp->uf_flags = flags; 3206 fp->uf_calls = 0; 3207 fp->uf_cleared = FALSE; 3208 fp->uf_script_ctx = current_sctx; 3209 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top; 3210 if (is_export) 3211 { 3212 fp->uf_flags |= FC_EXPORT; 3213 // let ex_export() know the export worked. 3214 is_export = FALSE; 3215 } 3216 3217 // ":def Func()" needs to be compiled 3218 if (eap->cmdidx == CMD_def) 3219 compile_def_function(fp, FALSE); 3220 3221 goto ret_free; 3222 3223 erret: 3224 ga_clear_strings(&newargs); 3225 ga_clear_strings(&default_args); 3226 errret_2: 3227 ga_clear_strings(&newlines); 3228 ret_free: 3229 ga_clear_strings(&argtypes); 3230 vim_free(skip_until); 3231 vim_free(line_to_free); 3232 vim_free(fudi.fd_newkey); 3233 vim_free(name); 3234 vim_free(ret_type); 3235 did_emsg |= saved_did_emsg; 3236 need_wait_return |= saved_wait_return; 3237 } 3238 3239 /* 3240 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case). 3241 * Return 2 if "p" starts with "s:". 3242 * Return 0 otherwise. 3243 */ 3244 int 3245 eval_fname_script(char_u *p) 3246 { 3247 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with 3248 // the standard library function. 3249 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0 3250 || MB_STRNICMP(p + 1, "SNR>", 4) == 0)) 3251 return 5; 3252 if (p[0] == 's' && p[1] == ':') 3253 return 2; 3254 return 0; 3255 } 3256 3257 int 3258 translated_function_exists(char_u *name) 3259 { 3260 if (builtin_function(name, -1)) 3261 return has_internal_func(name); 3262 return find_func(name, NULL) != NULL; 3263 } 3264 3265 /* 3266 * Return TRUE when "ufunc" has old-style "..." varargs 3267 * or named varargs "...name: type". 3268 */ 3269 int 3270 has_varargs(ufunc_T *ufunc) 3271 { 3272 return ufunc->uf_varargs || ufunc->uf_va_name != NULL; 3273 } 3274 3275 /* 3276 * Return TRUE if a function "name" exists. 3277 * If "no_defef" is TRUE, do not dereference a Funcref. 3278 */ 3279 int 3280 function_exists(char_u *name, int no_deref) 3281 { 3282 char_u *nm = name; 3283 char_u *p; 3284 int n = FALSE; 3285 int flag; 3286 3287 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD; 3288 if (no_deref) 3289 flag |= TFN_NO_DEREF; 3290 p = trans_function_name(&nm, FALSE, flag, NULL, NULL); 3291 nm = skipwhite(nm); 3292 3293 // Only accept "funcname", "funcname ", "funcname (..." and 3294 // "funcname(...", not "funcname!...". 3295 if (p != NULL && (*nm == NUL || *nm == '(')) 3296 n = translated_function_exists(p); 3297 vim_free(p); 3298 return n; 3299 } 3300 3301 #if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO) 3302 char_u * 3303 get_expanded_name(char_u *name, int check) 3304 { 3305 char_u *nm = name; 3306 char_u *p; 3307 3308 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL); 3309 3310 if (p != NULL && *nm == NUL) 3311 if (!check || translated_function_exists(p)) 3312 return p; 3313 3314 vim_free(p); 3315 return NULL; 3316 } 3317 #endif 3318 3319 /* 3320 * Function given to ExpandGeneric() to obtain the list of user defined 3321 * function names. 3322 */ 3323 char_u * 3324 get_user_func_name(expand_T *xp, int idx) 3325 { 3326 static long_u done; 3327 static hashitem_T *hi; 3328 ufunc_T *fp; 3329 3330 if (idx == 0) 3331 { 3332 done = 0; 3333 hi = func_hashtab.ht_array; 3334 } 3335 if (done < func_hashtab.ht_used) 3336 { 3337 if (done++ > 0) 3338 ++hi; 3339 while (HASHITEM_EMPTY(hi)) 3340 ++hi; 3341 fp = HI2UF(hi); 3342 3343 // don't show dead, dict and lambda functions 3344 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT) 3345 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0) 3346 return (char_u *)""; 3347 3348 if (STRLEN(fp->uf_name) + 4 >= IOSIZE) 3349 return fp->uf_name; // prevents overflow 3350 3351 cat_func_name(IObuff, fp); 3352 if (xp->xp_context != EXPAND_USER_FUNC) 3353 { 3354 STRCAT(IObuff, "("); 3355 if (!has_varargs(fp) && fp->uf_args.ga_len == 0) 3356 STRCAT(IObuff, ")"); 3357 } 3358 return IObuff; 3359 } 3360 return NULL; 3361 } 3362 3363 /* 3364 * ":delfunction {name}" 3365 */ 3366 void 3367 ex_delfunction(exarg_T *eap) 3368 { 3369 ufunc_T *fp = NULL; 3370 char_u *p; 3371 char_u *name; 3372 funcdict_T fudi; 3373 3374 p = eap->arg; 3375 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL); 3376 vim_free(fudi.fd_newkey); 3377 if (name == NULL) 3378 { 3379 if (fudi.fd_dict != NULL && !eap->skip) 3380 emsg(_(e_funcref)); 3381 return; 3382 } 3383 if (!ends_excmd(*skipwhite(p))) 3384 { 3385 vim_free(name); 3386 emsg(_(e_trailing)); 3387 return; 3388 } 3389 eap->nextcmd = check_nextcmd(p); 3390 if (eap->nextcmd != NULL) 3391 *p = NUL; 3392 3393 if (!eap->skip) 3394 fp = find_func(name, NULL); 3395 vim_free(name); 3396 3397 if (!eap->skip) 3398 { 3399 if (fp == NULL) 3400 { 3401 if (!eap->forceit) 3402 semsg(_(e_nofunc), eap->arg); 3403 return; 3404 } 3405 if (fp->uf_calls > 0) 3406 { 3407 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg); 3408 return; 3409 } 3410 3411 if (fudi.fd_dict != NULL) 3412 { 3413 // Delete the dict item that refers to the function, it will 3414 // invoke func_unref() and possibly delete the function. 3415 dictitem_remove(fudi.fd_dict, fudi.fd_di); 3416 } 3417 else 3418 { 3419 // A normal function (not a numbered function or lambda) has a 3420 // refcount of 1 for the entry in the hashtable. When deleting 3421 // it and the refcount is more than one, it should be kept. 3422 // A numbered function and lambda should be kept if the refcount is 3423 // one or more. 3424 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1)) 3425 { 3426 // Function is still referenced somewhere. Don't free it but 3427 // do remove it from the hashtable. 3428 if (func_remove(fp)) 3429 fp->uf_refcount--; 3430 fp->uf_flags |= FC_DELETED; 3431 } 3432 else 3433 func_clear_free(fp, FALSE); 3434 } 3435 } 3436 } 3437 3438 /* 3439 * Unreference a Function: decrement the reference count and free it when it 3440 * becomes zero. 3441 */ 3442 void 3443 func_unref(char_u *name) 3444 { 3445 ufunc_T *fp = NULL; 3446 3447 if (name == NULL || !func_name_refcount(name)) 3448 return; 3449 fp = find_func(name, NULL); 3450 if (fp == NULL && isdigit(*name)) 3451 { 3452 #ifdef EXITFREE 3453 if (!entered_free_all_mem) 3454 #endif 3455 internal_error("func_unref()"); 3456 } 3457 if (fp != NULL && --fp->uf_refcount <= 0) 3458 { 3459 // Only delete it when it's not being used. Otherwise it's done 3460 // when "uf_calls" becomes zero. 3461 if (fp->uf_calls == 0) 3462 func_clear_free(fp, FALSE); 3463 } 3464 } 3465 3466 /* 3467 * Unreference a Function: decrement the reference count and free it when it 3468 * becomes zero. 3469 */ 3470 void 3471 func_ptr_unref(ufunc_T *fp) 3472 { 3473 if (fp != NULL && --fp->uf_refcount <= 0) 3474 { 3475 // Only delete it when it's not being used. Otherwise it's done 3476 // when "uf_calls" becomes zero. 3477 if (fp->uf_calls == 0) 3478 func_clear_free(fp, FALSE); 3479 } 3480 } 3481 3482 /* 3483 * Count a reference to a Function. 3484 */ 3485 void 3486 func_ref(char_u *name) 3487 { 3488 ufunc_T *fp; 3489 3490 if (name == NULL || !func_name_refcount(name)) 3491 return; 3492 fp = find_func(name, NULL); 3493 if (fp != NULL) 3494 ++fp->uf_refcount; 3495 else if (isdigit(*name)) 3496 // Only give an error for a numbered function. 3497 // Fail silently, when named or lambda function isn't found. 3498 internal_error("func_ref()"); 3499 } 3500 3501 /* 3502 * Count a reference to a Function. 3503 */ 3504 void 3505 func_ptr_ref(ufunc_T *fp) 3506 { 3507 if (fp != NULL) 3508 ++fp->uf_refcount; 3509 } 3510 3511 /* 3512 * Return TRUE if items in "fc" do not have "copyID". That means they are not 3513 * referenced from anywhere that is in use. 3514 */ 3515 static int 3516 can_free_funccal(funccall_T *fc, int copyID) 3517 { 3518 return (fc->l_varlist.lv_copyID != copyID 3519 && fc->l_vars.dv_copyID != copyID 3520 && fc->l_avars.dv_copyID != copyID 3521 && fc->fc_copyID != copyID); 3522 } 3523 3524 /* 3525 * ":return [expr]" 3526 */ 3527 void 3528 ex_return(exarg_T *eap) 3529 { 3530 char_u *arg = eap->arg; 3531 typval_T rettv; 3532 int returning = FALSE; 3533 3534 if (current_funccal == NULL) 3535 { 3536 emsg(_("E133: :return not inside a function")); 3537 return; 3538 } 3539 3540 if (eap->skip) 3541 ++emsg_skip; 3542 3543 eap->nextcmd = NULL; 3544 if ((*arg != NUL && *arg != '|' && *arg != '\n') 3545 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL) 3546 { 3547 if (!eap->skip) 3548 returning = do_return(eap, FALSE, TRUE, &rettv); 3549 else 3550 clear_tv(&rettv); 3551 } 3552 // It's safer to return also on error. 3553 else if (!eap->skip) 3554 { 3555 // In return statement, cause_abort should be force_abort. 3556 update_force_abort(); 3557 3558 /* 3559 * Return unless the expression evaluation has been cancelled due to an 3560 * aborting error, an interrupt, or an exception. 3561 */ 3562 if (!aborting()) 3563 returning = do_return(eap, FALSE, TRUE, NULL); 3564 } 3565 3566 // When skipping or the return gets pending, advance to the next command 3567 // in this line (!returning). Otherwise, ignore the rest of the line. 3568 // Following lines will be ignored by get_func_line(). 3569 if (returning) 3570 eap->nextcmd = NULL; 3571 else if (eap->nextcmd == NULL) // no argument 3572 eap->nextcmd = check_nextcmd(arg); 3573 3574 if (eap->skip) 3575 --emsg_skip; 3576 } 3577 3578 /* 3579 * ":1,25call func(arg1, arg2)" function call. 3580 */ 3581 void 3582 ex_call(exarg_T *eap) 3583 { 3584 char_u *arg = eap->arg; 3585 char_u *startarg; 3586 char_u *name; 3587 char_u *tofree; 3588 int len; 3589 typval_T rettv; 3590 linenr_T lnum; 3591 int doesrange; 3592 int failed = FALSE; 3593 funcdict_T fudi; 3594 partial_T *partial = NULL; 3595 3596 if (eap->skip) 3597 { 3598 // trans_function_name() doesn't work well when skipping, use eval0() 3599 // instead to skip to any following command, e.g. for: 3600 // :if 0 | call dict.foo().bar() | endif 3601 ++emsg_skip; 3602 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL) 3603 clear_tv(&rettv); 3604 --emsg_skip; 3605 return; 3606 } 3607 3608 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial); 3609 if (fudi.fd_newkey != NULL) 3610 { 3611 // Still need to give an error message for missing key. 3612 semsg(_(e_dictkey), fudi.fd_newkey); 3613 vim_free(fudi.fd_newkey); 3614 } 3615 if (tofree == NULL) 3616 return; 3617 3618 // Increase refcount on dictionary, it could get deleted when evaluating 3619 // the arguments. 3620 if (fudi.fd_dict != NULL) 3621 ++fudi.fd_dict->dv_refcount; 3622 3623 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its 3624 // contents. For VAR_PARTIAL get its partial, unless we already have one 3625 // from trans_function_name(). 3626 len = (int)STRLEN(tofree); 3627 name = deref_func_name(tofree, &len, 3628 partial != NULL ? NULL : &partial, FALSE); 3629 3630 // Skip white space to allow ":call func ()". Not good, but required for 3631 // backward compatibility. 3632 startarg = skipwhite(arg); 3633 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this 3634 3635 if (*startarg != '(') 3636 { 3637 semsg(_(e_missing_paren), eap->arg); 3638 goto end; 3639 } 3640 3641 /* 3642 * When skipping, evaluate the function once, to find the end of the 3643 * arguments. 3644 * When the function takes a range, this is discovered after the first 3645 * call, and the loop is broken. 3646 */ 3647 if (eap->skip) 3648 { 3649 ++emsg_skip; 3650 lnum = eap->line2; // do it once, also with an invalid range 3651 } 3652 else 3653 lnum = eap->line1; 3654 for ( ; lnum <= eap->line2; ++lnum) 3655 { 3656 funcexe_T funcexe; 3657 3658 if (!eap->skip && eap->addr_count > 0) 3659 { 3660 if (lnum > curbuf->b_ml.ml_line_count) 3661 { 3662 // If the function deleted lines or switched to another buffer 3663 // the line number may become invalid. 3664 emsg(_(e_invrange)); 3665 break; 3666 } 3667 curwin->w_cursor.lnum = lnum; 3668 curwin->w_cursor.col = 0; 3669 curwin->w_cursor.coladd = 0; 3670 } 3671 arg = startarg; 3672 3673 CLEAR_FIELD(funcexe); 3674 funcexe.firstline = eap->line1; 3675 funcexe.lastline = eap->line2; 3676 funcexe.doesrange = &doesrange; 3677 funcexe.evaluate = !eap->skip; 3678 funcexe.partial = partial; 3679 funcexe.selfdict = fudi.fd_dict; 3680 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL) 3681 { 3682 failed = TRUE; 3683 break; 3684 } 3685 if (has_watchexpr()) 3686 dbg_check_breakpoint(eap); 3687 3688 // Handle a function returning a Funcref, Dictionary or List. 3689 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE, 3690 name, &name) == FAIL) 3691 { 3692 failed = TRUE; 3693 break; 3694 } 3695 3696 clear_tv(&rettv); 3697 if (doesrange || eap->skip) 3698 break; 3699 3700 // Stop when immediately aborting on error, or when an interrupt 3701 // occurred or an exception was thrown but not caught. 3702 // get_func_tv() returned OK, so that the check for trailing 3703 // characters below is executed. 3704 if (aborting()) 3705 break; 3706 } 3707 if (eap->skip) 3708 --emsg_skip; 3709 3710 // When inside :try we need to check for following "| catch". 3711 if (!failed || eap->cstack->cs_trylevel > 0) 3712 { 3713 // Check for trailing illegal characters and a following command. 3714 if (!ends_excmd(*arg)) 3715 { 3716 if (!failed) 3717 { 3718 emsg_severe = TRUE; 3719 emsg(_(e_trailing)); 3720 } 3721 } 3722 else 3723 eap->nextcmd = check_nextcmd(arg); 3724 } 3725 3726 end: 3727 dict_unref(fudi.fd_dict); 3728 vim_free(tofree); 3729 } 3730 3731 /* 3732 * Return from a function. Possibly makes the return pending. Also called 3733 * for a pending return at the ":endtry" or after returning from an extra 3734 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set 3735 * when called due to a ":return" command. "rettv" may point to a typval_T 3736 * with the return rettv. Returns TRUE when the return can be carried out, 3737 * FALSE when the return gets pending. 3738 */ 3739 int 3740 do_return( 3741 exarg_T *eap, 3742 int reanimate, 3743 int is_cmd, 3744 void *rettv) 3745 { 3746 int idx; 3747 cstack_T *cstack = eap->cstack; 3748 3749 if (reanimate) 3750 // Undo the return. 3751 current_funccal->returned = FALSE; 3752 3753 /* 3754 * Cleanup (and inactivate) conditionals, but stop when a try conditional 3755 * not in its finally clause (which then is to be executed next) is found. 3756 * In this case, make the ":return" pending for execution at the ":endtry". 3757 * Otherwise, return normally. 3758 */ 3759 idx = cleanup_conditionals(eap->cstack, 0, TRUE); 3760 if (idx >= 0) 3761 { 3762 cstack->cs_pending[idx] = CSTP_RETURN; 3763 3764 if (!is_cmd && !reanimate) 3765 // A pending return again gets pending. "rettv" points to an 3766 // allocated variable with the rettv of the original ":return"'s 3767 // argument if present or is NULL else. 3768 cstack->cs_rettv[idx] = rettv; 3769 else 3770 { 3771 // When undoing a return in order to make it pending, get the stored 3772 // return rettv. 3773 if (reanimate) 3774 rettv = current_funccal->rettv; 3775 3776 if (rettv != NULL) 3777 { 3778 // Store the value of the pending return. 3779 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL) 3780 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv; 3781 else 3782 emsg(_(e_outofmem)); 3783 } 3784 else 3785 cstack->cs_rettv[idx] = NULL; 3786 3787 if (reanimate) 3788 { 3789 // The pending return value could be overwritten by a ":return" 3790 // without argument in a finally clause; reset the default 3791 // return value. 3792 current_funccal->rettv->v_type = VAR_NUMBER; 3793 current_funccal->rettv->vval.v_number = 0; 3794 } 3795 } 3796 report_make_pending(CSTP_RETURN, rettv); 3797 } 3798 else 3799 { 3800 current_funccal->returned = TRUE; 3801 3802 // If the return is carried out now, store the return value. For 3803 // a return immediately after reanimation, the value is already 3804 // there. 3805 if (!reanimate && rettv != NULL) 3806 { 3807 clear_tv(current_funccal->rettv); 3808 *current_funccal->rettv = *(typval_T *)rettv; 3809 if (!is_cmd) 3810 vim_free(rettv); 3811 } 3812 } 3813 3814 return idx < 0; 3815 } 3816 3817 /* 3818 * Free the variable with a pending return value. 3819 */ 3820 void 3821 discard_pending_return(void *rettv) 3822 { 3823 free_tv((typval_T *)rettv); 3824 } 3825 3826 /* 3827 * Generate a return command for producing the value of "rettv". The result 3828 * is an allocated string. Used by report_pending() for verbose messages. 3829 */ 3830 char_u * 3831 get_return_cmd(void *rettv) 3832 { 3833 char_u *s = NULL; 3834 char_u *tofree = NULL; 3835 char_u numbuf[NUMBUFLEN]; 3836 3837 if (rettv != NULL) 3838 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0); 3839 if (s == NULL) 3840 s = (char_u *)""; 3841 3842 STRCPY(IObuff, ":return "); 3843 STRNCPY(IObuff + 8, s, IOSIZE - 8); 3844 if (STRLEN(s) + 8 >= IOSIZE) 3845 STRCPY(IObuff + IOSIZE - 4, "..."); 3846 vim_free(tofree); 3847 return vim_strsave(IObuff); 3848 } 3849 3850 /* 3851 * Get next function line. 3852 * Called by do_cmdline() to get the next line. 3853 * Returns allocated string, or NULL for end of function. 3854 */ 3855 char_u * 3856 get_func_line( 3857 int c UNUSED, 3858 void *cookie, 3859 int indent UNUSED, 3860 int do_concat UNUSED) 3861 { 3862 funccall_T *fcp = (funccall_T *)cookie; 3863 ufunc_T *fp = fcp->func; 3864 char_u *retval; 3865 garray_T *gap; // growarray with function lines 3866 3867 // If breakpoints have been added/deleted need to check for it. 3868 if (fcp->dbg_tick != debug_tick) 3869 { 3870 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, 3871 SOURCING_LNUM); 3872 fcp->dbg_tick = debug_tick; 3873 } 3874 #ifdef FEAT_PROFILE 3875 if (do_profiling == PROF_YES) 3876 func_line_end(cookie); 3877 #endif 3878 3879 gap = &fp->uf_lines; 3880 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try()) 3881 || fcp->returned) 3882 retval = NULL; 3883 else 3884 { 3885 // Skip NULL lines (continuation lines). 3886 while (fcp->linenr < gap->ga_len 3887 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL) 3888 ++fcp->linenr; 3889 if (fcp->linenr >= gap->ga_len) 3890 retval = NULL; 3891 else 3892 { 3893 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]); 3894 SOURCING_LNUM = fcp->linenr; 3895 #ifdef FEAT_PROFILE 3896 if (do_profiling == PROF_YES) 3897 func_line_start(cookie); 3898 #endif 3899 } 3900 } 3901 3902 // Did we encounter a breakpoint? 3903 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM) 3904 { 3905 dbg_breakpoint(fp->uf_name, SOURCING_LNUM); 3906 // Find next breakpoint. 3907 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, 3908 SOURCING_LNUM); 3909 fcp->dbg_tick = debug_tick; 3910 } 3911 3912 return retval; 3913 } 3914 3915 /* 3916 * Return TRUE if the currently active function should be ended, because a 3917 * return was encountered or an error occurred. Used inside a ":while". 3918 */ 3919 int 3920 func_has_ended(void *cookie) 3921 { 3922 funccall_T *fcp = (funccall_T *)cookie; 3923 3924 // Ignore the "abort" flag if the abortion behavior has been changed due to 3925 // an error inside a try conditional. 3926 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try()) 3927 || fcp->returned); 3928 } 3929 3930 /* 3931 * return TRUE if cookie indicates a function which "abort"s on errors. 3932 */ 3933 int 3934 func_has_abort( 3935 void *cookie) 3936 { 3937 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT; 3938 } 3939 3940 3941 /* 3942 * Turn "dict.Func" into a partial for "Func" bound to "dict". 3943 * Don't do this when "Func" is already a partial that was bound 3944 * explicitly (pt_auto is FALSE). 3945 * Changes "rettv" in-place. 3946 * Returns the updated "selfdict_in". 3947 */ 3948 dict_T * 3949 make_partial(dict_T *selfdict_in, typval_T *rettv) 3950 { 3951 char_u *fname; 3952 char_u *tofree = NULL; 3953 ufunc_T *fp; 3954 char_u fname_buf[FLEN_FIXED + 1]; 3955 int error; 3956 dict_T *selfdict = selfdict_in; 3957 3958 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL) 3959 fp = rettv->vval.v_partial->pt_func; 3960 else 3961 { 3962 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string 3963 : rettv->vval.v_partial->pt_name; 3964 // Translate "s:func" to the stored function name. 3965 fname = fname_trans_sid(fname, fname_buf, &tofree, &error); 3966 fp = find_func(fname, NULL); 3967 vim_free(tofree); 3968 } 3969 3970 if (fp != NULL && (fp->uf_flags & FC_DICT)) 3971 { 3972 partial_T *pt = ALLOC_CLEAR_ONE(partial_T); 3973 3974 if (pt != NULL) 3975 { 3976 pt->pt_refcount = 1; 3977 pt->pt_dict = selfdict; 3978 pt->pt_auto = TRUE; 3979 selfdict = NULL; 3980 if (rettv->v_type == VAR_FUNC) 3981 { 3982 // Just a function: Take over the function name and use 3983 // selfdict. 3984 pt->pt_name = rettv->vval.v_string; 3985 } 3986 else 3987 { 3988 partial_T *ret_pt = rettv->vval.v_partial; 3989 int i; 3990 3991 // Partial: copy the function name, use selfdict and copy 3992 // args. Can't take over name or args, the partial might 3993 // be referenced elsewhere. 3994 if (ret_pt->pt_name != NULL) 3995 { 3996 pt->pt_name = vim_strsave(ret_pt->pt_name); 3997 func_ref(pt->pt_name); 3998 } 3999 else 4000 { 4001 pt->pt_func = ret_pt->pt_func; 4002 func_ptr_ref(pt->pt_func); 4003 } 4004 if (ret_pt->pt_argc > 0) 4005 { 4006 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc); 4007 if (pt->pt_argv == NULL) 4008 // out of memory: drop the arguments 4009 pt->pt_argc = 0; 4010 else 4011 { 4012 pt->pt_argc = ret_pt->pt_argc; 4013 for (i = 0; i < pt->pt_argc; i++) 4014 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]); 4015 } 4016 } 4017 partial_unref(ret_pt); 4018 } 4019 rettv->v_type = VAR_PARTIAL; 4020 rettv->vval.v_partial = pt; 4021 } 4022 } 4023 return selfdict; 4024 } 4025 4026 /* 4027 * Return the name of the executed function. 4028 */ 4029 char_u * 4030 func_name(void *cookie) 4031 { 4032 return ((funccall_T *)cookie)->func->uf_name; 4033 } 4034 4035 /* 4036 * Return the address holding the next breakpoint line for a funccall cookie. 4037 */ 4038 linenr_T * 4039 func_breakpoint(void *cookie) 4040 { 4041 return &((funccall_T *)cookie)->breakpoint; 4042 } 4043 4044 /* 4045 * Return the address holding the debug tick for a funccall cookie. 4046 */ 4047 int * 4048 func_dbg_tick(void *cookie) 4049 { 4050 return &((funccall_T *)cookie)->dbg_tick; 4051 } 4052 4053 /* 4054 * Return the nesting level for a funccall cookie. 4055 */ 4056 int 4057 func_level(void *cookie) 4058 { 4059 return ((funccall_T *)cookie)->level; 4060 } 4061 4062 /* 4063 * Return TRUE when a function was ended by a ":return" command. 4064 */ 4065 int 4066 current_func_returned(void) 4067 { 4068 return current_funccal->returned; 4069 } 4070 4071 int 4072 free_unref_funccal(int copyID, int testing) 4073 { 4074 int did_free = FALSE; 4075 int did_free_funccal = FALSE; 4076 funccall_T *fc, **pfc; 4077 4078 for (pfc = &previous_funccal; *pfc != NULL; ) 4079 { 4080 if (can_free_funccal(*pfc, copyID)) 4081 { 4082 fc = *pfc; 4083 *pfc = fc->caller; 4084 free_funccal_contents(fc); 4085 did_free = TRUE; 4086 did_free_funccal = TRUE; 4087 } 4088 else 4089 pfc = &(*pfc)->caller; 4090 } 4091 if (did_free_funccal) 4092 // When a funccal was freed some more items might be garbage 4093 // collected, so run again. 4094 (void)garbage_collect(testing); 4095 4096 return did_free; 4097 } 4098 4099 /* 4100 * Get function call environment based on backtrace debug level 4101 */ 4102 static funccall_T * 4103 get_funccal(void) 4104 { 4105 int i; 4106 funccall_T *funccal; 4107 funccall_T *temp_funccal; 4108 4109 funccal = current_funccal; 4110 if (debug_backtrace_level > 0) 4111 { 4112 for (i = 0; i < debug_backtrace_level; i++) 4113 { 4114 temp_funccal = funccal->caller; 4115 if (temp_funccal) 4116 funccal = temp_funccal; 4117 else 4118 // backtrace level overflow. reset to max 4119 debug_backtrace_level = i; 4120 } 4121 } 4122 return funccal; 4123 } 4124 4125 /* 4126 * Return the hashtable used for local variables in the current funccal. 4127 * Return NULL if there is no current funccal. 4128 */ 4129 hashtab_T * 4130 get_funccal_local_ht() 4131 { 4132 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0) 4133 return NULL; 4134 return &get_funccal()->l_vars.dv_hashtab; 4135 } 4136 4137 /* 4138 * Return the l: scope variable. 4139 * Return NULL if there is no current funccal. 4140 */ 4141 dictitem_T * 4142 get_funccal_local_var() 4143 { 4144 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0) 4145 return NULL; 4146 return &get_funccal()->l_vars_var; 4147 } 4148 4149 /* 4150 * Return the hashtable used for argument in the current funccal. 4151 * Return NULL if there is no current funccal. 4152 */ 4153 hashtab_T * 4154 get_funccal_args_ht() 4155 { 4156 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0) 4157 return NULL; 4158 return &get_funccal()->l_avars.dv_hashtab; 4159 } 4160 4161 /* 4162 * Return the a: scope variable. 4163 * Return NULL if there is no current funccal. 4164 */ 4165 dictitem_T * 4166 get_funccal_args_var() 4167 { 4168 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0) 4169 return NULL; 4170 return &get_funccal()->l_avars_var; 4171 } 4172 4173 /* 4174 * List function variables, if there is a function. 4175 */ 4176 void 4177 list_func_vars(int *first) 4178 { 4179 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0) 4180 list_hashtable_vars(¤t_funccal->l_vars.dv_hashtab, 4181 "l:", FALSE, first); 4182 } 4183 4184 /* 4185 * If "ht" is the hashtable for local variables in the current funccal, return 4186 * the dict that contains it. 4187 * Otherwise return NULL. 4188 */ 4189 dict_T * 4190 get_current_funccal_dict(hashtab_T *ht) 4191 { 4192 if (current_funccal != NULL 4193 && ht == ¤t_funccal->l_vars.dv_hashtab) 4194 return ¤t_funccal->l_vars; 4195 return NULL; 4196 } 4197 4198 /* 4199 * Search hashitem in parent scope. 4200 */ 4201 hashitem_T * 4202 find_hi_in_scoped_ht(char_u *name, hashtab_T **pht) 4203 { 4204 funccall_T *old_current_funccal = current_funccal; 4205 hashtab_T *ht; 4206 hashitem_T *hi = NULL; 4207 char_u *varname; 4208 4209 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL) 4210 return NULL; 4211 4212 // Search in parent scope which is possible to reference from lambda 4213 current_funccal = current_funccal->func->uf_scoped; 4214 while (current_funccal != NULL) 4215 { 4216 ht = find_var_ht(name, &varname); 4217 if (ht != NULL && *varname != NUL) 4218 { 4219 hi = hash_find(ht, varname); 4220 if (!HASHITEM_EMPTY(hi)) 4221 { 4222 *pht = ht; 4223 break; 4224 } 4225 } 4226 if (current_funccal == current_funccal->func->uf_scoped) 4227 break; 4228 current_funccal = current_funccal->func->uf_scoped; 4229 } 4230 current_funccal = old_current_funccal; 4231 4232 return hi; 4233 } 4234 4235 /* 4236 * Search variable in parent scope. 4237 */ 4238 dictitem_T * 4239 find_var_in_scoped_ht(char_u *name, int no_autoload) 4240 { 4241 dictitem_T *v = NULL; 4242 funccall_T *old_current_funccal = current_funccal; 4243 hashtab_T *ht; 4244 char_u *varname; 4245 4246 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL) 4247 return NULL; 4248 4249 // Search in parent scope which is possible to reference from lambda 4250 current_funccal = current_funccal->func->uf_scoped; 4251 while (current_funccal) 4252 { 4253 ht = find_var_ht(name, &varname); 4254 if (ht != NULL && *varname != NUL) 4255 { 4256 v = find_var_in_ht(ht, *name, varname, no_autoload); 4257 if (v != NULL) 4258 break; 4259 } 4260 if (current_funccal == current_funccal->func->uf_scoped) 4261 break; 4262 current_funccal = current_funccal->func->uf_scoped; 4263 } 4264 current_funccal = old_current_funccal; 4265 4266 return v; 4267 } 4268 4269 /* 4270 * Set "copyID + 1" in previous_funccal and callers. 4271 */ 4272 int 4273 set_ref_in_previous_funccal(int copyID) 4274 { 4275 int abort = FALSE; 4276 funccall_T *fc; 4277 4278 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller) 4279 { 4280 fc->fc_copyID = copyID + 1; 4281 abort = abort 4282 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL) 4283 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL) 4284 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL); 4285 } 4286 return abort; 4287 } 4288 4289 static int 4290 set_ref_in_funccal(funccall_T *fc, int copyID) 4291 { 4292 int abort = FALSE; 4293 4294 if (fc->fc_copyID != copyID) 4295 { 4296 fc->fc_copyID = copyID; 4297 abort = abort 4298 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL) 4299 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL) 4300 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL) 4301 || set_ref_in_func(NULL, fc->func, copyID); 4302 } 4303 return abort; 4304 } 4305 4306 /* 4307 * Set "copyID" in all local vars and arguments in the call stack. 4308 */ 4309 int 4310 set_ref_in_call_stack(int copyID) 4311 { 4312 int abort = FALSE; 4313 funccall_T *fc; 4314 funccal_entry_T *entry; 4315 4316 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller) 4317 abort = abort || set_ref_in_funccal(fc, copyID); 4318 4319 // Also go through the funccal_stack. 4320 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next) 4321 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller) 4322 abort = abort || set_ref_in_funccal(fc, copyID); 4323 4324 return abort; 4325 } 4326 4327 /* 4328 * Set "copyID" in all functions available by name. 4329 */ 4330 int 4331 set_ref_in_functions(int copyID) 4332 { 4333 int todo; 4334 hashitem_T *hi = NULL; 4335 int abort = FALSE; 4336 ufunc_T *fp; 4337 4338 todo = (int)func_hashtab.ht_used; 4339 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi) 4340 { 4341 if (!HASHITEM_EMPTY(hi)) 4342 { 4343 --todo; 4344 fp = HI2UF(hi); 4345 if (!func_name_refcount(fp->uf_name)) 4346 abort = abort || set_ref_in_func(NULL, fp, copyID); 4347 } 4348 } 4349 return abort; 4350 } 4351 4352 /* 4353 * Set "copyID" in all function arguments. 4354 */ 4355 int 4356 set_ref_in_func_args(int copyID) 4357 { 4358 int i; 4359 int abort = FALSE; 4360 4361 for (i = 0; i < funcargs.ga_len; ++i) 4362 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i], 4363 copyID, NULL, NULL); 4364 return abort; 4365 } 4366 4367 /* 4368 * Mark all lists and dicts referenced through function "name" with "copyID". 4369 * Returns TRUE if setting references failed somehow. 4370 */ 4371 int 4372 set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID) 4373 { 4374 ufunc_T *fp = fp_in; 4375 funccall_T *fc; 4376 int error = FCERR_NONE; 4377 char_u fname_buf[FLEN_FIXED + 1]; 4378 char_u *tofree = NULL; 4379 char_u *fname; 4380 int abort = FALSE; 4381 4382 if (name == NULL && fp_in == NULL) 4383 return FALSE; 4384 4385 if (fp_in == NULL) 4386 { 4387 fname = fname_trans_sid(name, fname_buf, &tofree, &error); 4388 fp = find_func(fname, NULL); 4389 } 4390 if (fp != NULL) 4391 { 4392 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped) 4393 abort = abort || set_ref_in_funccal(fc, copyID); 4394 } 4395 vim_free(tofree); 4396 return abort; 4397 } 4398 4399 #endif // FEAT_EVAL 4400