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