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