blob: b7f9dfcba047444b8039e5a70ee85b74cca6a0b7 [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 {
342 emsg(_("E989: 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 Moolenaar32b3f822021-01-06 21:59:39 +01001592 v = find_var(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
1611 // imported variable from another script
1612 if (import != NULL)
1613 {
1614 if (import->imp_funcname != NULL)
1615 {
1616 s = import->imp_funcname;
1617 *lenp = (int)STRLEN(s);
1618 return s;
1619 }
Bram Moolenaarf8a79fc2021-12-14 12:06:16 +00001620 if (import->imp_flags & IMP_FLAGS_STAR)
1621 {
1622 name[len] = NUL;
1623 semsg(_(e_cannot_use_str_itself_it_is_imported_with_star),
1624 name);
1625 name[len] = cc;
1626 *lenp = 0;
1627 return (char_u *)""; // just in case
1628 }
1629 else
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001630 {
1631 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
1632 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1633 + import->imp_var_vals_idx;
1634 tv = sv->sv_tv;
1635 if (type != NULL)
1636 *type = sv->sv_type;
1637 did_type = TRUE;
1638 }
1639 }
1640 }
1641
1642 if (tv != NULL)
1643 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001644 if (found_var != NULL)
1645 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001646 if (tv->v_type == VAR_FUNC)
1647 {
1648 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001649 {
1650 *lenp = 0;
1651 return (char_u *)""; // just in case
1652 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001653 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001654 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001655 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001656
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001657 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001658 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001659 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001660
1661 if (pt == NULL)
1662 {
1663 *lenp = 0;
1664 return (char_u *)""; // just in case
1665 }
1666 if (partialp != NULL)
1667 *partialp = pt;
1668 s = partial_name(pt);
1669 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001670 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001671
1672 if (s != NULL)
1673 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001674 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001675 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001676 svar_T *sv = find_typval_in_script(tv);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001677
1678 if (sv != NULL)
1679 *type = sv->sv_type;
1680 }
1681 return s;
1682 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001683 }
1684
1685 return name;
1686}
1687
1688/*
1689 * Give an error message with a function name. Handle <SNR> things.
1690 * "ermsg" is to be passed without translation, use N_() instead of _().
1691 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001692 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001693emsg_funcname(char *ermsg, char_u *name)
1694{
1695 char_u *p;
1696
1697 if (*name == K_SPECIAL)
1698 p = concat_str((char_u *)"<SNR>", name + 3);
1699 else
1700 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001701 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001702 if (p != name)
1703 vim_free(p);
1704}
1705
1706/*
1707 * Allocate a variable for the result of a function.
1708 * Return OK or FAIL.
1709 */
1710 int
1711get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001712 char_u *name, // name of the function
1713 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001714 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001715 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +02001716 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001717 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001718{
1719 char_u *argp;
1720 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001721 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1722 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001723 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001724
1725 /*
1726 * Get the arguments.
1727 */
1728 argp = *arg;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001729 while (argcount < MAX_FUNC_ARGS - (funcexe->fe_partial == NULL ? 0
1730 : funcexe->fe_partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001731 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001732 // skip the '(' or ',' and possibly line breaks
1733 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1734
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001735 if (*argp == ')' || *argp == ',' || *argp == NUL)
1736 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001737 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001738 {
1739 ret = FAIL;
1740 break;
1741 }
1742 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001743 // The comma should come right after the argument, but this wasn't
1744 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001745 if (vim9script)
1746 {
1747 if (*argp != ',' && *skipwhite(argp) == ',')
1748 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001749 semsg(_(e_no_white_space_allowed_before_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001750 ret = FAIL;
1751 break;
1752 }
1753 }
1754 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001755 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001756 if (*argp != ',')
1757 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001758 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1759 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001760 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001761 ret = FAIL;
1762 break;
1763 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001764 }
Bram Moolenaare6b53242020-07-01 17:28:33 +02001765 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001766 if (*argp == ')')
1767 ++argp;
1768 else
1769 ret = FAIL;
1770
1771 if (ret == OK)
1772 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001773 int i = 0;
1774 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001775
1776 if (get_vim_var_nr(VV_TESTING))
1777 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001778 // Prepare for calling test_garbagecollect_now(), need to know
1779 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001780 if (funcargs.ga_itemsize == 0)
1781 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
1782 for (i = 0; i < argcount; ++i)
1783 if (ga_grow(&funcargs, 1) == OK)
1784 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1785 &argvars[i];
1786 }
1787
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001788 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001789 if (in_vim9script() && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001790 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001791 // An error in a builtin function does not return FAIL, but we do
1792 // want to abort further processing if an error was given.
1793 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001794 clear_tv(rettv);
1795 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001796
1797 funcargs.ga_len -= i;
1798 }
1799 else if (!aborting())
1800 {
1801 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001802 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001803 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001804 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 }
1806
1807 while (--argcount >= 0)
1808 clear_tv(&argvars[argcount]);
1809
Bram Moolenaar8294d492020-08-10 22:40:56 +02001810 if (in_vim9script())
1811 *arg = argp;
1812 else
1813 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001814 return ret;
1815}
1816
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001817/*
1818 * Return TRUE if "p" starts with "<SID>" or "s:".
1819 * Only works if eval_fname_script() returned non-zero for "p"!
1820 */
1821 static int
1822eval_fname_sid(char_u *p)
1823{
1824 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1825}
1826
1827/*
1828 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1829 * Change <SNR>123_name() to K_SNR 123_name().
1830 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
1831 * (slow).
1832 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001833 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001834fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
1835{
1836 int llen;
1837 char_u *fname;
1838 int i;
1839
1840 llen = eval_fname_script(name);
1841 if (llen > 0)
1842 {
1843 fname_buf[0] = K_SPECIAL;
1844 fname_buf[1] = KS_EXTRA;
1845 fname_buf[2] = (int)KE_SNR;
1846 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001847 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001848 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001849 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001850 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001851 else
1852 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001853 sprintf((char *)fname_buf + 3, "%ld_",
1854 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001855 i = (int)STRLEN(fname_buf);
1856 }
1857 }
1858 if (i + STRLEN(name + llen) < FLEN_FIXED)
1859 {
1860 STRCPY(fname_buf + i, name + llen);
1861 fname = fname_buf;
1862 }
1863 else
1864 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001865 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001866 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +01001867 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001868 else
1869 {
1870 *tofree = fname;
1871 mch_memmove(fname, fname_buf, (size_t)i);
1872 STRCPY(fname + i, name + llen);
1873 }
1874 }
1875 }
1876 else
1877 fname = name;
1878 return fname;
1879}
1880
1881/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882 * Find a function "name" in script "sid".
1883 */
1884 static ufunc_T *
1885find_func_with_sid(char_u *name, int sid)
1886{
1887 hashitem_T *hi;
1888 char_u buffer[200];
1889
1890 buffer[0] = K_SPECIAL;
1891 buffer[1] = KS_EXTRA;
1892 buffer[2] = (int)KE_SNR;
1893 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
1894 (long)sid, name);
1895 hi = hash_find(&func_hashtab, buffer);
1896 if (!HASHITEM_EMPTY(hi))
1897 return HI2UF(hi);
1898
1899 return NULL;
1900}
1901
1902/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001903 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001904 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001905 * Return NULL for unknown function.
1906 */
Bram Moolenaarce658352020-07-31 23:47:12 +02001907 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001908find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001909{
1910 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001911 ufunc_T *func;
1912 imported_T *imported;
1913
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001914 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001915 {
Bram Moolenaar333894b2020-08-01 18:53:07 +02001916 char_u *after_script = NULL;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001917 long sid = 0;
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001918 int find_script_local = in_vim9script() && eval_isnamec1(*name)
1919 && (name[1] != ':' || *name == 's');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920
Bram Moolenaar95006e32020-08-29 17:47:08 +02001921 if (find_script_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001923 // Find script-local function before global one.
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001924 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
1925 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001926 if (func != NULL)
1927 return func;
1928 }
1929
Bram Moolenaarefa94442020-08-08 22:16:00 +02001930 if (name[0] == K_SPECIAL
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001931 && name[1] == KS_EXTRA
1932 && name[2] == KE_SNR)
1933 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001934 // Caller changes s: to <SNR>99_name.
1935
1936 after_script = name + 3;
1937 sid = getdigits(&after_script);
Bram Moolenaarefa94442020-08-08 22:16:00 +02001938 if (*after_script == '_')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001939 ++after_script;
1940 else
1941 after_script = NULL;
1942 }
Bram Moolenaar95006e32020-08-29 17:47:08 +02001943 if (find_script_local || after_script != NULL)
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001944 {
1945 // Find imported function before global one.
Bram Moolenaarefa94442020-08-08 22:16:00 +02001946 if (after_script != NULL && sid != current_sctx.sc_sid)
1947 imported = find_imported_in_script(after_script, 0, sid);
1948 else
1949 imported = find_imported(after_script == NULL
1950 ? name : after_script, 0, cctx);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001951 if (imported != NULL && imported->imp_funcname != NULL)
1952 {
1953 hi = hash_find(&func_hashtab, imported->imp_funcname);
1954 if (!HASHITEM_EMPTY(hi))
1955 return HI2UF(hi);
1956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 }
1958 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001959
Bram Moolenaara26b9702020-04-18 19:53:28 +02001960 hi = hash_find(&func_hashtab,
1961 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001962 if (!HASHITEM_EMPTY(hi))
1963 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964
1965 return NULL;
1966}
1967
1968/*
1969 * Find a function by name, return pointer to it in ufuncs.
1970 * "cctx" is passed in a :def function to find imported functions.
1971 * Return NULL for unknown or dead function.
1972 */
1973 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001974find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001976 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977
1978 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
1979 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001980 return NULL;
1981}
1982
1983/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02001984 * Return TRUE if "ufunc" is a global function.
1985 */
1986 int
1987func_is_global(ufunc_T *ufunc)
1988{
1989 return ufunc->uf_name[0] != K_SPECIAL;
1990}
1991
1992/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001993 * Copy the function name of "fp" to buffer "buf".
1994 * "buf" must be able to hold the function name plus three bytes.
1995 * Takes care of script-local function names.
1996 */
1997 static void
1998cat_func_name(char_u *buf, ufunc_T *fp)
1999{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002000 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002001 {
2002 STRCPY(buf, "<SNR>");
2003 STRCAT(buf, fp->uf_name + 3);
2004 }
2005 else
2006 STRCPY(buf, fp->uf_name);
2007}
2008
2009/*
2010 * Add a number variable "name" to dict "dp" with value "nr".
2011 */
2012 static void
2013add_nr_var(
2014 dict_T *dp,
2015 dictitem_T *v,
2016 char *name,
2017 varnumber_T nr)
2018{
2019 STRCPY(v->di_key, name);
2020 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2021 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
2022 v->di_tv.v_type = VAR_NUMBER;
2023 v->di_tv.v_lock = VAR_FIXED;
2024 v->di_tv.vval.v_number = nr;
2025}
2026
2027/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002028 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002029 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002030 static void
2031free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002032{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002033 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002034
2035 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2036 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002037 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002038
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002039 // When garbage collecting a funccall_T may be freed before the
2040 // function that references it, clear its uf_scoped field.
2041 // The function may have been redefined and point to another
2042 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002043 if (fp != NULL && fp->uf_scoped == fc)
2044 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002045 }
Bram Moolenaar58016442016-07-31 18:30:22 +02002046 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002047
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002048 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002049 vim_free(fc);
2050}
2051
2052/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002053 * Free "fc" and what it contains.
2054 * Can be called only when "fc" is kept beyond the period of it called,
2055 * i.e. after cleanup_function_call(fc).
2056 */
2057 static void
2058free_funccal_contents(funccall_T *fc)
2059{
2060 listitem_T *li;
2061
2062 // Free all l: variables.
2063 vars_clear(&fc->l_vars.dv_hashtab);
2064
2065 // Free all a: variables.
2066 vars_clear(&fc->l_avars.dv_hashtab);
2067
2068 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002069 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002070 clear_tv(&li->li_tv);
2071
2072 free_funccal(fc);
2073}
2074
2075/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002076 * Handle the last part of returning from a function: free the local hashtable.
2077 * Unless it is still in use by a closure.
2078 */
2079 static void
2080cleanup_function_call(funccall_T *fc)
2081{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002082 int may_free_fc = fc->fc_refcount <= 0;
2083 int free_fc = TRUE;
2084
Bram Moolenaar6914c642017-04-01 21:21:30 +02002085 current_funccal = fc->caller;
2086
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002087 // Free all l: variables if not referred.
2088 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
2089 vars_clear(&fc->l_vars.dv_hashtab);
2090 else
2091 free_fc = FALSE;
2092
2093 // If the a:000 list and the l: and a: dicts are not referenced and
2094 // there is no closure using it, we can free the funccall_T and what's
2095 // in it.
2096 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
2097 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002098 else
2099 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002100 int todo;
2101 hashitem_T *hi;
2102 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002103
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002104 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002105
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002106 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002107 todo = (int)fc->l_avars.dv_hashtab.ht_used;
2108 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
2109 {
2110 if (!HASHITEM_EMPTY(hi))
2111 {
2112 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002113 di = HI2DI(hi);
2114 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002115 }
2116 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002117 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002118
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002119 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2120 fc->l_varlist.lv_first = NULL;
2121 else
2122 {
2123 listitem_T *li;
2124
2125 free_fc = FALSE;
2126
2127 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002128 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002129 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002130 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002131
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002132 if (free_fc)
2133 free_funccal(fc);
2134 else
2135 {
2136 static int made_copy = 0;
2137
2138 // "fc" is still in use. This can happen when returning "a:000",
2139 // assigning "l:" to a global variable or defining a closure.
2140 // Link "fc" in the list for garbage collection later.
2141 fc->caller = previous_funccal;
2142 previous_funccal = fc;
2143
2144 if (want_garbage_collect)
2145 // If garbage collector is ready, clear count.
2146 made_copy = 0;
2147 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002148 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002149 // We have made a lot of copies, worth 4 Mbyte. This can happen
2150 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002151 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002152 // too much memory.
2153 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002154 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002155 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002156 }
2157}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002158
2159/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002160 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2161 */
2162 static int
2163numbered_function(char_u *name)
2164{
2165 return isdigit(*name)
2166 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2167}
2168
2169/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002170 * There are two kinds of function names:
2171 * 1. ordinary names, function defined with :function or :def
2172 * 2. numbered functions and lambdas
2173 * For the first we only count the name stored in func_hashtab as a reference,
2174 * using function() does not count as a reference, because the function is
2175 * looked up by name.
2176 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002177 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002178func_name_refcount(char_u *name)
2179{
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002180 return numbered_function(name) || *name == '<';
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002181}
2182
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002183/*
2184 * Unreference "fc": decrement the reference count and free it when it
2185 * becomes zero. "fp" is detached from "fc".
2186 * When "force" is TRUE we are exiting.
2187 */
2188 static void
2189funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2190{
2191 funccall_T **pfc;
2192 int i;
2193
2194 if (fc == NULL)
2195 return;
2196
2197 if (--fc->fc_refcount <= 0 && (force || (
2198 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
2199 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
2200 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2201 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
2202 {
2203 if (fc == *pfc)
2204 {
2205 *pfc = fc->caller;
2206 free_funccal_contents(fc);
2207 return;
2208 }
2209 }
2210 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2211 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
2212 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
2213}
2214
2215/*
2216 * Remove the function from the function hashtable. If the function was
2217 * deleted while it still has references this was already done.
2218 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2219 */
2220 static int
2221func_remove(ufunc_T *fp)
2222{
2223 hashitem_T *hi;
2224
2225 // Return if it was already virtually deleted.
2226 if (fp->uf_flags & FC_DEAD)
2227 return FALSE;
2228
2229 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
2230 if (!HASHITEM_EMPTY(hi))
2231 {
2232 // When there is a def-function index do not actually remove the
2233 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002234 // Do remove it when it's a copy.
2235 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaarc970e422021-03-17 15:03:04 +01002236 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002238 return FALSE;
2239 }
2240 hash_remove(&func_hashtab, hi);
2241 fp->uf_flags |= FC_DELETED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 return TRUE;
2243 }
2244 return FALSE;
2245}
2246
2247 static void
2248func_clear_items(ufunc_T *fp)
2249{
2250 ga_clear_strings(&(fp->uf_args));
2251 ga_clear_strings(&(fp->uf_def_args));
2252 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002254 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002255 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002256 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002257
2258 // Increment the refcount of this function to avoid it being freed
2259 // recursively when the partial is freed.
2260 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002261 partial_unref(fp->uf_partial);
2262 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002263 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002264
2265#ifdef FEAT_LUA
2266 if (fp->uf_cb_free != NULL)
2267 {
2268 fp->uf_cb_free(fp->uf_cb_state);
2269 fp->uf_cb_free = NULL;
2270 }
2271
2272 fp->uf_cb_state = NULL;
2273 fp->uf_cb = NULL;
2274#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275#ifdef FEAT_PROFILE
2276 VIM_CLEAR(fp->uf_tml_count);
2277 VIM_CLEAR(fp->uf_tml_total);
2278 VIM_CLEAR(fp->uf_tml_self);
2279#endif
2280}
2281
2282/*
2283 * Free all things that a function contains. Does not free the function
2284 * itself, use func_free() for that.
2285 * When "force" is TRUE we are exiting.
2286 */
2287 static void
2288func_clear(ufunc_T *fp, int force)
2289{
2290 if (fp->uf_cleared)
2291 return;
2292 fp->uf_cleared = TRUE;
2293
2294 // clear this function
2295 func_clear_items(fp);
2296 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002297 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298}
2299
2300/*
2301 * Free a function and remove it from the list of functions. Does not free
2302 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002303 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002304 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002306 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002307func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308{
2309 // Only remove it when not done already, otherwise we would remove a newer
2310 // version of the function with the same name.
2311 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2312 func_remove(fp);
2313
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002314 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002315 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002316 if (fp->uf_dfunc_idx > 0)
2317 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002318 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002320 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002321 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002322 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323}
2324
2325/*
2326 * Free all things that a function contains and free the function itself.
2327 * When "force" is TRUE we are exiting.
2328 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002329 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330func_clear_free(ufunc_T *fp, int force)
2331{
2332 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002333 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2334 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002335 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002336 else
2337 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338}
2339
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002340/*
2341 * Copy already defined function "lambda" to a new function with name "global".
2342 * This is for when a compiled function defines a global function.
2343 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002344 int
2345copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002346{
2347 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002348 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002349
2350 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002351 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002352 semsg(_(e_lambda_function_not_found_str), lambda);
2353 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002354 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002355
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002356 fp = find_func(global, TRUE, NULL);
2357 if (fp != NULL)
2358 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002359 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002360 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002361 return FAIL;
2362 }
2363
2364 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
2365 if (fp == NULL)
2366 return FAIL;
2367
2368 fp->uf_varargs = ufunc->uf_varargs;
2369 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2370 fp->uf_def_status = ufunc->uf_def_status;
2371 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2372 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2373 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2374 == FAIL
2375 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2376 goto failed;
2377
2378 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
2379 : vim_strsave(ufunc->uf_name_exp);
2380 if (ufunc->uf_arg_types != NULL)
2381 {
2382 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2383 if (fp->uf_arg_types == NULL)
2384 goto failed;
2385 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2386 sizeof(type_T *) * fp->uf_args.ga_len);
2387 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002388 if (ufunc->uf_va_name != NULL)
2389 {
2390 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2391 if (fp->uf_va_name == NULL)
2392 goto failed;
2393 }
2394 fp->uf_ret_type = ufunc->uf_ret_type;
2395
2396 fp->uf_refcount = 1;
2397 STRCPY(fp->uf_name, global);
2398 hash_add(&func_hashtab, UF2HIKEY(fp));
2399
2400 // the referenced dfunc_T is now used one more time
2401 link_def_function(fp);
2402
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002403 // Create a partial to store the context of the function where it was
2404 // instantiated. Only needs to be done once. Do this on the original
2405 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002406 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2407 {
2408 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2409
2410 if (pt == NULL)
2411 goto failed;
2412 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002413 {
2414 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002415 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002416 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002417 ufunc->uf_partial = pt;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002418 --pt->pt_refcount; // not actually referenced here
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002419 }
2420
2421 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002422
2423failed:
2424 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002425 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002426}
2427
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002428static int funcdepth = 0;
2429
2430/*
2431 * Increment the function call depth count.
2432 * Return FAIL when going over 'maxfuncdepth'.
2433 * Otherwise return OK, must call funcdepth_decrement() later!
2434 */
2435 int
2436funcdepth_increment(void)
2437{
2438 if (funcdepth >= p_mfd)
2439 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002440 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002441 return FAIL;
2442 }
2443 ++funcdepth;
2444 return OK;
2445}
2446
2447 void
2448funcdepth_decrement(void)
2449{
2450 --funcdepth;
2451}
2452
2453/*
2454 * Get the current function call depth.
2455 */
2456 int
2457funcdepth_get(void)
2458{
2459 return funcdepth;
2460}
2461
2462/*
2463 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002464 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002465 * times as funcdepth_increment().
2466 */
2467 void
2468funcdepth_restore(int depth)
2469{
2470 funcdepth = depth;
2471}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002472
2473/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002474 * Call a user function.
2475 */
2476 static void
2477call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002478 ufunc_T *fp, // pointer to function
2479 int argcount, // nr of args
2480 typval_T *argvars, // arguments
2481 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002482 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002483 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002484{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002485 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002486 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002487 funccall_T *fc;
2488 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002489 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002490 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002491 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002492 int i;
2493 int ai;
2494 int islambda = FALSE;
2495 char_u numbuf[NUMBUFLEN];
2496 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002497 typval_T *tv_to_free[MAX_FUNC_ARGS];
2498 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002499#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002500 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002501#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002502 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002503
Bram Moolenaarb2049902021-01-24 12:53:53 +01002504#ifdef FEAT_PROFILE
2505 CLEAR_FIELD(profile_info);
2506#endif
2507
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002508 // If depth of calling is getting too high, don't execute the function.
2509 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002510 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002511 rettv->v_type = VAR_NUMBER;
2512 rettv->vval.v_number = -1;
2513 return;
2514 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002515
Bram Moolenaare38eab22019-12-05 21:50:01 +01002516 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002517
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002518 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002519 if (fc == NULL)
2520 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002521 fc->caller = current_funccal;
2522 current_funccal = fc;
2523 fc->func = fp;
2524 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002525 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002526 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002527 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2528 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002529 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002530 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002531 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002532
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002533 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002535#ifdef FEAT_PROFILE
2536 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2537#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002538 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002539#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002540 if (do_profiling == PROF_YES)
2541 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002542#endif
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002543 call_def_function(fp, argcount, argvars, funcexe->fe_partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002544 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002545#ifdef FEAT_PROFILE
2546 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002547 || (caller != NULL && caller->uf_profiling)))
2548 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002549#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 free_funccal(fc);
2552 return;
2553 }
2554
Bram Moolenaar38453522021-11-28 22:00:12 +00002555 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002556
2557 /*
2558 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
2559 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
2560 * each argument variable and saves a lot of time.
2561 */
2562 /*
2563 * Init l: variables.
2564 */
2565 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
2566 if (selfdict != NULL)
2567 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002568 // Set l:self to "selfdict". Use "name" to avoid a warning from
2569 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002570 v = &fc->fixvar[fixvar_idx++].var;
2571 name = v->di_key;
2572 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002573 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002574 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
2575 v->di_tv.v_type = VAR_DICT;
2576 v->di_tv.v_lock = 0;
2577 v->di_tv.vval.v_dict = selfdict;
2578 ++selfdict->dv_refcount;
2579 }
2580
2581 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002582 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002583 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002584 * Set a:000 to a list with room for the "..." arguments.
2585 */
2586 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002587 if ((fp->uf_flags & FC_NOARGS) == 0)
2588 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002589 (varnumber_T)(argcount >= fp->uf_args.ga_len
2590 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01002591 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002592 if ((fp->uf_flags & FC_NOARGS) == 0)
2593 {
2594 // Use "name" to avoid a warning from some compiler that checks the
2595 // destination size.
2596 v = &fc->fixvar[fixvar_idx++].var;
2597 name = v->di_key;
2598 STRCPY(name, "000");
2599 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2600 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
2601 v->di_tv.v_type = VAR_LIST;
2602 v->di_tv.v_lock = VAR_FIXED;
2603 v->di_tv.vval.v_list = &fc->l_varlist;
2604 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02002605 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002606 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2607 fc->l_varlist.lv_lock = VAR_FIXED;
2608
2609 /*
2610 * Set a:firstline to "firstline" and a:lastline to "lastline".
2611 * Set a:name to named arguments.
2612 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002613 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002614 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002615 if ((fp->uf_flags & FC_NOARGS) == 0)
2616 {
2617 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002618 (varnumber_T)funcexe->fe_firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002619 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002620 (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002621 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002622 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002623 {
2624 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002625 typval_T def_rettv;
2626 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002627
2628 ai = i - fp->uf_args.ga_len;
2629 if (ai < 0)
2630 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002631 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002632 name = FUNCARG(fp, i);
2633 if (islambda)
2634 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002635
2636 // evaluate named argument default expression
2637 isdefault = ai + fp->uf_def_args.ga_len >= 0
2638 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2639 && argvars[i].vval.v_number == VVAL_NONE));
2640 if (isdefault)
2641 {
2642 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002643
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002644 def_rettv.v_type = VAR_NUMBER;
2645 def_rettv.vval.v_number = -1;
2646
2647 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2648 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002649 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002650 {
2651 default_arg_err = 1;
2652 break;
2653 }
2654 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002655 }
2656 else
2657 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002658 if ((fp->uf_flags & FC_NOARGS) != 0)
2659 // Bail out if no a: arguments used (in lambda).
2660 break;
2661
Bram Moolenaare38eab22019-12-05 21:50:01 +01002662 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002663 sprintf((char *)numbuf, "%d", ai + 1);
2664 name = numbuf;
2665 }
2666 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2667 {
2668 v = &fc->fixvar[fixvar_idx++].var;
2669 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002670 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002671 }
2672 else
2673 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002674 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002675 if (v == NULL)
2676 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002677 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002678 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002679
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002680 // Note: the values are copied directly to avoid alloc/free.
2681 // "argvars" must have VAR_FIXED for v_lock.
2682 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002683 v->di_tv.v_lock = VAR_FIXED;
2684
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002685 if (isdefault)
2686 // Need to free this later, no matter where it's stored.
2687 tv_to_free[tv_to_free_len++] = &v->di_tv;
2688
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002689 if (addlocal)
2690 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002691 // Named arguments should be accessed without the "a:" prefix in
2692 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002693 copy_tv(&v->di_tv, &v->di_tv);
2694 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002695 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002696 else
2697 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002698
2699 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2700 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002701 listitem_T *li = &fc->l_listitems[ai];
2702
2703 li->li_tv = argvars[i];
2704 li->li_tv.v_lock = VAR_FIXED;
2705 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002706 }
2707 }
2708
Bram Moolenaare38eab22019-12-05 21:50:01 +01002709 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002710 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002711
2712 if (fp->uf_flags & FC_SANDBOX)
2713 {
2714 using_sandbox = TRUE;
2715 ++sandbox;
2716 }
2717
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002718 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002719 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002720 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002721 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002722 ++no_wait_return;
2723 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002724
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002725 smsg(_("calling %s"), SOURCING_NAME);
2726 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002727 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002728 char_u buf[MSG_BUF_LEN];
2729 char_u numbuf2[NUMBUFLEN];
2730 char_u *tofree;
2731 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002732
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002733 msg_puts("(");
2734 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002735 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002736 if (i > 0)
2737 msg_puts(", ");
2738 if (argvars[i].v_type == VAR_NUMBER)
2739 msg_outnum((long)argvars[i].vval.v_number);
2740 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002741 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002742 // Do not want errors such as E724 here.
2743 ++emsg_off;
2744 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2745 --emsg_off;
2746 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002747 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002748 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002749 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002750 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2751 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002752 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002753 msg_puts((char *)s);
2754 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002755 }
2756 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002757 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002758 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002759 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002760 msg_puts("\n"); // don't overwrite this either
2761
2762 verbose_leave_scroll();
2763 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002764 }
2765#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002766 if (do_profiling == PROF_YES)
2767 profile_may_start_func(&profile_info, fp,
2768 fc->caller == NULL ? NULL : fc->caller->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002769#endif
2770
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002771 save_current_sctx = current_sctx;
2772 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002773 save_did_emsg = did_emsg;
2774 did_emsg = FALSE;
2775
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002776 if (default_arg_err && (fp->uf_flags & FC_ABORT))
2777 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002778 else if (islambda)
2779 {
2780 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
2781
2782 // A Lambda always has the command "return {expr}". It is much faster
2783 // to evaluate {expr} directly.
2784 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002785 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002786 --ex_nesting_level;
2787 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002788 else
2789 // call do_cmdline() to execute the lines
2790 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002791 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
2792
2793 --RedrawingDisabled;
2794
Bram Moolenaare38eab22019-12-05 21:50:01 +01002795 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002796 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
2797 {
2798 clear_tv(rettv);
2799 rettv->v_type = VAR_NUMBER;
2800 rettv->vval.v_number = -1;
2801 }
2802
2803#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002804 if (do_profiling == PROF_YES)
2805 {
2806 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2807
2808 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
2809 profile_may_end_func(&profile_info, fp, caller);
2810 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002811#endif
2812
Bram Moolenaare38eab22019-12-05 21:50:01 +01002813 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002814 if (p_verbose >= 12)
2815 {
2816 ++no_wait_return;
2817 verbose_enter_scroll();
2818
2819 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002820 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002822 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002823 (long)fc->rettv->vval.v_number);
2824 else
2825 {
2826 char_u buf[MSG_BUF_LEN];
2827 char_u numbuf2[NUMBUFLEN];
2828 char_u *tofree;
2829 char_u *s;
2830
Bram Moolenaare38eab22019-12-05 21:50:01 +01002831 // The value may be very long. Skip the middle part, so that we
2832 // have some idea how it starts and ends. smsg() would always
2833 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002834 ++emsg_off;
2835 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
2836 --emsg_off;
2837 if (s != NULL)
2838 {
2839 if (vim_strsize(s) > MSG_BUF_CLEN)
2840 {
2841 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2842 s = buf;
2843 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002844 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002845 vim_free(tofree);
2846 }
2847 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002848 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002849
2850 verbose_leave_scroll();
2851 --no_wait_return;
2852 }
2853
Bram Moolenaare31ee862020-01-07 20:59:34 +01002854 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002855 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002856 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002857#ifdef FEAT_PROFILE
2858 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002859 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002860#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02002861 if (using_sandbox)
2862 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002863
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002864 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002865 {
2866 ++no_wait_return;
2867 verbose_enter_scroll();
2868
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002869 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002870 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002871
2872 verbose_leave_scroll();
2873 --no_wait_return;
2874 }
2875
2876 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002877 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002878 for (i = 0; i < tv_to_free_len; ++i)
2879 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002880 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002881}
2882
2883/*
Bram Moolenaar50824712020-12-20 21:10:17 +01002884 * Check the argument count for user function "fp".
2885 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
2886 */
2887 int
2888check_user_func_argcount(ufunc_T *fp, int argcount)
2889{
2890 int regular_args = fp->uf_args.ga_len;
2891
2892 if (argcount < regular_args - fp->uf_def_args.ga_len)
2893 return FCERR_TOOFEW;
2894 else if (!has_varargs(fp) && argcount > regular_args)
2895 return FCERR_TOOMANY;
2896 return FCERR_UNKNOWN;
2897}
2898
2899/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002901 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 int
2903call_user_func_check(
2904 ufunc_T *fp,
2905 int argcount,
2906 typval_T *argvars,
2907 typval_T *rettv,
2908 funcexe_T *funcexe,
2909 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002910{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002912
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002913 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
2914 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01002915 error = check_user_func_argcount(fp, argcount);
2916 if (error != FCERR_UNKNOWN)
2917 return error;
2918 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 error = FCERR_DICT;
2920 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002921 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 int did_save_redo = FALSE;
2923 save_redo_T save_redo;
2924
2925 /*
2926 * Call the user function.
2927 * Save and restore search patterns, script variables and
2928 * redo buffer.
2929 */
2930 save_search_patterns();
2931 if (!ins_compl_active())
2932 {
2933 saveRedobuff(&save_redo);
2934 did_save_redo = TRUE;
2935 }
2936 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002937 call_user_func(fp, argcount, argvars, rettv, funcexe,
2938 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
2940 // Function was unreferenced while being used, free it now.
2941 func_clear_free(fp, FALSE);
2942 if (did_save_redo)
2943 restoreRedobuff(&save_redo);
2944 restore_search_patterns();
2945 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002946 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002948}
2949
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002950static funccal_entry_T *funccal_stack = NULL;
2951
2952/*
2953 * Save the current function call pointer, and set it to NULL.
2954 * Used when executing autocommands and for ":source".
2955 */
2956 void
2957save_funccal(funccal_entry_T *entry)
2958{
2959 entry->top_funccal = current_funccal;
2960 entry->next = funccal_stack;
2961 funccal_stack = entry;
2962 current_funccal = NULL;
2963}
2964
2965 void
2966restore_funccal(void)
2967{
2968 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002969 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002970 else
2971 {
2972 current_funccal = funccal_stack->top_funccal;
2973 funccal_stack = funccal_stack->next;
2974 }
2975}
2976
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02002977 funccall_T *
2978get_current_funccal(void)
2979{
2980 return current_funccal;
2981}
2982
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002983/*
2984 * Mark all functions of script "sid" as deleted.
2985 */
2986 void
2987delete_script_functions(int sid)
2988{
2989 hashitem_T *hi;
2990 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02002991 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002992 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002993 size_t len;
2994
2995 buf[0] = K_SPECIAL;
2996 buf[1] = KS_EXTRA;
2997 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002998 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002999 len = STRLEN(buf);
3000
Bram Moolenaarce658352020-07-31 23:47:12 +02003001 while (todo > 0)
3002 {
3003 todo = func_hashtab.ht_used;
3004 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3005 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003006 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003007 fp = HI2UF(hi);
3008 if (STRNCMP(fp->uf_name, buf, len) == 0)
3009 {
3010 int changed = func_hashtab.ht_changed;
3011
3012 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003013
3014 if (fp->uf_calls > 0)
3015 {
3016 // Function is executing, don't free it but do remove
3017 // it from the hashtable.
3018 if (func_remove(fp))
3019 fp->uf_refcount--;
3020 }
3021 else
3022 {
3023 func_clear(fp, TRUE);
3024 // When clearing a function another function can be
3025 // cleared as a side effect. When that happens start
3026 // over.
3027 if (changed != func_hashtab.ht_changed)
3028 break;
3029 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003030 }
3031 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003032 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003033 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003034}
3035
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003036#if defined(EXITFREE) || defined(PROTO)
3037 void
3038free_all_functions(void)
3039{
3040 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003041 ufunc_T *fp;
3042 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003043 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003044 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003045
Bram Moolenaare38eab22019-12-05 21:50:01 +01003046 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003047 while (current_funccal != NULL)
3048 {
3049 clear_tv(current_funccal->rettv);
3050 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003051 if (current_funccal == NULL && funccal_stack != NULL)
3052 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003053 }
3054
Bram Moolenaare38eab22019-12-05 21:50:01 +01003055 // First clear what the functions contain. Since this may lower the
3056 // reference count of a function, it may also free a function and change
3057 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003058 while (todo > 0)
3059 {
3060 todo = func_hashtab.ht_used;
3061 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3062 if (!HASHITEM_EMPTY(hi))
3063 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 // clear the def function index now
3065 fp = HI2UF(hi);
3066 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003067 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068
Bram Moolenaare38eab22019-12-05 21:50:01 +01003069 // Only free functions that are not refcounted, those are
3070 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003071 if (func_name_refcount(fp->uf_name))
3072 ++skipped;
3073 else
3074 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003075 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003076 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003077 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003078 {
3079 skipped = 0;
3080 break;
3081 }
3082 }
3083 --todo;
3084 }
3085 }
3086
Bram Moolenaare38eab22019-12-05 21:50:01 +01003087 // Now actually free the functions. Need to start all over every time,
3088 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003089 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003090 while (func_hashtab.ht_used > skipped)
3091 {
3092 todo = func_hashtab.ht_used;
3093 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003094 if (!HASHITEM_EMPTY(hi))
3095 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003096 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003097 // Only free functions that are not refcounted, those are
3098 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003099 fp = HI2UF(hi);
3100 if (func_name_refcount(fp->uf_name))
3101 ++skipped;
3102 else
3103 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003104 if (func_free(fp, FALSE) == OK)
3105 {
3106 skipped = 0;
3107 break;
3108 }
3109 // did not actually free it
3110 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003111 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003112 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003113 }
3114 if (skipped == 0)
3115 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116
3117 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003118}
3119#endif
3120
3121/*
3122 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02003123 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003124 * "len" is the length of "name", or -1 for NUL terminated.
3125 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003127builtin_function(char_u *name, int len)
3128{
3129 char_u *p;
3130
Bram Moolenaara26b9702020-04-18 19:53:28 +02003131 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003132 return FALSE;
3133 p = vim_strchr(name, AUTOLOAD_CHAR);
3134 return p == NULL || (len > 0 && p > name + len);
3135}
3136
3137 int
3138func_call(
3139 char_u *name,
3140 typval_T *args,
3141 partial_T *partial,
3142 dict_T *selfdict,
3143 typval_T *rettv)
3144{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003145 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003146 listitem_T *item;
3147 typval_T argv[MAX_FUNC_ARGS + 1];
3148 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003149 int r = 0;
3150
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003151 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003152 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003153 {
3154 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3155 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003156 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003157 break;
3158 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003159 // Make a copy of each argument. This is needed to be able to set
3160 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003161 copy_tv(&item->li_tv, &argv[argc++]);
3162 }
3163
3164 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003165 {
3166 funcexe_T funcexe;
3167
Bram Moolenaara80faa82020-04-12 19:37:17 +02003168 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003169 funcexe.fe_firstline = curwin->w_cursor.lnum;
3170 funcexe.fe_lastline = curwin->w_cursor.lnum;
3171 funcexe.fe_evaluate = TRUE;
3172 funcexe.fe_partial = partial;
3173 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003174 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3175 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003176
Bram Moolenaare38eab22019-12-05 21:50:01 +01003177 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003178 while (argc > 0)
3179 clear_tv(&argv[--argc]);
3180
3181 return r;
3182}
3183
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003184static int callback_depth = 0;
3185
3186 int
3187get_callback_depth(void)
3188{
3189 return callback_depth;
3190}
3191
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003192/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003193 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003194 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003195 */
3196 int
3197call_callback(
3198 callback_T *callback,
3199 int len, // length of "name" or -1 to use strlen()
3200 typval_T *rettv, // return value goes here
3201 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003202 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003203 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003204{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003205 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003206 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003207
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003208 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3209 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003210 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003211 funcexe.fe_evaluate = TRUE;
3212 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003213 ++callback_depth;
3214 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3215 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003216
3217 // When a :def function was called that uses :try an error would be turned
3218 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003219 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003220 {
3221 need_rethrow = FALSE;
3222 handle_did_throw();
3223 }
3224
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003225 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003226}
3227
3228/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003229 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003230 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003231 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3232 */
3233 varnumber_T
3234call_callback_retnr(
3235 callback_T *callback,
3236 int argcount, // number of "argvars"
3237 typval_T *argvars) // vars for arguments, must have "argcount"
3238 // PLUS ONE elements!
3239{
3240 typval_T rettv;
3241 varnumber_T retval;
3242
3243 if (call_callback(callback, 0, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003244 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003245
3246 retval = tv_get_number_chk(&rettv, NULL);
3247 clear_tv(&rettv);
3248 return retval;
3249}
3250
3251/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 * Give an error message for the result of a function.
3253 * Nothing if "error" is FCERR_NONE.
3254 */
3255 void
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003256user_func_error(int error, char_u *name, funcexe_T *funcexe)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257{
3258 switch (error)
3259 {
3260 case FCERR_UNKNOWN:
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003261 if (funcexe->fe_found_var)
3262 semsg(_(e_not_callable_type_str), name);
3263 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003264 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 break;
3266 case FCERR_NOTMETHOD:
3267 emsg_funcname(
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003268 N_(e_cannot_use_function_as_method_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 break;
3270 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003271 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 break;
3273 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003274 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 break;
3276 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003277 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 break;
3279 case FCERR_SCRIPT:
3280 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00003281 e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 break;
3283 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003284 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3285 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 break;
3287 }
3288}
3289
3290/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003291 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003292 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003293 * Return FAIL when the function can't be called, OK otherwise.
3294 * Also returns OK when an error was encountered while executing the function.
3295 */
3296 int
3297call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003298 char_u *funcname, // name of the function
3299 int len, // length of "name" or -1 to use strlen()
3300 typval_T *rettv, // return value goes here
3301 int argcount_in, // number of "argvars"
3302 typval_T *argvars_in, // vars for arguments, must have "argcount"
3303 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003304 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003305{
3306 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01003307 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003308 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003309 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003310 char_u fname_buf[FLEN_FIXED + 1];
3311 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003312 char_u *fname = NULL;
3313 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003314 int argcount = argcount_in;
3315 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003316 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003317 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003318 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003319 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003320 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003321 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003322 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003323 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003324
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003325 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3326 // even when call_func() returns FAIL.
3327 rettv->v_type = VAR_UNKNOWN;
3328
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003329 if (partial != NULL)
3330 fp = partial->pt_func;
3331 if (fp == NULL)
3332 {
3333 // Make a copy of the name, if it comes from a funcref variable it
3334 // could be changed or deleted in the called function.
3335 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3336 if (name == NULL)
3337 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003338
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003339 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3340 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003341
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003342 if (funcexe->fe_doesrange != NULL)
3343 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003344
3345 if (partial != NULL)
3346 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003347 // When the function has a partial with a dict and there is a dict
3348 // argument, use the dict argument. That is backwards compatible.
3349 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003350 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003351 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003352 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003353 {
3354 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003355 {
3356 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3357 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003358 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003359 goto theend;
3360 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003361 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003362 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003363 for (i = 0; i < argcount_in; ++i)
3364 argv[i + argv_clear] = argvars_in[i];
3365 argvars = argv;
3366 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003367
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003368 if (funcexe->fe_check_type != NULL
3369 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003370 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003371 // Now funcexe->fe_check_type is missing the added arguments,
3372 // make a copy of the type with the correction.
3373 check_type = *funcexe->fe_check_type;
3374 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003375 check_type.tt_args = check_type_args;
3376 CLEAR_FIELD(check_type_args);
3377 for (i = 0; i < check_type.tt_argcount; ++i)
3378 check_type_args[i + partial->pt_argc] =
3379 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003380 check_type.tt_argcount += partial->pt_argc;
3381 check_type.tt_min_argcount += partial->pt_argc;
3382 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003383 }
3384 }
3385
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003386 if (error == FCERR_NONE && funcexe->fe_check_type != NULL
3387 && funcexe->fe_evaluate)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003388 {
3389 // Check that the argument types are OK for the types of the funcref.
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003390 if (check_argument_types(funcexe->fe_check_type, argvars, argcount,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003391 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003392 error = FCERR_OTHER;
3393 }
3394
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003395 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003396 {
3397 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003398 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003399
Bram Moolenaar333894b2020-08-01 18:53:07 +02003400 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003401 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003402 {
3403 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003404 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003405 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003406
Bram Moolenaare38eab22019-12-05 21:50:01 +01003407 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003408 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003409 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003410
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003411 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003412 {
3413 /*
3414 * User defined function.
3415 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003416 if (fp == NULL)
Bram Moolenaar333894b2020-08-01 18:53:07 +02003417 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003418
Bram Moolenaare38eab22019-12-05 21:50:01 +01003419 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003420 if (fp == NULL
3421 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003422 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003423 && !aborting())
3424 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003425 // executed an autocommand, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02003426 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003427 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003428 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003429 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3430 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003431 // loaded a package, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02003432 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003433 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003434 if (fp == NULL)
3435 {
3436 char_u *p = untrans_function_name(rfname);
3437
3438 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003439 // Don't do this if the name starts with "s:".
3440 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaar333894b2020-08-01 18:53:07 +02003441 fp = find_func(p, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003442 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003443
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003444 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003445 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02003446#ifdef FEAT_LUA
3447 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
3448 {
3449 cfunc_T cb = fp->uf_cb;
3450
3451 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3452 }
3453#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003454 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003455 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003456 if (funcexe->fe_argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003457 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003458 argcount = funcexe->fe_argv_func(argcount, argvars,
3459 argv_clear, fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003460
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003461 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003462 {
3463 // Method call: base->Method()
3464 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003465 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003466 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003467 argvars = argv;
3468 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003469 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003470
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 error = call_user_func_check(fp, argcount, argvars, rettv,
3472 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003473 }
3474 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003475 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003476 {
3477 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003478 * expr->method(): Find the method name in the table, call its
3479 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003480 */
3481 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003482 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003483 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003484 else
3485 {
3486 /*
3487 * Find the function name in the table, call its implementation.
3488 */
3489 error = call_internal_func(fname, argcount, argvars, rettv);
3490 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003491
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003492 /*
3493 * The function call (or "FuncUndefined" autocommand sequence) might
3494 * have been aborted by an error, an interrupt, or an explicitly thrown
3495 * exception that has not been caught so far. This situation can be
3496 * tested for by calling aborting(). For an error in an internal
3497 * function or for the "E132" error in call_user_func(), however, the
3498 * throw point at which the "force_abort" flag (temporarily reset by
3499 * emsg()) is normally updated has not been reached yet. We need to
3500 * update that flag first to make aborting() reliable.
3501 */
3502 update_force_abort();
3503 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003504 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003505 ret = OK;
3506
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003507theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003508 /*
3509 * Report an error unless the argument evaluation or function call has been
3510 * cancelled due to an aborting error, an interrupt, or an exception.
3511 */
3512 if (!aborting())
3513 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003514 user_func_error(error, (name != NULL) ? name : funcname, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003515 }
3516
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003517 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003518 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003519 clear_tv(&argv[--argv_clear + argv_base]);
3520
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003521 vim_free(tofree);
3522 vim_free(name);
3523
3524 return ret;
3525}
3526
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003527 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528printable_func_name(ufunc_T *fp)
3529{
3530 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3531}
3532
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003533/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003534 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003535 */
3536 static void
3537list_func_head(ufunc_T *fp, int indent)
3538{
3539 int j;
3540
3541 msg_start();
3542 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003543 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003544 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003545 msg_puts("def ");
3546 else
3547 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003549 msg_putchar('(');
3550 for (j = 0; j < fp->uf_args.ga_len; ++j)
3551 {
3552 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003553 msg_puts(", ");
3554 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 if (fp->uf_arg_types != NULL)
3556 {
3557 char *tofree;
3558
3559 msg_puts(": ");
3560 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3561 vim_free(tofree);
3562 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003563 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3564 {
3565 msg_puts(" = ");
3566 msg_puts(((char **)(fp->uf_def_args.ga_data))
3567 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3568 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003569 }
3570 if (fp->uf_varargs)
3571 {
3572 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003573 msg_puts(", ");
3574 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003575 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 if (fp->uf_va_name != NULL)
3577 {
3578 if (j)
3579 msg_puts(", ");
3580 msg_puts("...");
3581 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003582 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 {
3584 char *tofree;
3585
3586 msg_puts(": ");
3587 msg_puts(type_name(fp->uf_va_type, &tofree));
3588 vim_free(tofree);
3589 }
3590 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003591 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003592
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003593 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003594 {
3595 if (fp->uf_ret_type != &t_void)
3596 {
3597 char *tofree;
3598
3599 msg_puts(": ");
3600 msg_puts(type_name(fp->uf_ret_type, &tofree));
3601 vim_free(tofree);
3602 }
3603 }
3604 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003605 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003606 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003607 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003608 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003609 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003610 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003611 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003612 msg_clr_eos();
3613 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003614 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003615}
3616
3617/*
3618 * Get a function name, translating "<SID>" and "<SNR>".
3619 * Also handles a Funcref in a List or Dictionary.
3620 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003621 * Set "*is_global" to TRUE when the function must be global, unless
3622 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003623 * flags:
3624 * TFN_INT: internal function name OK
3625 * TFN_QUIET: be quiet
3626 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003627 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003628 * Advances "pp" to just after the function name (if no error).
3629 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003630 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003631trans_function_name(
3632 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003633 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003634 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003635 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003636 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003637 partial_T **partial, // return: partial of a FuncRef
3638 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003639{
3640 char_u *name = NULL;
3641 char_u *start;
3642 char_u *end;
3643 int lead;
3644 char_u sid_buf[20];
3645 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003647 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003649
3650 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003651 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003652 start = *pp;
3653
Bram Moolenaare38eab22019-12-05 21:50:01 +01003654 // Check for hard coded <SNR>: already translated function ID (from a user
3655 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003656 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
3657 && (*pp)[2] == (int)KE_SNR)
3658 {
3659 *pp += 3;
3660 len = get_id_len(pp) + 3;
3661 return vim_strnsave(start, len);
3662 }
3663
Bram Moolenaare38eab22019-12-05 21:50:01 +01003664 // A name starting with "<SID>" or "<SNR>" is local to a script. But
3665 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003666 lead = eval_fname_script(start);
3667 if (lead > 2)
3668 start += lead;
3669
Bram Moolenaare38eab22019-12-05 21:50:01 +01003670 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01003671 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003672 lead > 2 ? 0 : FNE_CHECK_START);
3673 if (end == start)
3674 {
3675 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003676 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003677 goto theend;
3678 }
3679 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
3680 {
3681 /*
3682 * Report an invalid expression in braces, unless the expression
3683 * evaluation has been cancelled due to an aborting error, an
3684 * interrupt, or an exception.
3685 */
3686 if (!aborting())
3687 {
3688 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003689 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003690 }
3691 else
3692 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
3693 goto theend;
3694 }
3695
3696 if (lv.ll_tv != NULL)
3697 {
3698 if (fdp != NULL)
3699 {
3700 fdp->fd_dict = lv.ll_dict;
3701 fdp->fd_newkey = lv.ll_newkey;
3702 lv.ll_newkey = NULL;
3703 fdp->fd_di = lv.ll_di;
3704 }
3705 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
3706 {
3707 name = vim_strsave(lv.ll_tv->vval.v_string);
3708 *pp = end;
3709 }
3710 else if (lv.ll_tv->v_type == VAR_PARTIAL
3711 && lv.ll_tv->vval.v_partial != NULL)
3712 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003713 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003714 *pp = end;
3715 if (partial != NULL)
3716 *partial = lv.ll_tv->vval.v_partial;
3717 }
3718 else
3719 {
3720 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
3721 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00003722 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003723 else
3724 *pp = end;
3725 name = NULL;
3726 }
3727 goto theend;
3728 }
3729
3730 if (lv.ll_name == NULL)
3731 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003732 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003733 *pp = end;
3734 goto theend;
3735 }
3736
Bram Moolenaare38eab22019-12-05 21:50:01 +01003737 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003738 if (lv.ll_exp_name != NULL)
3739 {
3740 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003741 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003742 flags & TFN_NO_AUTOLOAD, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003743 if (name == lv.ll_exp_name)
3744 name = NULL;
3745 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003746 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003747 {
3748 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003749 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003750 flags & TFN_NO_AUTOLOAD, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003751 if (name == *pp)
3752 name = NULL;
3753 }
3754 if (name != NULL)
3755 {
3756 name = vim_strsave(name);
3757 *pp = end;
3758 if (STRNCMP(name, "<SNR>", 5) == 0)
3759 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003760 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003761 name[0] = K_SPECIAL;
3762 name[1] = KS_EXTRA;
3763 name[2] = (int)KE_SNR;
3764 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
3765 }
3766 goto theend;
3767 }
3768
3769 if (lv.ll_exp_name != NULL)
3770 {
3771 len = (int)STRLEN(lv.ll_exp_name);
3772 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
3773 && STRNCMP(lv.ll_name, "s:", 2) == 0)
3774 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003775 // When there was "s:" already or the name expanded to get a
3776 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003777 lv.ll_name += 2;
3778 len -= 2;
3779 lead = 2;
3780 }
3781 }
3782 else
3783 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003784 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003785 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003786 {
3787 if (is_global != NULL && lv.ll_name[0] == 'g')
3788 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003789 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003790 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003791 len = (int)(end - lv.ll_name);
3792 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003793 if (len <= 0)
3794 {
3795 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003796 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003797 goto theend;
3798 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003799
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003800 // In Vim9 script a user function is script-local by default, unless it
3801 // starts with a lower case character: dict.func().
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003802 vim9script = ASCII_ISUPPER(*start) && in_vim9script();
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003803 if (vim9script)
3804 {
3805 char_u *p;
3806
3807 // SomeScript#func() is a global function.
3808 for (p = start; *p != NUL && *p != '('; ++p)
3809 if (*p == AUTOLOAD_CHAR)
3810 vim9script = FALSE;
3811 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003813 /*
3814 * Copy the function name to allocated memory.
3815 * Accept <SID>name() inside a script, translate into <SNR>123_name().
3816 * Accept <SNR>123_name() outside a script.
3817 */
3818 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003819 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003821 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 if (!vim9script)
3823 lead = 3;
3824 if (vim9script || (lv.ll_exp_name != NULL
3825 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003826 || eval_fname_sid(*pp))
3827 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003829 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003830 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00003831 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003832 goto theend;
3833 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003834 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835 if (vim9script)
3836 extra = 3 + (int)STRLEN(sid_buf);
3837 else
3838 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003839 }
3840 }
Bram Moolenaar22f17a22021-06-21 20:48:58 +02003841 else if (!(flags & TFN_INT) && (builtin_function(lv.ll_name, len)
3842 || (in_vim9script() && *lv.ll_name == '_')))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003843 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00003844 semsg(_(e_function_name_must_start_with_capital_or_s_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003845 goto theend;
3846 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003847 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003848 {
3849 char_u *cp = vim_strchr(lv.ll_name, ':');
3850
3851 if (cp != NULL && cp < end)
3852 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003853 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003854 goto theend;
3855 }
3856 }
3857
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003859 if (name != NULL)
3860 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01003861 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003862 {
3863 name[0] = K_SPECIAL;
3864 name[1] = KS_EXTRA;
3865 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003867 STRCPY(name + 3, sid_buf);
3868 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
3870 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003871 }
3872 *pp = end;
3873
3874theend:
3875 clear_lval(&lv);
3876 return name;
3877}
3878
3879/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02003880 * Assuming "name" is the result of trans_function_name() and it was prefixed
3881 * to use the script-local name, return the unmodified name (points into
3882 * "name"). Otherwise return NULL.
3883 * This can be used to first search for a script-local function and fall back
3884 * to the global function if not found.
3885 */
3886 char_u *
3887untrans_function_name(char_u *name)
3888{
3889 char_u *p;
3890
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003891 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02003892 {
3893 p = vim_strchr(name, '_');
3894 if (p != NULL)
3895 return p + 1;
3896 }
3897 return NULL;
3898}
3899
3900/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00003901 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
3902 * current script ID and returns the expanded function name. The caller should
3903 * free the returned name. If not called from a script context or the function
3904 * name doesn't start with these prefixes, then returns NULL.
3905 * This doesn't check whether the script-local function exists or not.
3906 */
3907 char_u *
3908get_scriptlocal_funcname(char_u *funcname)
3909{
3910 char sid_buf[25];
3911 int off;
3912 char_u *newname;
3913
3914 if (funcname == NULL)
3915 return NULL;
3916
3917 if (STRNCMP(funcname, "s:", 2) != 0
3918 && STRNCMP(funcname, "<SID>", 5) != 0)
3919 // The function name is not a script-local function name
3920 return NULL;
3921
3922 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3923 {
3924 emsg(_(e_using_sid_not_in_script_context));
3925 return NULL;
3926 }
3927 // Expand s: prefix into <SNR>nr_<name>
3928 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
3929 (long)current_sctx.sc_sid);
3930 off = *funcname == 's' ? 2 : 5;
3931 newname = alloc(STRLEN(sid_buf) + STRLEN(funcname + off) + 1);
3932 if (newname == NULL)
3933 return NULL;
3934 STRCPY(newname, sid_buf);
3935 STRCAT(newname, funcname + off);
3936
3937 return newname;
3938}
3939
3940/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00003941 * Call trans_function_name(), except that a lambda is returned as-is.
3942 * Returns the name in allocated memory.
3943 */
3944 char_u *
3945save_function_name(
3946 char_u **name,
3947 int *is_global,
3948 int skip,
3949 int flags,
3950 funcdict_T *fudi)
3951{
3952 char_u *p = *name;
3953 char_u *saved;
3954
3955 if (STRNCMP(p, "<lambda>", 8) == 0)
3956 {
3957 p += 8;
3958 (void)getdigits(&p);
3959 saved = vim_strnsave(*name, p - *name);
3960 if (fudi != NULL)
3961 CLEAR_POINTER(fudi);
3962 }
3963 else
3964 saved = trans_function_name(&p, is_global, skip,
3965 flags, fudi, NULL, NULL);
3966 *name = p;
3967 return saved;
3968}
3969
3970/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003971 * List functions. When "regmatch" is NULL all of then.
3972 * Otherwise functions matching "regmatch".
3973 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003974 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003975list_functions(regmatch_T *regmatch)
3976{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003977 int changed = func_hashtab.ht_changed;
3978 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003979 hashitem_T *hi;
3980
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003981 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003982 {
3983 if (!HASHITEM_EMPTY(hi))
3984 {
3985 ufunc_T *fp = HI2UF(hi);
3986
3987 --todo;
3988 if ((fp->uf_flags & FC_DEAD) == 0
3989 && (regmatch == NULL
3990 ? !message_filtered(fp->uf_name)
3991 && !func_name_refcount(fp->uf_name)
3992 : !isdigit(*fp->uf_name)
3993 && vim_regexec(regmatch, fp->uf_name, 0)))
3994 {
3995 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003996 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003997 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00003998 emsg(_(e_function_list_was_modified));
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003999 return;
4000 }
4001 }
4002 }
4003 }
4004}
4005
4006/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004007 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004008 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4009 * the function name.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004010 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004011 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004012 ufunc_T *
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004013define_function(exarg_T *eap, char_u *name_arg, char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004014{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004015 int j;
4016 int c;
4017 int saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004018 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004019 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004020 char_u *p;
4021 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004022 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004023 char_u *line_arg = NULL;
4024 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004026 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004027 garray_T newlines;
4028 int varargs = FALSE;
4029 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004031 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004032 int fp_allocated = FALSE;
4033 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004034 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004035 dictitem_T *v;
4036 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004037 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004038 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004039 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004040 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004041 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004042 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004043
4044 /*
4045 * ":function" without argument: list functions.
4046 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004047 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004048 {
4049 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004050 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004051 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004052 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004053 }
4054
4055 /*
4056 * ":function /pat": list functions matching pattern.
4057 */
4058 if (*eap->arg == '/')
4059 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004060 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004061 if (!eap->skip)
4062 {
4063 regmatch_T regmatch;
4064
4065 c = *p;
4066 *p = NUL;
4067 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4068 *p = c;
4069 if (regmatch.regprog != NULL)
4070 {
4071 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004072 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004073 vim_regfree(regmatch.regprog);
4074 }
4075 }
4076 if (*p == '/')
4077 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004078 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004079 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004080 }
4081
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 ga_init(&newargs);
4083 ga_init(&argtypes);
4084 ga_init(&default_args);
4085
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004086 /*
4087 * Get the function name. There are these situations:
4088 * func normal function name
4089 * "name" == func, "fudi.fd_dict" == NULL
4090 * dict.func new dictionary entry
4091 * "name" == NULL, "fudi.fd_dict" set,
4092 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4093 * dict.func existing dict entry with a Funcref
4094 * "name" == func, "fudi.fd_dict" set,
4095 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4096 * dict.func existing dict entry that's not a Funcref
4097 * "name" == NULL, "fudi.fd_dict" set,
4098 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4099 * s:func script-local function name
4100 * g:func global function name, same as "func"
4101 */
4102 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004103 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004104 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004105 // nested function, argument is (args).
4106 paren = TRUE;
4107 CLEAR_FIELD(fudi);
4108 }
4109 else
4110 {
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004111 name = save_function_name(&p, &is_global, eap->skip,
4112 TFN_NO_AUTOLOAD, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004113 paren = (vim_strchr(p, '(') != NULL);
4114 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004115 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004116 /*
4117 * Return on an invalid expression in braces, unless the expression
4118 * evaluation has been cancelled due to an aborting error, an
4119 * interrupt, or an exception.
4120 */
4121 if (!aborting())
4122 {
4123 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004124 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004125 vim_free(fudi.fd_newkey);
4126 return NULL;
4127 }
4128 else
4129 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004130 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004131 }
4132
Bram Moolenaare38eab22019-12-05 21:50:01 +01004133 // An error in a function call during evaluation of an expression in magic
4134 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004135 saved_did_emsg = did_emsg;
4136 did_emsg = FALSE;
4137
4138 /*
4139 * ":function func" with only function name: list function.
4140 */
4141 if (!paren)
4142 {
4143 if (!ends_excmd(*skipwhite(p)))
4144 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004145 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004146 goto ret_free;
4147 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004148 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004149 if (eap->nextcmd != NULL)
4150 *p = NUL;
4151 if (!eap->skip && !got_int)
4152 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004153 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004154 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4155 {
4156 char_u *up = untrans_function_name(name);
4157
4158 // With Vim9 script the name was made script-local, if not
4159 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004160 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004161 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004162 }
4163
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004164 if (fp != NULL)
4165 {
4166 list_func_head(fp, TRUE);
4167 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4168 {
4169 if (FUNCLINE(fp, j) == NULL)
4170 continue;
4171 msg_putchar('\n');
4172 msg_outnum((long)(j + 1));
4173 if (j < 9)
4174 msg_putchar(' ');
4175 if (j < 99)
4176 msg_putchar(' ');
4177 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004178 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004179 ui_breakcheck();
4180 }
4181 if (!got_int)
4182 {
4183 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004184 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 msg_puts(" enddef");
4186 else
4187 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004188 }
4189 }
4190 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004191 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004192 }
4193 goto ret_free;
4194 }
4195
4196 /*
4197 * ":function name(arg1, arg2)" Define function.
4198 */
4199 p = skipwhite(p);
4200 if (*p != '(')
4201 {
4202 if (!eap->skip)
4203 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004204 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004205 goto ret_free;
4206 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004207 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004208 if (vim_strchr(p, '(') != NULL)
4209 p = vim_strchr(p, '(');
4210 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004211
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004212 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4213 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004214 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004215 goto ret_free;
4216 }
4217
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004218 // In Vim9 script only global functions can be redefined.
4219 if (vim9script && eap->forceit && !is_global)
4220 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004221 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004222 goto ret_free;
4223 }
4224
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004225 ga_init2(&newlines, (int)sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004226
Bram Moolenaar04b12692020-05-04 23:24:44 +02004227 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004228 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004229 // Check the name of the function. Unless it's a dictionary function
4230 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004231 if (name != NULL)
4232 arg = name;
4233 else
4234 arg = fudi.fd_newkey;
4235 if (arg != NULL && (fudi.fd_di == NULL
4236 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4237 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4238 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004239 char_u *name_base = arg;
4240 int i;
4241
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004242 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004243 {
4244 name_base = vim_strchr(arg, '_');
4245 if (name_base == NULL)
4246 name_base = arg + 3;
4247 else
4248 ++name_base;
4249 }
4250 for (i = 0; name_base[i] != NUL && (i == 0
4251 ? eval_isnamec1(name_base[i])
4252 : eval_isnamec(name_base[i])); ++i)
4253 ;
4254 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004255 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004256
4257 // In Vim9 script a function cannot have the same name as a
4258 // variable.
4259 if (vim9script && *arg == K_SPECIAL
Mike Williams1821d142021-12-15 16:38:33 +00004260 && eval_variable(name_base, (int)STRLEN(name_base), NULL, NULL,
Bram Moolenaar052ff292021-12-11 13:54:46 +00004261 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
4262 + EVAL_VAR_NO_FUNC) == OK)
4263 {
4264 semsg(_(e_redefining_script_item_str), name_base);
4265 goto ret_free;
4266 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004267 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004268 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004269 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004270 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004271 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004272 goto ret_free;
4273 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004274 }
4275
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004276 // This may get more lines and make the pointers into the first line
4277 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004278 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004280 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004281 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004282 eap, line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004283 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004284 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004287 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004289 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004290 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004291 if (*p != ':')
4292 {
4293 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4294 p = skipwhite(p);
4295 }
4296 else if (!IS_WHITE_OR_NUL(p[1]))
4297 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004299 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004301 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004302 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004303 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004305 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004307 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004308 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004309 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004310 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004311 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004312 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004313 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 else
4315 // find extra arguments "range", "dict", "abort" and "closure"
4316 for (;;)
4317 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004318 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 p = skipwhite(p);
4320 if (STRNCMP(p, "range", 5) == 0)
4321 {
4322 flags |= FC_RANGE;
4323 p += 5;
4324 }
4325 else if (STRNCMP(p, "dict", 4) == 0)
4326 {
4327 flags |= FC_DICT;
4328 p += 4;
4329 }
4330 else if (STRNCMP(p, "abort", 5) == 0)
4331 {
4332 flags |= FC_ABORT;
4333 p += 5;
4334 }
4335 else if (STRNCMP(p, "closure", 7) == 0)
4336 {
4337 flags |= FC_CLOSURE;
4338 p += 7;
4339 if (current_funccal == NULL)
4340 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004341 emsg_funcname(e_closure_function_should_not_be_at_top_level,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 name == NULL ? (char_u *)"" : name);
4343 goto erret;
4344 }
4345 }
4346 else
4347 break;
4348 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004349
Bram Moolenaare38eab22019-12-05 21:50:01 +01004350 // When there is a line break use what follows for the function body.
4351 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004352 if (*p == '\n')
4353 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004354 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004355 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4356 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004357 && !(VIM_ISWHITE(*whitep) && *p == '#'
4358 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004359 && !eap->skip
4360 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004361 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004362
4363 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4365 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004366 */
4367 if (KeyTyped)
4368 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004369 // Check if the function already exists, don't let the user type the
4370 // whole function before telling him it doesn't work! For a script we
4371 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004372 if (!eap->skip && !eap->forceit)
4373 {
4374 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004375 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004376 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00004377 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004378 }
4379
4380 if (!eap->skip && did_emsg)
4381 goto erret;
4382
Bram Moolenaare38eab22019-12-05 21:50:01 +01004383 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004384 cmdline_row = msg_row;
4385 }
4386
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004387 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004388 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004389
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004390 // Do not define the function when getting the body fails and when
4391 // skipping.
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004392 if (get_function_body(eap, &newlines, line_arg, line_to_free) == FAIL
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004393 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004394 goto erret;
4395
4396 /*
4397 * If there are no errors, add the function
4398 */
4399 if (fudi.fd_dict == NULL)
4400 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 hashtab_T *ht;
4402
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004403 v = find_var(name, &ht, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004404 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
4405 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004406 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004407 goto erret;
4408 }
4409
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004410 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaareef21022020-08-01 22:16:43 +02004411 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004412 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004413 char_u *uname = untrans_function_name(name);
4414
4415 import = find_imported(uname == NULL ? name : uname, 0, NULL);
4416 }
4417
4418 if (fp != NULL || import != NULL)
4419 {
4420 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004422 // Function can be replaced with "function!" and when sourcing the
4423 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02004424 // A name that is used by an import can not be overruled.
4425 if (import != NULL
4426 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004427 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02004428 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004429 {
Bram Moolenaard604d782021-11-20 21:46:20 +00004430 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02004431 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004432 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02004433 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00004434 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004435 goto erret;
4436 }
4437 if (fp->uf_calls > 0)
4438 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004439 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00004440 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004441 goto erret;
4442 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004443 if (fp->uf_refcount > 1)
4444 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004445 // This function is referenced somewhere, don't redefine it but
4446 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004447 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004448 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004449 fp = NULL;
4450 overwrite = TRUE;
4451 }
4452 else
4453 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004454 char_u *exp_name = fp->uf_name_exp;
4455
4456 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01004457 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004458 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004459 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004460 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004462#ifdef FEAT_PROFILE
4463 fp->uf_profiling = FALSE;
4464 fp->uf_prof_initialized = FALSE;
4465#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01004466 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004467 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004468 }
4469 }
4470 else
4471 {
4472 char numbuf[20];
4473
4474 fp = NULL;
4475 if (fudi.fd_newkey == NULL && !eap->forceit)
4476 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004477 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004478 goto erret;
4479 }
4480 if (fudi.fd_di == NULL)
4481 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004482 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02004483 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004484 goto erret;
4485 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004486 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02004487 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004488 goto erret;
4489
Bram Moolenaare38eab22019-12-05 21:50:01 +01004490 // Give the function a sequential number. Can only be used with a
4491 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004492 vim_free(name);
4493 sprintf(numbuf, "%d", ++func_nr);
4494 name = vim_strsave((char_u *)numbuf);
4495 if (name == NULL)
4496 goto erret;
4497 }
4498
4499 if (fp == NULL)
4500 {
4501 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
4502 {
4503 int slen, plen;
4504 char_u *scriptname;
4505
Bram Moolenaare38eab22019-12-05 21:50:01 +01004506 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004507 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004508 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004509 {
4510 scriptname = autoload_name(name);
4511 if (scriptname != NULL)
4512 {
4513 p = vim_strchr(scriptname, '/');
4514 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004515 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004516 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004517 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004518 j = OK;
4519 vim_free(scriptname);
4520 }
4521 }
4522 if (j == FAIL)
4523 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004524 linenr_T save_lnum = SOURCING_LNUM;
4525
4526 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00004527 semsg(_(e_function_name_does_not_match_script_file_name_str),
4528 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004529 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004530 goto erret;
4531 }
4532 }
4533
Bram Moolenaar47ed5532019-08-08 20:49:14 +02004534 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004535 if (fp == NULL)
4536 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004537 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004538
4539 if (fudi.fd_dict != NULL)
4540 {
4541 if (fudi.fd_di == NULL)
4542 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004543 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004544 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
4545 if (fudi.fd_di == NULL)
4546 {
4547 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004548 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004549 goto erret;
4550 }
4551 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
4552 {
4553 vim_free(fudi.fd_di);
4554 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004555 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004556 goto erret;
4557 }
4558 }
4559 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004560 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004561 clear_tv(&fudi.fd_di->di_tv);
4562 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004563 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004564
Bram Moolenaare38eab22019-12-05 21:50:01 +01004565 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004566 flags |= FC_DICT;
4567 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004568 }
4569 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004570 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004572 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573
4574 if (eap->cmdidx == CMD_def)
4575 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004576 int lnum_save = SOURCING_LNUM;
4577 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004578
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004579 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004580
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004581 // error messages are for the first function line
4582 SOURCING_LNUM = sourcing_lnum_top;
4583
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02004584 // The function may use script variables from the context.
4585 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004586
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004587 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004589 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004590 free_fp = fp_allocated;
4591 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004593 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594
4595 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004596 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004598 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004599 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004600 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02004602 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004604 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004605 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004607 if (fp_allocated)
4608 {
4609 // insert the new function in the function list
4610 set_ufunc_name(fp, name);
4611 if (overwrite)
4612 {
4613 hi = hash_find(&func_hashtab, name);
4614 hi->hi_key = UF2HIKEY(fp);
4615 }
4616 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
4617 {
4618 free_fp = TRUE;
4619 goto erret;
4620 }
4621 fp->uf_refcount = 1;
4622 }
4623
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004624 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004625 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004626 if ((flags & FC_CLOSURE) != 0)
4627 {
Bram Moolenaar58016442016-07-31 18:30:22 +02004628 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004629 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004630 }
4631 else
4632 fp->uf_scoped = NULL;
4633
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004634#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004635 if (prof_def_func())
4636 func_do_profile(fp);
4637#endif
4638 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02004639 if (sandbox)
4640 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004641 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004642 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004643 fp->uf_flags = flags;
4644 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01004645 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004646 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004647 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004648 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649 if (is_export)
4650 {
4651 fp->uf_flags |= FC_EXPORT;
4652 // let ex_export() know the export worked.
4653 is_export = FALSE;
4654 }
4655
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004656 if (eap->cmdidx == CMD_def)
4657 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02004658 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
4659 // :func does not use Vim9 script syntax, even in a Vim9 script file
4660 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004661
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004662 goto ret_free;
4663
4664erret:
4665 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004666 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004667 if (fp != NULL)
4668 {
4669 ga_init(&fp->uf_args);
4670 ga_init(&fp->uf_def_args);
4671 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004672errret_2:
4673 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004674 if (fp != NULL)
4675 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004676 if (free_fp)
4677 {
4678 vim_free(fp);
4679 fp = NULL;
4680 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004681ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004682 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004683 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004684 if (name != name_arg)
4685 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004686 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004687 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004688
4689 return fp;
4690}
4691
4692/*
4693 * ":function"
4694 */
4695 void
4696ex_function(exarg_T *eap)
4697{
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004698 char_u *line_to_free = NULL;
4699
4700 (void)define_function(eap, NULL, &line_to_free);
4701 vim_free(line_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02004702}
4703
4704/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004705 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarb2049902021-01-24 12:53:53 +01004706 * be compiled. Except dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02004707 */
4708 void
4709ex_defcompile(exarg_T *eap UNUSED)
4710{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004711 long todo = (long)func_hashtab.ht_used;
4712 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004713 hashitem_T *hi;
4714 ufunc_T *ufunc;
4715
4716 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
4717 {
4718 if (!HASHITEM_EMPTY(hi))
4719 {
4720 --todo;
4721 ufunc = HI2UF(hi);
4722 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004723 && ufunc->uf_def_status == UF_TO_BE_COMPILED
4724 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004725 {
Bram Moolenaare99d4222021-06-13 14:01:26 +02004726 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004727
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004728 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004729 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004730 // a function has been added or removed, need to start over
4731 todo = (long)func_hashtab.ht_used;
4732 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004733 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02004734 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004735 }
4736 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004737 }
4738 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004739}
4740
4741/*
4742 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
4743 * Return 2 if "p" starts with "s:".
4744 * Return 0 otherwise.
4745 */
4746 int
4747eval_fname_script(char_u *p)
4748{
Bram Moolenaare38eab22019-12-05 21:50:01 +01004749 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
4750 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004751 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
4752 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
4753 return 5;
4754 if (p[0] == 's' && p[1] == ':')
4755 return 2;
4756 return 0;
4757}
4758
4759 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004760translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004761{
4762 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004763 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004764 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765}
4766
4767/*
4768 * Return TRUE when "ufunc" has old-style "..." varargs
4769 * or named varargs "...name: type".
4770 */
4771 int
4772has_varargs(ufunc_T *ufunc)
4773{
4774 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004775}
4776
4777/*
4778 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004779 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004780 */
4781 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004782function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004783{
4784 char_u *nm = name;
4785 char_u *p;
4786 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004787 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004788 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004789
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004790 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
4791 if (no_deref)
4792 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004793 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004794 nm = skipwhite(nm);
4795
Bram Moolenaare38eab22019-12-05 21:50:01 +01004796 // Only accept "funcname", "funcname ", "funcname (..." and
4797 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004798 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004799 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004800 vim_free(p);
4801 return n;
4802}
4803
Bram Moolenaar113e1072019-01-20 15:30:40 +01004804#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004805 char_u *
4806get_expanded_name(char_u *name, int check)
4807{
4808 char_u *nm = name;
4809 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004810 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004811
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004812 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004813 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004814
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004815 if (p != NULL && *nm == NUL
4816 && (!check || translated_function_exists(p, is_global)))
4817 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004818
4819 vim_free(p);
4820 return NULL;
4821}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004822#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004823
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004824/*
4825 * Function given to ExpandGeneric() to obtain the list of user defined
4826 * function names.
4827 */
4828 char_u *
4829get_user_func_name(expand_T *xp, int idx)
4830{
4831 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004832 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004833 static hashitem_T *hi;
4834 ufunc_T *fp;
4835
4836 if (idx == 0)
4837 {
4838 done = 0;
4839 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004840 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004841 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004842 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004843 {
4844 if (done++ > 0)
4845 ++hi;
4846 while (HASHITEM_EMPTY(hi))
4847 ++hi;
4848 fp = HI2UF(hi);
4849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 // don't show dead, dict and lambda functions
4851 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02004852 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004854
4855 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004856 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004857
4858 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02004859 if (xp->xp_context != EXPAND_USER_FUNC
4860 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004861 {
4862 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004864 STRCAT(IObuff, ")");
4865 }
4866 return IObuff;
4867 }
4868 return NULL;
4869}
4870
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004871/*
4872 * ":delfunction {name}"
4873 */
4874 void
4875ex_delfunction(exarg_T *eap)
4876{
4877 ufunc_T *fp = NULL;
4878 char_u *p;
4879 char_u *name;
4880 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004881 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004882
4883 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004884 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
4885 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004886 vim_free(fudi.fd_newkey);
4887 if (name == NULL)
4888 {
4889 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004890 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004891 return;
4892 }
4893 if (!ends_excmd(*skipwhite(p)))
4894 {
4895 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00004896 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004897 return;
4898 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004899 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004900 if (eap->nextcmd != NULL)
4901 *p = NUL;
4902
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004903 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02004904 {
4905 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004906 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02004907 vim_free(name);
4908 return;
4909 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004910 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004911 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004912 vim_free(name);
4913
4914 if (!eap->skip)
4915 {
4916 if (fp == NULL)
4917 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004918 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004919 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004920 return;
4921 }
4922 if (fp->uf_calls > 0)
4923 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004924 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004925 return;
4926 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004927 if (fp->uf_flags & FC_VIM9)
4928 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004929 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004930 return;
4931 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004932
4933 if (fudi.fd_dict != NULL)
4934 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004935 // Delete the dict item that refers to the function, it will
4936 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004937 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4938 }
4939 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004940 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004941 // A normal function (not a numbered function or lambda) has a
4942 // refcount of 1 for the entry in the hashtable. When deleting
4943 // it and the refcount is more than one, it should be kept.
4944 // A numbered function and lambda should be kept if the refcount is
4945 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004946 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004947 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004948 // Function is still referenced somewhere. Don't free it but
4949 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004950 if (func_remove(fp))
4951 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004952 }
4953 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004954 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004955 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004956 }
4957}
4958
4959/*
4960 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004961 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004962 */
4963 void
4964func_unref(char_u *name)
4965{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004966 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004967
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004968 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004969 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004970 fp = find_func(name, FALSE, NULL);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004971 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004972 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004973#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004974 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004975#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004976 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004977 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004978 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004979}
4980
4981/*
4982 * Unreference a Function: decrement the reference count and free it when it
4983 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004984 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004985 */
4986 void
4987func_ptr_unref(ufunc_T *fp)
4988{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004989 if (fp != NULL && (--fp->uf_refcount <= 0
4990 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
4991 && fp->uf_partial->pt_refcount <= 1
4992 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02004993 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004994 // Only delete it when it's not being used. Otherwise it's done
4995 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004996 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004997 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004998 }
4999}
5000
5001/*
5002 * Count a reference to a Function.
5003 */
5004 void
5005func_ref(char_u *name)
5006{
5007 ufunc_T *fp;
5008
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005009 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005010 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005011 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005012 if (fp != NULL)
5013 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005014 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005015 // Only give an error for a numbered function.
5016 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005017 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005018}
5019
5020/*
5021 * Count a reference to a Function.
5022 */
5023 void
5024func_ptr_ref(ufunc_T *fp)
5025{
5026 if (fp != NULL)
5027 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005028}
5029
5030/*
5031 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5032 * referenced from anywhere that is in use.
5033 */
5034 static int
5035can_free_funccal(funccall_T *fc, int copyID)
5036{
5037 return (fc->l_varlist.lv_copyID != copyID
5038 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005039 && fc->l_avars.dv_copyID != copyID
5040 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005041}
5042
5043/*
5044 * ":return [expr]"
5045 */
5046 void
5047ex_return(exarg_T *eap)
5048{
5049 char_u *arg = eap->arg;
5050 typval_T rettv;
5051 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005052 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005053
5054 if (current_funccal == NULL)
5055 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005056 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005057 return;
5058 }
5059
Bram Moolenaar844fb642021-10-23 13:32:30 +01005060 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005061 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5062
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005063 if (eap->skip)
5064 ++emsg_skip;
5065
5066 eap->nextcmd = NULL;
5067 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005068 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005069 {
5070 if (!eap->skip)
5071 returning = do_return(eap, FALSE, TRUE, &rettv);
5072 else
5073 clear_tv(&rettv);
5074 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005075 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005076 else if (!eap->skip)
5077 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005078 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005079 update_force_abort();
5080
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005081 /*
5082 * Return unless the expression evaluation has been cancelled due to an
5083 * aborting error, an interrupt, or an exception.
5084 */
5085 if (!aborting())
5086 returning = do_return(eap, FALSE, TRUE, NULL);
5087 }
5088
Bram Moolenaare38eab22019-12-05 21:50:01 +01005089 // When skipping or the return gets pending, advance to the next command
5090 // in this line (!returning). Otherwise, ignore the rest of the line.
5091 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005092 if (returning)
5093 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005094 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005095 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005096
5097 if (eap->skip)
5098 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005099 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005100}
5101
5102/*
5103 * ":1,25call func(arg1, arg2)" function call.
5104 */
5105 void
5106ex_call(exarg_T *eap)
5107{
5108 char_u *arg = eap->arg;
5109 char_u *startarg;
5110 char_u *name;
5111 char_u *tofree;
5112 int len;
5113 typval_T rettv;
5114 linenr_T lnum;
5115 int doesrange;
5116 int failed = FALSE;
5117 funcdict_T fudi;
5118 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005119 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005120 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005121 int found_var = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005122
Bram Moolenaare6b53242020-07-01 17:28:33 +02005123 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005124 if (eap->skip)
5125 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005126 // trans_function_name() doesn't work well when skipping, use eval0()
5127 // instead to skip to any following command, e.g. for:
5128 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005129 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005130 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005131 clear_tv(&rettv);
5132 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005133 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005134 return;
5135 }
5136
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005137 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
5138 &fudi, &partial, in_vim9script() ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005139 if (fudi.fd_newkey != NULL)
5140 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005141 // Still need to give an error message for missing key.
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005142 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005143 vim_free(fudi.fd_newkey);
5144 }
5145 if (tofree == NULL)
5146 return;
5147
Bram Moolenaare38eab22019-12-05 21:50:01 +01005148 // Increase refcount on dictionary, it could get deleted when evaluating
5149 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005150 if (fudi.fd_dict != NULL)
5151 ++fudi.fd_dict->dv_refcount;
5152
Bram Moolenaare38eab22019-12-05 21:50:01 +01005153 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
5154 // contents. For VAR_PARTIAL get its partial, unless we already have one
5155 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005156 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005157 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005158 in_vim9script() && type == NULL ? &type : NULL, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005159
Bram Moolenaare38eab22019-12-05 21:50:01 +01005160 // Skip white space to allow ":call func ()". Not good, but required for
5161 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005162 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005163 if (*startarg != '(')
5164 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005165 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005166 goto end;
5167 }
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005168 if (in_vim9script() && startarg > arg)
5169 {
5170 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
5171 goto end;
5172 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005173
5174 /*
5175 * When skipping, evaluate the function once, to find the end of the
5176 * arguments.
5177 * When the function takes a range, this is discovered after the first
5178 * call, and the loop is broken.
5179 */
5180 if (eap->skip)
5181 {
5182 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005183 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005184 }
5185 else
5186 lnum = eap->line1;
5187 for ( ; lnum <= eap->line2; ++lnum)
5188 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005189 funcexe_T funcexe;
5190
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005191 if (!eap->skip && eap->addr_count > 0)
5192 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01005193 if (lnum > curbuf->b_ml.ml_line_count)
5194 {
5195 // If the function deleted lines or switched to another buffer
5196 // the line number may become invalid.
Bram Moolenaar108010a2021-06-27 22:03:33 +02005197 emsg(_(e_invalid_range));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01005198 break;
5199 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005200 curwin->w_cursor.lnum = lnum;
5201 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005202 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005203 }
5204 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005205
Bram Moolenaara80faa82020-04-12 19:37:17 +02005206 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005207 funcexe.fe_firstline = eap->line1;
5208 funcexe.fe_lastline = eap->line2;
5209 funcexe.fe_doesrange = &doesrange;
5210 funcexe.fe_evaluate = !eap->skip;
5211 funcexe.fe_partial = partial;
5212 funcexe.fe_selfdict = fudi.fd_dict;
5213 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005214 funcexe.fe_found_var = found_var;
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005215 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaare6b53242020-07-01 17:28:33 +02005216 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005217 {
5218 failed = TRUE;
5219 break;
5220 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01005221 if (has_watchexpr())
5222 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005223
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005224 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005225 if (handle_subscript(&arg, &rettv,
5226 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005227 {
5228 failed = TRUE;
5229 break;
5230 }
5231
5232 clear_tv(&rettv);
5233 if (doesrange || eap->skip)
5234 break;
5235
Bram Moolenaare38eab22019-12-05 21:50:01 +01005236 // Stop when immediately aborting on error, or when an interrupt
5237 // occurred or an exception was thrown but not caught.
5238 // get_func_tv() returned OK, so that the check for trailing
5239 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005240 if (aborting())
5241 break;
5242 }
5243 if (eap->skip)
5244 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005245 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005246
Bram Moolenaar1d341892021-09-18 15:25:52 +02005247 // When inside :try we need to check for following "| catch" or "| endtry".
5248 // Not when there was an error, but do check if an exception was thrown.
5249 if ((!aborting() || did_throw)
5250 && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005251 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005252 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02005253 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02005254 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005255 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02005256 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005257 {
5258 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00005259 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005260 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005261 }
5262 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02005263 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005264 }
5265
5266end:
5267 dict_unref(fudi.fd_dict);
5268 vim_free(tofree);
5269}
5270
5271/*
5272 * Return from a function. Possibly makes the return pending. Also called
5273 * for a pending return at the ":endtry" or after returning from an extra
5274 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
5275 * when called due to a ":return" command. "rettv" may point to a typval_T
5276 * with the return rettv. Returns TRUE when the return can be carried out,
5277 * FALSE when the return gets pending.
5278 */
5279 int
5280do_return(
5281 exarg_T *eap,
5282 int reanimate,
5283 int is_cmd,
5284 void *rettv)
5285{
5286 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01005287 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005288
5289 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005290 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005291 current_funccal->returned = FALSE;
5292
5293 /*
5294 * Cleanup (and inactivate) conditionals, but stop when a try conditional
5295 * not in its finally clause (which then is to be executed next) is found.
5296 * In this case, make the ":return" pending for execution at the ":endtry".
5297 * Otherwise, return normally.
5298 */
5299 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
5300 if (idx >= 0)
5301 {
5302 cstack->cs_pending[idx] = CSTP_RETURN;
5303
5304 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005305 // A pending return again gets pending. "rettv" points to an
5306 // allocated variable with the rettv of the original ":return"'s
5307 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005308 cstack->cs_rettv[idx] = rettv;
5309 else
5310 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005311 // When undoing a return in order to make it pending, get the stored
5312 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005313 if (reanimate)
5314 rettv = current_funccal->rettv;
5315
5316 if (rettv != NULL)
5317 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005318 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005319 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
5320 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
5321 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005322 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005323 }
5324 else
5325 cstack->cs_rettv[idx] = NULL;
5326
5327 if (reanimate)
5328 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005329 // The pending return value could be overwritten by a ":return"
5330 // without argument in a finally clause; reset the default
5331 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005332 current_funccal->rettv->v_type = VAR_NUMBER;
5333 current_funccal->rettv->vval.v_number = 0;
5334 }
5335 }
5336 report_make_pending(CSTP_RETURN, rettv);
5337 }
5338 else
5339 {
5340 current_funccal->returned = TRUE;
5341
Bram Moolenaare38eab22019-12-05 21:50:01 +01005342 // If the return is carried out now, store the return value. For
5343 // a return immediately after reanimation, the value is already
5344 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005345 if (!reanimate && rettv != NULL)
5346 {
5347 clear_tv(current_funccal->rettv);
5348 *current_funccal->rettv = *(typval_T *)rettv;
5349 if (!is_cmd)
5350 vim_free(rettv);
5351 }
5352 }
5353
5354 return idx < 0;
5355}
5356
5357/*
5358 * Free the variable with a pending return value.
5359 */
5360 void
5361discard_pending_return(void *rettv)
5362{
5363 free_tv((typval_T *)rettv);
5364}
5365
5366/*
5367 * Generate a return command for producing the value of "rettv". The result
5368 * is an allocated string. Used by report_pending() for verbose messages.
5369 */
5370 char_u *
5371get_return_cmd(void *rettv)
5372{
5373 char_u *s = NULL;
5374 char_u *tofree = NULL;
5375 char_u numbuf[NUMBUFLEN];
5376
5377 if (rettv != NULL)
5378 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
5379 if (s == NULL)
5380 s = (char_u *)"";
5381
5382 STRCPY(IObuff, ":return ");
5383 STRNCPY(IObuff + 8, s, IOSIZE - 8);
5384 if (STRLEN(s) + 8 >= IOSIZE)
5385 STRCPY(IObuff + IOSIZE - 4, "...");
5386 vim_free(tofree);
5387 return vim_strsave(IObuff);
5388}
5389
5390/*
5391 * Get next function line.
5392 * Called by do_cmdline() to get the next line.
5393 * Returns allocated string, or NULL for end of function.
5394 */
5395 char_u *
5396get_func_line(
5397 int c UNUSED,
5398 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02005399 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005400 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005401{
5402 funccall_T *fcp = (funccall_T *)cookie;
5403 ufunc_T *fp = fcp->func;
5404 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005405 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005406
Bram Moolenaare38eab22019-12-05 21:50:01 +01005407 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005408 if (fcp->dbg_tick != debug_tick)
5409 {
5410 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005411 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005412 fcp->dbg_tick = debug_tick;
5413 }
5414#ifdef FEAT_PROFILE
5415 if (do_profiling == PROF_YES)
5416 func_line_end(cookie);
5417#endif
5418
5419 gap = &fp->uf_lines;
5420 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5421 || fcp->returned)
5422 retval = NULL;
5423 else
5424 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005425 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005426 while (fcp->linenr < gap->ga_len
5427 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
5428 ++fcp->linenr;
5429 if (fcp->linenr >= gap->ga_len)
5430 retval = NULL;
5431 else
5432 {
5433 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005434 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005435#ifdef FEAT_PROFILE
5436 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005437 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005438#endif
5439 }
5440 }
5441
Bram Moolenaare38eab22019-12-05 21:50:01 +01005442 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005443 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005444 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005445 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01005446 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005447 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005448 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005449 fcp->dbg_tick = debug_tick;
5450 }
5451
5452 return retval;
5453}
5454
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005455/*
5456 * Return TRUE if the currently active function should be ended, because a
5457 * return was encountered or an error occurred. Used inside a ":while".
5458 */
5459 int
5460func_has_ended(void *cookie)
5461{
5462 funccall_T *fcp = (funccall_T *)cookie;
5463
Bram Moolenaare38eab22019-12-05 21:50:01 +01005464 // Ignore the "abort" flag if the abortion behavior has been changed due to
5465 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005466 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5467 || fcp->returned);
5468}
5469
5470/*
5471 * return TRUE if cookie indicates a function which "abort"s on errors.
5472 */
5473 int
5474func_has_abort(
5475 void *cookie)
5476{
5477 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
5478}
5479
5480
5481/*
5482 * Turn "dict.Func" into a partial for "Func" bound to "dict".
5483 * Don't do this when "Func" is already a partial that was bound
5484 * explicitly (pt_auto is FALSE).
5485 * Changes "rettv" in-place.
5486 * Returns the updated "selfdict_in".
5487 */
5488 dict_T *
5489make_partial(dict_T *selfdict_in, typval_T *rettv)
5490{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005491 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005492 char_u *tofree = NULL;
5493 ufunc_T *fp;
5494 char_u fname_buf[FLEN_FIXED + 1];
5495 int error;
5496 dict_T *selfdict = selfdict_in;
5497
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005498 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
5499 fp = rettv->vval.v_partial->pt_func;
5500 else
5501 {
5502 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
5503 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005504 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005505 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005506 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005507 vim_free(tofree);
5508 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005509
5510 if (fp != NULL && (fp->uf_flags & FC_DICT))
5511 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005512 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005513
5514 if (pt != NULL)
5515 {
5516 pt->pt_refcount = 1;
5517 pt->pt_dict = selfdict;
5518 pt->pt_auto = TRUE;
5519 selfdict = NULL;
5520 if (rettv->v_type == VAR_FUNC)
5521 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005522 // Just a function: Take over the function name and use
5523 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005524 pt->pt_name = rettv->vval.v_string;
5525 }
5526 else
5527 {
5528 partial_T *ret_pt = rettv->vval.v_partial;
5529 int i;
5530
Bram Moolenaare38eab22019-12-05 21:50:01 +01005531 // Partial: copy the function name, use selfdict and copy
5532 // args. Can't take over name or args, the partial might
5533 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005534 if (ret_pt->pt_name != NULL)
5535 {
5536 pt->pt_name = vim_strsave(ret_pt->pt_name);
5537 func_ref(pt->pt_name);
5538 }
5539 else
5540 {
5541 pt->pt_func = ret_pt->pt_func;
5542 func_ptr_ref(pt->pt_func);
5543 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005544 if (ret_pt->pt_argc > 0)
5545 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005546 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005547 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005548 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005549 pt->pt_argc = 0;
5550 else
5551 {
5552 pt->pt_argc = ret_pt->pt_argc;
5553 for (i = 0; i < pt->pt_argc; i++)
5554 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
5555 }
5556 }
5557 partial_unref(ret_pt);
5558 }
5559 rettv->v_type = VAR_PARTIAL;
5560 rettv->vval.v_partial = pt;
5561 }
5562 }
5563 return selfdict;
5564}
5565
5566/*
5567 * Return the name of the executed function.
5568 */
5569 char_u *
5570func_name(void *cookie)
5571{
5572 return ((funccall_T *)cookie)->func->uf_name;
5573}
5574
5575/*
5576 * Return the address holding the next breakpoint line for a funccall cookie.
5577 */
5578 linenr_T *
5579func_breakpoint(void *cookie)
5580{
5581 return &((funccall_T *)cookie)->breakpoint;
5582}
5583
5584/*
5585 * Return the address holding the debug tick for a funccall cookie.
5586 */
5587 int *
5588func_dbg_tick(void *cookie)
5589{
5590 return &((funccall_T *)cookie)->dbg_tick;
5591}
5592
5593/*
5594 * Return the nesting level for a funccall cookie.
5595 */
5596 int
5597func_level(void *cookie)
5598{
5599 return ((funccall_T *)cookie)->level;
5600}
5601
5602/*
5603 * Return TRUE when a function was ended by a ":return" command.
5604 */
5605 int
5606current_func_returned(void)
5607{
5608 return current_funccal->returned;
5609}
5610
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005611 int
5612free_unref_funccal(int copyID, int testing)
5613{
5614 int did_free = FALSE;
5615 int did_free_funccal = FALSE;
5616 funccall_T *fc, **pfc;
5617
5618 for (pfc = &previous_funccal; *pfc != NULL; )
5619 {
5620 if (can_free_funccal(*pfc, copyID))
5621 {
5622 fc = *pfc;
5623 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01005624 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005625 did_free = TRUE;
5626 did_free_funccal = TRUE;
5627 }
5628 else
5629 pfc = &(*pfc)->caller;
5630 }
5631 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005632 // When a funccal was freed some more items might be garbage
5633 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005634 (void)garbage_collect(testing);
5635
5636 return did_free;
5637}
5638
5639/*
Bram Moolenaarba209902016-08-24 22:06:38 +02005640 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005641 */
5642 static funccall_T *
5643get_funccal(void)
5644{
5645 int i;
5646 funccall_T *funccal;
5647 funccall_T *temp_funccal;
5648
5649 funccal = current_funccal;
5650 if (debug_backtrace_level > 0)
5651 {
5652 for (i = 0; i < debug_backtrace_level; i++)
5653 {
5654 temp_funccal = funccal->caller;
5655 if (temp_funccal)
5656 funccal = temp_funccal;
5657 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005658 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005659 debug_backtrace_level = i;
5660 }
5661 }
5662 return funccal;
5663}
5664
5665/*
5666 * Return the hashtable used for local variables in the current funccal.
5667 * Return NULL if there is no current funccal.
5668 */
5669 hashtab_T *
5670get_funccal_local_ht()
5671{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005672 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005673 return NULL;
5674 return &get_funccal()->l_vars.dv_hashtab;
5675}
5676
5677/*
5678 * Return the l: scope variable.
5679 * Return NULL if there is no current funccal.
5680 */
5681 dictitem_T *
5682get_funccal_local_var()
5683{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005684 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005685 return NULL;
5686 return &get_funccal()->l_vars_var;
5687}
5688
5689/*
5690 * Return the hashtable used for argument in the current funccal.
5691 * Return NULL if there is no current funccal.
5692 */
5693 hashtab_T *
5694get_funccal_args_ht()
5695{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005696 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005697 return NULL;
5698 return &get_funccal()->l_avars.dv_hashtab;
5699}
5700
5701/*
5702 * Return the a: scope variable.
5703 * Return NULL if there is no current funccal.
5704 */
5705 dictitem_T *
5706get_funccal_args_var()
5707{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005708 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005709 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01005710 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005711}
5712
5713/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005714 * List function variables, if there is a function.
5715 */
5716 void
5717list_func_vars(int *first)
5718{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005719 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005720 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01005721 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005722}
5723
5724/*
5725 * If "ht" is the hashtable for local variables in the current funccal, return
5726 * the dict that contains it.
5727 * Otherwise return NULL.
5728 */
5729 dict_T *
5730get_current_funccal_dict(hashtab_T *ht)
5731{
5732 if (current_funccal != NULL
5733 && ht == &current_funccal->l_vars.dv_hashtab)
5734 return &current_funccal->l_vars;
5735 return NULL;
5736}
5737
5738/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005739 * Search hashitem in parent scope.
5740 */
5741 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005742find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005743{
5744 funccall_T *old_current_funccal = current_funccal;
5745 hashtab_T *ht;
5746 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005747 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005748
5749 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5750 return NULL;
5751
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02005752 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005753 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02005754 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005755 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005756 ht = find_var_ht(name, &varname);
5757 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02005758 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005759 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02005760 if (!HASHITEM_EMPTY(hi))
5761 {
5762 *pht = ht;
5763 break;
5764 }
5765 }
5766 if (current_funccal == current_funccal->func->uf_scoped)
5767 break;
5768 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005769 }
5770 current_funccal = old_current_funccal;
5771
5772 return hi;
5773}
5774
5775/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005776 * Search variable in parent scope.
5777 */
5778 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005779find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005780{
5781 dictitem_T *v = NULL;
5782 funccall_T *old_current_funccal = current_funccal;
5783 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005784 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005785
5786 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5787 return NULL;
5788
Bram Moolenaare38eab22019-12-05 21:50:01 +01005789 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005790 current_funccal = current_funccal->func->uf_scoped;
5791 while (current_funccal)
5792 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005793 ht = find_var_ht(name, &varname);
5794 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005795 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005796 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005797 if (v != NULL)
5798 break;
5799 }
5800 if (current_funccal == current_funccal->func->uf_scoped)
5801 break;
5802 current_funccal = current_funccal->func->uf_scoped;
5803 }
5804 current_funccal = old_current_funccal;
5805
5806 return v;
5807}
5808
5809/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005810 * Set "copyID + 1" in previous_funccal and callers.
5811 */
5812 int
5813set_ref_in_previous_funccal(int copyID)
5814{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005815 funccall_T *fc;
5816
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005817 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005818 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005819 fc->fc_copyID = copyID + 1;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005820 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
5821 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
5822 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL))
5823 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005824 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005825 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005826}
5827
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005828 static int
5829set_ref_in_funccal(funccall_T *fc, int copyID)
5830{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005831 if (fc->fc_copyID != copyID)
5832 {
5833 fc->fc_copyID = copyID;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005834 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
5835 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
5836 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
5837 || set_ref_in_func(NULL, fc->func, copyID))
5838 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005839 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005840 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005841}
5842
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005843/*
5844 * Set "copyID" in all local vars and arguments in the call stack.
5845 */
5846 int
5847set_ref_in_call_stack(int copyID)
5848{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005849 funccall_T *fc;
5850 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005851
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005852 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5853 if (set_ref_in_funccal(fc, copyID))
5854 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005855
5856 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005857 for (entry = funccal_stack; entry != NULL; entry = entry->next)
5858 for (fc = entry->top_funccal; fc != NULL; fc = fc->caller)
5859 if (set_ref_in_funccal(fc, copyID))
5860 return TRUE;
5861 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005862}
5863
5864/*
5865 * Set "copyID" in all functions available by name.
5866 */
5867 int
5868set_ref_in_functions(int copyID)
5869{
5870 int todo;
5871 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005872 ufunc_T *fp;
5873
5874 todo = (int)func_hashtab.ht_used;
5875 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005876 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005877 if (!HASHITEM_EMPTY(hi))
5878 {
5879 --todo;
5880 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005881 if (!func_name_refcount(fp->uf_name)
5882 && set_ref_in_func(NULL, fp, copyID))
5883 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005884 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005885 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005886 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005887}
5888
5889/*
5890 * Set "copyID" in all function arguments.
5891 */
5892 int
5893set_ref_in_func_args(int copyID)
5894{
5895 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005896
5897 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005898 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5899 copyID, NULL, NULL))
5900 return TRUE;
5901 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005902}
5903
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005904/*
5905 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005906 * Returns TRUE if setting references failed somehow.
5907 */
5908 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005909set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005910{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005911 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005912 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005913 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005914 char_u fname_buf[FLEN_FIXED + 1];
5915 char_u *tofree = NULL;
5916 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005917 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005918
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005919 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005920 return FALSE;
5921
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005922 if (fp_in == NULL)
5923 {
5924 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005925 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005926 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005927 if (fp != NULL)
5928 {
5929 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005930 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005931 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005932
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005933 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005934 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005935}
5936
Bram Moolenaare38eab22019-12-05 21:50:01 +01005937#endif // FEAT_EVAL