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