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