blob: 6821569b686cde8f435fdb0c4554a7f7367fe6c6 [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 Moolenaarbc7ce672016-08-01 22:49:22 +020032static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010033static void func_clear(ufunc_T *fp, int force);
34static int func_free(ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020035
36 void
37func_init()
38{
39 hash_init(&func_hashtab);
40}
41
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020042/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020043 * Return the function hash table
44 */
45 hashtab_T *
46func_tbl_get(void)
47{
48 return &func_hashtab;
49}
50
51/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020052 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020053 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010054 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010055 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056 * Return a pointer to after the type.
57 * When something is wrong return "arg".
58 */
59 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010060one_function_arg(
61 char_u *arg,
62 garray_T *newargs,
63 garray_T *argtypes,
64 int types_optional,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010065 evalarg_T *evalarg,
Bram Moolenaar2a389082021-04-09 20:24:31 +020066 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010067 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010068{
Bram Moolenaar6e949782020-04-13 17:21:00 +020069 char_u *p = arg;
70 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020071 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072
73 while (ASCII_ISALNUM(*p) || *p == '_')
74 ++p;
75 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020076 || (argtypes == NULL
77 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
78 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 {
80 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000081 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082 return arg;
83 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010084
Bram Moolenaar057e84a2021-02-28 16:55:11 +010085 // Vim9 script: cannot use script var name for argument. In function: also
86 // check local vars and arguments.
87 if (!skip && argtypes != NULL && check_defined(arg, p - arg,
88 evalarg == NULL ? NULL : evalarg->eval_cctx, TRUE) == FAIL)
Bram Moolenaarb4893b82021-02-21 22:20:24 +010089 return arg;
Bram Moolenaarb4893b82021-02-21 22:20:24 +010090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010091 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
92 return arg;
93 if (newargs != NULL)
94 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095 int c;
96 int i;
97
98 c = *p;
99 *p = NUL;
100 arg_copy = vim_strsave(arg);
101 if (arg_copy == NULL)
102 {
103 *p = c;
104 return arg;
105 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200106 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200107 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200108 // Check for duplicate argument name.
109 for (i = 0; i < newargs->ga_len; ++i)
110 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
111 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000112 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200113 vim_free(arg_copy);
114 return arg;
115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100116 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
117 newargs->ga_len++;
118
119 *p = c;
120 }
121
122 // get any type from "arg: type"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100123 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 {
125 char_u *type = NULL;
126
Bram Moolenaar6e949782020-04-13 17:21:00 +0200127 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
128 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200129 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200130 arg_copy == NULL ? arg : arg_copy);
131 p = skipwhite(p);
132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 if (*p == ':')
134 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200135 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100136 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200137 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100138 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200139 return arg;
140 }
141 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200142 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100143 if (!skip)
144 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200146 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200147 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200148 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200149 arg_copy == NULL ? arg : arg_copy);
150 return arg;
151 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100152 if (!skip)
153 {
154 if (type == NULL && types_optional)
155 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200156 type = vim_strsave((char_u *)
157 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100158 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
159 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160 }
161
162 return p;
163}
164
165/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000166 * Handle line continuation in function arguments or body.
167 * Get a next line, store it in "eap" if appropriate and use "line_to_free" to
168 * handle freeing the line later.
169 */
170 static char_u *
171get_function_line(
172 exarg_T *eap,
173 char_u **line_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000174 int indent,
175 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000176{
177 char_u *theline;
178
179 if (eap->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000180 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000181 else
182 theline = eap->getline(':', eap->cookie, indent, getline_options);
183 if (theline != NULL)
184 {
185 if (*eap->cmdlinep == *line_to_free)
186 *eap->cmdlinep = theline;
187 vim_free(*line_to_free);
188 *line_to_free = theline;
189 }
190
191 return theline;
192}
193
194/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200195 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100196 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100197 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200198 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100199 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200200get_function_args(
201 char_u **argp,
202 char_u endchar,
203 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100205 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100206 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200207 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200208 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200209 int skip,
210 exarg_T *eap,
211 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200212{
213 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100214 char_u *arg;
215 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200216 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200217 int any_default = FALSE;
218 char_u *expr;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100219 char_u *whitep = *argp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200220
221 if (newargs != NULL)
222 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100223 if (argtypes != NULL)
224 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200225 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200226 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200227
228 if (varargs != NULL)
229 *varargs = FALSE;
230
231 /*
232 * Isolate the arguments: "arg1, arg2, ...)"
233 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100234 arg = skipwhite(*argp);
235 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200236 while (*p != endchar)
237 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200238 while (eap != NULL && eap->getline != NULL
239 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200240 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200241 // End of the line, get the next one.
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000242 char_u *theline = get_function_line(eap, line_to_free, 0,
243 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000244
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200245 if (theline == NULL)
246 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200247 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200248 p = skipwhite(theline);
249 }
250
251 if (mustend && *p != endchar)
252 {
253 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000254 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100255 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200256 }
257 if (*p == endchar)
258 break;
259
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200260 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
261 {
262 if (varargs != NULL)
263 *varargs = TRUE;
264 p += 3;
265 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266
267 if (argtypes != NULL)
268 {
269 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200270 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100272 if (!skip)
273 emsg(_(e_missing_name_after_dots));
274 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275 }
276
277 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100278 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaar2a389082021-04-09 20:24:31 +0200279 evalarg, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100280 if (p == arg)
281 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100282 if (*skipwhite(p) == '=')
283 {
284 emsg(_(e_cannot_use_default_for_variable_arguments));
285 break;
286 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100287 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200288 }
289 else
290 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200291 char_u *np;
292
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200293 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100294 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaar2a389082021-04-09 20:24:31 +0200295 evalarg, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100296 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200297 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200298
Bram Moolenaar015cf102021-06-26 21:52:02 +0200299 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200300 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200301 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200302 if (*np == '=' && np[1] != '=' && np[1] != '~'
303 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200304 {
305 typval_T rettv;
306
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100307 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200308 any_default = TRUE;
309 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200310 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200311 p = skipwhite(p);
312 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200313 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200314 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200315 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200316 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200317 if (ga_grow(default_args, 1) == FAIL)
318 goto err_ret;
319
320 // trim trailing whitespace
321 while (p > expr && VIM_ISWHITE(p[-1]))
322 p--;
323 c = *p;
324 *p = NUL;
325 expr = vim_strsave(expr);
326 if (expr == NULL)
327 {
328 *p = c;
329 goto err_ret;
330 }
331 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200332 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200333 default_args->ga_len++;
334 *p = c;
335 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200336 }
337 else
338 mustend = TRUE;
339 }
340 else if (any_default)
341 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000342 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200343 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200344 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200345
346 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
347 {
348 // Be tolerant when skipping
349 if (!skip)
350 {
351 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
352 goto err_ret;
353 }
354 p = skipwhite(p);
355 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200356 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200357 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200358 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200359 // Don't give this error when skipping, it makes the "->" not
360 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100361 // Allow missing space after comma in legacy functions.
362 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200363 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
364 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100365 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200366 goto err_ret;
367 }
368 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200369 else
370 mustend = TRUE;
371 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200372 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200373 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200374 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200375
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200376 if (*p != endchar)
377 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100378 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200379
380 *argp = p;
381 return OK;
382
383err_ret:
384 if (newargs != NULL)
385 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200386 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200387 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200388 return FAIL;
389}
390
391/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100392 * Parse the argument types, filling "fp->uf_arg_types".
393 * Return OK or FAIL.
394 */
395 static int
396parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
397{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200398 int len = 0;
399
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100400 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
401 if (argtypes->ga_len > 0)
402 {
403 // When "varargs" is set the last name/type goes into uf_va_name
404 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200405 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100406
407 if (len > 0)
408 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
409 if (fp->uf_arg_types != NULL)
410 {
411 int i;
412 type_T *type;
413
414 for (i = 0; i < len; ++ i)
415 {
416 char_u *p = ((char_u **)argtypes->ga_data)[i];
417
418 if (p == NULL)
419 // will get the type from the default value
420 type = &t_unknown;
421 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100422 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100423 if (type == NULL)
424 return FAIL;
425 fp->uf_arg_types[i] = type;
426 }
427 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100428 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200429
430 if (varargs)
431 {
432 char_u *p;
433
434 // Move the last argument "...name: type" to uf_va_name and
435 // uf_va_type.
436 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
437 [fp->uf_args.ga_len - 1];
438 --fp->uf_args.ga_len;
439 p = ((char_u **)argtypes->ga_data)[len];
440 if (p == NULL)
441 // TODO: get type from default value
442 fp->uf_va_type = &t_list_any;
443 else
444 {
445 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
446 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
447 {
448 semsg(_(e_variable_arguments_type_must_be_list_str),
449 ((char_u **)argtypes->ga_data)[len]);
450 return FAIL;
451 }
452 }
453 if (fp->uf_va_type == NULL)
454 return FAIL;
455 }
456
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100457 return OK;
458}
459
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100460 static int
461parse_return_type(ufunc_T *fp, char_u *ret_type)
462{
463 if (ret_type == NULL)
464 fp->uf_ret_type = &t_void;
465 else
466 {
467 char_u *p = ret_type;
468
469 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
470 if (fp->uf_ret_type == NULL)
471 {
472 fp->uf_ret_type = &t_void;
473 return FAIL;
474 }
475 }
476 return OK;
477}
478
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100479/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200480 * Register function "fp" as using "current_funccal" as its scope.
481 */
482 static int
483register_closure(ufunc_T *fp)
484{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200485 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100486 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200487 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200488 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200489 fp->uf_scoped = current_funccal;
490 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200491
Bram Moolenaar58016442016-07-31 18:30:22 +0200492 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
493 return FAIL;
494 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
495 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200496 return OK;
497}
498
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100499 static void
500set_ufunc_name(ufunc_T *fp, char_u *name)
501{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100502 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
503 // actually extends beyond the struct.
504 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100505
506 if (name[0] == K_SPECIAL)
507 {
508 fp->uf_name_exp = alloc(STRLEN(name) + 3);
509 if (fp->uf_name_exp != NULL)
510 {
511 STRCPY(fp->uf_name_exp, "<SNR>");
512 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
513 }
514 }
515}
516
Bram Moolenaar58016442016-07-31 18:30:22 +0200517/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200518 * Get a name for a lambda. Returned in static memory.
519 */
520 char_u *
521get_lambda_name(void)
522{
523 static char_u name[30];
524 static int lambda_no = 0;
525
526 sprintf((char*)name, "<lambda>%d", ++lambda_no);
527 return name;
528}
529
Bram Moolenaar801ab062020-06-25 19:27:56 +0200530#if defined(FEAT_LUA) || defined(PROTO)
531/*
532 * Registers a native C callback which can be called from Vim script.
533 * Returns the name of the Vim script function.
534 */
535 char_u *
536register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
537{
538 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200539 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200540
541 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
542 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200543 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200544
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200545 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200546 fp->uf_refcount = 1;
547 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000548 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200549 fp->uf_calls = 0;
550 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200551 fp->uf_cb = cb;
552 fp->uf_cb_free = cb_free;
553 fp->uf_cb_state = state;
554
555 set_ufunc_name(fp, name);
556 hash_add(&func_hashtab, UF2HIKEY(fp));
557
558 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200559}
560#endif
561
Bram Moolenaar04b12692020-05-04 23:24:44 +0200562/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100563 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100564 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100565 * If "white_error" is not NULL check for correct use of white space and set
566 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100567 * Return NULL if no valid arrow found.
568 */
569 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100570skip_arrow(
571 char_u *start,
572 int equal_arrow,
573 char_u **ret_type,
574 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100575{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100576 char_u *s = start;
577 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100578
579 if (equal_arrow)
580 {
581 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100582 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100583 if (white_error != NULL && !VIM_ISWHITE(s[1]))
584 {
585 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100586 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100587 return NULL;
588 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100589 s = skipwhite(s + 1);
590 *ret_type = s;
591 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100592 if (s == *ret_type)
593 {
594 emsg(_(e_missing_return_type));
595 return NULL;
596 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100597 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100598 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100599 s = skipwhite(s);
600 if (*s != '=')
601 return NULL;
602 ++s;
603 }
604 if (*s != '>')
605 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100606 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
607 || !IS_WHITE_OR_NUL(s[1])))
608 {
609 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100610 semsg(_(e_white_space_required_before_and_after_str_at_str),
611 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100612 return NULL;
613 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100614 return skipwhite(s + 1);
615}
616
617/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100618 * Check if "*cmd" points to a function command and if so advance "*cmd" and
619 * return TRUE.
620 * Otherwise return FALSE;
621 * Do not consider "function(" to be a command.
622 */
623 static int
624is_function_cmd(char_u **cmd)
625{
626 char_u *p = *cmd;
627
628 if (checkforcmd(&p, "function", 2))
629 {
630 if (*p == '(')
631 return FALSE;
632 *cmd = p;
633 return TRUE;
634 }
635 return FALSE;
636}
637
638/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200639 * Called when defining a function: The context may be needed for script
640 * variables declared in a block that is visible now but not when the function
641 * is compiled or called later.
642 */
643 static void
644function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
645{
646 if (cstack != NULL && cstack->cs_idx >= 0)
647 {
648 int count = cstack->cs_idx + 1;
649 int i;
650
651 fp->uf_block_ids = ALLOC_MULT(int, count);
652 if (fp->uf_block_ids != NULL)
653 {
654 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
655 sizeof(int) * count);
656 fp->uf_block_depth = count;
657 }
658
659 // Set flag in each block to indicate a function was defined. This
660 // is used to keep the variable when leaving the block, see
661 // hide_script_var().
662 for (i = 0; i <= cstack->cs_idx; ++i)
663 cstack->cs_flags[i] |= CSF_FUNC_DEF;
664 }
665}
666
667/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100668 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200669 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100670 * "newlines" must already have been initialized.
671 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
672 */
673 static int
674get_function_body(
675 exarg_T *eap,
676 garray_T *newlines,
677 char_u *line_arg_in,
678 char_u **line_to_free)
679{
680 linenr_T sourcing_lnum_top = SOURCING_LNUM;
681 linenr_T sourcing_lnum_off;
682 int saved_wait_return = need_wait_return;
683 char_u *line_arg = line_arg_in;
684 int vim9_function = eap->cmdidx == CMD_def
685 || eap->cmdidx == CMD_block;
686#define MAX_FUNC_NESTING 50
687 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200688 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100689 int nesting = 0;
690 getline_opt_T getline_options;
691 int indent = 2;
692 char_u *skip_until = NULL;
693 int ret = FAIL;
694 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200695 int heredoc_concat_len = 0;
696 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100697 char_u *heredoc_trimmed = NULL;
698
Bram Moolenaar20677332021-06-06 17:02:53 +0200699 ga_init2(&heredoc_ga, 1, 500);
700
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100701 // Detect having skipped over comment lines to find the return
702 // type. Add NULL lines to keep the line count correct.
703 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
704 if (SOURCING_LNUM < sourcing_lnum_off)
705 {
706 sourcing_lnum_off -= SOURCING_LNUM;
707 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
708 goto theend;
709 while (sourcing_lnum_off-- > 0)
710 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
711 }
712
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200713 nesting_def[0] = vim9_function;
714 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100715 getline_options = vim9_function
716 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
717 for (;;)
718 {
719 char_u *theline;
720 char_u *p;
721 char_u *arg;
722
723 if (KeyTyped)
724 {
725 msg_scroll = TRUE;
726 saved_wait_return = FALSE;
727 }
728 need_wait_return = FALSE;
729
730 if (line_arg != NULL)
731 {
732 // Use eap->arg, split up in parts by line breaks.
733 theline = line_arg;
734 p = vim_strchr(theline, '\n');
735 if (p == NULL)
736 line_arg += STRLEN(line_arg);
737 else
738 {
739 *p = NUL;
740 line_arg = p + 1;
741 }
742 }
743 else
744 {
Bram Moolenaar7473a842021-12-28 17:55:26 +0000745 theline = get_function_line(eap, line_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100746 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100747 }
748 if (KeyTyped)
749 lines_left = Rows - 1;
750 if (theline == NULL)
751 {
752 // Use the start of the function for the line number.
753 SOURCING_LNUM = sourcing_lnum_top;
754 if (skip_until != NULL)
755 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200756 else if (nesting_inline[nesting])
757 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100758 else if (eap->cmdidx == CMD_def)
759 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100760 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000761 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100762 goto theend;
763 }
764
765 // Detect line continuation: SOURCING_LNUM increased more than one.
766 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
767 if (SOURCING_LNUM < sourcing_lnum_off)
768 sourcing_lnum_off -= SOURCING_LNUM;
769 else
770 sourcing_lnum_off = 0;
771
772 if (skip_until != NULL)
773 {
774 // Don't check for ":endfunc"/":enddef" between
775 // * ":append" and "."
776 // * ":python <<EOF" and "EOF"
777 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
778 if (heredoc_trimmed == NULL
779 || (is_heredoc && skipwhite(theline) == theline)
780 || STRNCMP(theline, heredoc_trimmed,
781 STRLEN(heredoc_trimmed)) == 0)
782 {
783 if (heredoc_trimmed == NULL)
784 p = theline;
785 else if (is_heredoc)
786 p = skipwhite(theline) == theline
787 ? theline : theline + STRLEN(heredoc_trimmed);
788 else
789 p = theline + STRLEN(heredoc_trimmed);
790 if (STRCMP(p, skip_until) == 0)
791 {
792 VIM_CLEAR(skip_until);
793 VIM_CLEAR(heredoc_trimmed);
794 getline_options = vim9_function
795 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
796 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200797
798 if (heredoc_concat_len > 0)
799 {
800 // Replace the starting line with all the concatenated
801 // lines.
802 ga_concat(&heredoc_ga, theline);
803 vim_free(((char_u **)(newlines->ga_data))[
804 heredoc_concat_len - 1]);
805 ((char_u **)(newlines->ga_data))[
806 heredoc_concat_len - 1] = heredoc_ga.ga_data;
807 ga_init(&heredoc_ga);
808 heredoc_concat_len = 0;
809 theline += STRLEN(theline); // skip the "EOF"
810 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100811 }
812 }
813 }
814 else
815 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200816 int c;
817 char_u *end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100818
819 // skip ':' and blanks
820 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
821 ;
822
823 // Check for "endfunction", "enddef" or "}".
824 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200825 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100826 ? *p == '}'
827 : (checkforcmd(&p, nesting_def[nesting]
828 ? "enddef" : "endfunction", 4)
829 && *p != ':'))
830 {
831 if (nesting-- == 0)
832 {
833 char_u *nextcmd = NULL;
834
835 if (*p == '|' || *p == '}')
836 nextcmd = p + 1;
837 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
838 nextcmd = line_arg;
839 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100840 && (vim9_function || p_verbose > 0))
841 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200842 SOURCING_LNUM = sourcing_lnum_top
843 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100844 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000845 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100846 else
847 give_warning2((char_u *)
848 _("W22: Text found after :endfunction: %s"),
849 p, TRUE);
850 }
851 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100852 {
853 // Another command follows. If the line came from "eap"
854 // we can simply point into it, otherwise we need to
855 // change "eap->cmdlinep".
856 eap->nextcmd = nextcmd;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +0000857 if (*line_to_free != NULL
858 && *eap->cmdlinep != *line_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100859 {
860 vim_free(*eap->cmdlinep);
861 *eap->cmdlinep = *line_to_free;
862 *line_to_free = NULL;
863 }
864 }
865 break;
866 }
867 }
868
869 // Check for mismatched "endfunc" or "enddef".
870 // We don't check for "def" inside "func" thus we also can't check
871 // for "enddef".
872 // We continue to find the end of the function, although we might
873 // not find it.
874 else if (nesting_def[nesting])
875 {
876 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
877 emsg(_(e_mismatched_endfunction));
878 }
879 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
880 emsg(_(e_mismatched_enddef));
881
882 // Increase indent inside "if", "while", "for" and "try", decrease
883 // at "end".
884 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
885 indent -= 2;
886 else if (STRNCMP(p, "if", 2) == 0
887 || STRNCMP(p, "wh", 2) == 0
888 || STRNCMP(p, "for", 3) == 0
889 || STRNCMP(p, "try", 3) == 0)
890 indent += 2;
891
892 // Check for defining a function inside this function.
893 // Only recognize "def" inside "def", not inside "function",
894 // For backwards compatibility, see Test_function_python().
895 c = *p;
896 if (is_function_cmd(&p)
897 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
898 {
899 if (*p == '!')
900 p = skipwhite(p + 1);
901 p += eval_fname_script(p);
902 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
903 NULL, NULL));
904 if (*skipwhite(p) == '(')
905 {
906 if (nesting == MAX_FUNC_NESTING - 1)
907 emsg(_(e_function_nesting_too_deep));
908 else
909 {
910 ++nesting;
911 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200912 nesting_inline[nesting] = FALSE;
913 indent += 2;
914 }
915 }
916 }
917
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200918 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200919 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200920 // Not a comment line: check for nested inline function.
921 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200922 while (end > p && VIM_ISWHITE(*end))
923 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +0200924 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200925 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200926 int is_block;
927
928 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200929 --end;
930 while (end > p && VIM_ISWHITE(*end))
931 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200932 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
933 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200934 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200935 char_u *s = p;
936
937 // check for line starting with "au" for :autocmd or
938 // "com" for :command, these can use a {} block
939 is_block = checkforcmd_noparen(&s, "autocmd", 2)
940 || checkforcmd_noparen(&s, "command", 3);
941 }
942
943 if (is_block)
944 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200945 if (nesting == MAX_FUNC_NESTING - 1)
946 emsg(_(e_function_nesting_too_deep));
947 else
948 {
949 ++nesting;
950 nesting_def[nesting] = TRUE;
951 nesting_inline[nesting] = TRUE;
952 indent += 2;
953 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100954 }
955 }
956 }
957
958 // Check for ":append", ":change", ":insert". Not for :def.
959 p = skip_range(p, FALSE, NULL);
960 if (!vim9_function
961 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
962 || (p[0] == 'c'
963 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
964 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
965 && (STRNCMP(&p[3], "nge", 3) != 0
966 || !ASCII_ISALPHA(p[6])))))))
967 || (p[0] == 'i'
968 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
969 && (!ASCII_ISALPHA(p[2])
970 || (p[2] == 's'
971 && (!ASCII_ISALPHA(p[3])
972 || p[3] == 'e'))))))))
973 skip_until = vim_strsave((char_u *)".");
974
975 // Check for ":python <<EOF", ":tcl <<EOF", etc.
976 arg = skipwhite(skiptowhite(p));
977 if (arg[0] == '<' && arg[1] =='<'
978 && ((p[0] == 'p' && p[1] == 'y'
979 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
980 || ((p[2] == '3' || p[2] == 'x')
981 && !ASCII_ISALPHA(p[3]))))
982 || (p[0] == 'p' && p[1] == 'e'
983 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
984 || (p[0] == 't' && p[1] == 'c'
985 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
986 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
987 && !ASCII_ISALPHA(p[3]))
988 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
989 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
990 || (p[0] == 'm' && p[1] == 'z'
991 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
992 ))
993 {
994 // ":python <<" continues until a dot, like ":append"
995 p = skipwhite(arg + 2);
996 if (STRNCMP(p, "trim", 4) == 0)
997 {
998 // Ignore leading white space.
999 p = skipwhite(p + 4);
1000 heredoc_trimmed = vim_strnsave(theline,
1001 skipwhite(theline) - theline);
1002 }
1003 if (*p == NUL)
1004 skip_until = vim_strsave((char_u *)".");
1005 else
1006 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1007 getline_options = GETLINE_NONE;
1008 is_heredoc = TRUE;
Bram Moolenaar20677332021-06-06 17:02:53 +02001009 if (eap->cmdidx == CMD_def)
1010 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001011 }
1012
1013 // Check for ":cmd v =<< [trim] EOF"
1014 // and ":cmd [a, b] =<< [trim] EOF"
1015 // and "lines =<< [trim] EOF" for Vim9
1016 // Where "cmd" can be "let", "var", "final" or "const".
1017 arg = skipwhite(skiptowhite(p));
1018 if (*arg == '[')
1019 arg = vim_strchr(arg, ']');
1020 if (arg != NULL)
1021 {
1022 int found = (eap->cmdidx == CMD_def && arg[0] == '='
1023 && arg[1] == '<' && arg[2] =='<');
1024
1025 if (!found)
1026 // skip over the argument after "cmd"
1027 arg = skipwhite(skiptowhite(arg));
1028 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
1029 && (checkforcmd(&p, "let", 2)
1030 || checkforcmd(&p, "var", 3)
1031 || checkforcmd(&p, "final", 5)
1032 || checkforcmd(&p, "const", 5))))
1033 {
1034 p = skipwhite(arg + 3);
1035 if (STRNCMP(p, "trim", 4) == 0)
1036 {
1037 // Ignore leading white space.
1038 p = skipwhite(p + 4);
1039 heredoc_trimmed = vim_strnsave(theline,
1040 skipwhite(theline) - theline);
1041 }
1042 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1043 getline_options = GETLINE_NONE;
1044 is_heredoc = TRUE;
1045 }
1046 }
1047 }
1048
1049 // Add the line to the function.
1050 if (ga_grow(newlines, 1 + sourcing_lnum_off) == FAIL)
1051 goto theend;
1052
Bram Moolenaar20677332021-06-06 17:02:53 +02001053 if (heredoc_concat_len > 0)
1054 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001055 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001056 // to be used for the instruction later.
1057 ga_concat(&heredoc_ga, theline);
1058 ga_concat(&heredoc_ga, (char_u *)"\n");
1059 p = vim_strsave((char_u *)"");
1060 }
1061 else
1062 {
1063 // Copy the line to newly allocated memory. get_one_sourceline()
1064 // allocates 250 bytes per line, this saves 80% on average. The
1065 // cost is an extra alloc/free.
1066 p = vim_strsave(theline);
1067 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001068 if (p == NULL)
1069 goto theend;
1070 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1071
1072 // Add NULL lines for continuation lines, so that the line count is
1073 // equal to the index in the growarray.
1074 while (sourcing_lnum_off-- > 0)
1075 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1076
1077 // Check for end of eap->arg.
1078 if (line_arg != NULL && *line_arg == NUL)
1079 line_arg = NULL;
1080 }
1081
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001082 // Return OK when no error was detected.
1083 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001084 ret = OK;
1085
1086theend:
1087 vim_free(skip_until);
1088 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001089 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001090 need_wait_return |= saved_wait_return;
1091 return ret;
1092}
1093
1094/*
1095 * Handle the body of a lambda. *arg points to the "{", process statements
1096 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001097 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001098 * When successful "rettv" is set to a funcref.
1099 */
1100 static int
1101lambda_function_body(
1102 char_u **arg,
1103 typval_T *rettv,
1104 evalarg_T *evalarg,
1105 garray_T *newargs,
1106 garray_T *argtypes,
1107 int varargs,
1108 garray_T *default_args,
1109 char_u *ret_type)
1110{
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001111 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001112 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001113 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001114 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001115 exarg_T eap;
1116 garray_T newlines;
1117 char_u *cmdline = NULL;
1118 int ret = FAIL;
1119 char_u *line_to_free = NULL;
1120 partial_T *pt;
1121 char_u *name;
1122 int lnum_save = -1;
1123 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1124
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001125 if (!ends_excmd2(*arg, skipwhite(*arg + 1)))
1126 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001127 semsg(_(e_trailing_characters_str), *arg + 1);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001128 return FAIL;
1129 }
1130
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001131 CLEAR_FIELD(eap);
1132 eap.cmdidx = CMD_block;
1133 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001134 eap.cmdlinep = &cmdline;
1135 eap.skip = !evaluate;
1136 if (evalarg->eval_cctx != NULL)
1137 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1138 else
1139 {
1140 eap.getline = evalarg->eval_getline;
1141 eap.cookie = evalarg->eval_cookie;
1142 }
1143
1144 ga_init2(&newlines, (int)sizeof(char_u *), 10);
1145 if (get_function_body(&eap, &newlines, NULL, &line_to_free) == FAIL)
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001146 {
Bram Moolenaar8c697e32021-12-28 20:18:50 +00001147 if (cmdline != line_to_free)
1148 vim_free(cmdline);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001149 goto erret;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001150 }
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001151
1152 // When inside a lambda must add the function lines to evalarg.eval_ga.
1153 evalarg->eval_break_count += newlines.ga_len;
1154 if (gap->ga_itemsize > 0)
1155 {
1156 int idx;
1157 char_u *last;
1158 size_t plen;
1159 char_u *pnl;
1160
1161 for (idx = 0; idx < newlines.ga_len; ++idx)
1162 {
1163 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1164
Bram Moolenaarecb66452021-05-18 15:09:18 +02001165 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001166 goto erret;
1167
1168 // Going to concatenate the lines after parsing. For an empty or
1169 // comment line use an empty string.
1170 // Insert NL characters at the start of each line, the string will
1171 // be split again later in .get_lambda_tv().
1172 if (*p == NUL || vim9_comment_start(p))
1173 p = (char_u *)"";
1174 plen = STRLEN(p);
1175 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1176 if (pnl != NULL)
1177 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001178 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1179 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001180 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001181 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001182 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001183 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001184 // more is following after the "}", which was skipped
1185 last = cmdline;
1186 else
1187 // nothing is following the "}"
1188 last = (char_u *)"}";
1189 plen = STRLEN(last);
1190 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1191 if (pnl != NULL)
1192 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001193 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1194 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001195 }
1196
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001197 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001198 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001199 garray_T *tfgap = &evalarg->eval_tofree_ga;
1200
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001201 // Something comes after the "}".
1202 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001203
1204 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001205 if (ga_grow(tfgap, 1) == OK)
1206 {
1207 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1208 evalarg->eval_using_cmdline = TRUE;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001209 if (cmdline == line_to_free)
1210 line_to_free = NULL;
Bram Moolenaar844fb642021-10-23 13:32:30 +01001211 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001212 }
1213 else
1214 *arg = (char_u *)"";
1215
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001216 if (!evaluate)
1217 {
1218 ret = OK;
1219 goto erret;
1220 }
1221
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001222 name = get_lambda_name();
1223 ufunc = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
1224 if (ufunc == NULL)
1225 goto erret;
1226 set_ufunc_name(ufunc, name);
1227 if (hash_add(&func_hashtab, UF2HIKEY(ufunc)) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001228 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001229 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001230 ufunc->uf_refcount = 1;
1231 ufunc->uf_args = *newargs;
1232 newargs->ga_data = NULL;
1233 ufunc->uf_def_args = *default_args;
1234 default_args->ga_data = NULL;
1235 ufunc->uf_func_type = &t_func_any;
1236
1237 // error messages are for the first function line
1238 lnum_save = SOURCING_LNUM;
1239 SOURCING_LNUM = sourcing_lnum_top;
1240
1241 // parse argument types
1242 if (parse_argument_types(ufunc, argtypes, varargs) == FAIL)
1243 {
1244 SOURCING_LNUM = lnum_save;
1245 goto erret;
1246 }
1247
1248 // parse the return type, if any
1249 if (parse_return_type(ufunc, ret_type) == FAIL)
1250 goto erret;
1251
1252 pt = ALLOC_CLEAR_ONE(partial_T);
1253 if (pt == NULL)
1254 goto erret;
1255 pt->pt_func = ufunc;
1256 pt->pt_refcount = 1;
1257
1258 ufunc->uf_lines = newlines;
1259 newlines.ga_data = NULL;
1260 if (sandbox)
1261 ufunc->uf_flags |= FC_SANDBOX;
1262 if (!ASCII_ISUPPER(*ufunc->uf_name))
1263 ufunc->uf_flags |= FC_VIM9;
1264 ufunc->uf_script_ctx = current_sctx;
1265 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1266 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1267 set_function_type(ufunc);
1268
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001269 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1270
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001271 rettv->vval.v_partial = pt;
1272 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001273 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001274 ret = OK;
1275
1276erret:
1277 if (lnum_save >= 0)
1278 SOURCING_LNUM = lnum_save;
1279 vim_free(line_to_free);
1280 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001281 if (newargs != NULL)
1282 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001283 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001284 if (ufunc != NULL)
1285 {
1286 func_clear(ufunc, TRUE);
1287 func_free(ufunc, TRUE);
1288 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001289 return ret;
1290}
1291
1292/*
1293 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001294 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001295 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001296 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1297 */
1298 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001299get_lambda_tv(
1300 char_u **arg,
1301 typval_T *rettv,
1302 int types_optional,
1303 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001304{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001305 int evaluate = evalarg != NULL
1306 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001307 garray_T newargs;
1308 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001309 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001310 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001311 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001312 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001313 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001314 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001315 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001316 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001317 char_u *s;
1318 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001319 int *old_eval_lavars = eval_lavars_used;
1320 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001321 char_u *tofree1 = NULL;
1322 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001323 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001324 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001325 int called_emsg_start = called_emsg;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001326
1327 if (equal_arrow && !in_vim9script())
1328 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001329
1330 ga_init(&newargs);
1331 ga_init(&newlines);
1332
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001333 // First, check if this is really a lambda expression. "->" or "=>" must
1334 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001335 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001336 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001337 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar14ded112021-06-26 19:25:49 +02001338 NULL, &default_args, TRUE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001339 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001340 {
1341 if (types_optional)
1342 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +01001343 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001344 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001345
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001346 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001347 if (evaluate)
1348 pnewargs = &newargs;
1349 else
1350 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001351 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001352 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001353 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001354 &varargs, &default_args,
1355 FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001356 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001357 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaare462f522020-12-27 14:43:30 +01001358 equal_arrow || in_vim9script() ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001359 {
1360 if (types_optional)
1361 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001362 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001363 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001364 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001365 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001366
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001367 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1368 if (ret_type != NULL)
1369 {
1370 ret_type = vim_strsave(ret_type);
1371 tofree2 = ret_type;
1372 }
1373
Bram Moolenaare38eab22019-12-05 21:50:01 +01001374 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001375 if (evaluate)
1376 eval_lavars_used = &eval_lavars;
1377
Bram Moolenaar65c44152020-12-24 15:14:01 +01001378 *arg = skipwhite_and_linebreak(*arg, evalarg);
1379
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001380 // Recognize "{" as the start of a function body.
1381 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001382 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001383 if (evalarg == NULL)
1384 // cannot happen?
1385 goto theend;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001386 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1387 types_optional ? &argtypes : NULL, varargs,
1388 &default_args, ret_type) == FAIL)
1389 goto errret;
1390 goto theend;
1391 }
1392 if (default_args.ga_len > 0)
1393 {
1394 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001395 goto errret;
1396 }
1397
Bram Moolenaare38eab22019-12-05 21:50:01 +01001398 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001399 start = *arg;
1400 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001401 if (ret == FAIL)
1402 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001403 if (evalarg != NULL)
1404 {
1405 // avoid that the expression gets freed when another line break follows
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001406 tofree1 = evalarg->eval_tofree;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001407 evalarg->eval_tofree = NULL;
1408 }
1409
Bram Moolenaar65c44152020-12-24 15:14:01 +01001410 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001411 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001412 *arg = skipwhite_and_linebreak(*arg, evalarg);
1413 if (**arg != '}')
1414 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001415 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001416 goto errret;
1417 }
1418 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001419 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001420
1421 if (evaluate)
1422 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001423 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001424 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001425 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001426 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001427 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001428
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001429 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001430 if (fp == NULL)
1431 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001432 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001433 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001434 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001435 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001436
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001437 ga_init2(&newlines, (int)sizeof(char_u *), 1);
1438 if (ga_grow(&newlines, 1) == FAIL)
1439 goto errret;
1440
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001441 // If there are line breaks, we need to split up the string.
1442 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001443 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001444 line_end = end;
1445
1446 // Add "return " before the expression (or the first line).
1447 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001448 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001449 if (p == NULL)
1450 goto errret;
1451 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1452 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001453 vim_strncpy(p + 7, start, line_end - start);
1454
1455 if (line_end != end)
1456 {
1457 // Add more lines, split by line breaks. Thus is used when a
1458 // lambda with { cmds } is encountered.
1459 while (*line_end == '\n')
1460 {
1461 if (ga_grow(&newlines, 1) == FAIL)
1462 goto errret;
1463 start = line_end + 1;
1464 line_end = vim_strchr(start, '\n');
1465 if (line_end == NULL)
1466 line_end = end;
1467 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1468 vim_strnsave(start, line_end - start);
1469 }
1470 }
1471
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001472 if (strstr((char *)p + 7, "a:") == NULL)
1473 // No a: variables are used for sure.
1474 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001475
1476 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001477 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001478 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001479 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001480 if (types_optional)
1481 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001482 if (parse_argument_types(fp, &argtypes,
1483 in_vim9script() && varargs) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001484 goto errret;
1485 if (ret_type != NULL)
1486 {
1487 fp->uf_ret_type = parse_type(&ret_type,
1488 &fp->uf_type_list, TRUE);
1489 if (fp->uf_ret_type == NULL)
1490 goto errret;
1491 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001492 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001493 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001494 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001495
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001496 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001497 if (current_funccal != NULL && eval_lavars)
1498 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001499 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001500 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001501 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001502 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001503
1504#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001505 if (prof_def_func())
1506 func_do_profile(fp);
1507#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001508 if (sandbox)
1509 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001510 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001511 // uf_args.ga_len. In Vim9 script "...name" has to be used.
1512 fp->uf_varargs = !in_vim9script() || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001513 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001514 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001515 fp->uf_script_ctx = current_sctx;
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001516 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len + 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001517
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001518 function_using_block_scopes(fp, evalarg->eval_cstack);
1519
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001520 pt->pt_func = fp;
1521 pt->pt_refcount = 1;
1522 rettv->vval.v_partial = pt;
1523 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001524
1525 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001526 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001527
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001528theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001529 eval_lavars_used = old_eval_lavars;
Bram Moolenaarefaaaa62020-07-08 22:24:09 +02001530 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001531 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001532 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001533 vim_free(tofree1);
1534 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001535 if (types_optional)
1536 ga_clear_strings(&argtypes);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001537
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001538 return OK;
1539
1540errret:
1541 ga_clear_strings(&newargs);
1542 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001543 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001544 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001545 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001546 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001547 if (fp != NULL)
1548 vim_free(fp->uf_arg_types);
1549 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001550 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001551 vim_free(pt);
Bram Moolenaarefaaaa62020-07-08 22:24:09 +02001552 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001553 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001554 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001555 vim_free(tofree1);
1556 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001557 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001558 return FAIL;
1559}
1560
1561/*
1562 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1563 * name it contains, otherwise return "name".
1564 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1565 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001566 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1567 * type of the variable.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001568 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001569 */
1570 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001571deref_func_name(
1572 char_u *name,
1573 int *lenp,
1574 partial_T **partialp,
1575 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001576 int no_autoload,
1577 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001578{
1579 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001580 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001581 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001582 char_u *s = NULL;
1583 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001584 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001585
1586 if (partialp != NULL)
1587 *partialp = NULL;
1588
1589 cc = name[*lenp];
1590 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001591
Bram Moolenaar71f21932022-01-07 18:20:55 +00001592 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001593 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001594 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001595 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001596 tv = &v->di_tv;
1597 }
1598 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1599 {
1600 imported_T *import;
1601 char_u *p = name;
1602 int len = *lenp;
1603
1604 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001605 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001606 p = name + 2;
1607 len -= 2;
1608 }
1609 import = find_imported(p, len, NULL);
1610
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001611 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001612 if (import != NULL)
1613 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001614 name[len] = NUL;
1615 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
1616 name[len] = cc;
1617 *lenp = 0;
1618 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001619 }
1620 }
1621
1622 if (tv != NULL)
1623 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001624 if (found_var != NULL)
1625 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001626 if (tv->v_type == VAR_FUNC)
1627 {
1628 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001629 {
1630 *lenp = 0;
1631 return (char_u *)""; // just in case
1632 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001633 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001634 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001635 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001636
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001637 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001638 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001639 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001640
1641 if (pt == NULL)
1642 {
1643 *lenp = 0;
1644 return (char_u *)""; // just in case
1645 }
1646 if (partialp != NULL)
1647 *partialp = pt;
1648 s = partial_name(pt);
1649 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001650 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001651
1652 if (s != NULL)
1653 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001654 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001655 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001656 svar_T *sv = find_typval_in_script(tv, 0);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001657
1658 if (sv != NULL)
1659 *type = sv->sv_type;
1660 }
1661 return s;
1662 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001663 }
1664
1665 return name;
1666}
1667
1668/*
1669 * Give an error message with a function name. Handle <SNR> things.
1670 * "ermsg" is to be passed without translation, use N_() instead of _().
1671 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001672 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001673emsg_funcname(char *ermsg, char_u *name)
1674{
1675 char_u *p;
1676
1677 if (*name == K_SPECIAL)
1678 p = concat_str((char_u *)"<SNR>", name + 3);
1679 else
1680 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001681 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001682 if (p != name)
1683 vim_free(p);
1684}
1685
1686/*
1687 * Allocate a variable for the result of a function.
1688 * Return OK or FAIL.
1689 */
1690 int
1691get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001692 char_u *name, // name of the function
1693 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001694 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001695 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +02001696 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001697 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001698{
1699 char_u *argp;
1700 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001701 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1702 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001703 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001704
1705 /*
1706 * Get the arguments.
1707 */
1708 argp = *arg;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001709 while (argcount < MAX_FUNC_ARGS - (funcexe->fe_partial == NULL ? 0
1710 : funcexe->fe_partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001711 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001712 // skip the '(' or ',' and possibly line breaks
1713 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1714
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001715 if (*argp == ')' || *argp == ',' || *argp == NUL)
1716 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001717 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001718 {
1719 ret = FAIL;
1720 break;
1721 }
1722 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001723 // The comma should come right after the argument, but this wasn't
1724 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001725 if (vim9script)
1726 {
1727 if (*argp != ',' && *skipwhite(argp) == ',')
1728 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001729 semsg(_(e_no_white_space_allowed_before_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001730 ret = FAIL;
1731 break;
1732 }
1733 }
1734 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001735 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001736 if (*argp != ',')
1737 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001738 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1739 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001740 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001741 ret = FAIL;
1742 break;
1743 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001744 }
Bram Moolenaare6b53242020-07-01 17:28:33 +02001745 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001746 if (*argp == ')')
1747 ++argp;
1748 else
1749 ret = FAIL;
1750
1751 if (ret == OK)
1752 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001753 int i = 0;
1754 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001755
1756 if (get_vim_var_nr(VV_TESTING))
1757 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001758 // Prepare for calling test_garbagecollect_now(), need to know
1759 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001760 if (funcargs.ga_itemsize == 0)
1761 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
1762 for (i = 0; i < argcount; ++i)
1763 if (ga_grow(&funcargs, 1) == OK)
1764 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1765 &argvars[i];
1766 }
1767
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001768 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001769 if (in_vim9script() && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001770 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001771 // An error in a builtin function does not return FAIL, but we do
1772 // want to abort further processing if an error was given.
1773 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001774 clear_tv(rettv);
1775 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001776
1777 funcargs.ga_len -= i;
1778 }
1779 else if (!aborting())
1780 {
1781 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001782 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001783 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001784 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001785 }
1786
1787 while (--argcount >= 0)
1788 clear_tv(&argvars[argcount]);
1789
Bram Moolenaar8294d492020-08-10 22:40:56 +02001790 if (in_vim9script())
1791 *arg = argp;
1792 else
1793 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001794 return ret;
1795}
1796
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001797/*
1798 * Return TRUE if "p" starts with "<SID>" or "s:".
1799 * Only works if eval_fname_script() returned non-zero for "p"!
1800 */
1801 static int
1802eval_fname_sid(char_u *p)
1803{
1804 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1805}
1806
1807/*
1808 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1809 * Change <SNR>123_name() to K_SNR 123_name().
1810 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
1811 * (slow).
1812 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001813 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001814fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
1815{
1816 int llen;
1817 char_u *fname;
1818 int i;
1819
1820 llen = eval_fname_script(name);
1821 if (llen > 0)
1822 {
1823 fname_buf[0] = K_SPECIAL;
1824 fname_buf[1] = KS_EXTRA;
1825 fname_buf[2] = (int)KE_SNR;
1826 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001827 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001828 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001829 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001830 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001831 else
1832 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001833 sprintf((char *)fname_buf + 3, "%ld_",
1834 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001835 i = (int)STRLEN(fname_buf);
1836 }
1837 }
1838 if (i + STRLEN(name + llen) < FLEN_FIXED)
1839 {
1840 STRCPY(fname_buf + i, name + llen);
1841 fname = fname_buf;
1842 }
1843 else
1844 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001845 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001846 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +01001847 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001848 else
1849 {
1850 *tofree = fname;
1851 mch_memmove(fname, fname_buf, (size_t)i);
1852 STRCPY(fname + i, name + llen);
1853 }
1854 }
1855 }
1856 else
1857 fname = name;
1858 return fname;
1859}
1860
1861/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 * Find a function "name" in script "sid".
1863 */
1864 static ufunc_T *
1865find_func_with_sid(char_u *name, int sid)
1866{
1867 hashitem_T *hi;
1868 char_u buffer[200];
1869
1870 buffer[0] = K_SPECIAL;
1871 buffer[1] = KS_EXTRA;
1872 buffer[2] = (int)KE_SNR;
1873 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
1874 (long)sid, name);
1875 hi = hash_find(&func_hashtab, buffer);
1876 if (!HASHITEM_EMPTY(hi))
1877 return HI2UF(hi);
1878
1879 return NULL;
1880}
1881
1882/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001883 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001884 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001885 * Return NULL for unknown function.
1886 */
Bram Moolenaarce658352020-07-31 23:47:12 +02001887 ufunc_T *
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001888find_func_even_dead(char_u *name, int is_global, cctx_T *cctx UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001889{
1890 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001893 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001895 int find_script_local = in_vim9script() && eval_isnamec1(*name)
1896 && (name[1] != ':' || *name == 's');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001897
Bram Moolenaar95006e32020-08-29 17:47:08 +02001898 if (find_script_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001900 // Find script-local function before global one.
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001901 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
1902 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001903 if (func != NULL)
1904 return func;
1905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001907
Bram Moolenaara26b9702020-04-18 19:53:28 +02001908 hi = hash_find(&func_hashtab,
1909 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001910 if (!HASHITEM_EMPTY(hi))
1911 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912
1913 return NULL;
1914}
1915
1916/*
1917 * Find a function by name, return pointer to it in ufuncs.
1918 * "cctx" is passed in a :def function to find imported functions.
1919 * Return NULL for unknown or dead function.
1920 */
1921 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001922find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001924 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925
1926 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
1927 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001928 return NULL;
1929}
1930
1931/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02001932 * Return TRUE if "ufunc" is a global function.
1933 */
1934 int
1935func_is_global(ufunc_T *ufunc)
1936{
1937 return ufunc->uf_name[0] != K_SPECIAL;
1938}
1939
1940/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001941 * Copy the function name of "fp" to buffer "buf".
1942 * "buf" must be able to hold the function name plus three bytes.
1943 * Takes care of script-local function names.
1944 */
1945 static void
1946cat_func_name(char_u *buf, ufunc_T *fp)
1947{
Bram Moolenaar0f769812020-09-12 18:32:34 +02001948 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001949 {
1950 STRCPY(buf, "<SNR>");
1951 STRCAT(buf, fp->uf_name + 3);
1952 }
1953 else
1954 STRCPY(buf, fp->uf_name);
1955}
1956
1957/*
1958 * Add a number variable "name" to dict "dp" with value "nr".
1959 */
1960 static void
1961add_nr_var(
1962 dict_T *dp,
1963 dictitem_T *v,
1964 char *name,
1965 varnumber_T nr)
1966{
1967 STRCPY(v->di_key, name);
1968 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1969 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
1970 v->di_tv.v_type = VAR_NUMBER;
1971 v->di_tv.v_lock = VAR_FIXED;
1972 v->di_tv.vval.v_number = nr;
1973}
1974
1975/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001976 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001977 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001978 static void
1979free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001980{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001981 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001982
1983 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1984 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001985 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001986
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001987 // When garbage collecting a funccall_T may be freed before the
1988 // function that references it, clear its uf_scoped field.
1989 // The function may have been redefined and point to another
1990 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001991 if (fp != NULL && fp->uf_scoped == fc)
1992 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001993 }
Bram Moolenaar58016442016-07-31 18:30:22 +02001994 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001995
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001996 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001997 vim_free(fc);
1998}
1999
2000/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002001 * Free "fc" and what it contains.
2002 * Can be called only when "fc" is kept beyond the period of it called,
2003 * i.e. after cleanup_function_call(fc).
2004 */
2005 static void
2006free_funccal_contents(funccall_T *fc)
2007{
2008 listitem_T *li;
2009
2010 // Free all l: variables.
2011 vars_clear(&fc->l_vars.dv_hashtab);
2012
2013 // Free all a: variables.
2014 vars_clear(&fc->l_avars.dv_hashtab);
2015
2016 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002017 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002018 clear_tv(&li->li_tv);
2019
2020 free_funccal(fc);
2021}
2022
2023/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002024 * Handle the last part of returning from a function: free the local hashtable.
2025 * Unless it is still in use by a closure.
2026 */
2027 static void
2028cleanup_function_call(funccall_T *fc)
2029{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002030 int may_free_fc = fc->fc_refcount <= 0;
2031 int free_fc = TRUE;
2032
Bram Moolenaar6914c642017-04-01 21:21:30 +02002033 current_funccal = fc->caller;
2034
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002035 // Free all l: variables if not referred.
2036 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
2037 vars_clear(&fc->l_vars.dv_hashtab);
2038 else
2039 free_fc = FALSE;
2040
2041 // If the a:000 list and the l: and a: dicts are not referenced and
2042 // there is no closure using it, we can free the funccall_T and what's
2043 // in it.
2044 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
2045 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002046 else
2047 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002048 int todo;
2049 hashitem_T *hi;
2050 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002051
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002052 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002053
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002054 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002055 todo = (int)fc->l_avars.dv_hashtab.ht_used;
2056 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
2057 {
2058 if (!HASHITEM_EMPTY(hi))
2059 {
2060 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002061 di = HI2DI(hi);
2062 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002063 }
2064 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002065 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002066
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002067 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2068 fc->l_varlist.lv_first = NULL;
2069 else
2070 {
2071 listitem_T *li;
2072
2073 free_fc = FALSE;
2074
2075 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002076 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002077 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002078 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002079
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002080 if (free_fc)
2081 free_funccal(fc);
2082 else
2083 {
2084 static int made_copy = 0;
2085
2086 // "fc" is still in use. This can happen when returning "a:000",
2087 // assigning "l:" to a global variable or defining a closure.
2088 // Link "fc" in the list for garbage collection later.
2089 fc->caller = previous_funccal;
2090 previous_funccal = fc;
2091
2092 if (want_garbage_collect)
2093 // If garbage collector is ready, clear count.
2094 made_copy = 0;
2095 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002096 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002097 // We have made a lot of copies, worth 4 Mbyte. This can happen
2098 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002099 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002100 // too much memory.
2101 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002102 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002103 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002104 }
2105}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002106
2107/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002108 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2109 */
2110 static int
2111numbered_function(char_u *name)
2112{
2113 return isdigit(*name)
2114 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2115}
2116
2117/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002118 * There are two kinds of function names:
2119 * 1. ordinary names, function defined with :function or :def
2120 * 2. numbered functions and lambdas
2121 * For the first we only count the name stored in func_hashtab as a reference,
2122 * using function() does not count as a reference, because the function is
2123 * looked up by name.
2124 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002125 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002126func_name_refcount(char_u *name)
2127{
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002128 return numbered_function(name) || *name == '<';
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002129}
2130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131/*
2132 * Unreference "fc": decrement the reference count and free it when it
2133 * becomes zero. "fp" is detached from "fc".
2134 * When "force" is TRUE we are exiting.
2135 */
2136 static void
2137funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2138{
2139 funccall_T **pfc;
2140 int i;
2141
2142 if (fc == NULL)
2143 return;
2144
2145 if (--fc->fc_refcount <= 0 && (force || (
2146 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
2147 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
2148 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2149 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
2150 {
2151 if (fc == *pfc)
2152 {
2153 *pfc = fc->caller;
2154 free_funccal_contents(fc);
2155 return;
2156 }
2157 }
2158 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2159 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
2160 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
2161}
2162
2163/*
2164 * Remove the function from the function hashtable. If the function was
2165 * deleted while it still has references this was already done.
2166 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2167 */
2168 static int
2169func_remove(ufunc_T *fp)
2170{
2171 hashitem_T *hi;
2172
2173 // Return if it was already virtually deleted.
2174 if (fp->uf_flags & FC_DEAD)
2175 return FALSE;
2176
2177 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
2178 if (!HASHITEM_EMPTY(hi))
2179 {
2180 // When there is a def-function index do not actually remove the
2181 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002182 // Do remove it when it's a copy.
2183 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaarc970e422021-03-17 15:03:04 +01002184 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002186 return FALSE;
2187 }
2188 hash_remove(&func_hashtab, hi);
2189 fp->uf_flags |= FC_DELETED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 return TRUE;
2191 }
2192 return FALSE;
2193}
2194
2195 static void
2196func_clear_items(ufunc_T *fp)
2197{
2198 ga_clear_strings(&(fp->uf_args));
2199 ga_clear_strings(&(fp->uf_def_args));
2200 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002202 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002203 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002204 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002205
2206 // Increment the refcount of this function to avoid it being freed
2207 // recursively when the partial is freed.
2208 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002209 partial_unref(fp->uf_partial);
2210 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002211 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002212
2213#ifdef FEAT_LUA
2214 if (fp->uf_cb_free != NULL)
2215 {
2216 fp->uf_cb_free(fp->uf_cb_state);
2217 fp->uf_cb_free = NULL;
2218 }
2219
2220 fp->uf_cb_state = NULL;
2221 fp->uf_cb = NULL;
2222#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223#ifdef FEAT_PROFILE
2224 VIM_CLEAR(fp->uf_tml_count);
2225 VIM_CLEAR(fp->uf_tml_total);
2226 VIM_CLEAR(fp->uf_tml_self);
2227#endif
2228}
2229
2230/*
2231 * Free all things that a function contains. Does not free the function
2232 * itself, use func_free() for that.
2233 * When "force" is TRUE we are exiting.
2234 */
2235 static void
2236func_clear(ufunc_T *fp, int force)
2237{
2238 if (fp->uf_cleared)
2239 return;
2240 fp->uf_cleared = TRUE;
2241
2242 // clear this function
2243 func_clear_items(fp);
2244 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002245 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246}
2247
2248/*
2249 * Free a function and remove it from the list of functions. Does not free
2250 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002251 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002252 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002254 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002255func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256{
2257 // Only remove it when not done already, otherwise we would remove a newer
2258 // version of the function with the same name.
2259 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2260 func_remove(fp);
2261
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002262 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002263 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002264 if (fp->uf_dfunc_idx > 0)
2265 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002266 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002268 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002269 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002270 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271}
2272
2273/*
2274 * Free all things that a function contains and free the function itself.
2275 * When "force" is TRUE we are exiting.
2276 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002277 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278func_clear_free(ufunc_T *fp, int force)
2279{
2280 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002281 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2282 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002283 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002284 else
2285 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286}
2287
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002288/*
2289 * Copy already defined function "lambda" to a new function with name "global".
2290 * This is for when a compiled function defines a global function.
2291 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002292 int
2293copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002294{
2295 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002296 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002297
2298 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002299 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002300 semsg(_(e_lambda_function_not_found_str), lambda);
2301 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002302 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002303
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002304 fp = find_func(global, TRUE, NULL);
2305 if (fp != NULL)
2306 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002307 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002308 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002309 return FAIL;
2310 }
2311
2312 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
2313 if (fp == NULL)
2314 return FAIL;
2315
2316 fp->uf_varargs = ufunc->uf_varargs;
2317 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2318 fp->uf_def_status = ufunc->uf_def_status;
2319 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2320 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2321 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2322 == FAIL
2323 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2324 goto failed;
2325
2326 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
2327 : vim_strsave(ufunc->uf_name_exp);
2328 if (ufunc->uf_arg_types != NULL)
2329 {
2330 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2331 if (fp->uf_arg_types == NULL)
2332 goto failed;
2333 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2334 sizeof(type_T *) * fp->uf_args.ga_len);
2335 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002336 if (ufunc->uf_va_name != NULL)
2337 {
2338 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2339 if (fp->uf_va_name == NULL)
2340 goto failed;
2341 }
2342 fp->uf_ret_type = ufunc->uf_ret_type;
2343
2344 fp->uf_refcount = 1;
2345 STRCPY(fp->uf_name, global);
2346 hash_add(&func_hashtab, UF2HIKEY(fp));
2347
2348 // the referenced dfunc_T is now used one more time
2349 link_def_function(fp);
2350
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002351 // Create a partial to store the context of the function where it was
2352 // instantiated. Only needs to be done once. Do this on the original
2353 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002354 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2355 {
2356 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2357
2358 if (pt == NULL)
2359 goto failed;
2360 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002361 {
2362 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002363 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002364 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002365 ufunc->uf_partial = pt;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002366 --pt->pt_refcount; // not actually referenced here
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002367 }
2368
2369 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002370
2371failed:
2372 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002373 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002374}
2375
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002376static int funcdepth = 0;
2377
2378/*
2379 * Increment the function call depth count.
2380 * Return FAIL when going over 'maxfuncdepth'.
2381 * Otherwise return OK, must call funcdepth_decrement() later!
2382 */
2383 int
2384funcdepth_increment(void)
2385{
2386 if (funcdepth >= p_mfd)
2387 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002388 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002389 return FAIL;
2390 }
2391 ++funcdepth;
2392 return OK;
2393}
2394
2395 void
2396funcdepth_decrement(void)
2397{
2398 --funcdepth;
2399}
2400
2401/*
2402 * Get the current function call depth.
2403 */
2404 int
2405funcdepth_get(void)
2406{
2407 return funcdepth;
2408}
2409
2410/*
2411 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002412 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002413 * times as funcdepth_increment().
2414 */
2415 void
2416funcdepth_restore(int depth)
2417{
2418 funcdepth = depth;
2419}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002420
2421/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002422 * Call a user function.
2423 */
2424 static void
2425call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002426 ufunc_T *fp, // pointer to function
2427 int argcount, // nr of args
2428 typval_T *argvars, // arguments
2429 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002430 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002431 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002432{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002433 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002434 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002435 funccall_T *fc;
2436 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002437 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002438 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002439 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002440 int i;
2441 int ai;
2442 int islambda = FALSE;
2443 char_u numbuf[NUMBUFLEN];
2444 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002445 typval_T *tv_to_free[MAX_FUNC_ARGS];
2446 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002447#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002448 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002449#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002450 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002451
Bram Moolenaarb2049902021-01-24 12:53:53 +01002452#ifdef FEAT_PROFILE
2453 CLEAR_FIELD(profile_info);
2454#endif
2455
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002456 // If depth of calling is getting too high, don't execute the function.
2457 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002458 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002459 rettv->v_type = VAR_NUMBER;
2460 rettv->vval.v_number = -1;
2461 return;
2462 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002463
Bram Moolenaare38eab22019-12-05 21:50:01 +01002464 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002465
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002466 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002467 if (fc == NULL)
2468 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002469 fc->caller = current_funccal;
2470 current_funccal = fc;
2471 fc->func = fp;
2472 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002473 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002474 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002475 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2476 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002477 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002478 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002479 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002480
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002481 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002483#ifdef FEAT_PROFILE
2484 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2485#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002486 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002487#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002488 if (do_profiling == PROF_YES)
2489 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002490#endif
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002491 call_def_function(fp, argcount, argvars, funcexe->fe_partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002492 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002493#ifdef FEAT_PROFILE
2494 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002495 || (caller != NULL && caller->uf_profiling)))
2496 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002497#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 free_funccal(fc);
2500 return;
2501 }
2502
Bram Moolenaar38453522021-11-28 22:00:12 +00002503 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002504
2505 /*
2506 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
2507 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
2508 * each argument variable and saves a lot of time.
2509 */
2510 /*
2511 * Init l: variables.
2512 */
2513 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
2514 if (selfdict != NULL)
2515 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002516 // Set l:self to "selfdict". Use "name" to avoid a warning from
2517 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002518 v = &fc->fixvar[fixvar_idx++].var;
2519 name = v->di_key;
2520 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002521 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002522 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
2523 v->di_tv.v_type = VAR_DICT;
2524 v->di_tv.v_lock = 0;
2525 v->di_tv.vval.v_dict = selfdict;
2526 ++selfdict->dv_refcount;
2527 }
2528
2529 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002530 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002531 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002532 * Set a:000 to a list with room for the "..." arguments.
2533 */
2534 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002535 if ((fp->uf_flags & FC_NOARGS) == 0)
2536 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002537 (varnumber_T)(argcount >= fp->uf_args.ga_len
2538 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01002539 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002540 if ((fp->uf_flags & FC_NOARGS) == 0)
2541 {
2542 // Use "name" to avoid a warning from some compiler that checks the
2543 // destination size.
2544 v = &fc->fixvar[fixvar_idx++].var;
2545 name = v->di_key;
2546 STRCPY(name, "000");
2547 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2548 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
2549 v->di_tv.v_type = VAR_LIST;
2550 v->di_tv.v_lock = VAR_FIXED;
2551 v->di_tv.vval.v_list = &fc->l_varlist;
2552 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02002553 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002554 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2555 fc->l_varlist.lv_lock = VAR_FIXED;
2556
2557 /*
2558 * Set a:firstline to "firstline" and a:lastline to "lastline".
2559 * Set a:name to named arguments.
2560 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002561 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002562 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002563 if ((fp->uf_flags & FC_NOARGS) == 0)
2564 {
2565 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002566 (varnumber_T)funcexe->fe_firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002567 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002568 (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002569 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002570 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002571 {
2572 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002573 typval_T def_rettv;
2574 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002575
2576 ai = i - fp->uf_args.ga_len;
2577 if (ai < 0)
2578 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002579 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002580 name = FUNCARG(fp, i);
2581 if (islambda)
2582 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002583
2584 // evaluate named argument default expression
2585 isdefault = ai + fp->uf_def_args.ga_len >= 0
2586 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2587 && argvars[i].vval.v_number == VVAL_NONE));
2588 if (isdefault)
2589 {
2590 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002591
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002592 def_rettv.v_type = VAR_NUMBER;
2593 def_rettv.vval.v_number = -1;
2594
2595 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2596 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002597 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002598 {
2599 default_arg_err = 1;
2600 break;
2601 }
2602 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002603 }
2604 else
2605 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002606 if ((fp->uf_flags & FC_NOARGS) != 0)
2607 // Bail out if no a: arguments used (in lambda).
2608 break;
2609
Bram Moolenaare38eab22019-12-05 21:50:01 +01002610 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002611 sprintf((char *)numbuf, "%d", ai + 1);
2612 name = numbuf;
2613 }
2614 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2615 {
2616 v = &fc->fixvar[fixvar_idx++].var;
2617 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002618 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002619 }
2620 else
2621 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002622 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002623 if (v == NULL)
2624 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002625 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002626 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002627
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002628 // Note: the values are copied directly to avoid alloc/free.
2629 // "argvars" must have VAR_FIXED for v_lock.
2630 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002631 v->di_tv.v_lock = VAR_FIXED;
2632
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002633 if (isdefault)
2634 // Need to free this later, no matter where it's stored.
2635 tv_to_free[tv_to_free_len++] = &v->di_tv;
2636
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002637 if (addlocal)
2638 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002639 // Named arguments should be accessed without the "a:" prefix in
2640 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002641 copy_tv(&v->di_tv, &v->di_tv);
2642 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002643 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002644 else
2645 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002646
2647 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2648 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002649 listitem_T *li = &fc->l_listitems[ai];
2650
2651 li->li_tv = argvars[i];
2652 li->li_tv.v_lock = VAR_FIXED;
2653 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002654 }
2655 }
2656
Bram Moolenaare38eab22019-12-05 21:50:01 +01002657 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002658 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002659
2660 if (fp->uf_flags & FC_SANDBOX)
2661 {
2662 using_sandbox = TRUE;
2663 ++sandbox;
2664 }
2665
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002666 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002667 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002668 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002669 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002670 ++no_wait_return;
2671 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002672
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002673 smsg(_("calling %s"), SOURCING_NAME);
2674 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002675 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002676 char_u buf[MSG_BUF_LEN];
2677 char_u numbuf2[NUMBUFLEN];
2678 char_u *tofree;
2679 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002680
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002681 msg_puts("(");
2682 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002683 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002684 if (i > 0)
2685 msg_puts(", ");
2686 if (argvars[i].v_type == VAR_NUMBER)
2687 msg_outnum((long)argvars[i].vval.v_number);
2688 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002689 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002690 // Do not want errors such as E724 here.
2691 ++emsg_off;
2692 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2693 --emsg_off;
2694 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002695 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002696 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002697 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002698 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2699 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002700 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002701 msg_puts((char *)s);
2702 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002703 }
2704 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002705 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002706 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002707 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002708 msg_puts("\n"); // don't overwrite this either
2709
2710 verbose_leave_scroll();
2711 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002712 }
2713#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002714 if (do_profiling == PROF_YES)
2715 profile_may_start_func(&profile_info, fp,
2716 fc->caller == NULL ? NULL : fc->caller->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002717#endif
2718
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002719 save_current_sctx = current_sctx;
2720 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002721 save_did_emsg = did_emsg;
2722 did_emsg = FALSE;
2723
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002724 if (default_arg_err && (fp->uf_flags & FC_ABORT))
2725 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002726 else if (islambda)
2727 {
2728 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
2729
2730 // A Lambda always has the command "return {expr}". It is much faster
2731 // to evaluate {expr} directly.
2732 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002733 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002734 --ex_nesting_level;
2735 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002736 else
2737 // call do_cmdline() to execute the lines
2738 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002739 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
2740
2741 --RedrawingDisabled;
2742
Bram Moolenaare38eab22019-12-05 21:50:01 +01002743 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002744 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
2745 {
2746 clear_tv(rettv);
2747 rettv->v_type = VAR_NUMBER;
2748 rettv->vval.v_number = -1;
2749 }
2750
2751#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002752 if (do_profiling == PROF_YES)
2753 {
2754 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2755
2756 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
2757 profile_may_end_func(&profile_info, fp, caller);
2758 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002759#endif
2760
Bram Moolenaare38eab22019-12-05 21:50:01 +01002761 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002762 if (p_verbose >= 12)
2763 {
2764 ++no_wait_return;
2765 verbose_enter_scroll();
2766
2767 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002768 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002769 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002770 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002771 (long)fc->rettv->vval.v_number);
2772 else
2773 {
2774 char_u buf[MSG_BUF_LEN];
2775 char_u numbuf2[NUMBUFLEN];
2776 char_u *tofree;
2777 char_u *s;
2778
Bram Moolenaare38eab22019-12-05 21:50:01 +01002779 // The value may be very long. Skip the middle part, so that we
2780 // have some idea how it starts and ends. smsg() would always
2781 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002782 ++emsg_off;
2783 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
2784 --emsg_off;
2785 if (s != NULL)
2786 {
2787 if (vim_strsize(s) > MSG_BUF_CLEN)
2788 {
2789 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2790 s = buf;
2791 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002792 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002793 vim_free(tofree);
2794 }
2795 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002796 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002797
2798 verbose_leave_scroll();
2799 --no_wait_return;
2800 }
2801
Bram Moolenaare31ee862020-01-07 20:59:34 +01002802 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002803 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002804 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002805#ifdef FEAT_PROFILE
2806 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002807 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002808#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02002809 if (using_sandbox)
2810 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002811
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002812 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002813 {
2814 ++no_wait_return;
2815 verbose_enter_scroll();
2816
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002817 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002818 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002819
2820 verbose_leave_scroll();
2821 --no_wait_return;
2822 }
2823
2824 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002825 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002826 for (i = 0; i < tv_to_free_len; ++i)
2827 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002828 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002829}
2830
2831/*
Bram Moolenaar50824712020-12-20 21:10:17 +01002832 * Check the argument count for user function "fp".
2833 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
2834 */
2835 int
2836check_user_func_argcount(ufunc_T *fp, int argcount)
2837{
2838 int regular_args = fp->uf_args.ga_len;
2839
2840 if (argcount < regular_args - fp->uf_def_args.ga_len)
2841 return FCERR_TOOFEW;
2842 else if (!has_varargs(fp) && argcount > regular_args)
2843 return FCERR_TOOMANY;
2844 return FCERR_UNKNOWN;
2845}
2846
2847/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002849 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 int
2851call_user_func_check(
2852 ufunc_T *fp,
2853 int argcount,
2854 typval_T *argvars,
2855 typval_T *rettv,
2856 funcexe_T *funcexe,
2857 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002858{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002860
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002861 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
2862 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01002863 error = check_user_func_argcount(fp, argcount);
2864 if (error != FCERR_UNKNOWN)
2865 return error;
2866 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 error = FCERR_DICT;
2868 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002869 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 int did_save_redo = FALSE;
2871 save_redo_T save_redo;
2872
2873 /*
2874 * Call the user function.
2875 * Save and restore search patterns, script variables and
2876 * redo buffer.
2877 */
2878 save_search_patterns();
2879 if (!ins_compl_active())
2880 {
2881 saveRedobuff(&save_redo);
2882 did_save_redo = TRUE;
2883 }
2884 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002885 call_user_func(fp, argcount, argvars, rettv, funcexe,
2886 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
2888 // Function was unreferenced while being used, free it now.
2889 func_clear_free(fp, FALSE);
2890 if (did_save_redo)
2891 restoreRedobuff(&save_redo);
2892 restore_search_patterns();
2893 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002896}
2897
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002898static funccal_entry_T *funccal_stack = NULL;
2899
2900/*
2901 * Save the current function call pointer, and set it to NULL.
2902 * Used when executing autocommands and for ":source".
2903 */
2904 void
2905save_funccal(funccal_entry_T *entry)
2906{
2907 entry->top_funccal = current_funccal;
2908 entry->next = funccal_stack;
2909 funccal_stack = entry;
2910 current_funccal = NULL;
2911}
2912
2913 void
2914restore_funccal(void)
2915{
2916 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002917 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002918 else
2919 {
2920 current_funccal = funccal_stack->top_funccal;
2921 funccal_stack = funccal_stack->next;
2922 }
2923}
2924
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02002925 funccall_T *
2926get_current_funccal(void)
2927{
2928 return current_funccal;
2929}
2930
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002931/*
2932 * Mark all functions of script "sid" as deleted.
2933 */
2934 void
2935delete_script_functions(int sid)
2936{
2937 hashitem_T *hi;
2938 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02002939 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002940 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002941 size_t len;
2942
2943 buf[0] = K_SPECIAL;
2944 buf[1] = KS_EXTRA;
2945 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002946 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002947 len = STRLEN(buf);
2948
Bram Moolenaarce658352020-07-31 23:47:12 +02002949 while (todo > 0)
2950 {
2951 todo = func_hashtab.ht_used;
2952 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2953 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002954 {
Bram Moolenaarce658352020-07-31 23:47:12 +02002955 fp = HI2UF(hi);
2956 if (STRNCMP(fp->uf_name, buf, len) == 0)
2957 {
2958 int changed = func_hashtab.ht_changed;
2959
2960 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002961
2962 if (fp->uf_calls > 0)
2963 {
2964 // Function is executing, don't free it but do remove
2965 // it from the hashtable.
2966 if (func_remove(fp))
2967 fp->uf_refcount--;
2968 }
2969 else
2970 {
2971 func_clear(fp, TRUE);
2972 // When clearing a function another function can be
2973 // cleared as a side effect. When that happens start
2974 // over.
2975 if (changed != func_hashtab.ht_changed)
2976 break;
2977 }
Bram Moolenaarce658352020-07-31 23:47:12 +02002978 }
2979 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002980 }
Bram Moolenaarce658352020-07-31 23:47:12 +02002981 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002982}
2983
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002984#if defined(EXITFREE) || defined(PROTO)
2985 void
2986free_all_functions(void)
2987{
2988 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002989 ufunc_T *fp;
2990 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002991 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002992 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002993
Bram Moolenaare38eab22019-12-05 21:50:01 +01002994 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002995 while (current_funccal != NULL)
2996 {
2997 clear_tv(current_funccal->rettv);
2998 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002999 if (current_funccal == NULL && funccal_stack != NULL)
3000 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003001 }
3002
Bram Moolenaare38eab22019-12-05 21:50:01 +01003003 // First clear what the functions contain. Since this may lower the
3004 // reference count of a function, it may also free a function and change
3005 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003006 while (todo > 0)
3007 {
3008 todo = func_hashtab.ht_used;
3009 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3010 if (!HASHITEM_EMPTY(hi))
3011 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 // clear the def function index now
3013 fp = HI2UF(hi);
3014 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003015 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016
Bram Moolenaare38eab22019-12-05 21:50:01 +01003017 // Only free functions that are not refcounted, those are
3018 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003019 if (func_name_refcount(fp->uf_name))
3020 ++skipped;
3021 else
3022 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003023 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003024 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003025 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003026 {
3027 skipped = 0;
3028 break;
3029 }
3030 }
3031 --todo;
3032 }
3033 }
3034
Bram Moolenaare38eab22019-12-05 21:50:01 +01003035 // Now actually free the functions. Need to start all over every time,
3036 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003037 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003038 while (func_hashtab.ht_used > skipped)
3039 {
3040 todo = func_hashtab.ht_used;
3041 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003042 if (!HASHITEM_EMPTY(hi))
3043 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003044 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003045 // Only free functions that are not refcounted, those are
3046 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003047 fp = HI2UF(hi);
3048 if (func_name_refcount(fp->uf_name))
3049 ++skipped;
3050 else
3051 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003052 if (func_free(fp, FALSE) == OK)
3053 {
3054 skipped = 0;
3055 break;
3056 }
3057 // did not actually free it
3058 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003059 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003060 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003061 }
3062 if (skipped == 0)
3063 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064
3065 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003066}
3067#endif
3068
3069/*
3070 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02003071 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003072 * "len" is the length of "name", or -1 for NUL terminated.
3073 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003075builtin_function(char_u *name, int len)
3076{
3077 char_u *p;
3078
Bram Moolenaara26b9702020-04-18 19:53:28 +02003079 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003080 return FALSE;
3081 p = vim_strchr(name, AUTOLOAD_CHAR);
3082 return p == NULL || (len > 0 && p > name + len);
3083}
3084
3085 int
3086func_call(
3087 char_u *name,
3088 typval_T *args,
3089 partial_T *partial,
3090 dict_T *selfdict,
3091 typval_T *rettv)
3092{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003093 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003094 listitem_T *item;
3095 typval_T argv[MAX_FUNC_ARGS + 1];
3096 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003097 int r = 0;
3098
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003099 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003100 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003101 {
3102 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3103 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003104 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003105 break;
3106 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003107 // Make a copy of each argument. This is needed to be able to set
3108 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003109 copy_tv(&item->li_tv, &argv[argc++]);
3110 }
3111
3112 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003113 {
3114 funcexe_T funcexe;
3115
Bram Moolenaara80faa82020-04-12 19:37:17 +02003116 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003117 funcexe.fe_firstline = curwin->w_cursor.lnum;
3118 funcexe.fe_lastline = curwin->w_cursor.lnum;
3119 funcexe.fe_evaluate = TRUE;
3120 funcexe.fe_partial = partial;
3121 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003122 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3123 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003124
Bram Moolenaare38eab22019-12-05 21:50:01 +01003125 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003126 while (argc > 0)
3127 clear_tv(&argv[--argc]);
3128
3129 return r;
3130}
3131
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003132static int callback_depth = 0;
3133
3134 int
3135get_callback_depth(void)
3136{
3137 return callback_depth;
3138}
3139
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003140/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003141 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003142 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003143 */
3144 int
3145call_callback(
3146 callback_T *callback,
3147 int len, // length of "name" or -1 to use strlen()
3148 typval_T *rettv, // return value goes here
3149 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003150 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003151 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003152{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003153 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003154 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003155
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003156 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3157 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003158 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003159 funcexe.fe_evaluate = TRUE;
3160 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003161 ++callback_depth;
3162 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3163 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003164
3165 // When a :def function was called that uses :try an error would be turned
3166 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003167 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003168 {
3169 need_rethrow = FALSE;
3170 handle_did_throw();
3171 }
3172
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003173 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003174}
3175
3176/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003177 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003178 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003179 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3180 */
3181 varnumber_T
3182call_callback_retnr(
3183 callback_T *callback,
3184 int argcount, // number of "argvars"
3185 typval_T *argvars) // vars for arguments, must have "argcount"
3186 // PLUS ONE elements!
3187{
3188 typval_T rettv;
3189 varnumber_T retval;
3190
3191 if (call_callback(callback, 0, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003192 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003193
3194 retval = tv_get_number_chk(&rettv, NULL);
3195 clear_tv(&rettv);
3196 return retval;
3197}
3198
3199/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 * Give an error message for the result of a function.
3201 * Nothing if "error" is FCERR_NONE.
3202 */
3203 void
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003204user_func_error(int error, char_u *name, funcexe_T *funcexe)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205{
3206 switch (error)
3207 {
3208 case FCERR_UNKNOWN:
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003209 if (funcexe->fe_found_var)
3210 semsg(_(e_not_callable_type_str), name);
3211 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003212 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 break;
3214 case FCERR_NOTMETHOD:
3215 emsg_funcname(
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003216 N_(e_cannot_use_function_as_method_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 break;
3218 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003219 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 break;
3221 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003222 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 break;
3224 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003225 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 break;
3227 case FCERR_SCRIPT:
3228 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00003229 e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 break;
3231 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003232 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3233 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 break;
3235 }
3236}
3237
3238/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003239 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003240 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003241 * Return FAIL when the function can't be called, OK otherwise.
3242 * Also returns OK when an error was encountered while executing the function.
3243 */
3244 int
3245call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003246 char_u *funcname, // name of the function
3247 int len, // length of "name" or -1 to use strlen()
3248 typval_T *rettv, // return value goes here
3249 int argcount_in, // number of "argvars"
3250 typval_T *argvars_in, // vars for arguments, must have "argcount"
3251 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003252 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003253{
3254 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01003255 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003256 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003257 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003258 char_u fname_buf[FLEN_FIXED + 1];
3259 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003260 char_u *fname = NULL;
3261 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003262 int argcount = argcount_in;
3263 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003264 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003265 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003266 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003267 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003268 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003269 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003270 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003271 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003272
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003273 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3274 // even when call_func() returns FAIL.
3275 rettv->v_type = VAR_UNKNOWN;
3276
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003277 if (partial != NULL)
3278 fp = partial->pt_func;
3279 if (fp == NULL)
3280 {
3281 // Make a copy of the name, if it comes from a funcref variable it
3282 // could be changed or deleted in the called function.
3283 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3284 if (name == NULL)
3285 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003286
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003287 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3288 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003289
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003290 if (funcexe->fe_doesrange != NULL)
3291 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003292
3293 if (partial != NULL)
3294 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003295 // When the function has a partial with a dict and there is a dict
3296 // argument, use the dict argument. That is backwards compatible.
3297 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003298 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003299 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003300 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003301 {
3302 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003303 {
3304 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3305 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003306 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003307 goto theend;
3308 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003309 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003310 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003311 for (i = 0; i < argcount_in; ++i)
3312 argv[i + argv_clear] = argvars_in[i];
3313 argvars = argv;
3314 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003315
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003316 if (funcexe->fe_check_type != NULL
3317 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003318 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003319 // Now funcexe->fe_check_type is missing the added arguments,
3320 // make a copy of the type with the correction.
3321 check_type = *funcexe->fe_check_type;
3322 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003323 check_type.tt_args = check_type_args;
3324 CLEAR_FIELD(check_type_args);
3325 for (i = 0; i < check_type.tt_argcount; ++i)
3326 check_type_args[i + partial->pt_argc] =
3327 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003328 check_type.tt_argcount += partial->pt_argc;
3329 check_type.tt_min_argcount += partial->pt_argc;
3330 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003331 }
3332 }
3333
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003334 if (error == FCERR_NONE && funcexe->fe_check_type != NULL
3335 && funcexe->fe_evaluate)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003336 {
3337 // Check that the argument types are OK for the types of the funcref.
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003338 if (check_argument_types(funcexe->fe_check_type, argvars, argcount,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003339 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003340 error = FCERR_OTHER;
3341 }
3342
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003343 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003344 {
3345 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003346 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003347
Bram Moolenaar333894b2020-08-01 18:53:07 +02003348 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003349 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003350 {
3351 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003352 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003353 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003354
Bram Moolenaare38eab22019-12-05 21:50:01 +01003355 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003356 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003357 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003358
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003359 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003360 {
3361 /*
3362 * User defined function.
3363 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003364 if (fp == NULL)
Bram Moolenaar333894b2020-08-01 18:53:07 +02003365 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003366
Bram Moolenaare38eab22019-12-05 21:50:01 +01003367 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003368 if (fp == NULL
3369 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003370 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003371 && !aborting())
3372 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003373 // executed an autocommand, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02003374 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003375 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003376 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003377 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3378 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003379 // loaded a package, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02003380 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003381 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003382 if (fp == NULL)
3383 {
3384 char_u *p = untrans_function_name(rfname);
3385
3386 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003387 // Don't do this if the name starts with "s:".
3388 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaar333894b2020-08-01 18:53:07 +02003389 fp = find_func(p, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003390 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003391
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003392 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003393 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02003394#ifdef FEAT_LUA
3395 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
3396 {
3397 cfunc_T cb = fp->uf_cb;
3398
3399 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3400 }
3401#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003402 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003403 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003404 if (funcexe->fe_argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003405 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003406 argcount = funcexe->fe_argv_func(argcount, argvars,
3407 argv_clear, fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003408
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003409 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003410 {
3411 // Method call: base->Method()
3412 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003413 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003414 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003415 argvars = argv;
3416 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003417 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 error = call_user_func_check(fp, argcount, argvars, rettv,
3420 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003421 }
3422 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003423 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003424 {
3425 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003426 * expr->method(): Find the method name in the table, call its
3427 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003428 */
3429 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003430 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003431 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003432 else
3433 {
3434 /*
3435 * Find the function name in the table, call its implementation.
3436 */
3437 error = call_internal_func(fname, argcount, argvars, rettv);
3438 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003439
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003440 /*
3441 * The function call (or "FuncUndefined" autocommand sequence) might
3442 * have been aborted by an error, an interrupt, or an explicitly thrown
3443 * exception that has not been caught so far. This situation can be
3444 * tested for by calling aborting(). For an error in an internal
3445 * function or for the "E132" error in call_user_func(), however, the
3446 * throw point at which the "force_abort" flag (temporarily reset by
3447 * emsg()) is normally updated has not been reached yet. We need to
3448 * update that flag first to make aborting() reliable.
3449 */
3450 update_force_abort();
3451 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003452 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003453 ret = OK;
3454
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003455theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003456 /*
3457 * Report an error unless the argument evaluation or function call has been
3458 * cancelled due to an aborting error, an interrupt, or an exception.
3459 */
3460 if (!aborting())
3461 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003462 user_func_error(error, (name != NULL) ? name : funcname, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003463 }
3464
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003465 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003466 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003467 clear_tv(&argv[--argv_clear + argv_base]);
3468
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003469 vim_free(tofree);
3470 vim_free(name);
3471
3472 return ret;
3473}
3474
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003475 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476printable_func_name(ufunc_T *fp)
3477{
3478 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3479}
3480
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003481/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003482 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003483 */
3484 static void
3485list_func_head(ufunc_T *fp, int indent)
3486{
3487 int j;
3488
3489 msg_start();
3490 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003491 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003492 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003493 msg_puts("def ");
3494 else
3495 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003497 msg_putchar('(');
3498 for (j = 0; j < fp->uf_args.ga_len; ++j)
3499 {
3500 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003501 msg_puts(", ");
3502 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 if (fp->uf_arg_types != NULL)
3504 {
3505 char *tofree;
3506
3507 msg_puts(": ");
3508 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3509 vim_free(tofree);
3510 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003511 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3512 {
3513 msg_puts(" = ");
3514 msg_puts(((char **)(fp->uf_def_args.ga_data))
3515 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3516 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003517 }
3518 if (fp->uf_varargs)
3519 {
3520 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003521 msg_puts(", ");
3522 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003523 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 if (fp->uf_va_name != NULL)
3525 {
3526 if (j)
3527 msg_puts(", ");
3528 msg_puts("...");
3529 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003530 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 {
3532 char *tofree;
3533
3534 msg_puts(": ");
3535 msg_puts(type_name(fp->uf_va_type, &tofree));
3536 vim_free(tofree);
3537 }
3538 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003539 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003540
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003541 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003542 {
3543 if (fp->uf_ret_type != &t_void)
3544 {
3545 char *tofree;
3546
3547 msg_puts(": ");
3548 msg_puts(type_name(fp->uf_ret_type, &tofree));
3549 vim_free(tofree);
3550 }
3551 }
3552 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003553 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003554 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003555 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003556 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003557 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003558 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003559 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003560 msg_clr_eos();
3561 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003562 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003563}
3564
3565/*
3566 * Get a function name, translating "<SID>" and "<SNR>".
3567 * Also handles a Funcref in a List or Dictionary.
3568 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003569 * Set "*is_global" to TRUE when the function must be global, unless
3570 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003571 * flags:
3572 * TFN_INT: internal function name OK
3573 * TFN_QUIET: be quiet
3574 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003575 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003576 * Advances "pp" to just after the function name (if no error).
3577 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003578 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003579trans_function_name(
3580 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003581 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003582 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003583 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003584 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003585 partial_T **partial, // return: partial of a FuncRef
3586 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003587{
3588 char_u *name = NULL;
3589 char_u *start;
3590 char_u *end;
3591 int lead;
3592 char_u sid_buf[20];
3593 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003595 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003597
3598 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003599 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003600 start = *pp;
3601
Bram Moolenaare38eab22019-12-05 21:50:01 +01003602 // Check for hard coded <SNR>: already translated function ID (from a user
3603 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003604 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
3605 && (*pp)[2] == (int)KE_SNR)
3606 {
3607 *pp += 3;
3608 len = get_id_len(pp) + 3;
3609 return vim_strnsave(start, len);
3610 }
3611
Bram Moolenaare38eab22019-12-05 21:50:01 +01003612 // A name starting with "<SID>" or "<SNR>" is local to a script. But
3613 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003614 lead = eval_fname_script(start);
3615 if (lead > 2)
3616 start += lead;
3617
Bram Moolenaare38eab22019-12-05 21:50:01 +01003618 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01003619 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003620 lead > 2 ? 0 : FNE_CHECK_START);
3621 if (end == start)
3622 {
3623 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003624 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003625 goto theend;
3626 }
3627 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
3628 {
3629 /*
3630 * Report an invalid expression in braces, unless the expression
3631 * evaluation has been cancelled due to an aborting error, an
3632 * interrupt, or an exception.
3633 */
3634 if (!aborting())
3635 {
3636 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003637 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003638 }
3639 else
3640 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
3641 goto theend;
3642 }
3643
3644 if (lv.ll_tv != NULL)
3645 {
3646 if (fdp != NULL)
3647 {
3648 fdp->fd_dict = lv.ll_dict;
3649 fdp->fd_newkey = lv.ll_newkey;
3650 lv.ll_newkey = NULL;
3651 fdp->fd_di = lv.ll_di;
3652 }
3653 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
3654 {
3655 name = vim_strsave(lv.ll_tv->vval.v_string);
3656 *pp = end;
3657 }
3658 else if (lv.ll_tv->v_type == VAR_PARTIAL
3659 && lv.ll_tv->vval.v_partial != NULL)
3660 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003661 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003662 *pp = end;
3663 if (partial != NULL)
3664 *partial = lv.ll_tv->vval.v_partial;
3665 }
3666 else
3667 {
3668 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
3669 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00003670 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003671 else
3672 *pp = end;
3673 name = NULL;
3674 }
3675 goto theend;
3676 }
3677
3678 if (lv.ll_name == NULL)
3679 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003680 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003681 *pp = end;
3682 goto theend;
3683 }
3684
Bram Moolenaare38eab22019-12-05 21:50:01 +01003685 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003686 if (lv.ll_exp_name != NULL)
3687 {
3688 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003689 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003690 flags & TFN_NO_AUTOLOAD, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003691 if (name == lv.ll_exp_name)
3692 name = NULL;
3693 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003694 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003695 {
3696 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003697 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003698 flags & TFN_NO_AUTOLOAD, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003699 if (name == *pp)
3700 name = NULL;
3701 }
3702 if (name != NULL)
3703 {
3704 name = vim_strsave(name);
3705 *pp = end;
3706 if (STRNCMP(name, "<SNR>", 5) == 0)
3707 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003708 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003709 name[0] = K_SPECIAL;
3710 name[1] = KS_EXTRA;
3711 name[2] = (int)KE_SNR;
3712 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
3713 }
3714 goto theend;
3715 }
3716
3717 if (lv.ll_exp_name != NULL)
3718 {
3719 len = (int)STRLEN(lv.ll_exp_name);
3720 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
3721 && STRNCMP(lv.ll_name, "s:", 2) == 0)
3722 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003723 // When there was "s:" already or the name expanded to get a
3724 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003725 lv.ll_name += 2;
3726 len -= 2;
3727 lead = 2;
3728 }
3729 }
3730 else
3731 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003732 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003733 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003734 {
3735 if (is_global != NULL && lv.ll_name[0] == 'g')
3736 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003737 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003738 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003739 len = (int)(end - lv.ll_name);
3740 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003741 if (len <= 0)
3742 {
3743 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003744 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003745 goto theend;
3746 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003747
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003748 // In Vim9 script a user function is script-local by default, unless it
3749 // starts with a lower case character: dict.func().
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003750 vim9script = ASCII_ISUPPER(*start) && in_vim9script();
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003751 if (vim9script)
3752 {
3753 char_u *p;
3754
3755 // SomeScript#func() is a global function.
3756 for (p = start; *p != NUL && *p != '('; ++p)
3757 if (*p == AUTOLOAD_CHAR)
3758 vim9script = FALSE;
3759 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003761 /*
3762 * Copy the function name to allocated memory.
3763 * Accept <SID>name() inside a script, translate into <SNR>123_name().
3764 * Accept <SNR>123_name() outside a script.
3765 */
3766 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003767 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003769 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 if (!vim9script)
3771 lead = 3;
3772 if (vim9script || (lv.ll_exp_name != NULL
3773 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003774 || eval_fname_sid(*pp))
3775 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003777 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003778 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00003779 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003780 goto theend;
3781 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003782 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 if (vim9script)
3784 extra = 3 + (int)STRLEN(sid_buf);
3785 else
3786 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003787 }
3788 }
Bram Moolenaar22f17a22021-06-21 20:48:58 +02003789 else if (!(flags & TFN_INT) && (builtin_function(lv.ll_name, len)
3790 || (in_vim9script() && *lv.ll_name == '_')))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003791 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00003792 semsg(_(e_function_name_must_start_with_capital_or_s_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003793 goto theend;
3794 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003795 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003796 {
3797 char_u *cp = vim_strchr(lv.ll_name, ':');
3798
3799 if (cp != NULL && cp < end)
3800 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003801 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003802 goto theend;
3803 }
3804 }
3805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003807 if (name != NULL)
3808 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01003809 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003810 {
3811 name[0] = K_SPECIAL;
3812 name[1] = KS_EXTRA;
3813 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003815 STRCPY(name + 3, sid_buf);
3816 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
3818 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003819 }
3820 *pp = end;
3821
3822theend:
3823 clear_lval(&lv);
3824 return name;
3825}
3826
3827/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02003828 * Assuming "name" is the result of trans_function_name() and it was prefixed
3829 * to use the script-local name, return the unmodified name (points into
3830 * "name"). Otherwise return NULL.
3831 * This can be used to first search for a script-local function and fall back
3832 * to the global function if not found.
3833 */
3834 char_u *
3835untrans_function_name(char_u *name)
3836{
3837 char_u *p;
3838
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003839 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02003840 {
3841 p = vim_strchr(name, '_');
3842 if (p != NULL)
3843 return p + 1;
3844 }
3845 return NULL;
3846}
3847
3848/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00003849 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
3850 * current script ID and returns the expanded function name. The caller should
3851 * free the returned name. If not called from a script context or the function
3852 * name doesn't start with these prefixes, then returns NULL.
3853 * This doesn't check whether the script-local function exists or not.
3854 */
3855 char_u *
3856get_scriptlocal_funcname(char_u *funcname)
3857{
3858 char sid_buf[25];
3859 int off;
3860 char_u *newname;
3861
3862 if (funcname == NULL)
3863 return NULL;
3864
3865 if (STRNCMP(funcname, "s:", 2) != 0
3866 && STRNCMP(funcname, "<SID>", 5) != 0)
3867 // The function name is not a script-local function name
3868 return NULL;
3869
3870 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3871 {
3872 emsg(_(e_using_sid_not_in_script_context));
3873 return NULL;
3874 }
3875 // Expand s: prefix into <SNR>nr_<name>
3876 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
3877 (long)current_sctx.sc_sid);
3878 off = *funcname == 's' ? 2 : 5;
3879 newname = alloc(STRLEN(sid_buf) + STRLEN(funcname + off) + 1);
3880 if (newname == NULL)
3881 return NULL;
3882 STRCPY(newname, sid_buf);
3883 STRCAT(newname, funcname + off);
3884
3885 return newname;
3886}
3887
3888/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00003889 * Call trans_function_name(), except that a lambda is returned as-is.
3890 * Returns the name in allocated memory.
3891 */
3892 char_u *
3893save_function_name(
3894 char_u **name,
3895 int *is_global,
3896 int skip,
3897 int flags,
3898 funcdict_T *fudi)
3899{
3900 char_u *p = *name;
3901 char_u *saved;
3902
3903 if (STRNCMP(p, "<lambda>", 8) == 0)
3904 {
3905 p += 8;
3906 (void)getdigits(&p);
3907 saved = vim_strnsave(*name, p - *name);
3908 if (fudi != NULL)
3909 CLEAR_POINTER(fudi);
3910 }
3911 else
3912 saved = trans_function_name(&p, is_global, skip,
3913 flags, fudi, NULL, NULL);
3914 *name = p;
3915 return saved;
3916}
3917
3918/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003919 * List functions. When "regmatch" is NULL all of then.
3920 * Otherwise functions matching "regmatch".
3921 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003922 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003923list_functions(regmatch_T *regmatch)
3924{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003925 int changed = func_hashtab.ht_changed;
3926 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003927 hashitem_T *hi;
3928
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003929 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003930 {
3931 if (!HASHITEM_EMPTY(hi))
3932 {
3933 ufunc_T *fp = HI2UF(hi);
3934
3935 --todo;
3936 if ((fp->uf_flags & FC_DEAD) == 0
3937 && (regmatch == NULL
3938 ? !message_filtered(fp->uf_name)
3939 && !func_name_refcount(fp->uf_name)
3940 : !isdigit(*fp->uf_name)
3941 && vim_regexec(regmatch, fp->uf_name, 0)))
3942 {
3943 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003944 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003945 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00003946 emsg(_(e_function_list_was_modified));
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003947 return;
3948 }
3949 }
3950 }
3951 }
3952}
3953
3954/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02003955 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003956 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
3957 * the function name.
Bram Moolenaar04b12692020-05-04 23:24:44 +02003958 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003959 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02003960 ufunc_T *
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00003961define_function(exarg_T *eap, char_u *name_arg, char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003962{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003963 int j;
3964 int c;
3965 int saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003966 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003967 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003968 char_u *p;
3969 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01003970 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003971 char_u *line_arg = NULL;
3972 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003974 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003975 garray_T newlines;
3976 int varargs = FALSE;
3977 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003979 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00003980 int fp_allocated = FALSE;
3981 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003982 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003983 dictitem_T *v;
3984 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003985 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003986 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003987 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003988 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003989 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02003990 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003991
3992 /*
3993 * ":function" without argument: list functions.
3994 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003995 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003996 {
3997 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003998 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02003999 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004000 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004001 }
4002
4003 /*
4004 * ":function /pat": list functions matching pattern.
4005 */
4006 if (*eap->arg == '/')
4007 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004008 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004009 if (!eap->skip)
4010 {
4011 regmatch_T regmatch;
4012
4013 c = *p;
4014 *p = NUL;
4015 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4016 *p = c;
4017 if (regmatch.regprog != NULL)
4018 {
4019 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004020 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004021 vim_regfree(regmatch.regprog);
4022 }
4023 }
4024 if (*p == '/')
4025 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004026 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004027 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004028 }
4029
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 ga_init(&newargs);
4031 ga_init(&argtypes);
4032 ga_init(&default_args);
4033
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004034 /*
4035 * Get the function name. There are these situations:
4036 * func normal function name
4037 * "name" == func, "fudi.fd_dict" == NULL
4038 * dict.func new dictionary entry
4039 * "name" == NULL, "fudi.fd_dict" set,
4040 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4041 * dict.func existing dict entry with a Funcref
4042 * "name" == func, "fudi.fd_dict" set,
4043 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4044 * dict.func existing dict entry that's not a Funcref
4045 * "name" == NULL, "fudi.fd_dict" set,
4046 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4047 * s:func script-local function name
4048 * g:func global function name, same as "func"
4049 */
4050 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004051 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004052 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004053 // nested function, argument is (args).
4054 paren = TRUE;
4055 CLEAR_FIELD(fudi);
4056 }
4057 else
4058 {
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004059 name = save_function_name(&p, &is_global, eap->skip,
4060 TFN_NO_AUTOLOAD, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004061 paren = (vim_strchr(p, '(') != NULL);
4062 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004063 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004064 /*
4065 * Return on an invalid expression in braces, unless the expression
4066 * evaluation has been cancelled due to an aborting error, an
4067 * interrupt, or an exception.
4068 */
4069 if (!aborting())
4070 {
4071 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004072 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004073 vim_free(fudi.fd_newkey);
4074 return NULL;
4075 }
4076 else
4077 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004078 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004079 }
4080
Bram Moolenaare38eab22019-12-05 21:50:01 +01004081 // An error in a function call during evaluation of an expression in magic
4082 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004083 saved_did_emsg = did_emsg;
4084 did_emsg = FALSE;
4085
4086 /*
4087 * ":function func" with only function name: list function.
4088 */
4089 if (!paren)
4090 {
4091 if (!ends_excmd(*skipwhite(p)))
4092 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004093 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004094 goto ret_free;
4095 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004096 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004097 if (eap->nextcmd != NULL)
4098 *p = NUL;
4099 if (!eap->skip && !got_int)
4100 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004101 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004102 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4103 {
4104 char_u *up = untrans_function_name(name);
4105
4106 // With Vim9 script the name was made script-local, if not
4107 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004108 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004109 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004110 }
4111
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004112 if (fp != NULL)
4113 {
4114 list_func_head(fp, TRUE);
4115 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4116 {
4117 if (FUNCLINE(fp, j) == NULL)
4118 continue;
4119 msg_putchar('\n');
4120 msg_outnum((long)(j + 1));
4121 if (j < 9)
4122 msg_putchar(' ');
4123 if (j < 99)
4124 msg_putchar(' ');
4125 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004126 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004127 ui_breakcheck();
4128 }
4129 if (!got_int)
4130 {
4131 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004132 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 msg_puts(" enddef");
4134 else
4135 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004136 }
4137 }
4138 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004139 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004140 }
4141 goto ret_free;
4142 }
4143
4144 /*
4145 * ":function name(arg1, arg2)" Define function.
4146 */
4147 p = skipwhite(p);
4148 if (*p != '(')
4149 {
4150 if (!eap->skip)
4151 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004152 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004153 goto ret_free;
4154 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004155 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004156 if (vim_strchr(p, '(') != NULL)
4157 p = vim_strchr(p, '(');
4158 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004159
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004160 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4161 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004162 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004163 goto ret_free;
4164 }
4165
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004166 // In Vim9 script only global functions can be redefined.
4167 if (vim9script && eap->forceit && !is_global)
4168 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004169 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004170 goto ret_free;
4171 }
4172
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004173 ga_init2(&newlines, (int)sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004174
Bram Moolenaar04b12692020-05-04 23:24:44 +02004175 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004176 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004177 // Check the name of the function. Unless it's a dictionary function
4178 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004179 if (name != NULL)
4180 arg = name;
4181 else
4182 arg = fudi.fd_newkey;
4183 if (arg != NULL && (fudi.fd_di == NULL
4184 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4185 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4186 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004187 char_u *name_base = arg;
4188 int i;
4189
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004190 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004191 {
4192 name_base = vim_strchr(arg, '_');
4193 if (name_base == NULL)
4194 name_base = arg + 3;
4195 else
4196 ++name_base;
4197 }
4198 for (i = 0; name_base[i] != NUL && (i == 0
4199 ? eval_isnamec1(name_base[i])
4200 : eval_isnamec(name_base[i])); ++i)
4201 ;
4202 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004203 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004204
4205 // In Vim9 script a function cannot have the same name as a
4206 // variable.
4207 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004208 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4209 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004210 + EVAL_VAR_NO_FUNC) == OK)
4211 {
4212 semsg(_(e_redefining_script_item_str), name_base);
4213 goto ret_free;
4214 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004215 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004216 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004217 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004218 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004219 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004220 goto ret_free;
4221 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004222 }
4223
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004224 // This may get more lines and make the pointers into the first line
4225 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004226 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004228 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004229 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004230 eap, line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004231 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004232 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004233
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004235 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004237 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004238 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004239 if (*p != ':')
4240 {
4241 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4242 p = skipwhite(p);
4243 }
4244 else if (!IS_WHITE_OR_NUL(p[1]))
4245 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004247 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004249 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004250 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004251 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004255 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004256 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004257 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004258 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004259 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004260 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004261 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 else
4263 // find extra arguments "range", "dict", "abort" and "closure"
4264 for (;;)
4265 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004266 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 p = skipwhite(p);
4268 if (STRNCMP(p, "range", 5) == 0)
4269 {
4270 flags |= FC_RANGE;
4271 p += 5;
4272 }
4273 else if (STRNCMP(p, "dict", 4) == 0)
4274 {
4275 flags |= FC_DICT;
4276 p += 4;
4277 }
4278 else if (STRNCMP(p, "abort", 5) == 0)
4279 {
4280 flags |= FC_ABORT;
4281 p += 5;
4282 }
4283 else if (STRNCMP(p, "closure", 7) == 0)
4284 {
4285 flags |= FC_CLOSURE;
4286 p += 7;
4287 if (current_funccal == NULL)
4288 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004289 emsg_funcname(e_closure_function_should_not_be_at_top_level,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 name == NULL ? (char_u *)"" : name);
4291 goto erret;
4292 }
4293 }
4294 else
4295 break;
4296 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004297
Bram Moolenaare38eab22019-12-05 21:50:01 +01004298 // When there is a line break use what follows for the function body.
4299 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004300 if (*p == '\n')
4301 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004302 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004303 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4304 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004305 && !(VIM_ISWHITE(*whitep) && *p == '#'
4306 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004307 && !eap->skip
4308 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004309 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004310
4311 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4313 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004314 */
4315 if (KeyTyped)
4316 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004317 // Check if the function already exists, don't let the user type the
4318 // whole function before telling him it doesn't work! For a script we
4319 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004320 if (!eap->skip && !eap->forceit)
4321 {
4322 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004323 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004324 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00004325 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004326 }
4327
4328 if (!eap->skip && did_emsg)
4329 goto erret;
4330
Bram Moolenaare38eab22019-12-05 21:50:01 +01004331 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004332 cmdline_row = msg_row;
4333 }
4334
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004335 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004336 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004337
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004338 // Do not define the function when getting the body fails and when
4339 // skipping.
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004340 if (get_function_body(eap, &newlines, line_arg, line_to_free) == FAIL
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004341 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004342 goto erret;
4343
4344 /*
4345 * If there are no errors, add the function
4346 */
4347 if (fudi.fd_dict == NULL)
4348 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 hashtab_T *ht;
4350
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004351 v = find_var(name, &ht, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004352 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
4353 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004354 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004355 goto erret;
4356 }
4357
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004358 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaareef21022020-08-01 22:16:43 +02004359 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004360 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004361 char_u *uname = untrans_function_name(name);
4362
4363 import = find_imported(uname == NULL ? name : uname, 0, NULL);
4364 }
4365
4366 if (fp != NULL || import != NULL)
4367 {
4368 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004370 // Function can be replaced with "function!" and when sourcing the
4371 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02004372 // A name that is used by an import can not be overruled.
4373 if (import != NULL
4374 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004375 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02004376 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004377 {
Bram Moolenaard604d782021-11-20 21:46:20 +00004378 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02004379 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004380 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02004381 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00004382 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004383 goto erret;
4384 }
4385 if (fp->uf_calls > 0)
4386 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004387 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00004388 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004389 goto erret;
4390 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004391 if (fp->uf_refcount > 1)
4392 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004393 // This function is referenced somewhere, don't redefine it but
4394 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004395 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004396 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004397 fp = NULL;
4398 overwrite = TRUE;
4399 }
4400 else
4401 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004402 char_u *exp_name = fp->uf_name_exp;
4403
4404 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01004405 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004406 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004407 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004408 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004410#ifdef FEAT_PROFILE
4411 fp->uf_profiling = FALSE;
4412 fp->uf_prof_initialized = FALSE;
4413#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01004414 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004415 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004416 }
4417 }
4418 else
4419 {
4420 char numbuf[20];
4421
4422 fp = NULL;
4423 if (fudi.fd_newkey == NULL && !eap->forceit)
4424 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004425 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004426 goto erret;
4427 }
4428 if (fudi.fd_di == NULL)
4429 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004430 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02004431 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004432 goto erret;
4433 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004434 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02004435 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004436 goto erret;
4437
Bram Moolenaare38eab22019-12-05 21:50:01 +01004438 // Give the function a sequential number. Can only be used with a
4439 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004440 vim_free(name);
4441 sprintf(numbuf, "%d", ++func_nr);
4442 name = vim_strsave((char_u *)numbuf);
4443 if (name == NULL)
4444 goto erret;
4445 }
4446
4447 if (fp == NULL)
4448 {
4449 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
4450 {
4451 int slen, plen;
4452 char_u *scriptname;
4453
Bram Moolenaare38eab22019-12-05 21:50:01 +01004454 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004455 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004456 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004457 {
4458 scriptname = autoload_name(name);
4459 if (scriptname != NULL)
4460 {
4461 p = vim_strchr(scriptname, '/');
4462 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004463 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004464 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004465 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004466 j = OK;
4467 vim_free(scriptname);
4468 }
4469 }
4470 if (j == FAIL)
4471 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004472 linenr_T save_lnum = SOURCING_LNUM;
4473
4474 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00004475 semsg(_(e_function_name_does_not_match_script_file_name_str),
4476 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004477 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004478 goto erret;
4479 }
4480 }
4481
Bram Moolenaar47ed5532019-08-08 20:49:14 +02004482 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004483 if (fp == NULL)
4484 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004485 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004486
4487 if (fudi.fd_dict != NULL)
4488 {
4489 if (fudi.fd_di == NULL)
4490 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004491 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004492 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
4493 if (fudi.fd_di == NULL)
4494 {
4495 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004496 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004497 goto erret;
4498 }
4499 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
4500 {
4501 vim_free(fudi.fd_di);
4502 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004503 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004504 goto erret;
4505 }
4506 }
4507 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004508 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004509 clear_tv(&fudi.fd_di->di_tv);
4510 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004511 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004512
Bram Moolenaare38eab22019-12-05 21:50:01 +01004513 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004514 flags |= FC_DICT;
4515 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004516 }
4517 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004518 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004520 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521
4522 if (eap->cmdidx == CMD_def)
4523 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004524 int lnum_save = SOURCING_LNUM;
4525 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004526
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004527 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004528
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004529 // error messages are for the first function line
4530 SOURCING_LNUM = sourcing_lnum_top;
4531
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02004532 // The function may use script variables from the context.
4533 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004534
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004535 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004537 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004538 free_fp = fp_allocated;
4539 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004541 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542
4543 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004544 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004546 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004547 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004548 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02004550 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004552 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004553 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004555 if (fp_allocated)
4556 {
4557 // insert the new function in the function list
4558 set_ufunc_name(fp, name);
4559 if (overwrite)
4560 {
4561 hi = hash_find(&func_hashtab, name);
4562 hi->hi_key = UF2HIKEY(fp);
4563 }
4564 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
4565 {
4566 free_fp = TRUE;
4567 goto erret;
4568 }
4569 fp->uf_refcount = 1;
4570 }
4571
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004572 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004573 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004574 if ((flags & FC_CLOSURE) != 0)
4575 {
Bram Moolenaar58016442016-07-31 18:30:22 +02004576 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004577 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004578 }
4579 else
4580 fp->uf_scoped = NULL;
4581
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004582#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004583 if (prof_def_func())
4584 func_do_profile(fp);
4585#endif
4586 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02004587 if (sandbox)
4588 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004589 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004590 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004591 fp->uf_flags = flags;
4592 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01004593 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004594 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004595 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004596 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 if (is_export)
4598 {
4599 fp->uf_flags |= FC_EXPORT;
4600 // let ex_export() know the export worked.
4601 is_export = FALSE;
4602 }
4603
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004604 if (eap->cmdidx == CMD_def)
4605 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02004606 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
4607 // :func does not use Vim9 script syntax, even in a Vim9 script file
4608 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004609
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004610 goto ret_free;
4611
4612erret:
4613 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004614 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004615 if (fp != NULL)
4616 {
4617 ga_init(&fp->uf_args);
4618 ga_init(&fp->uf_def_args);
4619 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004620errret_2:
4621 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004622 if (fp != NULL)
4623 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004624 if (free_fp)
4625 {
4626 vim_free(fp);
4627 fp = NULL;
4628 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004629ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004630 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004631 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004632 if (name != name_arg)
4633 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004634 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004635 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004636
4637 return fp;
4638}
4639
4640/*
4641 * ":function"
4642 */
4643 void
4644ex_function(exarg_T *eap)
4645{
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004646 char_u *line_to_free = NULL;
4647
4648 (void)define_function(eap, NULL, &line_to_free);
4649 vim_free(line_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02004650}
4651
4652/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004653 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarb2049902021-01-24 12:53:53 +01004654 * be compiled. Except dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02004655 */
4656 void
4657ex_defcompile(exarg_T *eap UNUSED)
4658{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004659 long todo = (long)func_hashtab.ht_used;
4660 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004661 hashitem_T *hi;
4662 ufunc_T *ufunc;
4663
4664 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
4665 {
4666 if (!HASHITEM_EMPTY(hi))
4667 {
4668 --todo;
4669 ufunc = HI2UF(hi);
4670 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004671 && ufunc->uf_def_status == UF_TO_BE_COMPILED
4672 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004673 {
Bram Moolenaare99d4222021-06-13 14:01:26 +02004674 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004675
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004676 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004677 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004678 // a function has been added or removed, need to start over
4679 todo = (long)func_hashtab.ht_used;
4680 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004681 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02004682 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004683 }
4684 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004685 }
4686 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004687}
4688
4689/*
4690 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
4691 * Return 2 if "p" starts with "s:".
4692 * Return 0 otherwise.
4693 */
4694 int
4695eval_fname_script(char_u *p)
4696{
Bram Moolenaare38eab22019-12-05 21:50:01 +01004697 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
4698 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004699 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
4700 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
4701 return 5;
4702 if (p[0] == 's' && p[1] == ':')
4703 return 2;
4704 return 0;
4705}
4706
4707 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004708translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004709{
4710 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004711 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004712 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713}
4714
4715/*
4716 * Return TRUE when "ufunc" has old-style "..." varargs
4717 * or named varargs "...name: type".
4718 */
4719 int
4720has_varargs(ufunc_T *ufunc)
4721{
4722 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004723}
4724
4725/*
4726 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004727 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004728 */
4729 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004730function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004731{
4732 char_u *nm = name;
4733 char_u *p;
4734 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004735 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004736 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004737
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004738 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
4739 if (no_deref)
4740 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004741 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004742 nm = skipwhite(nm);
4743
Bram Moolenaare38eab22019-12-05 21:50:01 +01004744 // Only accept "funcname", "funcname ", "funcname (..." and
4745 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004746 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004747 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004748 vim_free(p);
4749 return n;
4750}
4751
Bram Moolenaar113e1072019-01-20 15:30:40 +01004752#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004753 char_u *
4754get_expanded_name(char_u *name, int check)
4755{
4756 char_u *nm = name;
4757 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004758 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004759
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004760 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004761 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004762
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004763 if (p != NULL && *nm == NUL
4764 && (!check || translated_function_exists(p, is_global)))
4765 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004766
4767 vim_free(p);
4768 return NULL;
4769}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004770#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004771
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004772/*
4773 * Function given to ExpandGeneric() to obtain the list of user defined
4774 * function names.
4775 */
4776 char_u *
4777get_user_func_name(expand_T *xp, int idx)
4778{
4779 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004780 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004781 static hashitem_T *hi;
4782 ufunc_T *fp;
4783
4784 if (idx == 0)
4785 {
4786 done = 0;
4787 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004788 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004789 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004790 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004791 {
4792 if (done++ > 0)
4793 ++hi;
4794 while (HASHITEM_EMPTY(hi))
4795 ++hi;
4796 fp = HI2UF(hi);
4797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 // don't show dead, dict and lambda functions
4799 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02004800 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004802
4803 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004804 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004805
4806 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02004807 if (xp->xp_context != EXPAND_USER_FUNC
4808 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004809 {
4810 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004812 STRCAT(IObuff, ")");
4813 }
4814 return IObuff;
4815 }
4816 return NULL;
4817}
4818
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004819/*
4820 * ":delfunction {name}"
4821 */
4822 void
4823ex_delfunction(exarg_T *eap)
4824{
4825 ufunc_T *fp = NULL;
4826 char_u *p;
4827 char_u *name;
4828 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004829 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004830
4831 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004832 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
4833 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004834 vim_free(fudi.fd_newkey);
4835 if (name == NULL)
4836 {
4837 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004838 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004839 return;
4840 }
4841 if (!ends_excmd(*skipwhite(p)))
4842 {
4843 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00004844 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004845 return;
4846 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004847 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004848 if (eap->nextcmd != NULL)
4849 *p = NUL;
4850
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004851 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02004852 {
4853 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004854 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02004855 vim_free(name);
4856 return;
4857 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004858 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004859 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004860 vim_free(name);
4861
4862 if (!eap->skip)
4863 {
4864 if (fp == NULL)
4865 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004866 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004867 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004868 return;
4869 }
4870 if (fp->uf_calls > 0)
4871 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004872 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004873 return;
4874 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004875 if (fp->uf_flags & FC_VIM9)
4876 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004877 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004878 return;
4879 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004880
4881 if (fudi.fd_dict != NULL)
4882 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004883 // Delete the dict item that refers to the function, it will
4884 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004885 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4886 }
4887 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004888 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004889 // A normal function (not a numbered function or lambda) has a
4890 // refcount of 1 for the entry in the hashtable. When deleting
4891 // it and the refcount is more than one, it should be kept.
4892 // A numbered function and lambda should be kept if the refcount is
4893 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004894 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004895 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004896 // Function is still referenced somewhere. Don't free it but
4897 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004898 if (func_remove(fp))
4899 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004900 }
4901 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004902 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004903 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004904 }
4905}
4906
4907/*
4908 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004909 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004910 */
4911 void
4912func_unref(char_u *name)
4913{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004914 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004915
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004916 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004917 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004918 fp = find_func(name, FALSE, NULL);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004919 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004920 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004921#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004922 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004923#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004924 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004925 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004926 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004927}
4928
4929/*
4930 * Unreference a Function: decrement the reference count and free it when it
4931 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004932 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004933 */
4934 void
4935func_ptr_unref(ufunc_T *fp)
4936{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004937 if (fp != NULL && (--fp->uf_refcount <= 0
4938 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
4939 && fp->uf_partial->pt_refcount <= 1
4940 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02004941 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004942 // Only delete it when it's not being used. Otherwise it's done
4943 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004944 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004945 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004946 }
4947}
4948
4949/*
4950 * Count a reference to a Function.
4951 */
4952 void
4953func_ref(char_u *name)
4954{
4955 ufunc_T *fp;
4956
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004957 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004958 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004959 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004960 if (fp != NULL)
4961 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004962 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01004963 // Only give an error for a numbered function.
4964 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01004965 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004966}
4967
4968/*
4969 * Count a reference to a Function.
4970 */
4971 void
4972func_ptr_ref(ufunc_T *fp)
4973{
4974 if (fp != NULL)
4975 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004976}
4977
4978/*
4979 * Return TRUE if items in "fc" do not have "copyID". That means they are not
4980 * referenced from anywhere that is in use.
4981 */
4982 static int
4983can_free_funccal(funccall_T *fc, int copyID)
4984{
4985 return (fc->l_varlist.lv_copyID != copyID
4986 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004987 && fc->l_avars.dv_copyID != copyID
4988 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004989}
4990
4991/*
4992 * ":return [expr]"
4993 */
4994 void
4995ex_return(exarg_T *eap)
4996{
4997 char_u *arg = eap->arg;
4998 typval_T rettv;
4999 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005000 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005001
5002 if (current_funccal == NULL)
5003 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005004 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005005 return;
5006 }
5007
Bram Moolenaar844fb642021-10-23 13:32:30 +01005008 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005009 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5010
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005011 if (eap->skip)
5012 ++emsg_skip;
5013
5014 eap->nextcmd = NULL;
5015 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005016 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005017 {
5018 if (!eap->skip)
5019 returning = do_return(eap, FALSE, TRUE, &rettv);
5020 else
5021 clear_tv(&rettv);
5022 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005023 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005024 else if (!eap->skip)
5025 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005026 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005027 update_force_abort();
5028
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005029 /*
5030 * Return unless the expression evaluation has been cancelled due to an
5031 * aborting error, an interrupt, or an exception.
5032 */
5033 if (!aborting())
5034 returning = do_return(eap, FALSE, TRUE, NULL);
5035 }
5036
Bram Moolenaare38eab22019-12-05 21:50:01 +01005037 // When skipping or the return gets pending, advance to the next command
5038 // in this line (!returning). Otherwise, ignore the rest of the line.
5039 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005040 if (returning)
5041 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005042 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005043 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005044
5045 if (eap->skip)
5046 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005047 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005048}
5049
5050/*
5051 * ":1,25call func(arg1, arg2)" function call.
5052 */
5053 void
5054ex_call(exarg_T *eap)
5055{
5056 char_u *arg = eap->arg;
5057 char_u *startarg;
5058 char_u *name;
5059 char_u *tofree;
5060 int len;
5061 typval_T rettv;
5062 linenr_T lnum;
5063 int doesrange;
5064 int failed = FALSE;
5065 funcdict_T fudi;
5066 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005067 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005068 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005069 int found_var = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005070
Bram Moolenaare6b53242020-07-01 17:28:33 +02005071 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005072 if (eap->skip)
5073 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005074 // trans_function_name() doesn't work well when skipping, use eval0()
5075 // instead to skip to any following command, e.g. for:
5076 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005077 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005078 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005079 clear_tv(&rettv);
5080 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005081 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005082 return;
5083 }
5084
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005085 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
5086 &fudi, &partial, in_vim9script() ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005087 if (fudi.fd_newkey != NULL)
5088 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005089 // Still need to give an error message for missing key.
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005090 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005091 vim_free(fudi.fd_newkey);
5092 }
5093 if (tofree == NULL)
5094 return;
5095
Bram Moolenaare38eab22019-12-05 21:50:01 +01005096 // Increase refcount on dictionary, it could get deleted when evaluating
5097 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005098 if (fudi.fd_dict != NULL)
5099 ++fudi.fd_dict->dv_refcount;
5100
Bram Moolenaare38eab22019-12-05 21:50:01 +01005101 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
5102 // contents. For VAR_PARTIAL get its partial, unless we already have one
5103 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005104 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005105 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005106 in_vim9script() && type == NULL ? &type : NULL, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005107
Bram Moolenaare38eab22019-12-05 21:50:01 +01005108 // Skip white space to allow ":call func ()". Not good, but required for
5109 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005110 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005111 if (*startarg != '(')
5112 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005113 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005114 goto end;
5115 }
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005116 if (in_vim9script() && startarg > arg)
5117 {
5118 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
5119 goto end;
5120 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005121
5122 /*
5123 * When skipping, evaluate the function once, to find the end of the
5124 * arguments.
5125 * When the function takes a range, this is discovered after the first
5126 * call, and the loop is broken.
5127 */
5128 if (eap->skip)
5129 {
5130 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005131 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005132 }
5133 else
5134 lnum = eap->line1;
5135 for ( ; lnum <= eap->line2; ++lnum)
5136 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005137 funcexe_T funcexe;
5138
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005139 if (!eap->skip && eap->addr_count > 0)
5140 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01005141 if (lnum > curbuf->b_ml.ml_line_count)
5142 {
5143 // If the function deleted lines or switched to another buffer
5144 // the line number may become invalid.
Bram Moolenaar108010a2021-06-27 22:03:33 +02005145 emsg(_(e_invalid_range));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01005146 break;
5147 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005148 curwin->w_cursor.lnum = lnum;
5149 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005150 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005151 }
5152 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005153
Bram Moolenaara80faa82020-04-12 19:37:17 +02005154 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005155 funcexe.fe_firstline = eap->line1;
5156 funcexe.fe_lastline = eap->line2;
5157 funcexe.fe_doesrange = &doesrange;
5158 funcexe.fe_evaluate = !eap->skip;
5159 funcexe.fe_partial = partial;
5160 funcexe.fe_selfdict = fudi.fd_dict;
5161 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005162 funcexe.fe_found_var = found_var;
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005163 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaare6b53242020-07-01 17:28:33 +02005164 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005165 {
5166 failed = TRUE;
5167 break;
5168 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01005169 if (has_watchexpr())
5170 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005171
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005172 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005173 if (handle_subscript(&arg, NULL, &rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005174 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005175 {
5176 failed = TRUE;
5177 break;
5178 }
5179
5180 clear_tv(&rettv);
5181 if (doesrange || eap->skip)
5182 break;
5183
Bram Moolenaare38eab22019-12-05 21:50:01 +01005184 // Stop when immediately aborting on error, or when an interrupt
5185 // occurred or an exception was thrown but not caught.
5186 // get_func_tv() returned OK, so that the check for trailing
5187 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005188 if (aborting())
5189 break;
5190 }
5191 if (eap->skip)
5192 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005193 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005194
Bram Moolenaar1d341892021-09-18 15:25:52 +02005195 // When inside :try we need to check for following "| catch" or "| endtry".
5196 // Not when there was an error, but do check if an exception was thrown.
5197 if ((!aborting() || did_throw)
5198 && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005199 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005200 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02005201 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02005202 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005203 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02005204 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005205 {
5206 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00005207 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005208 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005209 }
5210 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02005211 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005212 }
5213
5214end:
5215 dict_unref(fudi.fd_dict);
5216 vim_free(tofree);
5217}
5218
5219/*
5220 * Return from a function. Possibly makes the return pending. Also called
5221 * for a pending return at the ":endtry" or after returning from an extra
5222 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
5223 * when called due to a ":return" command. "rettv" may point to a typval_T
5224 * with the return rettv. Returns TRUE when the return can be carried out,
5225 * FALSE when the return gets pending.
5226 */
5227 int
5228do_return(
5229 exarg_T *eap,
5230 int reanimate,
5231 int is_cmd,
5232 void *rettv)
5233{
5234 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01005235 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005236
5237 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005238 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005239 current_funccal->returned = FALSE;
5240
5241 /*
5242 * Cleanup (and inactivate) conditionals, but stop when a try conditional
5243 * not in its finally clause (which then is to be executed next) is found.
5244 * In this case, make the ":return" pending for execution at the ":endtry".
5245 * Otherwise, return normally.
5246 */
5247 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
5248 if (idx >= 0)
5249 {
5250 cstack->cs_pending[idx] = CSTP_RETURN;
5251
5252 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005253 // A pending return again gets pending. "rettv" points to an
5254 // allocated variable with the rettv of the original ":return"'s
5255 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005256 cstack->cs_rettv[idx] = rettv;
5257 else
5258 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005259 // When undoing a return in order to make it pending, get the stored
5260 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005261 if (reanimate)
5262 rettv = current_funccal->rettv;
5263
5264 if (rettv != NULL)
5265 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005266 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005267 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
5268 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
5269 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005270 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005271 }
5272 else
5273 cstack->cs_rettv[idx] = NULL;
5274
5275 if (reanimate)
5276 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005277 // The pending return value could be overwritten by a ":return"
5278 // without argument in a finally clause; reset the default
5279 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005280 current_funccal->rettv->v_type = VAR_NUMBER;
5281 current_funccal->rettv->vval.v_number = 0;
5282 }
5283 }
5284 report_make_pending(CSTP_RETURN, rettv);
5285 }
5286 else
5287 {
5288 current_funccal->returned = TRUE;
5289
Bram Moolenaare38eab22019-12-05 21:50:01 +01005290 // If the return is carried out now, store the return value. For
5291 // a return immediately after reanimation, the value is already
5292 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005293 if (!reanimate && rettv != NULL)
5294 {
5295 clear_tv(current_funccal->rettv);
5296 *current_funccal->rettv = *(typval_T *)rettv;
5297 if (!is_cmd)
5298 vim_free(rettv);
5299 }
5300 }
5301
5302 return idx < 0;
5303}
5304
5305/*
5306 * Free the variable with a pending return value.
5307 */
5308 void
5309discard_pending_return(void *rettv)
5310{
5311 free_tv((typval_T *)rettv);
5312}
5313
5314/*
5315 * Generate a return command for producing the value of "rettv". The result
5316 * is an allocated string. Used by report_pending() for verbose messages.
5317 */
5318 char_u *
5319get_return_cmd(void *rettv)
5320{
5321 char_u *s = NULL;
5322 char_u *tofree = NULL;
5323 char_u numbuf[NUMBUFLEN];
5324
5325 if (rettv != NULL)
5326 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
5327 if (s == NULL)
5328 s = (char_u *)"";
5329
5330 STRCPY(IObuff, ":return ");
5331 STRNCPY(IObuff + 8, s, IOSIZE - 8);
5332 if (STRLEN(s) + 8 >= IOSIZE)
5333 STRCPY(IObuff + IOSIZE - 4, "...");
5334 vim_free(tofree);
5335 return vim_strsave(IObuff);
5336}
5337
5338/*
5339 * Get next function line.
5340 * Called by do_cmdline() to get the next line.
5341 * Returns allocated string, or NULL for end of function.
5342 */
5343 char_u *
5344get_func_line(
5345 int c UNUSED,
5346 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02005347 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005348 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005349{
5350 funccall_T *fcp = (funccall_T *)cookie;
5351 ufunc_T *fp = fcp->func;
5352 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005353 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005354
Bram Moolenaare38eab22019-12-05 21:50:01 +01005355 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005356 if (fcp->dbg_tick != debug_tick)
5357 {
5358 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005359 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005360 fcp->dbg_tick = debug_tick;
5361 }
5362#ifdef FEAT_PROFILE
5363 if (do_profiling == PROF_YES)
5364 func_line_end(cookie);
5365#endif
5366
5367 gap = &fp->uf_lines;
5368 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5369 || fcp->returned)
5370 retval = NULL;
5371 else
5372 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005373 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005374 while (fcp->linenr < gap->ga_len
5375 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
5376 ++fcp->linenr;
5377 if (fcp->linenr >= gap->ga_len)
5378 retval = NULL;
5379 else
5380 {
5381 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005382 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005383#ifdef FEAT_PROFILE
5384 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005385 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005386#endif
5387 }
5388 }
5389
Bram Moolenaare38eab22019-12-05 21:50:01 +01005390 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005391 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005392 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005393 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01005394 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005395 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005396 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005397 fcp->dbg_tick = debug_tick;
5398 }
5399
5400 return retval;
5401}
5402
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005403/*
5404 * Return TRUE if the currently active function should be ended, because a
5405 * return was encountered or an error occurred. Used inside a ":while".
5406 */
5407 int
5408func_has_ended(void *cookie)
5409{
5410 funccall_T *fcp = (funccall_T *)cookie;
5411
Bram Moolenaare38eab22019-12-05 21:50:01 +01005412 // Ignore the "abort" flag if the abortion behavior has been changed due to
5413 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005414 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5415 || fcp->returned);
5416}
5417
5418/*
5419 * return TRUE if cookie indicates a function which "abort"s on errors.
5420 */
5421 int
5422func_has_abort(
5423 void *cookie)
5424{
5425 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
5426}
5427
5428
5429/*
5430 * Turn "dict.Func" into a partial for "Func" bound to "dict".
5431 * Don't do this when "Func" is already a partial that was bound
5432 * explicitly (pt_auto is FALSE).
5433 * Changes "rettv" in-place.
5434 * Returns the updated "selfdict_in".
5435 */
5436 dict_T *
5437make_partial(dict_T *selfdict_in, typval_T *rettv)
5438{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005439 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005440 char_u *tofree = NULL;
5441 ufunc_T *fp;
5442 char_u fname_buf[FLEN_FIXED + 1];
5443 int error;
5444 dict_T *selfdict = selfdict_in;
5445
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005446 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
5447 fp = rettv->vval.v_partial->pt_func;
5448 else
5449 {
5450 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
5451 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005452 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005453 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005454 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005455 vim_free(tofree);
5456 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005457
5458 if (fp != NULL && (fp->uf_flags & FC_DICT))
5459 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005460 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005461
5462 if (pt != NULL)
5463 {
5464 pt->pt_refcount = 1;
5465 pt->pt_dict = selfdict;
5466 pt->pt_auto = TRUE;
5467 selfdict = NULL;
5468 if (rettv->v_type == VAR_FUNC)
5469 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005470 // Just a function: Take over the function name and use
5471 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005472 pt->pt_name = rettv->vval.v_string;
5473 }
5474 else
5475 {
5476 partial_T *ret_pt = rettv->vval.v_partial;
5477 int i;
5478
Bram Moolenaare38eab22019-12-05 21:50:01 +01005479 // Partial: copy the function name, use selfdict and copy
5480 // args. Can't take over name or args, the partial might
5481 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005482 if (ret_pt->pt_name != NULL)
5483 {
5484 pt->pt_name = vim_strsave(ret_pt->pt_name);
5485 func_ref(pt->pt_name);
5486 }
5487 else
5488 {
5489 pt->pt_func = ret_pt->pt_func;
5490 func_ptr_ref(pt->pt_func);
5491 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005492 if (ret_pt->pt_argc > 0)
5493 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005494 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005495 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005496 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005497 pt->pt_argc = 0;
5498 else
5499 {
5500 pt->pt_argc = ret_pt->pt_argc;
5501 for (i = 0; i < pt->pt_argc; i++)
5502 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
5503 }
5504 }
5505 partial_unref(ret_pt);
5506 }
5507 rettv->v_type = VAR_PARTIAL;
5508 rettv->vval.v_partial = pt;
5509 }
5510 }
5511 return selfdict;
5512}
5513
5514/*
5515 * Return the name of the executed function.
5516 */
5517 char_u *
5518func_name(void *cookie)
5519{
5520 return ((funccall_T *)cookie)->func->uf_name;
5521}
5522
5523/*
5524 * Return the address holding the next breakpoint line for a funccall cookie.
5525 */
5526 linenr_T *
5527func_breakpoint(void *cookie)
5528{
5529 return &((funccall_T *)cookie)->breakpoint;
5530}
5531
5532/*
5533 * Return the address holding the debug tick for a funccall cookie.
5534 */
5535 int *
5536func_dbg_tick(void *cookie)
5537{
5538 return &((funccall_T *)cookie)->dbg_tick;
5539}
5540
5541/*
5542 * Return the nesting level for a funccall cookie.
5543 */
5544 int
5545func_level(void *cookie)
5546{
5547 return ((funccall_T *)cookie)->level;
5548}
5549
5550/*
5551 * Return TRUE when a function was ended by a ":return" command.
5552 */
5553 int
5554current_func_returned(void)
5555{
5556 return current_funccal->returned;
5557}
5558
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005559 int
5560free_unref_funccal(int copyID, int testing)
5561{
5562 int did_free = FALSE;
5563 int did_free_funccal = FALSE;
5564 funccall_T *fc, **pfc;
5565
5566 for (pfc = &previous_funccal; *pfc != NULL; )
5567 {
5568 if (can_free_funccal(*pfc, copyID))
5569 {
5570 fc = *pfc;
5571 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01005572 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005573 did_free = TRUE;
5574 did_free_funccal = TRUE;
5575 }
5576 else
5577 pfc = &(*pfc)->caller;
5578 }
5579 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005580 // When a funccal was freed some more items might be garbage
5581 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005582 (void)garbage_collect(testing);
5583
5584 return did_free;
5585}
5586
5587/*
Bram Moolenaarba209902016-08-24 22:06:38 +02005588 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005589 */
5590 static funccall_T *
5591get_funccal(void)
5592{
5593 int i;
5594 funccall_T *funccal;
5595 funccall_T *temp_funccal;
5596
5597 funccal = current_funccal;
5598 if (debug_backtrace_level > 0)
5599 {
5600 for (i = 0; i < debug_backtrace_level; i++)
5601 {
5602 temp_funccal = funccal->caller;
5603 if (temp_funccal)
5604 funccal = temp_funccal;
5605 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005606 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005607 debug_backtrace_level = i;
5608 }
5609 }
5610 return funccal;
5611}
5612
5613/*
5614 * Return the hashtable used for local variables in the current funccal.
5615 * Return NULL if there is no current funccal.
5616 */
5617 hashtab_T *
5618get_funccal_local_ht()
5619{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005620 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005621 return NULL;
5622 return &get_funccal()->l_vars.dv_hashtab;
5623}
5624
5625/*
5626 * Return the l: scope variable.
5627 * Return NULL if there is no current funccal.
5628 */
5629 dictitem_T *
5630get_funccal_local_var()
5631{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005632 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005633 return NULL;
5634 return &get_funccal()->l_vars_var;
5635}
5636
5637/*
5638 * Return the hashtable used for argument in the current funccal.
5639 * Return NULL if there is no current funccal.
5640 */
5641 hashtab_T *
5642get_funccal_args_ht()
5643{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005644 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005645 return NULL;
5646 return &get_funccal()->l_avars.dv_hashtab;
5647}
5648
5649/*
5650 * Return the a: scope variable.
5651 * Return NULL if there is no current funccal.
5652 */
5653 dictitem_T *
5654get_funccal_args_var()
5655{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005656 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005657 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01005658 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005659}
5660
5661/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005662 * List function variables, if there is a function.
5663 */
5664 void
5665list_func_vars(int *first)
5666{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005667 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005668 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01005669 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005670}
5671
5672/*
5673 * If "ht" is the hashtable for local variables in the current funccal, return
5674 * the dict that contains it.
5675 * Otherwise return NULL.
5676 */
5677 dict_T *
5678get_current_funccal_dict(hashtab_T *ht)
5679{
5680 if (current_funccal != NULL
5681 && ht == &current_funccal->l_vars.dv_hashtab)
5682 return &current_funccal->l_vars;
5683 return NULL;
5684}
5685
5686/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005687 * Search hashitem in parent scope.
5688 */
5689 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005690find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005691{
5692 funccall_T *old_current_funccal = current_funccal;
5693 hashtab_T *ht;
5694 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005695 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005696
5697 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5698 return NULL;
5699
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02005700 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005701 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02005702 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005703 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005704 ht = find_var_ht(name, &varname);
5705 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02005706 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005707 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02005708 if (!HASHITEM_EMPTY(hi))
5709 {
5710 *pht = ht;
5711 break;
5712 }
5713 }
5714 if (current_funccal == current_funccal->func->uf_scoped)
5715 break;
5716 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005717 }
5718 current_funccal = old_current_funccal;
5719
5720 return hi;
5721}
5722
5723/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005724 * Search variable in parent scope.
5725 */
5726 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005727find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005728{
5729 dictitem_T *v = NULL;
5730 funccall_T *old_current_funccal = current_funccal;
5731 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005732 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005733
5734 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5735 return NULL;
5736
Bram Moolenaare38eab22019-12-05 21:50:01 +01005737 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005738 current_funccal = current_funccal->func->uf_scoped;
5739 while (current_funccal)
5740 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005741 ht = find_var_ht(name, &varname);
5742 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005743 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005744 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005745 if (v != NULL)
5746 break;
5747 }
5748 if (current_funccal == current_funccal->func->uf_scoped)
5749 break;
5750 current_funccal = current_funccal->func->uf_scoped;
5751 }
5752 current_funccal = old_current_funccal;
5753
5754 return v;
5755}
5756
5757/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005758 * Set "copyID + 1" in previous_funccal and callers.
5759 */
5760 int
5761set_ref_in_previous_funccal(int copyID)
5762{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005763 funccall_T *fc;
5764
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005765 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005766 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005767 fc->fc_copyID = copyID + 1;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005768 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
5769 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
5770 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL))
5771 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005772 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005773 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005774}
5775
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005776 static int
5777set_ref_in_funccal(funccall_T *fc, int copyID)
5778{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005779 if (fc->fc_copyID != copyID)
5780 {
5781 fc->fc_copyID = copyID;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005782 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
5783 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
5784 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
5785 || set_ref_in_func(NULL, fc->func, copyID))
5786 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005787 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005788 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005789}
5790
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005791/*
5792 * Set "copyID" in all local vars and arguments in the call stack.
5793 */
5794 int
5795set_ref_in_call_stack(int copyID)
5796{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005797 funccall_T *fc;
5798 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005799
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005800 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5801 if (set_ref_in_funccal(fc, copyID))
5802 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005803
5804 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005805 for (entry = funccal_stack; entry != NULL; entry = entry->next)
5806 for (fc = entry->top_funccal; fc != NULL; fc = fc->caller)
5807 if (set_ref_in_funccal(fc, copyID))
5808 return TRUE;
5809 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005810}
5811
5812/*
5813 * Set "copyID" in all functions available by name.
5814 */
5815 int
5816set_ref_in_functions(int copyID)
5817{
5818 int todo;
5819 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005820 ufunc_T *fp;
5821
5822 todo = (int)func_hashtab.ht_used;
5823 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005824 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005825 if (!HASHITEM_EMPTY(hi))
5826 {
5827 --todo;
5828 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005829 if (!func_name_refcount(fp->uf_name)
5830 && set_ref_in_func(NULL, fp, copyID))
5831 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005832 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005833 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005834 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005835}
5836
5837/*
5838 * Set "copyID" in all function arguments.
5839 */
5840 int
5841set_ref_in_func_args(int copyID)
5842{
5843 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005844
5845 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005846 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5847 copyID, NULL, NULL))
5848 return TRUE;
5849 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005850}
5851
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005852/*
5853 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005854 * Returns TRUE if setting references failed somehow.
5855 */
5856 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005857set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005858{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005859 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005860 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005861 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005862 char_u fname_buf[FLEN_FIXED + 1];
5863 char_u *tofree = NULL;
5864 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005865 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005866
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005867 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005868 return FALSE;
5869
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005870 if (fp_in == NULL)
5871 {
5872 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005873 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005874 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005875 if (fp != NULL)
5876 {
5877 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005878 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005879 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005880
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005881 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005882 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005883}
5884
Bram Moolenaare38eab22019-12-05 21:50:01 +01005885#endif // FEAT_EVAL