blob: b87cdedb68ce87cb3e0421b7cec1566e9ad5406d [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 Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
Bram Moolenaara9b579f2016-07-17 18:29:19 +020032static char *e_funcdict = N_("E717: Dictionary entry already exists");
33static char *e_funcref = N_("E718: Funcref required");
Bram Moolenaara9b579f2016-07-17 18:29:19 +020034
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020035static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010036static void func_clear(ufunc_T *fp, int force);
37static int func_free(ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020038
39 void
40func_init()
41{
42 hash_init(&func_hashtab);
43}
44
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020045/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020046 * Return the function hash table
47 */
48 hashtab_T *
49func_tbl_get(void)
50{
51 return &func_hashtab;
52}
53
54/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020055 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020056 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010057 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010058 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010059 * Return a pointer to after the type.
60 * When something is wrong return "arg".
61 */
62 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010063one_function_arg(
64 char_u *arg,
65 garray_T *newargs,
66 garray_T *argtypes,
67 int types_optional,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010068 evalarg_T *evalarg,
Bram Moolenaar2a389082021-04-09 20:24:31 +020069 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010070 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010071{
Bram Moolenaar6e949782020-04-13 17:21:00 +020072 char_u *p = arg;
73 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020074 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010075
76 while (ASCII_ISALNUM(*p) || *p == '_')
77 ++p;
78 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020079 || (argtypes == NULL
80 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
81 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082 {
83 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000084 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 return arg;
86 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010087
Bram Moolenaar057e84a2021-02-28 16:55:11 +010088 // Vim9 script: cannot use script var name for argument. In function: also
89 // check local vars and arguments.
90 if (!skip && argtypes != NULL && check_defined(arg, p - arg,
91 evalarg == NULL ? NULL : evalarg->eval_cctx, TRUE) == FAIL)
Bram Moolenaarb4893b82021-02-21 22:20:24 +010092 return arg;
Bram Moolenaarb4893b82021-02-21 22:20:24 +010093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010094 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
95 return arg;
96 if (newargs != NULL)
97 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098 int c;
99 int i;
100
101 c = *p;
102 *p = NUL;
103 arg_copy = vim_strsave(arg);
104 if (arg_copy == NULL)
105 {
106 *p = c;
107 return arg;
108 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200109 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200110 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200111 // Check for duplicate argument name.
112 for (i = 0; i < newargs->ga_len; ++i)
113 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
114 {
115 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
116 vim_free(arg_copy);
117 return arg;
118 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
120 newargs->ga_len++;
121
122 *p = c;
123 }
124
125 // get any type from "arg: type"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100126 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100127 {
128 char_u *type = NULL;
129
Bram Moolenaar6e949782020-04-13 17:21:00 +0200130 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
131 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200132 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200133 arg_copy == NULL ? arg : arg_copy);
134 p = skipwhite(p);
135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100136 if (*p == ':')
137 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200138 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100139 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200140 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100141 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200142 return arg;
143 }
144 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200145 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100146 if (!skip)
147 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100148 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200149 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200150 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200151 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200152 arg_copy == NULL ? arg : arg_copy);
153 return arg;
154 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100155 if (!skip)
156 {
157 if (type == NULL && types_optional)
158 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200159 type = vim_strsave((char_u *)
160 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100161 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
162 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163 }
164
165 return p;
166}
167
168/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000169 * Handle line continuation in function arguments or body.
170 * Get a next line, store it in "eap" if appropriate and use "line_to_free" to
171 * handle freeing the line later.
172 */
173 static char_u *
174get_function_line(
175 exarg_T *eap,
176 char_u **line_to_free,
177 getline_opt_T getline_options,
178 int indent)
179{
180 char_u *theline;
181
182 if (eap->getline == NULL)
183 theline = getcmdline(':', 0L, indent, getline_options);
184 else
185 theline = eap->getline(':', eap->cookie, indent, getline_options);
186 if (theline != NULL)
187 {
188 if (*eap->cmdlinep == *line_to_free)
189 *eap->cmdlinep = theline;
190 vim_free(*line_to_free);
191 *line_to_free = theline;
192 }
193
194 return theline;
195}
196
197/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200198 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100199 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100200 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200201 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100202 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200203get_function_args(
204 char_u **argp,
205 char_u endchar,
206 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100208 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100209 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200210 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200211 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200212 int skip,
213 exarg_T *eap,
214 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200215{
216 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100217 char_u *arg;
218 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200219 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200220 int any_default = FALSE;
221 char_u *expr;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100222 char_u *whitep = *argp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200223
224 if (newargs != NULL)
225 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 if (argtypes != NULL)
227 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200228 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200229 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200230
231 if (varargs != NULL)
232 *varargs = FALSE;
233
234 /*
235 * Isolate the arguments: "arg1, arg2, ...)"
236 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100237 arg = skipwhite(*argp);
238 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200239 while (*p != endchar)
240 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200241 while (eap != NULL && eap->getline != NULL
242 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200243 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200244 // End of the line, get the next one.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000245 char_u *theline = get_function_line(eap, line_to_free, 0, TRUE);
246
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200247 if (theline == NULL)
248 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200249 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200250 p = skipwhite(theline);
251 }
252
253 if (mustend && *p != endchar)
254 {
255 if (!skip)
256 semsg(_(e_invarg2), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100257 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200258 }
259 if (*p == endchar)
260 break;
261
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200262 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
263 {
264 if (varargs != NULL)
265 *varargs = TRUE;
266 p += 3;
267 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268
269 if (argtypes != NULL)
270 {
271 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200272 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100273 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100274 if (!skip)
275 emsg(_(e_missing_name_after_dots));
276 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277 }
278
279 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100280 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaar2a389082021-04-09 20:24:31 +0200281 evalarg, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 if (p == arg)
283 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100284 if (*skipwhite(p) == '=')
285 {
286 emsg(_(e_cannot_use_default_for_variable_arguments));
287 break;
288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100289 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200290 }
291 else
292 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200293 char_u *np;
294
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200295 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100296 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaar2a389082021-04-09 20:24:31 +0200297 evalarg, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200299 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200300
Bram Moolenaar015cf102021-06-26 21:52:02 +0200301 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200302 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200303 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200304 if (*np == '=' && np[1] != '=' && np[1] != '~'
305 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200306 {
307 typval_T rettv;
308
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100309 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200310 any_default = TRUE;
311 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200312 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200313 p = skipwhite(p);
314 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200315 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200316 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200317 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200318 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200319 if (ga_grow(default_args, 1) == FAIL)
320 goto err_ret;
321
322 // trim trailing whitespace
323 while (p > expr && VIM_ISWHITE(p[-1]))
324 p--;
325 c = *p;
326 *p = NUL;
327 expr = vim_strsave(expr);
328 if (expr == NULL)
329 {
330 *p = c;
331 goto err_ret;
332 }
333 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200334 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200335 default_args->ga_len++;
336 *p = c;
337 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200338 }
339 else
340 mustend = TRUE;
341 }
342 else if (any_default)
343 {
344 emsg(_("E989: Non-default argument follows default argument"));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200345 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200346 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200347
348 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
349 {
350 // Be tolerant when skipping
351 if (!skip)
352 {
353 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
354 goto err_ret;
355 }
356 p = skipwhite(p);
357 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200358 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200359 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200360 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200361 // Don't give this error when skipping, it makes the "->" not
362 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100363 // Allow missing space after comma in legacy functions.
364 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200365 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
366 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100367 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200368 goto err_ret;
369 }
370 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200371 else
372 mustend = TRUE;
373 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200374 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200375 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200376 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200377
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200378 if (*p != endchar)
379 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100380 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200381
382 *argp = p;
383 return OK;
384
385err_ret:
386 if (newargs != NULL)
387 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200388 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200389 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200390 return FAIL;
391}
392
393/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100394 * Parse the argument types, filling "fp->uf_arg_types".
395 * Return OK or FAIL.
396 */
397 static int
398parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
399{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200400 int len = 0;
401
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100402 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
403 if (argtypes->ga_len > 0)
404 {
405 // When "varargs" is set the last name/type goes into uf_va_name
406 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200407 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100408
409 if (len > 0)
410 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
411 if (fp->uf_arg_types != NULL)
412 {
413 int i;
414 type_T *type;
415
416 for (i = 0; i < len; ++ i)
417 {
418 char_u *p = ((char_u **)argtypes->ga_data)[i];
419
420 if (p == NULL)
421 // will get the type from the default value
422 type = &t_unknown;
423 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100424 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100425 if (type == NULL)
426 return FAIL;
427 fp->uf_arg_types[i] = type;
428 }
429 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100430 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200431
432 if (varargs)
433 {
434 char_u *p;
435
436 // Move the last argument "...name: type" to uf_va_name and
437 // uf_va_type.
438 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
439 [fp->uf_args.ga_len - 1];
440 --fp->uf_args.ga_len;
441 p = ((char_u **)argtypes->ga_data)[len];
442 if (p == NULL)
443 // TODO: get type from default value
444 fp->uf_va_type = &t_list_any;
445 else
446 {
447 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
448 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
449 {
450 semsg(_(e_variable_arguments_type_must_be_list_str),
451 ((char_u **)argtypes->ga_data)[len]);
452 return FAIL;
453 }
454 }
455 if (fp->uf_va_type == NULL)
456 return FAIL;
457 }
458
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100459 return OK;
460}
461
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100462 static int
463parse_return_type(ufunc_T *fp, char_u *ret_type)
464{
465 if (ret_type == NULL)
466 fp->uf_ret_type = &t_void;
467 else
468 {
469 char_u *p = ret_type;
470
471 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
472 if (fp->uf_ret_type == NULL)
473 {
474 fp->uf_ret_type = &t_void;
475 return FAIL;
476 }
477 }
478 return OK;
479}
480
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100481/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200482 * Register function "fp" as using "current_funccal" as its scope.
483 */
484 static int
485register_closure(ufunc_T *fp)
486{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200487 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100488 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200489 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200490 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200491 fp->uf_scoped = current_funccal;
492 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200493
Bram Moolenaar58016442016-07-31 18:30:22 +0200494 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
495 return FAIL;
496 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
497 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200498 return OK;
499}
500
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100501 static void
502set_ufunc_name(ufunc_T *fp, char_u *name)
503{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100504 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
505 // actually extends beyond the struct.
506 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100507
508 if (name[0] == K_SPECIAL)
509 {
510 fp->uf_name_exp = alloc(STRLEN(name) + 3);
511 if (fp->uf_name_exp != NULL)
512 {
513 STRCPY(fp->uf_name_exp, "<SNR>");
514 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
515 }
516 }
517}
518
Bram Moolenaar58016442016-07-31 18:30:22 +0200519/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200520 * Get a name for a lambda. Returned in static memory.
521 */
522 char_u *
523get_lambda_name(void)
524{
525 static char_u name[30];
526 static int lambda_no = 0;
527
528 sprintf((char*)name, "<lambda>%d", ++lambda_no);
529 return name;
530}
531
Bram Moolenaar801ab062020-06-25 19:27:56 +0200532#if defined(FEAT_LUA) || defined(PROTO)
533/*
534 * Registers a native C callback which can be called from Vim script.
535 * Returns the name of the Vim script function.
536 */
537 char_u *
538register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
539{
540 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200541 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200542
543 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
544 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200545 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200546
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200547 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200548 fp->uf_refcount = 1;
549 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000550 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200551 fp->uf_calls = 0;
552 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200553 fp->uf_cb = cb;
554 fp->uf_cb_free = cb_free;
555 fp->uf_cb_state = state;
556
557 set_ufunc_name(fp, name);
558 hash_add(&func_hashtab, UF2HIKEY(fp));
559
560 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200561}
562#endif
563
Bram Moolenaar04b12692020-05-04 23:24:44 +0200564/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100565 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100566 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100567 * If "white_error" is not NULL check for correct use of white space and set
568 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100569 * Return NULL if no valid arrow found.
570 */
571 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100572skip_arrow(
573 char_u *start,
574 int equal_arrow,
575 char_u **ret_type,
576 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100577{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100578 char_u *s = start;
579 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100580
581 if (equal_arrow)
582 {
583 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100584 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100585 if (white_error != NULL && !VIM_ISWHITE(s[1]))
586 {
587 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100588 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100589 return NULL;
590 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100591 s = skipwhite(s + 1);
592 *ret_type = s;
593 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100594 if (s == *ret_type)
595 {
596 emsg(_(e_missing_return_type));
597 return NULL;
598 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100599 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100600 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100601 s = skipwhite(s);
602 if (*s != '=')
603 return NULL;
604 ++s;
605 }
606 if (*s != '>')
607 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100608 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
609 || !IS_WHITE_OR_NUL(s[1])))
610 {
611 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100612 semsg(_(e_white_space_required_before_and_after_str_at_str),
613 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100614 return NULL;
615 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100616 return skipwhite(s + 1);
617}
618
619/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100620 * Check if "*cmd" points to a function command and if so advance "*cmd" and
621 * return TRUE.
622 * Otherwise return FALSE;
623 * Do not consider "function(" to be a command.
624 */
625 static int
626is_function_cmd(char_u **cmd)
627{
628 char_u *p = *cmd;
629
630 if (checkforcmd(&p, "function", 2))
631 {
632 if (*p == '(')
633 return FALSE;
634 *cmd = p;
635 return TRUE;
636 }
637 return FALSE;
638}
639
640/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200641 * Called when defining a function: The context may be needed for script
642 * variables declared in a block that is visible now but not when the function
643 * is compiled or called later.
644 */
645 static void
646function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
647{
648 if (cstack != NULL && cstack->cs_idx >= 0)
649 {
650 int count = cstack->cs_idx + 1;
651 int i;
652
653 fp->uf_block_ids = ALLOC_MULT(int, count);
654 if (fp->uf_block_ids != NULL)
655 {
656 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
657 sizeof(int) * count);
658 fp->uf_block_depth = count;
659 }
660
661 // Set flag in each block to indicate a function was defined. This
662 // is used to keep the variable when leaving the block, see
663 // hide_script_var().
664 for (i = 0; i <= cstack->cs_idx; ++i)
665 cstack->cs_flags[i] |= CSF_FUNC_DEF;
666 }
667}
668
669/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100670 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200671 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100672 * "newlines" must already have been initialized.
673 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
674 */
675 static int
676get_function_body(
677 exarg_T *eap,
678 garray_T *newlines,
679 char_u *line_arg_in,
680 char_u **line_to_free)
681{
682 linenr_T sourcing_lnum_top = SOURCING_LNUM;
683 linenr_T sourcing_lnum_off;
684 int saved_wait_return = need_wait_return;
685 char_u *line_arg = line_arg_in;
686 int vim9_function = eap->cmdidx == CMD_def
687 || eap->cmdidx == CMD_block;
688#define MAX_FUNC_NESTING 50
689 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200690 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100691 int nesting = 0;
692 getline_opt_T getline_options;
693 int indent = 2;
694 char_u *skip_until = NULL;
695 int ret = FAIL;
696 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200697 int heredoc_concat_len = 0;
698 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100699 char_u *heredoc_trimmed = NULL;
700
Bram Moolenaar20677332021-06-06 17:02:53 +0200701 ga_init2(&heredoc_ga, 1, 500);
702
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100703 // Detect having skipped over comment lines to find the return
704 // type. Add NULL lines to keep the line count correct.
705 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
706 if (SOURCING_LNUM < sourcing_lnum_off)
707 {
708 sourcing_lnum_off -= SOURCING_LNUM;
709 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
710 goto theend;
711 while (sourcing_lnum_off-- > 0)
712 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
713 }
714
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200715 nesting_def[0] = vim9_function;
716 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100717 getline_options = vim9_function
718 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
719 for (;;)
720 {
721 char_u *theline;
722 char_u *p;
723 char_u *arg;
724
725 if (KeyTyped)
726 {
727 msg_scroll = TRUE;
728 saved_wait_return = FALSE;
729 }
730 need_wait_return = FALSE;
731
732 if (line_arg != NULL)
733 {
734 // Use eap->arg, split up in parts by line breaks.
735 theline = line_arg;
736 p = vim_strchr(theline, '\n');
737 if (p == NULL)
738 line_arg += STRLEN(line_arg);
739 else
740 {
741 *p = NUL;
742 line_arg = p + 1;
743 }
744 }
745 else
746 {
Bram Moolenaar7473a842021-12-28 17:55:26 +0000747 theline = get_function_line(eap, line_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100748 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100749 }
750 if (KeyTyped)
751 lines_left = Rows - 1;
752 if (theline == NULL)
753 {
754 // Use the start of the function for the line number.
755 SOURCING_LNUM = sourcing_lnum_top;
756 if (skip_until != NULL)
757 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200758 else if (nesting_inline[nesting])
759 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100760 else if (eap->cmdidx == CMD_def)
761 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100762 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000763 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100764 goto theend;
765 }
766
767 // Detect line continuation: SOURCING_LNUM increased more than one.
768 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
769 if (SOURCING_LNUM < sourcing_lnum_off)
770 sourcing_lnum_off -= SOURCING_LNUM;
771 else
772 sourcing_lnum_off = 0;
773
774 if (skip_until != NULL)
775 {
776 // Don't check for ":endfunc"/":enddef" between
777 // * ":append" and "."
778 // * ":python <<EOF" and "EOF"
779 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
780 if (heredoc_trimmed == NULL
781 || (is_heredoc && skipwhite(theline) == theline)
782 || STRNCMP(theline, heredoc_trimmed,
783 STRLEN(heredoc_trimmed)) == 0)
784 {
785 if (heredoc_trimmed == NULL)
786 p = theline;
787 else if (is_heredoc)
788 p = skipwhite(theline) == theline
789 ? theline : theline + STRLEN(heredoc_trimmed);
790 else
791 p = theline + STRLEN(heredoc_trimmed);
792 if (STRCMP(p, skip_until) == 0)
793 {
794 VIM_CLEAR(skip_until);
795 VIM_CLEAR(heredoc_trimmed);
796 getline_options = vim9_function
797 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
798 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200799
800 if (heredoc_concat_len > 0)
801 {
802 // Replace the starting line with all the concatenated
803 // lines.
804 ga_concat(&heredoc_ga, theline);
805 vim_free(((char_u **)(newlines->ga_data))[
806 heredoc_concat_len - 1]);
807 ((char_u **)(newlines->ga_data))[
808 heredoc_concat_len - 1] = heredoc_ga.ga_data;
809 ga_init(&heredoc_ga);
810 heredoc_concat_len = 0;
811 theline += STRLEN(theline); // skip the "EOF"
812 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100813 }
814 }
815 }
816 else
817 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200818 int c;
819 char_u *end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100820
821 // skip ':' and blanks
822 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
823 ;
824
825 // Check for "endfunction", "enddef" or "}".
826 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200827 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100828 ? *p == '}'
829 : (checkforcmd(&p, nesting_def[nesting]
830 ? "enddef" : "endfunction", 4)
831 && *p != ':'))
832 {
833 if (nesting-- == 0)
834 {
835 char_u *nextcmd = NULL;
836
837 if (*p == '|' || *p == '}')
838 nextcmd = p + 1;
839 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
840 nextcmd = line_arg;
841 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100842 && (vim9_function || p_verbose > 0))
843 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200844 SOURCING_LNUM = sourcing_lnum_top
845 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100846 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000847 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100848 else
849 give_warning2((char_u *)
850 _("W22: Text found after :endfunction: %s"),
851 p, TRUE);
852 }
853 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100854 {
855 // Another command follows. If the line came from "eap"
856 // we can simply point into it, otherwise we need to
857 // change "eap->cmdlinep".
858 eap->nextcmd = nextcmd;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +0000859 if (*line_to_free != NULL
860 && *eap->cmdlinep != *line_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100861 {
862 vim_free(*eap->cmdlinep);
863 *eap->cmdlinep = *line_to_free;
864 *line_to_free = NULL;
865 }
866 }
867 break;
868 }
869 }
870
871 // Check for mismatched "endfunc" or "enddef".
872 // We don't check for "def" inside "func" thus we also can't check
873 // for "enddef".
874 // We continue to find the end of the function, although we might
875 // not find it.
876 else if (nesting_def[nesting])
877 {
878 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
879 emsg(_(e_mismatched_endfunction));
880 }
881 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
882 emsg(_(e_mismatched_enddef));
883
884 // Increase indent inside "if", "while", "for" and "try", decrease
885 // at "end".
886 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
887 indent -= 2;
888 else if (STRNCMP(p, "if", 2) == 0
889 || STRNCMP(p, "wh", 2) == 0
890 || STRNCMP(p, "for", 3) == 0
891 || STRNCMP(p, "try", 3) == 0)
892 indent += 2;
893
894 // Check for defining a function inside this function.
895 // Only recognize "def" inside "def", not inside "function",
896 // For backwards compatibility, see Test_function_python().
897 c = *p;
898 if (is_function_cmd(&p)
899 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
900 {
901 if (*p == '!')
902 p = skipwhite(p + 1);
903 p += eval_fname_script(p);
904 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
905 NULL, NULL));
906 if (*skipwhite(p) == '(')
907 {
908 if (nesting == MAX_FUNC_NESTING - 1)
909 emsg(_(e_function_nesting_too_deep));
910 else
911 {
912 ++nesting;
913 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200914 nesting_inline[nesting] = FALSE;
915 indent += 2;
916 }
917 }
918 }
919
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200920 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200921 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200922 // Not a comment line: check for nested inline function.
923 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200924 while (end > p && VIM_ISWHITE(*end))
925 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +0200926 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200927 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200928 int is_block;
929
930 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200931 --end;
932 while (end > p && VIM_ISWHITE(*end))
933 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200934 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
935 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200936 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200937 char_u *s = p;
938
939 // check for line starting with "au" for :autocmd or
940 // "com" for :command, these can use a {} block
941 is_block = checkforcmd_noparen(&s, "autocmd", 2)
942 || checkforcmd_noparen(&s, "command", 3);
943 }
944
945 if (is_block)
946 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200947 if (nesting == MAX_FUNC_NESTING - 1)
948 emsg(_(e_function_nesting_too_deep));
949 else
950 {
951 ++nesting;
952 nesting_def[nesting] = TRUE;
953 nesting_inline[nesting] = TRUE;
954 indent += 2;
955 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100956 }
957 }
958 }
959
960 // Check for ":append", ":change", ":insert". Not for :def.
961 p = skip_range(p, FALSE, NULL);
962 if (!vim9_function
963 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
964 || (p[0] == 'c'
965 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
966 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
967 && (STRNCMP(&p[3], "nge", 3) != 0
968 || !ASCII_ISALPHA(p[6])))))))
969 || (p[0] == 'i'
970 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
971 && (!ASCII_ISALPHA(p[2])
972 || (p[2] == 's'
973 && (!ASCII_ISALPHA(p[3])
974 || p[3] == 'e'))))))))
975 skip_until = vim_strsave((char_u *)".");
976
977 // Check for ":python <<EOF", ":tcl <<EOF", etc.
978 arg = skipwhite(skiptowhite(p));
979 if (arg[0] == '<' && arg[1] =='<'
980 && ((p[0] == 'p' && p[1] == 'y'
981 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
982 || ((p[2] == '3' || p[2] == 'x')
983 && !ASCII_ISALPHA(p[3]))))
984 || (p[0] == 'p' && p[1] == 'e'
985 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
986 || (p[0] == 't' && p[1] == 'c'
987 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
988 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
989 && !ASCII_ISALPHA(p[3]))
990 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
991 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
992 || (p[0] == 'm' && p[1] == 'z'
993 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
994 ))
995 {
996 // ":python <<" continues until a dot, like ":append"
997 p = skipwhite(arg + 2);
998 if (STRNCMP(p, "trim", 4) == 0)
999 {
1000 // Ignore leading white space.
1001 p = skipwhite(p + 4);
1002 heredoc_trimmed = vim_strnsave(theline,
1003 skipwhite(theline) - theline);
1004 }
1005 if (*p == NUL)
1006 skip_until = vim_strsave((char_u *)".");
1007 else
1008 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1009 getline_options = GETLINE_NONE;
1010 is_heredoc = TRUE;
Bram Moolenaar20677332021-06-06 17:02:53 +02001011 if (eap->cmdidx == CMD_def)
1012 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001013 }
1014
1015 // Check for ":cmd v =<< [trim] EOF"
1016 // and ":cmd [a, b] =<< [trim] EOF"
1017 // and "lines =<< [trim] EOF" for Vim9
1018 // Where "cmd" can be "let", "var", "final" or "const".
1019 arg = skipwhite(skiptowhite(p));
1020 if (*arg == '[')
1021 arg = vim_strchr(arg, ']');
1022 if (arg != NULL)
1023 {
1024 int found = (eap->cmdidx == CMD_def && arg[0] == '='
1025 && arg[1] == '<' && arg[2] =='<');
1026
1027 if (!found)
1028 // skip over the argument after "cmd"
1029 arg = skipwhite(skiptowhite(arg));
1030 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
1031 && (checkforcmd(&p, "let", 2)
1032 || checkforcmd(&p, "var", 3)
1033 || checkforcmd(&p, "final", 5)
1034 || checkforcmd(&p, "const", 5))))
1035 {
1036 p = skipwhite(arg + 3);
1037 if (STRNCMP(p, "trim", 4) == 0)
1038 {
1039 // Ignore leading white space.
1040 p = skipwhite(p + 4);
1041 heredoc_trimmed = vim_strnsave(theline,
1042 skipwhite(theline) - theline);
1043 }
1044 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1045 getline_options = GETLINE_NONE;
1046 is_heredoc = TRUE;
1047 }
1048 }
1049 }
1050
1051 // Add the line to the function.
1052 if (ga_grow(newlines, 1 + sourcing_lnum_off) == FAIL)
1053 goto theend;
1054
Bram Moolenaar20677332021-06-06 17:02:53 +02001055 if (heredoc_concat_len > 0)
1056 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001057 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001058 // to be used for the instruction later.
1059 ga_concat(&heredoc_ga, theline);
1060 ga_concat(&heredoc_ga, (char_u *)"\n");
1061 p = vim_strsave((char_u *)"");
1062 }
1063 else
1064 {
1065 // Copy the line to newly allocated memory. get_one_sourceline()
1066 // allocates 250 bytes per line, this saves 80% on average. The
1067 // cost is an extra alloc/free.
1068 p = vim_strsave(theline);
1069 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001070 if (p == NULL)
1071 goto theend;
1072 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1073
1074 // Add NULL lines for continuation lines, so that the line count is
1075 // equal to the index in the growarray.
1076 while (sourcing_lnum_off-- > 0)
1077 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1078
1079 // Check for end of eap->arg.
1080 if (line_arg != NULL && *line_arg == NUL)
1081 line_arg = NULL;
1082 }
1083
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001084 // Return OK when no error was detected.
1085 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001086 ret = OK;
1087
1088theend:
1089 vim_free(skip_until);
1090 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001091 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001092 need_wait_return |= saved_wait_return;
1093 return ret;
1094}
1095
1096/*
1097 * Handle the body of a lambda. *arg points to the "{", process statements
1098 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001099 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001100 * When successful "rettv" is set to a funcref.
1101 */
1102 static int
1103lambda_function_body(
1104 char_u **arg,
1105 typval_T *rettv,
1106 evalarg_T *evalarg,
1107 garray_T *newargs,
1108 garray_T *argtypes,
1109 int varargs,
1110 garray_T *default_args,
1111 char_u *ret_type)
1112{
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001113 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001114 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001115 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001116 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001117 exarg_T eap;
1118 garray_T newlines;
1119 char_u *cmdline = NULL;
1120 int ret = FAIL;
1121 char_u *line_to_free = NULL;
1122 partial_T *pt;
1123 char_u *name;
1124 int lnum_save = -1;
1125 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1126
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001127 if (!ends_excmd2(*arg, skipwhite(*arg + 1)))
1128 {
1129 semsg(_(e_trailing_arg), *arg + 1);
1130 return FAIL;
1131 }
1132
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001133 CLEAR_FIELD(eap);
1134 eap.cmdidx = CMD_block;
1135 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001136 eap.cmdlinep = &cmdline;
1137 eap.skip = !evaluate;
1138 if (evalarg->eval_cctx != NULL)
1139 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1140 else
1141 {
1142 eap.getline = evalarg->eval_getline;
1143 eap.cookie = evalarg->eval_cookie;
1144 }
1145
1146 ga_init2(&newlines, (int)sizeof(char_u *), 10);
1147 if (get_function_body(&eap, &newlines, NULL, &line_to_free) == FAIL)
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001148 {
1149 vim_free(cmdline);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001150 goto erret;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001151 }
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001152
1153 // When inside a lambda must add the function lines to evalarg.eval_ga.
1154 evalarg->eval_break_count += newlines.ga_len;
1155 if (gap->ga_itemsize > 0)
1156 {
1157 int idx;
1158 char_u *last;
1159 size_t plen;
1160 char_u *pnl;
1161
1162 for (idx = 0; idx < newlines.ga_len; ++idx)
1163 {
1164 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1165
Bram Moolenaarecb66452021-05-18 15:09:18 +02001166 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001167 goto erret;
1168
1169 // Going to concatenate the lines after parsing. For an empty or
1170 // comment line use an empty string.
1171 // Insert NL characters at the start of each line, the string will
1172 // be split again later in .get_lambda_tv().
1173 if (*p == NUL || vim9_comment_start(p))
1174 p = (char_u *)"";
1175 plen = STRLEN(p);
1176 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1177 if (pnl != NULL)
1178 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001179 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1180 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001181 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001182 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001183 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001184 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001185 // more is following after the "}", which was skipped
1186 last = cmdline;
1187 else
1188 // nothing is following the "}"
1189 last = (char_u *)"}";
1190 plen = STRLEN(last);
1191 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1192 if (pnl != NULL)
1193 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001194 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1195 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001196 }
1197
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001198 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001199 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001200 garray_T *tfgap = &evalarg->eval_tofree_ga;
1201
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001202 // Something comes after the "}".
1203 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001204
1205 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001206 if (ga_grow(tfgap, 1) == OK)
1207 {
1208 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1209 evalarg->eval_using_cmdline = TRUE;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001210 if (cmdline == line_to_free)
1211 line_to_free = NULL;
Bram Moolenaar844fb642021-10-23 13:32:30 +01001212 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001213 }
1214 else
1215 *arg = (char_u *)"";
1216
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001217 if (!evaluate)
1218 {
1219 ret = OK;
1220 goto erret;
1221 }
1222
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001223 name = get_lambda_name();
1224 ufunc = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
1225 if (ufunc == NULL)
1226 goto erret;
1227 set_ufunc_name(ufunc, name);
1228 if (hash_add(&func_hashtab, UF2HIKEY(ufunc)) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001229 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001230 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001231 ufunc->uf_refcount = 1;
1232 ufunc->uf_args = *newargs;
1233 newargs->ga_data = NULL;
1234 ufunc->uf_def_args = *default_args;
1235 default_args->ga_data = NULL;
1236 ufunc->uf_func_type = &t_func_any;
1237
1238 // error messages are for the first function line
1239 lnum_save = SOURCING_LNUM;
1240 SOURCING_LNUM = sourcing_lnum_top;
1241
1242 // parse argument types
1243 if (parse_argument_types(ufunc, argtypes, varargs) == FAIL)
1244 {
1245 SOURCING_LNUM = lnum_save;
1246 goto erret;
1247 }
1248
1249 // parse the return type, if any
1250 if (parse_return_type(ufunc, ret_type) == FAIL)
1251 goto erret;
1252
1253 pt = ALLOC_CLEAR_ONE(partial_T);
1254 if (pt == NULL)
1255 goto erret;
1256 pt->pt_func = ufunc;
1257 pt->pt_refcount = 1;
1258
1259 ufunc->uf_lines = newlines;
1260 newlines.ga_data = NULL;
1261 if (sandbox)
1262 ufunc->uf_flags |= FC_SANDBOX;
1263 if (!ASCII_ISUPPER(*ufunc->uf_name))
1264 ufunc->uf_flags |= FC_VIM9;
1265 ufunc->uf_script_ctx = current_sctx;
1266 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1267 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1268 set_function_type(ufunc);
1269
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001270 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1271
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001272 rettv->vval.v_partial = pt;
1273 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001274 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001275 ret = OK;
1276
1277erret:
1278 if (lnum_save >= 0)
1279 SOURCING_LNUM = lnum_save;
1280 vim_free(line_to_free);
1281 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001282 if (newargs != NULL)
1283 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001284 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001285 if (ufunc != NULL)
1286 {
1287 func_clear(ufunc, TRUE);
1288 func_free(ufunc, TRUE);
1289 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001290 return ret;
1291}
1292
1293/*
1294 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001295 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001296 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001297 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1298 */
1299 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001300get_lambda_tv(
1301 char_u **arg,
1302 typval_T *rettv,
1303 int types_optional,
1304 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001305{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001306 int evaluate = evalarg != NULL
1307 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001308 garray_T newargs;
1309 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001310 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001311 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001312 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001313 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001314 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001315 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001316 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001317 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001318 char_u *s;
1319 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001320 int *old_eval_lavars = eval_lavars_used;
1321 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001322 char_u *tofree1 = NULL;
1323 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001324 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001325 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001326 int called_emsg_start = called_emsg;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001327
1328 if (equal_arrow && !in_vim9script())
1329 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001330
1331 ga_init(&newargs);
1332 ga_init(&newlines);
1333
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001334 // First, check if this is really a lambda expression. "->" or "=>" must
1335 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001336 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001337 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001338 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar14ded112021-06-26 19:25:49 +02001339 NULL, &default_args, TRUE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001340 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001341 {
1342 if (types_optional)
1343 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +01001344 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001345 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001346
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001347 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001348 if (evaluate)
1349 pnewargs = &newargs;
1350 else
1351 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001352 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001353 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001354 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001355 &varargs, &default_args,
1356 FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001357 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001358 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaare462f522020-12-27 14:43:30 +01001359 equal_arrow || in_vim9script() ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001360 {
1361 if (types_optional)
1362 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001363 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001364 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001365 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001366 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001367
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001368 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1369 if (ret_type != NULL)
1370 {
1371 ret_type = vim_strsave(ret_type);
1372 tofree2 = ret_type;
1373 }
1374
Bram Moolenaare38eab22019-12-05 21:50:01 +01001375 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001376 if (evaluate)
1377 eval_lavars_used = &eval_lavars;
1378
Bram Moolenaar65c44152020-12-24 15:14:01 +01001379 *arg = skipwhite_and_linebreak(*arg, evalarg);
1380
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001381 // Recognize "{" as the start of a function body.
1382 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001383 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001384 if (evalarg == NULL)
1385 // cannot happen?
1386 goto theend;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001387 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1388 types_optional ? &argtypes : NULL, varargs,
1389 &default_args, ret_type) == FAIL)
1390 goto errret;
1391 goto theend;
1392 }
1393 if (default_args.ga_len > 0)
1394 {
1395 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001396 goto errret;
1397 }
1398
Bram Moolenaare38eab22019-12-05 21:50:01 +01001399 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001400 start = *arg;
1401 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001402 if (ret == FAIL)
1403 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001404 if (evalarg != NULL)
1405 {
1406 // avoid that the expression gets freed when another line break follows
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001407 tofree1 = evalarg->eval_tofree;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001408 evalarg->eval_tofree = NULL;
1409 }
1410
Bram Moolenaar65c44152020-12-24 15:14:01 +01001411 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001412 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001413 *arg = skipwhite_and_linebreak(*arg, evalarg);
1414 if (**arg != '}')
1415 {
1416 semsg(_("E451: Expected }: %s"), *arg);
1417 goto errret;
1418 }
1419 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001420 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001421
1422 if (evaluate)
1423 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001424 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001425 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001426 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001427 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001428 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001429
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001430 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001431 if (fp == NULL)
1432 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001433 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001434 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001435 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001436 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001437
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001438 ga_init2(&newlines, (int)sizeof(char_u *), 1);
1439 if (ga_grow(&newlines, 1) == FAIL)
1440 goto errret;
1441
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001442 // If there are line breaks, we need to split up the string.
1443 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001444 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001445 line_end = end;
1446
1447 // Add "return " before the expression (or the first line).
1448 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001449 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001450 if (p == NULL)
1451 goto errret;
1452 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1453 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001454 vim_strncpy(p + 7, start, line_end - start);
1455
1456 if (line_end != end)
1457 {
1458 // Add more lines, split by line breaks. Thus is used when a
1459 // lambda with { cmds } is encountered.
1460 while (*line_end == '\n')
1461 {
1462 if (ga_grow(&newlines, 1) == FAIL)
1463 goto errret;
1464 start = line_end + 1;
1465 line_end = vim_strchr(start, '\n');
1466 if (line_end == NULL)
1467 line_end = end;
1468 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1469 vim_strnsave(start, line_end - start);
1470 }
1471 }
1472
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001473 if (strstr((char *)p + 7, "a:") == NULL)
1474 // No a: variables are used for sure.
1475 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001476
1477 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001478 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001479 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001480 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001481 if (types_optional)
1482 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001483 if (parse_argument_types(fp, &argtypes,
1484 in_vim9script() && varargs) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001485 goto errret;
1486 if (ret_type != NULL)
1487 {
1488 fp->uf_ret_type = parse_type(&ret_type,
1489 &fp->uf_type_list, TRUE);
1490 if (fp->uf_ret_type == NULL)
1491 goto errret;
1492 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001493 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001494 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001495 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001496
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001497 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001498 if (current_funccal != NULL && eval_lavars)
1499 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001500 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001501 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001502 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001503 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001504
1505#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001506 if (prof_def_func())
1507 func_do_profile(fp);
1508#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001509 if (sandbox)
1510 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001511 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001512 // uf_args.ga_len. In Vim9 script "...name" has to be used.
1513 fp->uf_varargs = !in_vim9script() || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001514 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001515 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001516 fp->uf_script_ctx = current_sctx;
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001517 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len + 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001518
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001519 function_using_block_scopes(fp, evalarg->eval_cstack);
1520
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001521 pt->pt_func = fp;
1522 pt->pt_refcount = 1;
1523 rettv->vval.v_partial = pt;
1524 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001525
1526 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001527 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001528
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001529theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001530 eval_lavars_used = old_eval_lavars;
Bram Moolenaarefaaaa62020-07-08 22:24:09 +02001531 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001532 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001533 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001534 vim_free(tofree1);
1535 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001536 if (types_optional)
1537 ga_clear_strings(&argtypes);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001538
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001539 return OK;
1540
1541errret:
1542 ga_clear_strings(&newargs);
1543 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001544 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001545 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001546 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001547 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001548 if (fp != NULL)
1549 vim_free(fp->uf_arg_types);
1550 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001551 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001552 vim_free(pt);
Bram Moolenaarefaaaa62020-07-08 22:24:09 +02001553 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001554 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001555 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001556 vim_free(tofree1);
1557 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001558 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001559 return FAIL;
1560}
1561
1562/*
1563 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1564 * name it contains, otherwise return "name".
1565 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1566 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001567 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1568 * type of the variable.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001569 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001570 */
1571 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001572deref_func_name(
1573 char_u *name,
1574 int *lenp,
1575 partial_T **partialp,
1576 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001577 int no_autoload,
1578 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001579{
1580 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001581 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001582 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001583 char_u *s = NULL;
1584 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001585 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001586
1587 if (partialp != NULL)
1588 *partialp = NULL;
1589
1590 cc = name[*lenp];
1591 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001592
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001593 v = find_var(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001594 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001595 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001596 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001597 tv = &v->di_tv;
1598 }
1599 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1600 {
1601 imported_T *import;
1602 char_u *p = name;
1603 int len = *lenp;
1604
1605 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001606 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001607 p = name + 2;
1608 len -= 2;
1609 }
1610 import = find_imported(p, len, NULL);
1611
1612 // imported variable from another script
1613 if (import != NULL)
1614 {
1615 if (import->imp_funcname != NULL)
1616 {
1617 s = import->imp_funcname;
1618 *lenp = (int)STRLEN(s);
1619 return s;
1620 }
Bram Moolenaarf8a79fc2021-12-14 12:06:16 +00001621 if (import->imp_flags & IMP_FLAGS_STAR)
1622 {
1623 name[len] = NUL;
1624 semsg(_(e_cannot_use_str_itself_it_is_imported_with_star),
1625 name);
1626 name[len] = cc;
1627 *lenp = 0;
1628 return (char_u *)""; // just in case
1629 }
1630 else
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001631 {
1632 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
1633 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1634 + import->imp_var_vals_idx;
1635 tv = sv->sv_tv;
1636 if (type != NULL)
1637 *type = sv->sv_type;
1638 did_type = TRUE;
1639 }
1640 }
1641 }
1642
1643 if (tv != NULL)
1644 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001645 if (found_var != NULL)
1646 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001647 if (tv->v_type == VAR_FUNC)
1648 {
1649 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001650 {
1651 *lenp = 0;
1652 return (char_u *)""; // just in case
1653 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001654 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001655 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001656 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001657
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001658 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001659 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001660 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001661
1662 if (pt == NULL)
1663 {
1664 *lenp = 0;
1665 return (char_u *)""; // just in case
1666 }
1667 if (partialp != NULL)
1668 *partialp = pt;
1669 s = partial_name(pt);
1670 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001671 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001672
1673 if (s != NULL)
1674 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001675 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001676 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001677 svar_T *sv = find_typval_in_script(tv);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001678
1679 if (sv != NULL)
1680 *type = sv->sv_type;
1681 }
1682 return s;
1683 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001684 }
1685
1686 return name;
1687}
1688
1689/*
1690 * Give an error message with a function name. Handle <SNR> things.
1691 * "ermsg" is to be passed without translation, use N_() instead of _().
1692 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001693 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001694emsg_funcname(char *ermsg, char_u *name)
1695{
1696 char_u *p;
1697
1698 if (*name == K_SPECIAL)
1699 p = concat_str((char_u *)"<SNR>", name + 3);
1700 else
1701 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001702 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001703 if (p != name)
1704 vim_free(p);
1705}
1706
1707/*
1708 * Allocate a variable for the result of a function.
1709 * Return OK or FAIL.
1710 */
1711 int
1712get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001713 char_u *name, // name of the function
1714 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001715 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001716 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +02001717 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001718 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001719{
1720 char_u *argp;
1721 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001722 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1723 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001724 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001725
1726 /*
1727 * Get the arguments.
1728 */
1729 argp = *arg;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001730 while (argcount < MAX_FUNC_ARGS - (funcexe->fe_partial == NULL ? 0
1731 : funcexe->fe_partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001732 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001733 // skip the '(' or ',' and possibly line breaks
1734 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1735
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001736 if (*argp == ')' || *argp == ',' || *argp == NUL)
1737 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001738 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001739 {
1740 ret = FAIL;
1741 break;
1742 }
1743 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001744 // The comma should come right after the argument, but this wasn't
1745 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001746 if (vim9script)
1747 {
1748 if (*argp != ',' && *skipwhite(argp) == ',')
1749 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001750 semsg(_(e_no_white_space_allowed_before_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001751 ret = FAIL;
1752 break;
1753 }
1754 }
1755 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001756 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001757 if (*argp != ',')
1758 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001759 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1760 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001761 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001762 ret = FAIL;
1763 break;
1764 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001765 }
Bram Moolenaare6b53242020-07-01 17:28:33 +02001766 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001767 if (*argp == ')')
1768 ++argp;
1769 else
1770 ret = FAIL;
1771
1772 if (ret == OK)
1773 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001774 int i = 0;
1775 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001776
1777 if (get_vim_var_nr(VV_TESTING))
1778 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001779 // Prepare for calling test_garbagecollect_now(), need to know
1780 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001781 if (funcargs.ga_itemsize == 0)
1782 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
1783 for (i = 0; i < argcount; ++i)
1784 if (ga_grow(&funcargs, 1) == OK)
1785 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1786 &argvars[i];
1787 }
1788
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001789 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001790 if (in_vim9script() && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001791 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001792 // An error in a builtin function does not return FAIL, but we do
1793 // want to abort further processing if an error was given.
1794 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001795 clear_tv(rettv);
1796 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001797
1798 funcargs.ga_len -= i;
1799 }
1800 else if (!aborting())
1801 {
1802 if (argcount == MAX_FUNC_ARGS)
1803 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
1804 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001805 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001806 }
1807
1808 while (--argcount >= 0)
1809 clear_tv(&argvars[argcount]);
1810
Bram Moolenaar8294d492020-08-10 22:40:56 +02001811 if (in_vim9script())
1812 *arg = argp;
1813 else
1814 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001815 return ret;
1816}
1817
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001818/*
1819 * Return TRUE if "p" starts with "<SID>" or "s:".
1820 * Only works if eval_fname_script() returned non-zero for "p"!
1821 */
1822 static int
1823eval_fname_sid(char_u *p)
1824{
1825 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1826}
1827
1828/*
1829 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1830 * Change <SNR>123_name() to K_SNR 123_name().
1831 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
1832 * (slow).
1833 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001834 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001835fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
1836{
1837 int llen;
1838 char_u *fname;
1839 int i;
1840
1841 llen = eval_fname_script(name);
1842 if (llen > 0)
1843 {
1844 fname_buf[0] = K_SPECIAL;
1845 fname_buf[1] = KS_EXTRA;
1846 fname_buf[2] = (int)KE_SNR;
1847 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001848 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001849 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001850 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001851 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001852 else
1853 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001854 sprintf((char *)fname_buf + 3, "%ld_",
1855 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001856 i = (int)STRLEN(fname_buf);
1857 }
1858 }
1859 if (i + STRLEN(name + llen) < FLEN_FIXED)
1860 {
1861 STRCPY(fname_buf + i, name + llen);
1862 fname = fname_buf;
1863 }
1864 else
1865 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001866 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001867 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +01001868 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001869 else
1870 {
1871 *tofree = fname;
1872 mch_memmove(fname, fname_buf, (size_t)i);
1873 STRCPY(fname + i, name + llen);
1874 }
1875 }
1876 }
1877 else
1878 fname = name;
1879 return fname;
1880}
1881
1882/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883 * Find a function "name" in script "sid".
1884 */
1885 static ufunc_T *
1886find_func_with_sid(char_u *name, int sid)
1887{
1888 hashitem_T *hi;
1889 char_u buffer[200];
1890
1891 buffer[0] = K_SPECIAL;
1892 buffer[1] = KS_EXTRA;
1893 buffer[2] = (int)KE_SNR;
1894 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
1895 (long)sid, name);
1896 hi = hash_find(&func_hashtab, buffer);
1897 if (!HASHITEM_EMPTY(hi))
1898 return HI2UF(hi);
1899
1900 return NULL;
1901}
1902
1903/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001904 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001905 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001906 * Return NULL for unknown function.
1907 */
Bram Moolenaarce658352020-07-31 23:47:12 +02001908 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001909find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001910{
1911 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 ufunc_T *func;
1913 imported_T *imported;
1914
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001915 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 {
Bram Moolenaar333894b2020-08-01 18:53:07 +02001917 char_u *after_script = NULL;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001918 long sid = 0;
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001919 int find_script_local = in_vim9script() && eval_isnamec1(*name)
1920 && (name[1] != ':' || *name == 's');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921
Bram Moolenaar95006e32020-08-29 17:47:08 +02001922 if (find_script_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001924 // Find script-local function before global one.
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001925 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
1926 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001927 if (func != NULL)
1928 return func;
1929 }
1930
Bram Moolenaarefa94442020-08-08 22:16:00 +02001931 if (name[0] == K_SPECIAL
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001932 && name[1] == KS_EXTRA
1933 && name[2] == KE_SNR)
1934 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001935 // Caller changes s: to <SNR>99_name.
1936
1937 after_script = name + 3;
1938 sid = getdigits(&after_script);
Bram Moolenaarefa94442020-08-08 22:16:00 +02001939 if (*after_script == '_')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001940 ++after_script;
1941 else
1942 after_script = NULL;
1943 }
Bram Moolenaar95006e32020-08-29 17:47:08 +02001944 if (find_script_local || after_script != NULL)
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001945 {
1946 // Find imported function before global one.
Bram Moolenaarefa94442020-08-08 22:16:00 +02001947 if (after_script != NULL && sid != current_sctx.sc_sid)
1948 imported = find_imported_in_script(after_script, 0, sid);
1949 else
1950 imported = find_imported(after_script == NULL
1951 ? name : after_script, 0, cctx);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001952 if (imported != NULL && imported->imp_funcname != NULL)
1953 {
1954 hi = hash_find(&func_hashtab, imported->imp_funcname);
1955 if (!HASHITEM_EMPTY(hi))
1956 return HI2UF(hi);
1957 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 }
1959 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001960
Bram Moolenaara26b9702020-04-18 19:53:28 +02001961 hi = hash_find(&func_hashtab,
1962 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001963 if (!HASHITEM_EMPTY(hi))
1964 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965
1966 return NULL;
1967}
1968
1969/*
1970 * Find a function by name, return pointer to it in ufuncs.
1971 * "cctx" is passed in a :def function to find imported functions.
1972 * Return NULL for unknown or dead function.
1973 */
1974 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001975find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001977 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978
1979 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
1980 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001981 return NULL;
1982}
1983
1984/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02001985 * Return TRUE if "ufunc" is a global function.
1986 */
1987 int
1988func_is_global(ufunc_T *ufunc)
1989{
1990 return ufunc->uf_name[0] != K_SPECIAL;
1991}
1992
1993/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001994 * Copy the function name of "fp" to buffer "buf".
1995 * "buf" must be able to hold the function name plus three bytes.
1996 * Takes care of script-local function names.
1997 */
1998 static void
1999cat_func_name(char_u *buf, ufunc_T *fp)
2000{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002001 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002002 {
2003 STRCPY(buf, "<SNR>");
2004 STRCAT(buf, fp->uf_name + 3);
2005 }
2006 else
2007 STRCPY(buf, fp->uf_name);
2008}
2009
2010/*
2011 * Add a number variable "name" to dict "dp" with value "nr".
2012 */
2013 static void
2014add_nr_var(
2015 dict_T *dp,
2016 dictitem_T *v,
2017 char *name,
2018 varnumber_T nr)
2019{
2020 STRCPY(v->di_key, name);
2021 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2022 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
2023 v->di_tv.v_type = VAR_NUMBER;
2024 v->di_tv.v_lock = VAR_FIXED;
2025 v->di_tv.vval.v_number = nr;
2026}
2027
2028/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002029 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002030 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002031 static void
2032free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002033{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002034 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002035
2036 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2037 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002038 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002039
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002040 // When garbage collecting a funccall_T may be freed before the
2041 // function that references it, clear its uf_scoped field.
2042 // The function may have been redefined and point to another
2043 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002044 if (fp != NULL && fp->uf_scoped == fc)
2045 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002046 }
Bram Moolenaar58016442016-07-31 18:30:22 +02002047 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002048
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002049 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002050 vim_free(fc);
2051}
2052
2053/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002054 * Free "fc" and what it contains.
2055 * Can be called only when "fc" is kept beyond the period of it called,
2056 * i.e. after cleanup_function_call(fc).
2057 */
2058 static void
2059free_funccal_contents(funccall_T *fc)
2060{
2061 listitem_T *li;
2062
2063 // Free all l: variables.
2064 vars_clear(&fc->l_vars.dv_hashtab);
2065
2066 // Free all a: variables.
2067 vars_clear(&fc->l_avars.dv_hashtab);
2068
2069 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002070 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002071 clear_tv(&li->li_tv);
2072
2073 free_funccal(fc);
2074}
2075
2076/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002077 * Handle the last part of returning from a function: free the local hashtable.
2078 * Unless it is still in use by a closure.
2079 */
2080 static void
2081cleanup_function_call(funccall_T *fc)
2082{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002083 int may_free_fc = fc->fc_refcount <= 0;
2084 int free_fc = TRUE;
2085
Bram Moolenaar6914c642017-04-01 21:21:30 +02002086 current_funccal = fc->caller;
2087
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002088 // Free all l: variables if not referred.
2089 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
2090 vars_clear(&fc->l_vars.dv_hashtab);
2091 else
2092 free_fc = FALSE;
2093
2094 // If the a:000 list and the l: and a: dicts are not referenced and
2095 // there is no closure using it, we can free the funccall_T and what's
2096 // in it.
2097 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
2098 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002099 else
2100 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002101 int todo;
2102 hashitem_T *hi;
2103 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002104
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002105 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002106
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002107 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002108 todo = (int)fc->l_avars.dv_hashtab.ht_used;
2109 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
2110 {
2111 if (!HASHITEM_EMPTY(hi))
2112 {
2113 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002114 di = HI2DI(hi);
2115 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002116 }
2117 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002118 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002119
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002120 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2121 fc->l_varlist.lv_first = NULL;
2122 else
2123 {
2124 listitem_T *li;
2125
2126 free_fc = FALSE;
2127
2128 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002129 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002130 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002131 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002132
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002133 if (free_fc)
2134 free_funccal(fc);
2135 else
2136 {
2137 static int made_copy = 0;
2138
2139 // "fc" is still in use. This can happen when returning "a:000",
2140 // assigning "l:" to a global variable or defining a closure.
2141 // Link "fc" in the list for garbage collection later.
2142 fc->caller = previous_funccal;
2143 previous_funccal = fc;
2144
2145 if (want_garbage_collect)
2146 // If garbage collector is ready, clear count.
2147 made_copy = 0;
2148 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002149 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002150 // We have made a lot of copies, worth 4 Mbyte. This can happen
2151 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002152 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002153 // too much memory.
2154 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002155 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002156 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002157 }
2158}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002159
2160/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002161 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2162 */
2163 static int
2164numbered_function(char_u *name)
2165{
2166 return isdigit(*name)
2167 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2168}
2169
2170/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002171 * There are two kinds of function names:
2172 * 1. ordinary names, function defined with :function or :def
2173 * 2. numbered functions and lambdas
2174 * For the first we only count the name stored in func_hashtab as a reference,
2175 * using function() does not count as a reference, because the function is
2176 * looked up by name.
2177 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002178 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002179func_name_refcount(char_u *name)
2180{
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002181 return numbered_function(name) || *name == '<';
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002182}
2183
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184/*
2185 * Unreference "fc": decrement the reference count and free it when it
2186 * becomes zero. "fp" is detached from "fc".
2187 * When "force" is TRUE we are exiting.
2188 */
2189 static void
2190funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2191{
2192 funccall_T **pfc;
2193 int i;
2194
2195 if (fc == NULL)
2196 return;
2197
2198 if (--fc->fc_refcount <= 0 && (force || (
2199 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
2200 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
2201 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2202 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
2203 {
2204 if (fc == *pfc)
2205 {
2206 *pfc = fc->caller;
2207 free_funccal_contents(fc);
2208 return;
2209 }
2210 }
2211 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2212 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
2213 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
2214}
2215
2216/*
2217 * Remove the function from the function hashtable. If the function was
2218 * deleted while it still has references this was already done.
2219 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2220 */
2221 static int
2222func_remove(ufunc_T *fp)
2223{
2224 hashitem_T *hi;
2225
2226 // Return if it was already virtually deleted.
2227 if (fp->uf_flags & FC_DEAD)
2228 return FALSE;
2229
2230 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
2231 if (!HASHITEM_EMPTY(hi))
2232 {
2233 // When there is a def-function index do not actually remove the
2234 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002235 // Do remove it when it's a copy.
2236 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaarc970e422021-03-17 15:03:04 +01002237 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002239 return FALSE;
2240 }
2241 hash_remove(&func_hashtab, hi);
2242 fp->uf_flags |= FC_DELETED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243 return TRUE;
2244 }
2245 return FALSE;
2246}
2247
2248 static void
2249func_clear_items(ufunc_T *fp)
2250{
2251 ga_clear_strings(&(fp->uf_args));
2252 ga_clear_strings(&(fp->uf_def_args));
2253 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002255 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002256 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002257 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002258
2259 // Increment the refcount of this function to avoid it being freed
2260 // recursively when the partial is freed.
2261 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002262 partial_unref(fp->uf_partial);
2263 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002264 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002265
2266#ifdef FEAT_LUA
2267 if (fp->uf_cb_free != NULL)
2268 {
2269 fp->uf_cb_free(fp->uf_cb_state);
2270 fp->uf_cb_free = NULL;
2271 }
2272
2273 fp->uf_cb_state = NULL;
2274 fp->uf_cb = NULL;
2275#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276#ifdef FEAT_PROFILE
2277 VIM_CLEAR(fp->uf_tml_count);
2278 VIM_CLEAR(fp->uf_tml_total);
2279 VIM_CLEAR(fp->uf_tml_self);
2280#endif
2281}
2282
2283/*
2284 * Free all things that a function contains. Does not free the function
2285 * itself, use func_free() for that.
2286 * When "force" is TRUE we are exiting.
2287 */
2288 static void
2289func_clear(ufunc_T *fp, int force)
2290{
2291 if (fp->uf_cleared)
2292 return;
2293 fp->uf_cleared = TRUE;
2294
2295 // clear this function
2296 func_clear_items(fp);
2297 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002298 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299}
2300
2301/*
2302 * Free a function and remove it from the list of functions. Does not free
2303 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002304 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002305 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002307 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002308func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309{
2310 // Only remove it when not done already, otherwise we would remove a newer
2311 // version of the function with the same name.
2312 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2313 func_remove(fp);
2314
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002315 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002316 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002317 if (fp->uf_dfunc_idx > 0)
2318 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002319 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002321 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002322 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002323 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324}
2325
2326/*
2327 * Free all things that a function contains and free the function itself.
2328 * When "force" is TRUE we are exiting.
2329 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002330 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002331func_clear_free(ufunc_T *fp, int force)
2332{
2333 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002334 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2335 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002336 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002337 else
2338 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339}
2340
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002341/*
2342 * Copy already defined function "lambda" to a new function with name "global".
2343 * This is for when a compiled function defines a global function.
2344 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002345 int
2346copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002347{
2348 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002349 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002350
2351 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002352 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002353 semsg(_(e_lambda_function_not_found_str), lambda);
2354 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002355 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002356
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002357 fp = find_func(global, TRUE, NULL);
2358 if (fp != NULL)
2359 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002360 // TODO: handle ! to overwrite
Bram Moolenaarc553a212021-12-26 20:20:34 +00002361 semsg(_(e_function_str_already_exists_add_excl_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002362 return FAIL;
2363 }
2364
2365 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
2366 if (fp == NULL)
2367 return FAIL;
2368
2369 fp->uf_varargs = ufunc->uf_varargs;
2370 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2371 fp->uf_def_status = ufunc->uf_def_status;
2372 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2373 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2374 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2375 == FAIL
2376 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2377 goto failed;
2378
2379 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
2380 : vim_strsave(ufunc->uf_name_exp);
2381 if (ufunc->uf_arg_types != NULL)
2382 {
2383 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2384 if (fp->uf_arg_types == NULL)
2385 goto failed;
2386 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2387 sizeof(type_T *) * fp->uf_args.ga_len);
2388 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002389 if (ufunc->uf_va_name != NULL)
2390 {
2391 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2392 if (fp->uf_va_name == NULL)
2393 goto failed;
2394 }
2395 fp->uf_ret_type = ufunc->uf_ret_type;
2396
2397 fp->uf_refcount = 1;
2398 STRCPY(fp->uf_name, global);
2399 hash_add(&func_hashtab, UF2HIKEY(fp));
2400
2401 // the referenced dfunc_T is now used one more time
2402 link_def_function(fp);
2403
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002404 // Create a partial to store the context of the function where it was
2405 // instantiated. Only needs to be done once. Do this on the original
2406 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002407 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2408 {
2409 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2410
2411 if (pt == NULL)
2412 goto failed;
2413 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002414 {
2415 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002416 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002417 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002418 ufunc->uf_partial = pt;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002419 --pt->pt_refcount; // not actually referenced here
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002420 }
2421
2422 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002423
2424failed:
2425 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002426 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002427}
2428
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002429static int funcdepth = 0;
2430
2431/*
2432 * Increment the function call depth count.
2433 * Return FAIL when going over 'maxfuncdepth'.
2434 * Otherwise return OK, must call funcdepth_decrement() later!
2435 */
2436 int
2437funcdepth_increment(void)
2438{
2439 if (funcdepth >= p_mfd)
2440 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002441 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002442 return FAIL;
2443 }
2444 ++funcdepth;
2445 return OK;
2446}
2447
2448 void
2449funcdepth_decrement(void)
2450{
2451 --funcdepth;
2452}
2453
2454/*
2455 * Get the current function call depth.
2456 */
2457 int
2458funcdepth_get(void)
2459{
2460 return funcdepth;
2461}
2462
2463/*
2464 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002465 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002466 * times as funcdepth_increment().
2467 */
2468 void
2469funcdepth_restore(int depth)
2470{
2471 funcdepth = depth;
2472}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002473
2474/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002475 * Call a user function.
2476 */
2477 static void
2478call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002479 ufunc_T *fp, // pointer to function
2480 int argcount, // nr of args
2481 typval_T *argvars, // arguments
2482 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002483 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002484 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002485{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002486 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002487 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002488 funccall_T *fc;
2489 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002490 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002491 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002492 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002493 int i;
2494 int ai;
2495 int islambda = FALSE;
2496 char_u numbuf[NUMBUFLEN];
2497 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002498 typval_T *tv_to_free[MAX_FUNC_ARGS];
2499 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002500#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002501 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002502#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002503 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002504
Bram Moolenaarb2049902021-01-24 12:53:53 +01002505#ifdef FEAT_PROFILE
2506 CLEAR_FIELD(profile_info);
2507#endif
2508
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002509 // If depth of calling is getting too high, don't execute the function.
2510 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002511 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002512 rettv->v_type = VAR_NUMBER;
2513 rettv->vval.v_number = -1;
2514 return;
2515 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002516
Bram Moolenaare38eab22019-12-05 21:50:01 +01002517 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002518
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002519 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002520 if (fc == NULL)
2521 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002522 fc->caller = current_funccal;
2523 current_funccal = fc;
2524 fc->func = fp;
2525 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002526 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002527 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002528 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2529 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002530 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002531 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002532 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002533
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002534 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002536#ifdef FEAT_PROFILE
2537 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2538#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002539 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002540#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002541 if (do_profiling == PROF_YES)
2542 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002543#endif
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002544 call_def_function(fp, argcount, argvars, funcexe->fe_partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002545 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002546#ifdef FEAT_PROFILE
2547 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002548 || (caller != NULL && caller->uf_profiling)))
2549 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002550#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 free_funccal(fc);
2553 return;
2554 }
2555
Bram Moolenaar38453522021-11-28 22:00:12 +00002556 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002557
2558 /*
2559 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
2560 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
2561 * each argument variable and saves a lot of time.
2562 */
2563 /*
2564 * Init l: variables.
2565 */
2566 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
2567 if (selfdict != NULL)
2568 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002569 // Set l:self to "selfdict". Use "name" to avoid a warning from
2570 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002571 v = &fc->fixvar[fixvar_idx++].var;
2572 name = v->di_key;
2573 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002574 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002575 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
2576 v->di_tv.v_type = VAR_DICT;
2577 v->di_tv.v_lock = 0;
2578 v->di_tv.vval.v_dict = selfdict;
2579 ++selfdict->dv_refcount;
2580 }
2581
2582 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002583 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002584 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002585 * Set a:000 to a list with room for the "..." arguments.
2586 */
2587 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002588 if ((fp->uf_flags & FC_NOARGS) == 0)
2589 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002590 (varnumber_T)(argcount >= fp->uf_args.ga_len
2591 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01002592 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002593 if ((fp->uf_flags & FC_NOARGS) == 0)
2594 {
2595 // Use "name" to avoid a warning from some compiler that checks the
2596 // destination size.
2597 v = &fc->fixvar[fixvar_idx++].var;
2598 name = v->di_key;
2599 STRCPY(name, "000");
2600 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2601 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
2602 v->di_tv.v_type = VAR_LIST;
2603 v->di_tv.v_lock = VAR_FIXED;
2604 v->di_tv.vval.v_list = &fc->l_varlist;
2605 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02002606 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002607 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2608 fc->l_varlist.lv_lock = VAR_FIXED;
2609
2610 /*
2611 * Set a:firstline to "firstline" and a:lastline to "lastline".
2612 * Set a:name to named arguments.
2613 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002614 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002615 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002616 if ((fp->uf_flags & FC_NOARGS) == 0)
2617 {
2618 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002619 (varnumber_T)funcexe->fe_firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002620 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002621 (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002622 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002623 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002624 {
2625 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002626 typval_T def_rettv;
2627 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002628
2629 ai = i - fp->uf_args.ga_len;
2630 if (ai < 0)
2631 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002632 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002633 name = FUNCARG(fp, i);
2634 if (islambda)
2635 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002636
2637 // evaluate named argument default expression
2638 isdefault = ai + fp->uf_def_args.ga_len >= 0
2639 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2640 && argvars[i].vval.v_number == VVAL_NONE));
2641 if (isdefault)
2642 {
2643 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002644
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002645 def_rettv.v_type = VAR_NUMBER;
2646 def_rettv.vval.v_number = -1;
2647
2648 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2649 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002650 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002651 {
2652 default_arg_err = 1;
2653 break;
2654 }
2655 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002656 }
2657 else
2658 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002659 if ((fp->uf_flags & FC_NOARGS) != 0)
2660 // Bail out if no a: arguments used (in lambda).
2661 break;
2662
Bram Moolenaare38eab22019-12-05 21:50:01 +01002663 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002664 sprintf((char *)numbuf, "%d", ai + 1);
2665 name = numbuf;
2666 }
2667 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2668 {
2669 v = &fc->fixvar[fixvar_idx++].var;
2670 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002671 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002672 }
2673 else
2674 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002675 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002676 if (v == NULL)
2677 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002678 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002679 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002680
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002681 // Note: the values are copied directly to avoid alloc/free.
2682 // "argvars" must have VAR_FIXED for v_lock.
2683 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002684 v->di_tv.v_lock = VAR_FIXED;
2685
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002686 if (isdefault)
2687 // Need to free this later, no matter where it's stored.
2688 tv_to_free[tv_to_free_len++] = &v->di_tv;
2689
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002690 if (addlocal)
2691 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002692 // Named arguments should be accessed without the "a:" prefix in
2693 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002694 copy_tv(&v->di_tv, &v->di_tv);
2695 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002696 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002697 else
2698 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002699
2700 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2701 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002702 listitem_T *li = &fc->l_listitems[ai];
2703
2704 li->li_tv = argvars[i];
2705 li->li_tv.v_lock = VAR_FIXED;
2706 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002707 }
2708 }
2709
Bram Moolenaare38eab22019-12-05 21:50:01 +01002710 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002711 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002712
2713 if (fp->uf_flags & FC_SANDBOX)
2714 {
2715 using_sandbox = TRUE;
2716 ++sandbox;
2717 }
2718
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002719 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002720 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002721 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002722 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002723 ++no_wait_return;
2724 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002725
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002726 smsg(_("calling %s"), SOURCING_NAME);
2727 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002728 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002729 char_u buf[MSG_BUF_LEN];
2730 char_u numbuf2[NUMBUFLEN];
2731 char_u *tofree;
2732 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002733
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002734 msg_puts("(");
2735 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002736 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002737 if (i > 0)
2738 msg_puts(", ");
2739 if (argvars[i].v_type == VAR_NUMBER)
2740 msg_outnum((long)argvars[i].vval.v_number);
2741 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002742 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002743 // Do not want errors such as E724 here.
2744 ++emsg_off;
2745 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2746 --emsg_off;
2747 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002748 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002749 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002750 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002751 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2752 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002753 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002754 msg_puts((char *)s);
2755 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002756 }
2757 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002758 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002759 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002760 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002761 msg_puts("\n"); // don't overwrite this either
2762
2763 verbose_leave_scroll();
2764 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002765 }
2766#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002767 if (do_profiling == PROF_YES)
2768 profile_may_start_func(&profile_info, fp,
2769 fc->caller == NULL ? NULL : fc->caller->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002770#endif
2771
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002772 save_current_sctx = current_sctx;
2773 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002774 save_did_emsg = did_emsg;
2775 did_emsg = FALSE;
2776
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002777 if (default_arg_err && (fp->uf_flags & FC_ABORT))
2778 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002779 else if (islambda)
2780 {
2781 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
2782
2783 // A Lambda always has the command "return {expr}". It is much faster
2784 // to evaluate {expr} directly.
2785 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002786 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002787 --ex_nesting_level;
2788 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002789 else
2790 // call do_cmdline() to execute the lines
2791 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002792 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
2793
2794 --RedrawingDisabled;
2795
Bram Moolenaare38eab22019-12-05 21:50:01 +01002796 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002797 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
2798 {
2799 clear_tv(rettv);
2800 rettv->v_type = VAR_NUMBER;
2801 rettv->vval.v_number = -1;
2802 }
2803
2804#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002805 if (do_profiling == PROF_YES)
2806 {
2807 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2808
2809 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
2810 profile_may_end_func(&profile_info, fp, caller);
2811 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002812#endif
2813
Bram Moolenaare38eab22019-12-05 21:50:01 +01002814 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002815 if (p_verbose >= 12)
2816 {
2817 ++no_wait_return;
2818 verbose_enter_scroll();
2819
2820 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002821 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002822 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002823 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002824 (long)fc->rettv->vval.v_number);
2825 else
2826 {
2827 char_u buf[MSG_BUF_LEN];
2828 char_u numbuf2[NUMBUFLEN];
2829 char_u *tofree;
2830 char_u *s;
2831
Bram Moolenaare38eab22019-12-05 21:50:01 +01002832 // The value may be very long. Skip the middle part, so that we
2833 // have some idea how it starts and ends. smsg() would always
2834 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002835 ++emsg_off;
2836 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
2837 --emsg_off;
2838 if (s != NULL)
2839 {
2840 if (vim_strsize(s) > MSG_BUF_CLEN)
2841 {
2842 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2843 s = buf;
2844 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002845 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002846 vim_free(tofree);
2847 }
2848 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002849 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002850
2851 verbose_leave_scroll();
2852 --no_wait_return;
2853 }
2854
Bram Moolenaare31ee862020-01-07 20:59:34 +01002855 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002856 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002857 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002858#ifdef FEAT_PROFILE
2859 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002860 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002861#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02002862 if (using_sandbox)
2863 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002864
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002865 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002866 {
2867 ++no_wait_return;
2868 verbose_enter_scroll();
2869
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002870 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002871 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002872
2873 verbose_leave_scroll();
2874 --no_wait_return;
2875 }
2876
2877 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002878 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002879 for (i = 0; i < tv_to_free_len; ++i)
2880 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002881 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002882}
2883
2884/*
Bram Moolenaar50824712020-12-20 21:10:17 +01002885 * Check the argument count for user function "fp".
2886 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
2887 */
2888 int
2889check_user_func_argcount(ufunc_T *fp, int argcount)
2890{
2891 int regular_args = fp->uf_args.ga_len;
2892
2893 if (argcount < regular_args - fp->uf_def_args.ga_len)
2894 return FCERR_TOOFEW;
2895 else if (!has_varargs(fp) && argcount > regular_args)
2896 return FCERR_TOOMANY;
2897 return FCERR_UNKNOWN;
2898}
2899
2900/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002902 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 int
2904call_user_func_check(
2905 ufunc_T *fp,
2906 int argcount,
2907 typval_T *argvars,
2908 typval_T *rettv,
2909 funcexe_T *funcexe,
2910 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002911{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002913
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002914 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
2915 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01002916 error = check_user_func_argcount(fp, argcount);
2917 if (error != FCERR_UNKNOWN)
2918 return error;
2919 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 error = FCERR_DICT;
2921 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002922 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 int did_save_redo = FALSE;
2924 save_redo_T save_redo;
2925
2926 /*
2927 * Call the user function.
2928 * Save and restore search patterns, script variables and
2929 * redo buffer.
2930 */
2931 save_search_patterns();
2932 if (!ins_compl_active())
2933 {
2934 saveRedobuff(&save_redo);
2935 did_save_redo = TRUE;
2936 }
2937 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002938 call_user_func(fp, argcount, argvars, rettv, funcexe,
2939 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
2941 // Function was unreferenced while being used, free it now.
2942 func_clear_free(fp, FALSE);
2943 if (did_save_redo)
2944 restoreRedobuff(&save_redo);
2945 restore_search_patterns();
2946 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002947 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002949}
2950
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002951static funccal_entry_T *funccal_stack = NULL;
2952
2953/*
2954 * Save the current function call pointer, and set it to NULL.
2955 * Used when executing autocommands and for ":source".
2956 */
2957 void
2958save_funccal(funccal_entry_T *entry)
2959{
2960 entry->top_funccal = current_funccal;
2961 entry->next = funccal_stack;
2962 funccal_stack = entry;
2963 current_funccal = NULL;
2964}
2965
2966 void
2967restore_funccal(void)
2968{
2969 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002970 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002971 else
2972 {
2973 current_funccal = funccal_stack->top_funccal;
2974 funccal_stack = funccal_stack->next;
2975 }
2976}
2977
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02002978 funccall_T *
2979get_current_funccal(void)
2980{
2981 return current_funccal;
2982}
2983
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002984/*
2985 * Mark all functions of script "sid" as deleted.
2986 */
2987 void
2988delete_script_functions(int sid)
2989{
2990 hashitem_T *hi;
2991 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02002992 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002993 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002994 size_t len;
2995
2996 buf[0] = K_SPECIAL;
2997 buf[1] = KS_EXTRA;
2998 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002999 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003000 len = STRLEN(buf);
3001
Bram Moolenaarce658352020-07-31 23:47:12 +02003002 while (todo > 0)
3003 {
3004 todo = func_hashtab.ht_used;
3005 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3006 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003007 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003008 fp = HI2UF(hi);
3009 if (STRNCMP(fp->uf_name, buf, len) == 0)
3010 {
3011 int changed = func_hashtab.ht_changed;
3012
3013 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003014
3015 if (fp->uf_calls > 0)
3016 {
3017 // Function is executing, don't free it but do remove
3018 // it from the hashtable.
3019 if (func_remove(fp))
3020 fp->uf_refcount--;
3021 }
3022 else
3023 {
3024 func_clear(fp, TRUE);
3025 // When clearing a function another function can be
3026 // cleared as a side effect. When that happens start
3027 // over.
3028 if (changed != func_hashtab.ht_changed)
3029 break;
3030 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003031 }
3032 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003033 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003034 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003035}
3036
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003037#if defined(EXITFREE) || defined(PROTO)
3038 void
3039free_all_functions(void)
3040{
3041 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003042 ufunc_T *fp;
3043 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003044 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003045 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003046
Bram Moolenaare38eab22019-12-05 21:50:01 +01003047 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003048 while (current_funccal != NULL)
3049 {
3050 clear_tv(current_funccal->rettv);
3051 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003052 if (current_funccal == NULL && funccal_stack != NULL)
3053 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003054 }
3055
Bram Moolenaare38eab22019-12-05 21:50:01 +01003056 // First clear what the functions contain. Since this may lower the
3057 // reference count of a function, it may also free a function and change
3058 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003059 while (todo > 0)
3060 {
3061 todo = func_hashtab.ht_used;
3062 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3063 if (!HASHITEM_EMPTY(hi))
3064 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 // clear the def function index now
3066 fp = HI2UF(hi);
3067 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003068 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069
Bram Moolenaare38eab22019-12-05 21:50:01 +01003070 // Only free functions that are not refcounted, those are
3071 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003072 if (func_name_refcount(fp->uf_name))
3073 ++skipped;
3074 else
3075 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003076 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003077 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003078 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003079 {
3080 skipped = 0;
3081 break;
3082 }
3083 }
3084 --todo;
3085 }
3086 }
3087
Bram Moolenaare38eab22019-12-05 21:50:01 +01003088 // Now actually free the functions. Need to start all over every time,
3089 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003090 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003091 while (func_hashtab.ht_used > skipped)
3092 {
3093 todo = func_hashtab.ht_used;
3094 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003095 if (!HASHITEM_EMPTY(hi))
3096 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003097 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003098 // Only free functions that are not refcounted, those are
3099 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003100 fp = HI2UF(hi);
3101 if (func_name_refcount(fp->uf_name))
3102 ++skipped;
3103 else
3104 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003105 if (func_free(fp, FALSE) == OK)
3106 {
3107 skipped = 0;
3108 break;
3109 }
3110 // did not actually free it
3111 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003112 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003113 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003114 }
3115 if (skipped == 0)
3116 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117
3118 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003119}
3120#endif
3121
3122/*
3123 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02003124 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003125 * "len" is the length of "name", or -1 for NUL terminated.
3126 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003128builtin_function(char_u *name, int len)
3129{
3130 char_u *p;
3131
Bram Moolenaara26b9702020-04-18 19:53:28 +02003132 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003133 return FALSE;
3134 p = vim_strchr(name, AUTOLOAD_CHAR);
3135 return p == NULL || (len > 0 && p > name + len);
3136}
3137
3138 int
3139func_call(
3140 char_u *name,
3141 typval_T *args,
3142 partial_T *partial,
3143 dict_T *selfdict,
3144 typval_T *rettv)
3145{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003146 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003147 listitem_T *item;
3148 typval_T argv[MAX_FUNC_ARGS + 1];
3149 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003150 int r = 0;
3151
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003152 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003153 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003154 {
3155 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003157 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003158 break;
3159 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003160 // Make a copy of each argument. This is needed to be able to set
3161 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003162 copy_tv(&item->li_tv, &argv[argc++]);
3163 }
3164
3165 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003166 {
3167 funcexe_T funcexe;
3168
Bram Moolenaara80faa82020-04-12 19:37:17 +02003169 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003170 funcexe.fe_firstline = curwin->w_cursor.lnum;
3171 funcexe.fe_lastline = curwin->w_cursor.lnum;
3172 funcexe.fe_evaluate = TRUE;
3173 funcexe.fe_partial = partial;
3174 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003175 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3176 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003177
Bram Moolenaare38eab22019-12-05 21:50:01 +01003178 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003179 while (argc > 0)
3180 clear_tv(&argv[--argc]);
3181
3182 return r;
3183}
3184
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003185static int callback_depth = 0;
3186
3187 int
3188get_callback_depth(void)
3189{
3190 return callback_depth;
3191}
3192
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003193/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003194 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003195 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003196 */
3197 int
3198call_callback(
3199 callback_T *callback,
3200 int len, // length of "name" or -1 to use strlen()
3201 typval_T *rettv, // return value goes here
3202 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003203 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003204 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003205{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003206 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003207 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003208
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003209 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3210 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003211 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003212 funcexe.fe_evaluate = TRUE;
3213 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003214 ++callback_depth;
3215 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3216 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003217
3218 // When a :def function was called that uses :try an error would be turned
3219 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003220 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003221 {
3222 need_rethrow = FALSE;
3223 handle_did_throw();
3224 }
3225
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003226 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003227}
3228
3229/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003230 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003231 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003232 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3233 */
3234 varnumber_T
3235call_callback_retnr(
3236 callback_T *callback,
3237 int argcount, // number of "argvars"
3238 typval_T *argvars) // vars for arguments, must have "argcount"
3239 // PLUS ONE elements!
3240{
3241 typval_T rettv;
3242 varnumber_T retval;
3243
3244 if (call_callback(callback, 0, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003245 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003246
3247 retval = tv_get_number_chk(&rettv, NULL);
3248 clear_tv(&rettv);
3249 return retval;
3250}
3251
3252/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 * Give an error message for the result of a function.
3254 * Nothing if "error" is FCERR_NONE.
3255 */
3256 void
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003257user_func_error(int error, char_u *name, funcexe_T *funcexe)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258{
3259 switch (error)
3260 {
3261 case FCERR_UNKNOWN:
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003262 if (funcexe->fe_found_var)
3263 semsg(_(e_not_callable_type_str), name);
3264 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003265 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 break;
3267 case FCERR_NOTMETHOD:
3268 emsg_funcname(
3269 N_("E276: Cannot use function as a method: %s"), name);
3270 break;
3271 case FCERR_DELETED:
Bram Moolenaarc553a212021-12-26 20:20:34 +00003272 emsg_funcname(e_func_deleted, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 break;
3274 case FCERR_TOOMANY:
Bram Moolenaare1242042021-12-16 20:56:57 +00003275 emsg_funcname((char *)e_too_many_arguments_for_function_str,
3276 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 break;
3278 case FCERR_TOOFEW:
Bram Moolenaare1242042021-12-16 20:56:57 +00003279 emsg_funcname((char *)e_not_enough_arguments_for_function_str,
3280 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 break;
3282 case FCERR_SCRIPT:
3283 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00003284 e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 break;
3286 case FCERR_DICT:
3287 emsg_funcname(
3288 N_("E725: Calling dict function without Dictionary: %s"),
3289 name);
3290 break;
3291 }
3292}
3293
3294/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003295 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003296 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003297 * Return FAIL when the function can't be called, OK otherwise.
3298 * Also returns OK when an error was encountered while executing the function.
3299 */
3300 int
3301call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003302 char_u *funcname, // name of the function
3303 int len, // length of "name" or -1 to use strlen()
3304 typval_T *rettv, // return value goes here
3305 int argcount_in, // number of "argvars"
3306 typval_T *argvars_in, // vars for arguments, must have "argcount"
3307 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003308 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003309{
3310 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01003311 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003312 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003313 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003314 char_u fname_buf[FLEN_FIXED + 1];
3315 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003316 char_u *fname = NULL;
3317 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003318 int argcount = argcount_in;
3319 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003320 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003321 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003322 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003323 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003324 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003325 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003326 type_T check_type;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003327
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003328 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3329 // even when call_func() returns FAIL.
3330 rettv->v_type = VAR_UNKNOWN;
3331
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003332 if (partial != NULL)
3333 fp = partial->pt_func;
3334 if (fp == NULL)
3335 {
3336 // Make a copy of the name, if it comes from a funcref variable it
3337 // could be changed or deleted in the called function.
3338 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3339 if (name == NULL)
3340 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003341
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003342 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3343 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003344
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003345 if (funcexe->fe_doesrange != NULL)
3346 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003347
3348 if (partial != NULL)
3349 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003350 // When the function has a partial with a dict and there is a dict
3351 // argument, use the dict argument. That is backwards compatible.
3352 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003353 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003354 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003355 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003356 {
3357 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003358 {
3359 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3360 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003361 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003362 goto theend;
3363 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003364 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003365 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003366 for (i = 0; i < argcount_in; ++i)
3367 argv[i + argv_clear] = argvars_in[i];
3368 argvars = argv;
3369 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003370
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003371 if (funcexe->fe_check_type != NULL
3372 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003373 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003374 // Now funcexe->fe_check_type is missing the added arguments,
3375 // make a copy of the type with the correction.
3376 check_type = *funcexe->fe_check_type;
3377 funcexe->fe_check_type = &check_type;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003378 check_type.tt_argcount += partial->pt_argc;
3379 check_type.tt_min_argcount += partial->pt_argc;
3380 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003381 }
3382 }
3383
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003384 if (error == FCERR_NONE && funcexe->fe_check_type != NULL
3385 && funcexe->fe_evaluate)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003386 {
3387 // Check that the argument types are OK for the types of the funcref.
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003388 if (check_argument_types(funcexe->fe_check_type, argvars, argcount,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003389 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003390 error = FCERR_OTHER;
3391 }
3392
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003393 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003394 {
3395 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003396 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003397
Bram Moolenaar333894b2020-08-01 18:53:07 +02003398 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003399 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003400 {
3401 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003402 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003403 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003404
Bram Moolenaare38eab22019-12-05 21:50:01 +01003405 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003406 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003407 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003408
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003409 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003410 {
3411 /*
3412 * User defined function.
3413 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003414 if (fp == NULL)
Bram Moolenaar333894b2020-08-01 18:53:07 +02003415 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003416
Bram Moolenaare38eab22019-12-05 21:50:01 +01003417 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003418 if (fp == NULL
3419 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003420 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003421 && !aborting())
3422 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003423 // executed an autocommand, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02003424 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003425 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003426 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003427 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3428 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003429 // loaded a package, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02003430 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003431 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003432 if (fp == NULL)
3433 {
3434 char_u *p = untrans_function_name(rfname);
3435
3436 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003437 // Don't do this if the name starts with "s:".
3438 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaar333894b2020-08-01 18:53:07 +02003439 fp = find_func(p, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003440 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003441
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003442 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003443 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02003444#ifdef FEAT_LUA
3445 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
3446 {
3447 cfunc_T cb = fp->uf_cb;
3448
3449 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3450 }
3451#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003452 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003453 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003454 if (funcexe->fe_argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003455 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003456 argcount = funcexe->fe_argv_func(argcount, argvars,
3457 argv_clear, fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003458
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003459 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003460 {
3461 // Method call: base->Method()
3462 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003463 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003464 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003465 argvars = argv;
3466 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003467 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003468
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 error = call_user_func_check(fp, argcount, argvars, rettv,
3470 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003471 }
3472 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003473 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003474 {
3475 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003476 * expr->method(): Find the method name in the table, call its
3477 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003478 */
3479 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003480 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003481 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003482 else
3483 {
3484 /*
3485 * Find the function name in the table, call its implementation.
3486 */
3487 error = call_internal_func(fname, argcount, argvars, rettv);
3488 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003489
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003490 /*
3491 * The function call (or "FuncUndefined" autocommand sequence) might
3492 * have been aborted by an error, an interrupt, or an explicitly thrown
3493 * exception that has not been caught so far. This situation can be
3494 * tested for by calling aborting(). For an error in an internal
3495 * function or for the "E132" error in call_user_func(), however, the
3496 * throw point at which the "force_abort" flag (temporarily reset by
3497 * emsg()) is normally updated has not been reached yet. We need to
3498 * update that flag first to make aborting() reliable.
3499 */
3500 update_force_abort();
3501 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003502 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003503 ret = OK;
3504
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003505theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003506 /*
3507 * Report an error unless the argument evaluation or function call has been
3508 * cancelled due to an aborting error, an interrupt, or an exception.
3509 */
3510 if (!aborting())
3511 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003512 user_func_error(error, (name != NULL) ? name : funcname, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003513 }
3514
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003515 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003516 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003517 clear_tv(&argv[--argv_clear + argv_base]);
3518
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003519 vim_free(tofree);
3520 vim_free(name);
3521
3522 return ret;
3523}
3524
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003525 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526printable_func_name(ufunc_T *fp)
3527{
3528 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3529}
3530
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003531/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003532 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003533 */
3534 static void
3535list_func_head(ufunc_T *fp, int indent)
3536{
3537 int j;
3538
3539 msg_start();
3540 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003541 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003542 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003543 msg_puts("def ");
3544 else
3545 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003547 msg_putchar('(');
3548 for (j = 0; j < fp->uf_args.ga_len; ++j)
3549 {
3550 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003551 msg_puts(", ");
3552 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 if (fp->uf_arg_types != NULL)
3554 {
3555 char *tofree;
3556
3557 msg_puts(": ");
3558 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3559 vim_free(tofree);
3560 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003561 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3562 {
3563 msg_puts(" = ");
3564 msg_puts(((char **)(fp->uf_def_args.ga_data))
3565 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3566 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003567 }
3568 if (fp->uf_varargs)
3569 {
3570 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003571 msg_puts(", ");
3572 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003573 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 if (fp->uf_va_name != NULL)
3575 {
3576 if (j)
3577 msg_puts(", ");
3578 msg_puts("...");
3579 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003580 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 {
3582 char *tofree;
3583
3584 msg_puts(": ");
3585 msg_puts(type_name(fp->uf_va_type, &tofree));
3586 vim_free(tofree);
3587 }
3588 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003589 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003590
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003591 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003592 {
3593 if (fp->uf_ret_type != &t_void)
3594 {
3595 char *tofree;
3596
3597 msg_puts(": ");
3598 msg_puts(type_name(fp->uf_ret_type, &tofree));
3599 vim_free(tofree);
3600 }
3601 }
3602 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003603 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003604 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003605 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003606 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003607 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003608 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003609 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003610 msg_clr_eos();
3611 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003612 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003613}
3614
3615/*
3616 * Get a function name, translating "<SID>" and "<SNR>".
3617 * Also handles a Funcref in a List or Dictionary.
3618 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003619 * Set "*is_global" to TRUE when the function must be global, unless
3620 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003621 * flags:
3622 * TFN_INT: internal function name OK
3623 * TFN_QUIET: be quiet
3624 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003625 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003626 * Advances "pp" to just after the function name (if no error).
3627 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003628 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003629trans_function_name(
3630 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003631 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003632 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003633 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003634 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003635 partial_T **partial, // return: partial of a FuncRef
3636 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003637{
3638 char_u *name = NULL;
3639 char_u *start;
3640 char_u *end;
3641 int lead;
3642 char_u sid_buf[20];
3643 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003645 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003647
3648 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003649 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003650 start = *pp;
3651
Bram Moolenaare38eab22019-12-05 21:50:01 +01003652 // Check for hard coded <SNR>: already translated function ID (from a user
3653 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003654 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
3655 && (*pp)[2] == (int)KE_SNR)
3656 {
3657 *pp += 3;
3658 len = get_id_len(pp) + 3;
3659 return vim_strnsave(start, len);
3660 }
3661
Bram Moolenaare38eab22019-12-05 21:50:01 +01003662 // A name starting with "<SID>" or "<SNR>" is local to a script. But
3663 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003664 lead = eval_fname_script(start);
3665 if (lead > 2)
3666 start += lead;
3667
Bram Moolenaare38eab22019-12-05 21:50:01 +01003668 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01003669 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003670 lead > 2 ? 0 : FNE_CHECK_START);
3671 if (end == start)
3672 {
3673 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003674 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003675 goto theend;
3676 }
3677 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
3678 {
3679 /*
3680 * Report an invalid expression in braces, unless the expression
3681 * evaluation has been cancelled due to an aborting error, an
3682 * interrupt, or an exception.
3683 */
3684 if (!aborting())
3685 {
3686 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003687 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003688 }
3689 else
3690 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
3691 goto theend;
3692 }
3693
3694 if (lv.ll_tv != NULL)
3695 {
3696 if (fdp != NULL)
3697 {
3698 fdp->fd_dict = lv.ll_dict;
3699 fdp->fd_newkey = lv.ll_newkey;
3700 lv.ll_newkey = NULL;
3701 fdp->fd_di = lv.ll_di;
3702 }
3703 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
3704 {
3705 name = vim_strsave(lv.ll_tv->vval.v_string);
3706 *pp = end;
3707 }
3708 else if (lv.ll_tv->v_type == VAR_PARTIAL
3709 && lv.ll_tv->vval.v_partial != NULL)
3710 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003711 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003712 *pp = end;
3713 if (partial != NULL)
3714 *partial = lv.ll_tv->vval.v_partial;
3715 }
3716 else
3717 {
3718 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
3719 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003720 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003721 else
3722 *pp = end;
3723 name = NULL;
3724 }
3725 goto theend;
3726 }
3727
3728 if (lv.ll_name == NULL)
3729 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003730 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003731 *pp = end;
3732 goto theend;
3733 }
3734
Bram Moolenaare38eab22019-12-05 21:50:01 +01003735 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003736 if (lv.ll_exp_name != NULL)
3737 {
3738 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003739 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003740 flags & TFN_NO_AUTOLOAD, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003741 if (name == lv.ll_exp_name)
3742 name = NULL;
3743 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003744 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003745 {
3746 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003747 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003748 flags & TFN_NO_AUTOLOAD, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003749 if (name == *pp)
3750 name = NULL;
3751 }
3752 if (name != NULL)
3753 {
3754 name = vim_strsave(name);
3755 *pp = end;
3756 if (STRNCMP(name, "<SNR>", 5) == 0)
3757 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003758 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003759 name[0] = K_SPECIAL;
3760 name[1] = KS_EXTRA;
3761 name[2] = (int)KE_SNR;
3762 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
3763 }
3764 goto theend;
3765 }
3766
3767 if (lv.ll_exp_name != NULL)
3768 {
3769 len = (int)STRLEN(lv.ll_exp_name);
3770 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
3771 && STRNCMP(lv.ll_name, "s:", 2) == 0)
3772 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003773 // When there was "s:" already or the name expanded to get a
3774 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003775 lv.ll_name += 2;
3776 len -= 2;
3777 lead = 2;
3778 }
3779 }
3780 else
3781 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003782 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003783 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003784 {
3785 if (is_global != NULL && lv.ll_name[0] == 'g')
3786 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003787 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003788 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003789 len = (int)(end - lv.ll_name);
3790 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003791 if (len <= 0)
3792 {
3793 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003794 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003795 goto theend;
3796 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003797
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003798 // In Vim9 script a user function is script-local by default, unless it
3799 // starts with a lower case character: dict.func().
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003800 vim9script = ASCII_ISUPPER(*start) && in_vim9script();
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003801 if (vim9script)
3802 {
3803 char_u *p;
3804
3805 // SomeScript#func() is a global function.
3806 for (p = start; *p != NUL && *p != '('; ++p)
3807 if (*p == AUTOLOAD_CHAR)
3808 vim9script = FALSE;
3809 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003811 /*
3812 * Copy the function name to allocated memory.
3813 * Accept <SID>name() inside a script, translate into <SNR>123_name().
3814 * Accept <SNR>123_name() outside a script.
3815 */
3816 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003817 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003819 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 if (!vim9script)
3821 lead = 3;
3822 if (vim9script || (lv.ll_exp_name != NULL
3823 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003824 || eval_fname_sid(*pp))
3825 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003827 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003828 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00003829 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003830 goto theend;
3831 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003832 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 if (vim9script)
3834 extra = 3 + (int)STRLEN(sid_buf);
3835 else
3836 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003837 }
3838 }
Bram Moolenaar22f17a22021-06-21 20:48:58 +02003839 else if (!(flags & TFN_INT) && (builtin_function(lv.ll_name, len)
3840 || (in_vim9script() && *lv.ll_name == '_')))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003841 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00003842 semsg(_(e_function_name_must_start_with_capital_or_s_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003843 goto theend;
3844 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003845 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003846 {
3847 char_u *cp = vim_strchr(lv.ll_name, ':');
3848
3849 if (cp != NULL && cp < end)
3850 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003851 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003852 goto theend;
3853 }
3854 }
3855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003857 if (name != NULL)
3858 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01003859 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003860 {
3861 name[0] = K_SPECIAL;
3862 name[1] = KS_EXTRA;
3863 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003865 STRCPY(name + 3, sid_buf);
3866 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
3868 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003869 }
3870 *pp = end;
3871
3872theend:
3873 clear_lval(&lv);
3874 return name;
3875}
3876
3877/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02003878 * Assuming "name" is the result of trans_function_name() and it was prefixed
3879 * to use the script-local name, return the unmodified name (points into
3880 * "name"). Otherwise return NULL.
3881 * This can be used to first search for a script-local function and fall back
3882 * to the global function if not found.
3883 */
3884 char_u *
3885untrans_function_name(char_u *name)
3886{
3887 char_u *p;
3888
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003889 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02003890 {
3891 p = vim_strchr(name, '_');
3892 if (p != NULL)
3893 return p + 1;
3894 }
3895 return NULL;
3896}
3897
3898/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00003899 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
3900 * current script ID and returns the expanded function name. The caller should
3901 * free the returned name. If not called from a script context or the function
3902 * name doesn't start with these prefixes, then returns NULL.
3903 * This doesn't check whether the script-local function exists or not.
3904 */
3905 char_u *
3906get_scriptlocal_funcname(char_u *funcname)
3907{
3908 char sid_buf[25];
3909 int off;
3910 char_u *newname;
3911
3912 if (funcname == NULL)
3913 return NULL;
3914
3915 if (STRNCMP(funcname, "s:", 2) != 0
3916 && STRNCMP(funcname, "<SID>", 5) != 0)
3917 // The function name is not a script-local function name
3918 return NULL;
3919
3920 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3921 {
3922 emsg(_(e_using_sid_not_in_script_context));
3923 return NULL;
3924 }
3925 // Expand s: prefix into <SNR>nr_<name>
3926 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
3927 (long)current_sctx.sc_sid);
3928 off = *funcname == 's' ? 2 : 5;
3929 newname = alloc(STRLEN(sid_buf) + STRLEN(funcname + off) + 1);
3930 if (newname == NULL)
3931 return NULL;
3932 STRCPY(newname, sid_buf);
3933 STRCAT(newname, funcname + off);
3934
3935 return newname;
3936}
3937
3938/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00003939 * Call trans_function_name(), except that a lambda is returned as-is.
3940 * Returns the name in allocated memory.
3941 */
3942 char_u *
3943save_function_name(
3944 char_u **name,
3945 int *is_global,
3946 int skip,
3947 int flags,
3948 funcdict_T *fudi)
3949{
3950 char_u *p = *name;
3951 char_u *saved;
3952
3953 if (STRNCMP(p, "<lambda>", 8) == 0)
3954 {
3955 p += 8;
3956 (void)getdigits(&p);
3957 saved = vim_strnsave(*name, p - *name);
3958 if (fudi != NULL)
3959 CLEAR_POINTER(fudi);
3960 }
3961 else
3962 saved = trans_function_name(&p, is_global, skip,
3963 flags, fudi, NULL, NULL);
3964 *name = p;
3965 return saved;
3966}
3967
3968/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003969 * List functions. When "regmatch" is NULL all of then.
3970 * Otherwise functions matching "regmatch".
3971 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003972 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003973list_functions(regmatch_T *regmatch)
3974{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003975 int changed = func_hashtab.ht_changed;
3976 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003977 hashitem_T *hi;
3978
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003979 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003980 {
3981 if (!HASHITEM_EMPTY(hi))
3982 {
3983 ufunc_T *fp = HI2UF(hi);
3984
3985 --todo;
3986 if ((fp->uf_flags & FC_DEAD) == 0
3987 && (regmatch == NULL
3988 ? !message_filtered(fp->uf_name)
3989 && !func_name_refcount(fp->uf_name)
3990 : !isdigit(*fp->uf_name)
3991 && vim_regexec(regmatch, fp->uf_name, 0)))
3992 {
3993 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003994 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003995 {
3996 emsg(_("E454: function list was modified"));
3997 return;
3998 }
3999 }
4000 }
4001 }
4002}
4003
4004/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004005 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004006 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4007 * the function name.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004008 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004009 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004010 ufunc_T *
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004011define_function(exarg_T *eap, char_u *name_arg, char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004012{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004013 int j;
4014 int c;
4015 int saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004016 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004017 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004018 char_u *p;
4019 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004020 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004021 char_u *line_arg = NULL;
4022 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004024 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004025 garray_T newlines;
4026 int varargs = FALSE;
4027 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004029 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004030 int fp_allocated = FALSE;
4031 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004032 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004033 dictitem_T *v;
4034 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004035 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004036 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004037 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004038 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004039 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004040 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004041
4042 /*
4043 * ":function" without argument: list functions.
4044 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004045 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004046 {
4047 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004048 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004049 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004050 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004051 }
4052
4053 /*
4054 * ":function /pat": list functions matching pattern.
4055 */
4056 if (*eap->arg == '/')
4057 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004058 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004059 if (!eap->skip)
4060 {
4061 regmatch_T regmatch;
4062
4063 c = *p;
4064 *p = NUL;
4065 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4066 *p = c;
4067 if (regmatch.regprog != NULL)
4068 {
4069 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004070 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004071 vim_regfree(regmatch.regprog);
4072 }
4073 }
4074 if (*p == '/')
4075 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004076 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004077 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004078 }
4079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080 ga_init(&newargs);
4081 ga_init(&argtypes);
4082 ga_init(&default_args);
4083
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004084 /*
4085 * Get the function name. There are these situations:
4086 * func normal function name
4087 * "name" == func, "fudi.fd_dict" == NULL
4088 * dict.func new dictionary entry
4089 * "name" == NULL, "fudi.fd_dict" set,
4090 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4091 * dict.func existing dict entry with a Funcref
4092 * "name" == func, "fudi.fd_dict" set,
4093 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4094 * dict.func existing dict entry that's not a Funcref
4095 * "name" == NULL, "fudi.fd_dict" set,
4096 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4097 * s:func script-local function name
4098 * g:func global function name, same as "func"
4099 */
4100 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004101 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004102 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004103 // nested function, argument is (args).
4104 paren = TRUE;
4105 CLEAR_FIELD(fudi);
4106 }
4107 else
4108 {
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004109 name = save_function_name(&p, &is_global, eap->skip,
4110 TFN_NO_AUTOLOAD, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004111 paren = (vim_strchr(p, '(') != NULL);
4112 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004113 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004114 /*
4115 * Return on an invalid expression in braces, unless the expression
4116 * evaluation has been cancelled due to an aborting error, an
4117 * interrupt, or an exception.
4118 */
4119 if (!aborting())
4120 {
4121 if (!eap->skip && fudi.fd_newkey != NULL)
4122 semsg(_(e_dictkey), fudi.fd_newkey);
4123 vim_free(fudi.fd_newkey);
4124 return NULL;
4125 }
4126 else
4127 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004128 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004129 }
4130
Bram Moolenaare38eab22019-12-05 21:50:01 +01004131 // An error in a function call during evaluation of an expression in magic
4132 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004133 saved_did_emsg = did_emsg;
4134 did_emsg = FALSE;
4135
4136 /*
4137 * ":function func" with only function name: list function.
4138 */
4139 if (!paren)
4140 {
4141 if (!ends_excmd(*skipwhite(p)))
4142 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004143 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004144 goto ret_free;
4145 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004146 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004147 if (eap->nextcmd != NULL)
4148 *p = NUL;
4149 if (!eap->skip && !got_int)
4150 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004151 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004152 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4153 {
4154 char_u *up = untrans_function_name(name);
4155
4156 // With Vim9 script the name was made script-local, if not
4157 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004158 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004159 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004160 }
4161
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004162 if (fp != NULL)
4163 {
4164 list_func_head(fp, TRUE);
4165 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4166 {
4167 if (FUNCLINE(fp, j) == NULL)
4168 continue;
4169 msg_putchar('\n');
4170 msg_outnum((long)(j + 1));
4171 if (j < 9)
4172 msg_putchar(' ');
4173 if (j < 99)
4174 msg_putchar(' ');
4175 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004176 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004177 ui_breakcheck();
4178 }
4179 if (!got_int)
4180 {
4181 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004182 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183 msg_puts(" enddef");
4184 else
4185 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004186 }
4187 }
4188 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004189 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004190 }
4191 goto ret_free;
4192 }
4193
4194 /*
4195 * ":function name(arg1, arg2)" Define function.
4196 */
4197 p = skipwhite(p);
4198 if (*p != '(')
4199 {
4200 if (!eap->skip)
4201 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004202 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004203 goto ret_free;
4204 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004205 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004206 if (vim_strchr(p, '(') != NULL)
4207 p = vim_strchr(p, '(');
4208 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004209
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004210 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4211 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004212 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004213 goto ret_free;
4214 }
4215
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004216 // In Vim9 script only global functions can be redefined.
4217 if (vim9script && eap->forceit && !is_global)
4218 {
4219 emsg(_(e_nobang));
4220 goto ret_free;
4221 }
4222
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004223 ga_init2(&newlines, (int)sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004224
Bram Moolenaar04b12692020-05-04 23:24:44 +02004225 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004226 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004227 // Check the name of the function. Unless it's a dictionary function
4228 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004229 if (name != NULL)
4230 arg = name;
4231 else
4232 arg = fudi.fd_newkey;
4233 if (arg != NULL && (fudi.fd_di == NULL
4234 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4235 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4236 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004237 char_u *name_base = arg;
4238 int i;
4239
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004240 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004241 {
4242 name_base = vim_strchr(arg, '_');
4243 if (name_base == NULL)
4244 name_base = arg + 3;
4245 else
4246 ++name_base;
4247 }
4248 for (i = 0; name_base[i] != NUL && (i == 0
4249 ? eval_isnamec1(name_base[i])
4250 : eval_isnamec(name_base[i])); ++i)
4251 ;
4252 if (name_base[i] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004253 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004254
4255 // In Vim9 script a function cannot have the same name as a
4256 // variable.
4257 if (vim9script && *arg == K_SPECIAL
Mike Williams1821d142021-12-15 16:38:33 +00004258 && eval_variable(name_base, (int)STRLEN(name_base), NULL, NULL,
Bram Moolenaar052ff292021-12-11 13:54:46 +00004259 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
4260 + EVAL_VAR_NO_FUNC) == OK)
4261 {
4262 semsg(_(e_redefining_script_item_str), name_base);
4263 goto ret_free;
4264 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004265 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004266 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004267 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004268 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004269 emsg(_("E862: Cannot use g: here"));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004270 goto ret_free;
4271 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004272 }
4273
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004274 // This may get more lines and make the pointers into the first line
4275 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004276 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004278 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004279 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004280 eap, line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004281 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004282 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004285 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004287 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004288 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004289 if (*p != ':')
4290 {
4291 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4292 p = skipwhite(p);
4293 }
4294 else if (!IS_WHITE_OR_NUL(p[1]))
4295 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004297 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004299 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004300 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004301 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004303 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004305 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004306 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004307 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004308 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004309 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004310 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 else
4313 // find extra arguments "range", "dict", "abort" and "closure"
4314 for (;;)
4315 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004316 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 p = skipwhite(p);
4318 if (STRNCMP(p, "range", 5) == 0)
4319 {
4320 flags |= FC_RANGE;
4321 p += 5;
4322 }
4323 else if (STRNCMP(p, "dict", 4) == 0)
4324 {
4325 flags |= FC_DICT;
4326 p += 4;
4327 }
4328 else if (STRNCMP(p, "abort", 5) == 0)
4329 {
4330 flags |= FC_ABORT;
4331 p += 5;
4332 }
4333 else if (STRNCMP(p, "closure", 7) == 0)
4334 {
4335 flags |= FC_CLOSURE;
4336 p += 7;
4337 if (current_funccal == NULL)
4338 {
4339 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
4340 name == NULL ? (char_u *)"" : name);
4341 goto erret;
4342 }
4343 }
4344 else
4345 break;
4346 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004347
Bram Moolenaare38eab22019-12-05 21:50:01 +01004348 // When there is a line break use what follows for the function body.
4349 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004350 if (*p == '\n')
4351 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004352 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004353 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4354 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004355 && !(VIM_ISWHITE(*whitep) && *p == '#'
4356 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004357 && !eap->skip
4358 && !did_emsg)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004359 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004360
4361 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4363 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004364 */
4365 if (KeyTyped)
4366 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004367 // Check if the function already exists, don't let the user type the
4368 // whole function before telling him it doesn't work! For a script we
4369 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004370 if (!eap->skip && !eap->forceit)
4371 {
4372 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004373 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004374 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004375 emsg_funcname(e_function_str_already_exists_add_excl_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004376 }
4377
4378 if (!eap->skip && did_emsg)
4379 goto erret;
4380
Bram Moolenaare38eab22019-12-05 21:50:01 +01004381 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004382 cmdline_row = msg_row;
4383 }
4384
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004385 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004386 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004387
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004388 // Do not define the function when getting the body fails and when
4389 // skipping.
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004390 if (get_function_body(eap, &newlines, line_arg, line_to_free) == FAIL
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004391 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004392 goto erret;
4393
4394 /*
4395 * If there are no errors, add the function
4396 */
4397 if (fudi.fd_dict == NULL)
4398 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 hashtab_T *ht;
4400
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004401 v = find_var(name, &ht, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004402 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
4403 {
4404 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
4405 name);
4406 goto erret;
4407 }
4408
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004409 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaareef21022020-08-01 22:16:43 +02004410 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004411 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004412 char_u *uname = untrans_function_name(name);
4413
4414 import = find_imported(uname == NULL ? name : uname, 0, NULL);
4415 }
4416
4417 if (fp != NULL || import != NULL)
4418 {
4419 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004421 // Function can be replaced with "function!" and when sourcing the
4422 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02004423 // A name that is used by an import can not be overruled.
4424 if (import != NULL
4425 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004426 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02004427 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004428 {
Bram Moolenaard604d782021-11-20 21:46:20 +00004429 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02004430 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004431 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02004432 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004433 emsg_funcname(e_function_str_already_exists_add_excl_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004434 goto erret;
4435 }
4436 if (fp->uf_calls > 0)
4437 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004438 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00004439 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004440 goto erret;
4441 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004442 if (fp->uf_refcount > 1)
4443 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004444 // This function is referenced somewhere, don't redefine it but
4445 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004446 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004447 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004448 fp = NULL;
4449 overwrite = TRUE;
4450 }
4451 else
4452 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004453 char_u *exp_name = fp->uf_name_exp;
4454
4455 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01004456 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004457 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004458 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004459 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004461#ifdef FEAT_PROFILE
4462 fp->uf_profiling = FALSE;
4463 fp->uf_prof_initialized = FALSE;
4464#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01004465 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004466 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004467 }
4468 }
4469 else
4470 {
4471 char numbuf[20];
4472
4473 fp = NULL;
4474 if (fudi.fd_newkey == NULL && !eap->forceit)
4475 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004476 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004477 goto erret;
4478 }
4479 if (fudi.fd_di == NULL)
4480 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004481 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02004482 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004483 goto erret;
4484 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004485 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02004486 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004487 goto erret;
4488
Bram Moolenaare38eab22019-12-05 21:50:01 +01004489 // Give the function a sequential number. Can only be used with a
4490 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004491 vim_free(name);
4492 sprintf(numbuf, "%d", ++func_nr);
4493 name = vim_strsave((char_u *)numbuf);
4494 if (name == NULL)
4495 goto erret;
4496 }
4497
4498 if (fp == NULL)
4499 {
4500 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
4501 {
4502 int slen, plen;
4503 char_u *scriptname;
4504
Bram Moolenaare38eab22019-12-05 21:50:01 +01004505 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004506 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004507 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004508 {
4509 scriptname = autoload_name(name);
4510 if (scriptname != NULL)
4511 {
4512 p = vim_strchr(scriptname, '/');
4513 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004514 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004515 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004516 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004517 j = OK;
4518 vim_free(scriptname);
4519 }
4520 }
4521 if (j == FAIL)
4522 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004523 linenr_T save_lnum = SOURCING_LNUM;
4524
4525 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004526 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004527 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004528 goto erret;
4529 }
4530 }
4531
Bram Moolenaar47ed5532019-08-08 20:49:14 +02004532 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004533 if (fp == NULL)
4534 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004535 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004536
4537 if (fudi.fd_dict != NULL)
4538 {
4539 if (fudi.fd_di == NULL)
4540 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004541 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004542 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
4543 if (fudi.fd_di == NULL)
4544 {
4545 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004546 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004547 goto erret;
4548 }
4549 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
4550 {
4551 vim_free(fudi.fd_di);
4552 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004553 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004554 goto erret;
4555 }
4556 }
4557 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004558 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004559 clear_tv(&fudi.fd_di->di_tv);
4560 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004561 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004562
Bram Moolenaare38eab22019-12-05 21:50:01 +01004563 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004564 flags |= FC_DICT;
4565 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004566 }
4567 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004568 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004570 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571
4572 if (eap->cmdidx == CMD_def)
4573 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004574 int lnum_save = SOURCING_LNUM;
4575 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004576
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004577 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004578
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004579 // error messages are for the first function line
4580 SOURCING_LNUM = sourcing_lnum_top;
4581
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02004582 // The function may use script variables from the context.
4583 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004584
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004585 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004587 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004588 free_fp = fp_allocated;
4589 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004591 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592
4593 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004594 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004596 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004597 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004598 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02004600 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004602 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004603 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004605 if (fp_allocated)
4606 {
4607 // insert the new function in the function list
4608 set_ufunc_name(fp, name);
4609 if (overwrite)
4610 {
4611 hi = hash_find(&func_hashtab, name);
4612 hi->hi_key = UF2HIKEY(fp);
4613 }
4614 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
4615 {
4616 free_fp = TRUE;
4617 goto erret;
4618 }
4619 fp->uf_refcount = 1;
4620 }
4621
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004622 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004623 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004624 if ((flags & FC_CLOSURE) != 0)
4625 {
Bram Moolenaar58016442016-07-31 18:30:22 +02004626 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004627 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004628 }
4629 else
4630 fp->uf_scoped = NULL;
4631
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004632#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004633 if (prof_def_func())
4634 func_do_profile(fp);
4635#endif
4636 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02004637 if (sandbox)
4638 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004639 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004640 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004641 fp->uf_flags = flags;
4642 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01004643 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004644 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004645 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004646 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 if (is_export)
4648 {
4649 fp->uf_flags |= FC_EXPORT;
4650 // let ex_export() know the export worked.
4651 is_export = FALSE;
4652 }
4653
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004654 if (eap->cmdidx == CMD_def)
4655 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02004656 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
4657 // :func does not use Vim9 script syntax, even in a Vim9 script file
4658 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004659
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004660 goto ret_free;
4661
4662erret:
4663 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004664 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004665 if (fp != NULL)
4666 {
4667 ga_init(&fp->uf_args);
4668 ga_init(&fp->uf_def_args);
4669 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004670errret_2:
4671 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004672 if (fp != NULL)
4673 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004674 if (free_fp)
4675 {
4676 vim_free(fp);
4677 fp = NULL;
4678 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004679ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004680 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004681 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004682 if (name != name_arg)
4683 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004684 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004685 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004686
4687 return fp;
4688}
4689
4690/*
4691 * ":function"
4692 */
4693 void
4694ex_function(exarg_T *eap)
4695{
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004696 char_u *line_to_free = NULL;
4697
4698 (void)define_function(eap, NULL, &line_to_free);
4699 vim_free(line_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02004700}
4701
4702/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004703 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarb2049902021-01-24 12:53:53 +01004704 * be compiled. Except dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02004705 */
4706 void
4707ex_defcompile(exarg_T *eap UNUSED)
4708{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004709 long todo = (long)func_hashtab.ht_used;
4710 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004711 hashitem_T *hi;
4712 ufunc_T *ufunc;
4713
4714 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
4715 {
4716 if (!HASHITEM_EMPTY(hi))
4717 {
4718 --todo;
4719 ufunc = HI2UF(hi);
4720 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004721 && ufunc->uf_def_status == UF_TO_BE_COMPILED
4722 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004723 {
Bram Moolenaare99d4222021-06-13 14:01:26 +02004724 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004725
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004726 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004727 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004728 // a function has been added or removed, need to start over
4729 todo = (long)func_hashtab.ht_used;
4730 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004731 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02004732 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004733 }
4734 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004735 }
4736 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004737}
4738
4739/*
4740 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
4741 * Return 2 if "p" starts with "s:".
4742 * Return 0 otherwise.
4743 */
4744 int
4745eval_fname_script(char_u *p)
4746{
Bram Moolenaare38eab22019-12-05 21:50:01 +01004747 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
4748 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004749 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
4750 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
4751 return 5;
4752 if (p[0] == 's' && p[1] == ':')
4753 return 2;
4754 return 0;
4755}
4756
4757 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004758translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004759{
4760 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004761 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004762 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763}
4764
4765/*
4766 * Return TRUE when "ufunc" has old-style "..." varargs
4767 * or named varargs "...name: type".
4768 */
4769 int
4770has_varargs(ufunc_T *ufunc)
4771{
4772 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004773}
4774
4775/*
4776 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004777 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004778 */
4779 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004780function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004781{
4782 char_u *nm = name;
4783 char_u *p;
4784 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004785 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004786 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004787
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004788 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
4789 if (no_deref)
4790 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004791 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004792 nm = skipwhite(nm);
4793
Bram Moolenaare38eab22019-12-05 21:50:01 +01004794 // Only accept "funcname", "funcname ", "funcname (..." and
4795 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004796 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004797 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004798 vim_free(p);
4799 return n;
4800}
4801
Bram Moolenaar113e1072019-01-20 15:30:40 +01004802#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004803 char_u *
4804get_expanded_name(char_u *name, int check)
4805{
4806 char_u *nm = name;
4807 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004808 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004809
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004810 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004811 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004812
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004813 if (p != NULL && *nm == NUL
4814 && (!check || translated_function_exists(p, is_global)))
4815 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004816
4817 vim_free(p);
4818 return NULL;
4819}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004820#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004821
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004822/*
4823 * Function given to ExpandGeneric() to obtain the list of user defined
4824 * function names.
4825 */
4826 char_u *
4827get_user_func_name(expand_T *xp, int idx)
4828{
4829 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004830 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004831 static hashitem_T *hi;
4832 ufunc_T *fp;
4833
4834 if (idx == 0)
4835 {
4836 done = 0;
4837 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004838 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004839 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004840 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004841 {
4842 if (done++ > 0)
4843 ++hi;
4844 while (HASHITEM_EMPTY(hi))
4845 ++hi;
4846 fp = HI2UF(hi);
4847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 // don't show dead, dict and lambda functions
4849 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02004850 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004852
4853 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004854 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004855
4856 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02004857 if (xp->xp_context != EXPAND_USER_FUNC
4858 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004859 {
4860 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004862 STRCAT(IObuff, ")");
4863 }
4864 return IObuff;
4865 }
4866 return NULL;
4867}
4868
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004869/*
4870 * ":delfunction {name}"
4871 */
4872 void
4873ex_delfunction(exarg_T *eap)
4874{
4875 ufunc_T *fp = NULL;
4876 char_u *p;
4877 char_u *name;
4878 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004879 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004880
4881 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004882 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
4883 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004884 vim_free(fudi.fd_newkey);
4885 if (name == NULL)
4886 {
4887 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004888 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004889 return;
4890 }
4891 if (!ends_excmd(*skipwhite(p)))
4892 {
4893 vim_free(name);
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004894 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004895 return;
4896 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004897 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004898 if (eap->nextcmd != NULL)
4899 *p = NUL;
4900
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004901 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02004902 {
4903 if (!eap->skip)
4904 semsg(_(e_invarg2), eap->arg);
4905 vim_free(name);
4906 return;
4907 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004908 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004909 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004910 vim_free(name);
4911
4912 if (!eap->skip)
4913 {
4914 if (fp == NULL)
4915 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004916 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004917 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004918 return;
4919 }
4920 if (fp->uf_calls > 0)
4921 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004922 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004923 return;
4924 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004925 if (fp->uf_flags & FC_VIM9)
4926 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004927 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004928 return;
4929 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004930
4931 if (fudi.fd_dict != NULL)
4932 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004933 // Delete the dict item that refers to the function, it will
4934 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004935 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4936 }
4937 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004938 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004939 // A normal function (not a numbered function or lambda) has a
4940 // refcount of 1 for the entry in the hashtable. When deleting
4941 // it and the refcount is more than one, it should be kept.
4942 // A numbered function and lambda should be kept if the refcount is
4943 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004944 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004945 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004946 // Function is still referenced somewhere. Don't free it but
4947 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004948 if (func_remove(fp))
4949 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004950 }
4951 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004952 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004953 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004954 }
4955}
4956
4957/*
4958 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004959 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004960 */
4961 void
4962func_unref(char_u *name)
4963{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004964 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004965
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004966 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004967 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004968 fp = find_func(name, FALSE, NULL);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004969 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004970 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004971#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004972 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004973#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004974 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004975 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004976 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004977}
4978
4979/*
4980 * Unreference a Function: decrement the reference count and free it when it
4981 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004982 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004983 */
4984 void
4985func_ptr_unref(ufunc_T *fp)
4986{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004987 if (fp != NULL && (--fp->uf_refcount <= 0
4988 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
4989 && fp->uf_partial->pt_refcount <= 1
4990 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02004991 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004992 // Only delete it when it's not being used. Otherwise it's done
4993 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004994 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004995 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004996 }
4997}
4998
4999/*
5000 * Count a reference to a Function.
5001 */
5002 void
5003func_ref(char_u *name)
5004{
5005 ufunc_T *fp;
5006
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005007 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005008 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005009 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005010 if (fp != NULL)
5011 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005012 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005013 // Only give an error for a numbered function.
5014 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005015 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005016}
5017
5018/*
5019 * Count a reference to a Function.
5020 */
5021 void
5022func_ptr_ref(ufunc_T *fp)
5023{
5024 if (fp != NULL)
5025 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005026}
5027
5028/*
5029 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5030 * referenced from anywhere that is in use.
5031 */
5032 static int
5033can_free_funccal(funccall_T *fc, int copyID)
5034{
5035 return (fc->l_varlist.lv_copyID != copyID
5036 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005037 && fc->l_avars.dv_copyID != copyID
5038 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005039}
5040
5041/*
5042 * ":return [expr]"
5043 */
5044 void
5045ex_return(exarg_T *eap)
5046{
5047 char_u *arg = eap->arg;
5048 typval_T rettv;
5049 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005050 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005051
5052 if (current_funccal == NULL)
5053 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005054 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005055 return;
5056 }
5057
Bram Moolenaar844fb642021-10-23 13:32:30 +01005058 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005059 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5060
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005061 if (eap->skip)
5062 ++emsg_skip;
5063
5064 eap->nextcmd = NULL;
5065 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005066 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005067 {
5068 if (!eap->skip)
5069 returning = do_return(eap, FALSE, TRUE, &rettv);
5070 else
5071 clear_tv(&rettv);
5072 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005073 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005074 else if (!eap->skip)
5075 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005076 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005077 update_force_abort();
5078
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005079 /*
5080 * Return unless the expression evaluation has been cancelled due to an
5081 * aborting error, an interrupt, or an exception.
5082 */
5083 if (!aborting())
5084 returning = do_return(eap, FALSE, TRUE, NULL);
5085 }
5086
Bram Moolenaare38eab22019-12-05 21:50:01 +01005087 // When skipping or the return gets pending, advance to the next command
5088 // in this line (!returning). Otherwise, ignore the rest of the line.
5089 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005090 if (returning)
5091 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005092 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005093 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005094
5095 if (eap->skip)
5096 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005097 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005098}
5099
5100/*
5101 * ":1,25call func(arg1, arg2)" function call.
5102 */
5103 void
5104ex_call(exarg_T *eap)
5105{
5106 char_u *arg = eap->arg;
5107 char_u *startarg;
5108 char_u *name;
5109 char_u *tofree;
5110 int len;
5111 typval_T rettv;
5112 linenr_T lnum;
5113 int doesrange;
5114 int failed = FALSE;
5115 funcdict_T fudi;
5116 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005117 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005118 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005119 int found_var = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005120
Bram Moolenaare6b53242020-07-01 17:28:33 +02005121 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005122 if (eap->skip)
5123 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005124 // trans_function_name() doesn't work well when skipping, use eval0()
5125 // instead to skip to any following command, e.g. for:
5126 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005127 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005128 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005129 clear_tv(&rettv);
5130 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005131 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005132 return;
5133 }
5134
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005135 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
5136 &fudi, &partial, in_vim9script() ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005137 if (fudi.fd_newkey != NULL)
5138 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005139 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005140 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005141 vim_free(fudi.fd_newkey);
5142 }
5143 if (tofree == NULL)
5144 return;
5145
Bram Moolenaare38eab22019-12-05 21:50:01 +01005146 // Increase refcount on dictionary, it could get deleted when evaluating
5147 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005148 if (fudi.fd_dict != NULL)
5149 ++fudi.fd_dict->dv_refcount;
5150
Bram Moolenaare38eab22019-12-05 21:50:01 +01005151 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
5152 // contents. For VAR_PARTIAL get its partial, unless we already have one
5153 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005154 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005155 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005156 in_vim9script() && type == NULL ? &type : NULL, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005157
Bram Moolenaare38eab22019-12-05 21:50:01 +01005158 // Skip white space to allow ":call func ()". Not good, but required for
5159 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005160 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005161 if (*startarg != '(')
5162 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005163 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005164 goto end;
5165 }
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005166 if (in_vim9script() && startarg > arg)
5167 {
5168 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
5169 goto end;
5170 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005171
5172 /*
5173 * When skipping, evaluate the function once, to find the end of the
5174 * arguments.
5175 * When the function takes a range, this is discovered after the first
5176 * call, and the loop is broken.
5177 */
5178 if (eap->skip)
5179 {
5180 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005181 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005182 }
5183 else
5184 lnum = eap->line1;
5185 for ( ; lnum <= eap->line2; ++lnum)
5186 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005187 funcexe_T funcexe;
5188
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005189 if (!eap->skip && eap->addr_count > 0)
5190 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01005191 if (lnum > curbuf->b_ml.ml_line_count)
5192 {
5193 // If the function deleted lines or switched to another buffer
5194 // the line number may become invalid.
Bram Moolenaar108010a2021-06-27 22:03:33 +02005195 emsg(_(e_invalid_range));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01005196 break;
5197 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005198 curwin->w_cursor.lnum = lnum;
5199 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005200 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005201 }
5202 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005203
Bram Moolenaara80faa82020-04-12 19:37:17 +02005204 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005205 funcexe.fe_firstline = eap->line1;
5206 funcexe.fe_lastline = eap->line2;
5207 funcexe.fe_doesrange = &doesrange;
5208 funcexe.fe_evaluate = !eap->skip;
5209 funcexe.fe_partial = partial;
5210 funcexe.fe_selfdict = fudi.fd_dict;
5211 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005212 funcexe.fe_found_var = found_var;
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005213 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaare6b53242020-07-01 17:28:33 +02005214 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005215 {
5216 failed = TRUE;
5217 break;
5218 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01005219 if (has_watchexpr())
5220 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005221
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005222 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005223 if (handle_subscript(&arg, &rettv,
5224 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005225 {
5226 failed = TRUE;
5227 break;
5228 }
5229
5230 clear_tv(&rettv);
5231 if (doesrange || eap->skip)
5232 break;
5233
Bram Moolenaare38eab22019-12-05 21:50:01 +01005234 // Stop when immediately aborting on error, or when an interrupt
5235 // occurred or an exception was thrown but not caught.
5236 // get_func_tv() returned OK, so that the check for trailing
5237 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005238 if (aborting())
5239 break;
5240 }
5241 if (eap->skip)
5242 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005243 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005244
Bram Moolenaar1d341892021-09-18 15:25:52 +02005245 // When inside :try we need to check for following "| catch" or "| endtry".
5246 // Not when there was an error, but do check if an exception was thrown.
5247 if ((!aborting() || did_throw)
5248 && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005249 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005250 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02005251 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02005252 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005253 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02005254 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005255 {
5256 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005257 semsg(_(e_trailing_arg), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005258 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005259 }
5260 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02005261 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005262 }
5263
5264end:
5265 dict_unref(fudi.fd_dict);
5266 vim_free(tofree);
5267}
5268
5269/*
5270 * Return from a function. Possibly makes the return pending. Also called
5271 * for a pending return at the ":endtry" or after returning from an extra
5272 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
5273 * when called due to a ":return" command. "rettv" may point to a typval_T
5274 * with the return rettv. Returns TRUE when the return can be carried out,
5275 * FALSE when the return gets pending.
5276 */
5277 int
5278do_return(
5279 exarg_T *eap,
5280 int reanimate,
5281 int is_cmd,
5282 void *rettv)
5283{
5284 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01005285 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005286
5287 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005288 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005289 current_funccal->returned = FALSE;
5290
5291 /*
5292 * Cleanup (and inactivate) conditionals, but stop when a try conditional
5293 * not in its finally clause (which then is to be executed next) is found.
5294 * In this case, make the ":return" pending for execution at the ":endtry".
5295 * Otherwise, return normally.
5296 */
5297 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
5298 if (idx >= 0)
5299 {
5300 cstack->cs_pending[idx] = CSTP_RETURN;
5301
5302 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005303 // A pending return again gets pending. "rettv" points to an
5304 // allocated variable with the rettv of the original ":return"'s
5305 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005306 cstack->cs_rettv[idx] = rettv;
5307 else
5308 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005309 // When undoing a return in order to make it pending, get the stored
5310 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005311 if (reanimate)
5312 rettv = current_funccal->rettv;
5313
5314 if (rettv != NULL)
5315 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005316 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005317 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
5318 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
5319 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005320 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005321 }
5322 else
5323 cstack->cs_rettv[idx] = NULL;
5324
5325 if (reanimate)
5326 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005327 // The pending return value could be overwritten by a ":return"
5328 // without argument in a finally clause; reset the default
5329 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005330 current_funccal->rettv->v_type = VAR_NUMBER;
5331 current_funccal->rettv->vval.v_number = 0;
5332 }
5333 }
5334 report_make_pending(CSTP_RETURN, rettv);
5335 }
5336 else
5337 {
5338 current_funccal->returned = TRUE;
5339
Bram Moolenaare38eab22019-12-05 21:50:01 +01005340 // If the return is carried out now, store the return value. For
5341 // a return immediately after reanimation, the value is already
5342 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005343 if (!reanimate && rettv != NULL)
5344 {
5345 clear_tv(current_funccal->rettv);
5346 *current_funccal->rettv = *(typval_T *)rettv;
5347 if (!is_cmd)
5348 vim_free(rettv);
5349 }
5350 }
5351
5352 return idx < 0;
5353}
5354
5355/*
5356 * Free the variable with a pending return value.
5357 */
5358 void
5359discard_pending_return(void *rettv)
5360{
5361 free_tv((typval_T *)rettv);
5362}
5363
5364/*
5365 * Generate a return command for producing the value of "rettv". The result
5366 * is an allocated string. Used by report_pending() for verbose messages.
5367 */
5368 char_u *
5369get_return_cmd(void *rettv)
5370{
5371 char_u *s = NULL;
5372 char_u *tofree = NULL;
5373 char_u numbuf[NUMBUFLEN];
5374
5375 if (rettv != NULL)
5376 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
5377 if (s == NULL)
5378 s = (char_u *)"";
5379
5380 STRCPY(IObuff, ":return ");
5381 STRNCPY(IObuff + 8, s, IOSIZE - 8);
5382 if (STRLEN(s) + 8 >= IOSIZE)
5383 STRCPY(IObuff + IOSIZE - 4, "...");
5384 vim_free(tofree);
5385 return vim_strsave(IObuff);
5386}
5387
5388/*
5389 * Get next function line.
5390 * Called by do_cmdline() to get the next line.
5391 * Returns allocated string, or NULL for end of function.
5392 */
5393 char_u *
5394get_func_line(
5395 int c UNUSED,
5396 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02005397 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005398 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005399{
5400 funccall_T *fcp = (funccall_T *)cookie;
5401 ufunc_T *fp = fcp->func;
5402 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005403 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005404
Bram Moolenaare38eab22019-12-05 21:50:01 +01005405 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005406 if (fcp->dbg_tick != debug_tick)
5407 {
5408 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005409 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005410 fcp->dbg_tick = debug_tick;
5411 }
5412#ifdef FEAT_PROFILE
5413 if (do_profiling == PROF_YES)
5414 func_line_end(cookie);
5415#endif
5416
5417 gap = &fp->uf_lines;
5418 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5419 || fcp->returned)
5420 retval = NULL;
5421 else
5422 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005423 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005424 while (fcp->linenr < gap->ga_len
5425 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
5426 ++fcp->linenr;
5427 if (fcp->linenr >= gap->ga_len)
5428 retval = NULL;
5429 else
5430 {
5431 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005432 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005433#ifdef FEAT_PROFILE
5434 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005435 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005436#endif
5437 }
5438 }
5439
Bram Moolenaare38eab22019-12-05 21:50:01 +01005440 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005441 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005442 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005443 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01005444 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005445 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005446 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005447 fcp->dbg_tick = debug_tick;
5448 }
5449
5450 return retval;
5451}
5452
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005453/*
5454 * Return TRUE if the currently active function should be ended, because a
5455 * return was encountered or an error occurred. Used inside a ":while".
5456 */
5457 int
5458func_has_ended(void *cookie)
5459{
5460 funccall_T *fcp = (funccall_T *)cookie;
5461
Bram Moolenaare38eab22019-12-05 21:50:01 +01005462 // Ignore the "abort" flag if the abortion behavior has been changed due to
5463 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005464 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5465 || fcp->returned);
5466}
5467
5468/*
5469 * return TRUE if cookie indicates a function which "abort"s on errors.
5470 */
5471 int
5472func_has_abort(
5473 void *cookie)
5474{
5475 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
5476}
5477
5478
5479/*
5480 * Turn "dict.Func" into a partial for "Func" bound to "dict".
5481 * Don't do this when "Func" is already a partial that was bound
5482 * explicitly (pt_auto is FALSE).
5483 * Changes "rettv" in-place.
5484 * Returns the updated "selfdict_in".
5485 */
5486 dict_T *
5487make_partial(dict_T *selfdict_in, typval_T *rettv)
5488{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005489 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005490 char_u *tofree = NULL;
5491 ufunc_T *fp;
5492 char_u fname_buf[FLEN_FIXED + 1];
5493 int error;
5494 dict_T *selfdict = selfdict_in;
5495
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005496 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
5497 fp = rettv->vval.v_partial->pt_func;
5498 else
5499 {
5500 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
5501 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005502 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005503 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005504 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005505 vim_free(tofree);
5506 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005507
5508 if (fp != NULL && (fp->uf_flags & FC_DICT))
5509 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005510 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005511
5512 if (pt != NULL)
5513 {
5514 pt->pt_refcount = 1;
5515 pt->pt_dict = selfdict;
5516 pt->pt_auto = TRUE;
5517 selfdict = NULL;
5518 if (rettv->v_type == VAR_FUNC)
5519 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005520 // Just a function: Take over the function name and use
5521 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005522 pt->pt_name = rettv->vval.v_string;
5523 }
5524 else
5525 {
5526 partial_T *ret_pt = rettv->vval.v_partial;
5527 int i;
5528
Bram Moolenaare38eab22019-12-05 21:50:01 +01005529 // Partial: copy the function name, use selfdict and copy
5530 // args. Can't take over name or args, the partial might
5531 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005532 if (ret_pt->pt_name != NULL)
5533 {
5534 pt->pt_name = vim_strsave(ret_pt->pt_name);
5535 func_ref(pt->pt_name);
5536 }
5537 else
5538 {
5539 pt->pt_func = ret_pt->pt_func;
5540 func_ptr_ref(pt->pt_func);
5541 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005542 if (ret_pt->pt_argc > 0)
5543 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005544 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005545 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005546 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005547 pt->pt_argc = 0;
5548 else
5549 {
5550 pt->pt_argc = ret_pt->pt_argc;
5551 for (i = 0; i < pt->pt_argc; i++)
5552 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
5553 }
5554 }
5555 partial_unref(ret_pt);
5556 }
5557 rettv->v_type = VAR_PARTIAL;
5558 rettv->vval.v_partial = pt;
5559 }
5560 }
5561 return selfdict;
5562}
5563
5564/*
5565 * Return the name of the executed function.
5566 */
5567 char_u *
5568func_name(void *cookie)
5569{
5570 return ((funccall_T *)cookie)->func->uf_name;
5571}
5572
5573/*
5574 * Return the address holding the next breakpoint line for a funccall cookie.
5575 */
5576 linenr_T *
5577func_breakpoint(void *cookie)
5578{
5579 return &((funccall_T *)cookie)->breakpoint;
5580}
5581
5582/*
5583 * Return the address holding the debug tick for a funccall cookie.
5584 */
5585 int *
5586func_dbg_tick(void *cookie)
5587{
5588 return &((funccall_T *)cookie)->dbg_tick;
5589}
5590
5591/*
5592 * Return the nesting level for a funccall cookie.
5593 */
5594 int
5595func_level(void *cookie)
5596{
5597 return ((funccall_T *)cookie)->level;
5598}
5599
5600/*
5601 * Return TRUE when a function was ended by a ":return" command.
5602 */
5603 int
5604current_func_returned(void)
5605{
5606 return current_funccal->returned;
5607}
5608
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005609 int
5610free_unref_funccal(int copyID, int testing)
5611{
5612 int did_free = FALSE;
5613 int did_free_funccal = FALSE;
5614 funccall_T *fc, **pfc;
5615
5616 for (pfc = &previous_funccal; *pfc != NULL; )
5617 {
5618 if (can_free_funccal(*pfc, copyID))
5619 {
5620 fc = *pfc;
5621 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01005622 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005623 did_free = TRUE;
5624 did_free_funccal = TRUE;
5625 }
5626 else
5627 pfc = &(*pfc)->caller;
5628 }
5629 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005630 // When a funccal was freed some more items might be garbage
5631 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005632 (void)garbage_collect(testing);
5633
5634 return did_free;
5635}
5636
5637/*
Bram Moolenaarba209902016-08-24 22:06:38 +02005638 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005639 */
5640 static funccall_T *
5641get_funccal(void)
5642{
5643 int i;
5644 funccall_T *funccal;
5645 funccall_T *temp_funccal;
5646
5647 funccal = current_funccal;
5648 if (debug_backtrace_level > 0)
5649 {
5650 for (i = 0; i < debug_backtrace_level; i++)
5651 {
5652 temp_funccal = funccal->caller;
5653 if (temp_funccal)
5654 funccal = temp_funccal;
5655 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005656 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005657 debug_backtrace_level = i;
5658 }
5659 }
5660 return funccal;
5661}
5662
5663/*
5664 * Return the hashtable used for local variables in the current funccal.
5665 * Return NULL if there is no current funccal.
5666 */
5667 hashtab_T *
5668get_funccal_local_ht()
5669{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005670 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005671 return NULL;
5672 return &get_funccal()->l_vars.dv_hashtab;
5673}
5674
5675/*
5676 * Return the l: scope variable.
5677 * Return NULL if there is no current funccal.
5678 */
5679 dictitem_T *
5680get_funccal_local_var()
5681{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005682 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005683 return NULL;
5684 return &get_funccal()->l_vars_var;
5685}
5686
5687/*
5688 * Return the hashtable used for argument in the current funccal.
5689 * Return NULL if there is no current funccal.
5690 */
5691 hashtab_T *
5692get_funccal_args_ht()
5693{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005694 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005695 return NULL;
5696 return &get_funccal()->l_avars.dv_hashtab;
5697}
5698
5699/*
5700 * Return the a: scope variable.
5701 * Return NULL if there is no current funccal.
5702 */
5703 dictitem_T *
5704get_funccal_args_var()
5705{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005706 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005707 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01005708 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005709}
5710
5711/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005712 * List function variables, if there is a function.
5713 */
5714 void
5715list_func_vars(int *first)
5716{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005717 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005718 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01005719 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005720}
5721
5722/*
5723 * If "ht" is the hashtable for local variables in the current funccal, return
5724 * the dict that contains it.
5725 * Otherwise return NULL.
5726 */
5727 dict_T *
5728get_current_funccal_dict(hashtab_T *ht)
5729{
5730 if (current_funccal != NULL
5731 && ht == &current_funccal->l_vars.dv_hashtab)
5732 return &current_funccal->l_vars;
5733 return NULL;
5734}
5735
5736/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005737 * Search hashitem in parent scope.
5738 */
5739 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005740find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005741{
5742 funccall_T *old_current_funccal = current_funccal;
5743 hashtab_T *ht;
5744 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005745 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005746
5747 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5748 return NULL;
5749
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02005750 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005751 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02005752 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005753 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005754 ht = find_var_ht(name, &varname);
5755 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02005756 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005757 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02005758 if (!HASHITEM_EMPTY(hi))
5759 {
5760 *pht = ht;
5761 break;
5762 }
5763 }
5764 if (current_funccal == current_funccal->func->uf_scoped)
5765 break;
5766 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005767 }
5768 current_funccal = old_current_funccal;
5769
5770 return hi;
5771}
5772
5773/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005774 * Search variable in parent scope.
5775 */
5776 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005777find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005778{
5779 dictitem_T *v = NULL;
5780 funccall_T *old_current_funccal = current_funccal;
5781 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005782 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005783
5784 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5785 return NULL;
5786
Bram Moolenaare38eab22019-12-05 21:50:01 +01005787 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005788 current_funccal = current_funccal->func->uf_scoped;
5789 while (current_funccal)
5790 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005791 ht = find_var_ht(name, &varname);
5792 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005793 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005794 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005795 if (v != NULL)
5796 break;
5797 }
5798 if (current_funccal == current_funccal->func->uf_scoped)
5799 break;
5800 current_funccal = current_funccal->func->uf_scoped;
5801 }
5802 current_funccal = old_current_funccal;
5803
5804 return v;
5805}
5806
5807/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005808 * Set "copyID + 1" in previous_funccal and callers.
5809 */
5810 int
5811set_ref_in_previous_funccal(int copyID)
5812{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005813 funccall_T *fc;
5814
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005815 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005816 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005817 fc->fc_copyID = copyID + 1;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005818 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
5819 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
5820 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL))
5821 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005822 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005823 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005824}
5825
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005826 static int
5827set_ref_in_funccal(funccall_T *fc, int copyID)
5828{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005829 if (fc->fc_copyID != copyID)
5830 {
5831 fc->fc_copyID = copyID;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005832 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
5833 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
5834 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
5835 || set_ref_in_func(NULL, fc->func, copyID))
5836 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005837 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005838 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005839}
5840
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005841/*
5842 * Set "copyID" in all local vars and arguments in the call stack.
5843 */
5844 int
5845set_ref_in_call_stack(int copyID)
5846{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005847 funccall_T *fc;
5848 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005849
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005850 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5851 if (set_ref_in_funccal(fc, copyID))
5852 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005853
5854 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005855 for (entry = funccal_stack; entry != NULL; entry = entry->next)
5856 for (fc = entry->top_funccal; fc != NULL; fc = fc->caller)
5857 if (set_ref_in_funccal(fc, copyID))
5858 return TRUE;
5859 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005860}
5861
5862/*
5863 * Set "copyID" in all functions available by name.
5864 */
5865 int
5866set_ref_in_functions(int copyID)
5867{
5868 int todo;
5869 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005870 ufunc_T *fp;
5871
5872 todo = (int)func_hashtab.ht_used;
5873 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005874 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005875 if (!HASHITEM_EMPTY(hi))
5876 {
5877 --todo;
5878 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005879 if (!func_name_refcount(fp->uf_name)
5880 && set_ref_in_func(NULL, fp, copyID))
5881 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005882 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005883 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005884 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005885}
5886
5887/*
5888 * Set "copyID" in all function arguments.
5889 */
5890 int
5891set_ref_in_func_args(int copyID)
5892{
5893 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005894
5895 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005896 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5897 copyID, NULL, NULL))
5898 return TRUE;
5899 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005900}
5901
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005902/*
5903 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005904 * Returns TRUE if setting references failed somehow.
5905 */
5906 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005907set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005908{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005909 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005910 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005911 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005912 char_u fname_buf[FLEN_FIXED + 1];
5913 char_u *tofree = NULL;
5914 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005915 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005916
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005917 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005918 return FALSE;
5919
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005920 if (fp_in == NULL)
5921 {
5922 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005923 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005924 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005925 if (fp != NULL)
5926 {
5927 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005928 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005929 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005930
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005931 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005932 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005933}
5934
Bram Moolenaare38eab22019-12-05 21:50:01 +01005935#endif // FEAT_EVAL