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