blob: c894c5896ef2761dbeaa134488455c0e64106d3f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
Bram Moolenaara9b579f2016-07-17 18:29:19 +020032static char *e_funcdict = N_("E717: Dictionary entry already exists");
33static char *e_funcref = N_("E718: Funcref required");
Bram Moolenaara9b579f2016-07-17 18:29:19 +020034
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020035static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010036static void func_clear(ufunc_T *fp, int force);
37static int func_free(ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020038
39 void
40func_init()
41{
42 hash_init(&func_hashtab);
43}
44
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020045/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020046 * Return the function hash table
47 */
48 hashtab_T *
49func_tbl_get(void)
50{
51 return &func_hashtab;
52}
53
54/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020055 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020056 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010057 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010058 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010059 * Return a pointer to after the type.
60 * When something is wrong return "arg".
61 */
62 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010063one_function_arg(
64 char_u *arg,
65 garray_T *newargs,
66 garray_T *argtypes,
67 int types_optional,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010068 evalarg_T *evalarg,
Bram Moolenaar2a389082021-04-09 20:24:31 +020069 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010070 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010071{
Bram Moolenaar6e949782020-04-13 17:21:00 +020072 char_u *p = arg;
73 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020074 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010075
76 while (ASCII_ISALNUM(*p) || *p == '_')
77 ++p;
78 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020079 || (argtypes == NULL
80 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
81 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082 {
83 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000084 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 return arg;
86 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010087
Bram Moolenaar057e84a2021-02-28 16:55:11 +010088 // Vim9 script: cannot use script var name for argument. In function: also
89 // check local vars and arguments.
90 if (!skip && argtypes != NULL && check_defined(arg, p - arg,
91 evalarg == NULL ? NULL : evalarg->eval_cctx, TRUE) == FAIL)
Bram Moolenaarb4893b82021-02-21 22:20:24 +010092 return arg;
Bram Moolenaarb4893b82021-02-21 22:20:24 +010093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010094 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
95 return arg;
96 if (newargs != NULL)
97 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098 int c;
99 int i;
100
101 c = *p;
102 *p = NUL;
103 arg_copy = vim_strsave(arg);
104 if (arg_copy == NULL)
105 {
106 *p = c;
107 return arg;
108 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200109 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200110 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200111 // Check for duplicate argument name.
112 for (i = 0; i < newargs->ga_len; ++i)
113 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
114 {
115 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
116 vim_free(arg_copy);
117 return arg;
118 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
120 newargs->ga_len++;
121
122 *p = c;
123 }
124
125 // get any type from "arg: type"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100126 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100127 {
128 char_u *type = NULL;
129
Bram Moolenaar6e949782020-04-13 17:21:00 +0200130 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
131 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200132 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200133 arg_copy == NULL ? arg : arg_copy);
134 p = skipwhite(p);
135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100136 if (*p == ':')
137 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200138 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100139 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200140 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100141 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200142 return arg;
143 }
144 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200145 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100146 if (!skip)
147 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100148 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200149 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200150 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200151 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200152 arg_copy == NULL ? arg : arg_copy);
153 return arg;
154 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100155 if (!skip)
156 {
157 if (type == NULL && types_optional)
158 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200159 type = vim_strsave((char_u *)
160 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100161 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
162 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163 }
164
165 return p;
166}
167
168/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000169 * Handle line continuation in function arguments or body.
170 * Get a next line, store it in "eap" if appropriate and use "line_to_free" to
171 * handle freeing the line later.
172 */
173 static char_u *
174get_function_line(
175 exarg_T *eap,
176 char_u **line_to_free,
177 getline_opt_T getline_options,
178 int indent)
179{
180 char_u *theline;
181
182 if (eap->getline == NULL)
183 theline = getcmdline(':', 0L, indent, getline_options);
184 else
185 theline = eap->getline(':', eap->cookie, indent, getline_options);
186 if (theline != NULL)
187 {
188 if (*eap->cmdlinep == *line_to_free)
189 *eap->cmdlinep = theline;
190 vim_free(*line_to_free);
191 *line_to_free = theline;
192 }
193
194 return theline;
195}
196
197/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200198 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100199 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100200 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200201 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100202 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200203get_function_args(
204 char_u **argp,
205 char_u endchar,
206 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100208 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100209 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200210 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200211 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200212 int skip,
213 exarg_T *eap,
214 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200215{
216 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100217 char_u *arg;
218 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200219 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200220 int any_default = FALSE;
221 char_u *expr;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100222 char_u *whitep = *argp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200223
224 if (newargs != NULL)
225 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 if (argtypes != NULL)
227 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200228 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200229 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200230
231 if (varargs != NULL)
232 *varargs = FALSE;
233
234 /*
235 * Isolate the arguments: "arg1, arg2, ...)"
236 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100237 arg = skipwhite(*argp);
238 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200239 while (*p != endchar)
240 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200241 while (eap != NULL && eap->getline != NULL
242 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200243 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200244 // End of the line, get the next one.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000245 char_u *theline = get_function_line(eap, line_to_free, 0, TRUE);
246
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200247 if (theline == NULL)
248 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200249 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200250 p = skipwhite(theline);
251 }
252
253 if (mustend && *p != endchar)
254 {
255 if (!skip)
256 semsg(_(e_invarg2), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100257 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200258 }
259 if (*p == endchar)
260 break;
261
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200262 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
263 {
264 if (varargs != NULL)
265 *varargs = TRUE;
266 p += 3;
267 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268
269 if (argtypes != NULL)
270 {
271 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200272 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100273 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100274 if (!skip)
275 emsg(_(e_missing_name_after_dots));
276 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277 }
278
279 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100280 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaar2a389082021-04-09 20:24:31 +0200281 evalarg, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 if (p == arg)
283 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100284 if (*skipwhite(p) == '=')
285 {
286 emsg(_(e_cannot_use_default_for_variable_arguments));
287 break;
288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100289 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200290 }
291 else
292 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200293 char_u *np;
294
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200295 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100296 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaar2a389082021-04-09 20:24:31 +0200297 evalarg, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200299 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200300
Bram Moolenaar015cf102021-06-26 21:52:02 +0200301 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200302 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200303 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200304 if (*np == '=' && np[1] != '=' && np[1] != '~'
305 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200306 {
307 typval_T rettv;
308
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100309 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200310 any_default = TRUE;
311 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200312 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200313 p = skipwhite(p);
314 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200315 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200316 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200317 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200318 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200319 if (ga_grow(default_args, 1) == FAIL)
320 goto err_ret;
321
322 // trim trailing whitespace
323 while (p > expr && VIM_ISWHITE(p[-1]))
324 p--;
325 c = *p;
326 *p = NUL;
327 expr = vim_strsave(expr);
328 if (expr == NULL)
329 {
330 *p = c;
331 goto err_ret;
332 }
333 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200334 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200335 default_args->ga_len++;
336 *p = c;
337 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200338 }
339 else
340 mustend = TRUE;
341 }
342 else if (any_default)
343 {
344 emsg(_("E989: Non-default argument follows default argument"));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200345 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200346 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200347
348 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
349 {
350 // Be tolerant when skipping
351 if (!skip)
352 {
353 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
354 goto err_ret;
355 }
356 p = skipwhite(p);
357 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200358 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200359 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200360 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200361 // Don't give this error when skipping, it makes the "->" not
362 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100363 // Allow missing space after comma in legacy functions.
364 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200365 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
366 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100367 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200368 goto err_ret;
369 }
370 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200371 else
372 mustend = TRUE;
373 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200374 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200375 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200376 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200377
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200378 if (*p != endchar)
379 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100380 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200381
382 *argp = p;
383 return OK;
384
385err_ret:
386 if (newargs != NULL)
387 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200388 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200389 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200390 return FAIL;
391}
392
393/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100394 * Parse the argument types, filling "fp->uf_arg_types".
395 * Return OK or FAIL.
396 */
397 static int
398parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
399{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200400 int len = 0;
401
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100402 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
403 if (argtypes->ga_len > 0)
404 {
405 // When "varargs" is set the last name/type goes into uf_va_name
406 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200407 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100408
409 if (len > 0)
410 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
411 if (fp->uf_arg_types != NULL)
412 {
413 int i;
414 type_T *type;
415
416 for (i = 0; i < len; ++ i)
417 {
418 char_u *p = ((char_u **)argtypes->ga_data)[i];
419
420 if (p == NULL)
421 // will get the type from the default value
422 type = &t_unknown;
423 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100424 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100425 if (type == NULL)
426 return FAIL;
427 fp->uf_arg_types[i] = type;
428 }
429 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100430 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200431
432 if (varargs)
433 {
434 char_u *p;
435
436 // Move the last argument "...name: type" to uf_va_name and
437 // uf_va_type.
438 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
439 [fp->uf_args.ga_len - 1];
440 --fp->uf_args.ga_len;
441 p = ((char_u **)argtypes->ga_data)[len];
442 if (p == NULL)
443 // TODO: get type from default value
444 fp->uf_va_type = &t_list_any;
445 else
446 {
447 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
448 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
449 {
450 semsg(_(e_variable_arguments_type_must_be_list_str),
451 ((char_u **)argtypes->ga_data)[len]);
452 return FAIL;
453 }
454 }
455 if (fp->uf_va_type == NULL)
456 return FAIL;
457 }
458
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100459 return OK;
460}
461
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100462 static int
463parse_return_type(ufunc_T *fp, char_u *ret_type)
464{
465 if (ret_type == NULL)
466 fp->uf_ret_type = &t_void;
467 else
468 {
469 char_u *p = ret_type;
470
471 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
472 if (fp->uf_ret_type == NULL)
473 {
474 fp->uf_ret_type = &t_void;
475 return FAIL;
476 }
477 }
478 return OK;
479}
480
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100481/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200482 * Register function "fp" as using "current_funccal" as its scope.
483 */
484 static int
485register_closure(ufunc_T *fp)
486{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200487 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100488 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200489 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200490 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200491 fp->uf_scoped = current_funccal;
492 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200493
Bram Moolenaar58016442016-07-31 18:30:22 +0200494 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
495 return FAIL;
496 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
497 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200498 return OK;
499}
500
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100501 static void
502set_ufunc_name(ufunc_T *fp, char_u *name)
503{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100504 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
505 // actually extends beyond the struct.
506 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100507
508 if (name[0] == K_SPECIAL)
509 {
510 fp->uf_name_exp = alloc(STRLEN(name) + 3);
511 if (fp->uf_name_exp != NULL)
512 {
513 STRCPY(fp->uf_name_exp, "<SNR>");
514 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
515 }
516 }
517}
518
Bram Moolenaar58016442016-07-31 18:30:22 +0200519/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200520 * Get a name for a lambda. Returned in static memory.
521 */
522 char_u *
523get_lambda_name(void)
524{
525 static char_u name[30];
526 static int lambda_no = 0;
527
528 sprintf((char*)name, "<lambda>%d", ++lambda_no);
529 return name;
530}
531
Bram Moolenaar801ab062020-06-25 19:27:56 +0200532#if defined(FEAT_LUA) || defined(PROTO)
533/*
534 * Registers a native C callback which can be called from Vim script.
535 * Returns the name of the Vim script function.
536 */
537 char_u *
538register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
539{
540 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200541 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200542
543 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
544 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200545 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200546
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200547 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200548 fp->uf_refcount = 1;
549 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000550 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200551 fp->uf_calls = 0;
552 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200553 fp->uf_cb = cb;
554 fp->uf_cb_free = cb_free;
555 fp->uf_cb_state = state;
556
557 set_ufunc_name(fp, name);
558 hash_add(&func_hashtab, UF2HIKEY(fp));
559
560 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200561}
562#endif
563
Bram Moolenaar04b12692020-05-04 23:24:44 +0200564/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100565 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100566 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100567 * If "white_error" is not NULL check for correct use of white space and set
568 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100569 * Return NULL if no valid arrow found.
570 */
571 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100572skip_arrow(
573 char_u *start,
574 int equal_arrow,
575 char_u **ret_type,
576 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100577{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100578 char_u *s = start;
579 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100580
581 if (equal_arrow)
582 {
583 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100584 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100585 if (white_error != NULL && !VIM_ISWHITE(s[1]))
586 {
587 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100588 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100589 return NULL;
590 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100591 s = skipwhite(s + 1);
592 *ret_type = s;
593 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100594 if (s == *ret_type)
595 {
596 emsg(_(e_missing_return_type));
597 return NULL;
598 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100599 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100600 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100601 s = skipwhite(s);
602 if (*s != '=')
603 return NULL;
604 ++s;
605 }
606 if (*s != '>')
607 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100608 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
609 || !IS_WHITE_OR_NUL(s[1])))
610 {
611 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100612 semsg(_(e_white_space_required_before_and_after_str_at_str),
613 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100614 return NULL;
615 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100616 return skipwhite(s + 1);
617}
618
619/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100620 * Check if "*cmd" points to a function command and if so advance "*cmd" and
621 * return TRUE.
622 * Otherwise return FALSE;
623 * Do not consider "function(" to be a command.
624 */
625 static int
626is_function_cmd(char_u **cmd)
627{
628 char_u *p = *cmd;
629
630 if (checkforcmd(&p, "function", 2))
631 {
632 if (*p == '(')
633 return FALSE;
634 *cmd = p;
635 return TRUE;
636 }
637 return FALSE;
638}
639
640/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200641 * Called when defining a function: The context may be needed for script
642 * variables declared in a block that is visible now but not when the function
643 * is compiled or called later.
644 */
645 static void
646function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
647{
648 if (cstack != NULL && cstack->cs_idx >= 0)
649 {
650 int count = cstack->cs_idx + 1;
651 int i;
652
653 fp->uf_block_ids = ALLOC_MULT(int, count);
654 if (fp->uf_block_ids != NULL)
655 {
656 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
657 sizeof(int) * count);
658 fp->uf_block_depth = count;
659 }
660
661 // Set flag in each block to indicate a function was defined. This
662 // is used to keep the variable when leaving the block, see
663 // hide_script_var().
664 for (i = 0; i <= cstack->cs_idx; ++i)
665 cstack->cs_flags[i] |= CSF_FUNC_DEF;
666 }
667}
668
669/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100670 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200671 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100672 * "newlines" must already have been initialized.
673 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
674 */
675 static int
676get_function_body(
677 exarg_T *eap,
678 garray_T *newlines,
679 char_u *line_arg_in,
680 char_u **line_to_free)
681{
682 linenr_T sourcing_lnum_top = SOURCING_LNUM;
683 linenr_T sourcing_lnum_off;
684 int saved_wait_return = need_wait_return;
685 char_u *line_arg = line_arg_in;
686 int vim9_function = eap->cmdidx == CMD_def
687 || eap->cmdidx == CMD_block;
688#define MAX_FUNC_NESTING 50
689 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200690 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100691 int nesting = 0;
692 getline_opt_T getline_options;
693 int indent = 2;
694 char_u *skip_until = NULL;
695 int ret = FAIL;
696 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200697 int heredoc_concat_len = 0;
698 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100699 char_u *heredoc_trimmed = NULL;
700
Bram Moolenaar20677332021-06-06 17:02:53 +0200701 ga_init2(&heredoc_ga, 1, 500);
702
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100703 // Detect having skipped over comment lines to find the return
704 // type. Add NULL lines to keep the line count correct.
705 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
706 if (SOURCING_LNUM < sourcing_lnum_off)
707 {
708 sourcing_lnum_off -= SOURCING_LNUM;
709 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
710 goto theend;
711 while (sourcing_lnum_off-- > 0)
712 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
713 }
714
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200715 nesting_def[0] = vim9_function;
716 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100717 getline_options = vim9_function
718 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
719 for (;;)
720 {
721 char_u *theline;
722 char_u *p;
723 char_u *arg;
724
725 if (KeyTyped)
726 {
727 msg_scroll = TRUE;
728 saved_wait_return = FALSE;
729 }
730 need_wait_return = FALSE;
731
732 if (line_arg != NULL)
733 {
734 // Use eap->arg, split up in parts by line breaks.
735 theline = line_arg;
736 p = vim_strchr(theline, '\n');
737 if (p == NULL)
738 line_arg += STRLEN(line_arg);
739 else
740 {
741 *p = NUL;
742 line_arg = p + 1;
743 }
744 }
745 else
746 {
Bram Moolenaar7473a842021-12-28 17:55:26 +0000747 theline = get_function_line(eap, line_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100748 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100749 }
750 if (KeyTyped)
751 lines_left = Rows - 1;
752 if (theline == NULL)
753 {
754 // Use the start of the function for the line number.
755 SOURCING_LNUM = sourcing_lnum_top;
756 if (skip_until != NULL)
757 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200758 else if (nesting_inline[nesting])
759 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100760 else if (eap->cmdidx == CMD_def)
761 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100762 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000763 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100764 goto theend;
765 }
766
767 // Detect line continuation: SOURCING_LNUM increased more than one.
768 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
769 if (SOURCING_LNUM < sourcing_lnum_off)
770 sourcing_lnum_off -= SOURCING_LNUM;
771 else
772 sourcing_lnum_off = 0;
773
774 if (skip_until != NULL)
775 {
776 // Don't check for ":endfunc"/":enddef" between
777 // * ":append" and "."
778 // * ":python <<EOF" and "EOF"
779 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
780 if (heredoc_trimmed == NULL
781 || (is_heredoc && skipwhite(theline) == theline)
782 || STRNCMP(theline, heredoc_trimmed,
783 STRLEN(heredoc_trimmed)) == 0)
784 {
785 if (heredoc_trimmed == NULL)
786 p = theline;
787 else if (is_heredoc)
788 p = skipwhite(theline) == theline
789 ? theline : theline + STRLEN(heredoc_trimmed);
790 else
791 p = theline + STRLEN(heredoc_trimmed);
792 if (STRCMP(p, skip_until) == 0)
793 {
794 VIM_CLEAR(skip_until);
795 VIM_CLEAR(heredoc_trimmed);
796 getline_options = vim9_function
797 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
798 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200799
800 if (heredoc_concat_len > 0)
801 {
802 // Replace the starting line with all the concatenated
803 // lines.
804 ga_concat(&heredoc_ga, theline);
805 vim_free(((char_u **)(newlines->ga_data))[
806 heredoc_concat_len - 1]);
807 ((char_u **)(newlines->ga_data))[
808 heredoc_concat_len - 1] = heredoc_ga.ga_data;
809 ga_init(&heredoc_ga);
810 heredoc_concat_len = 0;
811 theline += STRLEN(theline); // skip the "EOF"
812 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100813 }
814 }
815 }
816 else
817 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200818 int c;
819 char_u *end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100820
821 // skip ':' and blanks
822 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
823 ;
824
825 // Check for "endfunction", "enddef" or "}".
826 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200827 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100828 ? *p == '}'
829 : (checkforcmd(&p, nesting_def[nesting]
830 ? "enddef" : "endfunction", 4)
831 && *p != ':'))
832 {
833 if (nesting-- == 0)
834 {
835 char_u *nextcmd = NULL;
836
837 if (*p == '|' || *p == '}')
838 nextcmd = p + 1;
839 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
840 nextcmd = line_arg;
841 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100842 && (vim9_function || p_verbose > 0))
843 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200844 SOURCING_LNUM = sourcing_lnum_top
845 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100846 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000847 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100848 else
849 give_warning2((char_u *)
850 _("W22: Text found after :endfunction: %s"),
851 p, TRUE);
852 }
853 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100854 {
855 // Another command follows. If the line came from "eap"
856 // we can simply point into it, otherwise we need to
857 // change "eap->cmdlinep".
858 eap->nextcmd = nextcmd;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +0000859 if (*line_to_free != NULL
860 && *eap->cmdlinep != *line_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100861 {
862 vim_free(*eap->cmdlinep);
863 *eap->cmdlinep = *line_to_free;
864 *line_to_free = NULL;
865 }
866 }
867 break;
868 }
869 }
870
871 // Check for mismatched "endfunc" or "enddef".
872 // We don't check for "def" inside "func" thus we also can't check
873 // for "enddef".
874 // We continue to find the end of the function, although we might
875 // not find it.
876 else if (nesting_def[nesting])
877 {
878 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
879 emsg(_(e_mismatched_endfunction));
880 }
881 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
882 emsg(_(e_mismatched_enddef));
883
884 // Increase indent inside "if", "while", "for" and "try", decrease
885 // at "end".
886 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
887 indent -= 2;
888 else if (STRNCMP(p, "if", 2) == 0
889 || STRNCMP(p, "wh", 2) == 0
890 || STRNCMP(p, "for", 3) == 0
891 || STRNCMP(p, "try", 3) == 0)
892 indent += 2;
893
894 // Check for defining a function inside this function.
895 // Only recognize "def" inside "def", not inside "function",
896 // For backwards compatibility, see Test_function_python().
897 c = *p;
898 if (is_function_cmd(&p)
899 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
900 {
901 if (*p == '!')
902 p = skipwhite(p + 1);
903 p += eval_fname_script(p);
904 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
905 NULL, NULL));
906 if (*skipwhite(p) == '(')
907 {
908 if (nesting == MAX_FUNC_NESTING - 1)
909 emsg(_(e_function_nesting_too_deep));
910 else
911 {
912 ++nesting;
913 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200914 nesting_inline[nesting] = FALSE;
915 indent += 2;
916 }
917 }
918 }
919
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200920 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200921 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200922 // Not a comment line: check for nested inline function.
923 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200924 while (end > p && VIM_ISWHITE(*end))
925 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +0200926 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200927 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200928 int is_block;
929
930 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200931 --end;
932 while (end > p && VIM_ISWHITE(*end))
933 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200934 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
935 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200936 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200937 char_u *s = p;
938
939 // check for line starting with "au" for :autocmd or
940 // "com" for :command, these can use a {} block
941 is_block = checkforcmd_noparen(&s, "autocmd", 2)
942 || checkforcmd_noparen(&s, "command", 3);
943 }
944
945 if (is_block)
946 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200947 if (nesting == MAX_FUNC_NESTING - 1)
948 emsg(_(e_function_nesting_too_deep));
949 else
950 {
951 ++nesting;
952 nesting_def[nesting] = TRUE;
953 nesting_inline[nesting] = TRUE;
954 indent += 2;
955 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100956 }
957 }
958 }
959
960 // Check for ":append", ":change", ":insert". Not for :def.
961 p = skip_range(p, FALSE, NULL);
962 if (!vim9_function
963 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
964 || (p[0] == 'c'
965 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
966 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
967 && (STRNCMP(&p[3], "nge", 3) != 0
968 || !ASCII_ISALPHA(p[6])))))))
969 || (p[0] == 'i'
970 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
971 && (!ASCII_ISALPHA(p[2])
972 || (p[2] == 's'
973 && (!ASCII_ISALPHA(p[3])
974 || p[3] == 'e'))))))))
975 skip_until = vim_strsave((char_u *)".");
976
977 // Check for ":python <<EOF", ":tcl <<EOF", etc.
978 arg = skipwhite(skiptowhite(p));
979 if (arg[0] == '<' && arg[1] =='<'
980 && ((p[0] == 'p' && p[1] == 'y'
981 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
982 || ((p[2] == '3' || p[2] == 'x')
983 && !ASCII_ISALPHA(p[3]))))
984 || (p[0] == 'p' && p[1] == 'e'
985 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
986 || (p[0] == 't' && p[1] == 'c'
987 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
988 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
989 && !ASCII_ISALPHA(p[3]))
990 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
991 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
992 || (p[0] == 'm' && p[1] == 'z'
993 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
994 ))
995 {
996 // ":python <<" continues until a dot, like ":append"
997 p = skipwhite(arg + 2);
998 if (STRNCMP(p, "trim", 4) == 0)
999 {
1000 // Ignore leading white space.
1001 p = skipwhite(p + 4);
1002 heredoc_trimmed = vim_strnsave(theline,
1003 skipwhite(theline) - theline);
1004 }
1005 if (*p == NUL)
1006 skip_until = vim_strsave((char_u *)".");
1007 else
1008 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1009 getline_options = GETLINE_NONE;
1010 is_heredoc = TRUE;
Bram Moolenaar20677332021-06-06 17:02:53 +02001011 if (eap->cmdidx == CMD_def)
1012 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001013 }
1014
1015 // Check for ":cmd v =<< [trim] EOF"
1016 // and ":cmd [a, b] =<< [trim] EOF"
1017 // and "lines =<< [trim] EOF" for Vim9
1018 // Where "cmd" can be "let", "var", "final" or "const".
1019 arg = skipwhite(skiptowhite(p));
1020 if (*arg == '[')
1021 arg = vim_strchr(arg, ']');
1022 if (arg != NULL)
1023 {
1024 int found = (eap->cmdidx == CMD_def && arg[0] == '='
1025 && arg[1] == '<' && arg[2] =='<');
1026
1027 if (!found)
1028 // skip over the argument after "cmd"
1029 arg = skipwhite(skiptowhite(arg));
1030 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
1031 && (checkforcmd(&p, "let", 2)
1032 || checkforcmd(&p, "var", 3)
1033 || checkforcmd(&p, "final", 5)
1034 || checkforcmd(&p, "const", 5))))
1035 {
1036 p = skipwhite(arg + 3);
1037 if (STRNCMP(p, "trim", 4) == 0)
1038 {
1039 // Ignore leading white space.
1040 p = skipwhite(p + 4);
1041 heredoc_trimmed = vim_strnsave(theline,
1042 skipwhite(theline) - theline);
1043 }
1044 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1045 getline_options = GETLINE_NONE;
1046 is_heredoc = TRUE;
1047 }
1048 }
1049 }
1050
1051 // Add the line to the function.
1052 if (ga_grow(newlines, 1 + sourcing_lnum_off) == FAIL)
1053 goto theend;
1054
Bram Moolenaar20677332021-06-06 17:02:53 +02001055 if (heredoc_concat_len > 0)
1056 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001057 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001058 // to be used for the instruction later.
1059 ga_concat(&heredoc_ga, theline);
1060 ga_concat(&heredoc_ga, (char_u *)"\n");
1061 p = vim_strsave((char_u *)"");
1062 }
1063 else
1064 {
1065 // Copy the line to newly allocated memory. get_one_sourceline()
1066 // allocates 250 bytes per line, this saves 80% on average. The
1067 // cost is an extra alloc/free.
1068 p = vim_strsave(theline);
1069 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001070 if (p == NULL)
1071 goto theend;
1072 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1073
1074 // Add NULL lines for continuation lines, so that the line count is
1075 // equal to the index in the growarray.
1076 while (sourcing_lnum_off-- > 0)
1077 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1078
1079 // Check for end of eap->arg.
1080 if (line_arg != NULL && *line_arg == NUL)
1081 line_arg = NULL;
1082 }
1083
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001084 // Return OK when no error was detected.
1085 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001086 ret = OK;
1087
1088theend:
1089 vim_free(skip_until);
1090 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001091 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001092 need_wait_return |= saved_wait_return;
1093 return ret;
1094}
1095
1096/*
1097 * Handle the body of a lambda. *arg points to the "{", process statements
1098 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001099 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001100 * When successful "rettv" is set to a funcref.
1101 */
1102 static int
1103lambda_function_body(
1104 char_u **arg,
1105 typval_T *rettv,
1106 evalarg_T *evalarg,
1107 garray_T *newargs,
1108 garray_T *argtypes,
1109 int varargs,
1110 garray_T *default_args,
1111 char_u *ret_type)
1112{
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001113 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001114 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001115 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001116 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001117 exarg_T eap;
1118 garray_T newlines;
1119 char_u *cmdline = NULL;
1120 int ret = FAIL;
1121 char_u *line_to_free = NULL;
1122 partial_T *pt;
1123 char_u *name;
1124 int lnum_save = -1;
1125 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1126
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001127 if (!ends_excmd2(*arg, skipwhite(*arg + 1)))
1128 {
1129 semsg(_(e_trailing_arg), *arg + 1);
1130 return FAIL;
1131 }
1132
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001133 CLEAR_FIELD(eap);
1134 eap.cmdidx = CMD_block;
1135 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001136 eap.cmdlinep = &cmdline;
1137 eap.skip = !evaluate;
1138 if (evalarg->eval_cctx != NULL)
1139 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1140 else
1141 {
1142 eap.getline = evalarg->eval_getline;
1143 eap.cookie = evalarg->eval_cookie;
1144 }
1145
1146 ga_init2(&newlines, (int)sizeof(char_u *), 10);
1147 if (get_function_body(&eap, &newlines, NULL, &line_to_free) == FAIL)
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001148 {
Bram Moolenaar8c697e32021-12-28 20:18:50 +00001149 if (cmdline != line_to_free)
1150 vim_free(cmdline);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001151 goto erret;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001152 }
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001153
1154 // When inside a lambda must add the function lines to evalarg.eval_ga.
1155 evalarg->eval_break_count += newlines.ga_len;
1156 if (gap->ga_itemsize > 0)
1157 {
1158 int idx;
1159 char_u *last;
1160 size_t plen;
1161 char_u *pnl;
1162
1163 for (idx = 0; idx < newlines.ga_len; ++idx)
1164 {
1165 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1166
Bram Moolenaarecb66452021-05-18 15:09:18 +02001167 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001168 goto erret;
1169
1170 // Going to concatenate the lines after parsing. For an empty or
1171 // comment line use an empty string.
1172 // Insert NL characters at the start of each line, the string will
1173 // be split again later in .get_lambda_tv().
1174 if (*p == NUL || vim9_comment_start(p))
1175 p = (char_u *)"";
1176 plen = STRLEN(p);
1177 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1178 if (pnl != NULL)
1179 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001180 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1181 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001182 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001183 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001184 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001185 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001186 // more is following after the "}", which was skipped
1187 last = cmdline;
1188 else
1189 // nothing is following the "}"
1190 last = (char_u *)"}";
1191 plen = STRLEN(last);
1192 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1193 if (pnl != NULL)
1194 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001195 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1196 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001197 }
1198
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001199 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001200 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001201 garray_T *tfgap = &evalarg->eval_tofree_ga;
1202
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001203 // Something comes after the "}".
1204 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001205
1206 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001207 if (ga_grow(tfgap, 1) == OK)
1208 {
1209 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1210 evalarg->eval_using_cmdline = TRUE;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001211 if (cmdline == line_to_free)
1212 line_to_free = NULL;
Bram Moolenaar844fb642021-10-23 13:32:30 +01001213 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001214 }
1215 else
1216 *arg = (char_u *)"";
1217
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001218 if (!evaluate)
1219 {
1220 ret = OK;
1221 goto erret;
1222 }
1223
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001224 name = get_lambda_name();
1225 ufunc = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
1226 if (ufunc == NULL)
1227 goto erret;
1228 set_ufunc_name(ufunc, name);
1229 if (hash_add(&func_hashtab, UF2HIKEY(ufunc)) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001230 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001231 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001232 ufunc->uf_refcount = 1;
1233 ufunc->uf_args = *newargs;
1234 newargs->ga_data = NULL;
1235 ufunc->uf_def_args = *default_args;
1236 default_args->ga_data = NULL;
1237 ufunc->uf_func_type = &t_func_any;
1238
1239 // error messages are for the first function line
1240 lnum_save = SOURCING_LNUM;
1241 SOURCING_LNUM = sourcing_lnum_top;
1242
1243 // parse argument types
1244 if (parse_argument_types(ufunc, argtypes, varargs) == FAIL)
1245 {
1246 SOURCING_LNUM = lnum_save;
1247 goto erret;
1248 }
1249
1250 // parse the return type, if any
1251 if (parse_return_type(ufunc, ret_type) == FAIL)
1252 goto erret;
1253
1254 pt = ALLOC_CLEAR_ONE(partial_T);
1255 if (pt == NULL)
1256 goto erret;
1257 pt->pt_func = ufunc;
1258 pt->pt_refcount = 1;
1259
1260 ufunc->uf_lines = newlines;
1261 newlines.ga_data = NULL;
1262 if (sandbox)
1263 ufunc->uf_flags |= FC_SANDBOX;
1264 if (!ASCII_ISUPPER(*ufunc->uf_name))
1265 ufunc->uf_flags |= FC_VIM9;
1266 ufunc->uf_script_ctx = current_sctx;
1267 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1268 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1269 set_function_type(ufunc);
1270
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001271 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1272
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001273 rettv->vval.v_partial = pt;
1274 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001275 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001276 ret = OK;
1277
1278erret:
1279 if (lnum_save >= 0)
1280 SOURCING_LNUM = lnum_save;
1281 vim_free(line_to_free);
1282 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001283 if (newargs != NULL)
1284 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001285 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001286 if (ufunc != NULL)
1287 {
1288 func_clear(ufunc, TRUE);
1289 func_free(ufunc, TRUE);
1290 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001291 return ret;
1292}
1293
1294/*
1295 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001296 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001297 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001298 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1299 */
1300 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001301get_lambda_tv(
1302 char_u **arg,
1303 typval_T *rettv,
1304 int types_optional,
1305 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001306{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001307 int evaluate = evalarg != NULL
1308 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001309 garray_T newargs;
1310 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001311 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001312 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001313 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001314 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001315 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001316 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001317 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001318 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001319 char_u *s;
1320 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001321 int *old_eval_lavars = eval_lavars_used;
1322 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001323 char_u *tofree1 = NULL;
1324 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001325 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001326 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001327 int called_emsg_start = called_emsg;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001328
1329 if (equal_arrow && !in_vim9script())
1330 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001331
1332 ga_init(&newargs);
1333 ga_init(&newlines);
1334
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001335 // First, check if this is really a lambda expression. "->" or "=>" must
1336 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001337 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001338 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001339 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar14ded112021-06-26 19:25:49 +02001340 NULL, &default_args, TRUE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001341 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001342 {
1343 if (types_optional)
1344 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +01001345 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001346 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001347
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001348 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001349 if (evaluate)
1350 pnewargs = &newargs;
1351 else
1352 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001353 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001354 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001355 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001356 &varargs, &default_args,
1357 FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001358 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001359 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaare462f522020-12-27 14:43:30 +01001360 equal_arrow || in_vim9script() ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001361 {
1362 if (types_optional)
1363 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001364 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001365 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001366 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001367 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001368
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001369 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1370 if (ret_type != NULL)
1371 {
1372 ret_type = vim_strsave(ret_type);
1373 tofree2 = ret_type;
1374 }
1375
Bram Moolenaare38eab22019-12-05 21:50:01 +01001376 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001377 if (evaluate)
1378 eval_lavars_used = &eval_lavars;
1379
Bram Moolenaar65c44152020-12-24 15:14:01 +01001380 *arg = skipwhite_and_linebreak(*arg, evalarg);
1381
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001382 // Recognize "{" as the start of a function body.
1383 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001384 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001385 if (evalarg == NULL)
1386 // cannot happen?
1387 goto theend;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001388 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1389 types_optional ? &argtypes : NULL, varargs,
1390 &default_args, ret_type) == FAIL)
1391 goto errret;
1392 goto theend;
1393 }
1394 if (default_args.ga_len > 0)
1395 {
1396 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001397 goto errret;
1398 }
1399
Bram Moolenaare38eab22019-12-05 21:50:01 +01001400 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001401 start = *arg;
1402 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001403 if (ret == FAIL)
1404 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001405 if (evalarg != NULL)
1406 {
1407 // avoid that the expression gets freed when another line break follows
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001408 tofree1 = evalarg->eval_tofree;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001409 evalarg->eval_tofree = NULL;
1410 }
1411
Bram Moolenaar65c44152020-12-24 15:14:01 +01001412 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001413 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001414 *arg = skipwhite_and_linebreak(*arg, evalarg);
1415 if (**arg != '}')
1416 {
1417 semsg(_("E451: Expected }: %s"), *arg);
1418 goto errret;
1419 }
1420 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001421 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001422
1423 if (evaluate)
1424 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001425 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001426 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001427 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001428 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001429 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001430
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001431 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001432 if (fp == NULL)
1433 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001434 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001435 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001436 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001437 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001438
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001439 ga_init2(&newlines, (int)sizeof(char_u *), 1);
1440 if (ga_grow(&newlines, 1) == FAIL)
1441 goto errret;
1442
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001443 // If there are line breaks, we need to split up the string.
1444 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001445 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001446 line_end = end;
1447
1448 // Add "return " before the expression (or the first line).
1449 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001450 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001451 if (p == NULL)
1452 goto errret;
1453 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1454 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001455 vim_strncpy(p + 7, start, line_end - start);
1456
1457 if (line_end != end)
1458 {
1459 // Add more lines, split by line breaks. Thus is used when a
1460 // lambda with { cmds } is encountered.
1461 while (*line_end == '\n')
1462 {
1463 if (ga_grow(&newlines, 1) == FAIL)
1464 goto errret;
1465 start = line_end + 1;
1466 line_end = vim_strchr(start, '\n');
1467 if (line_end == NULL)
1468 line_end = end;
1469 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1470 vim_strnsave(start, line_end - start);
1471 }
1472 }
1473
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001474 if (strstr((char *)p + 7, "a:") == NULL)
1475 // No a: variables are used for sure.
1476 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001477
1478 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001479 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001480 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001481 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001482 if (types_optional)
1483 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001484 if (parse_argument_types(fp, &argtypes,
1485 in_vim9script() && varargs) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001486 goto errret;
1487 if (ret_type != NULL)
1488 {
1489 fp->uf_ret_type = parse_type(&ret_type,
1490 &fp->uf_type_list, TRUE);
1491 if (fp->uf_ret_type == NULL)
1492 goto errret;
1493 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001494 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001495 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001496 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001497
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001498 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001499 if (current_funccal != NULL && eval_lavars)
1500 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001501 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001502 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001503 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001504 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001505
1506#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001507 if (prof_def_func())
1508 func_do_profile(fp);
1509#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001510 if (sandbox)
1511 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001512 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001513 // uf_args.ga_len. In Vim9 script "...name" has to be used.
1514 fp->uf_varargs = !in_vim9script() || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001515 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001516 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001517 fp->uf_script_ctx = current_sctx;
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001518 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len + 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001519
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001520 function_using_block_scopes(fp, evalarg->eval_cstack);
1521
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001522 pt->pt_func = fp;
1523 pt->pt_refcount = 1;
1524 rettv->vval.v_partial = pt;
1525 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001526
1527 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001528 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001529
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001530theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001531 eval_lavars_used = old_eval_lavars;
Bram Moolenaarefaaaa62020-07-08 22:24:09 +02001532 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001533 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001534 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001535 vim_free(tofree1);
1536 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001537 if (types_optional)
1538 ga_clear_strings(&argtypes);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001539
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001540 return OK;
1541
1542errret:
1543 ga_clear_strings(&newargs);
1544 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001545 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001546 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001547 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001548 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001549 if (fp != NULL)
1550 vim_free(fp->uf_arg_types);
1551 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001552 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001553 vim_free(pt);
Bram Moolenaarefaaaa62020-07-08 22:24:09 +02001554 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001555 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001556 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001557 vim_free(tofree1);
1558 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001559 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001560 return FAIL;
1561}
1562
1563/*
1564 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1565 * name it contains, otherwise return "name".
1566 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1567 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001568 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1569 * type of the variable.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001570 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001571 */
1572 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001573deref_func_name(
1574 char_u *name,
1575 int *lenp,
1576 partial_T **partialp,
1577 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001578 int no_autoload,
1579 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001580{
1581 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001582 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001583 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001584 char_u *s = NULL;
1585 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001586 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001587
1588 if (partialp != NULL)
1589 *partialp = NULL;
1590
1591 cc = name[*lenp];
1592 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001593
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001594 v = find_var(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001595 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001596 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001597 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001598 tv = &v->di_tv;
1599 }
1600 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1601 {
1602 imported_T *import;
1603 char_u *p = name;
1604 int len = *lenp;
1605
1606 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001607 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001608 p = name + 2;
1609 len -= 2;
1610 }
1611 import = find_imported(p, len, NULL);
1612
1613 // imported variable from another script
1614 if (import != NULL)
1615 {
1616 if (import->imp_funcname != NULL)
1617 {
1618 s = import->imp_funcname;
1619 *lenp = (int)STRLEN(s);
1620 return s;
1621 }
Bram Moolenaarf8a79fc2021-12-14 12:06:16 +00001622 if (import->imp_flags & IMP_FLAGS_STAR)
1623 {
1624 name[len] = NUL;
1625 semsg(_(e_cannot_use_str_itself_it_is_imported_with_star),
1626 name);
1627 name[len] = cc;
1628 *lenp = 0;
1629 return (char_u *)""; // just in case
1630 }
1631 else
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001632 {
1633 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
1634 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1635 + import->imp_var_vals_idx;
1636 tv = sv->sv_tv;
1637 if (type != NULL)
1638 *type = sv->sv_type;
1639 did_type = TRUE;
1640 }
1641 }
1642 }
1643
1644 if (tv != NULL)
1645 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001646 if (found_var != NULL)
1647 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001648 if (tv->v_type == VAR_FUNC)
1649 {
1650 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001651 {
1652 *lenp = 0;
1653 return (char_u *)""; // just in case
1654 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001655 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001656 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001657 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001658
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001659 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001660 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001661 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001662
1663 if (pt == NULL)
1664 {
1665 *lenp = 0;
1666 return (char_u *)""; // just in case
1667 }
1668 if (partialp != NULL)
1669 *partialp = pt;
1670 s = partial_name(pt);
1671 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001672 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001673
1674 if (s != NULL)
1675 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001676 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001677 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001678 svar_T *sv = find_typval_in_script(tv);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001679
1680 if (sv != NULL)
1681 *type = sv->sv_type;
1682 }
1683 return s;
1684 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001685 }
1686
1687 return name;
1688}
1689
1690/*
1691 * Give an error message with a function name. Handle <SNR> things.
1692 * "ermsg" is to be passed without translation, use N_() instead of _().
1693 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001694 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001695emsg_funcname(char *ermsg, char_u *name)
1696{
1697 char_u *p;
1698
1699 if (*name == K_SPECIAL)
1700 p = concat_str((char_u *)"<SNR>", name + 3);
1701 else
1702 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001703 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001704 if (p != name)
1705 vim_free(p);
1706}
1707
1708/*
1709 * Allocate a variable for the result of a function.
1710 * Return OK or FAIL.
1711 */
1712 int
1713get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001714 char_u *name, // name of the function
1715 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001716 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001717 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +02001718 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001719 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001720{
1721 char_u *argp;
1722 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001723 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1724 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001725 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001726
1727 /*
1728 * Get the arguments.
1729 */
1730 argp = *arg;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001731 while (argcount < MAX_FUNC_ARGS - (funcexe->fe_partial == NULL ? 0
1732 : funcexe->fe_partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001733 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001734 // skip the '(' or ',' and possibly line breaks
1735 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1736
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001737 if (*argp == ')' || *argp == ',' || *argp == NUL)
1738 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001739 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001740 {
1741 ret = FAIL;
1742 break;
1743 }
1744 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001745 // The comma should come right after the argument, but this wasn't
1746 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001747 if (vim9script)
1748 {
1749 if (*argp != ',' && *skipwhite(argp) == ',')
1750 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001751 semsg(_(e_no_white_space_allowed_before_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001752 ret = FAIL;
1753 break;
1754 }
1755 }
1756 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001757 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001758 if (*argp != ',')
1759 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001760 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1761 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001762 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001763 ret = FAIL;
1764 break;
1765 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001766 }
Bram Moolenaare6b53242020-07-01 17:28:33 +02001767 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001768 if (*argp == ')')
1769 ++argp;
1770 else
1771 ret = FAIL;
1772
1773 if (ret == OK)
1774 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001775 int i = 0;
1776 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001777
1778 if (get_vim_var_nr(VV_TESTING))
1779 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001780 // Prepare for calling test_garbagecollect_now(), need to know
1781 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001782 if (funcargs.ga_itemsize == 0)
1783 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
1784 for (i = 0; i < argcount; ++i)
1785 if (ga_grow(&funcargs, 1) == OK)
1786 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1787 &argvars[i];
1788 }
1789
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001790 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001791 if (in_vim9script() && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001792 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001793 // An error in a builtin function does not return FAIL, but we do
1794 // want to abort further processing if an error was given.
1795 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001796 clear_tv(rettv);
1797 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001798
1799 funcargs.ga_len -= i;
1800 }
1801 else if (!aborting())
1802 {
1803 if (argcount == MAX_FUNC_ARGS)
1804 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
1805 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001806 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001807 }
1808
1809 while (--argcount >= 0)
1810 clear_tv(&argvars[argcount]);
1811
Bram Moolenaar8294d492020-08-10 22:40:56 +02001812 if (in_vim9script())
1813 *arg = argp;
1814 else
1815 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001816 return ret;
1817}
1818
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001819/*
1820 * Return TRUE if "p" starts with "<SID>" or "s:".
1821 * Only works if eval_fname_script() returned non-zero for "p"!
1822 */
1823 static int
1824eval_fname_sid(char_u *p)
1825{
1826 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1827}
1828
1829/*
1830 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1831 * Change <SNR>123_name() to K_SNR 123_name().
1832 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
1833 * (slow).
1834 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001835 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001836fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
1837{
1838 int llen;
1839 char_u *fname;
1840 int i;
1841
1842 llen = eval_fname_script(name);
1843 if (llen > 0)
1844 {
1845 fname_buf[0] = K_SPECIAL;
1846 fname_buf[1] = KS_EXTRA;
1847 fname_buf[2] = (int)KE_SNR;
1848 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001849 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001850 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001851 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001852 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001853 else
1854 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001855 sprintf((char *)fname_buf + 3, "%ld_",
1856 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001857 i = (int)STRLEN(fname_buf);
1858 }
1859 }
1860 if (i + STRLEN(name + llen) < FLEN_FIXED)
1861 {
1862 STRCPY(fname_buf + i, name + llen);
1863 fname = fname_buf;
1864 }
1865 else
1866 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001867 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001868 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +01001869 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001870 else
1871 {
1872 *tofree = fname;
1873 mch_memmove(fname, fname_buf, (size_t)i);
1874 STRCPY(fname + i, name + llen);
1875 }
1876 }
1877 }
1878 else
1879 fname = name;
1880 return fname;
1881}
1882
1883/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 * Find a function "name" in script "sid".
1885 */
1886 static ufunc_T *
1887find_func_with_sid(char_u *name, int sid)
1888{
1889 hashitem_T *hi;
1890 char_u buffer[200];
1891
1892 buffer[0] = K_SPECIAL;
1893 buffer[1] = KS_EXTRA;
1894 buffer[2] = (int)KE_SNR;
1895 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
1896 (long)sid, name);
1897 hi = hash_find(&func_hashtab, buffer);
1898 if (!HASHITEM_EMPTY(hi))
1899 return HI2UF(hi);
1900
1901 return NULL;
1902}
1903
1904/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001905 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001906 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001907 * Return NULL for unknown function.
1908 */
Bram Moolenaarce658352020-07-31 23:47:12 +02001909 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001910find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001911{
1912 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001913 ufunc_T *func;
1914 imported_T *imported;
1915
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001916 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 {
Bram Moolenaar333894b2020-08-01 18:53:07 +02001918 char_u *after_script = NULL;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001919 long sid = 0;
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001920 int find_script_local = in_vim9script() && eval_isnamec1(*name)
1921 && (name[1] != ':' || *name == 's');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922
Bram Moolenaar95006e32020-08-29 17:47:08 +02001923 if (find_script_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001925 // Find script-local function before global one.
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001926 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
1927 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001928 if (func != NULL)
1929 return func;
1930 }
1931
Bram Moolenaarefa94442020-08-08 22:16:00 +02001932 if (name[0] == K_SPECIAL
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001933 && name[1] == KS_EXTRA
1934 && name[2] == KE_SNR)
1935 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001936 // Caller changes s: to <SNR>99_name.
1937
1938 after_script = name + 3;
1939 sid = getdigits(&after_script);
Bram Moolenaarefa94442020-08-08 22:16:00 +02001940 if (*after_script == '_')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001941 ++after_script;
1942 else
1943 after_script = NULL;
1944 }
Bram Moolenaar95006e32020-08-29 17:47:08 +02001945 if (find_script_local || after_script != NULL)
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001946 {
1947 // Find imported function before global one.
Bram Moolenaarefa94442020-08-08 22:16:00 +02001948 if (after_script != NULL && sid != current_sctx.sc_sid)
1949 imported = find_imported_in_script(after_script, 0, sid);
1950 else
1951 imported = find_imported(after_script == NULL
1952 ? name : after_script, 0, cctx);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001953 if (imported != NULL && imported->imp_funcname != NULL)
1954 {
1955 hi = hash_find(&func_hashtab, imported->imp_funcname);
1956 if (!HASHITEM_EMPTY(hi))
1957 return HI2UF(hi);
1958 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 }
1960 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001961
Bram Moolenaara26b9702020-04-18 19:53:28 +02001962 hi = hash_find(&func_hashtab,
1963 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001964 if (!HASHITEM_EMPTY(hi))
1965 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966
1967 return NULL;
1968}
1969
1970/*
1971 * Find a function by name, return pointer to it in ufuncs.
1972 * "cctx" is passed in a :def function to find imported functions.
1973 * Return NULL for unknown or dead function.
1974 */
1975 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001976find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001978 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979
1980 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
1981 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001982 return NULL;
1983}
1984
1985/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02001986 * Return TRUE if "ufunc" is a global function.
1987 */
1988 int
1989func_is_global(ufunc_T *ufunc)
1990{
1991 return ufunc->uf_name[0] != K_SPECIAL;
1992}
1993
1994/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001995 * Copy the function name of "fp" to buffer "buf".
1996 * "buf" must be able to hold the function name plus three bytes.
1997 * Takes care of script-local function names.
1998 */
1999 static void
2000cat_func_name(char_u *buf, ufunc_T *fp)
2001{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002002 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002003 {
2004 STRCPY(buf, "<SNR>");
2005 STRCAT(buf, fp->uf_name + 3);
2006 }
2007 else
2008 STRCPY(buf, fp->uf_name);
2009}
2010
2011/*
2012 * Add a number variable "name" to dict "dp" with value "nr".
2013 */
2014 static void
2015add_nr_var(
2016 dict_T *dp,
2017 dictitem_T *v,
2018 char *name,
2019 varnumber_T nr)
2020{
2021 STRCPY(v->di_key, name);
2022 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2023 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
2024 v->di_tv.v_type = VAR_NUMBER;
2025 v->di_tv.v_lock = VAR_FIXED;
2026 v->di_tv.vval.v_number = nr;
2027}
2028
2029/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002030 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002031 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002032 static void
2033free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002034{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002035 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002036
2037 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2038 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002039 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002040
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002041 // When garbage collecting a funccall_T may be freed before the
2042 // function that references it, clear its uf_scoped field.
2043 // The function may have been redefined and point to another
2044 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002045 if (fp != NULL && fp->uf_scoped == fc)
2046 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002047 }
Bram Moolenaar58016442016-07-31 18:30:22 +02002048 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002049
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002050 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002051 vim_free(fc);
2052}
2053
2054/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002055 * Free "fc" and what it contains.
2056 * Can be called only when "fc" is kept beyond the period of it called,
2057 * i.e. after cleanup_function_call(fc).
2058 */
2059 static void
2060free_funccal_contents(funccall_T *fc)
2061{
2062 listitem_T *li;
2063
2064 // Free all l: variables.
2065 vars_clear(&fc->l_vars.dv_hashtab);
2066
2067 // Free all a: variables.
2068 vars_clear(&fc->l_avars.dv_hashtab);
2069
2070 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002071 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002072 clear_tv(&li->li_tv);
2073
2074 free_funccal(fc);
2075}
2076
2077/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002078 * Handle the last part of returning from a function: free the local hashtable.
2079 * Unless it is still in use by a closure.
2080 */
2081 static void
2082cleanup_function_call(funccall_T *fc)
2083{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002084 int may_free_fc = fc->fc_refcount <= 0;
2085 int free_fc = TRUE;
2086
Bram Moolenaar6914c642017-04-01 21:21:30 +02002087 current_funccal = fc->caller;
2088
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002089 // Free all l: variables if not referred.
2090 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
2091 vars_clear(&fc->l_vars.dv_hashtab);
2092 else
2093 free_fc = FALSE;
2094
2095 // If the a:000 list and the l: and a: dicts are not referenced and
2096 // there is no closure using it, we can free the funccall_T and what's
2097 // in it.
2098 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
2099 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002100 else
2101 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002102 int todo;
2103 hashitem_T *hi;
2104 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002105
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002106 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002107
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002108 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002109 todo = (int)fc->l_avars.dv_hashtab.ht_used;
2110 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
2111 {
2112 if (!HASHITEM_EMPTY(hi))
2113 {
2114 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002115 di = HI2DI(hi);
2116 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002117 }
2118 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002119 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002120
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002121 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2122 fc->l_varlist.lv_first = NULL;
2123 else
2124 {
2125 listitem_T *li;
2126
2127 free_fc = FALSE;
2128
2129 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002130 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002131 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002132 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002133
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002134 if (free_fc)
2135 free_funccal(fc);
2136 else
2137 {
2138 static int made_copy = 0;
2139
2140 // "fc" is still in use. This can happen when returning "a:000",
2141 // assigning "l:" to a global variable or defining a closure.
2142 // Link "fc" in the list for garbage collection later.
2143 fc->caller = previous_funccal;
2144 previous_funccal = fc;
2145
2146 if (want_garbage_collect)
2147 // If garbage collector is ready, clear count.
2148 made_copy = 0;
2149 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002150 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002151 // We have made a lot of copies, worth 4 Mbyte. This can happen
2152 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002153 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002154 // too much memory.
2155 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002156 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002157 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002158 }
2159}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002160
2161/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002162 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2163 */
2164 static int
2165numbered_function(char_u *name)
2166{
2167 return isdigit(*name)
2168 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2169}
2170
2171/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002172 * There are two kinds of function names:
2173 * 1. ordinary names, function defined with :function or :def
2174 * 2. numbered functions and lambdas
2175 * For the first we only count the name stored in func_hashtab as a reference,
2176 * using function() does not count as a reference, because the function is
2177 * looked up by name.
2178 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002179 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002180func_name_refcount(char_u *name)
2181{
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002182 return numbered_function(name) || *name == '<';
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002183}
2184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185/*
2186 * Unreference "fc": decrement the reference count and free it when it
2187 * becomes zero. "fp" is detached from "fc".
2188 * When "force" is TRUE we are exiting.
2189 */
2190 static void
2191funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2192{
2193 funccall_T **pfc;
2194 int i;
2195
2196 if (fc == NULL)
2197 return;
2198
2199 if (--fc->fc_refcount <= 0 && (force || (
2200 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
2201 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
2202 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2203 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
2204 {
2205 if (fc == *pfc)
2206 {
2207 *pfc = fc->caller;
2208 free_funccal_contents(fc);
2209 return;
2210 }
2211 }
2212 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2213 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
2214 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
2215}
2216
2217/*
2218 * Remove the function from the function hashtable. If the function was
2219 * deleted while it still has references this was already done.
2220 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2221 */
2222 static int
2223func_remove(ufunc_T *fp)
2224{
2225 hashitem_T *hi;
2226
2227 // Return if it was already virtually deleted.
2228 if (fp->uf_flags & FC_DEAD)
2229 return FALSE;
2230
2231 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
2232 if (!HASHITEM_EMPTY(hi))
2233 {
2234 // When there is a def-function index do not actually remove the
2235 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002236 // Do remove it when it's a copy.
2237 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaarc970e422021-03-17 15:03:04 +01002238 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002240 return FALSE;
2241 }
2242 hash_remove(&func_hashtab, hi);
2243 fp->uf_flags |= FC_DELETED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002244 return TRUE;
2245 }
2246 return FALSE;
2247}
2248
2249 static void
2250func_clear_items(ufunc_T *fp)
2251{
2252 ga_clear_strings(&(fp->uf_args));
2253 ga_clear_strings(&(fp->uf_def_args));
2254 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002256 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002257 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002258 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002259
2260 // Increment the refcount of this function to avoid it being freed
2261 // recursively when the partial is freed.
2262 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002263 partial_unref(fp->uf_partial);
2264 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002265 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002266
2267#ifdef FEAT_LUA
2268 if (fp->uf_cb_free != NULL)
2269 {
2270 fp->uf_cb_free(fp->uf_cb_state);
2271 fp->uf_cb_free = NULL;
2272 }
2273
2274 fp->uf_cb_state = NULL;
2275 fp->uf_cb = NULL;
2276#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277#ifdef FEAT_PROFILE
2278 VIM_CLEAR(fp->uf_tml_count);
2279 VIM_CLEAR(fp->uf_tml_total);
2280 VIM_CLEAR(fp->uf_tml_self);
2281#endif
2282}
2283
2284/*
2285 * Free all things that a function contains. Does not free the function
2286 * itself, use func_free() for that.
2287 * When "force" is TRUE we are exiting.
2288 */
2289 static void
2290func_clear(ufunc_T *fp, int force)
2291{
2292 if (fp->uf_cleared)
2293 return;
2294 fp->uf_cleared = TRUE;
2295
2296 // clear this function
2297 func_clear_items(fp);
2298 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002299 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300}
2301
2302/*
2303 * Free a function and remove it from the list of functions. Does not free
2304 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002305 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002306 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002308 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002309func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310{
2311 // Only remove it when not done already, otherwise we would remove a newer
2312 // version of the function with the same name.
2313 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2314 func_remove(fp);
2315
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002316 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002317 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002318 if (fp->uf_dfunc_idx > 0)
2319 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002320 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002322 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002323 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002324 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325}
2326
2327/*
2328 * Free all things that a function contains and free the function itself.
2329 * When "force" is TRUE we are exiting.
2330 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002331 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332func_clear_free(ufunc_T *fp, int force)
2333{
2334 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002335 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2336 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002337 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002338 else
2339 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340}
2341
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002342/*
2343 * Copy already defined function "lambda" to a new function with name "global".
2344 * This is for when a compiled function defines a global function.
2345 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002346 int
2347copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002348{
2349 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002350 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002351
2352 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002353 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002354 semsg(_(e_lambda_function_not_found_str), lambda);
2355 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002356 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002357
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002358 fp = find_func(global, TRUE, NULL);
2359 if (fp != NULL)
2360 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002361 // TODO: handle ! to overwrite
Bram Moolenaarc553a212021-12-26 20:20:34 +00002362 semsg(_(e_function_str_already_exists_add_excl_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002363 return FAIL;
2364 }
2365
2366 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
2367 if (fp == NULL)
2368 return FAIL;
2369
2370 fp->uf_varargs = ufunc->uf_varargs;
2371 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2372 fp->uf_def_status = ufunc->uf_def_status;
2373 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2374 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2375 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2376 == FAIL
2377 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2378 goto failed;
2379
2380 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
2381 : vim_strsave(ufunc->uf_name_exp);
2382 if (ufunc->uf_arg_types != NULL)
2383 {
2384 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2385 if (fp->uf_arg_types == NULL)
2386 goto failed;
2387 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2388 sizeof(type_T *) * fp->uf_args.ga_len);
2389 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002390 if (ufunc->uf_va_name != NULL)
2391 {
2392 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2393 if (fp->uf_va_name == NULL)
2394 goto failed;
2395 }
2396 fp->uf_ret_type = ufunc->uf_ret_type;
2397
2398 fp->uf_refcount = 1;
2399 STRCPY(fp->uf_name, global);
2400 hash_add(&func_hashtab, UF2HIKEY(fp));
2401
2402 // the referenced dfunc_T is now used one more time
2403 link_def_function(fp);
2404
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002405 // Create a partial to store the context of the function where it was
2406 // instantiated. Only needs to be done once. Do this on the original
2407 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002408 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2409 {
2410 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2411
2412 if (pt == NULL)
2413 goto failed;
2414 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002415 {
2416 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002417 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002418 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002419 ufunc->uf_partial = pt;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002420 --pt->pt_refcount; // not actually referenced here
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002421 }
2422
2423 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002424
2425failed:
2426 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002427 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002428}
2429
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002430static int funcdepth = 0;
2431
2432/*
2433 * Increment the function call depth count.
2434 * Return FAIL when going over 'maxfuncdepth'.
2435 * Otherwise return OK, must call funcdepth_decrement() later!
2436 */
2437 int
2438funcdepth_increment(void)
2439{
2440 if (funcdepth >= p_mfd)
2441 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002442 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002443 return FAIL;
2444 }
2445 ++funcdepth;
2446 return OK;
2447}
2448
2449 void
2450funcdepth_decrement(void)
2451{
2452 --funcdepth;
2453}
2454
2455/*
2456 * Get the current function call depth.
2457 */
2458 int
2459funcdepth_get(void)
2460{
2461 return funcdepth;
2462}
2463
2464/*
2465 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002466 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002467 * times as funcdepth_increment().
2468 */
2469 void
2470funcdepth_restore(int depth)
2471{
2472 funcdepth = depth;
2473}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002474
2475/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002476 * Call a user function.
2477 */
2478 static void
2479call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002480 ufunc_T *fp, // pointer to function
2481 int argcount, // nr of args
2482 typval_T *argvars, // arguments
2483 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002484 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002485 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002486{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002487 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002488 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002489 funccall_T *fc;
2490 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002491 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002492 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002493 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002494 int i;
2495 int ai;
2496 int islambda = FALSE;
2497 char_u numbuf[NUMBUFLEN];
2498 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002499 typval_T *tv_to_free[MAX_FUNC_ARGS];
2500 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002501#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002502 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002503#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002504 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002505
Bram Moolenaarb2049902021-01-24 12:53:53 +01002506#ifdef FEAT_PROFILE
2507 CLEAR_FIELD(profile_info);
2508#endif
2509
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002510 // If depth of calling is getting too high, don't execute the function.
2511 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002512 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002513 rettv->v_type = VAR_NUMBER;
2514 rettv->vval.v_number = -1;
2515 return;
2516 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002517
Bram Moolenaare38eab22019-12-05 21:50:01 +01002518 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002519
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002520 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002521 if (fc == NULL)
2522 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002523 fc->caller = current_funccal;
2524 current_funccal = fc;
2525 fc->func = fp;
2526 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002527 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002528 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002529 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2530 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002531 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002532 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002533 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002534
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002535 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002537#ifdef FEAT_PROFILE
2538 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2539#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002540 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002541#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002542 if (do_profiling == PROF_YES)
2543 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002544#endif
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002545 call_def_function(fp, argcount, argvars, funcexe->fe_partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002546 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002547#ifdef FEAT_PROFILE
2548 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002549 || (caller != NULL && caller->uf_profiling)))
2550 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002551#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 free_funccal(fc);
2554 return;
2555 }
2556
Bram Moolenaar38453522021-11-28 22:00:12 +00002557 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002558
2559 /*
2560 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
2561 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
2562 * each argument variable and saves a lot of time.
2563 */
2564 /*
2565 * Init l: variables.
2566 */
2567 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
2568 if (selfdict != NULL)
2569 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002570 // Set l:self to "selfdict". Use "name" to avoid a warning from
2571 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002572 v = &fc->fixvar[fixvar_idx++].var;
2573 name = v->di_key;
2574 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002575 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002576 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
2577 v->di_tv.v_type = VAR_DICT;
2578 v->di_tv.v_lock = 0;
2579 v->di_tv.vval.v_dict = selfdict;
2580 ++selfdict->dv_refcount;
2581 }
2582
2583 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002584 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002585 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002586 * Set a:000 to a list with room for the "..." arguments.
2587 */
2588 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002589 if ((fp->uf_flags & FC_NOARGS) == 0)
2590 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002591 (varnumber_T)(argcount >= fp->uf_args.ga_len
2592 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01002593 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002594 if ((fp->uf_flags & FC_NOARGS) == 0)
2595 {
2596 // Use "name" to avoid a warning from some compiler that checks the
2597 // destination size.
2598 v = &fc->fixvar[fixvar_idx++].var;
2599 name = v->di_key;
2600 STRCPY(name, "000");
2601 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2602 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
2603 v->di_tv.v_type = VAR_LIST;
2604 v->di_tv.v_lock = VAR_FIXED;
2605 v->di_tv.vval.v_list = &fc->l_varlist;
2606 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02002607 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002608 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2609 fc->l_varlist.lv_lock = VAR_FIXED;
2610
2611 /*
2612 * Set a:firstline to "firstline" and a:lastline to "lastline".
2613 * Set a:name to named arguments.
2614 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002615 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002616 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002617 if ((fp->uf_flags & FC_NOARGS) == 0)
2618 {
2619 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002620 (varnumber_T)funcexe->fe_firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002621 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002622 (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002623 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002624 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002625 {
2626 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002627 typval_T def_rettv;
2628 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002629
2630 ai = i - fp->uf_args.ga_len;
2631 if (ai < 0)
2632 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002633 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002634 name = FUNCARG(fp, i);
2635 if (islambda)
2636 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002637
2638 // evaluate named argument default expression
2639 isdefault = ai + fp->uf_def_args.ga_len >= 0
2640 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2641 && argvars[i].vval.v_number == VVAL_NONE));
2642 if (isdefault)
2643 {
2644 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002645
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002646 def_rettv.v_type = VAR_NUMBER;
2647 def_rettv.vval.v_number = -1;
2648
2649 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2650 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002651 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002652 {
2653 default_arg_err = 1;
2654 break;
2655 }
2656 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002657 }
2658 else
2659 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002660 if ((fp->uf_flags & FC_NOARGS) != 0)
2661 // Bail out if no a: arguments used (in lambda).
2662 break;
2663
Bram Moolenaare38eab22019-12-05 21:50:01 +01002664 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002665 sprintf((char *)numbuf, "%d", ai + 1);
2666 name = numbuf;
2667 }
2668 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2669 {
2670 v = &fc->fixvar[fixvar_idx++].var;
2671 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002672 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002673 }
2674 else
2675 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002676 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002677 if (v == NULL)
2678 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002679 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002680 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002681
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002682 // Note: the values are copied directly to avoid alloc/free.
2683 // "argvars" must have VAR_FIXED for v_lock.
2684 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002685 v->di_tv.v_lock = VAR_FIXED;
2686
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002687 if (isdefault)
2688 // Need to free this later, no matter where it's stored.
2689 tv_to_free[tv_to_free_len++] = &v->di_tv;
2690
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002691 if (addlocal)
2692 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002693 // Named arguments should be accessed without the "a:" prefix in
2694 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002695 copy_tv(&v->di_tv, &v->di_tv);
2696 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002697 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002698 else
2699 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002700
2701 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2702 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002703 listitem_T *li = &fc->l_listitems[ai];
2704
2705 li->li_tv = argvars[i];
2706 li->li_tv.v_lock = VAR_FIXED;
2707 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002708 }
2709 }
2710
Bram Moolenaare38eab22019-12-05 21:50:01 +01002711 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002712 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002713
2714 if (fp->uf_flags & FC_SANDBOX)
2715 {
2716 using_sandbox = TRUE;
2717 ++sandbox;
2718 }
2719
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002720 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002721 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002722 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002723 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002724 ++no_wait_return;
2725 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002726
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002727 smsg(_("calling %s"), SOURCING_NAME);
2728 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002729 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002730 char_u buf[MSG_BUF_LEN];
2731 char_u numbuf2[NUMBUFLEN];
2732 char_u *tofree;
2733 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002734
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002735 msg_puts("(");
2736 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002737 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002738 if (i > 0)
2739 msg_puts(", ");
2740 if (argvars[i].v_type == VAR_NUMBER)
2741 msg_outnum((long)argvars[i].vval.v_number);
2742 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002743 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002744 // Do not want errors such as E724 here.
2745 ++emsg_off;
2746 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2747 --emsg_off;
2748 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002749 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002750 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002751 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002752 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2753 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002754 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002755 msg_puts((char *)s);
2756 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002757 }
2758 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002759 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002760 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002761 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002762 msg_puts("\n"); // don't overwrite this either
2763
2764 verbose_leave_scroll();
2765 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002766 }
2767#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002768 if (do_profiling == PROF_YES)
2769 profile_may_start_func(&profile_info, fp,
2770 fc->caller == NULL ? NULL : fc->caller->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002771#endif
2772
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002773 save_current_sctx = current_sctx;
2774 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002775 save_did_emsg = did_emsg;
2776 did_emsg = FALSE;
2777
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002778 if (default_arg_err && (fp->uf_flags & FC_ABORT))
2779 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002780 else if (islambda)
2781 {
2782 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
2783
2784 // A Lambda always has the command "return {expr}". It is much faster
2785 // to evaluate {expr} directly.
2786 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002787 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002788 --ex_nesting_level;
2789 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002790 else
2791 // call do_cmdline() to execute the lines
2792 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002793 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
2794
2795 --RedrawingDisabled;
2796
Bram Moolenaare38eab22019-12-05 21:50:01 +01002797 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002798 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
2799 {
2800 clear_tv(rettv);
2801 rettv->v_type = VAR_NUMBER;
2802 rettv->vval.v_number = -1;
2803 }
2804
2805#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002806 if (do_profiling == PROF_YES)
2807 {
2808 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2809
2810 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
2811 profile_may_end_func(&profile_info, fp, caller);
2812 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002813#endif
2814
Bram Moolenaare38eab22019-12-05 21:50:01 +01002815 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002816 if (p_verbose >= 12)
2817 {
2818 ++no_wait_return;
2819 verbose_enter_scroll();
2820
2821 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002822 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002823 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002824 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002825 (long)fc->rettv->vval.v_number);
2826 else
2827 {
2828 char_u buf[MSG_BUF_LEN];
2829 char_u numbuf2[NUMBUFLEN];
2830 char_u *tofree;
2831 char_u *s;
2832
Bram Moolenaare38eab22019-12-05 21:50:01 +01002833 // The value may be very long. Skip the middle part, so that we
2834 // have some idea how it starts and ends. smsg() would always
2835 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002836 ++emsg_off;
2837 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
2838 --emsg_off;
2839 if (s != NULL)
2840 {
2841 if (vim_strsize(s) > MSG_BUF_CLEN)
2842 {
2843 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2844 s = buf;
2845 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002846 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002847 vim_free(tofree);
2848 }
2849 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002850 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002851
2852 verbose_leave_scroll();
2853 --no_wait_return;
2854 }
2855
Bram Moolenaare31ee862020-01-07 20:59:34 +01002856 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002857 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002858 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002859#ifdef FEAT_PROFILE
2860 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002861 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002862#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02002863 if (using_sandbox)
2864 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002865
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002866 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002867 {
2868 ++no_wait_return;
2869 verbose_enter_scroll();
2870
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002871 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002872 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002873
2874 verbose_leave_scroll();
2875 --no_wait_return;
2876 }
2877
2878 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002879 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002880 for (i = 0; i < tv_to_free_len; ++i)
2881 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002882 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002883}
2884
2885/*
Bram Moolenaar50824712020-12-20 21:10:17 +01002886 * Check the argument count for user function "fp".
2887 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
2888 */
2889 int
2890check_user_func_argcount(ufunc_T *fp, int argcount)
2891{
2892 int regular_args = fp->uf_args.ga_len;
2893
2894 if (argcount < regular_args - fp->uf_def_args.ga_len)
2895 return FCERR_TOOFEW;
2896 else if (!has_varargs(fp) && argcount > regular_args)
2897 return FCERR_TOOMANY;
2898 return FCERR_UNKNOWN;
2899}
2900
2901/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002903 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 int
2905call_user_func_check(
2906 ufunc_T *fp,
2907 int argcount,
2908 typval_T *argvars,
2909 typval_T *rettv,
2910 funcexe_T *funcexe,
2911 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002912{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002914
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002915 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
2916 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01002917 error = check_user_func_argcount(fp, argcount);
2918 if (error != FCERR_UNKNOWN)
2919 return error;
2920 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 error = FCERR_DICT;
2922 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002923 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 int did_save_redo = FALSE;
2925 save_redo_T save_redo;
2926
2927 /*
2928 * Call the user function.
2929 * Save and restore search patterns, script variables and
2930 * redo buffer.
2931 */
2932 save_search_patterns();
2933 if (!ins_compl_active())
2934 {
2935 saveRedobuff(&save_redo);
2936 did_save_redo = TRUE;
2937 }
2938 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002939 call_user_func(fp, argcount, argvars, rettv, funcexe,
2940 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
2942 // Function was unreferenced while being used, free it now.
2943 func_clear_free(fp, FALSE);
2944 if (did_save_redo)
2945 restoreRedobuff(&save_redo);
2946 restore_search_patterns();
2947 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002950}
2951
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002952static funccal_entry_T *funccal_stack = NULL;
2953
2954/*
2955 * Save the current function call pointer, and set it to NULL.
2956 * Used when executing autocommands and for ":source".
2957 */
2958 void
2959save_funccal(funccal_entry_T *entry)
2960{
2961 entry->top_funccal = current_funccal;
2962 entry->next = funccal_stack;
2963 funccal_stack = entry;
2964 current_funccal = NULL;
2965}
2966
2967 void
2968restore_funccal(void)
2969{
2970 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002971 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002972 else
2973 {
2974 current_funccal = funccal_stack->top_funccal;
2975 funccal_stack = funccal_stack->next;
2976 }
2977}
2978
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02002979 funccall_T *
2980get_current_funccal(void)
2981{
2982 return current_funccal;
2983}
2984
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002985/*
2986 * Mark all functions of script "sid" as deleted.
2987 */
2988 void
2989delete_script_functions(int sid)
2990{
2991 hashitem_T *hi;
2992 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02002993 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002994 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002995 size_t len;
2996
2997 buf[0] = K_SPECIAL;
2998 buf[1] = KS_EXTRA;
2999 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003000 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003001 len = STRLEN(buf);
3002
Bram Moolenaarce658352020-07-31 23:47:12 +02003003 while (todo > 0)
3004 {
3005 todo = func_hashtab.ht_used;
3006 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3007 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003008 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003009 fp = HI2UF(hi);
3010 if (STRNCMP(fp->uf_name, buf, len) == 0)
3011 {
3012 int changed = func_hashtab.ht_changed;
3013
3014 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003015
3016 if (fp->uf_calls > 0)
3017 {
3018 // Function is executing, don't free it but do remove
3019 // it from the hashtable.
3020 if (func_remove(fp))
3021 fp->uf_refcount--;
3022 }
3023 else
3024 {
3025 func_clear(fp, TRUE);
3026 // When clearing a function another function can be
3027 // cleared as a side effect. When that happens start
3028 // over.
3029 if (changed != func_hashtab.ht_changed)
3030 break;
3031 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003032 }
3033 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003034 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003035 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003036}
3037
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003038#if defined(EXITFREE) || defined(PROTO)
3039 void
3040free_all_functions(void)
3041{
3042 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003043 ufunc_T *fp;
3044 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003045 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003046 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003047
Bram Moolenaare38eab22019-12-05 21:50:01 +01003048 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003049 while (current_funccal != NULL)
3050 {
3051 clear_tv(current_funccal->rettv);
3052 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003053 if (current_funccal == NULL && funccal_stack != NULL)
3054 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003055 }
3056
Bram Moolenaare38eab22019-12-05 21:50:01 +01003057 // First clear what the functions contain. Since this may lower the
3058 // reference count of a function, it may also free a function and change
3059 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003060 while (todo > 0)
3061 {
3062 todo = func_hashtab.ht_used;
3063 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3064 if (!HASHITEM_EMPTY(hi))
3065 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 // clear the def function index now
3067 fp = HI2UF(hi);
3068 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003069 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070
Bram Moolenaare38eab22019-12-05 21:50:01 +01003071 // Only free functions that are not refcounted, those are
3072 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003073 if (func_name_refcount(fp->uf_name))
3074 ++skipped;
3075 else
3076 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003077 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003078 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003079 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003080 {
3081 skipped = 0;
3082 break;
3083 }
3084 }
3085 --todo;
3086 }
3087 }
3088
Bram Moolenaare38eab22019-12-05 21:50:01 +01003089 // Now actually free the functions. Need to start all over every time,
3090 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003091 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003092 while (func_hashtab.ht_used > skipped)
3093 {
3094 todo = func_hashtab.ht_used;
3095 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003096 if (!HASHITEM_EMPTY(hi))
3097 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003098 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003099 // Only free functions that are not refcounted, those are
3100 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003101 fp = HI2UF(hi);
3102 if (func_name_refcount(fp->uf_name))
3103 ++skipped;
3104 else
3105 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003106 if (func_free(fp, FALSE) == OK)
3107 {
3108 skipped = 0;
3109 break;
3110 }
3111 // did not actually free it
3112 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003113 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003114 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003115 }
3116 if (skipped == 0)
3117 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118
3119 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003120}
3121#endif
3122
3123/*
3124 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02003125 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003126 * "len" is the length of "name", or -1 for NUL terminated.
3127 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003129builtin_function(char_u *name, int len)
3130{
3131 char_u *p;
3132
Bram Moolenaara26b9702020-04-18 19:53:28 +02003133 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003134 return FALSE;
3135 p = vim_strchr(name, AUTOLOAD_CHAR);
3136 return p == NULL || (len > 0 && p > name + len);
3137}
3138
3139 int
3140func_call(
3141 char_u *name,
3142 typval_T *args,
3143 partial_T *partial,
3144 dict_T *selfdict,
3145 typval_T *rettv)
3146{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003147 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003148 listitem_T *item;
3149 typval_T argv[MAX_FUNC_ARGS + 1];
3150 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003151 int r = 0;
3152
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003153 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003154 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003155 {
3156 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3157 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003158 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003159 break;
3160 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003161 // Make a copy of each argument. This is needed to be able to set
3162 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003163 copy_tv(&item->li_tv, &argv[argc++]);
3164 }
3165
3166 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003167 {
3168 funcexe_T funcexe;
3169
Bram Moolenaara80faa82020-04-12 19:37:17 +02003170 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003171 funcexe.fe_firstline = curwin->w_cursor.lnum;
3172 funcexe.fe_lastline = curwin->w_cursor.lnum;
3173 funcexe.fe_evaluate = TRUE;
3174 funcexe.fe_partial = partial;
3175 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003176 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3177 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003178
Bram Moolenaare38eab22019-12-05 21:50:01 +01003179 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003180 while (argc > 0)
3181 clear_tv(&argv[--argc]);
3182
3183 return r;
3184}
3185
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003186static int callback_depth = 0;
3187
3188 int
3189get_callback_depth(void)
3190{
3191 return callback_depth;
3192}
3193
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003194/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003195 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003196 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003197 */
3198 int
3199call_callback(
3200 callback_T *callback,
3201 int len, // length of "name" or -1 to use strlen()
3202 typval_T *rettv, // return value goes here
3203 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003204 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003205 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003206{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003207 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003208 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003209
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003210 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3211 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003212 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003213 funcexe.fe_evaluate = TRUE;
3214 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003215 ++callback_depth;
3216 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3217 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003218
3219 // When a :def function was called that uses :try an error would be turned
3220 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003221 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003222 {
3223 need_rethrow = FALSE;
3224 handle_did_throw();
3225 }
3226
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003227 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003228}
3229
3230/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003231 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003232 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003233 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3234 */
3235 varnumber_T
3236call_callback_retnr(
3237 callback_T *callback,
3238 int argcount, // number of "argvars"
3239 typval_T *argvars) // vars for arguments, must have "argcount"
3240 // PLUS ONE elements!
3241{
3242 typval_T rettv;
3243 varnumber_T retval;
3244
3245 if (call_callback(callback, 0, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003246 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003247
3248 retval = tv_get_number_chk(&rettv, NULL);
3249 clear_tv(&rettv);
3250 return retval;
3251}
3252
3253/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 * Give an error message for the result of a function.
3255 * Nothing if "error" is FCERR_NONE.
3256 */
3257 void
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003258user_func_error(int error, char_u *name, funcexe_T *funcexe)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259{
3260 switch (error)
3261 {
3262 case FCERR_UNKNOWN:
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003263 if (funcexe->fe_found_var)
3264 semsg(_(e_not_callable_type_str), name);
3265 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003266 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 break;
3268 case FCERR_NOTMETHOD:
3269 emsg_funcname(
3270 N_("E276: Cannot use function as a method: %s"), name);
3271 break;
3272 case FCERR_DELETED:
Bram Moolenaarc553a212021-12-26 20:20:34 +00003273 emsg_funcname(e_func_deleted, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 break;
3275 case FCERR_TOOMANY:
Bram Moolenaare1242042021-12-16 20:56:57 +00003276 emsg_funcname((char *)e_too_many_arguments_for_function_str,
3277 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 break;
3279 case FCERR_TOOFEW:
Bram Moolenaare1242042021-12-16 20:56:57 +00003280 emsg_funcname((char *)e_not_enough_arguments_for_function_str,
3281 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 break;
3283 case FCERR_SCRIPT:
3284 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00003285 e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 break;
3287 case FCERR_DICT:
3288 emsg_funcname(
3289 N_("E725: Calling dict function without Dictionary: %s"),
3290 name);
3291 break;
3292 }
3293}
3294
3295/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003296 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003297 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003298 * Return FAIL when the function can't be called, OK otherwise.
3299 * Also returns OK when an error was encountered while executing the function.
3300 */
3301 int
3302call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003303 char_u *funcname, // name of the function
3304 int len, // length of "name" or -1 to use strlen()
3305 typval_T *rettv, // return value goes here
3306 int argcount_in, // number of "argvars"
3307 typval_T *argvars_in, // vars for arguments, must have "argcount"
3308 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003309 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003310{
3311 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01003312 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003313 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003314 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003315 char_u fname_buf[FLEN_FIXED + 1];
3316 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003317 char_u *fname = NULL;
3318 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003319 int argcount = argcount_in;
3320 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003321 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003322 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003323 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003324 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003325 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003326 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003327 type_T check_type;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003328
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003329 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3330 // even when call_func() returns FAIL.
3331 rettv->v_type = VAR_UNKNOWN;
3332
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003333 if (partial != NULL)
3334 fp = partial->pt_func;
3335 if (fp == NULL)
3336 {
3337 // Make a copy of the name, if it comes from a funcref variable it
3338 // could be changed or deleted in the called function.
3339 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3340 if (name == NULL)
3341 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003342
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003343 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3344 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003345
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003346 if (funcexe->fe_doesrange != NULL)
3347 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003348
3349 if (partial != NULL)
3350 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003351 // When the function has a partial with a dict and there is a dict
3352 // argument, use the dict argument. That is backwards compatible.
3353 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003354 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003355 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003356 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003357 {
3358 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003359 {
3360 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3361 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003362 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003363 goto theend;
3364 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003365 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003366 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003367 for (i = 0; i < argcount_in; ++i)
3368 argv[i + argv_clear] = argvars_in[i];
3369 argvars = argv;
3370 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003371
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003372 if (funcexe->fe_check_type != NULL
3373 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003374 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003375 // Now funcexe->fe_check_type is missing the added arguments,
3376 // make a copy of the type with the correction.
3377 check_type = *funcexe->fe_check_type;
3378 funcexe->fe_check_type = &check_type;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003379 check_type.tt_argcount += partial->pt_argc;
3380 check_type.tt_min_argcount += partial->pt_argc;
3381 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003382 }
3383 }
3384
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003385 if (error == FCERR_NONE && funcexe->fe_check_type != NULL
3386 && funcexe->fe_evaluate)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003387 {
3388 // Check that the argument types are OK for the types of the funcref.
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003389 if (check_argument_types(funcexe->fe_check_type, argvars, argcount,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003390 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003391 error = FCERR_OTHER;
3392 }
3393
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003394 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003395 {
3396 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003397 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003398
Bram Moolenaar333894b2020-08-01 18:53:07 +02003399 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003400 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003401 {
3402 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003403 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003404 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003405
Bram Moolenaare38eab22019-12-05 21:50:01 +01003406 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003407 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003408 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003409
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003410 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003411 {
3412 /*
3413 * User defined function.
3414 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003415 if (fp == NULL)
Bram Moolenaar333894b2020-08-01 18:53:07 +02003416 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003417
Bram Moolenaare38eab22019-12-05 21:50:01 +01003418 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003419 if (fp == NULL
3420 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003421 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003422 && !aborting())
3423 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003424 // executed an autocommand, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02003425 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003426 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003427 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003428 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3429 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003430 // loaded a package, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02003431 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003432 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003433 if (fp == NULL)
3434 {
3435 char_u *p = untrans_function_name(rfname);
3436
3437 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003438 // Don't do this if the name starts with "s:".
3439 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaar333894b2020-08-01 18:53:07 +02003440 fp = find_func(p, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003441 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003442
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003443 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003444 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02003445#ifdef FEAT_LUA
3446 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
3447 {
3448 cfunc_T cb = fp->uf_cb;
3449
3450 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3451 }
3452#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003453 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003454 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003455 if (funcexe->fe_argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003456 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003457 argcount = funcexe->fe_argv_func(argcount, argvars,
3458 argv_clear, fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003459
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003460 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003461 {
3462 // Method call: base->Method()
3463 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003464 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003465 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003466 argvars = argv;
3467 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003468 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 error = call_user_func_check(fp, argcount, argvars, rettv,
3471 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003472 }
3473 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003474 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003475 {
3476 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003477 * expr->method(): Find the method name in the table, call its
3478 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003479 */
3480 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003481 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003482 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003483 else
3484 {
3485 /*
3486 * Find the function name in the table, call its implementation.
3487 */
3488 error = call_internal_func(fname, argcount, argvars, rettv);
3489 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003490
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003491 /*
3492 * The function call (or "FuncUndefined" autocommand sequence) might
3493 * have been aborted by an error, an interrupt, or an explicitly thrown
3494 * exception that has not been caught so far. This situation can be
3495 * tested for by calling aborting(). For an error in an internal
3496 * function or for the "E132" error in call_user_func(), however, the
3497 * throw point at which the "force_abort" flag (temporarily reset by
3498 * emsg()) is normally updated has not been reached yet. We need to
3499 * update that flag first to make aborting() reliable.
3500 */
3501 update_force_abort();
3502 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003503 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003504 ret = OK;
3505
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003506theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003507 /*
3508 * Report an error unless the argument evaluation or function call has been
3509 * cancelled due to an aborting error, an interrupt, or an exception.
3510 */
3511 if (!aborting())
3512 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003513 user_func_error(error, (name != NULL) ? name : funcname, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003514 }
3515
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003516 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003517 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003518 clear_tv(&argv[--argv_clear + argv_base]);
3519
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003520 vim_free(tofree);
3521 vim_free(name);
3522
3523 return ret;
3524}
3525
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003526 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527printable_func_name(ufunc_T *fp)
3528{
3529 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3530}
3531
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003532/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003533 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003534 */
3535 static void
3536list_func_head(ufunc_T *fp, int indent)
3537{
3538 int j;
3539
3540 msg_start();
3541 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003542 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003543 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003544 msg_puts("def ");
3545 else
3546 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003548 msg_putchar('(');
3549 for (j = 0; j < fp->uf_args.ga_len; ++j)
3550 {
3551 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003552 msg_puts(", ");
3553 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 if (fp->uf_arg_types != NULL)
3555 {
3556 char *tofree;
3557
3558 msg_puts(": ");
3559 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3560 vim_free(tofree);
3561 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003562 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3563 {
3564 msg_puts(" = ");
3565 msg_puts(((char **)(fp->uf_def_args.ga_data))
3566 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3567 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003568 }
3569 if (fp->uf_varargs)
3570 {
3571 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003572 msg_puts(", ");
3573 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003574 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 if (fp->uf_va_name != NULL)
3576 {
3577 if (j)
3578 msg_puts(", ");
3579 msg_puts("...");
3580 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003581 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 {
3583 char *tofree;
3584
3585 msg_puts(": ");
3586 msg_puts(type_name(fp->uf_va_type, &tofree));
3587 vim_free(tofree);
3588 }
3589 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003590 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003591
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003592 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003593 {
3594 if (fp->uf_ret_type != &t_void)
3595 {
3596 char *tofree;
3597
3598 msg_puts(": ");
3599 msg_puts(type_name(fp->uf_ret_type, &tofree));
3600 vim_free(tofree);
3601 }
3602 }
3603 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003604 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003605 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003606 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003607 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003608 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003609 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003610 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003611 msg_clr_eos();
3612 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003613 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003614}
3615
3616/*
3617 * Get a function name, translating "<SID>" and "<SNR>".
3618 * Also handles a Funcref in a List or Dictionary.
3619 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003620 * Set "*is_global" to TRUE when the function must be global, unless
3621 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003622 * flags:
3623 * TFN_INT: internal function name OK
3624 * TFN_QUIET: be quiet
3625 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003626 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003627 * Advances "pp" to just after the function name (if no error).
3628 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003629 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003630trans_function_name(
3631 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003632 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003633 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003634 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003635 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003636 partial_T **partial, // return: partial of a FuncRef
3637 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003638{
3639 char_u *name = NULL;
3640 char_u *start;
3641 char_u *end;
3642 int lead;
3643 char_u sid_buf[20];
3644 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003646 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003648
3649 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003650 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003651 start = *pp;
3652
Bram Moolenaare38eab22019-12-05 21:50:01 +01003653 // Check for hard coded <SNR>: already translated function ID (from a user
3654 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003655 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
3656 && (*pp)[2] == (int)KE_SNR)
3657 {
3658 *pp += 3;
3659 len = get_id_len(pp) + 3;
3660 return vim_strnsave(start, len);
3661 }
3662
Bram Moolenaare38eab22019-12-05 21:50:01 +01003663 // A name starting with "<SID>" or "<SNR>" is local to a script. But
3664 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003665 lead = eval_fname_script(start);
3666 if (lead > 2)
3667 start += lead;
3668
Bram Moolenaare38eab22019-12-05 21:50:01 +01003669 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01003670 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003671 lead > 2 ? 0 : FNE_CHECK_START);
3672 if (end == start)
3673 {
3674 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003675 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003676 goto theend;
3677 }
3678 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
3679 {
3680 /*
3681 * Report an invalid expression in braces, unless the expression
3682 * evaluation has been cancelled due to an aborting error, an
3683 * interrupt, or an exception.
3684 */
3685 if (!aborting())
3686 {
3687 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003688 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003689 }
3690 else
3691 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
3692 goto theend;
3693 }
3694
3695 if (lv.ll_tv != NULL)
3696 {
3697 if (fdp != NULL)
3698 {
3699 fdp->fd_dict = lv.ll_dict;
3700 fdp->fd_newkey = lv.ll_newkey;
3701 lv.ll_newkey = NULL;
3702 fdp->fd_di = lv.ll_di;
3703 }
3704 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
3705 {
3706 name = vim_strsave(lv.ll_tv->vval.v_string);
3707 *pp = end;
3708 }
3709 else if (lv.ll_tv->v_type == VAR_PARTIAL
3710 && lv.ll_tv->vval.v_partial != NULL)
3711 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003712 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003713 *pp = end;
3714 if (partial != NULL)
3715 *partial = lv.ll_tv->vval.v_partial;
3716 }
3717 else
3718 {
3719 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
3720 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003721 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003722 else
3723 *pp = end;
3724 name = NULL;
3725 }
3726 goto theend;
3727 }
3728
3729 if (lv.ll_name == NULL)
3730 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003731 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003732 *pp = end;
3733 goto theend;
3734 }
3735
Bram Moolenaare38eab22019-12-05 21:50:01 +01003736 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003737 if (lv.ll_exp_name != NULL)
3738 {
3739 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003740 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003741 flags & TFN_NO_AUTOLOAD, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003742 if (name == lv.ll_exp_name)
3743 name = NULL;
3744 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003745 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003746 {
3747 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003748 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003749 flags & TFN_NO_AUTOLOAD, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003750 if (name == *pp)
3751 name = NULL;
3752 }
3753 if (name != NULL)
3754 {
3755 name = vim_strsave(name);
3756 *pp = end;
3757 if (STRNCMP(name, "<SNR>", 5) == 0)
3758 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003759 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003760 name[0] = K_SPECIAL;
3761 name[1] = KS_EXTRA;
3762 name[2] = (int)KE_SNR;
3763 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
3764 }
3765 goto theend;
3766 }
3767
3768 if (lv.ll_exp_name != NULL)
3769 {
3770 len = (int)STRLEN(lv.ll_exp_name);
3771 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
3772 && STRNCMP(lv.ll_name, "s:", 2) == 0)
3773 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003774 // When there was "s:" already or the name expanded to get a
3775 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003776 lv.ll_name += 2;
3777 len -= 2;
3778 lead = 2;
3779 }
3780 }
3781 else
3782 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003783 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003784 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003785 {
3786 if (is_global != NULL && lv.ll_name[0] == 'g')
3787 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003788 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003789 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003790 len = (int)(end - lv.ll_name);
3791 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003792 if (len <= 0)
3793 {
3794 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003795 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003796 goto theend;
3797 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003798
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003799 // In Vim9 script a user function is script-local by default, unless it
3800 // starts with a lower case character: dict.func().
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003801 vim9script = ASCII_ISUPPER(*start) && in_vim9script();
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003802 if (vim9script)
3803 {
3804 char_u *p;
3805
3806 // SomeScript#func() is a global function.
3807 for (p = start; *p != NUL && *p != '('; ++p)
3808 if (*p == AUTOLOAD_CHAR)
3809 vim9script = FALSE;
3810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003812 /*
3813 * Copy the function name to allocated memory.
3814 * Accept <SID>name() inside a script, translate into <SNR>123_name().
3815 * Accept <SNR>123_name() outside a script.
3816 */
3817 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003818 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003820 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 if (!vim9script)
3822 lead = 3;
3823 if (vim9script || (lv.ll_exp_name != NULL
3824 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003825 || eval_fname_sid(*pp))
3826 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003828 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003829 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00003830 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003831 goto theend;
3832 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003833 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 if (vim9script)
3835 extra = 3 + (int)STRLEN(sid_buf);
3836 else
3837 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003838 }
3839 }
Bram Moolenaar22f17a22021-06-21 20:48:58 +02003840 else if (!(flags & TFN_INT) && (builtin_function(lv.ll_name, len)
3841 || (in_vim9script() && *lv.ll_name == '_')))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003842 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00003843 semsg(_(e_function_name_must_start_with_capital_or_s_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003844 goto theend;
3845 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003846 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003847 {
3848 char_u *cp = vim_strchr(lv.ll_name, ':');
3849
3850 if (cp != NULL && cp < end)
3851 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003852 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003853 goto theend;
3854 }
3855 }
3856
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003858 if (name != NULL)
3859 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01003860 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003861 {
3862 name[0] = K_SPECIAL;
3863 name[1] = KS_EXTRA;
3864 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003866 STRCPY(name + 3, sid_buf);
3867 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
3869 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003870 }
3871 *pp = end;
3872
3873theend:
3874 clear_lval(&lv);
3875 return name;
3876}
3877
3878/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02003879 * Assuming "name" is the result of trans_function_name() and it was prefixed
3880 * to use the script-local name, return the unmodified name (points into
3881 * "name"). Otherwise return NULL.
3882 * This can be used to first search for a script-local function and fall back
3883 * to the global function if not found.
3884 */
3885 char_u *
3886untrans_function_name(char_u *name)
3887{
3888 char_u *p;
3889
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003890 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02003891 {
3892 p = vim_strchr(name, '_');
3893 if (p != NULL)
3894 return p + 1;
3895 }
3896 return NULL;
3897}
3898
3899/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00003900 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
3901 * current script ID and returns the expanded function name. The caller should
3902 * free the returned name. If not called from a script context or the function
3903 * name doesn't start with these prefixes, then returns NULL.
3904 * This doesn't check whether the script-local function exists or not.
3905 */
3906 char_u *
3907get_scriptlocal_funcname(char_u *funcname)
3908{
3909 char sid_buf[25];
3910 int off;
3911 char_u *newname;
3912
3913 if (funcname == NULL)
3914 return NULL;
3915
3916 if (STRNCMP(funcname, "s:", 2) != 0
3917 && STRNCMP(funcname, "<SID>", 5) != 0)
3918 // The function name is not a script-local function name
3919 return NULL;
3920
3921 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3922 {
3923 emsg(_(e_using_sid_not_in_script_context));
3924 return NULL;
3925 }
3926 // Expand s: prefix into <SNR>nr_<name>
3927 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
3928 (long)current_sctx.sc_sid);
3929 off = *funcname == 's' ? 2 : 5;
3930 newname = alloc(STRLEN(sid_buf) + STRLEN(funcname + off) + 1);
3931 if (newname == NULL)
3932 return NULL;
3933 STRCPY(newname, sid_buf);
3934 STRCAT(newname, funcname + off);
3935
3936 return newname;
3937}
3938
3939/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00003940 * Call trans_function_name(), except that a lambda is returned as-is.
3941 * Returns the name in allocated memory.
3942 */
3943 char_u *
3944save_function_name(
3945 char_u **name,
3946 int *is_global,
3947 int skip,
3948 int flags,
3949 funcdict_T *fudi)
3950{
3951 char_u *p = *name;
3952 char_u *saved;
3953
3954 if (STRNCMP(p, "<lambda>", 8) == 0)
3955 {
3956 p += 8;
3957 (void)getdigits(&p);
3958 saved = vim_strnsave(*name, p - *name);
3959 if (fudi != NULL)
3960 CLEAR_POINTER(fudi);
3961 }
3962 else
3963 saved = trans_function_name(&p, is_global, skip,
3964 flags, fudi, NULL, NULL);
3965 *name = p;
3966 return saved;
3967}
3968
3969/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003970 * List functions. When "regmatch" is NULL all of then.
3971 * Otherwise functions matching "regmatch".
3972 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003973 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003974list_functions(regmatch_T *regmatch)
3975{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003976 int changed = func_hashtab.ht_changed;
3977 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003978 hashitem_T *hi;
3979
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003980 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003981 {
3982 if (!HASHITEM_EMPTY(hi))
3983 {
3984 ufunc_T *fp = HI2UF(hi);
3985
3986 --todo;
3987 if ((fp->uf_flags & FC_DEAD) == 0
3988 && (regmatch == NULL
3989 ? !message_filtered(fp->uf_name)
3990 && !func_name_refcount(fp->uf_name)
3991 : !isdigit(*fp->uf_name)
3992 && vim_regexec(regmatch, fp->uf_name, 0)))
3993 {
3994 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003995 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003996 {
3997 emsg(_("E454: function list was modified"));
3998 return;
3999 }
4000 }
4001 }
4002 }
4003}
4004
4005/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004006 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004007 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4008 * the function name.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004009 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004010 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004011 ufunc_T *
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004012define_function(exarg_T *eap, char_u *name_arg, char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004013{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004014 int j;
4015 int c;
4016 int saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004017 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004018 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004019 char_u *p;
4020 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004021 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004022 char_u *line_arg = NULL;
4023 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004025 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004026 garray_T newlines;
4027 int varargs = FALSE;
4028 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004030 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004031 int fp_allocated = FALSE;
4032 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004033 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004034 dictitem_T *v;
4035 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004036 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004037 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004038 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004039 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004040 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004041 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004042
4043 /*
4044 * ":function" without argument: list functions.
4045 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004046 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004047 {
4048 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004049 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004050 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004051 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004052 }
4053
4054 /*
4055 * ":function /pat": list functions matching pattern.
4056 */
4057 if (*eap->arg == '/')
4058 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004059 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004060 if (!eap->skip)
4061 {
4062 regmatch_T regmatch;
4063
4064 c = *p;
4065 *p = NUL;
4066 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4067 *p = c;
4068 if (regmatch.regprog != NULL)
4069 {
4070 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004071 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004072 vim_regfree(regmatch.regprog);
4073 }
4074 }
4075 if (*p == '/')
4076 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004077 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004078 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004079 }
4080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 ga_init(&newargs);
4082 ga_init(&argtypes);
4083 ga_init(&default_args);
4084
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004085 /*
4086 * Get the function name. There are these situations:
4087 * func normal function name
4088 * "name" == func, "fudi.fd_dict" == NULL
4089 * dict.func new dictionary entry
4090 * "name" == NULL, "fudi.fd_dict" set,
4091 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4092 * dict.func existing dict entry with a Funcref
4093 * "name" == func, "fudi.fd_dict" set,
4094 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4095 * dict.func existing dict entry that's not a Funcref
4096 * "name" == NULL, "fudi.fd_dict" set,
4097 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4098 * s:func script-local function name
4099 * g:func global function name, same as "func"
4100 */
4101 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004102 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004103 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004104 // nested function, argument is (args).
4105 paren = TRUE;
4106 CLEAR_FIELD(fudi);
4107 }
4108 else
4109 {
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004110 name = save_function_name(&p, &is_global, eap->skip,
4111 TFN_NO_AUTOLOAD, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004112 paren = (vim_strchr(p, '(') != NULL);
4113 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004114 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004115 /*
4116 * Return on an invalid expression in braces, unless the expression
4117 * evaluation has been cancelled due to an aborting error, an
4118 * interrupt, or an exception.
4119 */
4120 if (!aborting())
4121 {
4122 if (!eap->skip && fudi.fd_newkey != NULL)
4123 semsg(_(e_dictkey), fudi.fd_newkey);
4124 vim_free(fudi.fd_newkey);
4125 return NULL;
4126 }
4127 else
4128 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004129 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004130 }
4131
Bram Moolenaare38eab22019-12-05 21:50:01 +01004132 // An error in a function call during evaluation of an expression in magic
4133 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004134 saved_did_emsg = did_emsg;
4135 did_emsg = FALSE;
4136
4137 /*
4138 * ":function func" with only function name: list function.
4139 */
4140 if (!paren)
4141 {
4142 if (!ends_excmd(*skipwhite(p)))
4143 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004144 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004145 goto ret_free;
4146 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004147 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004148 if (eap->nextcmd != NULL)
4149 *p = NUL;
4150 if (!eap->skip && !got_int)
4151 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004152 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004153 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4154 {
4155 char_u *up = untrans_function_name(name);
4156
4157 // With Vim9 script the name was made script-local, if not
4158 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004159 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004160 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004161 }
4162
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004163 if (fp != NULL)
4164 {
4165 list_func_head(fp, TRUE);
4166 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4167 {
4168 if (FUNCLINE(fp, j) == NULL)
4169 continue;
4170 msg_putchar('\n');
4171 msg_outnum((long)(j + 1));
4172 if (j < 9)
4173 msg_putchar(' ');
4174 if (j < 99)
4175 msg_putchar(' ');
4176 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004177 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004178 ui_breakcheck();
4179 }
4180 if (!got_int)
4181 {
4182 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004183 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 msg_puts(" enddef");
4185 else
4186 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004187 }
4188 }
4189 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004190 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004191 }
4192 goto ret_free;
4193 }
4194
4195 /*
4196 * ":function name(arg1, arg2)" Define function.
4197 */
4198 p = skipwhite(p);
4199 if (*p != '(')
4200 {
4201 if (!eap->skip)
4202 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004203 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004204 goto ret_free;
4205 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004206 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004207 if (vim_strchr(p, '(') != NULL)
4208 p = vim_strchr(p, '(');
4209 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004210
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004211 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4212 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004213 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004214 goto ret_free;
4215 }
4216
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004217 // In Vim9 script only global functions can be redefined.
4218 if (vim9script && eap->forceit && !is_global)
4219 {
4220 emsg(_(e_nobang));
4221 goto ret_free;
4222 }
4223
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004224 ga_init2(&newlines, (int)sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004225
Bram Moolenaar04b12692020-05-04 23:24:44 +02004226 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004227 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004228 // Check the name of the function. Unless it's a dictionary function
4229 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004230 if (name != NULL)
4231 arg = name;
4232 else
4233 arg = fudi.fd_newkey;
4234 if (arg != NULL && (fudi.fd_di == NULL
4235 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4236 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4237 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004238 char_u *name_base = arg;
4239 int i;
4240
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004241 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004242 {
4243 name_base = vim_strchr(arg, '_');
4244 if (name_base == NULL)
4245 name_base = arg + 3;
4246 else
4247 ++name_base;
4248 }
4249 for (i = 0; name_base[i] != NUL && (i == 0
4250 ? eval_isnamec1(name_base[i])
4251 : eval_isnamec(name_base[i])); ++i)
4252 ;
4253 if (name_base[i] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004254 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004255
4256 // In Vim9 script a function cannot have the same name as a
4257 // variable.
4258 if (vim9script && *arg == K_SPECIAL
Mike Williams1821d142021-12-15 16:38:33 +00004259 && eval_variable(name_base, (int)STRLEN(name_base), NULL, NULL,
Bram Moolenaar052ff292021-12-11 13:54:46 +00004260 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
4261 + EVAL_VAR_NO_FUNC) == OK)
4262 {
4263 semsg(_(e_redefining_script_item_str), name_base);
4264 goto ret_free;
4265 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004266 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004267 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004268 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004269 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004270 emsg(_("E862: Cannot use g: here"));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004271 goto ret_free;
4272 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004273 }
4274
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004275 // This may get more lines and make the pointers into the first line
4276 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004277 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004279 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004280 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004281 eap, line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004282 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004283 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004286 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004288 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004289 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004290 if (*p != ':')
4291 {
4292 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4293 p = skipwhite(p);
4294 }
4295 else if (!IS_WHITE_OR_NUL(p[1]))
4296 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004298 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004300 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004301 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004302 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004304 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004306 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004307 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004308 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004309 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004310 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004311 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 else
4314 // find extra arguments "range", "dict", "abort" and "closure"
4315 for (;;)
4316 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004317 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 p = skipwhite(p);
4319 if (STRNCMP(p, "range", 5) == 0)
4320 {
4321 flags |= FC_RANGE;
4322 p += 5;
4323 }
4324 else if (STRNCMP(p, "dict", 4) == 0)
4325 {
4326 flags |= FC_DICT;
4327 p += 4;
4328 }
4329 else if (STRNCMP(p, "abort", 5) == 0)
4330 {
4331 flags |= FC_ABORT;
4332 p += 5;
4333 }
4334 else if (STRNCMP(p, "closure", 7) == 0)
4335 {
4336 flags |= FC_CLOSURE;
4337 p += 7;
4338 if (current_funccal == NULL)
4339 {
4340 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
4341 name == NULL ? (char_u *)"" : name);
4342 goto erret;
4343 }
4344 }
4345 else
4346 break;
4347 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004348
Bram Moolenaare38eab22019-12-05 21:50:01 +01004349 // When there is a line break use what follows for the function body.
4350 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004351 if (*p == '\n')
4352 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004353 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004354 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4355 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004356 && !(VIM_ISWHITE(*whitep) && *p == '#'
4357 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004358 && !eap->skip
4359 && !did_emsg)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004360 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004361
4362 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4364 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004365 */
4366 if (KeyTyped)
4367 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004368 // Check if the function already exists, don't let the user type the
4369 // whole function before telling him it doesn't work! For a script we
4370 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004371 if (!eap->skip && !eap->forceit)
4372 {
4373 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004374 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004375 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004376 emsg_funcname(e_function_str_already_exists_add_excl_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004377 }
4378
4379 if (!eap->skip && did_emsg)
4380 goto erret;
4381
Bram Moolenaare38eab22019-12-05 21:50:01 +01004382 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004383 cmdline_row = msg_row;
4384 }
4385
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004386 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004387 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004388
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004389 // Do not define the function when getting the body fails and when
4390 // skipping.
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004391 if (get_function_body(eap, &newlines, line_arg, line_to_free) == FAIL
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004392 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004393 goto erret;
4394
4395 /*
4396 * If there are no errors, add the function
4397 */
4398 if (fudi.fd_dict == NULL)
4399 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 hashtab_T *ht;
4401
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004402 v = find_var(name, &ht, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004403 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
4404 {
4405 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
4406 name);
4407 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 Moolenaarc553a212021-12-26 20:20:34 +00004434 emsg_funcname(e_function_str_already_exists_add_excl_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 Moolenaarf9e3e092019-01-13 23:38:42 +01004477 emsg(_(e_funcdict));
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 Moolenaarf9e3e092019-01-13 23:38:42 +01004527 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004528 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004529 goto erret;
4530 }
4531 }
4532
Bram Moolenaar47ed5532019-08-08 20:49:14 +02004533 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004534 if (fp == NULL)
4535 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004536 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004537
4538 if (fudi.fd_dict != NULL)
4539 {
4540 if (fudi.fd_di == NULL)
4541 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004542 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004543 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
4544 if (fudi.fd_di == NULL)
4545 {
4546 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004547 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004548 goto erret;
4549 }
4550 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
4551 {
4552 vim_free(fudi.fd_di);
4553 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004554 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004555 goto erret;
4556 }
4557 }
4558 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004559 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004560 clear_tv(&fudi.fd_di->di_tv);
4561 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004562 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004563
Bram Moolenaare38eab22019-12-05 21:50:01 +01004564 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004565 flags |= FC_DICT;
4566 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004567 }
4568 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004569 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004571 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572
4573 if (eap->cmdidx == CMD_def)
4574 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004575 int lnum_save = SOURCING_LNUM;
4576 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004577
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004578 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004579
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004580 // error messages are for the first function line
4581 SOURCING_LNUM = sourcing_lnum_top;
4582
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02004583 // The function may use script variables from the context.
4584 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004585
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004586 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004588 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004589 free_fp = fp_allocated;
4590 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004592 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593
4594 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004595 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004597 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004598 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004599 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02004601 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004603 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004604 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004606 if (fp_allocated)
4607 {
4608 // insert the new function in the function list
4609 set_ufunc_name(fp, name);
4610 if (overwrite)
4611 {
4612 hi = hash_find(&func_hashtab, name);
4613 hi->hi_key = UF2HIKEY(fp);
4614 }
4615 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
4616 {
4617 free_fp = TRUE;
4618 goto erret;
4619 }
4620 fp->uf_refcount = 1;
4621 }
4622
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004623 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004624 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004625 if ((flags & FC_CLOSURE) != 0)
4626 {
Bram Moolenaar58016442016-07-31 18:30:22 +02004627 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004628 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004629 }
4630 else
4631 fp->uf_scoped = NULL;
4632
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004633#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004634 if (prof_def_func())
4635 func_do_profile(fp);
4636#endif
4637 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02004638 if (sandbox)
4639 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004640 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004641 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004642 fp->uf_flags = flags;
4643 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01004644 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004645 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004646 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004647 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 if (is_export)
4649 {
4650 fp->uf_flags |= FC_EXPORT;
4651 // let ex_export() know the export worked.
4652 is_export = FALSE;
4653 }
4654
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004655 if (eap->cmdidx == CMD_def)
4656 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02004657 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
4658 // :func does not use Vim9 script syntax, even in a Vim9 script file
4659 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004660
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004661 goto ret_free;
4662
4663erret:
4664 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004665 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004666 if (fp != NULL)
4667 {
4668 ga_init(&fp->uf_args);
4669 ga_init(&fp->uf_def_args);
4670 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004671errret_2:
4672 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004673 if (fp != NULL)
4674 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004675 if (free_fp)
4676 {
4677 vim_free(fp);
4678 fp = NULL;
4679 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004680ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004681 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004682 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004683 if (name != name_arg)
4684 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004685 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004686 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004687
4688 return fp;
4689}
4690
4691/*
4692 * ":function"
4693 */
4694 void
4695ex_function(exarg_T *eap)
4696{
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004697 char_u *line_to_free = NULL;
4698
4699 (void)define_function(eap, NULL, &line_to_free);
4700 vim_free(line_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02004701}
4702
4703/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004704 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarb2049902021-01-24 12:53:53 +01004705 * be compiled. Except dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02004706 */
4707 void
4708ex_defcompile(exarg_T *eap UNUSED)
4709{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004710 long todo = (long)func_hashtab.ht_used;
4711 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004712 hashitem_T *hi;
4713 ufunc_T *ufunc;
4714
4715 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
4716 {
4717 if (!HASHITEM_EMPTY(hi))
4718 {
4719 --todo;
4720 ufunc = HI2UF(hi);
4721 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004722 && ufunc->uf_def_status == UF_TO_BE_COMPILED
4723 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004724 {
Bram Moolenaare99d4222021-06-13 14:01:26 +02004725 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004726
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004727 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004728 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004729 // a function has been added or removed, need to start over
4730 todo = (long)func_hashtab.ht_used;
4731 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004732 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02004733 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004734 }
4735 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004736 }
4737 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004738}
4739
4740/*
4741 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
4742 * Return 2 if "p" starts with "s:".
4743 * Return 0 otherwise.
4744 */
4745 int
4746eval_fname_script(char_u *p)
4747{
Bram Moolenaare38eab22019-12-05 21:50:01 +01004748 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
4749 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004750 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
4751 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
4752 return 5;
4753 if (p[0] == 's' && p[1] == ':')
4754 return 2;
4755 return 0;
4756}
4757
4758 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004759translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004760{
4761 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004762 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004763 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764}
4765
4766/*
4767 * Return TRUE when "ufunc" has old-style "..." varargs
4768 * or named varargs "...name: type".
4769 */
4770 int
4771has_varargs(ufunc_T *ufunc)
4772{
4773 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004774}
4775
4776/*
4777 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004778 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004779 */
4780 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004781function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004782{
4783 char_u *nm = name;
4784 char_u *p;
4785 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004786 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004787 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004788
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004789 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
4790 if (no_deref)
4791 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004792 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004793 nm = skipwhite(nm);
4794
Bram Moolenaare38eab22019-12-05 21:50:01 +01004795 // Only accept "funcname", "funcname ", "funcname (..." and
4796 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004797 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004798 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004799 vim_free(p);
4800 return n;
4801}
4802
Bram Moolenaar113e1072019-01-20 15:30:40 +01004803#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004804 char_u *
4805get_expanded_name(char_u *name, int check)
4806{
4807 char_u *nm = name;
4808 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004809 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004810
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004811 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004812 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004813
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004814 if (p != NULL && *nm == NUL
4815 && (!check || translated_function_exists(p, is_global)))
4816 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004817
4818 vim_free(p);
4819 return NULL;
4820}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004821#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004822
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004823/*
4824 * Function given to ExpandGeneric() to obtain the list of user defined
4825 * function names.
4826 */
4827 char_u *
4828get_user_func_name(expand_T *xp, int idx)
4829{
4830 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004831 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004832 static hashitem_T *hi;
4833 ufunc_T *fp;
4834
4835 if (idx == 0)
4836 {
4837 done = 0;
4838 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004839 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004840 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004841 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004842 {
4843 if (done++ > 0)
4844 ++hi;
4845 while (HASHITEM_EMPTY(hi))
4846 ++hi;
4847 fp = HI2UF(hi);
4848
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849 // don't show dead, dict and lambda functions
4850 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02004851 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004853
4854 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004855 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004856
4857 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02004858 if (xp->xp_context != EXPAND_USER_FUNC
4859 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004860 {
4861 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004863 STRCAT(IObuff, ")");
4864 }
4865 return IObuff;
4866 }
4867 return NULL;
4868}
4869
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004870/*
4871 * ":delfunction {name}"
4872 */
4873 void
4874ex_delfunction(exarg_T *eap)
4875{
4876 ufunc_T *fp = NULL;
4877 char_u *p;
4878 char_u *name;
4879 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004880 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004881
4882 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004883 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
4884 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004885 vim_free(fudi.fd_newkey);
4886 if (name == NULL)
4887 {
4888 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004889 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004890 return;
4891 }
4892 if (!ends_excmd(*skipwhite(p)))
4893 {
4894 vim_free(name);
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004895 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004896 return;
4897 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004898 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004899 if (eap->nextcmd != NULL)
4900 *p = NUL;
4901
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004902 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02004903 {
4904 if (!eap->skip)
4905 semsg(_(e_invarg2), eap->arg);
4906 vim_free(name);
4907 return;
4908 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004909 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004910 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004911 vim_free(name);
4912
4913 if (!eap->skip)
4914 {
4915 if (fp == NULL)
4916 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004917 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004918 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004919 return;
4920 }
4921 if (fp->uf_calls > 0)
4922 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004923 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004924 return;
4925 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004926 if (fp->uf_flags & FC_VIM9)
4927 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004928 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004929 return;
4930 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004931
4932 if (fudi.fd_dict != NULL)
4933 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004934 // Delete the dict item that refers to the function, it will
4935 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004936 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4937 }
4938 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004939 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004940 // A normal function (not a numbered function or lambda) has a
4941 // refcount of 1 for the entry in the hashtable. When deleting
4942 // it and the refcount is more than one, it should be kept.
4943 // A numbered function and lambda should be kept if the refcount is
4944 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004945 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004946 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004947 // Function is still referenced somewhere. Don't free it but
4948 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004949 if (func_remove(fp))
4950 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004951 }
4952 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004953 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004954 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004955 }
4956}
4957
4958/*
4959 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004960 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004961 */
4962 void
4963func_unref(char_u *name)
4964{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004965 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004966
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004967 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004968 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004969 fp = find_func(name, FALSE, NULL);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004970 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004971 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004972#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004973 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004974#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004975 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004976 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004977 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004978}
4979
4980/*
4981 * Unreference a Function: decrement the reference count and free it when it
4982 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004983 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004984 */
4985 void
4986func_ptr_unref(ufunc_T *fp)
4987{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004988 if (fp != NULL && (--fp->uf_refcount <= 0
4989 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
4990 && fp->uf_partial->pt_refcount <= 1
4991 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02004992 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004993 // Only delete it when it's not being used. Otherwise it's done
4994 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004995 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004996 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004997 }
4998}
4999
5000/*
5001 * Count a reference to a Function.
5002 */
5003 void
5004func_ref(char_u *name)
5005{
5006 ufunc_T *fp;
5007
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005008 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005009 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005010 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005011 if (fp != NULL)
5012 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005013 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005014 // Only give an error for a numbered function.
5015 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005016 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005017}
5018
5019/*
5020 * Count a reference to a Function.
5021 */
5022 void
5023func_ptr_ref(ufunc_T *fp)
5024{
5025 if (fp != NULL)
5026 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005027}
5028
5029/*
5030 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5031 * referenced from anywhere that is in use.
5032 */
5033 static int
5034can_free_funccal(funccall_T *fc, int copyID)
5035{
5036 return (fc->l_varlist.lv_copyID != copyID
5037 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005038 && fc->l_avars.dv_copyID != copyID
5039 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005040}
5041
5042/*
5043 * ":return [expr]"
5044 */
5045 void
5046ex_return(exarg_T *eap)
5047{
5048 char_u *arg = eap->arg;
5049 typval_T rettv;
5050 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005051 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005052
5053 if (current_funccal == NULL)
5054 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005055 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005056 return;
5057 }
5058
Bram Moolenaar844fb642021-10-23 13:32:30 +01005059 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005060 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5061
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005062 if (eap->skip)
5063 ++emsg_skip;
5064
5065 eap->nextcmd = NULL;
5066 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005067 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005068 {
5069 if (!eap->skip)
5070 returning = do_return(eap, FALSE, TRUE, &rettv);
5071 else
5072 clear_tv(&rettv);
5073 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005074 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005075 else if (!eap->skip)
5076 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005077 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005078 update_force_abort();
5079
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005080 /*
5081 * Return unless the expression evaluation has been cancelled due to an
5082 * aborting error, an interrupt, or an exception.
5083 */
5084 if (!aborting())
5085 returning = do_return(eap, FALSE, TRUE, NULL);
5086 }
5087
Bram Moolenaare38eab22019-12-05 21:50:01 +01005088 // When skipping or the return gets pending, advance to the next command
5089 // in this line (!returning). Otherwise, ignore the rest of the line.
5090 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005091 if (returning)
5092 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005093 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005094 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005095
5096 if (eap->skip)
5097 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005098 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005099}
5100
5101/*
5102 * ":1,25call func(arg1, arg2)" function call.
5103 */
5104 void
5105ex_call(exarg_T *eap)
5106{
5107 char_u *arg = eap->arg;
5108 char_u *startarg;
5109 char_u *name;
5110 char_u *tofree;
5111 int len;
5112 typval_T rettv;
5113 linenr_T lnum;
5114 int doesrange;
5115 int failed = FALSE;
5116 funcdict_T fudi;
5117 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005118 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005119 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005120 int found_var = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005121
Bram Moolenaare6b53242020-07-01 17:28:33 +02005122 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005123 if (eap->skip)
5124 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005125 // trans_function_name() doesn't work well when skipping, use eval0()
5126 // instead to skip to any following command, e.g. for:
5127 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005128 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005129 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005130 clear_tv(&rettv);
5131 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005132 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005133 return;
5134 }
5135
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005136 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
5137 &fudi, &partial, in_vim9script() ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005138 if (fudi.fd_newkey != NULL)
5139 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005140 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005141 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005142 vim_free(fudi.fd_newkey);
5143 }
5144 if (tofree == NULL)
5145 return;
5146
Bram Moolenaare38eab22019-12-05 21:50:01 +01005147 // Increase refcount on dictionary, it could get deleted when evaluating
5148 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005149 if (fudi.fd_dict != NULL)
5150 ++fudi.fd_dict->dv_refcount;
5151
Bram Moolenaare38eab22019-12-05 21:50:01 +01005152 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
5153 // contents. For VAR_PARTIAL get its partial, unless we already have one
5154 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005155 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005156 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005157 in_vim9script() && type == NULL ? &type : NULL, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005158
Bram Moolenaare38eab22019-12-05 21:50:01 +01005159 // Skip white space to allow ":call func ()". Not good, but required for
5160 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005161 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005162 if (*startarg != '(')
5163 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005164 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005165 goto end;
5166 }
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005167 if (in_vim9script() && startarg > arg)
5168 {
5169 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
5170 goto end;
5171 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005172
5173 /*
5174 * When skipping, evaluate the function once, to find the end of the
5175 * arguments.
5176 * When the function takes a range, this is discovered after the first
5177 * call, and the loop is broken.
5178 */
5179 if (eap->skip)
5180 {
5181 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005182 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005183 }
5184 else
5185 lnum = eap->line1;
5186 for ( ; lnum <= eap->line2; ++lnum)
5187 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005188 funcexe_T funcexe;
5189
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005190 if (!eap->skip && eap->addr_count > 0)
5191 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01005192 if (lnum > curbuf->b_ml.ml_line_count)
5193 {
5194 // If the function deleted lines or switched to another buffer
5195 // the line number may become invalid.
Bram Moolenaar108010a2021-06-27 22:03:33 +02005196 emsg(_(e_invalid_range));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01005197 break;
5198 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005199 curwin->w_cursor.lnum = lnum;
5200 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005201 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005202 }
5203 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005204
Bram Moolenaara80faa82020-04-12 19:37:17 +02005205 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005206 funcexe.fe_firstline = eap->line1;
5207 funcexe.fe_lastline = eap->line2;
5208 funcexe.fe_doesrange = &doesrange;
5209 funcexe.fe_evaluate = !eap->skip;
5210 funcexe.fe_partial = partial;
5211 funcexe.fe_selfdict = fudi.fd_dict;
5212 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005213 funcexe.fe_found_var = found_var;
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005214 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaare6b53242020-07-01 17:28:33 +02005215 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005216 {
5217 failed = TRUE;
5218 break;
5219 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01005220 if (has_watchexpr())
5221 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005222
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005223 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005224 if (handle_subscript(&arg, &rettv,
5225 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005226 {
5227 failed = TRUE;
5228 break;
5229 }
5230
5231 clear_tv(&rettv);
5232 if (doesrange || eap->skip)
5233 break;
5234
Bram Moolenaare38eab22019-12-05 21:50:01 +01005235 // Stop when immediately aborting on error, or when an interrupt
5236 // occurred or an exception was thrown but not caught.
5237 // get_func_tv() returned OK, so that the check for trailing
5238 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005239 if (aborting())
5240 break;
5241 }
5242 if (eap->skip)
5243 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005244 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005245
Bram Moolenaar1d341892021-09-18 15:25:52 +02005246 // When inside :try we need to check for following "| catch" or "| endtry".
5247 // Not when there was an error, but do check if an exception was thrown.
5248 if ((!aborting() || did_throw)
5249 && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005250 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005251 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02005252 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02005253 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005254 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02005255 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005256 {
5257 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005258 semsg(_(e_trailing_arg), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005259 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005260 }
5261 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02005262 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005263 }
5264
5265end:
5266 dict_unref(fudi.fd_dict);
5267 vim_free(tofree);
5268}
5269
5270/*
5271 * Return from a function. Possibly makes the return pending. Also called
5272 * for a pending return at the ":endtry" or after returning from an extra
5273 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
5274 * when called due to a ":return" command. "rettv" may point to a typval_T
5275 * with the return rettv. Returns TRUE when the return can be carried out,
5276 * FALSE when the return gets pending.
5277 */
5278 int
5279do_return(
5280 exarg_T *eap,
5281 int reanimate,
5282 int is_cmd,
5283 void *rettv)
5284{
5285 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01005286 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005287
5288 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005289 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005290 current_funccal->returned = FALSE;
5291
5292 /*
5293 * Cleanup (and inactivate) conditionals, but stop when a try conditional
5294 * not in its finally clause (which then is to be executed next) is found.
5295 * In this case, make the ":return" pending for execution at the ":endtry".
5296 * Otherwise, return normally.
5297 */
5298 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
5299 if (idx >= 0)
5300 {
5301 cstack->cs_pending[idx] = CSTP_RETURN;
5302
5303 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005304 // A pending return again gets pending. "rettv" points to an
5305 // allocated variable with the rettv of the original ":return"'s
5306 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005307 cstack->cs_rettv[idx] = rettv;
5308 else
5309 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005310 // When undoing a return in order to make it pending, get the stored
5311 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005312 if (reanimate)
5313 rettv = current_funccal->rettv;
5314
5315 if (rettv != NULL)
5316 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005317 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005318 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
5319 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
5320 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005321 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005322 }
5323 else
5324 cstack->cs_rettv[idx] = NULL;
5325
5326 if (reanimate)
5327 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005328 // The pending return value could be overwritten by a ":return"
5329 // without argument in a finally clause; reset the default
5330 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005331 current_funccal->rettv->v_type = VAR_NUMBER;
5332 current_funccal->rettv->vval.v_number = 0;
5333 }
5334 }
5335 report_make_pending(CSTP_RETURN, rettv);
5336 }
5337 else
5338 {
5339 current_funccal->returned = TRUE;
5340
Bram Moolenaare38eab22019-12-05 21:50:01 +01005341 // If the return is carried out now, store the return value. For
5342 // a return immediately after reanimation, the value is already
5343 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005344 if (!reanimate && rettv != NULL)
5345 {
5346 clear_tv(current_funccal->rettv);
5347 *current_funccal->rettv = *(typval_T *)rettv;
5348 if (!is_cmd)
5349 vim_free(rettv);
5350 }
5351 }
5352
5353 return idx < 0;
5354}
5355
5356/*
5357 * Free the variable with a pending return value.
5358 */
5359 void
5360discard_pending_return(void *rettv)
5361{
5362 free_tv((typval_T *)rettv);
5363}
5364
5365/*
5366 * Generate a return command for producing the value of "rettv". The result
5367 * is an allocated string. Used by report_pending() for verbose messages.
5368 */
5369 char_u *
5370get_return_cmd(void *rettv)
5371{
5372 char_u *s = NULL;
5373 char_u *tofree = NULL;
5374 char_u numbuf[NUMBUFLEN];
5375
5376 if (rettv != NULL)
5377 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
5378 if (s == NULL)
5379 s = (char_u *)"";
5380
5381 STRCPY(IObuff, ":return ");
5382 STRNCPY(IObuff + 8, s, IOSIZE - 8);
5383 if (STRLEN(s) + 8 >= IOSIZE)
5384 STRCPY(IObuff + IOSIZE - 4, "...");
5385 vim_free(tofree);
5386 return vim_strsave(IObuff);
5387}
5388
5389/*
5390 * Get next function line.
5391 * Called by do_cmdline() to get the next line.
5392 * Returns allocated string, or NULL for end of function.
5393 */
5394 char_u *
5395get_func_line(
5396 int c UNUSED,
5397 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02005398 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005399 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005400{
5401 funccall_T *fcp = (funccall_T *)cookie;
5402 ufunc_T *fp = fcp->func;
5403 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005404 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005405
Bram Moolenaare38eab22019-12-05 21:50:01 +01005406 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005407 if (fcp->dbg_tick != debug_tick)
5408 {
5409 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005410 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005411 fcp->dbg_tick = debug_tick;
5412 }
5413#ifdef FEAT_PROFILE
5414 if (do_profiling == PROF_YES)
5415 func_line_end(cookie);
5416#endif
5417
5418 gap = &fp->uf_lines;
5419 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5420 || fcp->returned)
5421 retval = NULL;
5422 else
5423 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005424 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005425 while (fcp->linenr < gap->ga_len
5426 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
5427 ++fcp->linenr;
5428 if (fcp->linenr >= gap->ga_len)
5429 retval = NULL;
5430 else
5431 {
5432 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005433 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005434#ifdef FEAT_PROFILE
5435 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005436 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005437#endif
5438 }
5439 }
5440
Bram Moolenaare38eab22019-12-05 21:50:01 +01005441 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005442 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005443 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005444 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01005445 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005446 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005447 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005448 fcp->dbg_tick = debug_tick;
5449 }
5450
5451 return retval;
5452}
5453
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005454/*
5455 * Return TRUE if the currently active function should be ended, because a
5456 * return was encountered or an error occurred. Used inside a ":while".
5457 */
5458 int
5459func_has_ended(void *cookie)
5460{
5461 funccall_T *fcp = (funccall_T *)cookie;
5462
Bram Moolenaare38eab22019-12-05 21:50:01 +01005463 // Ignore the "abort" flag if the abortion behavior has been changed due to
5464 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005465 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5466 || fcp->returned);
5467}
5468
5469/*
5470 * return TRUE if cookie indicates a function which "abort"s on errors.
5471 */
5472 int
5473func_has_abort(
5474 void *cookie)
5475{
5476 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
5477}
5478
5479
5480/*
5481 * Turn "dict.Func" into a partial for "Func" bound to "dict".
5482 * Don't do this when "Func" is already a partial that was bound
5483 * explicitly (pt_auto is FALSE).
5484 * Changes "rettv" in-place.
5485 * Returns the updated "selfdict_in".
5486 */
5487 dict_T *
5488make_partial(dict_T *selfdict_in, typval_T *rettv)
5489{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005490 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005491 char_u *tofree = NULL;
5492 ufunc_T *fp;
5493 char_u fname_buf[FLEN_FIXED + 1];
5494 int error;
5495 dict_T *selfdict = selfdict_in;
5496
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005497 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
5498 fp = rettv->vval.v_partial->pt_func;
5499 else
5500 {
5501 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
5502 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005503 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005504 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005505 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005506 vim_free(tofree);
5507 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005508
5509 if (fp != NULL && (fp->uf_flags & FC_DICT))
5510 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005511 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005512
5513 if (pt != NULL)
5514 {
5515 pt->pt_refcount = 1;
5516 pt->pt_dict = selfdict;
5517 pt->pt_auto = TRUE;
5518 selfdict = NULL;
5519 if (rettv->v_type == VAR_FUNC)
5520 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005521 // Just a function: Take over the function name and use
5522 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005523 pt->pt_name = rettv->vval.v_string;
5524 }
5525 else
5526 {
5527 partial_T *ret_pt = rettv->vval.v_partial;
5528 int i;
5529
Bram Moolenaare38eab22019-12-05 21:50:01 +01005530 // Partial: copy the function name, use selfdict and copy
5531 // args. Can't take over name or args, the partial might
5532 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005533 if (ret_pt->pt_name != NULL)
5534 {
5535 pt->pt_name = vim_strsave(ret_pt->pt_name);
5536 func_ref(pt->pt_name);
5537 }
5538 else
5539 {
5540 pt->pt_func = ret_pt->pt_func;
5541 func_ptr_ref(pt->pt_func);
5542 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005543 if (ret_pt->pt_argc > 0)
5544 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005545 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005546 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005547 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005548 pt->pt_argc = 0;
5549 else
5550 {
5551 pt->pt_argc = ret_pt->pt_argc;
5552 for (i = 0; i < pt->pt_argc; i++)
5553 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
5554 }
5555 }
5556 partial_unref(ret_pt);
5557 }
5558 rettv->v_type = VAR_PARTIAL;
5559 rettv->vval.v_partial = pt;
5560 }
5561 }
5562 return selfdict;
5563}
5564
5565/*
5566 * Return the name of the executed function.
5567 */
5568 char_u *
5569func_name(void *cookie)
5570{
5571 return ((funccall_T *)cookie)->func->uf_name;
5572}
5573
5574/*
5575 * Return the address holding the next breakpoint line for a funccall cookie.
5576 */
5577 linenr_T *
5578func_breakpoint(void *cookie)
5579{
5580 return &((funccall_T *)cookie)->breakpoint;
5581}
5582
5583/*
5584 * Return the address holding the debug tick for a funccall cookie.
5585 */
5586 int *
5587func_dbg_tick(void *cookie)
5588{
5589 return &((funccall_T *)cookie)->dbg_tick;
5590}
5591
5592/*
5593 * Return the nesting level for a funccall cookie.
5594 */
5595 int
5596func_level(void *cookie)
5597{
5598 return ((funccall_T *)cookie)->level;
5599}
5600
5601/*
5602 * Return TRUE when a function was ended by a ":return" command.
5603 */
5604 int
5605current_func_returned(void)
5606{
5607 return current_funccal->returned;
5608}
5609
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005610 int
5611free_unref_funccal(int copyID, int testing)
5612{
5613 int did_free = FALSE;
5614 int did_free_funccal = FALSE;
5615 funccall_T *fc, **pfc;
5616
5617 for (pfc = &previous_funccal; *pfc != NULL; )
5618 {
5619 if (can_free_funccal(*pfc, copyID))
5620 {
5621 fc = *pfc;
5622 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01005623 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005624 did_free = TRUE;
5625 did_free_funccal = TRUE;
5626 }
5627 else
5628 pfc = &(*pfc)->caller;
5629 }
5630 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005631 // When a funccal was freed some more items might be garbage
5632 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005633 (void)garbage_collect(testing);
5634
5635 return did_free;
5636}
5637
5638/*
Bram Moolenaarba209902016-08-24 22:06:38 +02005639 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005640 */
5641 static funccall_T *
5642get_funccal(void)
5643{
5644 int i;
5645 funccall_T *funccal;
5646 funccall_T *temp_funccal;
5647
5648 funccal = current_funccal;
5649 if (debug_backtrace_level > 0)
5650 {
5651 for (i = 0; i < debug_backtrace_level; i++)
5652 {
5653 temp_funccal = funccal->caller;
5654 if (temp_funccal)
5655 funccal = temp_funccal;
5656 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005657 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005658 debug_backtrace_level = i;
5659 }
5660 }
5661 return funccal;
5662}
5663
5664/*
5665 * Return the hashtable used for local variables in the current funccal.
5666 * Return NULL if there is no current funccal.
5667 */
5668 hashtab_T *
5669get_funccal_local_ht()
5670{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005671 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005672 return NULL;
5673 return &get_funccal()->l_vars.dv_hashtab;
5674}
5675
5676/*
5677 * Return the l: scope variable.
5678 * Return NULL if there is no current funccal.
5679 */
5680 dictitem_T *
5681get_funccal_local_var()
5682{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005683 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005684 return NULL;
5685 return &get_funccal()->l_vars_var;
5686}
5687
5688/*
5689 * Return the hashtable used for argument in the current funccal.
5690 * Return NULL if there is no current funccal.
5691 */
5692 hashtab_T *
5693get_funccal_args_ht()
5694{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005695 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005696 return NULL;
5697 return &get_funccal()->l_avars.dv_hashtab;
5698}
5699
5700/*
5701 * Return the a: scope variable.
5702 * Return NULL if there is no current funccal.
5703 */
5704 dictitem_T *
5705get_funccal_args_var()
5706{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005707 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005708 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01005709 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005710}
5711
5712/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005713 * List function variables, if there is a function.
5714 */
5715 void
5716list_func_vars(int *first)
5717{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005718 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005719 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01005720 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005721}
5722
5723/*
5724 * If "ht" is the hashtable for local variables in the current funccal, return
5725 * the dict that contains it.
5726 * Otherwise return NULL.
5727 */
5728 dict_T *
5729get_current_funccal_dict(hashtab_T *ht)
5730{
5731 if (current_funccal != NULL
5732 && ht == &current_funccal->l_vars.dv_hashtab)
5733 return &current_funccal->l_vars;
5734 return NULL;
5735}
5736
5737/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005738 * Search hashitem in parent scope.
5739 */
5740 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005741find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005742{
5743 funccall_T *old_current_funccal = current_funccal;
5744 hashtab_T *ht;
5745 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005746 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005747
5748 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5749 return NULL;
5750
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02005751 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005752 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02005753 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005754 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005755 ht = find_var_ht(name, &varname);
5756 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02005757 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005758 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02005759 if (!HASHITEM_EMPTY(hi))
5760 {
5761 *pht = ht;
5762 break;
5763 }
5764 }
5765 if (current_funccal == current_funccal->func->uf_scoped)
5766 break;
5767 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005768 }
5769 current_funccal = old_current_funccal;
5770
5771 return hi;
5772}
5773
5774/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005775 * Search variable in parent scope.
5776 */
5777 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005778find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005779{
5780 dictitem_T *v = NULL;
5781 funccall_T *old_current_funccal = current_funccal;
5782 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005783 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005784
5785 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5786 return NULL;
5787
Bram Moolenaare38eab22019-12-05 21:50:01 +01005788 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005789 current_funccal = current_funccal->func->uf_scoped;
5790 while (current_funccal)
5791 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005792 ht = find_var_ht(name, &varname);
5793 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005794 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005795 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005796 if (v != NULL)
5797 break;
5798 }
5799 if (current_funccal == current_funccal->func->uf_scoped)
5800 break;
5801 current_funccal = current_funccal->func->uf_scoped;
5802 }
5803 current_funccal = old_current_funccal;
5804
5805 return v;
5806}
5807
5808/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005809 * Set "copyID + 1" in previous_funccal and callers.
5810 */
5811 int
5812set_ref_in_previous_funccal(int copyID)
5813{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005814 funccall_T *fc;
5815
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005816 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005817 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005818 fc->fc_copyID = copyID + 1;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005819 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
5820 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
5821 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL))
5822 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005823 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005824 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005825}
5826
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005827 static int
5828set_ref_in_funccal(funccall_T *fc, int copyID)
5829{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005830 if (fc->fc_copyID != copyID)
5831 {
5832 fc->fc_copyID = copyID;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005833 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
5834 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
5835 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
5836 || set_ref_in_func(NULL, fc->func, copyID))
5837 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005838 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005839 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005840}
5841
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005842/*
5843 * Set "copyID" in all local vars and arguments in the call stack.
5844 */
5845 int
5846set_ref_in_call_stack(int copyID)
5847{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005848 funccall_T *fc;
5849 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005850
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005851 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5852 if (set_ref_in_funccal(fc, copyID))
5853 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005854
5855 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005856 for (entry = funccal_stack; entry != NULL; entry = entry->next)
5857 for (fc = entry->top_funccal; fc != NULL; fc = fc->caller)
5858 if (set_ref_in_funccal(fc, copyID))
5859 return TRUE;
5860 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005861}
5862
5863/*
5864 * Set "copyID" in all functions available by name.
5865 */
5866 int
5867set_ref_in_functions(int copyID)
5868{
5869 int todo;
5870 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005871 ufunc_T *fp;
5872
5873 todo = (int)func_hashtab.ht_used;
5874 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005875 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005876 if (!HASHITEM_EMPTY(hi))
5877 {
5878 --todo;
5879 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005880 if (!func_name_refcount(fp->uf_name)
5881 && set_ref_in_func(NULL, fp, copyID))
5882 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005883 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005884 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005885 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005886}
5887
5888/*
5889 * Set "copyID" in all function arguments.
5890 */
5891 int
5892set_ref_in_func_args(int copyID)
5893{
5894 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005895
5896 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005897 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5898 copyID, NULL, NULL))
5899 return TRUE;
5900 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005901}
5902
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005903/*
5904 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005905 * Returns TRUE if setting references failed somehow.
5906 */
5907 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005908set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005909{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005910 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005911 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005912 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005913 char_u fname_buf[FLEN_FIXED + 1];
5914 char_u *tofree = NULL;
5915 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005916 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005917
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005918 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005919 return FALSE;
5920
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005921 if (fp_in == NULL)
5922 {
5923 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005924 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005925 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005926 if (fp != NULL)
5927 {
5928 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005929 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005930 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005931
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005932 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005933 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005934}
5935
Bram Moolenaare38eab22019-12-05 21:50:01 +01005936#endif // FEAT_EVAL