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