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