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