blob: 71591eb5e92ca21afb75173ef67116a13674f41d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
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/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar93343722018-07-10 19:39:18 +020017// 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 Moolenaar8a7d6542020-01-26 15:56:19 +010025#define FC_DEAD 0x80 // function kept only for reference to dfunc
26#define FC_EXPORT 0x100 // "export def Func()"
Bram Moolenaarf10806b2020-04-02 18:34:35 +020027#define FC_NOARGS 0x200 // no a: variables in lambda
Bram Moolenaara9b579f2016-07-17 18:29:19 +020028
Bram Moolenaara9b579f2016-07-17 18:29:19 +020029/*
30 * All user-defined functions are found in this hashtable.
31 */
32static hashtab_T func_hashtab;
33
Bram Moolenaare38eab22019-12-05 21:50:01 +010034// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020035static garray_T funcargs = GA_EMPTY;
36
Bram Moolenaar209b8e32019-03-14 13:43:24 +010037// pointer to funccal for currently active function
38static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020039
Bram Moolenaar209b8e32019-03-14 13:43:24 +010040// Pointer to list of previously used funccal, still around because some
41// item in it is still being used.
42static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020043
44static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
45static char *e_funcdict = N_("E717: Dictionary entry already exists");
46static char *e_funcref = N_("E718: Funcref required");
47static char *e_nofunc = N_("E130: Unknown function: %s");
48
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020049static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020050
51 void
52func_init()
53{
54 hash_init(&func_hashtab);
55}
56
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020057/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020058 * Return the function hash table
59 */
60 hashtab_T *
61func_tbl_get(void)
62{
63 return &func_hashtab;
64}
65
66/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010067 * Get one function argument and an optional type: "arg: type".
68 * Return a pointer to after the type.
69 * When something is wrong return "arg".
70 */
71 static char_u *
72one_function_arg(char_u *arg, garray_T *newargs, garray_T *argtypes, int skip)
73{
74 char_u *p = arg;
75
76 while (ASCII_ISALNUM(*p) || *p == '_')
77 ++p;
78 if (arg == p || isdigit(*arg)
79 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
80 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
81 {
82 if (!skip)
83 semsg(_("E125: Illegal argument: %s"), arg);
84 return arg;
85 }
86 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
87 return arg;
88 if (newargs != NULL)
89 {
90 char_u *arg_copy;
91 int c;
92 int i;
93
94 c = *p;
95 *p = NUL;
96 arg_copy = vim_strsave(arg);
97 if (arg_copy == NULL)
98 {
99 *p = c;
100 return arg;
101 }
102
103 // Check for duplicate argument name.
104 for (i = 0; i < newargs->ga_len; ++i)
105 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
106 {
107 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
108 vim_free(arg_copy);
109 return arg;
110 }
111 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
112 newargs->ga_len++;
113
114 *p = c;
115 }
116
117 // get any type from "arg: type"
118 if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
119 {
120 char_u *type = NULL;
121
122 if (*p == ':')
123 {
124 type = skipwhite(p + 1);
125 p = skip_type(type);
126 type = vim_strnsave(type, p - type);
127 }
128 else if (*skipwhite(p) == ':')
129 emsg(_("E1059: No white space allowed before :"));
130 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
131 }
132
133 return p;
134}
135
136/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200137 * Get function arguments.
138 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200140get_function_args(
141 char_u **argp,
142 char_u endchar,
143 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144 garray_T *argtypes, // NULL unless using :def
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200145 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200146 garray_T *default_args,
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200147 int skip)
148{
149 int mustend = FALSE;
150 char_u *arg = *argp;
151 char_u *p = arg;
152 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200153 int any_default = FALSE;
154 char_u *expr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200155
156 if (newargs != NULL)
157 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 if (argtypes != NULL)
159 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200160 if (default_args != NULL)
161 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200162
163 if (varargs != NULL)
164 *varargs = FALSE;
165
166 /*
167 * Isolate the arguments: "arg1, arg2, ...)"
168 */
169 while (*p != endchar)
170 {
171 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
172 {
173 if (varargs != NULL)
174 *varargs = TRUE;
175 p += 3;
176 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100177
178 if (argtypes != NULL)
179 {
180 // ...name: list<type>
181 if (!ASCII_ISALPHA(*p))
182 {
183 emsg(_("E1055: Missing name after ..."));
184 break;
185 }
186
187 arg = p;
188 p = one_function_arg(p, newargs, argtypes, skip);
189 if (p == arg)
190 break;
191 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200192 }
193 else
194 {
195 arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 p = one_function_arg(p, newargs, argtypes, skip);
197 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200198 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200199
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200200 if (*skipwhite(p) == '=' && default_args != NULL)
201 {
202 typval_T rettv;
203
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100204 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200205 any_default = TRUE;
206 p = skipwhite(p) + 1;
207 p = skipwhite(p);
208 expr = p;
209 if (eval1(&p, &rettv, FALSE) != FAIL)
210 {
211 if (ga_grow(default_args, 1) == FAIL)
212 goto err_ret;
213
214 // trim trailing whitespace
215 while (p > expr && VIM_ISWHITE(p[-1]))
216 p--;
217 c = *p;
218 *p = NUL;
219 expr = vim_strsave(expr);
220 if (expr == NULL)
221 {
222 *p = c;
223 goto err_ret;
224 }
225 ((char_u **)(default_args->ga_data))
226 [default_args->ga_len] = expr;
227 default_args->ga_len++;
228 *p = c;
229 }
230 else
231 mustend = TRUE;
232 }
233 else if (any_default)
234 {
235 emsg(_("E989: Non-default argument follows default argument"));
236 mustend = TRUE;
237 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200238 if (*p == ',')
239 ++p;
240 else
241 mustend = TRUE;
242 }
243 p = skipwhite(p);
244 if (mustend && *p != endchar)
245 {
246 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100247 semsg(_(e_invarg2), *argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200248 break;
249 }
250 }
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200251 if (*p != endchar)
252 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100253 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200254
255 *argp = p;
256 return OK;
257
258err_ret:
259 if (newargs != NULL)
260 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200261 if (default_args != NULL)
262 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200263 return FAIL;
264}
265
266/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200267 * Register function "fp" as using "current_funccal" as its scope.
268 */
269 static int
270register_closure(ufunc_T *fp)
271{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200272 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100273 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200274 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200275 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200276 fp->uf_scoped = current_funccal;
277 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200278
Bram Moolenaar58016442016-07-31 18:30:22 +0200279 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
280 return FAIL;
281 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
282 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200283 return OK;
284}
285
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100286 static void
287set_ufunc_name(ufunc_T *fp, char_u *name)
288{
289 STRCPY(fp->uf_name, name);
290
291 if (name[0] == K_SPECIAL)
292 {
293 fp->uf_name_exp = alloc(STRLEN(name) + 3);
294 if (fp->uf_name_exp != NULL)
295 {
296 STRCPY(fp->uf_name_exp, "<SNR>");
297 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
298 }
299 }
300}
301
Bram Moolenaar58016442016-07-31 18:30:22 +0200302/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200303 * Parse a lambda expression and get a Funcref from "*arg".
304 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
305 */
306 int
307get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
308{
309 garray_T newargs;
310 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200311 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200312 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100313 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200314 int varargs;
315 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200316 char_u *start = skipwhite(*arg + 1);
317 char_u *s, *e;
318 static int lambda_no = 0;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200319 int *old_eval_lavars = eval_lavars_used;
320 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200321
322 ga_init(&newargs);
323 ga_init(&newlines);
324
Bram Moolenaare38eab22019-12-05 21:50:01 +0100325 // First, check if this is a lambda expression. "->" must exist.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200327 if (ret == FAIL || *start != '>')
328 return NOTDONE;
329
Bram Moolenaare38eab22019-12-05 21:50:01 +0100330 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200331 if (evaluate)
332 pnewargs = &newargs;
333 else
334 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200335 *arg = skipwhite(*arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 // TODO: argument types
337 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200338 if (ret == FAIL || **arg != '>')
339 goto errret;
340
Bram Moolenaare38eab22019-12-05 21:50:01 +0100341 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200342 if (evaluate)
343 eval_lavars_used = &eval_lavars;
344
Bram Moolenaare38eab22019-12-05 21:50:01 +0100345 // Get the start and the end of the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200346 *arg = skipwhite(*arg + 1);
347 s = *arg;
348 ret = skip_expr(arg);
349 if (ret == FAIL)
350 goto errret;
351 e = *arg;
352 *arg = skipwhite(*arg);
353 if (**arg != '}')
Bram Moolenaaree619e52020-03-28 21:38:06 +0100354 {
355 semsg(_("E451: Expected }: %s"), *arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200356 goto errret;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100357 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200358 ++*arg;
359
360 if (evaluate)
361 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200362 int len, flags = 0;
363 char_u *p;
364 char_u name[20];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200365
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200366 sprintf((char*)name, "<lambda>%d", ++lambda_no);
367
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200368 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200369 if (fp == NULL)
370 goto errret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100371 fp->uf_dfunc_idx = -1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200372 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200373 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200374 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200375
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200376 ga_init2(&newlines, (int)sizeof(char_u *), 1);
377 if (ga_grow(&newlines, 1) == FAIL)
378 goto errret;
379
Bram Moolenaare38eab22019-12-05 21:50:01 +0100380 // Add "return " before the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200381 len = 7 + e - s + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200382 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200383 if (p == NULL)
384 goto errret;
385 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
386 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200387 vim_strncpy(p + 7, s, e - s);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200388 if (strstr((char *)p + 7, "a:") == NULL)
389 // No a: variables are used for sure.
390 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200391
392 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100393 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200394 hash_add(&func_hashtab, UF2HIKEY(fp));
395 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200396 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200397 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200398 if (current_funccal != NULL && eval_lavars)
399 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200400 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200401 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200402 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200403 }
404 else
405 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200406
407#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200408 if (prof_def_func())
409 func_do_profile(fp);
410#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200411 if (sandbox)
412 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200414 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200415 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200416 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200417 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100418 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200419
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200420 pt->pt_func = fp;
421 pt->pt_refcount = 1;
422 rettv->vval.v_partial = pt;
423 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200424 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200425
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200426 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200427 return OK;
428
429errret:
430 ga_clear_strings(&newargs);
431 ga_clear_strings(&newlines);
432 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100433 vim_free(pt);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200434 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200435 return FAIL;
436}
437
438/*
439 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
440 * name it contains, otherwise return "name".
441 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
442 * "partialp".
443 */
444 char_u *
445deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
446{
447 dictitem_T *v;
448 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200449 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200450
451 if (partialp != NULL)
452 *partialp = NULL;
453
454 cc = name[*lenp];
455 name[*lenp] = NUL;
456 v = find_var(name, NULL, no_autoload);
457 name[*lenp] = cc;
458 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
459 {
460 if (v->di_tv.vval.v_string == NULL)
461 {
462 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100463 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200464 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200465 s = v->di_tv.vval.v_string;
466 *lenp = (int)STRLEN(s);
467 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200468 }
469
470 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
471 {
472 partial_T *pt = v->di_tv.vval.v_partial;
473
474 if (pt == NULL)
475 {
476 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100477 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200478 }
479 if (partialp != NULL)
480 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200481 s = partial_name(pt);
482 *lenp = (int)STRLEN(s);
483 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200484 }
485
486 return name;
487}
488
489/*
490 * Give an error message with a function name. Handle <SNR> things.
491 * "ermsg" is to be passed without translation, use N_() instead of _().
492 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100493 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200494emsg_funcname(char *ermsg, char_u *name)
495{
496 char_u *p;
497
498 if (*name == K_SPECIAL)
499 p = concat_str((char_u *)"<SNR>", name + 3);
500 else
501 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100502 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200503 if (p != name)
504 vim_free(p);
505}
506
507/*
508 * Allocate a variable for the result of a function.
509 * Return OK or FAIL.
510 */
511 int
512get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200513 char_u *name, // name of the function
514 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200515 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200516 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200517 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200518{
519 char_u *argp;
520 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100521 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
522 int argcount = 0; // number of arguments found
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200523
524 /*
525 * Get the arguments.
526 */
527 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200528 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
529 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200530 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100531 argp = skipwhite(argp + 1); // skip the '(' or ','
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200532 if (*argp == ')' || *argp == ',' || *argp == NUL)
533 break;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200534 if (eval1(&argp, &argvars[argcount], funcexe->evaluate) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200535 {
536 ret = FAIL;
537 break;
538 }
539 ++argcount;
540 if (*argp != ',')
541 break;
542 }
543 if (*argp == ')')
544 ++argp;
545 else
546 ret = FAIL;
547
548 if (ret == OK)
549 {
550 int i = 0;
551
552 if (get_vim_var_nr(VV_TESTING))
553 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100554 // Prepare for calling test_garbagecollect_now(), need to know
555 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200556 if (funcargs.ga_itemsize == 0)
557 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
558 for (i = 0; i < argcount; ++i)
559 if (ga_grow(&funcargs, 1) == OK)
560 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
561 &argvars[i];
562 }
563
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200564 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200565
566 funcargs.ga_len -= i;
567 }
568 else if (!aborting())
569 {
570 if (argcount == MAX_FUNC_ARGS)
571 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
572 else
573 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
574 }
575
576 while (--argcount >= 0)
577 clear_tv(&argvars[argcount]);
578
579 *arg = skipwhite(argp);
580 return ret;
581}
582
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200583/*
584 * Return TRUE if "p" starts with "<SID>" or "s:".
585 * Only works if eval_fname_script() returned non-zero for "p"!
586 */
587 static int
588eval_fname_sid(char_u *p)
589{
590 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
591}
592
593/*
594 * In a script change <SID>name() and s:name() to K_SNR 123_name().
595 * Change <SNR>123_name() to K_SNR 123_name().
596 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
597 * (slow).
598 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100599 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200600fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
601{
602 int llen;
603 char_u *fname;
604 int i;
605
606 llen = eval_fname_script(name);
607 if (llen > 0)
608 {
609 fname_buf[0] = K_SPECIAL;
610 fname_buf[1] = KS_EXTRA;
611 fname_buf[2] = (int)KE_SNR;
612 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100613 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200614 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200615 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100616 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200617 else
618 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200619 sprintf((char *)fname_buf + 3, "%ld_",
620 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200621 i = (int)STRLEN(fname_buf);
622 }
623 }
624 if (i + STRLEN(name + llen) < FLEN_FIXED)
625 {
626 STRCPY(fname_buf + i, name + llen);
627 fname = fname_buf;
628 }
629 else
630 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200631 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200632 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100633 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200634 else
635 {
636 *tofree = fname;
637 mch_memmove(fname, fname_buf, (size_t)i);
638 STRCPY(fname + i, name + llen);
639 }
640 }
641 }
642 else
643 fname = name;
644 return fname;
645}
646
647/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100648 * Find a function "name" in script "sid".
649 */
650 static ufunc_T *
651find_func_with_sid(char_u *name, int sid)
652{
653 hashitem_T *hi;
654 char_u buffer[200];
655
656 buffer[0] = K_SPECIAL;
657 buffer[1] = KS_EXTRA;
658 buffer[2] = (int)KE_SNR;
659 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
660 (long)sid, name);
661 hi = hash_find(&func_hashtab, buffer);
662 if (!HASHITEM_EMPTY(hi))
663 return HI2UF(hi);
664
665 return NULL;
666}
667
668/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200669 * Find a function by name, return pointer to it in ufuncs.
670 * Return NULL for unknown function.
671 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 static ufunc_T *
673find_func_even_dead(char_u *name, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200674{
675 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676 ufunc_T *func;
677 imported_T *imported;
678
679 if (in_vim9script())
680 {
681 // Find script-local function before global one.
682 func = find_func_with_sid(name, current_sctx.sc_sid);
683 if (func != NULL)
684 return func;
685
686 // Find imported funcion before global one.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +0100687 imported = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 if (imported != NULL && imported->imp_funcname != NULL)
689 {
690 hi = hash_find(&func_hashtab, imported->imp_funcname);
691 if (!HASHITEM_EMPTY(hi))
692 return HI2UF(hi);
693 }
694 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200695
696 hi = hash_find(&func_hashtab, name);
697 if (!HASHITEM_EMPTY(hi))
698 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100699
700 return NULL;
701}
702
703/*
704 * Find a function by name, return pointer to it in ufuncs.
705 * "cctx" is passed in a :def function to find imported functions.
706 * Return NULL for unknown or dead function.
707 */
708 ufunc_T *
709find_func(char_u *name, cctx_T *cctx)
710{
711 ufunc_T *fp = find_func_even_dead(name, cctx);
712
713 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
714 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200715 return NULL;
716}
717
718/*
719 * Copy the function name of "fp" to buffer "buf".
720 * "buf" must be able to hold the function name plus three bytes.
721 * Takes care of script-local function names.
722 */
723 static void
724cat_func_name(char_u *buf, ufunc_T *fp)
725{
726 if (fp->uf_name[0] == K_SPECIAL)
727 {
728 STRCPY(buf, "<SNR>");
729 STRCAT(buf, fp->uf_name + 3);
730 }
731 else
732 STRCPY(buf, fp->uf_name);
733}
734
735/*
736 * Add a number variable "name" to dict "dp" with value "nr".
737 */
738 static void
739add_nr_var(
740 dict_T *dp,
741 dictitem_T *v,
742 char *name,
743 varnumber_T nr)
744{
745 STRCPY(v->di_key, name);
746 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
747 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
748 v->di_tv.v_type = VAR_NUMBER;
749 v->di_tv.v_lock = VAR_FIXED;
750 v->di_tv.vval.v_number = nr;
751}
752
753/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100754 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200755 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100756 static void
757free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200758{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100759 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200760
761 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
762 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100763 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200764
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100765 // When garbage collecting a funccall_T may be freed before the
766 // function that references it, clear its uf_scoped field.
767 // The function may have been redefined and point to another
768 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200769 if (fp != NULL && fp->uf_scoped == fc)
770 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200771 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200772 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200773
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200774 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200775 vim_free(fc);
776}
777
778/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100779 * Free "fc" and what it contains.
780 * Can be called only when "fc" is kept beyond the period of it called,
781 * i.e. after cleanup_function_call(fc).
782 */
783 static void
784free_funccal_contents(funccall_T *fc)
785{
786 listitem_T *li;
787
788 // Free all l: variables.
789 vars_clear(&fc->l_vars.dv_hashtab);
790
791 // Free all a: variables.
792 vars_clear(&fc->l_avars.dv_hashtab);
793
794 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200795 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100796 clear_tv(&li->li_tv);
797
798 free_funccal(fc);
799}
800
801/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200802 * Handle the last part of returning from a function: free the local hashtable.
803 * Unless it is still in use by a closure.
804 */
805 static void
806cleanup_function_call(funccall_T *fc)
807{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100808 int may_free_fc = fc->fc_refcount <= 0;
809 int free_fc = TRUE;
810
Bram Moolenaar6914c642017-04-01 21:21:30 +0200811 current_funccal = fc->caller;
812
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100813 // Free all l: variables if not referred.
814 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
815 vars_clear(&fc->l_vars.dv_hashtab);
816 else
817 free_fc = FALSE;
818
819 // If the a:000 list and the l: and a: dicts are not referenced and
820 // there is no closure using it, we can free the funccall_T and what's
821 // in it.
822 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
823 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200824 else
825 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100826 int todo;
827 hashitem_T *hi;
828 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200829
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100830 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200831
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100832 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200833 todo = (int)fc->l_avars.dv_hashtab.ht_used;
834 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
835 {
836 if (!HASHITEM_EMPTY(hi))
837 {
838 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100839 di = HI2DI(hi);
840 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200841 }
842 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100843 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200844
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100845 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
846 fc->l_varlist.lv_first = NULL;
847 else
848 {
849 listitem_T *li;
850
851 free_fc = FALSE;
852
853 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200854 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +0200855 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100856 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100857
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100858 if (free_fc)
859 free_funccal(fc);
860 else
861 {
862 static int made_copy = 0;
863
864 // "fc" is still in use. This can happen when returning "a:000",
865 // assigning "l:" to a global variable or defining a closure.
866 // Link "fc" in the list for garbage collection later.
867 fc->caller = previous_funccal;
868 previous_funccal = fc;
869
870 if (want_garbage_collect)
871 // If garbage collector is ready, clear count.
872 made_copy = 0;
873 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100874 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100875 // We have made a lot of copies, worth 4 Mbyte. This can happen
876 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100877 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100878 // too much memory.
879 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100880 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100881 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200882 }
883}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884/*
885 * Unreference "fc": decrement the reference count and free it when it
886 * becomes zero. "fp" is detached from "fc".
887 * When "force" is TRUE we are exiting.
888 */
889 static void
890funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
891{
892 funccall_T **pfc;
893 int i;
894
895 if (fc == NULL)
896 return;
897
898 if (--fc->fc_refcount <= 0 && (force || (
899 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
900 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
901 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
902 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
903 {
904 if (fc == *pfc)
905 {
906 *pfc = fc->caller;
907 free_funccal_contents(fc);
908 return;
909 }
910 }
911 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
912 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
913 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
914}
915
916/*
917 * Remove the function from the function hashtable. If the function was
918 * deleted while it still has references this was already done.
919 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
920 */
921 static int
922func_remove(ufunc_T *fp)
923{
924 hashitem_T *hi;
925
926 // Return if it was already virtually deleted.
927 if (fp->uf_flags & FC_DEAD)
928 return FALSE;
929
930 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
931 if (!HASHITEM_EMPTY(hi))
932 {
933 // When there is a def-function index do not actually remove the
934 // function, so we can find the index when defining the function again.
935 if (fp->uf_dfunc_idx >= 0)
936 fp->uf_flags |= FC_DEAD;
937 else
938 hash_remove(&func_hashtab, hi);
939 return TRUE;
940 }
941 return FALSE;
942}
943
944 static void
945func_clear_items(ufunc_T *fp)
946{
947 ga_clear_strings(&(fp->uf_args));
948 ga_clear_strings(&(fp->uf_def_args));
949 ga_clear_strings(&(fp->uf_lines));
950 VIM_CLEAR(fp->uf_name_exp);
951 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100952 VIM_CLEAR(fp->uf_def_arg_idx);
953 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954 ga_clear(&fp->uf_type_list);
955#ifdef FEAT_PROFILE
956 VIM_CLEAR(fp->uf_tml_count);
957 VIM_CLEAR(fp->uf_tml_total);
958 VIM_CLEAR(fp->uf_tml_self);
959#endif
960}
961
962/*
963 * Free all things that a function contains. Does not free the function
964 * itself, use func_free() for that.
965 * When "force" is TRUE we are exiting.
966 */
967 static void
968func_clear(ufunc_T *fp, int force)
969{
970 if (fp->uf_cleared)
971 return;
972 fp->uf_cleared = TRUE;
973
974 // clear this function
975 func_clear_items(fp);
976 funccal_unref(fp->uf_scoped, fp, force);
977 delete_def_function(fp);
978}
979
980/*
981 * Free a function and remove it from the list of functions. Does not free
982 * what a function contains, call func_clear() first.
983 */
984 static void
985func_free(ufunc_T *fp)
986{
987 // Only remove it when not done already, otherwise we would remove a newer
988 // version of the function with the same name.
989 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
990 func_remove(fp);
991
992 if ((fp->uf_flags & FC_DEAD) == 0)
993 vim_free(fp);
994}
995
996/*
997 * Free all things that a function contains and free the function itself.
998 * When "force" is TRUE we are exiting.
999 */
1000 static void
1001func_clear_free(ufunc_T *fp, int force)
1002{
1003 func_clear(fp, force);
1004 func_free(fp);
1005}
1006
Bram Moolenaar6914c642017-04-01 21:21:30 +02001007
1008/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001009 * Call a user function.
1010 */
1011 static void
1012call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001013 ufunc_T *fp, // pointer to function
1014 int argcount, // nr of args
1015 typval_T *argvars, // arguments
1016 typval_T *rettv, // return value
1017 linenr_T firstline, // first line of range
1018 linenr_T lastline, // last line of range
1019 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001020{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001021 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001022 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001023 funccall_T *fc;
1024 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001025 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001026 static int depth = 0;
1027 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001028 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001029 int i;
1030 int ai;
1031 int islambda = FALSE;
1032 char_u numbuf[NUMBUFLEN];
1033 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001034#ifdef FEAT_PROFILE
1035 proftime_T wait_start;
1036 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001037 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001038#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001039 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001040
Bram Moolenaare38eab22019-12-05 21:50:01 +01001041 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001042 if (depth >= p_mfd)
1043 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001044 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001045 rettv->v_type = VAR_NUMBER;
1046 rettv->vval.v_number = -1;
1047 return;
1048 }
1049 ++depth;
1050
Bram Moolenaare38eab22019-12-05 21:50:01 +01001051 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001052
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001053 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001054 if (fc == NULL)
1055 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001056 fc->caller = current_funccal;
1057 current_funccal = fc;
1058 fc->func = fp;
1059 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001060 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001061 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001062 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1063 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001064 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001065 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001066 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001067
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 if (fp->uf_dfunc_idx >= 0)
1069 {
1070 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001071 save_current_sctx = current_sctx;
1072 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073
1074 // Execute the compiled function.
1075 call_def_function(fp, argcount, argvars, rettv);
1076 --depth;
1077 current_funccal = fc->caller;
1078
1079 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001080 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 free_funccal(fc);
1082 return;
1083 }
1084
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001085 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1086 islambda = TRUE;
1087
1088 /*
1089 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1090 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1091 * each argument variable and saves a lot of time.
1092 */
1093 /*
1094 * Init l: variables.
1095 */
1096 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1097 if (selfdict != NULL)
1098 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001099 // Set l:self to "selfdict". Use "name" to avoid a warning from
1100 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001101 v = &fc->fixvar[fixvar_idx++].var;
1102 name = v->di_key;
1103 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001104 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001105 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1106 v->di_tv.v_type = VAR_DICT;
1107 v->di_tv.v_lock = 0;
1108 v->di_tv.vval.v_dict = selfdict;
1109 ++selfdict->dv_refcount;
1110 }
1111
1112 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001113 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001114 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001115 * Set a:000 to a list with room for the "..." arguments.
1116 */
1117 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001118 if ((fp->uf_flags & FC_NOARGS) == 0)
1119 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001120 (varnumber_T)(argcount >= fp->uf_args.ga_len
1121 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001122 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001123 if ((fp->uf_flags & FC_NOARGS) == 0)
1124 {
1125 // Use "name" to avoid a warning from some compiler that checks the
1126 // destination size.
1127 v = &fc->fixvar[fixvar_idx++].var;
1128 name = v->di_key;
1129 STRCPY(name, "000");
1130 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1131 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1132 v->di_tv.v_type = VAR_LIST;
1133 v->di_tv.v_lock = VAR_FIXED;
1134 v->di_tv.vval.v_list = &fc->l_varlist;
1135 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001136 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
1137 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1138 fc->l_varlist.lv_lock = VAR_FIXED;
1139
1140 /*
1141 * Set a:firstline to "firstline" and a:lastline to "lastline".
1142 * Set a:name to named arguments.
1143 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001144 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001145 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001146 if ((fp->uf_flags & FC_NOARGS) == 0)
1147 {
1148 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001149 (varnumber_T)firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001150 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001151 (varnumber_T)lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001152 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001153 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001154 {
1155 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001156 typval_T def_rettv;
1157 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001158
1159 ai = i - fp->uf_args.ga_len;
1160 if (ai < 0)
1161 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001162 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001163 name = FUNCARG(fp, i);
1164 if (islambda)
1165 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001166
1167 // evaluate named argument default expression
1168 isdefault = ai + fp->uf_def_args.ga_len >= 0
1169 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1170 && argvars[i].vval.v_number == VVAL_NONE));
1171 if (isdefault)
1172 {
1173 char_u *default_expr = NULL;
1174 def_rettv.v_type = VAR_NUMBER;
1175 def_rettv.vval.v_number = -1;
1176
1177 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1178 [ai + fp->uf_def_args.ga_len];
1179 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL)
1180 {
1181 default_arg_err = 1;
1182 break;
1183 }
1184 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001185 }
1186 else
1187 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001188 if ((fp->uf_flags & FC_NOARGS) != 0)
1189 // Bail out if no a: arguments used (in lambda).
1190 break;
1191
Bram Moolenaare38eab22019-12-05 21:50:01 +01001192 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001193 sprintf((char *)numbuf, "%d", ai + 1);
1194 name = numbuf;
1195 }
1196 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1197 {
1198 v = &fc->fixvar[fixvar_idx++].var;
1199 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001200 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001201 }
1202 else
1203 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001204 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001205 if (v == NULL)
1206 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001207 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001208 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001209
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001210 // Note: the values are copied directly to avoid alloc/free.
1211 // "argvars" must have VAR_FIXED for v_lock.
1212 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001213 v->di_tv.v_lock = VAR_FIXED;
1214
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001215 if (addlocal)
1216 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001217 // Named arguments should be accessed without the "a:" prefix in
1218 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001219 copy_tv(&v->di_tv, &v->di_tv);
1220 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001221 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001222 else
1223 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001224
1225 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1226 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001227 listitem_T *li = &fc->l_listitems[ai];
1228
1229 li->li_tv = argvars[i];
1230 li->li_tv.v_lock = VAR_FIXED;
1231 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001232 }
1233 }
1234
Bram Moolenaare38eab22019-12-05 21:50:01 +01001235 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001236 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001237
1238 if (fp->uf_flags & FC_SANDBOX)
1239 {
1240 using_sandbox = TRUE;
1241 ++sandbox;
1242 }
1243
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001244 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001245 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001246 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001247 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001248 ++no_wait_return;
1249 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001250
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001251 smsg(_("calling %s"), SOURCING_NAME);
1252 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001253 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001254 char_u buf[MSG_BUF_LEN];
1255 char_u numbuf2[NUMBUFLEN];
1256 char_u *tofree;
1257 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001258
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001259 msg_puts("(");
1260 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001261 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001262 if (i > 0)
1263 msg_puts(", ");
1264 if (argvars[i].v_type == VAR_NUMBER)
1265 msg_outnum((long)argvars[i].vval.v_number);
1266 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001267 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001268 // Do not want errors such as E724 here.
1269 ++emsg_off;
1270 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1271 --emsg_off;
1272 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001273 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001274 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001275 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001276 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1277 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001278 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001279 msg_puts((char *)s);
1280 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001281 }
1282 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001283 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001284 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001285 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001286 msg_puts("\n"); // don't overwrite this either
1287
1288 verbose_leave_scroll();
1289 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001290 }
1291#ifdef FEAT_PROFILE
1292 if (do_profiling == PROF_YES)
1293 {
1294 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001295 {
1296 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001297 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001298 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001299 if (fp->uf_profiling
1300 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1301 {
1302 ++fp->uf_tm_count;
1303 profile_start(&call_start);
1304 profile_zero(&fp->uf_tm_children);
1305 }
1306 script_prof_save(&wait_start);
1307 }
1308#endif
1309
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001310 save_current_sctx = current_sctx;
1311 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001312 save_did_emsg = did_emsg;
1313 did_emsg = FALSE;
1314
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001315 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1316 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001317 else if (islambda)
1318 {
1319 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1320
1321 // A Lambda always has the command "return {expr}". It is much faster
1322 // to evaluate {expr} directly.
1323 ++ex_nesting_level;
Bram Moolenaard1e9dc22020-04-03 18:13:57 +02001324 (void)eval1(&p, rettv, TRUE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001325 --ex_nesting_level;
1326 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001327 else
1328 // call do_cmdline() to execute the lines
1329 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001330 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1331
1332 --RedrawingDisabled;
1333
Bram Moolenaare38eab22019-12-05 21:50:01 +01001334 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001335 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1336 {
1337 clear_tv(rettv);
1338 rettv->v_type = VAR_NUMBER;
1339 rettv->vval.v_number = -1;
1340 }
1341
1342#ifdef FEAT_PROFILE
1343 if (do_profiling == PROF_YES && (fp->uf_profiling
1344 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1345 {
1346 profile_end(&call_start);
1347 profile_sub_wait(&wait_start, &call_start);
1348 profile_add(&fp->uf_tm_total, &call_start);
1349 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1350 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1351 {
1352 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1353 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1354 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001355 if (started_profiling)
1356 // make a ":profdel func" stop profiling the function
1357 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001358 }
1359#endif
1360
Bram Moolenaare38eab22019-12-05 21:50:01 +01001361 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001362 if (p_verbose >= 12)
1363 {
1364 ++no_wait_return;
1365 verbose_enter_scroll();
1366
1367 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001368 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001369 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001370 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001371 (long)fc->rettv->vval.v_number);
1372 else
1373 {
1374 char_u buf[MSG_BUF_LEN];
1375 char_u numbuf2[NUMBUFLEN];
1376 char_u *tofree;
1377 char_u *s;
1378
Bram Moolenaare38eab22019-12-05 21:50:01 +01001379 // The value may be very long. Skip the middle part, so that we
1380 // have some idea how it starts and ends. smsg() would always
1381 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001382 ++emsg_off;
1383 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1384 --emsg_off;
1385 if (s != NULL)
1386 {
1387 if (vim_strsize(s) > MSG_BUF_CLEN)
1388 {
1389 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1390 s = buf;
1391 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001392 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001393 vim_free(tofree);
1394 }
1395 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001396 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001397
1398 verbose_leave_scroll();
1399 --no_wait_return;
1400 }
1401
Bram Moolenaare31ee862020-01-07 20:59:34 +01001402 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001403 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001404 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001405#ifdef FEAT_PROFILE
1406 if (do_profiling == PROF_YES)
1407 script_prof_restore(&wait_start);
1408#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001409 if (using_sandbox)
1410 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001411
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001412 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001413 {
1414 ++no_wait_return;
1415 verbose_enter_scroll();
1416
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001417 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001418 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001419
1420 verbose_leave_scroll();
1421 --no_wait_return;
1422 }
1423
1424 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001425 --depth;
1426
Bram Moolenaar6914c642017-04-01 21:21:30 +02001427 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001428}
1429
1430/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001432 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 int
1434call_user_func_check(
1435 ufunc_T *fp,
1436 int argcount,
1437 typval_T *argvars,
1438 typval_T *rettv,
1439 funcexe_T *funcexe,
1440 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001441{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 int error;
1443 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001444
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1446 *funcexe->doesrange = TRUE;
1447 if (argcount < regular_args - fp->uf_def_args.ga_len)
1448 error = FCERR_TOOFEW;
1449 else if (!has_varargs(fp) && argcount > regular_args)
1450 error = FCERR_TOOMANY;
1451 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1452 error = FCERR_DICT;
1453 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001454 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 int did_save_redo = FALSE;
1456 save_redo_T save_redo;
1457
1458 /*
1459 * Call the user function.
1460 * Save and restore search patterns, script variables and
1461 * redo buffer.
1462 */
1463 save_search_patterns();
1464 if (!ins_compl_active())
1465 {
1466 saveRedobuff(&save_redo);
1467 did_save_redo = TRUE;
1468 }
1469 ++fp->uf_calls;
1470 call_user_func(fp, argcount, argvars, rettv,
1471 funcexe->firstline, funcexe->lastline,
1472 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
1473 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1474 // Function was unreferenced while being used, free it now.
1475 func_clear_free(fp, FALSE);
1476 if (did_save_redo)
1477 restoreRedobuff(&save_redo);
1478 restore_search_patterns();
1479 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001482}
1483
1484/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001485 * There are two kinds of function names:
1486 * 1. ordinary names, function defined with :function
1487 * 2. numbered functions and lambdas
1488 * For the first we only count the name stored in func_hashtab as a reference,
1489 * using function() does not count as a reference, because the function is
1490 * looked up by name.
1491 */
1492 static int
1493func_name_refcount(char_u *name)
1494{
1495 return isdigit(*name) || *name == '<';
1496}
1497
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001498static funccal_entry_T *funccal_stack = NULL;
1499
1500/*
1501 * Save the current function call pointer, and set it to NULL.
1502 * Used when executing autocommands and for ":source".
1503 */
1504 void
1505save_funccal(funccal_entry_T *entry)
1506{
1507 entry->top_funccal = current_funccal;
1508 entry->next = funccal_stack;
1509 funccal_stack = entry;
1510 current_funccal = NULL;
1511}
1512
1513 void
1514restore_funccal(void)
1515{
1516 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001517 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001518 else
1519 {
1520 current_funccal = funccal_stack->top_funccal;
1521 funccal_stack = funccal_stack->next;
1522 }
1523}
1524
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001525 funccall_T *
1526get_current_funccal(void)
1527{
1528 return current_funccal;
1529}
1530
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001531#if defined(EXITFREE) || defined(PROTO)
1532 void
1533free_all_functions(void)
1534{
1535 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001536 ufunc_T *fp;
1537 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001538 long_u todo = 1;
1539 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001540
Bram Moolenaare38eab22019-12-05 21:50:01 +01001541 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001542 while (current_funccal != NULL)
1543 {
1544 clear_tv(current_funccal->rettv);
1545 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001546 if (current_funccal == NULL && funccal_stack != NULL)
1547 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001548 }
1549
Bram Moolenaare38eab22019-12-05 21:50:01 +01001550 // First clear what the functions contain. Since this may lower the
1551 // reference count of a function, it may also free a function and change
1552 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001553 while (todo > 0)
1554 {
1555 todo = func_hashtab.ht_used;
1556 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1557 if (!HASHITEM_EMPTY(hi))
1558 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 // clear the def function index now
1560 fp = HI2UF(hi);
1561 fp->uf_flags &= ~FC_DEAD;
1562 fp->uf_dfunc_idx = -1;
1563
Bram Moolenaare38eab22019-12-05 21:50:01 +01001564 // Only free functions that are not refcounted, those are
1565 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001566 if (func_name_refcount(fp->uf_name))
1567 ++skipped;
1568 else
1569 {
1570 used = func_hashtab.ht_used;
1571 func_clear(fp, TRUE);
1572 if (used != func_hashtab.ht_used)
1573 {
1574 skipped = 0;
1575 break;
1576 }
1577 }
1578 --todo;
1579 }
1580 }
1581
Bram Moolenaare38eab22019-12-05 21:50:01 +01001582 // Now actually free the functions. Need to start all over every time,
1583 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001584 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001585 while (func_hashtab.ht_used > skipped)
1586 {
1587 todo = func_hashtab.ht_used;
1588 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001589 if (!HASHITEM_EMPTY(hi))
1590 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001591 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001592 // Only free functions that are not refcounted, those are
1593 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001594 fp = HI2UF(hi);
1595 if (func_name_refcount(fp->uf_name))
1596 ++skipped;
1597 else
1598 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001599 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001600 skipped = 0;
1601 break;
1602 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001603 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001604 }
1605 if (skipped == 0)
1606 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607
1608 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001609}
1610#endif
1611
1612/*
1613 * Return TRUE if "name" looks like a builtin function name: starts with a
1614 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1615 * "len" is the length of "name", or -1 for NUL terminated.
1616 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001618builtin_function(char_u *name, int len)
1619{
1620 char_u *p;
1621
1622 if (!ASCII_ISLOWER(name[0]))
1623 return FALSE;
1624 p = vim_strchr(name, AUTOLOAD_CHAR);
1625 return p == NULL || (len > 0 && p > name + len);
1626}
1627
1628 int
1629func_call(
1630 char_u *name,
1631 typval_T *args,
1632 partial_T *partial,
1633 dict_T *selfdict,
1634 typval_T *rettv)
1635{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001636 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001637 listitem_T *item;
1638 typval_T argv[MAX_FUNC_ARGS + 1];
1639 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640 int r = 0;
1641
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001642 range_list_materialize(l);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001643 FOR_ALL_LIST_ITEMS(args->vval.v_list, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001644 {
1645 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1646 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001647 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001648 break;
1649 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001650 // Make a copy of each argument. This is needed to be able to set
1651 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001652 copy_tv(&item->li_tv, &argv[argc++]);
1653 }
1654
1655 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001656 {
1657 funcexe_T funcexe;
1658
Bram Moolenaarac92e252019-08-03 21:58:38 +02001659 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001660 funcexe.firstline = curwin->w_cursor.lnum;
1661 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001662 funcexe.evaluate = TRUE;
1663 funcexe.partial = partial;
1664 funcexe.selfdict = selfdict;
1665 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1666 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001667
Bram Moolenaare38eab22019-12-05 21:50:01 +01001668 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001669 while (argc > 0)
1670 clear_tv(&argv[--argc]);
1671
1672 return r;
1673}
1674
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001675static int callback_depth = 0;
1676
1677 int
1678get_callback_depth(void)
1679{
1680 return callback_depth;
1681}
1682
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001683/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001684 * Invoke call_func() with a callback.
1685 */
1686 int
1687call_callback(
1688 callback_T *callback,
1689 int len, // length of "name" or -1 to use strlen()
1690 typval_T *rettv, // return value goes here
1691 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001692 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001693 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001694{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001695 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001696 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001697
1698 vim_memset(&funcexe, 0, sizeof(funcexe));
1699 funcexe.evaluate = TRUE;
1700 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001701 ++callback_depth;
1702 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1703 --callback_depth;
1704 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001705}
1706
1707/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 * Give an error message for the result of a function.
1709 * Nothing if "error" is FCERR_NONE.
1710 */
1711 void
1712user_func_error(int error, char_u *name)
1713{
1714 switch (error)
1715 {
1716 case FCERR_UNKNOWN:
1717 emsg_funcname(e_unknownfunc, name);
1718 break;
1719 case FCERR_NOTMETHOD:
1720 emsg_funcname(
1721 N_("E276: Cannot use function as a method: %s"), name);
1722 break;
1723 case FCERR_DELETED:
1724 emsg_funcname(N_(e_func_deleted), name);
1725 break;
1726 case FCERR_TOOMANY:
1727 emsg_funcname((char *)e_toomanyarg, name);
1728 break;
1729 case FCERR_TOOFEW:
1730 emsg_funcname((char *)e_toofewarg, name);
1731 break;
1732 case FCERR_SCRIPT:
1733 emsg_funcname(
1734 N_("E120: Using <SID> not in a script context: %s"), name);
1735 break;
1736 case FCERR_DICT:
1737 emsg_funcname(
1738 N_("E725: Calling dict function without Dictionary: %s"),
1739 name);
1740 break;
1741 }
1742}
1743
1744/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001745 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001746 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001747 * Return FAIL when the function can't be called, OK otherwise.
1748 * Also returns OK when an error was encountered while executing the function.
1749 */
1750 int
1751call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001752 char_u *funcname, // name of the function
1753 int len, // length of "name" or -1 to use strlen()
1754 typval_T *rettv, // return value goes here
1755 int argcount_in, // number of "argvars"
1756 typval_T *argvars_in, // vars for arguments, must have "argcount"
1757 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001758 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001759{
1760 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001761 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001762 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001763 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001764 char_u fname_buf[FLEN_FIXED + 1];
1765 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001766 char_u *fname = NULL;
1767 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001768 int argcount = argcount_in;
1769 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001770 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001771 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1772 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001773 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001774 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001775 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001776
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001777 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1778 // even when call_func() returns FAIL.
1779 rettv->v_type = VAR_UNKNOWN;
1780
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001781 if (partial != NULL)
1782 fp = partial->pt_func;
1783 if (fp == NULL)
1784 {
1785 // Make a copy of the name, if it comes from a funcref variable it
1786 // could be changed or deleted in the called function.
1787 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
1788 if (name == NULL)
1789 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001790
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001791 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1792 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001793
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001794 if (funcexe->doesrange != NULL)
1795 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001796
1797 if (partial != NULL)
1798 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001799 // When the function has a partial with a dict and there is a dict
1800 // argument, use the dict argument. That is backwards compatible.
1801 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001802 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001803 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001804 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 {
1806 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001807 {
1808 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1809 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001810 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001811 goto theend;
1812 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001813 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001814 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001815 for (i = 0; i < argcount_in; ++i)
1816 argv[i + argv_clear] = argvars_in[i];
1817 argvars = argv;
1818 argcount = partial->pt_argc + argcount_in;
1819 }
1820 }
1821
Bram Moolenaaref140542019-12-31 21:27:13 +01001822 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001823 {
1824 char_u *rfname = fname;
1825
Bram Moolenaare38eab22019-12-05 21:50:01 +01001826 // Ignore "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001827 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001828 rfname = fname + 2;
1829
Bram Moolenaare38eab22019-12-05 21:50:01 +01001830 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001831 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001832 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001833
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001834 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001835 {
1836 /*
1837 * User defined function.
1838 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001839 if (fp == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001841
Bram Moolenaare38eab22019-12-05 21:50:01 +01001842 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001843 if (fp == NULL
1844 && apply_autocmds(EVENT_FUNCUNDEFINED,
1845 rfname, rfname, TRUE, NULL)
1846 && !aborting())
1847 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001848 // executed an autocommand, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001850 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001851 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001852 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1853 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001854 // loaded a package, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001856 }
1857
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001858 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01001859 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001860 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001861 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001862 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001863 // postponed filling in the arguments, do it now
1864 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001865 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001866
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001867 if (funcexe->basetv != NULL)
1868 {
1869 // Method call: base->Method()
1870 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1871 argv[0] = *funcexe->basetv;
1872 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001873 argvars = argv;
1874 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001875 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 error = call_user_func_check(fp, argcount, argvars, rettv,
1878 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001879 }
1880 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001881 else if (funcexe->basetv != NULL)
1882 {
1883 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001884 * expr->method(): Find the method name in the table, call its
1885 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02001886 */
1887 error = call_internal_method(fname, argcount, argvars, rettv,
1888 funcexe->basetv);
1889 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001890 else
1891 {
1892 /*
1893 * Find the function name in the table, call its implementation.
1894 */
1895 error = call_internal_func(fname, argcount, argvars, rettv);
1896 }
1897 /*
1898 * The function call (or "FuncUndefined" autocommand sequence) might
1899 * have been aborted by an error, an interrupt, or an explicitly thrown
1900 * exception that has not been caught so far. This situation can be
1901 * tested for by calling aborting(). For an error in an internal
1902 * function or for the "E132" error in call_user_func(), however, the
1903 * throw point at which the "force_abort" flag (temporarily reset by
1904 * emsg()) is normally updated has not been reached yet. We need to
1905 * update that flag first to make aborting() reliable.
1906 */
1907 update_force_abort();
1908 }
Bram Moolenaaref140542019-12-31 21:27:13 +01001909 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001910 ret = OK;
1911
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001912theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001913 /*
1914 * Report an error unless the argument evaluation or function call has been
1915 * cancelled due to an aborting error, an interrupt, or an exception.
1916 */
1917 if (!aborting())
1918 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001919 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001920 }
1921
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001922 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001923 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001924 clear_tv(&argv[--argv_clear + argv_base]);
1925
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001926 vim_free(tofree);
1927 vim_free(name);
1928
1929 return ret;
1930}
1931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 static char_u *
1933printable_func_name(ufunc_T *fp)
1934{
1935 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
1936}
1937
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001938/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001939 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001940 */
1941 static void
1942list_func_head(ufunc_T *fp, int indent)
1943{
1944 int j;
1945
1946 msg_start();
1947 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001948 msg_puts(" ");
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001949 if (fp->uf_dfunc_idx >= 0)
1950 msg_puts("def ");
1951 else
1952 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001954 msg_putchar('(');
1955 for (j = 0; j < fp->uf_args.ga_len; ++j)
1956 {
1957 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001958 msg_puts(", ");
1959 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 if (fp->uf_arg_types != NULL)
1961 {
1962 char *tofree;
1963
1964 msg_puts(": ");
1965 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
1966 vim_free(tofree);
1967 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001968 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
1969 {
1970 msg_puts(" = ");
1971 msg_puts(((char **)(fp->uf_def_args.ga_data))
1972 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
1973 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001974 }
1975 if (fp->uf_varargs)
1976 {
1977 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001978 msg_puts(", ");
1979 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 if (fp->uf_va_name != NULL)
1982 {
1983 if (j)
1984 msg_puts(", ");
1985 msg_puts("...");
1986 msg_puts((char *)fp->uf_va_name);
1987 if (fp->uf_va_type)
1988 {
1989 char *tofree;
1990
1991 msg_puts(": ");
1992 msg_puts(type_name(fp->uf_va_type, &tofree));
1993 vim_free(tofree);
1994 }
1995 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001996 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001997
1998 if (fp->uf_dfunc_idx >= 0)
1999 {
2000 if (fp->uf_ret_type != &t_void)
2001 {
2002 char *tofree;
2003
2004 msg_puts(": ");
2005 msg_puts(type_name(fp->uf_ret_type, &tofree));
2006 vim_free(tofree);
2007 }
2008 }
2009 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002010 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002011 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002012 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002013 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002014 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002015 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002016 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002017 msg_clr_eos();
2018 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002019 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002020}
2021
2022/*
2023 * Get a function name, translating "<SID>" and "<SNR>".
2024 * Also handles a Funcref in a List or Dictionary.
2025 * Returns the function name in allocated memory, or NULL for failure.
2026 * flags:
2027 * TFN_INT: internal function name OK
2028 * TFN_QUIET: be quiet
2029 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002030 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002031 * Advances "pp" to just after the function name (if no error).
2032 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002033 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002034trans_function_name(
2035 char_u **pp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002036 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002037 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002038 funcdict_T *fdp, // return: info about dictionary used
2039 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002040{
2041 char_u *name = NULL;
2042 char_u *start;
2043 char_u *end;
2044 int lead;
2045 char_u sid_buf[20];
2046 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002048 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002050
2051 if (fdp != NULL)
2052 vim_memset(fdp, 0, sizeof(funcdict_T));
2053 start = *pp;
2054
Bram Moolenaare38eab22019-12-05 21:50:01 +01002055 // Check for hard coded <SNR>: already translated function ID (from a user
2056 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002057 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2058 && (*pp)[2] == (int)KE_SNR)
2059 {
2060 *pp += 3;
2061 len = get_id_len(pp) + 3;
2062 return vim_strnsave(start, len);
2063 }
2064
Bram Moolenaare38eab22019-12-05 21:50:01 +01002065 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2066 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002067 lead = eval_fname_script(start);
2068 if (lead > 2)
2069 start += lead;
2070
Bram Moolenaare38eab22019-12-05 21:50:01 +01002071 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002072 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002073 lead > 2 ? 0 : FNE_CHECK_START);
2074 if (end == start)
2075 {
2076 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002077 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002078 goto theend;
2079 }
2080 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2081 {
2082 /*
2083 * Report an invalid expression in braces, unless the expression
2084 * evaluation has been cancelled due to an aborting error, an
2085 * interrupt, or an exception.
2086 */
2087 if (!aborting())
2088 {
2089 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002090 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002091 }
2092 else
2093 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2094 goto theend;
2095 }
2096
2097 if (lv.ll_tv != NULL)
2098 {
2099 if (fdp != NULL)
2100 {
2101 fdp->fd_dict = lv.ll_dict;
2102 fdp->fd_newkey = lv.ll_newkey;
2103 lv.ll_newkey = NULL;
2104 fdp->fd_di = lv.ll_di;
2105 }
2106 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2107 {
2108 name = vim_strsave(lv.ll_tv->vval.v_string);
2109 *pp = end;
2110 }
2111 else if (lv.ll_tv->v_type == VAR_PARTIAL
2112 && lv.ll_tv->vval.v_partial != NULL)
2113 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002114 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002115 *pp = end;
2116 if (partial != NULL)
2117 *partial = lv.ll_tv->vval.v_partial;
2118 }
2119 else
2120 {
2121 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2122 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002123 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002124 else
2125 *pp = end;
2126 name = NULL;
2127 }
2128 goto theend;
2129 }
2130
2131 if (lv.ll_name == NULL)
2132 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002133 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002134 *pp = end;
2135 goto theend;
2136 }
2137
Bram Moolenaare38eab22019-12-05 21:50:01 +01002138 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002139 if (lv.ll_exp_name != NULL)
2140 {
2141 len = (int)STRLEN(lv.ll_exp_name);
2142 name = deref_func_name(lv.ll_exp_name, &len, partial,
2143 flags & TFN_NO_AUTOLOAD);
2144 if (name == lv.ll_exp_name)
2145 name = NULL;
2146 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002147 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002148 {
2149 len = (int)(end - *pp);
2150 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2151 if (name == *pp)
2152 name = NULL;
2153 }
2154 if (name != NULL)
2155 {
2156 name = vim_strsave(name);
2157 *pp = end;
2158 if (STRNCMP(name, "<SNR>", 5) == 0)
2159 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002160 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002161 name[0] = K_SPECIAL;
2162 name[1] = KS_EXTRA;
2163 name[2] = (int)KE_SNR;
2164 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2165 }
2166 goto theend;
2167 }
2168
2169 if (lv.ll_exp_name != NULL)
2170 {
2171 len = (int)STRLEN(lv.ll_exp_name);
2172 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2173 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2174 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002175 // When there was "s:" already or the name expanded to get a
2176 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002177 lv.ll_name += 2;
2178 len -= 2;
2179 lead = 2;
2180 }
2181 }
2182 else
2183 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002184 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002185 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
2186 lv.ll_name += 2;
2187 len = (int)(end - lv.ll_name);
2188 }
2189
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 // In Vim9 script a user function is script-local by default.
2191 vim9script = ASCII_ISUPPER(*start)
2192 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2193
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002194 /*
2195 * Copy the function name to allocated memory.
2196 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2197 * Accept <SNR>123_name() outside a script.
2198 */
2199 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002200 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002202 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002203 if (!vim9script)
2204 lead = 3;
2205 if (vim9script || (lv.ll_exp_name != NULL
2206 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002207 || eval_fname_sid(*pp))
2208 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002210 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002212 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002213 goto theend;
2214 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002215 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216 if (vim9script)
2217 extra = 3 + (int)STRLEN(sid_buf);
2218 else
2219 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002220 }
2221 }
2222 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2223 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002224 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002225 start);
2226 goto theend;
2227 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002228 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002229 {
2230 char_u *cp = vim_strchr(lv.ll_name, ':');
2231
2232 if (cp != NULL && cp < end)
2233 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002234 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002235 goto theend;
2236 }
2237 }
2238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002240 if (name != NULL)
2241 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002242 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002243 {
2244 name[0] = K_SPECIAL;
2245 name[1] = KS_EXTRA;
2246 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002248 STRCPY(name + 3, sid_buf);
2249 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2251 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002252 }
2253 *pp = end;
2254
2255theend:
2256 clear_lval(&lv);
2257 return name;
2258}
2259
2260/*
2261 * ":function"
2262 */
2263 void
2264ex_function(exarg_T *eap)
2265{
2266 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002267 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002268 int j;
2269 int c;
2270 int saved_did_emsg;
2271 int saved_wait_return = need_wait_return;
2272 char_u *name = NULL;
2273 char_u *p;
2274 char_u *arg;
2275 char_u *line_arg = NULL;
2276 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002278 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002279 garray_T newlines;
2280 int varargs = FALSE;
2281 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002283 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002284 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002285 int indent;
2286 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287#define MAX_FUNC_NESTING 50
2288 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002289 dictitem_T *v;
2290 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002291 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002292 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002293 int todo;
2294 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002295 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002296 linenr_T sourcing_lnum_off;
2297 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002298 int is_heredoc = FALSE;
2299 char_u *skip_until = NULL;
2300 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002301
2302 /*
2303 * ":function" without argument: list functions.
2304 */
2305 if (ends_excmd(*eap->arg))
2306 {
2307 if (!eap->skip)
2308 {
2309 todo = (int)func_hashtab.ht_used;
2310 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2311 {
2312 if (!HASHITEM_EMPTY(hi))
2313 {
2314 --todo;
2315 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 if ((fp->uf_flags & FC_DEAD)
2317 || message_filtered(fp->uf_name))
Bram Moolenaarf86db782018-10-25 13:31:37 +02002318 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002319 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002320 list_func_head(fp, FALSE);
2321 }
2322 }
2323 }
2324 eap->nextcmd = check_nextcmd(eap->arg);
2325 return;
2326 }
2327
2328 /*
2329 * ":function /pat": list functions matching pattern.
2330 */
2331 if (*eap->arg == '/')
2332 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002333 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002334 if (!eap->skip)
2335 {
2336 regmatch_T regmatch;
2337
2338 c = *p;
2339 *p = NUL;
2340 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2341 *p = c;
2342 if (regmatch.regprog != NULL)
2343 {
2344 regmatch.rm_ic = p_ic;
2345
2346 todo = (int)func_hashtab.ht_used;
2347 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2348 {
2349 if (!HASHITEM_EMPTY(hi))
2350 {
2351 --todo;
2352 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 if ((fp->uf_flags & FC_DEAD) == 0
2354 && !isdigit(*fp->uf_name)
2355 && vim_regexec(&regmatch, fp->uf_name, 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002356 list_func_head(fp, FALSE);
2357 }
2358 }
2359 vim_regfree(regmatch.regprog);
2360 }
2361 }
2362 if (*p == '/')
2363 ++p;
2364 eap->nextcmd = check_nextcmd(p);
2365 return;
2366 }
2367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 ga_init(&newargs);
2369 ga_init(&argtypes);
2370 ga_init(&default_args);
2371
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002372 /*
2373 * Get the function name. There are these situations:
2374 * func normal function name
2375 * "name" == func, "fudi.fd_dict" == NULL
2376 * dict.func new dictionary entry
2377 * "name" == NULL, "fudi.fd_dict" set,
2378 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2379 * dict.func existing dict entry with a Funcref
2380 * "name" == func, "fudi.fd_dict" set,
2381 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2382 * dict.func existing dict entry that's not a Funcref
2383 * "name" == NULL, "fudi.fd_dict" set,
2384 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2385 * s:func script-local function name
2386 * g:func global function name, same as "func"
2387 */
2388 p = eap->arg;
Bram Moolenaar3388d332017-12-07 22:23:04 +01002389 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002390 paren = (vim_strchr(p, '(') != NULL);
2391 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2392 {
2393 /*
2394 * Return on an invalid expression in braces, unless the expression
2395 * evaluation has been cancelled due to an aborting error, an
2396 * interrupt, or an exception.
2397 */
2398 if (!aborting())
2399 {
2400 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002401 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002402 vim_free(fudi.fd_newkey);
2403 return;
2404 }
2405 else
2406 eap->skip = TRUE;
2407 }
2408
Bram Moolenaare38eab22019-12-05 21:50:01 +01002409 // An error in a function call during evaluation of an expression in magic
2410 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002411 saved_did_emsg = did_emsg;
2412 did_emsg = FALSE;
2413
2414 /*
2415 * ":function func" with only function name: list function.
2416 */
2417 if (!paren)
2418 {
2419 if (!ends_excmd(*skipwhite(p)))
2420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002421 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002422 goto ret_free;
2423 }
2424 eap->nextcmd = check_nextcmd(p);
2425 if (eap->nextcmd != NULL)
2426 *p = NUL;
2427 if (!eap->skip && !got_int)
2428 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002430 if (fp != NULL)
2431 {
2432 list_func_head(fp, TRUE);
2433 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2434 {
2435 if (FUNCLINE(fp, j) == NULL)
2436 continue;
2437 msg_putchar('\n');
2438 msg_outnum((long)(j + 1));
2439 if (j < 9)
2440 msg_putchar(' ');
2441 if (j < 99)
2442 msg_putchar(' ');
2443 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002444 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002445 ui_breakcheck();
2446 }
2447 if (!got_int)
2448 {
2449 msg_putchar('\n');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 if (fp->uf_dfunc_idx >= 0)
2451 msg_puts(" enddef");
2452 else
2453 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002454 }
2455 }
2456 else
2457 emsg_funcname(N_("E123: Undefined function: %s"), name);
2458 }
2459 goto ret_free;
2460 }
2461
2462 /*
2463 * ":function name(arg1, arg2)" Define function.
2464 */
2465 p = skipwhite(p);
2466 if (*p != '(')
2467 {
2468 if (!eap->skip)
2469 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002470 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002471 goto ret_free;
2472 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002473 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002474 if (vim_strchr(p, '(') != NULL)
2475 p = vim_strchr(p, '(');
2476 }
2477 p = skipwhite(p + 1);
2478
2479 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2480
2481 if (!eap->skip)
2482 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002483 // Check the name of the function. Unless it's a dictionary function
2484 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002485 if (name != NULL)
2486 arg = name;
2487 else
2488 arg = fudi.fd_newkey;
2489 if (arg != NULL && (fudi.fd_di == NULL
2490 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2491 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2492 {
2493 if (*arg == K_SPECIAL)
2494 j = 3;
2495 else
2496 j = 0;
2497 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2498 : eval_isnamec(arg[j])))
2499 ++j;
2500 if (arg[j] != NUL)
2501 emsg_funcname((char *)e_invarg2, arg);
2502 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002503 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002504 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002505 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002506 }
2507
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 if (get_function_args(&p, ')', &newargs,
2509 eap->cmdidx == CMD_def ? &argtypes : NULL,
2510 &varargs, &default_args, eap->skip) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002511 goto errret_2;
2512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002514 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 // find the return type: :def Func(): type
2516 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002517 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 ret_type = skipwhite(p + 1);
2519 p = skip_type(ret_type);
2520 if (p > ret_type)
2521 p = skipwhite(p);
2522 else
2523 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002524 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002525 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 else
2527 // find extra arguments "range", "dict", "abort" and "closure"
2528 for (;;)
2529 {
2530 p = skipwhite(p);
2531 if (STRNCMP(p, "range", 5) == 0)
2532 {
2533 flags |= FC_RANGE;
2534 p += 5;
2535 }
2536 else if (STRNCMP(p, "dict", 4) == 0)
2537 {
2538 flags |= FC_DICT;
2539 p += 4;
2540 }
2541 else if (STRNCMP(p, "abort", 5) == 0)
2542 {
2543 flags |= FC_ABORT;
2544 p += 5;
2545 }
2546 else if (STRNCMP(p, "closure", 7) == 0)
2547 {
2548 flags |= FC_CLOSURE;
2549 p += 7;
2550 if (current_funccal == NULL)
2551 {
2552 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2553 name == NULL ? (char_u *)"" : name);
2554 goto erret;
2555 }
2556 }
2557 else
2558 break;
2559 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002560
Bram Moolenaare38eab22019-12-05 21:50:01 +01002561 // When there is a line break use what follows for the function body.
2562 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002563 if (*p == '\n')
2564 line_arg = p + 1;
2565 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002566 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002567
2568 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2570 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002571 */
2572 if (KeyTyped)
2573 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002574 // Check if the function already exists, don't let the user type the
2575 // whole function before telling him it doesn't work! For a script we
2576 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002577 if (!eap->skip && !eap->forceit)
2578 {
2579 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002580 emsg(_(e_funcdict));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 else if (name != NULL && find_func(name, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002582 emsg_funcname(e_funcexts, name);
2583 }
2584
2585 if (!eap->skip && did_emsg)
2586 goto erret;
2587
Bram Moolenaare38eab22019-12-05 21:50:01 +01002588 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002589 cmdline_row = msg_row;
2590 }
2591
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002592 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002593 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002594
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002595 indent = 2;
2596 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002598 for (;;)
2599 {
2600 if (KeyTyped)
2601 {
2602 msg_scroll = TRUE;
2603 saved_wait_return = FALSE;
2604 }
2605 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002606
2607 if (line_arg != NULL)
2608 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002609 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002610 theline = line_arg;
2611 p = vim_strchr(theline, '\n');
2612 if (p == NULL)
2613 line_arg += STRLEN(line_arg);
2614 else
2615 {
2616 *p = NUL;
2617 line_arg = p + 1;
2618 }
2619 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002620 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002621 {
2622 vim_free(line_to_free);
2623 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002624 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002625 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002626 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002627 line_to_free = theline;
2628 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002629 if (KeyTyped)
2630 lines_left = Rows - 1;
2631 if (theline == NULL)
2632 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 if (eap->cmdidx == CMD_def)
2634 emsg(_("E1057: Missing :enddef"));
2635 else
2636 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002637 goto erret;
2638 }
2639
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002640 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002641 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002642 if (SOURCING_LNUM < sourcing_lnum_off)
2643 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002644 else
2645 sourcing_lnum_off = 0;
2646
2647 if (skip_until != NULL)
2648 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002650 // * ":append" and "."
2651 // * ":python <<EOF" and "EOF"
2652 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2653 if (heredoc_trimmed == NULL
2654 || (is_heredoc && skipwhite(theline) == theline)
2655 || STRNCMP(theline, heredoc_trimmed,
2656 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002657 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002658 if (heredoc_trimmed == NULL)
2659 p = theline;
2660 else if (is_heredoc)
2661 p = skipwhite(theline) == theline
2662 ? theline : theline + STRLEN(heredoc_trimmed);
2663 else
2664 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002665 if (STRCMP(p, skip_until) == 0)
2666 {
2667 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002668 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002669 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002670 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002671 }
2672 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002673 }
2674 else
2675 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002676 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002677 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002678 ;
2679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 // Check for "endfunction" or "enddef".
2681 if (checkforcmd(&p, nesting_def[nesting]
2682 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002683 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002684 char_u *nextcmd = NULL;
2685
Bram Moolenaar663bb232017-06-22 19:12:10 +02002686 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002687 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002688 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002689 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002690 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 give_warning2(eap->cmdidx == CMD_def
2692 ? (char_u *)_("W1001: Text found after :enddef: %s")
2693 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002694 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002695 if (nextcmd != NULL)
2696 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002697 // Another command follows. If the line came from "eap" we
2698 // can simply point into it, otherwise we need to change
2699 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002700 eap->nextcmd = nextcmd;
2701 if (line_to_free != NULL)
2702 {
2703 vim_free(*eap->cmdlinep);
2704 *eap->cmdlinep = line_to_free;
2705 line_to_free = NULL;
2706 }
2707 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002708 break;
2709 }
2710
Bram Moolenaare38eab22019-12-05 21:50:01 +01002711 // Increase indent inside "if", "while", "for" and "try", decrease
2712 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002714 indent -= 2;
2715 else if (STRNCMP(p, "if", 2) == 0
2716 || STRNCMP(p, "wh", 2) == 0
2717 || STRNCMP(p, "for", 3) == 0
2718 || STRNCMP(p, "try", 3) == 0)
2719 indent += 2;
2720
Bram Moolenaare38eab22019-12-05 21:50:01 +01002721 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002722 // Only recognize "def" inside "def", not inside "function",
2723 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002725 if (checkforcmd(&p, "function", 2)
2726 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002727 {
2728 if (*p == '!')
2729 p = skipwhite(p + 1);
2730 p += eval_fname_script(p);
2731 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2732 if (*skipwhite(p) == '(')
2733 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 if (nesting == MAX_FUNC_NESTING - 1)
2735 emsg(_("E1058: function nesting too deep"));
2736 else
2737 {
2738 ++nesting;
2739 nesting_def[nesting] = (c == 'd');
2740 indent += 2;
2741 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002742 }
2743 }
2744
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002745 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002746 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002747 if (eap->cmdidx != CMD_def
2748 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002749 || (p[0] == 'c'
2750 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2751 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2752 && (STRNCMP(&p[3], "nge", 3) != 0
2753 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002754 || (p[0] == 'i'
2755 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002756 && (!ASCII_ISALPHA(p[2])
2757 || (p[2] == 's'
2758 && (!ASCII_ISALPHA(p[3])
2759 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002760 skip_until = vim_strsave((char_u *)".");
2761
Bram Moolenaare38eab22019-12-05 21:50:01 +01002762 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002763 arg = skipwhite(skiptowhite(p));
2764 if (arg[0] == '<' && arg[1] =='<'
2765 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002766 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2767 || ((p[2] == '3' || p[2] == 'x')
2768 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002769 || (p[0] == 'p' && p[1] == 'e'
2770 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2771 || (p[0] == 't' && p[1] == 'c'
2772 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2773 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2774 && !ASCII_ISALPHA(p[3]))
2775 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2776 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2777 || (p[0] == 'm' && p[1] == 'z'
2778 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2779 ))
2780 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002781 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002782 p = skipwhite(arg + 2);
2783 if (*p == NUL)
2784 skip_until = vim_strsave((char_u *)".");
2785 else
2786 skip_until = vim_strsave(p);
2787 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002788
2789 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002790 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002791 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002792 if (*arg == '[')
2793 arg = vim_strchr(arg, ']');
2794 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002795 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002796 arg = skipwhite(skiptowhite(arg));
2797 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2798 && ((p[0] == 'l'
2799 && p[1] == 'e'
2800 && (!ASCII_ISALNUM(p[2])
2801 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02002802 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002803 p = skipwhite(arg + 3);
2804 if (STRNCMP(p, "trim", 4) == 0)
2805 {
2806 // Ignore leading white space.
2807 p = skipwhite(p + 4);
2808 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar8471e572019-05-19 21:37:18 +02002809 (int)(skipwhite(theline) - theline));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002810 }
2811 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2812 do_concat = FALSE;
2813 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002814 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002815 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002816 }
2817
Bram Moolenaare38eab22019-12-05 21:50:01 +01002818 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002819 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002820 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821
Bram Moolenaare38eab22019-12-05 21:50:01 +01002822 // Copy the line to newly allocated memory. get_one_sourceline()
2823 // allocates 250 bytes per line, this saves 80% on average. The cost
2824 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002825 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002826 if (p == NULL)
2827 goto erret;
2828 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002829
Bram Moolenaare38eab22019-12-05 21:50:01 +01002830 // Add NULL lines for continuation lines, so that the line count is
2831 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002832 while (sourcing_lnum_off-- > 0)
2833 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2834
Bram Moolenaare38eab22019-12-05 21:50:01 +01002835 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002836 if (line_arg != NULL && *line_arg == NUL)
2837 line_arg = NULL;
2838 }
2839
Bram Moolenaare38eab22019-12-05 21:50:01 +01002840 // Don't define the function when skipping commands or when an error was
2841 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002842 if (eap->skip || did_emsg)
2843 goto erret;
2844
2845 /*
2846 * If there are no errors, add the function
2847 */
2848 if (fudi.fd_dict == NULL)
2849 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 hashtab_T *ht;
2851
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002852 v = find_var(name, &ht, FALSE);
2853 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2854 {
2855 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2856 name);
2857 goto erret;
2858 }
2859
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 fp = find_func_even_dead(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002861 if (fp != NULL)
2862 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 int dead = fp->uf_flags & FC_DEAD;
2864
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002865 // Function can be replaced with "function!" and when sourcing the
2866 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002868 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2869 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002870 {
2871 emsg_funcname(e_funcexts, name);
2872 goto erret;
2873 }
2874 if (fp->uf_calls > 0)
2875 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002876 emsg_funcname(
2877 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002878 name);
2879 goto erret;
2880 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002881 if (fp->uf_refcount > 1)
2882 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002883 // This function is referenced somewhere, don't redefine it but
2884 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002885 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002886 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002887 fp = NULL;
2888 overwrite = TRUE;
2889 }
2890 else
2891 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002892 char_u *exp_name = fp->uf_name_exp;
2893
2894 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01002895 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002896 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002897 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002898 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002900#ifdef FEAT_PROFILE
2901 fp->uf_profiling = FALSE;
2902 fp->uf_prof_initialized = FALSE;
2903#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002904 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002905 }
2906 }
2907 else
2908 {
2909 char numbuf[20];
2910
2911 fp = NULL;
2912 if (fudi.fd_newkey == NULL && !eap->forceit)
2913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002914 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002915 goto erret;
2916 }
2917 if (fudi.fd_di == NULL)
2918 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002919 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002920 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002921 goto erret;
2922 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002923 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002924 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002925 goto erret;
2926
Bram Moolenaare38eab22019-12-05 21:50:01 +01002927 // Give the function a sequential number. Can only be used with a
2928 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002929 vim_free(name);
2930 sprintf(numbuf, "%d", ++func_nr);
2931 name = vim_strsave((char_u *)numbuf);
2932 if (name == NULL)
2933 goto erret;
2934 }
2935
2936 if (fp == NULL)
2937 {
2938 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
2939 {
2940 int slen, plen;
2941 char_u *scriptname;
2942
Bram Moolenaare38eab22019-12-05 21:50:01 +01002943 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002944 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002945 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002946 {
2947 scriptname = autoload_name(name);
2948 if (scriptname != NULL)
2949 {
2950 p = vim_strchr(scriptname, '/');
2951 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002952 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002953 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002954 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002955 j = OK;
2956 vim_free(scriptname);
2957 }
2958 }
2959 if (j == FAIL)
2960 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002961 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002962 goto erret;
2963 }
2964 }
2965
Bram Moolenaar47ed5532019-08-08 20:49:14 +02002966 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002967 if (fp == NULL)
2968 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 fp->uf_dfunc_idx = -1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002970
2971 if (fudi.fd_dict != NULL)
2972 {
2973 if (fudi.fd_di == NULL)
2974 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002975 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002976 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
2977 if (fudi.fd_di == NULL)
2978 {
2979 vim_free(fp);
2980 goto erret;
2981 }
2982 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
2983 {
2984 vim_free(fudi.fd_di);
2985 vim_free(fp);
2986 goto erret;
2987 }
2988 }
2989 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01002990 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002991 clear_tv(&fudi.fd_di->di_tv);
2992 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002993 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994
Bram Moolenaare38eab22019-12-05 21:50:01 +01002995 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002996 flags |= FC_DICT;
2997 }
2998
Bram Moolenaare38eab22019-12-05 21:50:01 +01002999 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003000 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003001 if (overwrite)
3002 {
3003 hi = hash_find(&func_hashtab, name);
3004 hi->hi_key = UF2HIKEY(fp);
3005 }
3006 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003007 {
3008 vim_free(fp);
3009 goto erret;
3010 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003011 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003012 }
3013 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003014 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 fp->uf_ret_type = &t_any;
3016
3017 if (eap->cmdidx == CMD_def)
3018 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003019 int lnum_save = SOURCING_LNUM;
3020
3021 // error messages are for the first function line
3022 SOURCING_LNUM = sourcing_lnum_top;
3023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 // parse the argument types
3025 ga_init2(&fp->uf_type_list, sizeof(type_T), 5);
3026
3027 if (argtypes.ga_len > 0)
3028 {
3029 // When "varargs" is set the last name/type goes into uf_va_name
3030 // and uf_va_type.
3031 int len = argtypes.ga_len - (varargs ? 1 : 0);
3032
3033 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
3034 if (fp->uf_arg_types != NULL)
3035 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003036 int i;
3037 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038
3039 for (i = 0; i < len; ++ i)
3040 {
3041 p = ((char_u **)argtypes.ga_data)[i];
3042 if (p == NULL)
3043 // todo: get type from default value
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003044 type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003046 type = parse_type(&p, &fp->uf_type_list);
3047 if (type == NULL)
3048 {
3049 SOURCING_LNUM = lnum_save;
3050 goto errret_2;
3051 }
3052 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 }
3054 }
3055 if (varargs)
3056 {
3057 // Move the last argument "...name: type" to uf_va_name and
3058 // uf_va_type.
3059 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3060 [fp->uf_args.ga_len - 1];
3061 --fp->uf_args.ga_len;
3062 p = ((char_u **)argtypes.ga_data)[len];
3063 if (p == NULL)
3064 // todo: get type from default value
3065 fp->uf_va_type = &t_any;
3066 else
3067 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003068 if (fp->uf_va_type == NULL)
3069 {
3070 SOURCING_LNUM = lnum_save;
3071 goto errret_2;
3072 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 }
3074 varargs = FALSE;
3075 }
3076
3077 // parse the return type, if any
3078 if (ret_type == NULL)
3079 fp->uf_ret_type = &t_void;
3080 else
3081 {
3082 p = ret_type;
3083 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3084 }
3085 }
3086
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003087 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003088 if ((flags & FC_CLOSURE) != 0)
3089 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003090 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003091 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003092 }
3093 else
3094 fp->uf_scoped = NULL;
3095
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003096#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003097 if (prof_def_func())
3098 func_do_profile(fp);
3099#endif
3100 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003101 if (sandbox)
3102 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003103 fp->uf_flags = flags;
3104 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003105 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003106 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003107 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 if (is_export)
3109 {
3110 fp->uf_flags |= FC_EXPORT;
3111 // let ex_export() know the export worked.
3112 is_export = FALSE;
3113 }
3114
3115 // ":def Func()" needs to be compiled
3116 if (eap->cmdidx == CMD_def)
3117 compile_def_function(fp, FALSE);
3118
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003119 goto ret_free;
3120
3121erret:
3122 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003123 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003124errret_2:
3125 ga_clear_strings(&newlines);
3126ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003127 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003128 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003129 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003130 vim_free(fudi.fd_newkey);
3131 vim_free(name);
3132 did_emsg |= saved_did_emsg;
3133 need_wait_return |= saved_wait_return;
3134}
3135
3136/*
3137 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3138 * Return 2 if "p" starts with "s:".
3139 * Return 0 otherwise.
3140 */
3141 int
3142eval_fname_script(char_u *p)
3143{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003144 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3145 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003146 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3147 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3148 return 5;
3149 if (p[0] == 's' && p[1] == ':')
3150 return 2;
3151 return 0;
3152}
3153
3154 int
3155translated_function_exists(char_u *name)
3156{
3157 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003158 return has_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 return find_func(name, NULL) != NULL;
3160}
3161
3162/*
3163 * Return TRUE when "ufunc" has old-style "..." varargs
3164 * or named varargs "...name: type".
3165 */
3166 int
3167has_varargs(ufunc_T *ufunc)
3168{
3169 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003170}
3171
3172/*
3173 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003174 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003175 */
3176 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003177function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003178{
3179 char_u *nm = name;
3180 char_u *p;
3181 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003182 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003183
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003184 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3185 if (no_deref)
3186 flag |= TFN_NO_DEREF;
3187 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003188 nm = skipwhite(nm);
3189
Bram Moolenaare38eab22019-12-05 21:50:01 +01003190 // Only accept "funcname", "funcname ", "funcname (..." and
3191 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003192 if (p != NULL && (*nm == NUL || *nm == '('))
3193 n = translated_function_exists(p);
3194 vim_free(p);
3195 return n;
3196}
3197
Bram Moolenaar113e1072019-01-20 15:30:40 +01003198#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003199 char_u *
3200get_expanded_name(char_u *name, int check)
3201{
3202 char_u *nm = name;
3203 char_u *p;
3204
3205 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
3206
3207 if (p != NULL && *nm == NUL)
3208 if (!check || translated_function_exists(p))
3209 return p;
3210
3211 vim_free(p);
3212 return NULL;
3213}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003214#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003215
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003216/*
3217 * Function given to ExpandGeneric() to obtain the list of user defined
3218 * function names.
3219 */
3220 char_u *
3221get_user_func_name(expand_T *xp, int idx)
3222{
3223 static long_u done;
3224 static hashitem_T *hi;
3225 ufunc_T *fp;
3226
3227 if (idx == 0)
3228 {
3229 done = 0;
3230 hi = func_hashtab.ht_array;
3231 }
3232 if (done < func_hashtab.ht_used)
3233 {
3234 if (done++ > 0)
3235 ++hi;
3236 while (HASHITEM_EMPTY(hi))
3237 ++hi;
3238 fp = HI2UF(hi);
3239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 // don't show dead, dict and lambda functions
3241 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003242 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003244
3245 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003246 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003247
3248 cat_func_name(IObuff, fp);
3249 if (xp->xp_context != EXPAND_USER_FUNC)
3250 {
3251 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003253 STRCAT(IObuff, ")");
3254 }
3255 return IObuff;
3256 }
3257 return NULL;
3258}
3259
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003260/*
3261 * ":delfunction {name}"
3262 */
3263 void
3264ex_delfunction(exarg_T *eap)
3265{
3266 ufunc_T *fp = NULL;
3267 char_u *p;
3268 char_u *name;
3269 funcdict_T fudi;
3270
3271 p = eap->arg;
3272 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
3273 vim_free(fudi.fd_newkey);
3274 if (name == NULL)
3275 {
3276 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003277 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003278 return;
3279 }
3280 if (!ends_excmd(*skipwhite(p)))
3281 {
3282 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003283 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003284 return;
3285 }
3286 eap->nextcmd = check_nextcmd(p);
3287 if (eap->nextcmd != NULL)
3288 *p = NUL;
3289
3290 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003292 vim_free(name);
3293
3294 if (!eap->skip)
3295 {
3296 if (fp == NULL)
3297 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003298 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003299 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003300 return;
3301 }
3302 if (fp->uf_calls > 0)
3303 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003304 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003305 return;
3306 }
3307
3308 if (fudi.fd_dict != NULL)
3309 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003310 // Delete the dict item that refers to the function, it will
3311 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003312 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3313 }
3314 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003315 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003316 // A normal function (not a numbered function or lambda) has a
3317 // refcount of 1 for the entry in the hashtable. When deleting
3318 // it and the refcount is more than one, it should be kept.
3319 // A numbered function and lambda should be kept if the refcount is
3320 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003321 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003322 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003323 // Function is still referenced somewhere. Don't free it but
3324 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003325 if (func_remove(fp))
3326 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003327 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003328 }
3329 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003330 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003331 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003332 }
3333}
3334
3335/*
3336 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003337 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003338 */
3339 void
3340func_unref(char_u *name)
3341{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003342 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003343
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003344 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003345 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003347 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003348 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003349#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003350 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003351#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003352 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003353 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003354 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003355 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003356 // Only delete it when it's not being used. Otherwise it's done
3357 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003358 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003359 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003360 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003361}
3362
3363/*
3364 * Unreference a Function: decrement the reference count and free it when it
3365 * becomes zero.
3366 */
3367 void
3368func_ptr_unref(ufunc_T *fp)
3369{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003370 if (fp != NULL && --fp->uf_refcount <= 0)
3371 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003372 // Only delete it when it's not being used. Otherwise it's done
3373 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003374 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003375 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003376 }
3377}
3378
3379/*
3380 * Count a reference to a Function.
3381 */
3382 void
3383func_ref(char_u *name)
3384{
3385 ufunc_T *fp;
3386
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003387 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003388 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003390 if (fp != NULL)
3391 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003392 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003393 // Only give an error for a numbered function.
3394 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003395 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003396}
3397
3398/*
3399 * Count a reference to a Function.
3400 */
3401 void
3402func_ptr_ref(ufunc_T *fp)
3403{
3404 if (fp != NULL)
3405 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003406}
3407
3408/*
3409 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3410 * referenced from anywhere that is in use.
3411 */
3412 static int
3413can_free_funccal(funccall_T *fc, int copyID)
3414{
3415 return (fc->l_varlist.lv_copyID != copyID
3416 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003417 && fc->l_avars.dv_copyID != copyID
3418 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003419}
3420
3421/*
3422 * ":return [expr]"
3423 */
3424 void
3425ex_return(exarg_T *eap)
3426{
3427 char_u *arg = eap->arg;
3428 typval_T rettv;
3429 int returning = FALSE;
3430
3431 if (current_funccal == NULL)
3432 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003433 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003434 return;
3435 }
3436
3437 if (eap->skip)
3438 ++emsg_skip;
3439
3440 eap->nextcmd = NULL;
3441 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3442 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3443 {
3444 if (!eap->skip)
3445 returning = do_return(eap, FALSE, TRUE, &rettv);
3446 else
3447 clear_tv(&rettv);
3448 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003449 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003450 else if (!eap->skip)
3451 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003452 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003453 update_force_abort();
3454
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003455 /*
3456 * Return unless the expression evaluation has been cancelled due to an
3457 * aborting error, an interrupt, or an exception.
3458 */
3459 if (!aborting())
3460 returning = do_return(eap, FALSE, TRUE, NULL);
3461 }
3462
Bram Moolenaare38eab22019-12-05 21:50:01 +01003463 // When skipping or the return gets pending, advance to the next command
3464 // in this line (!returning). Otherwise, ignore the rest of the line.
3465 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003466 if (returning)
3467 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003468 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003469 eap->nextcmd = check_nextcmd(arg);
3470
3471 if (eap->skip)
3472 --emsg_skip;
3473}
3474
3475/*
3476 * ":1,25call func(arg1, arg2)" function call.
3477 */
3478 void
3479ex_call(exarg_T *eap)
3480{
3481 char_u *arg = eap->arg;
3482 char_u *startarg;
3483 char_u *name;
3484 char_u *tofree;
3485 int len;
3486 typval_T rettv;
3487 linenr_T lnum;
3488 int doesrange;
3489 int failed = FALSE;
3490 funcdict_T fudi;
3491 partial_T *partial = NULL;
3492
3493 if (eap->skip)
3494 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003495 // trans_function_name() doesn't work well when skipping, use eval0()
3496 // instead to skip to any following command, e.g. for:
3497 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003498 ++emsg_skip;
3499 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3500 clear_tv(&rettv);
3501 --emsg_skip;
3502 return;
3503 }
3504
3505 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
3506 if (fudi.fd_newkey != NULL)
3507 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003508 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003509 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003510 vim_free(fudi.fd_newkey);
3511 }
3512 if (tofree == NULL)
3513 return;
3514
Bram Moolenaare38eab22019-12-05 21:50:01 +01003515 // Increase refcount on dictionary, it could get deleted when evaluating
3516 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003517 if (fudi.fd_dict != NULL)
3518 ++fudi.fd_dict->dv_refcount;
3519
Bram Moolenaare38eab22019-12-05 21:50:01 +01003520 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3521 // contents. For VAR_PARTIAL get its partial, unless we already have one
3522 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003523 len = (int)STRLEN(tofree);
3524 name = deref_func_name(tofree, &len,
3525 partial != NULL ? NULL : &partial, FALSE);
3526
Bram Moolenaare38eab22019-12-05 21:50:01 +01003527 // Skip white space to allow ":call func ()". Not good, but required for
3528 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003529 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003530 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003531
3532 if (*startarg != '(')
3533 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003535 goto end;
3536 }
3537
3538 /*
3539 * When skipping, evaluate the function once, to find the end of the
3540 * arguments.
3541 * When the function takes a range, this is discovered after the first
3542 * call, and the loop is broken.
3543 */
3544 if (eap->skip)
3545 {
3546 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003547 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003548 }
3549 else
3550 lnum = eap->line1;
3551 for ( ; lnum <= eap->line2; ++lnum)
3552 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003553 funcexe_T funcexe;
3554
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003555 if (!eap->skip && eap->addr_count > 0)
3556 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003557 if (lnum > curbuf->b_ml.ml_line_count)
3558 {
3559 // If the function deleted lines or switched to another buffer
3560 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003561 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003562 break;
3563 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003564 curwin->w_cursor.lnum = lnum;
3565 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003566 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003567 }
3568 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003569
Bram Moolenaarac92e252019-08-03 21:58:38 +02003570 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003571 funcexe.firstline = eap->line1;
3572 funcexe.lastline = eap->line2;
3573 funcexe.doesrange = &doesrange;
3574 funcexe.evaluate = !eap->skip;
3575 funcexe.partial = partial;
3576 funcexe.selfdict = fudi.fd_dict;
3577 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003578 {
3579 failed = TRUE;
3580 break;
3581 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003582 if (has_watchexpr())
3583 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003584
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003585 // Handle a function returning a Funcref, Dictionary or List.
3586 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE,
3587 name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003588 {
3589 failed = TRUE;
3590 break;
3591 }
3592
3593 clear_tv(&rettv);
3594 if (doesrange || eap->skip)
3595 break;
3596
Bram Moolenaare38eab22019-12-05 21:50:01 +01003597 // Stop when immediately aborting on error, or when an interrupt
3598 // occurred or an exception was thrown but not caught.
3599 // get_func_tv() returned OK, so that the check for trailing
3600 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003601 if (aborting())
3602 break;
3603 }
3604 if (eap->skip)
3605 --emsg_skip;
3606
Bram Moolenaare51bb172020-02-16 19:42:23 +01003607 // When inside :try we need to check for following "| catch".
3608 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003609 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003610 // Check for trailing illegal characters and a following command.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003611 if (!ends_excmd(*arg))
3612 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01003613 if (!failed)
3614 {
3615 emsg_severe = TRUE;
3616 emsg(_(e_trailing));
3617 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003618 }
3619 else
3620 eap->nextcmd = check_nextcmd(arg);
3621 }
3622
3623end:
3624 dict_unref(fudi.fd_dict);
3625 vim_free(tofree);
3626}
3627
3628/*
3629 * Return from a function. Possibly makes the return pending. Also called
3630 * for a pending return at the ":endtry" or after returning from an extra
3631 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3632 * when called due to a ":return" command. "rettv" may point to a typval_T
3633 * with the return rettv. Returns TRUE when the return can be carried out,
3634 * FALSE when the return gets pending.
3635 */
3636 int
3637do_return(
3638 exarg_T *eap,
3639 int reanimate,
3640 int is_cmd,
3641 void *rettv)
3642{
3643 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003644 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003645
3646 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003647 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003648 current_funccal->returned = FALSE;
3649
3650 /*
3651 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3652 * not in its finally clause (which then is to be executed next) is found.
3653 * In this case, make the ":return" pending for execution at the ":endtry".
3654 * Otherwise, return normally.
3655 */
3656 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3657 if (idx >= 0)
3658 {
3659 cstack->cs_pending[idx] = CSTP_RETURN;
3660
3661 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003662 // A pending return again gets pending. "rettv" points to an
3663 // allocated variable with the rettv of the original ":return"'s
3664 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003665 cstack->cs_rettv[idx] = rettv;
3666 else
3667 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003668 // When undoing a return in order to make it pending, get the stored
3669 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003670 if (reanimate)
3671 rettv = current_funccal->rettv;
3672
3673 if (rettv != NULL)
3674 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003675 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003676 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3677 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3678 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003679 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003680 }
3681 else
3682 cstack->cs_rettv[idx] = NULL;
3683
3684 if (reanimate)
3685 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003686 // The pending return value could be overwritten by a ":return"
3687 // without argument in a finally clause; reset the default
3688 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003689 current_funccal->rettv->v_type = VAR_NUMBER;
3690 current_funccal->rettv->vval.v_number = 0;
3691 }
3692 }
3693 report_make_pending(CSTP_RETURN, rettv);
3694 }
3695 else
3696 {
3697 current_funccal->returned = TRUE;
3698
Bram Moolenaare38eab22019-12-05 21:50:01 +01003699 // If the return is carried out now, store the return value. For
3700 // a return immediately after reanimation, the value is already
3701 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003702 if (!reanimate && rettv != NULL)
3703 {
3704 clear_tv(current_funccal->rettv);
3705 *current_funccal->rettv = *(typval_T *)rettv;
3706 if (!is_cmd)
3707 vim_free(rettv);
3708 }
3709 }
3710
3711 return idx < 0;
3712}
3713
3714/*
3715 * Free the variable with a pending return value.
3716 */
3717 void
3718discard_pending_return(void *rettv)
3719{
3720 free_tv((typval_T *)rettv);
3721}
3722
3723/*
3724 * Generate a return command for producing the value of "rettv". The result
3725 * is an allocated string. Used by report_pending() for verbose messages.
3726 */
3727 char_u *
3728get_return_cmd(void *rettv)
3729{
3730 char_u *s = NULL;
3731 char_u *tofree = NULL;
3732 char_u numbuf[NUMBUFLEN];
3733
3734 if (rettv != NULL)
3735 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3736 if (s == NULL)
3737 s = (char_u *)"";
3738
3739 STRCPY(IObuff, ":return ");
3740 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3741 if (STRLEN(s) + 8 >= IOSIZE)
3742 STRCPY(IObuff + IOSIZE - 4, "...");
3743 vim_free(tofree);
3744 return vim_strsave(IObuff);
3745}
3746
3747/*
3748 * Get next function line.
3749 * Called by do_cmdline() to get the next line.
3750 * Returns allocated string, or NULL for end of function.
3751 */
3752 char_u *
3753get_func_line(
3754 int c UNUSED,
3755 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003756 int indent UNUSED,
3757 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003758{
3759 funccall_T *fcp = (funccall_T *)cookie;
3760 ufunc_T *fp = fcp->func;
3761 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003762 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003763
Bram Moolenaare38eab22019-12-05 21:50:01 +01003764 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003765 if (fcp->dbg_tick != debug_tick)
3766 {
3767 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003768 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003769 fcp->dbg_tick = debug_tick;
3770 }
3771#ifdef FEAT_PROFILE
3772 if (do_profiling == PROF_YES)
3773 func_line_end(cookie);
3774#endif
3775
3776 gap = &fp->uf_lines;
3777 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3778 || fcp->returned)
3779 retval = NULL;
3780 else
3781 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003782 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003783 while (fcp->linenr < gap->ga_len
3784 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3785 ++fcp->linenr;
3786 if (fcp->linenr >= gap->ga_len)
3787 retval = NULL;
3788 else
3789 {
3790 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003791 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003792#ifdef FEAT_PROFILE
3793 if (do_profiling == PROF_YES)
3794 func_line_start(cookie);
3795#endif
3796 }
3797 }
3798
Bram Moolenaare38eab22019-12-05 21:50:01 +01003799 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003800 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003801 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003802 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003803 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003804 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003805 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003806 fcp->dbg_tick = debug_tick;
3807 }
3808
3809 return retval;
3810}
3811
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003812/*
3813 * Return TRUE if the currently active function should be ended, because a
3814 * return was encountered or an error occurred. Used inside a ":while".
3815 */
3816 int
3817func_has_ended(void *cookie)
3818{
3819 funccall_T *fcp = (funccall_T *)cookie;
3820
Bram Moolenaare38eab22019-12-05 21:50:01 +01003821 // Ignore the "abort" flag if the abortion behavior has been changed due to
3822 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003823 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3824 || fcp->returned);
3825}
3826
3827/*
3828 * return TRUE if cookie indicates a function which "abort"s on errors.
3829 */
3830 int
3831func_has_abort(
3832 void *cookie)
3833{
3834 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3835}
3836
3837
3838/*
3839 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3840 * Don't do this when "Func" is already a partial that was bound
3841 * explicitly (pt_auto is FALSE).
3842 * Changes "rettv" in-place.
3843 * Returns the updated "selfdict_in".
3844 */
3845 dict_T *
3846make_partial(dict_T *selfdict_in, typval_T *rettv)
3847{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003848 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003849 char_u *tofree = NULL;
3850 ufunc_T *fp;
3851 char_u fname_buf[FLEN_FIXED + 1];
3852 int error;
3853 dict_T *selfdict = selfdict_in;
3854
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003855 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3856 fp = rettv->vval.v_partial->pt_func;
3857 else
3858 {
3859 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3860 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003861 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003862 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003864 vim_free(tofree);
3865 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003866
3867 if (fp != NULL && (fp->uf_flags & FC_DICT))
3868 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003869 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003870
3871 if (pt != NULL)
3872 {
3873 pt->pt_refcount = 1;
3874 pt->pt_dict = selfdict;
3875 pt->pt_auto = TRUE;
3876 selfdict = NULL;
3877 if (rettv->v_type == VAR_FUNC)
3878 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003879 // Just a function: Take over the function name and use
3880 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003881 pt->pt_name = rettv->vval.v_string;
3882 }
3883 else
3884 {
3885 partial_T *ret_pt = rettv->vval.v_partial;
3886 int i;
3887
Bram Moolenaare38eab22019-12-05 21:50:01 +01003888 // Partial: copy the function name, use selfdict and copy
3889 // args. Can't take over name or args, the partial might
3890 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003891 if (ret_pt->pt_name != NULL)
3892 {
3893 pt->pt_name = vim_strsave(ret_pt->pt_name);
3894 func_ref(pt->pt_name);
3895 }
3896 else
3897 {
3898 pt->pt_func = ret_pt->pt_func;
3899 func_ptr_ref(pt->pt_func);
3900 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003901 if (ret_pt->pt_argc > 0)
3902 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003903 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003904 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003905 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003906 pt->pt_argc = 0;
3907 else
3908 {
3909 pt->pt_argc = ret_pt->pt_argc;
3910 for (i = 0; i < pt->pt_argc; i++)
3911 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
3912 }
3913 }
3914 partial_unref(ret_pt);
3915 }
3916 rettv->v_type = VAR_PARTIAL;
3917 rettv->vval.v_partial = pt;
3918 }
3919 }
3920 return selfdict;
3921}
3922
3923/*
3924 * Return the name of the executed function.
3925 */
3926 char_u *
3927func_name(void *cookie)
3928{
3929 return ((funccall_T *)cookie)->func->uf_name;
3930}
3931
3932/*
3933 * Return the address holding the next breakpoint line for a funccall cookie.
3934 */
3935 linenr_T *
3936func_breakpoint(void *cookie)
3937{
3938 return &((funccall_T *)cookie)->breakpoint;
3939}
3940
3941/*
3942 * Return the address holding the debug tick for a funccall cookie.
3943 */
3944 int *
3945func_dbg_tick(void *cookie)
3946{
3947 return &((funccall_T *)cookie)->dbg_tick;
3948}
3949
3950/*
3951 * Return the nesting level for a funccall cookie.
3952 */
3953 int
3954func_level(void *cookie)
3955{
3956 return ((funccall_T *)cookie)->level;
3957}
3958
3959/*
3960 * Return TRUE when a function was ended by a ":return" command.
3961 */
3962 int
3963current_func_returned(void)
3964{
3965 return current_funccal->returned;
3966}
3967
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003968 int
3969free_unref_funccal(int copyID, int testing)
3970{
3971 int did_free = FALSE;
3972 int did_free_funccal = FALSE;
3973 funccall_T *fc, **pfc;
3974
3975 for (pfc = &previous_funccal; *pfc != NULL; )
3976 {
3977 if (can_free_funccal(*pfc, copyID))
3978 {
3979 fc = *pfc;
3980 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003981 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003982 did_free = TRUE;
3983 did_free_funccal = TRUE;
3984 }
3985 else
3986 pfc = &(*pfc)->caller;
3987 }
3988 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003989 // When a funccal was freed some more items might be garbage
3990 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003991 (void)garbage_collect(testing);
3992
3993 return did_free;
3994}
3995
3996/*
Bram Moolenaarba209902016-08-24 22:06:38 +02003997 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003998 */
3999 static funccall_T *
4000get_funccal(void)
4001{
4002 int i;
4003 funccall_T *funccal;
4004 funccall_T *temp_funccal;
4005
4006 funccal = current_funccal;
4007 if (debug_backtrace_level > 0)
4008 {
4009 for (i = 0; i < debug_backtrace_level; i++)
4010 {
4011 temp_funccal = funccal->caller;
4012 if (temp_funccal)
4013 funccal = temp_funccal;
4014 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004015 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004016 debug_backtrace_level = i;
4017 }
4018 }
4019 return funccal;
4020}
4021
4022/*
4023 * Return the hashtable used for local variables in the current funccal.
4024 * Return NULL if there is no current funccal.
4025 */
4026 hashtab_T *
4027get_funccal_local_ht()
4028{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004030 return NULL;
4031 return &get_funccal()->l_vars.dv_hashtab;
4032}
4033
4034/*
4035 * Return the l: scope variable.
4036 * Return NULL if there is no current funccal.
4037 */
4038 dictitem_T *
4039get_funccal_local_var()
4040{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004042 return NULL;
4043 return &get_funccal()->l_vars_var;
4044}
4045
4046/*
4047 * Return the hashtable used for argument in the current funccal.
4048 * Return NULL if there is no current funccal.
4049 */
4050 hashtab_T *
4051get_funccal_args_ht()
4052{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004054 return NULL;
4055 return &get_funccal()->l_avars.dv_hashtab;
4056}
4057
4058/*
4059 * Return the a: scope variable.
4060 * Return NULL if there is no current funccal.
4061 */
4062 dictitem_T *
4063get_funccal_args_var()
4064{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004066 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004067 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004068}
4069
4070/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004071 * List function variables, if there is a function.
4072 */
4073 void
4074list_func_vars(int *first)
4075{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004077 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004078 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004079}
4080
4081/*
4082 * If "ht" is the hashtable for local variables in the current funccal, return
4083 * the dict that contains it.
4084 * Otherwise return NULL.
4085 */
4086 dict_T *
4087get_current_funccal_dict(hashtab_T *ht)
4088{
4089 if (current_funccal != NULL
4090 && ht == &current_funccal->l_vars.dv_hashtab)
4091 return &current_funccal->l_vars;
4092 return NULL;
4093}
4094
4095/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004096 * Search hashitem in parent scope.
4097 */
4098 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004099find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004100{
4101 funccall_T *old_current_funccal = current_funccal;
4102 hashtab_T *ht;
4103 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004104 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004105
4106 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4107 return NULL;
4108
Bram Moolenaare38eab22019-12-05 21:50:01 +01004109 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004110 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004111 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004112 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004113 ht = find_var_ht(name, &varname);
4114 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004115 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004116 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004117 if (!HASHITEM_EMPTY(hi))
4118 {
4119 *pht = ht;
4120 break;
4121 }
4122 }
4123 if (current_funccal == current_funccal->func->uf_scoped)
4124 break;
4125 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004126 }
4127 current_funccal = old_current_funccal;
4128
4129 return hi;
4130}
4131
4132/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004133 * Search variable in parent scope.
4134 */
4135 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004136find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004137{
4138 dictitem_T *v = NULL;
4139 funccall_T *old_current_funccal = current_funccal;
4140 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004141 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004142
4143 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4144 return NULL;
4145
Bram Moolenaare38eab22019-12-05 21:50:01 +01004146 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004147 current_funccal = current_funccal->func->uf_scoped;
4148 while (current_funccal)
4149 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004150 ht = find_var_ht(name, &varname);
4151 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004152 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004153 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004154 if (v != NULL)
4155 break;
4156 }
4157 if (current_funccal == current_funccal->func->uf_scoped)
4158 break;
4159 current_funccal = current_funccal->func->uf_scoped;
4160 }
4161 current_funccal = old_current_funccal;
4162
4163 return v;
4164}
4165
4166/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004167 * Set "copyID + 1" in previous_funccal and callers.
4168 */
4169 int
4170set_ref_in_previous_funccal(int copyID)
4171{
4172 int abort = FALSE;
4173 funccall_T *fc;
4174
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004175 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004176 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004177 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004178 abort = abort
4179 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4180 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004181 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004182 }
4183 return abort;
4184}
4185
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004186 static int
4187set_ref_in_funccal(funccall_T *fc, int copyID)
4188{
4189 int abort = FALSE;
4190
4191 if (fc->fc_copyID != copyID)
4192 {
4193 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004194 abort = abort
4195 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4196 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004197 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004198 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004199 }
4200 return abort;
4201}
4202
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004203/*
4204 * Set "copyID" in all local vars and arguments in the call stack.
4205 */
4206 int
4207set_ref_in_call_stack(int copyID)
4208{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004209 int abort = FALSE;
4210 funccall_T *fc;
4211 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004212
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004213 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004214 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004215
4216 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004217 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4218 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004219 abort = abort || set_ref_in_funccal(fc, copyID);
4220
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004221 return abort;
4222}
4223
4224/*
4225 * Set "copyID" in all functions available by name.
4226 */
4227 int
4228set_ref_in_functions(int copyID)
4229{
4230 int todo;
4231 hashitem_T *hi = NULL;
4232 int abort = FALSE;
4233 ufunc_T *fp;
4234
4235 todo = (int)func_hashtab.ht_used;
4236 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004237 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004238 if (!HASHITEM_EMPTY(hi))
4239 {
4240 --todo;
4241 fp = HI2UF(hi);
4242 if (!func_name_refcount(fp->uf_name))
4243 abort = abort || set_ref_in_func(NULL, fp, copyID);
4244 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004245 }
4246 return abort;
4247}
4248
4249/*
4250 * Set "copyID" in all function arguments.
4251 */
4252 int
4253set_ref_in_func_args(int copyID)
4254{
4255 int i;
4256 int abort = FALSE;
4257
4258 for (i = 0; i < funcargs.ga_len; ++i)
4259 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4260 copyID, NULL, NULL);
4261 return abort;
4262}
4263
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004264/*
4265 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004266 * Returns TRUE if setting references failed somehow.
4267 */
4268 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004269set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004270{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004271 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004272 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004273 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004274 char_u fname_buf[FLEN_FIXED + 1];
4275 char_u *tofree = NULL;
4276 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004277 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004278
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004279 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004280 return FALSE;
4281
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004282 if (fp_in == NULL)
4283 {
4284 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004286 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004287 if (fp != NULL)
4288 {
4289 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004290 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004291 }
4292 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004293 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004294}
4295
Bram Moolenaare38eab22019-12-05 21:50:01 +01004296#endif // FEAT_EVAL