blob: 9ac1274a48b127cddcd8fa3983ac8ffcfe973864 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020032static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010033static void func_clear(ufunc_T *fp, int force);
34static int func_free(ufunc_T *fp, int force);
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +010035static char_u *untrans_function_name(char_u *name);
Bram Moolenaar58779852022-09-06 18:31:14 +010036static void handle_defer_one(funccall_T *funccal);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020037
38 void
39func_init()
40{
41 hash_init(&func_hashtab);
42}
43
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020044/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020045 * Return the function hash table
46 */
47 hashtab_T *
48func_tbl_get(void)
49{
50 return &func_hashtab;
51}
52
53/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020054 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020055 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010056 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010057 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000058 * If "eap" 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 Moolenaardce24412022-02-08 20:35:30 +000069 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020070 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010071 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072{
Bram Moolenaar6e949782020-04-13 17:21:00 +020073 char_u *p = arg;
74 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020075 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010076
77 while (ASCII_ISALNUM(*p) || *p == '_')
78 ++p;
79 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020080 || (argtypes == NULL
81 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
82 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083 {
84 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000085 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 return arg;
87 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010088
Bram Moolenaar057e84a2021-02-28 16:55:11 +010089 // Vim9 script: cannot use script var name for argument. In function: also
90 // check local vars and arguments.
91 if (!skip && argtypes != NULL && check_defined(arg, p - arg,
Bram Moolenaardce24412022-02-08 20:35:30 +000092 evalarg == NULL ? NULL : evalarg->eval_cctx,
93 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarb4893b82021-02-21 22:20:24 +010094 return arg;
Bram Moolenaarb4893b82021-02-21 22:20:24 +010095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
97 return arg;
98 if (newargs != NULL)
99 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100100 int c;
101 int i;
102
103 c = *p;
104 *p = NUL;
105 arg_copy = vim_strsave(arg);
106 if (arg_copy == NULL)
107 {
108 *p = c;
109 return arg;
110 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200111 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200112 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200113 // Check for duplicate argument name.
114 for (i = 0; i < newargs->ga_len; ++i)
115 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
116 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000117 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200118 vim_free(arg_copy);
119 return arg;
120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
122 newargs->ga_len++;
123
124 *p = c;
125 }
126
127 // get any type from "arg: type"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100128 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 {
130 char_u *type = NULL;
131
Bram Moolenaar6e949782020-04-13 17:21:00 +0200132 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
133 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200134 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200135 arg_copy == NULL ? arg : arg_copy);
136 p = skipwhite(p);
137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 if (*p == ':')
139 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200140 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100141 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200142 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100143 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200144 return arg;
145 }
146 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200147 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100148 if (!skip)
149 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200151 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200152 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200153 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200154 arg_copy == NULL ? arg : arg_copy);
155 return arg;
156 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100157 if (!skip)
158 {
159 if (type == NULL && types_optional)
160 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200161 type = vim_strsave((char_u *)
162 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100163 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 }
166
167 return p;
168}
169
170/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000171 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000172 * Get a next line, store it in "eap" if appropriate and put the line in
173 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000174 */
175 static char_u *
176get_function_line(
177 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000178 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000179 int indent,
180 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000181{
182 char_u *theline;
183
184 if (eap->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000185 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000186 else
187 theline = eap->getline(':', eap->cookie, indent, getline_options);
188 if (theline != NULL)
189 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000190 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000191 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000192 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
193 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000194 *eap->cmdlinep = theline;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000195 ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000196 }
197
198 return theline;
199}
200
201/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200202 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100203 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100204 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200205 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100206 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200207get_function_args(
208 char_u **argp,
209 char_u endchar,
210 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100212 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100213 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200214 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200215 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200216 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000217 exarg_T *eap, // can be NULL
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000218 int in_class, // TRUE when inside a class
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000219 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000220 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200221{
222 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100223 char_u *arg;
224 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200225 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200226 int any_default = FALSE;
227 char_u *expr;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100228 char_u *whitep = *argp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200229
230 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000231 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000233 ga_init2(argtypes, sizeof(char_u *), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200234 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000235 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200236
237 if (varargs != NULL)
238 *varargs = FALSE;
239
240 /*
241 * Isolate the arguments: "arg1, arg2, ...)"
242 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100243 arg = skipwhite(*argp);
244 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200245 while (*p != endchar)
246 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200247 while (eap != NULL && eap->getline != NULL
248 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200249 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200250 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000251 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000252 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000253
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200254 if (theline == NULL)
255 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200256 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200257 p = skipwhite(theline);
258 }
259
260 if (mustend && *p != endchar)
261 {
262 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000263 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100264 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200265 }
266 if (*p == endchar)
267 break;
268
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200269 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
270 {
271 if (varargs != NULL)
272 *varargs = TRUE;
273 p += 3;
274 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275
276 if (argtypes != NULL)
277 {
278 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200279 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100280 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100281 if (!skip)
282 emsg(_(e_missing_name_after_dots));
283 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100284 }
285
286 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100287 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaardce24412022-02-08 20:35:30 +0000288 evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100289 if (p == arg)
290 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100291 if (*skipwhite(p) == '=')
292 {
293 emsg(_(e_cannot_use_default_for_variable_arguments));
294 break;
295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100296 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200297 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000298 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000299 {
300 // this.memberName
301 p += 5;
302 arg = p;
303 while (ASCII_ISALNUM(*p) || *p == '_')
304 ++p;
305
306 // TODO: check the argument is indeed a member
307 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
308 return FAIL;
309 if (newargs != NULL)
310 {
311 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
312 vim_strnsave(arg, p - arg);
313 newargs->ga_len++;
314
315 if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
316 {
317 // TODO: use the actual type
318 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
319 vim_strsave((char_u *)"any");
320
321 // Add a line to the function body for the assignment.
322 if (ga_grow(newlines, 1) == OK)
323 {
324 // "this.name = name"
325 int len = 5 + (p - arg) + 3 + (p - arg) + 1;
326 char_u *assignment = alloc(len);
327 if (assignment != NULL)
328 {
329 c = *p;
330 *p = NUL;
331 vim_snprintf((char *)assignment, len,
332 "this.%s = %s", arg, arg);
333 *p = c;
334 ((char_u **)(newlines->ga_data))[
335 newlines->ga_len++] = assignment;
336 }
337 }
338 }
339 }
340 if (*p == ',')
341 ++p;
342 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200343 else
344 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200345 char_u *np;
346
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200347 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100348 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaardce24412022-02-08 20:35:30 +0000349 evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100350 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200351 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200352
Bram Moolenaar015cf102021-06-26 21:52:02 +0200353 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200354 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200355 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200356 if (*np == '=' && np[1] != '=' && np[1] != '~'
357 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200358 {
359 typval_T rettv;
360
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100361 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200362 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100363 p = skipwhite(np + 1);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200364 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200365 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200366 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200367 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200368 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200369 if (ga_grow(default_args, 1) == FAIL)
370 goto err_ret;
371
372 // trim trailing whitespace
373 while (p > expr && VIM_ISWHITE(p[-1]))
374 p--;
375 c = *p;
376 *p = NUL;
377 expr = vim_strsave(expr);
378 if (expr == NULL)
379 {
380 *p = c;
381 goto err_ret;
382 }
383 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200384 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200385 default_args->ga_len++;
386 *p = c;
387 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200388 }
389 else
390 mustend = TRUE;
391 }
392 else if (any_default)
393 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000394 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200395 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200396 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200397
398 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
399 {
400 // Be tolerant when skipping
401 if (!skip)
402 {
403 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
404 goto err_ret;
405 }
406 p = skipwhite(p);
407 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200408 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200409 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200410 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200411 // Don't give this error when skipping, it makes the "->" not
412 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100413 // Allow missing space after comma in legacy functions.
414 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200415 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
416 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100417 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200418 goto err_ret;
419 }
420 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200421 else
422 mustend = TRUE;
423 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200424 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200425 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200426 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200427
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200428 if (*p != endchar)
429 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100430 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200431
432 *argp = p;
433 return OK;
434
435err_ret:
436 if (newargs != NULL)
437 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200438 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200439 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200440 return FAIL;
441}
442
443/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100444 * Parse the argument types, filling "fp->uf_arg_types".
445 * Return OK or FAIL.
446 */
447 static int
448parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
449{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200450 int len = 0;
451
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100452 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
453 if (argtypes->ga_len > 0)
454 {
455 // When "varargs" is set the last name/type goes into uf_va_name
456 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200457 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100458
459 if (len > 0)
460 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
461 if (fp->uf_arg_types != NULL)
462 {
463 int i;
464 type_T *type;
465
466 for (i = 0; i < len; ++ i)
467 {
468 char_u *p = ((char_u **)argtypes->ga_data)[i];
469
470 if (p == NULL)
471 // will get the type from the default value
472 type = &t_unknown;
473 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100474 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100475 if (type == NULL)
476 return FAIL;
477 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000478 if (i < fp->uf_args.ga_len
479 && (type->tt_type == VAR_FUNC
480 || type->tt_type == VAR_PARTIAL)
481 && var_wrong_func_name(
482 ((char_u **)fp->uf_args.ga_data)[i], TRUE))
483 return FAIL;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100484 }
485 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100486 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200487
488 if (varargs)
489 {
490 char_u *p;
491
492 // Move the last argument "...name: type" to uf_va_name and
493 // uf_va_type.
494 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
495 [fp->uf_args.ga_len - 1];
496 --fp->uf_args.ga_len;
497 p = ((char_u **)argtypes->ga_data)[len];
498 if (p == NULL)
499 // TODO: get type from default value
500 fp->uf_va_type = &t_list_any;
501 else
502 {
503 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
504 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
505 {
506 semsg(_(e_variable_arguments_type_must_be_list_str),
507 ((char_u **)argtypes->ga_data)[len]);
508 return FAIL;
509 }
510 }
511 if (fp->uf_va_type == NULL)
512 return FAIL;
513 }
514
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100515 return OK;
516}
517
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100518 static int
519parse_return_type(ufunc_T *fp, char_u *ret_type)
520{
521 if (ret_type == NULL)
522 fp->uf_ret_type = &t_void;
523 else
524 {
525 char_u *p = ret_type;
526
527 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
528 if (fp->uf_ret_type == NULL)
529 {
530 fp->uf_ret_type = &t_void;
531 return FAIL;
532 }
533 }
534 return OK;
535}
536
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100537/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200538 * Register function "fp" as using "current_funccal" as its scope.
539 */
540 static int
541register_closure(ufunc_T *fp)
542{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200543 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100544 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200545 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200546 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200547 fp->uf_scoped = current_funccal;
548 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200549
Bram Moolenaarca16c602022-09-06 18:57:08 +0100550 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200551 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100552 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
553 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200554 return OK;
555}
556
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100557 static void
558set_ufunc_name(ufunc_T *fp, char_u *name)
559{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100560 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
561 // actually extends beyond the struct.
562 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100563
564 if (name[0] == K_SPECIAL)
565 {
566 fp->uf_name_exp = alloc(STRLEN(name) + 3);
567 if (fp->uf_name_exp != NULL)
568 {
569 STRCPY(fp->uf_name_exp, "<SNR>");
570 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
571 }
572 }
573}
574
Bram Moolenaar58016442016-07-31 18:30:22 +0200575/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100576 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
577 * return "buf" filled with a readable function name.
578 * Otherwise just return "name", thus the return value can always be used.
579 * "name" and "buf" may be equal.
580 */
581 char_u *
582make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
583{
584 size_t len;
585
586 if (name[0] != K_SPECIAL)
587 return name;
588 len = STRLEN(name);
589 if (len + 3 > bufsize)
590 return name;
591
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100592 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100593 mch_memmove(buf, "<SNR>", 5);
594 return buf;
595}
596
597/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200598 * Get a name for a lambda. Returned in static memory.
599 */
600 char_u *
601get_lambda_name(void)
602{
603 static char_u name[30];
604 static int lambda_no = 0;
605
606 sprintf((char*)name, "<lambda>%d", ++lambda_no);
607 return name;
608}
609
Bram Moolenaar801ab062020-06-25 19:27:56 +0200610#if defined(FEAT_LUA) || defined(PROTO)
611/*
612 * Registers a native C callback which can be called from Vim script.
613 * Returns the name of the Vim script function.
614 */
615 char_u *
616register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
617{
618 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200619 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200620
621 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
622 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200623 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200624
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200625 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200626 fp->uf_refcount = 1;
627 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000628 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200629 fp->uf_calls = 0;
630 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200631 fp->uf_cb = cb;
632 fp->uf_cb_free = cb_free;
633 fp->uf_cb_state = state;
634
635 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000636 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200637
638 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200639}
640#endif
641
Bram Moolenaar04b12692020-05-04 23:24:44 +0200642/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100643 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100644 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100645 * If "white_error" is not NULL check for correct use of white space and set
646 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100647 * Return NULL if no valid arrow found.
648 */
649 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100650skip_arrow(
651 char_u *start,
652 int equal_arrow,
653 char_u **ret_type,
654 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100655{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100656 char_u *s = start;
657 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100658
659 if (equal_arrow)
660 {
661 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100662 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100663 if (white_error != NULL && !VIM_ISWHITE(s[1]))
664 {
665 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100666 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100667 return NULL;
668 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100669 s = skipwhite(s + 1);
670 *ret_type = s;
671 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100672 if (s == *ret_type)
673 {
674 emsg(_(e_missing_return_type));
675 return NULL;
676 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100677 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100678 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100679 s = skipwhite(s);
680 if (*s != '=')
681 return NULL;
682 ++s;
683 }
684 if (*s != '>')
685 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100686 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
687 || !IS_WHITE_OR_NUL(s[1])))
688 {
689 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100690 semsg(_(e_white_space_required_before_and_after_str_at_str),
691 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100692 return NULL;
693 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100694 return skipwhite(s + 1);
695}
696
697/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100698 * Check if "*cmd" points to a function command and if so advance "*cmd" and
699 * return TRUE.
700 * Otherwise return FALSE;
701 * Do not consider "function(" to be a command.
702 */
703 static int
704is_function_cmd(char_u **cmd)
705{
706 char_u *p = *cmd;
707
708 if (checkforcmd(&p, "function", 2))
709 {
710 if (*p == '(')
711 return FALSE;
712 *cmd = p;
713 return TRUE;
714 }
715 return FALSE;
716}
717
718/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200719 * Called when defining a function: The context may be needed for script
720 * variables declared in a block that is visible now but not when the function
721 * is compiled or called later.
722 */
723 static void
724function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
725{
726 if (cstack != NULL && cstack->cs_idx >= 0)
727 {
728 int count = cstack->cs_idx + 1;
729 int i;
730
731 fp->uf_block_ids = ALLOC_MULT(int, count);
732 if (fp->uf_block_ids != NULL)
733 {
734 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
735 sizeof(int) * count);
736 fp->uf_block_depth = count;
737 }
738
739 // Set flag in each block to indicate a function was defined. This
740 // is used to keep the variable when leaving the block, see
741 // hide_script_var().
742 for (i = 0; i <= cstack->cs_idx; ++i)
743 cstack->cs_flags[i] |= CSF_FUNC_DEF;
744 }
745}
746
747/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100748 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200749 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100750 * "newlines" must already have been initialized.
751 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
752 */
753 static int
754get_function_body(
755 exarg_T *eap,
756 garray_T *newlines,
757 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000758 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100759{
760 linenr_T sourcing_lnum_top = SOURCING_LNUM;
761 linenr_T sourcing_lnum_off;
762 int saved_wait_return = need_wait_return;
763 char_u *line_arg = line_arg_in;
764 int vim9_function = eap->cmdidx == CMD_def
765 || eap->cmdidx == CMD_block;
766#define MAX_FUNC_NESTING 50
767 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200768 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100769 int nesting = 0;
770 getline_opt_T getline_options;
771 int indent = 2;
772 char_u *skip_until = NULL;
773 int ret = FAIL;
774 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200775 int heredoc_concat_len = 0;
776 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100777 char_u *heredoc_trimmed = NULL;
778
Bram Moolenaar20677332021-06-06 17:02:53 +0200779 ga_init2(&heredoc_ga, 1, 500);
780
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100781 // Detect having skipped over comment lines to find the return
782 // type. Add NULL lines to keep the line count correct.
783 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
784 if (SOURCING_LNUM < sourcing_lnum_off)
785 {
786 sourcing_lnum_off -= SOURCING_LNUM;
787 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
788 goto theend;
789 while (sourcing_lnum_off-- > 0)
790 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
791 }
792
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200793 nesting_def[0] = vim9_function;
794 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100795 getline_options = vim9_function
796 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
797 for (;;)
798 {
799 char_u *theline;
800 char_u *p;
801 char_u *arg;
802
803 if (KeyTyped)
804 {
805 msg_scroll = TRUE;
806 saved_wait_return = FALSE;
807 }
808 need_wait_return = FALSE;
809
810 if (line_arg != NULL)
811 {
812 // Use eap->arg, split up in parts by line breaks.
813 theline = line_arg;
814 p = vim_strchr(theline, '\n');
815 if (p == NULL)
816 line_arg += STRLEN(line_arg);
817 else
818 {
819 *p = NUL;
820 line_arg = p + 1;
821 }
822 }
823 else
824 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000825 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100826 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100827 }
828 if (KeyTyped)
829 lines_left = Rows - 1;
830 if (theline == NULL)
831 {
832 // Use the start of the function for the line number.
833 SOURCING_LNUM = sourcing_lnum_top;
834 if (skip_until != NULL)
835 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200836 else if (nesting_inline[nesting])
837 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100838 else if (eap->cmdidx == CMD_def)
839 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100840 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000841 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100842 goto theend;
843 }
844
845 // Detect line continuation: SOURCING_LNUM increased more than one.
846 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
847 if (SOURCING_LNUM < sourcing_lnum_off)
848 sourcing_lnum_off -= SOURCING_LNUM;
849 else
850 sourcing_lnum_off = 0;
851
852 if (skip_until != NULL)
853 {
854 // Don't check for ":endfunc"/":enddef" between
855 // * ":append" and "."
856 // * ":python <<EOF" and "EOF"
857 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
858 if (heredoc_trimmed == NULL
859 || (is_heredoc && skipwhite(theline) == theline)
860 || STRNCMP(theline, heredoc_trimmed,
861 STRLEN(heredoc_trimmed)) == 0)
862 {
863 if (heredoc_trimmed == NULL)
864 p = theline;
865 else if (is_heredoc)
866 p = skipwhite(theline) == theline
867 ? theline : theline + STRLEN(heredoc_trimmed);
868 else
869 p = theline + STRLEN(heredoc_trimmed);
870 if (STRCMP(p, skip_until) == 0)
871 {
872 VIM_CLEAR(skip_until);
873 VIM_CLEAR(heredoc_trimmed);
874 getline_options = vim9_function
875 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
876 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200877
878 if (heredoc_concat_len > 0)
879 {
880 // Replace the starting line with all the concatenated
881 // lines.
882 ga_concat(&heredoc_ga, theline);
883 vim_free(((char_u **)(newlines->ga_data))[
884 heredoc_concat_len - 1]);
885 ((char_u **)(newlines->ga_data))[
886 heredoc_concat_len - 1] = heredoc_ga.ga_data;
887 ga_init(&heredoc_ga);
888 heredoc_concat_len = 0;
889 theline += STRLEN(theline); // skip the "EOF"
890 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100891 }
892 }
893 }
894 else
895 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200896 int c;
897 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +0000898 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100899
900 // skip ':' and blanks
901 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
902 ;
903
904 // Check for "endfunction", "enddef" or "}".
905 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +0000906 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200907 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100908 ? *p == '}'
909 : (checkforcmd(&p, nesting_def[nesting]
910 ? "enddef" : "endfunction", 4)
911 && *p != ':'))
912 {
Bram Moolenaarb2175222022-03-05 20:24:41 +0000913 if (!nesting_inline[nesting] && nesting_def[nesting]
914 && p < cmd + 6)
915 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100916 if (nesting-- == 0)
917 {
918 char_u *nextcmd = NULL;
919
920 if (*p == '|' || *p == '}')
921 nextcmd = p + 1;
922 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
923 nextcmd = line_arg;
924 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100925 && (vim9_function || p_verbose > 0))
926 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200927 SOURCING_LNUM = sourcing_lnum_top
928 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100929 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000930 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100931 else
932 give_warning2((char_u *)
933 _("W22: Text found after :endfunction: %s"),
934 p, TRUE);
935 }
936 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100937 {
938 // Another command follows. If the line came from "eap"
939 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000940 // change "eap->cmdlinep" to point to the last fetched
941 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100942 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000943 if (lines_to_free->ga_len > 0
944 && *eap->cmdlinep !=
945 ((char_u **)lines_to_free->ga_data)
946 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100947 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000948 // *cmdlinep will be freed later, thus remove the
949 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100950 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000951 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
952 [lines_to_free->ga_len - 1];
953 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100954 }
955 }
956 break;
957 }
958 }
959
960 // Check for mismatched "endfunc" or "enddef".
961 // We don't check for "def" inside "func" thus we also can't check
962 // for "enddef".
963 // We continue to find the end of the function, although we might
964 // not find it.
965 else if (nesting_def[nesting])
966 {
967 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
968 emsg(_(e_mismatched_endfunction));
969 }
970 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
971 emsg(_(e_mismatched_enddef));
972
973 // Increase indent inside "if", "while", "for" and "try", decrease
974 // at "end".
975 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
976 indent -= 2;
977 else if (STRNCMP(p, "if", 2) == 0
978 || STRNCMP(p, "wh", 2) == 0
979 || STRNCMP(p, "for", 3) == 0
980 || STRNCMP(p, "try", 3) == 0)
981 indent += 2;
982
983 // Check for defining a function inside this function.
984 // Only recognize "def" inside "def", not inside "function",
985 // For backwards compatibility, see Test_function_python().
986 c = *p;
987 if (is_function_cmd(&p)
988 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
989 {
990 if (*p == '!')
991 p = skipwhite(p + 1);
992 p += eval_fname_script(p);
993 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
994 NULL, NULL));
995 if (*skipwhite(p) == '(')
996 {
997 if (nesting == MAX_FUNC_NESTING - 1)
998 emsg(_(e_function_nesting_too_deep));
999 else
1000 {
1001 ++nesting;
1002 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001003 nesting_inline[nesting] = FALSE;
1004 indent += 2;
1005 }
1006 }
1007 }
1008
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001009 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001010 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001011 // Not a comment line: check for nested inline function.
1012 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001013 while (end > p && VIM_ISWHITE(*end))
1014 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001015 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001016 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001017 int is_block;
1018
1019 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001020 --end;
1021 while (end > p && VIM_ISWHITE(*end))
1022 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001023 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1024 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001025 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001026 char_u *s = p;
1027
1028 // check for line starting with "au" for :autocmd or
1029 // "com" for :command, these can use a {} block
1030 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1031 || checkforcmd_noparen(&s, "command", 3);
1032 }
1033
1034 if (is_block)
1035 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001036 if (nesting == MAX_FUNC_NESTING - 1)
1037 emsg(_(e_function_nesting_too_deep));
1038 else
1039 {
1040 ++nesting;
1041 nesting_def[nesting] = TRUE;
1042 nesting_inline[nesting] = TRUE;
1043 indent += 2;
1044 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001045 }
1046 }
1047 }
1048
1049 // Check for ":append", ":change", ":insert". Not for :def.
1050 p = skip_range(p, FALSE, NULL);
1051 if (!vim9_function
1052 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1053 || (p[0] == 'c'
1054 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1055 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1056 && (STRNCMP(&p[3], "nge", 3) != 0
1057 || !ASCII_ISALPHA(p[6])))))))
1058 || (p[0] == 'i'
1059 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1060 && (!ASCII_ISALPHA(p[2])
1061 || (p[2] == 's'
1062 && (!ASCII_ISALPHA(p[3])
1063 || p[3] == 'e'))))))))
1064 skip_until = vim_strsave((char_u *)".");
1065
1066 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1067 arg = skipwhite(skiptowhite(p));
1068 if (arg[0] == '<' && arg[1] =='<'
1069 && ((p[0] == 'p' && p[1] == 'y'
1070 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1071 || ((p[2] == '3' || p[2] == 'x')
1072 && !ASCII_ISALPHA(p[3]))))
1073 || (p[0] == 'p' && p[1] == 'e'
1074 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1075 || (p[0] == 't' && p[1] == 'c'
1076 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1077 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1078 && !ASCII_ISALPHA(p[3]))
1079 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1080 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1081 || (p[0] == 'm' && p[1] == 'z'
1082 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1083 ))
1084 {
1085 // ":python <<" continues until a dot, like ":append"
1086 p = skipwhite(arg + 2);
1087 if (STRNCMP(p, "trim", 4) == 0)
1088 {
1089 // Ignore leading white space.
1090 p = skipwhite(p + 4);
1091 heredoc_trimmed = vim_strnsave(theline,
1092 skipwhite(theline) - theline);
1093 }
1094 if (*p == NUL)
1095 skip_until = vim_strsave((char_u *)".");
1096 else
1097 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1098 getline_options = GETLINE_NONE;
1099 is_heredoc = TRUE;
Bram Moolenaard881d152022-05-13 13:50:36 +01001100 if (eap->cmdidx == CMD_def && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001101 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001102 }
1103
Bram Moolenaard881d152022-05-13 13:50:36 +01001104 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001105 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001106 // Check for ":cmd v =<< [trim] EOF"
1107 // and ":cmd [a, b] =<< [trim] EOF"
1108 // and "lines =<< [trim] EOF" for Vim9
1109 // Where "cmd" can be "let", "var", "final" or "const".
1110 arg = skipwhite(skiptowhite(p));
1111 if (*arg == '[')
1112 arg = vim_strchr(arg, ']');
1113 if (arg != NULL)
1114 {
1115 int found = (eap->cmdidx == CMD_def && arg[0] == '='
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001116 && arg[1] == '<' && arg[2] =='<');
1117
Bram Moolenaard881d152022-05-13 13:50:36 +01001118 if (!found)
1119 // skip over the argument after "cmd"
1120 arg = skipwhite(skiptowhite(arg));
1121 if (found || (arg[0] == '=' && arg[1] == '<'
1122 && arg[2] =='<'
1123 && (checkforcmd(&p, "let", 2)
1124 || checkforcmd(&p, "var", 3)
1125 || checkforcmd(&p, "final", 5)
1126 || checkforcmd(&p, "const", 5))))
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001127 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001128 p = skipwhite(arg + 3);
1129 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001130 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001131 if (STRNCMP(p, "trim", 4) == 0)
1132 {
1133 // Ignore leading white space.
1134 p = skipwhite(p + 4);
1135 heredoc_trimmed = vim_strnsave(theline,
1136 skipwhite(theline) - theline);
1137 continue;
1138 }
1139 if (STRNCMP(p, "eval", 4) == 0)
1140 {
1141 // Ignore leading white space.
1142 p = skipwhite(p + 4);
1143 continue;
1144 }
1145 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001146 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001147 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1148 getline_options = GETLINE_NONE;
1149 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001150 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001151 }
1152 }
1153 }
1154
1155 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001156 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001157 goto theend;
1158
Bram Moolenaar20677332021-06-06 17:02:53 +02001159 if (heredoc_concat_len > 0)
1160 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001161 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001162 // to be used for the instruction later.
1163 ga_concat(&heredoc_ga, theline);
1164 ga_concat(&heredoc_ga, (char_u *)"\n");
1165 p = vim_strsave((char_u *)"");
1166 }
1167 else
1168 {
1169 // Copy the line to newly allocated memory. get_one_sourceline()
1170 // allocates 250 bytes per line, this saves 80% on average. The
1171 // cost is an extra alloc/free.
1172 p = vim_strsave(theline);
1173 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001174 if (p == NULL)
1175 goto theend;
1176 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1177
1178 // Add NULL lines for continuation lines, so that the line count is
1179 // equal to the index in the growarray.
1180 while (sourcing_lnum_off-- > 0)
1181 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1182
1183 // Check for end of eap->arg.
1184 if (line_arg != NULL && *line_arg == NUL)
1185 line_arg = NULL;
1186 }
1187
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001188 // Return OK when no error was detected.
1189 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001190 ret = OK;
1191
1192theend:
1193 vim_free(skip_until);
1194 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001195 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001196 need_wait_return |= saved_wait_return;
1197 return ret;
1198}
1199
1200/*
1201 * Handle the body of a lambda. *arg points to the "{", process statements
1202 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001203 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001204 * When successful "rettv" is set to a funcref.
1205 */
1206 static int
1207lambda_function_body(
1208 char_u **arg,
1209 typval_T *rettv,
1210 evalarg_T *evalarg,
1211 garray_T *newargs,
1212 garray_T *argtypes,
1213 int varargs,
1214 garray_T *default_args,
1215 char_u *ret_type)
1216{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001217 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001218 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001219 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001220 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001221 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001222 exarg_T eap;
1223 garray_T newlines;
1224 char_u *cmdline = NULL;
1225 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001226 partial_T *pt;
1227 char_u *name;
1228 int lnum_save = -1;
1229 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1230
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001231 *arg = skipwhite(*arg + 1);
1232 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001233 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001234 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001235 return FAIL;
1236 }
1237
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001238 CLEAR_FIELD(eap);
1239 eap.cmdidx = CMD_block;
1240 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001241 eap.cmdlinep = &cmdline;
1242 eap.skip = !evaluate;
1243 if (evalarg->eval_cctx != NULL)
1244 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1245 else
1246 {
1247 eap.getline = evalarg->eval_getline;
1248 eap.cookie = evalarg->eval_cookie;
1249 }
1250
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001251 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001252 if (get_function_body(&eap, &newlines, NULL,
1253 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001254 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001255
1256 // When inside a lambda must add the function lines to evalarg.eval_ga.
1257 evalarg->eval_break_count += newlines.ga_len;
1258 if (gap->ga_itemsize > 0)
1259 {
1260 int idx;
1261 char_u *last;
1262 size_t plen;
1263 char_u *pnl;
1264
1265 for (idx = 0; idx < newlines.ga_len; ++idx)
1266 {
1267 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1268
Bram Moolenaarecb66452021-05-18 15:09:18 +02001269 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001270 goto erret;
1271
1272 // Going to concatenate the lines after parsing. For an empty or
1273 // comment line use an empty string.
1274 // Insert NL characters at the start of each line, the string will
1275 // be split again later in .get_lambda_tv().
1276 if (*p == NUL || vim9_comment_start(p))
1277 p = (char_u *)"";
1278 plen = STRLEN(p);
1279 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1280 if (pnl != NULL)
1281 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001282 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1283 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001284 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001285 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001286 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001287 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001288 // more is following after the "}", which was skipped
1289 last = cmdline;
1290 else
1291 // nothing is following the "}"
1292 last = (char_u *)"}";
1293 plen = STRLEN(last);
1294 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1295 if (pnl != NULL)
1296 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001297 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1298 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001299 }
1300
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001301 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001302 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001303 garray_T *tfgap = &evalarg->eval_tofree_ga;
1304
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001305 // Something comes after the "}".
1306 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001307
1308 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001309 if (ga_grow(tfgap, 1) == OK)
1310 {
1311 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1312 evalarg->eval_using_cmdline = TRUE;
1313 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001314 }
1315 else
1316 *arg = (char_u *)"";
1317
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001318 if (!evaluate)
1319 {
1320 ret = OK;
1321 goto erret;
1322 }
1323
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001324 name = get_lambda_name();
1325 ufunc = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
1326 if (ufunc == NULL)
1327 goto erret;
1328 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001329 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001330 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001331 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001332 ufunc->uf_refcount = 1;
1333 ufunc->uf_args = *newargs;
1334 newargs->ga_data = NULL;
1335 ufunc->uf_def_args = *default_args;
1336 default_args->ga_data = NULL;
1337 ufunc->uf_func_type = &t_func_any;
1338
1339 // error messages are for the first function line
1340 lnum_save = SOURCING_LNUM;
1341 SOURCING_LNUM = sourcing_lnum_top;
1342
1343 // parse argument types
1344 if (parse_argument_types(ufunc, argtypes, varargs) == FAIL)
1345 {
1346 SOURCING_LNUM = lnum_save;
1347 goto erret;
1348 }
1349
1350 // parse the return type, if any
1351 if (parse_return_type(ufunc, ret_type) == FAIL)
1352 goto erret;
1353
1354 pt = ALLOC_CLEAR_ONE(partial_T);
1355 if (pt == NULL)
1356 goto erret;
1357 pt->pt_func = ufunc;
1358 pt->pt_refcount = 1;
1359
1360 ufunc->uf_lines = newlines;
1361 newlines.ga_data = NULL;
1362 if (sandbox)
1363 ufunc->uf_flags |= FC_SANDBOX;
1364 if (!ASCII_ISUPPER(*ufunc->uf_name))
1365 ufunc->uf_flags |= FC_VIM9;
1366 ufunc->uf_script_ctx = current_sctx;
1367 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1368 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1369 set_function_type(ufunc);
1370
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001371 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1372
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001373 rettv->vval.v_partial = pt;
1374 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001375 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001376 ret = OK;
1377
1378erret:
1379 if (lnum_save >= 0)
1380 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001381 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001382 if (newargs != NULL)
1383 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001384 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001385 if (ufunc != NULL)
1386 {
1387 func_clear(ufunc, TRUE);
1388 func_free(ufunc, TRUE);
1389 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001390 return ret;
1391}
1392
1393/*
1394 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001395 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001396 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001397 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1398 */
1399 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001400get_lambda_tv(
1401 char_u **arg,
1402 typval_T *rettv,
1403 int types_optional,
1404 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001405{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001406 int evaluate = evalarg != NULL
1407 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001408 garray_T newargs;
1409 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001410 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001411 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001412 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001413 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001414 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001415 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001416 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001417 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001418 char_u *s;
1419 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001420 int *old_eval_lavars = eval_lavars_used;
1421 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001422 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001423 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001424 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001425 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001426 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001427 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001428
Bram Moolenaar4525a572022-02-13 11:57:33 +00001429 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001430 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001431
1432 ga_init(&newargs);
1433 ga_init(&newlines);
1434
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001435 // First, check if this is really a lambda expression. "->" or "=>" must
1436 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001437 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001438 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001439 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001440 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001441 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001442 {
1443 if (types_optional)
1444 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +01001445 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001446 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001447
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001448 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001449 if (evaluate)
1450 pnewargs = &newargs;
1451 else
1452 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001453 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001454 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001455 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001456 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001457 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001458 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001459 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001460 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001461 {
1462 if (types_optional)
1463 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001464 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001465 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001466 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001467 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001468
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001469 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1470 if (ret_type != NULL)
1471 {
1472 ret_type = vim_strsave(ret_type);
1473 tofree2 = ret_type;
1474 }
1475
Bram Moolenaare38eab22019-12-05 21:50:01 +01001476 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001477 if (evaluate)
1478 eval_lavars_used = &eval_lavars;
1479
Bram Moolenaar65c44152020-12-24 15:14:01 +01001480 *arg = skipwhite_and_linebreak(*arg, evalarg);
1481
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001482 // Recognize "{" as the start of a function body.
1483 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001484 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001485 if (evalarg == NULL)
1486 // cannot happen?
1487 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001488 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001489 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1490 types_optional ? &argtypes : NULL, varargs,
1491 &default_args, ret_type) == FAIL)
1492 goto errret;
1493 goto theend;
1494 }
1495 if (default_args.ga_len > 0)
1496 {
1497 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001498 goto errret;
1499 }
1500
Bram Moolenaare38eab22019-12-05 21:50:01 +01001501 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001502 start = *arg;
1503 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001504 if (ret == FAIL)
1505 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001506
Bram Moolenaar65c44152020-12-24 15:14:01 +01001507 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001508 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001509 *arg = skipwhite_and_linebreak(*arg, evalarg);
1510 if (**arg != '}')
1511 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001512 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001513 goto errret;
1514 }
1515 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001516 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001517
1518 if (evaluate)
1519 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001520 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001521 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001522 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001523 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001524 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001525
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001526 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001527 if (fp == NULL)
1528 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001529 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001530 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001531 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001532 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001533
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001534 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001535 if (ga_grow(&newlines, 1) == FAIL)
1536 goto errret;
1537
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001538 // If there are line breaks, we need to split up the string.
1539 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001540 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001541 line_end = end;
1542
1543 // Add "return " before the expression (or the first line).
1544 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001545 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001546 if (p == NULL)
1547 goto errret;
1548 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1549 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001550 vim_strncpy(p + 7, start, line_end - start);
1551
1552 if (line_end != end)
1553 {
1554 // Add more lines, split by line breaks. Thus is used when a
1555 // lambda with { cmds } is encountered.
1556 while (*line_end == '\n')
1557 {
1558 if (ga_grow(&newlines, 1) == FAIL)
1559 goto errret;
1560 start = line_end + 1;
1561 line_end = vim_strchr(start, '\n');
1562 if (line_end == NULL)
1563 line_end = end;
1564 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1565 vim_strnsave(start, line_end - start);
1566 }
1567 }
1568
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001569 if (strstr((char *)p + 7, "a:") == NULL)
1570 // No a: variables are used for sure.
1571 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001572
1573 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001574 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001575 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001576 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001577 if (types_optional)
1578 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001579 if (parse_argument_types(fp, &argtypes,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001580 vim9script && varargs) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001581 goto errret;
1582 if (ret_type != NULL)
1583 {
1584 fp->uf_ret_type = parse_type(&ret_type,
1585 &fp->uf_type_list, TRUE);
1586 if (fp->uf_ret_type == NULL)
1587 goto errret;
1588 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001589 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001590 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001591 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001592
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001593 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001594 if (current_funccal != NULL && eval_lavars)
1595 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001596 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001597 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001598 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001599 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001600
1601#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001602 if (prof_def_func())
1603 func_do_profile(fp);
1604#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001605 if (sandbox)
1606 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001607 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001608 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001609 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001610 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001611 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001612 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001613 // Use the line number of the arguments.
1614 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001615
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001616 function_using_block_scopes(fp, evalarg->eval_cstack);
1617
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001618 pt->pt_func = fp;
1619 pt->pt_refcount = 1;
1620 rettv->vval.v_partial = pt;
1621 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001622
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001623 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001624 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001625
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001626theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001627 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001628 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001629 if (types_optional)
1630 ga_clear_strings(&argtypes);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001631
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001632 return OK;
1633
1634errret:
1635 ga_clear_strings(&newargs);
1636 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001637 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001638 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001639 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001640 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001641 if (fp != NULL)
1642 vim_free(fp->uf_arg_types);
1643 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001644 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001645 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001646 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001647 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001648 return FAIL;
1649}
1650
1651/*
1652 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1653 * name it contains, otherwise return "name".
1654 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1655 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001656 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1657 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001658 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001659 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001660 */
1661 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001662deref_func_name(
1663 char_u *name,
1664 int *lenp,
1665 partial_T **partialp,
1666 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001667 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001668 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001669 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001670{
1671 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001672 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001673 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001674 char_u *s = NULL;
1675 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001676 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001677
1678 if (partialp != NULL)
1679 *partialp = NULL;
1680
1681 cc = name[*lenp];
1682 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001683
Bram Moolenaar71f21932022-01-07 18:20:55 +00001684 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001685 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001686 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001687 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001688 tv = &v->di_tv;
1689 }
1690 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1691 {
1692 imported_T *import;
1693 char_u *p = name;
1694 int len = *lenp;
1695
1696 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001697 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001698 p = name + 2;
1699 len -= 2;
1700 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001701 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001702
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001703 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001704 if (import != NULL)
1705 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001706 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001707 if (new_function)
1708 semsg(_(e_redefining_imported_item_str), name);
1709 else
1710 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001711 name[len] = cc;
1712 *lenp = 0;
1713 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001714 }
1715 }
1716
1717 if (tv != NULL)
1718 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001719 if (found_var != NULL)
1720 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001721 if (tv->v_type == VAR_FUNC)
1722 {
1723 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001724 {
1725 *lenp = 0;
1726 return (char_u *)""; // just in case
1727 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001728 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001729 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001730 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001731
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001732 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001733 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001734 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001735
1736 if (pt == NULL)
1737 {
1738 *lenp = 0;
1739 return (char_u *)""; // just in case
1740 }
1741 if (partialp != NULL)
1742 *partialp = pt;
1743 s = partial_name(pt);
1744 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001745 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001746
1747 if (s != NULL)
1748 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001749 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001750 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001751 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001752
1753 if (sv != NULL)
1754 *type = sv->sv_type;
1755 }
1756 return s;
1757 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001758 }
1759
1760 return name;
1761}
1762
1763/*
1764 * Give an error message with a function name. Handle <SNR> things.
1765 * "ermsg" is to be passed without translation, use N_() instead of _().
1766 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001767 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001768emsg_funcname(char *ermsg, char_u *name)
1769{
Bram Moolenaar54598062022-01-13 12:05:09 +00001770 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001771
Bram Moolenaar54598062022-01-13 12:05:09 +00001772 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001773 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001774 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001775 if (p != name)
1776 vim_free(p);
1777}
1778
1779/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001780 * Get function arguments at "*arg" and advance it.
1781 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001782 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001783 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001784 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001785get_func_arguments(
1786 char_u **arg,
1787 evalarg_T *evalarg,
1788 int partial_argc,
1789 typval_T *argvars,
1790 int *argcount)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001791{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001792 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001793 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001794 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001795 int evaluate = evalarg == NULL
1796 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001797
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001798 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001799 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001800 // skip the '(' or ',' and possibly line breaks
1801 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1802
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001803 if (*argp == ')' || *argp == ',' || *argp == NUL)
1804 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001805 if (eval1(&argp, &argvars[*argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001806 {
1807 ret = FAIL;
1808 break;
1809 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001810 ++*argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001811 // The comma should come right after the argument, but this wasn't
1812 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001813 if (vim9script)
1814 {
1815 if (*argp != ',' && *skipwhite(argp) == ',')
1816 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001817 if (evaluate)
1818 semsg(_(e_no_white_space_allowed_before_str_str),
1819 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001820 ret = FAIL;
1821 break;
1822 }
1823 }
1824 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001825 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001826 if (*argp != ',')
1827 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001828 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1829 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001830 if (evaluate)
1831 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001832 ret = FAIL;
1833 break;
1834 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001835 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001836
Bram Moolenaare6b53242020-07-01 17:28:33 +02001837 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001838 if (*argp == ')')
1839 ++argp;
1840 else
1841 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001842 *arg = argp;
1843 return ret;
1844}
1845
1846/*
1847 * Call a function and put the result in "rettv".
1848 * Return OK or FAIL.
1849 */
1850 int
1851get_func_tv(
1852 char_u *name, // name of the function
1853 int len, // length of "name" or -1 to use strlen()
1854 typval_T *rettv,
1855 char_u **arg, // argument, pointing to the '('
1856 evalarg_T *evalarg, // for line continuation
1857 funcexe_T *funcexe) // various values
1858{
1859 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001860 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001861 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1862 int argcount = 0; // number of arguments found
1863 int vim9script = in_vim9script();
1864 int evaluate = evalarg == NULL
1865 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1866
1867 argp = *arg;
1868 ret = get_func_arguments(&argp, evalarg,
1869 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
1870 argvars, &argcount);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001871
1872 if (ret == OK)
1873 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001874 int i = 0;
1875 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001876
1877 if (get_vim_var_nr(VV_TESTING))
1878 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001879 // Prepare for calling test_garbagecollect_now(), need to know
1880 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001881 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001882 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001883 for (i = 0; i < argcount; ++i)
1884 if (ga_grow(&funcargs, 1) == OK)
1885 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1886 &argvars[i];
1887 }
1888
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001889 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00001890 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001891 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001892 // An error in a builtin function does not return FAIL, but we do
1893 // want to abort further processing if an error was given.
1894 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001895 clear_tv(rettv);
1896 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001897
1898 funcargs.ga_len -= i;
1899 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001900 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001901 {
1902 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001903 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001904 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001905 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001906 }
1907
1908 while (--argcount >= 0)
1909 clear_tv(&argvars[argcount]);
1910
Bram Moolenaar4525a572022-02-13 11:57:33 +00001911 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02001912 *arg = argp;
1913 else
1914 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001915 return ret;
1916}
1917
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001918/*
1919 * Return TRUE if "p" starts with "<SID>" or "s:".
1920 * Only works if eval_fname_script() returned non-zero for "p"!
1921 */
1922 static int
1923eval_fname_sid(char_u *p)
1924{
1925 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1926}
1927
1928/*
1929 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1930 * Change <SNR>123_name() to K_SNR 123_name().
1931 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001932 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001933 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001934 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001935fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
1936{
1937 int llen;
1938 char_u *fname;
1939 int i;
1940
1941 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001942 if (llen == 0)
1943 return name; // no prefix
1944
1945 fname_buf[0] = K_SPECIAL;
1946 fname_buf[1] = KS_EXTRA;
1947 fname_buf[2] = (int)KE_SNR;
1948 i = 3;
1949 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001950 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001951 if (current_sctx.sc_sid <= 0)
1952 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001953 else
1954 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001955 sprintf((char *)fname_buf + 3, "%ld_",
1956 (long)current_sctx.sc_sid);
1957 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001958 }
1959 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001960 if (i + STRLEN(name + llen) < FLEN_FIXED)
1961 {
1962 STRCPY(fname_buf + i, name + llen);
1963 fname = fname_buf;
1964 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001965 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001966 {
1967 fname = alloc(i + STRLEN(name + llen) + 1);
1968 if (fname == NULL)
1969 *error = FCERR_OTHER;
1970 else
1971 {
1972 *tofree = fname;
1973 mch_memmove(fname, fname_buf, (size_t)i);
1974 STRCPY(fname + i, name + llen);
1975 }
1976 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001977 return fname;
1978}
1979
1980/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001981 * Concatenate the script ID and function name into "<SNR>99_name".
1982 * "buffer" must have size MAX_FUNC_NAME_LEN.
1983 */
1984 void
1985func_name_with_sid(char_u *name, int sid, char_u *buffer)
1986{
1987 // A script-local function is stored as "<SNR>99_name".
1988 buffer[0] = K_SPECIAL;
1989 buffer[1] = KS_EXTRA;
1990 buffer[2] = (int)KE_SNR;
1991 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
1992 (long)sid, name);
1993}
1994
1995/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001996 * Find a function "name" in script "sid".
1997 */
1998 static ufunc_T *
1999find_func_with_sid(char_u *name, int sid)
2000{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002001 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002002 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003
Bram Moolenaarb8822442022-01-11 15:24:05 +00002004 if (!SCRIPT_ID_VALID(sid))
2005 return NULL; // not in a script
2006
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002007 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 hi = hash_find(&func_hashtab, buffer);
2009 if (!HASHITEM_EMPTY(hi))
2010 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002011 return NULL;
2012}
2013
2014/*
2015 * Find a function "name" in script "sid" prefixing the autoload prefix.
2016 */
2017 static ufunc_T *
2018find_func_with_prefix(char_u *name, int sid)
2019{
2020 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002021 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002022 scriptitem_T *si;
2023
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002024 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002025 return NULL; // already has the prefix
2026 if (!SCRIPT_ID_VALID(sid))
2027 return NULL; // not in a script
2028 si = SCRIPT_ITEM(sid);
2029 if (si->sn_autoload_prefix != NULL)
2030 {
2031 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2032 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002033 char_u *namep;
2034
2035 // skip a "<SNR>99_" prefix
2036 namep = untrans_function_name(name);
2037 if (namep == NULL)
2038 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002039
2040 // An exported function in an autoload script is stored as
2041 // "dir#path#name".
2042 if (len < sizeof(buffer))
2043 auto_name = buffer;
2044 else
2045 auto_name = alloc(len);
2046 if (auto_name != NULL)
2047 {
2048 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002049 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002050 hi = hash_find(&func_hashtab, auto_name);
2051 if (auto_name != buffer)
2052 vim_free(auto_name);
2053 if (!HASHITEM_EMPTY(hi))
2054 return HI2UF(hi);
2055 }
2056 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057
2058 return NULL;
2059}
2060
2061/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002062 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002063 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2064 * functions.
2065 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002066 * Return NULL for unknown function.
2067 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002068 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002069find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002070{
2071 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002074 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002076 // Find script-local function before global one.
2077 if (in_vim9script() && eval_isnamec1(*name)
2078 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002080 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2081 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002082 if (func != NULL)
2083 return func;
2084 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002085 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2086 {
2087 char_u *p = name + 5;
2088 long sid;
2089
2090 // printable "<SNR>123_Name" form
2091 sid = getdigits(&p);
2092 if (*p == '_')
2093 {
2094 func = find_func_with_sid(p + 1, (int)sid);
2095 if (func != NULL)
2096 return func;
2097 }
2098 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002100
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002101 if ((flags & FFED_NO_GLOBAL) == 0)
2102 {
2103 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002104 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002105 if (!HASHITEM_EMPTY(hi))
2106 return HI2UF(hi);
2107 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108
Bram Moolenaarb8822442022-01-11 15:24:05 +00002109 // Find autoload function if this is an autoload script.
2110 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2111 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112}
2113
2114/*
2115 * Find a function by name, return pointer to it in ufuncs.
2116 * "cctx" is passed in a :def function to find imported functions.
2117 * Return NULL for unknown or dead function.
2118 */
2119 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002120find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002122 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123
2124 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2125 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002126 return NULL;
2127}
2128
2129/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002130 * Return TRUE if "ufunc" is a global function.
2131 */
2132 int
2133func_is_global(ufunc_T *ufunc)
2134{
2135 return ufunc->uf_name[0] != K_SPECIAL;
2136}
2137
2138/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002139 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2140 */
2141 int
2142func_requires_g_prefix(ufunc_T *ufunc)
2143{
2144 return ufunc->uf_name[0] != K_SPECIAL
2145 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002146 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
2147 && !isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002148}
2149
2150/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002151 * Copy the function name of "fp" to buffer "buf".
2152 * "buf" must be able to hold the function name plus three bytes.
2153 * Takes care of script-local function names.
2154 */
2155 static void
2156cat_func_name(char_u *buf, ufunc_T *fp)
2157{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002158 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002159 {
2160 STRCPY(buf, "<SNR>");
2161 STRCAT(buf, fp->uf_name + 3);
2162 }
2163 else
2164 STRCPY(buf, fp->uf_name);
2165}
2166
2167/*
2168 * Add a number variable "name" to dict "dp" with value "nr".
2169 */
2170 static void
2171add_nr_var(
2172 dict_T *dp,
2173 dictitem_T *v,
2174 char *name,
2175 varnumber_T nr)
2176{
2177 STRCPY(v->di_key, name);
2178 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002179 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002180 v->di_tv.v_type = VAR_NUMBER;
2181 v->di_tv.v_lock = VAR_FIXED;
2182 v->di_tv.vval.v_number = nr;
2183}
2184
2185/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002186 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002187 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002188 static void
2189free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002190{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002191 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002192
Bram Moolenaarca16c602022-09-06 18:57:08 +01002193 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002194 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002195 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002196
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002197 // When garbage collecting a funccall_T may be freed before the
2198 // function that references it, clear its uf_scoped field.
2199 // The function may have been redefined and point to another
2200 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002201 if (fp != NULL && fp->uf_scoped == fc)
2202 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002203 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002204 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002205
Bram Moolenaarca16c602022-09-06 18:57:08 +01002206 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002207 vim_free(fc);
2208}
2209
2210/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002211 * Free "fc" and what it contains.
2212 * Can be called only when "fc" is kept beyond the period of it called,
2213 * i.e. after cleanup_function_call(fc).
2214 */
2215 static void
2216free_funccal_contents(funccall_T *fc)
2217{
2218 listitem_T *li;
2219
2220 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002221 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002222
2223 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002224 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002225
2226 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002227 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002228 clear_tv(&li->li_tv);
2229
2230 free_funccal(fc);
2231}
2232
2233/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002234 * Handle the last part of returning from a function: free the local hashtable.
2235 * Unless it is still in use by a closure.
2236 */
2237 static void
2238cleanup_function_call(funccall_T *fc)
2239{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002240 int may_free_fc = fc->fc_refcount <= 0;
2241 int free_fc = TRUE;
2242
Bram Moolenaarca16c602022-09-06 18:57:08 +01002243 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002244
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002245 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002246 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2247 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002248 else
2249 free_fc = FALSE;
2250
2251 // If the a:000 list and the l: and a: dicts are not referenced and
2252 // there is no closure using it, we can free the funccall_T and what's
2253 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002254 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2255 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002256 else
2257 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002258 int todo;
2259 hashitem_T *hi;
2260 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002261
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002262 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002263
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002264 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002265 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
2266 for (hi = fc->fc_l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002267 {
2268 if (!HASHITEM_EMPTY(hi))
2269 {
2270 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002271 di = HI2DI(hi);
2272 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002273 }
2274 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002275 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002276
Bram Moolenaarca16c602022-09-06 18:57:08 +01002277 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2278 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002279 else
2280 {
2281 listitem_T *li;
2282
2283 free_fc = FALSE;
2284
2285 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002286 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002287 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002288 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002289
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002290 if (free_fc)
2291 free_funccal(fc);
2292 else
2293 {
2294 static int made_copy = 0;
2295
2296 // "fc" is still in use. This can happen when returning "a:000",
2297 // assigning "l:" to a global variable or defining a closure.
2298 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002299 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002300 previous_funccal = fc;
2301
2302 if (want_garbage_collect)
2303 // If garbage collector is ready, clear count.
2304 made_copy = 0;
2305 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002306 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002307 // We have made a lot of copies, worth 4 Mbyte. This can happen
2308 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002309 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002310 // too much memory.
2311 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002312 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002313 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002314 }
2315}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002316
2317/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002318 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2319 */
2320 static int
2321numbered_function(char_u *name)
2322{
2323 return isdigit(*name)
2324 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2325}
2326
2327/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002328 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002329 * 1. ordinary names, function defined with :function or :def;
2330 * can start with "<SNR>123_" literally or with K_SPECIAL.
2331 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002332 * For the first we only count the name stored in func_hashtab as a reference,
2333 * using function() does not count as a reference, because the function is
2334 * looked up by name.
2335 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002336 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002337func_name_refcount(char_u *name)
2338{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002339 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002340}
2341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342/*
2343 * Unreference "fc": decrement the reference count and free it when it
2344 * becomes zero. "fp" is detached from "fc".
2345 * When "force" is TRUE we are exiting.
2346 */
2347 static void
2348funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2349{
2350 funccall_T **pfc;
2351 int i;
2352
2353 if (fc == NULL)
2354 return;
2355
2356 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002357 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2358 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2359 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2360 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 {
2362 if (fc == *pfc)
2363 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002364 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 free_funccal_contents(fc);
2366 return;
2367 }
2368 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002369 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2370 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2371 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372}
2373
2374/*
2375 * Remove the function from the function hashtable. If the function was
2376 * deleted while it still has references this was already done.
2377 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2378 */
2379 static int
2380func_remove(ufunc_T *fp)
2381{
2382 hashitem_T *hi;
2383
2384 // Return if it was already virtually deleted.
2385 if (fp->uf_flags & FC_DEAD)
2386 return FALSE;
2387
2388 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
2389 if (!HASHITEM_EMPTY(hi))
2390 {
2391 // When there is a def-function index do not actually remove the
2392 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002393 // Do remove it when it's a copy.
2394 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaarc970e422021-03-17 15:03:04 +01002395 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002397 return FALSE;
2398 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002399 hash_remove(&func_hashtab, hi, "remove function");
Bram Moolenaarc970e422021-03-17 15:03:04 +01002400 fp->uf_flags |= FC_DELETED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 return TRUE;
2402 }
2403 return FALSE;
2404}
2405
2406 static void
2407func_clear_items(ufunc_T *fp)
2408{
2409 ga_clear_strings(&(fp->uf_args));
2410 ga_clear_strings(&(fp->uf_def_args));
2411 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002413 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002414 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002415 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002416
2417 // Increment the refcount of this function to avoid it being freed
2418 // recursively when the partial is freed.
2419 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002420 partial_unref(fp->uf_partial);
2421 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002422 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002423
2424#ifdef FEAT_LUA
2425 if (fp->uf_cb_free != NULL)
2426 {
2427 fp->uf_cb_free(fp->uf_cb_state);
2428 fp->uf_cb_free = NULL;
2429 }
2430
2431 fp->uf_cb_state = NULL;
2432 fp->uf_cb = NULL;
2433#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434#ifdef FEAT_PROFILE
2435 VIM_CLEAR(fp->uf_tml_count);
2436 VIM_CLEAR(fp->uf_tml_total);
2437 VIM_CLEAR(fp->uf_tml_self);
2438#endif
2439}
2440
2441/*
2442 * Free all things that a function contains. Does not free the function
2443 * itself, use func_free() for that.
2444 * When "force" is TRUE we are exiting.
2445 */
2446 static void
2447func_clear(ufunc_T *fp, int force)
2448{
2449 if (fp->uf_cleared)
2450 return;
2451 fp->uf_cleared = TRUE;
2452
2453 // clear this function
2454 func_clear_items(fp);
2455 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002456 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457}
2458
2459/*
2460 * Free a function and remove it from the list of functions. Does not free
2461 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002462 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002463 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002465 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002466func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467{
2468 // Only remove it when not done already, otherwise we would remove a newer
2469 // version of the function with the same name.
2470 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2471 func_remove(fp);
2472
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002473 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002474 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002475 if (fp->uf_dfunc_idx > 0)
2476 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002477 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002479 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002480 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002481 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482}
2483
2484/*
2485 * Free all things that a function contains and free the function itself.
2486 * When "force" is TRUE we are exiting.
2487 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002488 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489func_clear_free(ufunc_T *fp, int force)
2490{
2491 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002492 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2493 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002494 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002495 else
2496 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497}
2498
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002499/*
2500 * Copy already defined function "lambda" to a new function with name "global".
2501 * This is for when a compiled function defines a global function.
2502 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002503 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002504copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002505 char_u *lambda,
2506 char_u *global,
2507 loopvarinfo_T *loopvarinfo,
2508 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002509{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002510 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002511 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002512
2513 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002514 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002515 semsg(_(e_lambda_function_not_found_str), lambda);
2516 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002517 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002518
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002519 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002520 if (fp != NULL)
2521 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002522 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002523 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002524 return FAIL;
2525 }
2526
2527 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
2528 if (fp == NULL)
2529 return FAIL;
2530
2531 fp->uf_varargs = ufunc->uf_varargs;
2532 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2533 fp->uf_def_status = ufunc->uf_def_status;
2534 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2535 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2536 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2537 == FAIL
2538 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2539 goto failed;
2540
2541 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
2542 : vim_strsave(ufunc->uf_name_exp);
2543 if (ufunc->uf_arg_types != NULL)
2544 {
2545 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2546 if (fp->uf_arg_types == NULL)
2547 goto failed;
2548 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2549 sizeof(type_T *) * fp->uf_args.ga_len);
2550 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002551 if (ufunc->uf_va_name != NULL)
2552 {
2553 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2554 if (fp->uf_va_name == NULL)
2555 goto failed;
2556 }
2557 fp->uf_ret_type = ufunc->uf_ret_type;
2558
2559 fp->uf_refcount = 1;
2560 STRCPY(fp->uf_name, global);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002561 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002562
2563 // the referenced dfunc_T is now used one more time
2564 link_def_function(fp);
2565
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002566 // Create a partial to store the context of the function where it was
2567 // instantiated. Only needs to be done once. Do this on the original
2568 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002569 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2570 {
2571 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2572
2573 if (pt == NULL)
2574 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002575 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002576 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002577 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002578 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002579 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002580 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002581 }
2582
2583 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002584
2585failed:
2586 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002587 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002588}
2589
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002590static int funcdepth = 0;
2591
2592/*
2593 * Increment the function call depth count.
2594 * Return FAIL when going over 'maxfuncdepth'.
2595 * Otherwise return OK, must call funcdepth_decrement() later!
2596 */
2597 int
2598funcdepth_increment(void)
2599{
2600 if (funcdepth >= p_mfd)
2601 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002602 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002603 return FAIL;
2604 }
2605 ++funcdepth;
2606 return OK;
2607}
2608
2609 void
2610funcdepth_decrement(void)
2611{
2612 --funcdepth;
2613}
2614
2615/*
2616 * Get the current function call depth.
2617 */
2618 int
2619funcdepth_get(void)
2620{
2621 return funcdepth;
2622}
2623
2624/*
2625 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002626 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002627 * times as funcdepth_increment().
2628 */
2629 void
2630funcdepth_restore(int depth)
2631{
2632 funcdepth = depth;
2633}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002634
2635/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002636 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2637 * "rettv".
2638 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2639 * Returns NULL when allocation fails.
2640 */
2641 funccall_T *
2642create_funccal(ufunc_T *fp, typval_T *rettv)
2643{
2644 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2645
2646 if (fc == NULL)
2647 return NULL;
2648 fc->fc_caller = current_funccal;
2649 current_funccal = fc;
2650 fc->fc_func = fp;
2651 func_ptr_ref(fp);
2652 fc->fc_rettv = rettv;
2653 return fc;
2654}
2655
2656/*
2657 * To be called when returning from a compiled function; restores
2658 * current_funccal.
2659 */
2660 void
2661remove_funccal()
2662{
2663 funccall_T *fc = current_funccal;
2664
2665 current_funccal = fc->fc_caller;
2666 free_funccal(fc);
2667}
2668
2669/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002670 * Call a user function.
2671 */
2672 static void
2673call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002674 ufunc_T *fp, // pointer to function
2675 int argcount, // nr of args
2676 typval_T *argvars, // arguments
2677 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002678 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002679 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002680{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002681 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002682 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002683 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002684 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002685 funccall_T *fc;
2686 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002687 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002688 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002689 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002690 int i;
2691 int ai;
2692 int islambda = FALSE;
2693 char_u numbuf[NUMBUFLEN];
2694 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002695 typval_T *tv_to_free[MAX_FUNC_ARGS];
2696 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002697#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002698 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002699#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002700 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002701
Bram Moolenaarb2049902021-01-24 12:53:53 +01002702#ifdef FEAT_PROFILE
2703 CLEAR_FIELD(profile_info);
2704#endif
2705
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002706 // If depth of calling is getting too high, don't execute the function.
2707 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002708 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002709 rettv->v_type = VAR_NUMBER;
2710 rettv->vval.v_number = -1;
2711 return;
2712 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002713
Bram Moolenaare38eab22019-12-05 21:50:01 +01002714 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002715
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002716 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002717 if (fc == NULL)
2718 return;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002719 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002720 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002721 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2722 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002723 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002724 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002725
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002726 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002728#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002729 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002730#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002731 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002732#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002733 if (do_profiling == PROF_YES)
2734 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002735#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002736 sticky_cmdmod_flags = 0;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002737 call_def_function(fp, argcount, argvars, 0,
2738 funcexe->fe_partial, funcexe->fe_object, fc, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002739 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002740#ifdef FEAT_PROFILE
2741 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002742 || (caller != NULL && caller->uf_profiling)))
2743 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002744#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002745 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002746 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747 return;
2748 }
2749
Bram Moolenaar38453522021-11-28 22:00:12 +00002750 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002751
2752 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002753 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2754 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2755 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002756 */
2757 /*
2758 * Init l: variables.
2759 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002760 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002761 if (selfdict != NULL)
2762 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002763 // Set l:self to "selfdict". Use "name" to avoid a warning from
2764 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002765 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002766 name = v->di_key;
2767 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002768 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002769 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002770 v->di_tv.v_type = VAR_DICT;
2771 v->di_tv.v_lock = 0;
2772 v->di_tv.vval.v_dict = selfdict;
2773 ++selfdict->dv_refcount;
2774 }
2775
2776 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002777 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002778 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002779 * Set a:000 to a list with room for the "..." arguments.
2780 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002781 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002782 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002783 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002784 (varnumber_T)(argcount >= fp->uf_args.ga_len
2785 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002786 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002787 if ((fp->uf_flags & FC_NOARGS) == 0)
2788 {
2789 // Use "name" to avoid a warning from some compiler that checks the
2790 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002791 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002792 name = v->di_key;
2793 STRCPY(name, "000");
2794 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002795 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002796 v->di_tv.v_type = VAR_LIST;
2797 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002798 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002799 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002800 CLEAR_FIELD(fc->fc_l_varlist);
2801 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2802 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002803
2804 /*
2805 * Set a:firstline to "firstline" and a:lastline to "lastline".
2806 * Set a:name to named arguments.
2807 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002808 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002809 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002810 if ((fp->uf_flags & FC_NOARGS) == 0)
2811 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002812 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2813 "firstline", (varnumber_T)funcexe->fe_firstline);
2814 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2815 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002816 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002817 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002818 {
2819 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002820 typval_T def_rettv;
2821 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002822
2823 ai = i - fp->uf_args.ga_len;
2824 if (ai < 0)
2825 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002826 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002827 name = FUNCARG(fp, i);
2828 if (islambda)
2829 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002830
2831 // evaluate named argument default expression
2832 isdefault = ai + fp->uf_def_args.ga_len >= 0
2833 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2834 && argvars[i].vval.v_number == VVAL_NONE));
2835 if (isdefault)
2836 {
2837 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002838
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002839 def_rettv.v_type = VAR_NUMBER;
2840 def_rettv.vval.v_number = -1;
2841
2842 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2843 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002844 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002845 {
2846 default_arg_err = 1;
2847 break;
2848 }
2849 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002850 }
2851 else
2852 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002853 if ((fp->uf_flags & FC_NOARGS) != 0)
2854 // Bail out if no a: arguments used (in lambda).
2855 break;
2856
Bram Moolenaare38eab22019-12-05 21:50:01 +01002857 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002858 sprintf((char *)numbuf, "%d", ai + 1);
2859 name = numbuf;
2860 }
2861 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2862 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002863 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002864 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002865 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002866 }
2867 else
2868 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002869 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002870 if (v == NULL)
2871 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002872 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002873 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002874
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002875 // Note: the values are copied directly to avoid alloc/free.
2876 // "argvars" must have VAR_FIXED for v_lock.
2877 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002878 v->di_tv.v_lock = VAR_FIXED;
2879
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002880 if (isdefault)
2881 // Need to free this later, no matter where it's stored.
2882 tv_to_free[tv_to_free_len++] = &v->di_tv;
2883
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002884 if (addlocal)
2885 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002886 // Named arguments should be accessed without the "a:" prefix in
2887 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002888 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002889 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002890 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002891 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002892 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002893
2894 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2895 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002896 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002897
2898 li->li_tv = argvars[i];
2899 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002900 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002901 }
2902 }
2903
Bram Moolenaare38eab22019-12-05 21:50:01 +01002904 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002905 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002906
2907 if (fp->uf_flags & FC_SANDBOX)
2908 {
2909 using_sandbox = TRUE;
2910 ++sandbox;
2911 }
2912
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002913 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002914 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002915 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002916 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002917 ++no_wait_return;
2918 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002919
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002920 smsg(_("calling %s"), SOURCING_NAME);
2921 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002922 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002923 char_u buf[MSG_BUF_LEN];
2924 char_u numbuf2[NUMBUFLEN];
2925 char_u *tofree;
2926 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002927
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002928 msg_puts("(");
2929 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002930 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002931 if (i > 0)
2932 msg_puts(", ");
2933 if (argvars[i].v_type == VAR_NUMBER)
2934 msg_outnum((long)argvars[i].vval.v_number);
2935 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002936 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002937 // Do not want errors such as E724 here.
2938 ++emsg_off;
2939 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2940 --emsg_off;
2941 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002942 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002943 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002944 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002945 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2946 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002947 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002948 msg_puts((char *)s);
2949 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002950 }
2951 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002952 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002953 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002954 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002955 msg_puts("\n"); // don't overwrite this either
2956
2957 verbose_leave_scroll();
2958 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002959 }
2960#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002961 if (do_profiling == PROF_YES)
2962 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01002963 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964#endif
2965
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002966 // "legacy" does not apply to commands in the function
2967 sticky_cmdmod_flags = 0;
2968
Bram Moolenaar98aff652022-09-06 21:02:35 +01002969 // If called from a compiled :def function the execution context must be
2970 // hidden, any deferred functions need to be added to the function being
2971 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00002972 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01002973
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002974 save_current_sctx = current_sctx;
2975 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002976 save_did_emsg = did_emsg;
2977 did_emsg = FALSE;
2978
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002979 if (default_arg_err && (fp->uf_flags & FC_ABORT))
2980 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002981 else if (islambda)
2982 {
2983 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
2984
2985 // A Lambda always has the command "return {expr}". It is much faster
2986 // to evaluate {expr} directly.
2987 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002988 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002989 --ex_nesting_level;
2990 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002991 else
2992 // call do_cmdline() to execute the lines
2993 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
2995
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002996 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01002997 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002998
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002999 --RedrawingDisabled;
3000
Bram Moolenaare38eab22019-12-05 21:50:01 +01003001 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003002 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3003 {
3004 clear_tv(rettv);
3005 rettv->v_type = VAR_NUMBER;
3006 rettv->vval.v_number = -1;
3007 }
3008
3009#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003010 if (do_profiling == PROF_YES)
3011 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003012 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003013
3014 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3015 profile_may_end_func(&profile_info, fp, caller);
3016 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003017#endif
3018
Bram Moolenaare38eab22019-12-05 21:50:01 +01003019 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003020 if (p_verbose >= 12)
3021 {
3022 ++no_wait_return;
3023 verbose_enter_scroll();
3024
3025 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003026 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003027 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003028 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003029 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003030 else
3031 {
3032 char_u buf[MSG_BUF_LEN];
3033 char_u numbuf2[NUMBUFLEN];
3034 char_u *tofree;
3035 char_u *s;
3036
Bram Moolenaare38eab22019-12-05 21:50:01 +01003037 // The value may be very long. Skip the middle part, so that we
3038 // have some idea how it starts and ends. smsg() would always
3039 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003040 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003041 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003042 --emsg_off;
3043 if (s != NULL)
3044 {
3045 if (vim_strsize(s) > MSG_BUF_CLEN)
3046 {
3047 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3048 s = buf;
3049 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003050 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003051 vim_free(tofree);
3052 }
3053 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003054 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003055
3056 verbose_leave_scroll();
3057 --no_wait_return;
3058 }
3059
Bram Moolenaare31ee862020-01-07 20:59:34 +01003060 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003061 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003062 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003063 restore_current_ectx(save_current_ectx);
3064
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003065#ifdef FEAT_PROFILE
3066 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003067 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003068#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003069 if (using_sandbox)
3070 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003071 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003072
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003073 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003074 {
3075 ++no_wait_return;
3076 verbose_enter_scroll();
3077
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003078 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003079 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003080
3081 verbose_leave_scroll();
3082 --no_wait_return;
3083 }
3084
3085 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003086 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003087 for (i = 0; i < tv_to_free_len; ++i)
3088 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003089 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003090}
3091
3092/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003093 * Check the argument count for user function "fp".
3094 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3095 */
3096 int
3097check_user_func_argcount(ufunc_T *fp, int argcount)
3098{
3099 int regular_args = fp->uf_args.ga_len;
3100
3101 if (argcount < regular_args - fp->uf_def_args.ga_len)
3102 return FCERR_TOOFEW;
3103 else if (!has_varargs(fp) && argcount > regular_args)
3104 return FCERR_TOOMANY;
3105 return FCERR_UNKNOWN;
3106}
3107
3108/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003110 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 int
3112call_user_func_check(
3113 ufunc_T *fp,
3114 int argcount,
3115 typval_T *argvars,
3116 typval_T *rettv,
3117 funcexe_T *funcexe,
3118 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003119{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003121
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003122#ifdef FEAT_LUA
3123 if (fp->uf_flags & FC_CFUNC)
3124 {
3125 cfunc_T cb = fp->uf_cb;
3126
3127 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3128 }
3129#endif
3130
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003131 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3132 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003133 error = check_user_func_argcount(fp, argcount);
3134 if (error != FCERR_UNKNOWN)
3135 return error;
3136 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 error = FCERR_DICT;
3138 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003139 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 int did_save_redo = FALSE;
3141 save_redo_T save_redo;
3142
3143 /*
3144 * Call the user function.
3145 * Save and restore search patterns, script variables and
3146 * redo buffer.
3147 */
3148 save_search_patterns();
3149 if (!ins_compl_active())
3150 {
3151 saveRedobuff(&save_redo);
3152 did_save_redo = TRUE;
3153 }
3154 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003155 call_user_func(fp, argcount, argvars, rettv, funcexe,
3156 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3158 // Function was unreferenced while being used, free it now.
3159 func_clear_free(fp, FALSE);
3160 if (did_save_redo)
3161 restoreRedobuff(&save_redo);
3162 restore_search_patterns();
3163 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003166}
3167
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003168static funccal_entry_T *funccal_stack = NULL;
3169
3170/*
3171 * Save the current function call pointer, and set it to NULL.
3172 * Used when executing autocommands and for ":source".
3173 */
3174 void
3175save_funccal(funccal_entry_T *entry)
3176{
3177 entry->top_funccal = current_funccal;
3178 entry->next = funccal_stack;
3179 funccal_stack = entry;
3180 current_funccal = NULL;
3181}
3182
3183 void
3184restore_funccal(void)
3185{
3186 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003187 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003188 else
3189 {
3190 current_funccal = funccal_stack->top_funccal;
3191 funccal_stack = funccal_stack->next;
3192 }
3193}
3194
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003195 funccall_T *
3196get_current_funccal(void)
3197{
3198 return current_funccal;
3199}
3200
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003201/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003202 * Return TRUE when currently at the script level:
3203 * - not in a function
3204 * - not executing an autocommand
3205 * Note that when an autocommand sources a script the result is FALSE;
3206 */
3207 int
3208at_script_level(void)
3209{
3210 return current_funccal == NULL && autocmd_match == NULL;
3211}
3212
3213/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003214 * Mark all functions of script "sid" as deleted.
3215 */
3216 void
3217delete_script_functions(int sid)
3218{
3219 hashitem_T *hi;
3220 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003221 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003222 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003223 size_t len;
3224
3225 buf[0] = K_SPECIAL;
3226 buf[1] = KS_EXTRA;
3227 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003228 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003229 len = STRLEN(buf);
3230
Bram Moolenaarce658352020-07-31 23:47:12 +02003231 while (todo > 0)
3232 {
3233 todo = func_hashtab.ht_used;
3234 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3235 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003236 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003237 fp = HI2UF(hi);
3238 if (STRNCMP(fp->uf_name, buf, len) == 0)
3239 {
3240 int changed = func_hashtab.ht_changed;
3241
3242 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003243
3244 if (fp->uf_calls > 0)
3245 {
3246 // Function is executing, don't free it but do remove
3247 // it from the hashtable.
3248 if (func_remove(fp))
3249 fp->uf_refcount--;
3250 }
3251 else
3252 {
3253 func_clear(fp, TRUE);
3254 // When clearing a function another function can be
3255 // cleared as a side effect. When that happens start
3256 // over.
3257 if (changed != func_hashtab.ht_changed)
3258 break;
3259 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003260 }
3261 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003262 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003263 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003264}
3265
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003266#if defined(EXITFREE) || defined(PROTO)
3267 void
3268free_all_functions(void)
3269{
3270 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003271 ufunc_T *fp;
3272 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003273 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003274 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003275
Bram Moolenaare38eab22019-12-05 21:50:01 +01003276 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003277 while (current_funccal != NULL)
3278 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003279 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003280 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003281 if (current_funccal == NULL && funccal_stack != NULL)
3282 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003283 }
3284
Bram Moolenaare38eab22019-12-05 21:50:01 +01003285 // First clear what the functions contain. Since this may lower the
3286 // reference count of a function, it may also free a function and change
3287 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003288 while (todo > 0)
3289 {
3290 todo = func_hashtab.ht_used;
3291 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3292 if (!HASHITEM_EMPTY(hi))
3293 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 // clear the def function index now
3295 fp = HI2UF(hi);
3296 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003297 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298
Bram Moolenaare38eab22019-12-05 21:50:01 +01003299 // Only free functions that are not refcounted, those are
3300 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003301 if (func_name_refcount(fp->uf_name))
3302 ++skipped;
3303 else
3304 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003305 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003306 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003307 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003308 {
3309 skipped = 0;
3310 break;
3311 }
3312 }
3313 --todo;
3314 }
3315 }
3316
Bram Moolenaare38eab22019-12-05 21:50:01 +01003317 // Now actually free the functions. Need to start all over every time,
3318 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003319 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003320 while (func_hashtab.ht_used > skipped)
3321 {
3322 todo = func_hashtab.ht_used;
3323 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003324 if (!HASHITEM_EMPTY(hi))
3325 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003326 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003327 // Only free functions that are not refcounted, those are
3328 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003329 fp = HI2UF(hi);
3330 if (func_name_refcount(fp->uf_name))
3331 ++skipped;
3332 else
3333 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003334 if (func_free(fp, FALSE) == OK)
3335 {
3336 skipped = 0;
3337 break;
3338 }
3339 // did not actually free it
3340 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003341 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003342 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003343 }
3344 if (skipped == 0)
3345 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346
3347 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003348}
3349#endif
3350
3351/*
3352 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003353 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3354 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003355 * "len" is the length of "name", or -1 for NUL terminated.
3356 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003358builtin_function(char_u *name, int len)
3359{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003360 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003361
Bram Moolenaara26b9702020-04-18 19:53:28 +02003362 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003363 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003364 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3365 {
3366 if (name[i] == AUTOLOAD_CHAR)
3367 return FALSE;
3368 if (!eval_isnamec(name[i]))
3369 {
3370 // "name.something" is not a builtin function
3371 if (name[i] == '.')
3372 return FALSE;
3373 break;
3374 }
3375 }
3376 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003377}
3378
3379 int
3380func_call(
3381 char_u *name,
3382 typval_T *args,
3383 partial_T *partial,
3384 dict_T *selfdict,
3385 typval_T *rettv)
3386{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003387 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003388 listitem_T *item;
3389 typval_T argv[MAX_FUNC_ARGS + 1];
3390 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003391 int r = 0;
3392
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003393 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003394 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003395 {
3396 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3397 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003398 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003399 break;
3400 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003401 // Make a copy of each argument. This is needed to be able to set
3402 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003403 copy_tv(&item->li_tv, &argv[argc++]);
3404 }
3405
3406 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003407 {
3408 funcexe_T funcexe;
3409
Bram Moolenaara80faa82020-04-12 19:37:17 +02003410 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003411 funcexe.fe_firstline = curwin->w_cursor.lnum;
3412 funcexe.fe_lastline = curwin->w_cursor.lnum;
3413 funcexe.fe_evaluate = TRUE;
3414 funcexe.fe_partial = partial;
3415 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003416 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3417 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003418
Bram Moolenaare38eab22019-12-05 21:50:01 +01003419 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003420 while (argc > 0)
3421 clear_tv(&argv[--argc]);
3422
3423 return r;
3424}
3425
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003426static int callback_depth = 0;
3427
3428 int
3429get_callback_depth(void)
3430{
3431 return callback_depth;
3432}
3433
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003434/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003435 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003436 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003437 */
3438 int
3439call_callback(
3440 callback_T *callback,
3441 int len, // length of "name" or -1 to use strlen()
3442 typval_T *rettv, // return value goes here
3443 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003444 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003445 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003446{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003447 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003448 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003449
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003450 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3451 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003452 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003453 funcexe.fe_evaluate = TRUE;
3454 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003455 ++callback_depth;
3456 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3457 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003458
3459 // When a :def function was called that uses :try an error would be turned
3460 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003461 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003462 {
3463 need_rethrow = FALSE;
3464 handle_did_throw();
3465 }
3466
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003467 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003468}
3469
3470/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003471 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003472 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003473 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3474 */
3475 varnumber_T
3476call_callback_retnr(
3477 callback_T *callback,
3478 int argcount, // number of "argvars"
3479 typval_T *argvars) // vars for arguments, must have "argcount"
3480 // PLUS ONE elements!
3481{
3482 typval_T rettv;
3483 varnumber_T retval;
3484
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003485 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003486 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003487
3488 retval = tv_get_number_chk(&rettv, NULL);
3489 clear_tv(&rettv);
3490 return retval;
3491}
3492
3493/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 * Give an error message for the result of a function.
3495 * Nothing if "error" is FCERR_NONE.
3496 */
3497 void
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003498user_func_error(int error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499{
3500 switch (error)
3501 {
3502 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003503 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003504 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003505 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003506 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 break;
3508 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003509 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 break;
3511 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003512 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 break;
3514 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003515 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516 break;
3517 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003518 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 break;
3520 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003521 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 break;
3523 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003524 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3525 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 break;
3527 }
3528}
3529
3530/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003531 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003532 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003533 * Return FAIL when the function can't be called, OK otherwise.
3534 * Also returns OK when an error was encountered while executing the function.
3535 */
3536 int
3537call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003538 char_u *funcname, // name of the function
3539 int len, // length of "name" or -1 to use strlen()
3540 typval_T *rettv, // return value goes here
3541 int argcount_in, // number of "argvars"
3542 typval_T *argvars_in, // vars for arguments, must have "argcount"
3543 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003544 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003545{
3546 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01003547 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003548 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003549 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003550 char_u fname_buf[FLEN_FIXED + 1];
3551 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003552 char_u *fname = NULL;
3553 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003554 int argcount = argcount_in;
3555 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003556 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003557 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003558 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003559 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003560 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003561 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003562 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003563 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003564
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003565 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3566 // even when call_func() returns FAIL.
3567 rettv->v_type = VAR_UNKNOWN;
3568
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003569 if (partial != NULL)
3570 fp = partial->pt_func;
3571 if (fp == NULL)
3572 {
3573 // Make a copy of the name, if it comes from a funcref variable it
3574 // could be changed or deleted in the called function.
3575 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3576 if (name == NULL)
3577 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003578
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003579 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3580 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003581
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003582 if (funcexe->fe_doesrange != NULL)
3583 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003584
3585 if (partial != NULL)
3586 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003587 // When the function has a partial with a dict and there is a dict
3588 // argument, use the dict argument. That is backwards compatible.
3589 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003590 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003591 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003592 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003593 {
3594 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003595 {
3596 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3597 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003598 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003599 goto theend;
3600 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003601 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003602 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003603 for (i = 0; i < argcount_in; ++i)
3604 argv[i + argv_clear] = argvars_in[i];
3605 argvars = argv;
3606 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003607
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003608 if (funcexe->fe_check_type != NULL
3609 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003610 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003611 // Now funcexe->fe_check_type is missing the added arguments,
3612 // make a copy of the type with the correction.
3613 check_type = *funcexe->fe_check_type;
3614 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003615 check_type.tt_args = check_type_args;
3616 CLEAR_FIELD(check_type_args);
3617 for (i = 0; i < check_type.tt_argcount; ++i)
3618 check_type_args[i + partial->pt_argc] =
3619 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003620 check_type.tt_argcount += partial->pt_argc;
3621 check_type.tt_min_argcount += partial->pt_argc;
3622 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003623 }
3624 }
3625
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003626 if (error == FCERR_NONE && funcexe->fe_check_type != NULL
3627 && funcexe->fe_evaluate)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003628 {
3629 // Check that the argument types are OK for the types of the funcref.
Bram Moolenaarc84287d2022-01-16 18:06:21 +00003630 if (check_argument_types(funcexe->fe_check_type,
3631 argvars, argcount, funcexe->fe_basetv,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003632 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003633 error = FCERR_OTHER;
3634 }
3635
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003636 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003637 {
3638 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003639 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003640
Bram Moolenaar333894b2020-08-01 18:53:07 +02003641 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003642 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003643 {
3644 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003645 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003646 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003647
Bram Moolenaare38eab22019-12-05 21:50:01 +01003648 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003649 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003650 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003651
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003652 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003653 {
3654 /*
3655 * User defined function.
3656 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003657 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003658 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003659 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003660 if (fp != NULL && !is_global && in_vim9script()
3661 && func_requires_g_prefix(fp))
3662 // In Vim9 script g: is required to find a global
3663 // non-autoload function.
3664 fp = NULL;
3665 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003666
Bram Moolenaare38eab22019-12-05 21:50:01 +01003667 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003668 if (fp == NULL
3669 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003670 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003671 && !aborting())
3672 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003673 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003674 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003675 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003676 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003677 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3678 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003679 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003680 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003681 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003682 if (fp == NULL)
3683 {
3684 char_u *p = untrans_function_name(rfname);
3685
3686 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003687 // Don't do this if the name starts with "s:".
3688 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003689 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003690 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003691
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003692 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003693 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003694 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003695 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003696 if (funcexe->fe_argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003697 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003698 argcount = funcexe->fe_argv_func(argcount, argvars,
zeertzjq48db5da2022-09-16 12:10:03 +01003699 argv_clear, fp);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003700
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003701 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003702 {
3703 // Method call: base->Method()
3704 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003705 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003706 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003707 argvars = argv;
3708 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003709 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 error = call_user_func_check(fp, argcount, argvars, rettv,
3712 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003713 }
3714 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003715 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003716 {
3717 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003718 * expr->method(): Find the method name in the table, call its
3719 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003720 */
3721 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003722 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003723 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003724 else
3725 {
3726 /*
3727 * Find the function name in the table, call its implementation.
3728 */
3729 error = call_internal_func(fname, argcount, argvars, rettv);
3730 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003731
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003732 /*
3733 * The function call (or "FuncUndefined" autocommand sequence) might
3734 * have been aborted by an error, an interrupt, or an explicitly thrown
3735 * exception that has not been caught so far. This situation can be
3736 * tested for by calling aborting(). For an error in an internal
3737 * function or for the "E132" error in call_user_func(), however, the
3738 * throw point at which the "force_abort" flag (temporarily reset by
3739 * emsg()) is normally updated has not been reached yet. We need to
3740 * update that flag first to make aborting() reliable.
3741 */
3742 update_force_abort();
3743 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003744 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003745 ret = OK;
3746
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003747theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003748 /*
3749 * Report an error unless the argument evaluation or function call has been
3750 * cancelled due to an aborting error, an interrupt, or an exception.
3751 */
3752 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003753 user_func_error(error, (name != NULL) ? name : funcname,
3754 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003755
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003756 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003757 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003758 clear_tv(&argv[--argv_clear + argv_base]);
3759
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003760 vim_free(tofree);
3761 vim_free(name);
3762
3763 return ret;
3764}
3765
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003766/*
3767 * Call a function without arguments, partial or dict.
3768 * This is like call_func() when the call is only "FuncName()".
3769 * To be used by "expr" options.
3770 * Returns NOTDONE when the function could not be found.
3771 */
3772 int
3773call_simple_func(
3774 char_u *funcname, // name of the function
3775 int len, // length of "name" or -1 to use strlen()
3776 typval_T *rettv) // return value goes here
3777{
3778 int ret = FAIL;
3779 int error = FCERR_NONE;
3780 char_u fname_buf[FLEN_FIXED + 1];
3781 char_u *tofree = NULL;
3782 char_u *name;
3783 char_u *fname;
3784 char_u *rfname;
3785 int is_global = FALSE;
3786 ufunc_T *fp;
3787
3788 rettv->v_type = VAR_NUMBER; // default rettv is number zero
3789 rettv->vval.v_number = 0;
3790
3791 // Make a copy of the name, an option can be changed in the function.
3792 name = vim_strnsave(funcname, len);
3793 if (name == NULL)
3794 return ret;
3795
3796 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3797
3798 // Skip "g:" before a function name.
3799 if (fname[0] == 'g' && fname[1] == ':')
3800 {
3801 is_global = TRUE;
3802 rfname = fname + 2;
3803 }
3804 else
3805 rfname = fname;
3806 fp = find_func(rfname, is_global);
3807 if (fp != NULL && !is_global && in_vim9script()
3808 && func_requires_g_prefix(fp))
3809 // In Vim9 script g: is required to find a global non-autoload
3810 // function.
3811 fp = NULL;
3812 if (fp == NULL)
3813 ret = NOTDONE;
3814 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
3815 error = FCERR_DELETED;
3816 else if (fp != NULL)
3817 {
3818 typval_T argvars[1];
3819 funcexe_T funcexe;
3820
3821 argvars[0].v_type = VAR_UNKNOWN;
3822 CLEAR_FIELD(funcexe);
3823 funcexe.fe_evaluate = TRUE;
3824
3825 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
3826 if (error == FCERR_NONE)
3827 ret = OK;
3828 }
3829
3830 user_func_error(error, name, FALSE);
3831 vim_free(tofree);
3832 vim_free(name);
3833
3834 return ret;
3835}
3836
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003837 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838printable_func_name(ufunc_T *fp)
3839{
3840 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3841}
3842
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003843/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003844 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
3845 * TRUE. Otherwise return FALSE.
3846 */
3847 static int
3848function_list_modified(int prev_ht_changed)
3849{
3850 if (prev_ht_changed != func_hashtab.ht_changed)
3851 {
3852 emsg(_(e_function_list_was_modified));
3853 return TRUE;
3854 }
3855 return FALSE;
3856}
3857
3858/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003859 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003860 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003861 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003862list_func_head(ufunc_T *fp, int indent)
3863{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003864 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003865 int j;
3866
3867 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003868
3869 // a timer at the more prompt may have deleted the function
3870 if (function_list_modified(prev_ht_changed))
3871 return FAIL;
3872
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003873 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003874 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003875 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003876 msg_puts("def ");
3877 else
3878 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003880 msg_putchar('(');
3881 for (j = 0; j < fp->uf_args.ga_len; ++j)
3882 {
3883 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003884 msg_puts(", ");
3885 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 if (fp->uf_arg_types != NULL)
3887 {
3888 char *tofree;
3889
3890 msg_puts(": ");
3891 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3892 vim_free(tofree);
3893 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003894 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3895 {
3896 msg_puts(" = ");
3897 msg_puts(((char **)(fp->uf_def_args.ga_data))
3898 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3899 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003900 }
3901 if (fp->uf_varargs)
3902 {
3903 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003904 msg_puts(", ");
3905 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003906 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 if (fp->uf_va_name != NULL)
3908 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01003909 if (!fp->uf_varargs)
3910 {
3911 if (j)
3912 msg_puts(", ");
3913 msg_puts("...");
3914 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003916 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 {
3918 char *tofree;
3919
3920 msg_puts(": ");
3921 msg_puts(type_name(fp->uf_va_type, &tofree));
3922 vim_free(tofree);
3923 }
3924 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003925 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003926
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003927 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003928 {
3929 if (fp->uf_ret_type != &t_void)
3930 {
3931 char *tofree;
3932
3933 msg_puts(": ");
3934 msg_puts(type_name(fp->uf_ret_type, &tofree));
3935 vim_free(tofree);
3936 }
3937 }
3938 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003939 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003940 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003941 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003942 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003943 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003944 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003945 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003946 msg_clr_eos();
3947 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003948 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003949
3950 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003951}
3952
3953/*
3954 * Get a function name, translating "<SID>" and "<SNR>".
3955 * Also handles a Funcref in a List or Dictionary.
3956 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003957 * Set "*is_global" to TRUE when the function must be global, unless
3958 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003959 * flags:
3960 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00003961 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003962 * TFN_QUIET: be quiet
3963 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003964 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003965 * Advances "pp" to just after the function name (if no error).
3966 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003967 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003968trans_function_name(
3969 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003970 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003971 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003972 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003973 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003974 partial_T **partial, // return: partial of a FuncRef
3975 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003976{
3977 char_u *name = NULL;
3978 char_u *start;
3979 char_u *end;
3980 int lead;
3981 char_u sid_buf[20];
3982 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003984 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003985 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003986 int vim9script = in_vim9script();
3987 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003988
3989 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003990 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003991 start = *pp;
3992
Bram Moolenaare38eab22019-12-05 21:50:01 +01003993 // Check for hard coded <SNR>: already translated function ID (from a user
3994 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003995 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
3996 && (*pp)[2] == (int)KE_SNR)
3997 {
3998 *pp += 3;
3999 len = get_id_len(pp) + 3;
4000 return vim_strnsave(start, len);
4001 }
4002
Bram Moolenaare38eab22019-12-05 21:50:01 +01004003 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4004 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004005 lead = eval_fname_script(start);
4006 if (lead > 2)
4007 start += lead;
4008
Bram Moolenaare38eab22019-12-05 21:50:01 +01004009 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01004010 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004011 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004012 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004013 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004014 {
4015 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004016 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004017 goto theend;
4018 }
4019 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4020 {
4021 /*
4022 * Report an invalid expression in braces, unless the expression
4023 * evaluation has been cancelled due to an aborting error, an
4024 * interrupt, or an exception.
4025 */
4026 if (!aborting())
4027 {
4028 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004029 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004030 }
4031 else
4032 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4033 goto theend;
4034 }
4035
4036 if (lv.ll_tv != NULL)
4037 {
4038 if (fdp != NULL)
4039 {
4040 fdp->fd_dict = lv.ll_dict;
4041 fdp->fd_newkey = lv.ll_newkey;
4042 lv.ll_newkey = NULL;
4043 fdp->fd_di = lv.ll_di;
4044 }
4045 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4046 {
4047 name = vim_strsave(lv.ll_tv->vval.v_string);
4048 *pp = end;
4049 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004050 else if (lv.ll_tv->v_type == VAR_CLASS
4051 && lv.ll_tv->vval.v_class != NULL)
4052 {
4053 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4054 *pp = end;
4055 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004056 else if (lv.ll_tv->v_type == VAR_PARTIAL
4057 && lv.ll_tv->vval.v_partial != NULL)
4058 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004059 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004060 *pp = end;
4061 if (partial != NULL)
4062 *partial = lv.ll_tv->vval.v_partial;
4063 }
4064 else
4065 {
4066 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4067 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004068 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004069 else
4070 *pp = end;
4071 name = NULL;
4072 }
4073 goto theend;
4074 }
4075
4076 if (lv.ll_name == NULL)
4077 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004078 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004079 *pp = end;
4080 goto theend;
4081 }
4082
Bram Moolenaare38eab22019-12-05 21:50:01 +01004083 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004084 if (lv.ll_exp_name != NULL)
4085 {
4086 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004087 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004088 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004089 if (name == lv.ll_exp_name)
4090 name = NULL;
4091 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004092 else if (lv.ll_sid > 0)
4093 {
4094 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4095 int cc = *lv.ll_name_end;
4096
4097 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4098 *lv.ll_name_end = NUL;
4099 if (si->sn_autoload_prefix != NULL)
4100 {
4101 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4102 }
4103 else
4104 {
4105 sid_buf[0] = K_SPECIAL;
4106 sid_buf[1] = KS_EXTRA;
4107 sid_buf[2] = (int)KE_SNR;
4108 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004109 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004110 name = concat_str(sid_buf, lv.ll_name);
4111 }
4112 *lv.ll_name_end = cc;
4113 *pp = end;
4114 goto theend;
4115 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004116 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004117 {
4118 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004119 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004120 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004121 if (name == *pp)
4122 name = NULL;
4123 }
4124 if (name != NULL)
4125 {
4126 name = vim_strsave(name);
4127 *pp = end;
4128 if (STRNCMP(name, "<SNR>", 5) == 0)
4129 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004130 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004131 name[0] = K_SPECIAL;
4132 name[1] = KS_EXTRA;
4133 name[2] = (int)KE_SNR;
4134 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4135 }
4136 goto theend;
4137 }
4138
4139 if (lv.ll_exp_name != NULL)
4140 {
4141 len = (int)STRLEN(lv.ll_exp_name);
4142 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4143 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4144 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004145 // When there was "s:" already or the name expanded to get a
4146 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004147 lv.ll_name += 2;
4148 len -= 2;
4149 lead = 2;
4150 }
4151 }
4152 else
4153 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004154 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004155 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004156 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004157 if (lv.ll_name[0] == 'g')
4158 {
4159 if (is_global != NULL)
4160 {
4161 *is_global = TRUE;
4162 }
4163 else
4164 {
4165 // dropping "g:" without setting "is_global" won't work in
4166 // Vim9script, put it back later
4167 prefix_g = TRUE;
4168 extra = 2;
4169 }
4170 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004171 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004172 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004173 len = (int)(end - lv.ll_name);
4174 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004175 if (len <= 0)
4176 {
4177 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004178 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004179 goto theend;
4180 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004181
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004182 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004183 // starts with a lower case character: dict.func(). Or when in a class.
4184 vim9_local = ASCII_ISUPPER(*start) && vim9script
4185 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004187 /*
4188 * Copy the function name to allocated memory.
4189 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4190 * Accept <SNR>123_name() outside a script.
4191 */
4192 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004193 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004194 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004195 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004196 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004197 {
Kota Kato948a3892022-08-16 16:09:59 +01004198 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4199 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004200 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004201 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004202 goto theend;
4203 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004205 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004206 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004208 || eval_fname_sid(*pp))
4209 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004211 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004212 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004213 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004214 goto theend;
4215 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004216 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004217 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 extra = 3 + (int)STRLEN(sid_buf);
4219 else
4220 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004221 }
4222 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004223 else if (!(flags & TFN_INT)
4224 && (builtin_function(lv.ll_name, len)
4225 || (vim9script && *lv.ll_name == '_'))
4226 && !((flags & TFN_IN_CLASS) && STRNCMP(lv.ll_name, "new", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004227 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004228 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004229 : e_function_name_must_start_with_capital_or_s_str),
4230 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004231 goto theend;
4232 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004233 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004234 {
4235 char_u *cp = vim_strchr(lv.ll_name, ':');
4236
4237 if (cp != NULL && cp < end)
4238 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004239 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004240 goto theend;
4241 }
4242 }
4243
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004245 if (name != NULL)
4246 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004247 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004248 {
4249 name[0] = K_SPECIAL;
4250 name[1] = KS_EXTRA;
4251 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004252 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004253 STRCPY(name + 3, sid_buf);
4254 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004255 else if (prefix_g)
4256 {
4257 name[0] = 'g';
4258 name[1] = ':';
4259 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4261 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004262 }
4263 *pp = end;
4264
4265theend:
4266 clear_lval(&lv);
4267 return name;
4268}
4269
4270/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004271 * Assuming "name" is the result of trans_function_name() and it was prefixed
4272 * to use the script-local name, return the unmodified name (points into
4273 * "name"). Otherwise return NULL.
4274 * This can be used to first search for a script-local function and fall back
4275 * to the global function if not found.
4276 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004277 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004278untrans_function_name(char_u *name)
4279{
4280 char_u *p;
4281
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004282 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004283 {
4284 p = vim_strchr(name, '_');
4285 if (p != NULL)
4286 return p + 1;
4287 }
4288 return NULL;
4289}
4290
4291/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004292 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4293 * current script ID and returns the expanded function name. The caller should
4294 * free the returned name. If not called from a script context or the function
4295 * name doesn't start with these prefixes, then returns NULL.
4296 * This doesn't check whether the script-local function exists or not.
4297 */
4298 char_u *
4299get_scriptlocal_funcname(char_u *funcname)
4300{
4301 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004302 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004303 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004304 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004305
4306 if (funcname == NULL)
4307 return NULL;
4308
4309 if (STRNCMP(funcname, "s:", 2) != 0
4310 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004311 {
4312 ufunc_T *ufunc;
4313
4314 // The function name does not have a script-local prefix. Try finding
4315 // it when in a Vim9 script and there is no "g:" prefix.
4316 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4317 return NULL;
4318 ufunc = find_func(funcname, FALSE);
4319 if (ufunc == NULL || func_is_global(ufunc)
4320 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4321 return NULL;
4322 ++p;
4323 off = 0;
4324 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004325 else
4326 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004327
4328 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4329 {
4330 emsg(_(e_using_sid_not_in_script_context));
4331 return NULL;
4332 }
4333 // Expand s: prefix into <SNR>nr_<name>
4334 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4335 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004336 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004337 if (newname == NULL)
4338 return NULL;
4339 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004340 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004341
4342 return newname;
4343}
4344
4345/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004346 * Return script-local "fname" with the 3-byte sequence replaced by
4347 * printable <SNR> in allocated memory.
4348 */
4349 char_u *
4350alloc_printable_func_name(char_u *fname)
4351{
4352 char_u *n = alloc(STRLEN(fname + 3) + 6);
4353
4354 if (n != NULL)
4355 {
4356 STRCPY(n, "<SNR>");
4357 STRCPY(n + 5, fname + 3);
4358 }
4359 return n;
4360}
4361
4362/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004363 * Call trans_function_name(), except that a lambda is returned as-is.
4364 * Returns the name in allocated memory.
4365 */
4366 char_u *
4367save_function_name(
4368 char_u **name,
4369 int *is_global,
4370 int skip,
4371 int flags,
4372 funcdict_T *fudi)
4373{
4374 char_u *p = *name;
4375 char_u *saved;
4376
4377 if (STRNCMP(p, "<lambda>", 8) == 0)
4378 {
4379 p += 8;
4380 (void)getdigits(&p);
4381 saved = vim_strnsave(*name, p - *name);
4382 if (fudi != NULL)
4383 CLEAR_POINTER(fudi);
4384 }
4385 else
4386 saved = trans_function_name(&p, is_global, skip,
4387 flags, fudi, NULL, NULL);
4388 *name = p;
4389 return saved;
4390}
4391
4392/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004393 * List functions. When "regmatch" is NULL all of then.
4394 * Otherwise functions matching "regmatch".
4395 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004396 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004397list_functions(regmatch_T *regmatch)
4398{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004399 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004400 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004401 hashitem_T *hi;
4402
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004403 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004404 {
4405 if (!HASHITEM_EMPTY(hi))
4406 {
4407 ufunc_T *fp = HI2UF(hi);
4408
4409 --todo;
4410 if ((fp->uf_flags & FC_DEAD) == 0
4411 && (regmatch == NULL
4412 ? !message_filtered(fp->uf_name)
4413 && !func_name_refcount(fp->uf_name)
4414 : !isdigit(*fp->uf_name)
4415 && vim_regexec(regmatch, fp->uf_name, 0)))
4416 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004417 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004418 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004419 if (function_list_modified(prev_ht_changed))
4420 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004421 }
4422 }
4423 }
4424}
4425
4426/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004427 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004428 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4429 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004430 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004431 * If "in_class" is TRUE then the function is defined inside a class.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004432 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004433 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004434 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004435define_function(
4436 exarg_T *eap,
4437 char_u *name_arg,
4438 garray_T *lines_to_free,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004439 int in_class)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004440{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004441 int j;
4442 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004443 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004444 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004445 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004446 char_u *p;
4447 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004448 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004449 char_u *line_arg = NULL;
4450 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004452 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004453 garray_T newlines;
4454 int varargs = FALSE;
4455 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004457 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004458 int fp_allocated = FALSE;
4459 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004460 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004461 dictitem_T *v;
4462 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004463 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004464 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004465 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004466 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004467 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004468 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004469
4470 /*
4471 * ":function" without argument: list functions.
4472 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004473 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004474 {
4475 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004476 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004477 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004478 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004479 }
4480
4481 /*
4482 * ":function /pat": list functions matching pattern.
4483 */
4484 if (*eap->arg == '/')
4485 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004486 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004487 if (!eap->skip)
4488 {
4489 regmatch_T regmatch;
4490
4491 c = *p;
4492 *p = NUL;
4493 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4494 *p = c;
4495 if (regmatch.regprog != NULL)
4496 {
4497 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004498 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004499 vim_regfree(regmatch.regprog);
4500 }
4501 }
4502 if (*p == '/')
4503 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004504 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004505 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004506 }
4507
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 ga_init(&newargs);
4509 ga_init(&argtypes);
4510 ga_init(&default_args);
4511
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004512 /*
4513 * Get the function name. There are these situations:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004514 * func normal function name, also when "in_class" is TRUE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004515 * "name" == func, "fudi.fd_dict" == NULL
4516 * dict.func new dictionary entry
4517 * "name" == NULL, "fudi.fd_dict" set,
4518 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4519 * dict.func existing dict entry with a Funcref
4520 * "name" == func, "fudi.fd_dict" set,
4521 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4522 * dict.func existing dict entry that's not a Funcref
4523 * "name" == NULL, "fudi.fd_dict" set,
4524 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4525 * s:func script-local function name
4526 * g:func global function name, same as "func"
4527 */
4528 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004529 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004530 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004531 // nested function, argument is (args).
4532 paren = TRUE;
4533 CLEAR_FIELD(fudi);
4534 }
4535 else
4536 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004537 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004538 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004539 if (p[0] == 's' && p[1] == ':')
4540 {
4541 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4542 return NULL;
4543 }
4544 p = to_name_end(p, TRUE);
4545 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4546 {
4547 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4548 eap->arg);
4549 return NULL;
4550 }
4551 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004552 }
4553
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004554 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004555 | (in_class ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004556 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004557 paren = (vim_strchr(p, '(') != NULL);
4558 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004559 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004560 /*
4561 * Return on an invalid expression in braces, unless the expression
4562 * evaluation has been cancelled due to an aborting error, an
4563 * interrupt, or an exception.
4564 */
4565 if (!aborting())
4566 {
4567 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004568 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004569 vim_free(fudi.fd_newkey);
4570 return NULL;
4571 }
4572 else
4573 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004574 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004575
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004576 // For "export def FuncName()" in an autoload script the function name
4577 // is stored with the legacy autoload name "dir#script#FuncName" so
4578 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004579 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004580 {
4581 char_u *prefixed = may_prefix_autoload(name);
4582
4583 if (prefixed != NULL && prefixed != name)
4584 {
4585 vim_free(name);
4586 name = prefixed;
4587 }
4588 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004589 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004590 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004591 {
4592 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4593 goto ret_free;
4594 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004595 }
4596
Bram Moolenaare38eab22019-12-05 21:50:01 +01004597 // An error in a function call during evaluation of an expression in magic
4598 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004599 saved_did_emsg = did_emsg;
4600 did_emsg = FALSE;
4601
4602 /*
4603 * ":function func" with only function name: list function.
4604 */
4605 if (!paren)
4606 {
4607 if (!ends_excmd(*skipwhite(p)))
4608 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004609 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004610 goto ret_free;
4611 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004612 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004613 if (eap->nextcmd != NULL)
4614 *p = NUL;
4615 if (!eap->skip && !got_int)
4616 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004617 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004618 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4619 {
4620 char_u *up = untrans_function_name(name);
4621
4622 // With Vim9 script the name was made script-local, if not
4623 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004624 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004625 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004626 }
4627
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004628 if (fp != NULL)
4629 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004630 // Check no function was added or removed from a timer, e.g. at
4631 // the more prompt. "fp" may then be invalid.
4632 int prev_ht_changed = func_hashtab.ht_changed;
4633
4634 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004635 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004636 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4637 {
4638 if (FUNCLINE(fp, j) == NULL)
4639 continue;
4640 msg_putchar('\n');
4641 msg_outnum((long)(j + 1));
4642 if (j < 9)
4643 msg_putchar(' ');
4644 if (j < 99)
4645 msg_putchar(' ');
4646 if (function_list_modified(prev_ht_changed))
4647 break;
4648 msg_prt_line(FUNCLINE(fp, j), FALSE);
4649 out_flush(); // show a line at a time
4650 ui_breakcheck();
4651 }
4652 if (!got_int)
4653 {
4654 msg_putchar('\n');
4655 if (!function_list_modified(prev_ht_changed))
4656 {
4657 if (fp->uf_def_status != UF_NOT_COMPILED)
4658 msg_puts(" enddef");
4659 else
4660 msg_puts(" endfunction");
4661 }
4662 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004663 }
4664 }
4665 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004666 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004667 }
4668 goto ret_free;
4669 }
4670
4671 /*
4672 * ":function name(arg1, arg2)" Define function.
4673 */
4674 p = skipwhite(p);
4675 if (*p != '(')
4676 {
4677 if (!eap->skip)
4678 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004679 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004680 goto ret_free;
4681 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004682 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004683 if (vim_strchr(p, '(') != NULL)
4684 p = vim_strchr(p, '(');
4685 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004686
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004687 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4688 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004689 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004690 goto ret_free;
4691 }
4692
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004693 // In Vim9 script only global functions can be redefined.
4694 if (vim9script && eap->forceit && !is_global)
4695 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004696 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004697 goto ret_free;
4698 }
4699
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004700 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004701
Bram Moolenaar04b12692020-05-04 23:24:44 +02004702 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004703 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004704 // Check the name of the function. Unless it's a dictionary function
4705 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004706 if (name != NULL)
4707 arg = name;
4708 else
4709 arg = fudi.fd_newkey;
4710 if (arg != NULL && (fudi.fd_di == NULL
4711 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4712 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4713 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004714 char_u *name_base = arg;
4715 int i;
4716
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004717 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004718 {
4719 name_base = vim_strchr(arg, '_');
4720 if (name_base == NULL)
4721 name_base = arg + 3;
4722 else
4723 ++name_base;
4724 }
4725 for (i = 0; name_base[i] != NUL && (i == 0
4726 ? eval_isnamec1(name_base[i])
4727 : eval_isnamec(name_base[i])); ++i)
4728 ;
4729 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004730 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004731
4732 // In Vim9 script a function cannot have the same name as a
4733 // variable.
4734 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004735 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4736 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004737 + EVAL_VAR_NO_FUNC) == OK)
4738 {
4739 semsg(_(e_redefining_script_item_str), name_base);
4740 goto ret_free;
4741 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004742 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004743 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004744 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004745 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004746 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004747 goto ret_free;
4748 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004749 }
4750
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004751 // This may get more lines and make the pointers into the first line
4752 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004753 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004754 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004755 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004756 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004757 eap, in_class, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004758 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004759 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004760
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004762 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004764 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004765 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004766 if (*p != ':')
4767 {
4768 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4769 p = skipwhite(p);
4770 }
4771 else if (!IS_WHITE_OR_NUL(p[1]))
4772 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004774 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004776 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004777 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004778 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004780 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004782 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004783 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004784 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004785 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004786 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004787 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004788 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789 else
4790 // find extra arguments "range", "dict", "abort" and "closure"
4791 for (;;)
4792 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004793 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794 p = skipwhite(p);
4795 if (STRNCMP(p, "range", 5) == 0)
4796 {
4797 flags |= FC_RANGE;
4798 p += 5;
4799 }
4800 else if (STRNCMP(p, "dict", 4) == 0)
4801 {
4802 flags |= FC_DICT;
4803 p += 4;
4804 }
4805 else if (STRNCMP(p, "abort", 5) == 0)
4806 {
4807 flags |= FC_ABORT;
4808 p += 5;
4809 }
4810 else if (STRNCMP(p, "closure", 7) == 0)
4811 {
4812 flags |= FC_CLOSURE;
4813 p += 7;
4814 if (current_funccal == NULL)
4815 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004816 emsg_funcname(e_closure_function_should_not_be_at_top_level,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004817 name == NULL ? (char_u *)"" : name);
4818 goto erret;
4819 }
4820 }
4821 else
4822 break;
4823 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004824
Bram Moolenaare38eab22019-12-05 21:50:01 +01004825 // When there is a line break use what follows for the function body.
4826 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004827 if (*p == '\n')
4828 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004829 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004830 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4831 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004832 && !(VIM_ISWHITE(*whitep) && *p == '#'
4833 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004834 && !eap->skip
4835 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004836 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004837
4838 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4840 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004841 */
4842 if (KeyTyped)
4843 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004844 // Check if the function already exists, don't let the user type the
4845 // whole function before telling him it doesn't work! For a script we
4846 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004847 if (!eap->skip && !eap->forceit)
4848 {
4849 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004850 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004851 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00004852 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004853 }
4854
4855 if (!eap->skip && did_emsg)
4856 goto erret;
4857
Bram Moolenaare38eab22019-12-05 21:50:01 +01004858 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004859 cmdline_row = msg_row;
4860 }
4861
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004862 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004863 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004864
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004865 // Do not define the function when getting the body fails and when
4866 // skipping.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004867 if (get_function_body(eap, &newlines, line_arg, lines_to_free) == FAIL
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004868 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004869 goto erret;
4870
4871 /*
4872 * If there are no errors, add the function
4873 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004874 if (fudi.fd_dict != NULL)
4875 {
4876 char numbuf[20];
4877
4878 fp = NULL;
4879 if (fudi.fd_newkey == NULL && !eap->forceit)
4880 {
4881 emsg(_(e_dictionary_entry_already_exists));
4882 goto erret;
4883 }
4884 if (fudi.fd_di == NULL)
4885 {
4886 // Can't add a function to a locked dictionary
4887 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
4888 goto erret;
4889 }
4890 // Can't change an existing function if it is locked
4891 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
4892 goto erret;
4893
4894 // Give the function a sequential number. Can only be used with a
4895 // Funcref!
4896 vim_free(name);
4897 sprintf(numbuf, "%d", ++func_nr);
4898 name = vim_strsave((char_u *)numbuf);
4899 if (name == NULL)
4900 goto erret;
4901 }
4902 else if (!in_class)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004903 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004904 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004905 char_u *find_name = name;
4906 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004907 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004909 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004910 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004911 var_conflict = TRUE;
4912
4913 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
4914 {
4915 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4916
4917 if (si->sn_autoload_prefix != NULL)
4918 {
4919 if (is_export)
4920 {
4921 find_name = name + STRLEN(si->sn_autoload_prefix);
4922 v = find_var(find_name, &ht, TRUE);
4923 if (v != NULL)
4924 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004925 // Only check if the function already exists in the script,
4926 // global functions can be shadowed.
4927 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004928 }
4929 else
4930 {
4931 char_u *prefixed = may_prefix_autoload(name);
4932
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00004933 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004934 {
4935 v = find_var(prefixed, &ht, TRUE);
4936 if (v != NULL)
4937 var_conflict = TRUE;
4938 vim_free(prefixed);
4939 }
4940 }
4941 }
4942 }
4943 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004944 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004945 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004946 goto erret;
4947 }
4948
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004949 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02004950 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004951 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004952 char_u *uname = untrans_function_name(name);
4953
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004954 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004955 }
4956
4957 if (fp != NULL || import != NULL)
4958 {
4959 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004961 // Function can be replaced with "function!" and when sourcing the
4962 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02004963 // A name that is used by an import can not be overruled.
4964 if (import != NULL
4965 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004966 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02004967 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004968 {
Bram Moolenaard604d782021-11-20 21:46:20 +00004969 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02004970 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004971 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02004972 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00004973 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004974 goto erret;
4975 }
4976 if (fp->uf_calls > 0)
4977 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004978 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00004979 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004980 goto erret;
4981 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004982 if (fp->uf_refcount > 1)
4983 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004984 // This function is referenced somewhere, don't redefine it but
4985 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004986 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004987 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004988 fp = NULL;
4989 overwrite = TRUE;
4990 }
4991 else
4992 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004993 char_u *exp_name = fp->uf_name_exp;
4994
4995 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01004996 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004997 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004998 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004999 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005001#ifdef FEAT_PROFILE
5002 fp->uf_profiling = FALSE;
5003 fp->uf_prof_initialized = FALSE;
5004#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005005 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005006 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005007 }
5008 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005009
5010 if (fp == NULL)
5011 {
5012 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5013 {
5014 int slen, plen;
5015 char_u *scriptname;
5016
Bram Moolenaare38eab22019-12-05 21:50:01 +01005017 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005018 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005019 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005020 {
5021 scriptname = autoload_name(name);
5022 if (scriptname != NULL)
5023 {
5024 p = vim_strchr(scriptname, '/');
5025 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005026 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005027 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005028 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005029 j = OK;
5030 vim_free(scriptname);
5031 }
5032 }
5033 if (j == FAIL)
5034 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005035 linenr_T save_lnum = SOURCING_LNUM;
5036
5037 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005038 semsg(_(e_function_name_does_not_match_script_file_name_str),
5039 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005040 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005041 goto erret;
5042 }
5043 }
5044
Bram Moolenaar47ed5532019-08-08 20:49:14 +02005045 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005046 if (fp == NULL)
5047 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005048 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005049
5050 if (fudi.fd_dict != NULL)
5051 {
5052 if (fudi.fd_di == NULL)
5053 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005054 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005055 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5056 if (fudi.fd_di == NULL)
5057 {
5058 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02005059 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005060 goto erret;
5061 }
5062 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5063 {
5064 vim_free(fudi.fd_di);
5065 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02005066 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005067 goto erret;
5068 }
5069 }
5070 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005071 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005072 clear_tv(&fudi.fd_di->di_tv);
5073 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005074 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005075
Bram Moolenaare38eab22019-12-05 21:50:01 +01005076 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005077 flags |= FC_DICT;
5078 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005079 }
5080 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005081 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005083 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084
5085 if (eap->cmdidx == CMD_def)
5086 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005087 int lnum_save = SOURCING_LNUM;
5088 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005089
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005090 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005091
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005092 // error messages are for the first function line
5093 SOURCING_LNUM = sourcing_lnum_top;
5094
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005095 // The function may use script variables from the context.
5096 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005097
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005098 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005099 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005100 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005101 free_fp = fp_allocated;
5102 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005104 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105
5106 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005107 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005108 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005109 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005110 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005111 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005113 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005114 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005115 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005116 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005117
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005118 if (fp_allocated)
5119 {
5120 // insert the new function in the function list
5121 set_ufunc_name(fp, name);
5122 if (overwrite)
5123 {
5124 hi = hash_find(&func_hashtab, name);
5125 hi->hi_key = UF2HIKEY(fp);
5126 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005127 else if (!in_class && hash_add(&func_hashtab,
5128 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005129 {
5130 free_fp = TRUE;
5131 goto erret;
5132 }
5133 fp->uf_refcount = 1;
5134 }
5135
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005136 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005137 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005138 if ((flags & FC_CLOSURE) != 0)
5139 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005140 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005141 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005142 }
5143 else
5144 fp->uf_scoped = NULL;
5145
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005146#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005147 if (prof_def_func())
5148 func_do_profile(fp);
5149#endif
5150 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005151 if (sandbox)
5152 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005153 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005154 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005155 fp->uf_flags = flags;
5156 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005157 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005158 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005159 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005160 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005161 if (is_export)
5162 {
5163 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005164 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005165 is_export = FALSE;
5166 }
5167
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005168 if (eap->cmdidx == CMD_def)
5169 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005170 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5171 // :func does not use Vim9 script syntax, even in a Vim9 script file
5172 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005173
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005174 goto ret_free;
5175
5176erret:
5177 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005178 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005179 if (fp != NULL)
5180 {
5181 ga_init(&fp->uf_args);
5182 ga_init(&fp->uf_def_args);
5183 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005184errret_2:
5185 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005186 if (fp != NULL)
5187 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005188 if (free_fp)
5189 {
5190 vim_free(fp);
5191 fp = NULL;
5192 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005193ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005194 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005195 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005196 if (name != name_arg)
5197 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005198 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005199 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005200
5201 return fp;
5202}
5203
5204/*
5205 * ":function"
5206 */
5207 void
5208ex_function(exarg_T *eap)
5209{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005210 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005211
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005212 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005213 (void)define_function(eap, NULL, &lines_to_free, FALSE);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005214 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005215}
5216
5217/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005218 * Find a function by name, including "<lambda>123".
5219 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005220 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005221 * Return NULL if not found.
5222 */
5223 ufunc_T *
5224find_func_by_name(char_u *name, compiletype_T *compile_type)
5225{
5226 char_u *arg = name;
5227 char_u *fname;
5228 ufunc_T *ufunc;
5229 int is_global = FALSE;
5230
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005231 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5232 {
5233 *compile_type = CT_PROFILE;
5234 arg = skipwhite(arg + 7);
5235 }
5236 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5237 {
5238 *compile_type = CT_DEBUG;
5239 arg = skipwhite(arg + 5);
5240 }
5241
5242 if (STRNCMP(arg, "<lambda>", 8) == 0)
5243 {
5244 arg += 8;
5245 (void)getdigits(&arg);
5246 fname = vim_strnsave(name, arg - name);
5247 }
5248 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005249 {
5250 // First try finding a method in a class, find_func_by_name() will give
5251 // an error if the function is not found.
5252 ufunc = find_class_func(&arg);
5253 if (ufunc != NULL)
5254 return ufunc;
5255
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005256 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005257 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
5258 NULL, NULL, NULL);
5259 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005260 if (fname == NULL)
5261 {
5262 semsg(_(e_invalid_argument_str), name);
5263 return NULL;
5264 }
5265 if (!ends_excmd2(name, arg))
5266 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005267 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005268 emsg(ex_errmsg(e_trailing_characters_str, arg));
5269 return NULL;
5270 }
5271
5272 ufunc = find_func(fname, is_global);
5273 if (ufunc == NULL)
5274 {
5275 char_u *p = untrans_function_name(fname);
5276
5277 if (p != NULL)
5278 // Try again without making it script-local.
5279 ufunc = find_func(p, FALSE);
5280 }
5281 vim_free(fname);
5282 if (ufunc == NULL)
5283 semsg(_(e_cannot_find_function_str), name);
5284 return ufunc;
5285}
5286
5287/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005288 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005289 * be compiled or the one specified by the argument.
5290 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005291 */
5292 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005293ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005294{
Bram Moolenaar822ba242020-05-24 23:00:18 +02005295 ufunc_T *ufunc;
5296
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005297 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005298 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005299 compiletype_T compile_type = CT_NONE;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005300
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005301 ufunc = find_func_by_name(eap->arg, &compile_type);
5302 if (ufunc != NULL)
5303 {
5304 if (func_needs_compiling(ufunc, compile_type))
5305 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5306 else
5307 smsg(_("Function %s does not need compiling"), eap->arg);
5308 }
5309 }
5310 else
5311 {
5312 long todo = (long)func_hashtab.ht_used;
5313 int changed = func_hashtab.ht_changed;
5314 hashitem_T *hi;
5315
5316 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5317 {
5318 if (!HASHITEM_EMPTY(hi))
5319 {
5320 --todo;
5321 ufunc = HI2UF(hi);
5322 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5323 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5324 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005325 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005326 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5327
5328 if (func_hashtab.ht_changed != changed)
5329 {
5330 // a function has been added or removed, need to start
5331 // over
5332 todo = (long)func_hashtab.ht_used;
5333 changed = func_hashtab.ht_changed;
5334 hi = func_hashtab.ht_array;
5335 --hi;
5336 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005337 }
5338 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005339 }
5340 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005341}
5342
5343/*
5344 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5345 * Return 2 if "p" starts with "s:".
5346 * Return 0 otherwise.
5347 */
5348 int
5349eval_fname_script(char_u *p)
5350{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005351 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5352 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005353 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5354 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5355 return 5;
5356 if (p[0] == 's' && p[1] == ':')
5357 return 2;
5358 return 0;
5359}
5360
5361 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005362translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005363{
5364 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005365 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005366 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005367}
5368
5369/*
5370 * Return TRUE when "ufunc" has old-style "..." varargs
5371 * or named varargs "...name: type".
5372 */
5373 int
5374has_varargs(ufunc_T *ufunc)
5375{
5376 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005377}
5378
5379/*
5380 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005381 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005382 */
5383 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005384function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005385{
5386 char_u *nm = name;
5387 char_u *p;
5388 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005389 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005390 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005391
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005392 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5393 if (no_deref)
5394 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005395 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005396 nm = skipwhite(nm);
5397
Bram Moolenaare38eab22019-12-05 21:50:01 +01005398 // Only accept "funcname", "funcname ", "funcname (..." and
5399 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005400 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005401 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005402 vim_free(p);
5403 return n;
5404}
5405
Bram Moolenaar113e1072019-01-20 15:30:40 +01005406#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005407 char_u *
5408get_expanded_name(char_u *name, int check)
5409{
5410 char_u *nm = name;
5411 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005412 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005413
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005414 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005415 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005416
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005417 if (p != NULL && *nm == NUL
5418 && (!check || translated_function_exists(p, is_global)))
5419 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005420
5421 vim_free(p);
5422 return NULL;
5423}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005424#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005425
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005426/*
5427 * Function given to ExpandGeneric() to obtain the list of user defined
5428 * function names.
5429 */
5430 char_u *
5431get_user_func_name(expand_T *xp, int idx)
5432{
5433 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005434 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005435 static hashitem_T *hi;
5436 ufunc_T *fp;
5437
5438 if (idx == 0)
5439 {
5440 done = 0;
5441 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005442 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005443 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005444 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005445 {
5446 if (done++ > 0)
5447 ++hi;
5448 while (HASHITEM_EMPTY(hi))
5449 ++hi;
5450 fp = HI2UF(hi);
5451
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005452 // don't show dead, dict and lambda functions
5453 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005454 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005455 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005456
5457 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005458 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005459
5460 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005461 if (xp->xp_context != EXPAND_USER_FUNC
5462 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005463 {
5464 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005465 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005466 STRCAT(IObuff, ")");
5467 }
5468 return IObuff;
5469 }
5470 return NULL;
5471}
5472
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005473/*
5474 * ":delfunction {name}"
5475 */
5476 void
5477ex_delfunction(exarg_T *eap)
5478{
5479 ufunc_T *fp = NULL;
5480 char_u *p;
5481 char_u *name;
5482 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005483 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005484
5485 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005486 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
5487 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005488 vim_free(fudi.fd_newkey);
5489 if (name == NULL)
5490 {
5491 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005492 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005493 return;
5494 }
5495 if (!ends_excmd(*skipwhite(p)))
5496 {
5497 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005498 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005499 return;
5500 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005501 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005502 if (eap->nextcmd != NULL)
5503 *p = NUL;
5504
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005505 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005506 {
5507 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005508 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005509 vim_free(name);
5510 return;
5511 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005512 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005513 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005514 vim_free(name);
5515
5516 if (!eap->skip)
5517 {
5518 if (fp == NULL)
5519 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005520 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005521 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005522 return;
5523 }
5524 if (fp->uf_calls > 0)
5525 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005526 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005527 return;
5528 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005529 if (fp->uf_flags & FC_VIM9)
5530 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005531 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005532 return;
5533 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005534
5535 if (fudi.fd_dict != NULL)
5536 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005537 // Delete the dict item that refers to the function, it will
5538 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005539 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005540 }
5541 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005542 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005543 // A normal function (not a numbered function or lambda) has a
5544 // refcount of 1 for the entry in the hashtable. When deleting
5545 // it and the refcount is more than one, it should be kept.
5546 // A numbered function and lambda should be kept if the refcount is
5547 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005548 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005549 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005550 // Function is still referenced somewhere. Don't free it but
5551 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005552 if (func_remove(fp))
5553 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005554 }
5555 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005556 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005557 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005558 }
5559}
5560
5561/*
5562 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005563 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005564 */
5565 void
5566func_unref(char_u *name)
5567{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005568 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005569
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005570 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005571 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005572 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005573 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005574 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005575#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005576 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005577#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005578 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005579 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005580 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005581}
5582
5583/*
5584 * Unreference a Function: decrement the reference count and free it when it
5585 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005586 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005587 */
5588 void
5589func_ptr_unref(ufunc_T *fp)
5590{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005591 if (fp != NULL && (--fp->uf_refcount <= 0
5592 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5593 && fp->uf_partial->pt_refcount <= 1
5594 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005595 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005596 // Only delete it when it's not being used. Otherwise it's done
5597 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005598 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005599 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005600 }
5601}
5602
5603/*
5604 * Count a reference to a Function.
5605 */
5606 void
5607func_ref(char_u *name)
5608{
5609 ufunc_T *fp;
5610
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005611 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005612 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005613 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005614 if (fp != NULL)
5615 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005616 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005617 // Only give an error for a numbered function.
5618 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005619 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005620}
5621
5622/*
5623 * Count a reference to a Function.
5624 */
5625 void
5626func_ptr_ref(ufunc_T *fp)
5627{
5628 if (fp != NULL)
5629 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005630}
5631
5632/*
5633 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5634 * referenced from anywhere that is in use.
5635 */
5636 static int
5637can_free_funccal(funccall_T *fc, int copyID)
5638{
Bram Moolenaarca16c602022-09-06 18:57:08 +01005639 return (fc->fc_l_varlist.lv_copyID != copyID
5640 && fc->fc_l_vars.dv_copyID != copyID
5641 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005642 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005643}
5644
5645/*
5646 * ":return [expr]"
5647 */
5648 void
5649ex_return(exarg_T *eap)
5650{
5651 char_u *arg = eap->arg;
5652 typval_T rettv;
5653 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005654 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005655
5656 if (current_funccal == NULL)
5657 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005658 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005659 return;
5660 }
5661
Bram Moolenaar844fb642021-10-23 13:32:30 +01005662 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005663 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5664
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005665 if (eap->skip)
5666 ++emsg_skip;
5667
5668 eap->nextcmd = NULL;
5669 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005670 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005671 {
5672 if (!eap->skip)
5673 returning = do_return(eap, FALSE, TRUE, &rettv);
5674 else
5675 clear_tv(&rettv);
5676 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005677 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005678 else if (!eap->skip)
5679 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005680 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005681 update_force_abort();
5682
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005683 /*
5684 * Return unless the expression evaluation has been cancelled due to an
5685 * aborting error, an interrupt, or an exception.
5686 */
5687 if (!aborting())
5688 returning = do_return(eap, FALSE, TRUE, NULL);
5689 }
5690
Bram Moolenaare38eab22019-12-05 21:50:01 +01005691 // When skipping or the return gets pending, advance to the next command
5692 // in this line (!returning). Otherwise, ignore the rest of the line.
5693 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005694 if (returning)
5695 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005696 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005697 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005698
5699 if (eap->skip)
5700 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005701 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005702}
5703
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005704 static int
5705ex_call_inner(
5706 exarg_T *eap,
5707 char_u *name,
5708 char_u **arg,
5709 char_u *startarg,
5710 funcexe_T *funcexe_init,
5711 evalarg_T *evalarg)
5712{
5713 linenr_T lnum;
5714 int doesrange;
5715 typval_T rettv;
5716 int failed = FALSE;
5717
5718 /*
5719 * When skipping, evaluate the function once, to find the end of the
5720 * arguments.
5721 * When the function takes a range, this is discovered after the first
5722 * call, and the loop is broken.
5723 */
5724 if (eap->skip)
5725 {
5726 ++emsg_skip;
5727 lnum = eap->line2; // do it once, also with an invalid range
5728 }
5729 else
5730 lnum = eap->line1;
5731 for ( ; lnum <= eap->line2; ++lnum)
5732 {
5733 funcexe_T funcexe;
5734
5735 if (!eap->skip && eap->addr_count > 0)
5736 {
5737 if (lnum > curbuf->b_ml.ml_line_count)
5738 {
5739 // If the function deleted lines or switched to another buffer
5740 // the line number may become invalid.
5741 emsg(_(e_invalid_range));
5742 break;
5743 }
5744 curwin->w_cursor.lnum = lnum;
5745 curwin->w_cursor.col = 0;
5746 curwin->w_cursor.coladd = 0;
5747 }
5748 *arg = startarg;
5749
5750 funcexe = *funcexe_init;
5751 funcexe.fe_doesrange = &doesrange;
5752 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
5753 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
5754 {
5755 failed = TRUE;
5756 break;
5757 }
5758 if (has_watchexpr())
5759 dbg_check_breakpoint(eap);
5760
5761 // Handle a function returning a Funcref, Dictionary or List.
5762 if (handle_subscript(arg, NULL, &rettv,
5763 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
5764 {
5765 failed = TRUE;
5766 break;
5767 }
5768
5769 clear_tv(&rettv);
5770 if (doesrange || eap->skip)
5771 break;
5772
5773 // Stop when immediately aborting on error, or when an interrupt
5774 // occurred or an exception was thrown but not caught.
5775 // get_func_tv() returned OK, so that the check for trailing
5776 // characters below is executed.
5777 if (aborting())
5778 break;
5779 }
5780 if (eap->skip)
5781 --emsg_skip;
5782 return failed;
5783}
5784
5785/*
5786 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
5787 * Returns FAIL or OK.
5788 */
5789 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01005790ex_defer_inner(
5791 char_u *name,
5792 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01005793 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01005794 partial_T *partial,
5795 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005796{
5797 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01005798 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005799 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01005800 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005801
5802 if (current_funccal == NULL)
5803 {
5804 semsg(_(e_str_not_inside_function), "defer");
5805 return FAIL;
5806 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01005807 if (partial != NULL)
5808 {
5809 if (partial->pt_dict != NULL)
5810 {
5811 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
5812 return FAIL;
5813 }
5814 if (partial->pt_argc > 0)
5815 {
5816 int i;
5817
5818 partial_argc = partial->pt_argc;
5819 for (i = 0; i < partial_argc; ++i)
5820 copy_tv(&partial->pt_argv[i], &argvars[i]);
5821 }
5822 }
5823 r = get_func_arguments(arg, evalarg, FALSE,
5824 argvars + partial_argc, &argcount);
5825 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01005826
5827 if (r == OK)
5828 {
5829 if (type != NULL)
5830 {
5831 // Check that the arguments are OK for the types of the funcref.
5832 r = check_argument_types(type, argvars, argcount, NULL, name);
5833 }
5834 else if (builtin_function(name, -1))
5835 {
5836 int idx = find_internal_func(name);
5837
5838 if (idx < 0)
5839 {
5840 emsg_funcname(e_unknown_function_str, name);
5841 r = FAIL;
5842 }
5843 else if (check_internal_func(idx, argcount) == -1)
5844 r = FAIL;
5845 }
5846 else
5847 {
5848 ufunc_T *ufunc = find_func(name, FALSE);
5849
5850 // we tolerate an unknown function here, it might be defined later
5851 if (ufunc != NULL)
5852 {
5853 int error = check_user_func_argcount(ufunc, argcount);
5854
5855 if (error != FCERR_UNKNOWN)
5856 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01005857 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01005858 r = FAIL;
5859 }
5860 }
5861 }
5862 }
5863
Bram Moolenaar86d87252022-09-05 21:21:25 +01005864 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01005865 {
5866 while (--argcount >= 0)
5867 clear_tv(&argvars[argcount]);
5868 return FAIL;
5869 }
5870 return add_defer(name, argcount, argvars);
5871}
5872
5873/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01005874 * Return TRUE if currently inside a function call.
5875 * Give an error message and return FALSE when not.
5876 */
5877 int
5878can_add_defer(void)
5879{
5880 if (!in_def_function() && get_current_funccal() == NULL)
5881 {
5882 semsg(_(e_str_not_inside_function), "defer");
5883 return FALSE;
5884 }
5885 return TRUE;
5886}
5887
5888/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005889 * Add a deferred call for "name" with arguments "argvars[argcount]".
5890 * Consumes "argvars[]".
5891 * Caller must check that in_def_function() returns TRUE or current_funccal is
5892 * not NULL.
5893 * Returns OK or FAIL.
5894 */
5895 int
5896add_defer(char_u *name, int argcount_arg, typval_T *argvars)
5897{
5898 char_u *saved_name = vim_strsave(name);
5899 int argcount = argcount_arg;
5900 defer_T *dr;
5901 int ret = FAIL;
5902
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005903 if (saved_name == NULL)
5904 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01005905 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005906 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01005907 if (add_defer_function(saved_name, argcount, argvars) == OK)
5908 argcount = 0;
5909 }
5910 else
5911 {
5912 if (current_funccal->fc_defer.ga_itemsize == 0)
5913 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
5914 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
5915 goto theend;
5916 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
5917 + current_funccal->fc_defer.ga_len++;
5918 dr->dr_name = saved_name;
5919 dr->dr_argcount = argcount;
5920 while (argcount > 0)
5921 {
5922 --argcount;
5923 dr->dr_argvars[argcount] = argvars[argcount];
5924 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005925 }
5926 ret = OK;
5927
5928theend:
5929 while (--argcount >= 0)
5930 clear_tv(&argvars[argcount]);
5931 return ret;
5932}
5933
5934/*
5935 * Invoked after a functions has finished: invoke ":defer" functions.
5936 */
Bram Moolenaar58779852022-09-06 18:31:14 +01005937 static void
5938handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005939{
5940 int idx;
5941
Bram Moolenaar58779852022-09-06 18:31:14 +01005942 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005943 {
5944 funcexe_T funcexe;
5945 typval_T rettv;
Bram Moolenaar58779852022-09-06 18:31:14 +01005946 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005947 int i;
5948
5949 CLEAR_FIELD(funcexe);
5950 funcexe.fe_evaluate = TRUE;
5951
5952 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
5953 call_func(dr->dr_name, -1, &rettv,
5954 dr->dr_argcount, dr->dr_argvars, &funcexe);
5955 clear_tv(&rettv);
5956 vim_free(dr->dr_name);
5957 for (i = dr->dr_argcount - 1; i >= 0; --i)
5958 clear_tv(&dr->dr_argvars[i]);
5959 }
Bram Moolenaar58779852022-09-06 18:31:14 +01005960 ga_clear(&funccal->fc_defer);
5961}
5962
5963/*
5964 * Called when exiting: call all defer functions.
5965 */
5966 void
5967invoke_all_defer(void)
5968{
5969 funccall_T *funccal;
5970
Bram Moolenaarca16c602022-09-06 18:57:08 +01005971 for (funccal = current_funccal; funccal != NULL;
5972 funccal = funccal->fc_caller)
Bram Moolenaar58779852022-09-06 18:31:14 +01005973 if (funccal->fc_ectx != NULL)
5974 {
5975 // :def function
5976 unwind_def_callstack(funccal->fc_ectx);
5977 may_invoke_defer_funcs(funccal->fc_ectx);
5978 }
5979 else
5980 {
5981 // legacy function
5982 handle_defer_one(funccal);
5983 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005984}
5985
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005986/*
5987 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005988 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005989 */
5990 void
5991ex_call(exarg_T *eap)
5992{
5993 char_u *arg = eap->arg;
5994 char_u *startarg;
5995 char_u *name;
5996 char_u *tofree;
5997 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005998 int failed = FALSE;
5999 funcdict_T fudi;
6000 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006001 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006002 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006003 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006004 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006005
Bram Moolenaare6b53242020-07-01 17:28:33 +02006006 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006007 if (eap->skip)
6008 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006009 typval_T rettv;
6010
Bram Moolenaare38eab22019-12-05 21:50:01 +01006011 // trans_function_name() doesn't work well when skipping, use eval0()
6012 // instead to skip to any following command, e.g. for:
6013 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006014 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006015 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006016 clear_tv(&rettv);
6017 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006018 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006019 return;
6020 }
6021
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006022 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006023 &fudi, &partial, vim9script ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006024 if (fudi.fd_newkey != NULL)
6025 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006026 // Still need to give an error message for missing key.
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006027 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006028 vim_free(fudi.fd_newkey);
6029 }
6030 if (tofree == NULL)
6031 return;
6032
Bram Moolenaare38eab22019-12-05 21:50:01 +01006033 // Increase refcount on dictionary, it could get deleted when evaluating
6034 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006035 if (fudi.fd_dict != NULL)
6036 ++fudi.fd_dict->dv_refcount;
6037
Bram Moolenaare38eab22019-12-05 21:50:01 +01006038 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6039 // contents. For VAR_PARTIAL get its partial, unless we already have one
6040 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006041 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006042 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006043 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006044 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006045
Bram Moolenaare38eab22019-12-05 21:50:01 +01006046 // Skip white space to allow ":call func ()". Not good, but required for
6047 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006048 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006049 if (*startarg != '(')
6050 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006051 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006052 goto end;
6053 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006054 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006055 {
6056 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6057 goto end;
6058 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006059
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006060 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006061 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006062 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006063 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006064 }
6065 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006066 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006067 funcexe_T funcexe;
6068
Bram Moolenaara80faa82020-04-12 19:37:17 +02006069 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006070 funcexe.fe_check_type = type;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006071 funcexe.fe_partial = partial;
6072 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006073 funcexe.fe_firstline = eap->line1;
6074 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006075 funcexe.fe_found_var = found_var;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006076 funcexe.fe_evaluate = !eap->skip;
6077 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006078 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006079
Bram Moolenaar1d341892021-09-18 15:25:52 +02006080 // When inside :try we need to check for following "| catch" or "| endtry".
6081 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006082 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006083 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006084 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006085 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006086 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006087 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006088 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006089 {
6090 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006091 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006092 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006093 }
6094 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006095 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006096 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006097 // Must be after using "arg", it may point into memory cleared here.
6098 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006099
6100end:
6101 dict_unref(fudi.fd_dict);
6102 vim_free(tofree);
6103}
6104
6105/*
6106 * Return from a function. Possibly makes the return pending. Also called
6107 * for a pending return at the ":endtry" or after returning from an extra
6108 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6109 * when called due to a ":return" command. "rettv" may point to a typval_T
6110 * with the return rettv. Returns TRUE when the return can be carried out,
6111 * FALSE when the return gets pending.
6112 */
6113 int
6114do_return(
6115 exarg_T *eap,
6116 int reanimate,
6117 int is_cmd,
6118 void *rettv)
6119{
6120 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006121 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006122
6123 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006124 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006125 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006126
6127 /*
6128 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6129 * not in its finally clause (which then is to be executed next) is found.
6130 * In this case, make the ":return" pending for execution at the ":endtry".
6131 * Otherwise, return normally.
6132 */
6133 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6134 if (idx >= 0)
6135 {
6136 cstack->cs_pending[idx] = CSTP_RETURN;
6137
6138 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006139 // A pending return again gets pending. "rettv" points to an
6140 // allocated variable with the rettv of the original ":return"'s
6141 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006142 cstack->cs_rettv[idx] = rettv;
6143 else
6144 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006145 // When undoing a return in order to make it pending, get the stored
6146 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006147 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006148 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006149
6150 if (rettv != NULL)
6151 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006152 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006153 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6154 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6155 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006156 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006157 }
6158 else
6159 cstack->cs_rettv[idx] = NULL;
6160
6161 if (reanimate)
6162 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006163 // The pending return value could be overwritten by a ":return"
6164 // without argument in a finally clause; reset the default
6165 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006166 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6167 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006168 }
6169 }
6170 report_make_pending(CSTP_RETURN, rettv);
6171 }
6172 else
6173 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006174 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006175
Bram Moolenaare38eab22019-12-05 21:50:01 +01006176 // If the return is carried out now, store the return value. For
6177 // a return immediately after reanimation, the value is already
6178 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006179 if (!reanimate && rettv != NULL)
6180 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006181 clear_tv(current_funccal->fc_rettv);
6182 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006183 if (!is_cmd)
6184 vim_free(rettv);
6185 }
6186 }
6187
6188 return idx < 0;
6189}
6190
6191/*
6192 * Free the variable with a pending return value.
6193 */
6194 void
6195discard_pending_return(void *rettv)
6196{
6197 free_tv((typval_T *)rettv);
6198}
6199
6200/*
6201 * Generate a return command for producing the value of "rettv". The result
6202 * is an allocated string. Used by report_pending() for verbose messages.
6203 */
6204 char_u *
6205get_return_cmd(void *rettv)
6206{
6207 char_u *s = NULL;
6208 char_u *tofree = NULL;
6209 char_u numbuf[NUMBUFLEN];
6210
6211 if (rettv != NULL)
6212 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6213 if (s == NULL)
6214 s = (char_u *)"";
6215
6216 STRCPY(IObuff, ":return ");
6217 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6218 if (STRLEN(s) + 8 >= IOSIZE)
6219 STRCPY(IObuff + IOSIZE - 4, "...");
6220 vim_free(tofree);
6221 return vim_strsave(IObuff);
6222}
6223
6224/*
6225 * Get next function line.
6226 * Called by do_cmdline() to get the next line.
6227 * Returns allocated string, or NULL for end of function.
6228 */
6229 char_u *
6230get_func_line(
6231 int c UNUSED,
6232 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006233 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006234 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006235{
6236 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006237 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006238 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006239 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006240
Bram Moolenaare38eab22019-12-05 21:50:01 +01006241 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006242 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006243 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006244 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006245 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006246 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006247 }
6248#ifdef FEAT_PROFILE
6249 if (do_profiling == PROF_YES)
6250 func_line_end(cookie);
6251#endif
6252
6253 gap = &fp->uf_lines;
6254 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006255 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006256 retval = NULL;
6257 else
6258 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006259 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006260 while (fcp->fc_linenr < gap->ga_len
6261 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6262 ++fcp->fc_linenr;
6263 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006264 retval = NULL;
6265 else
6266 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006267 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6268 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006269#ifdef FEAT_PROFILE
6270 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006271 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006272#endif
6273 }
6274 }
6275
Bram Moolenaare38eab22019-12-05 21:50:01 +01006276 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006277 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006278 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006279 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006280 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006281 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006282 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006283 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006284 }
6285
6286 return retval;
6287}
6288
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006289/*
6290 * Return TRUE if the currently active function should be ended, because a
6291 * return was encountered or an error occurred. Used inside a ":while".
6292 */
6293 int
6294func_has_ended(void *cookie)
6295{
6296 funccall_T *fcp = (funccall_T *)cookie;
6297
Bram Moolenaare38eab22019-12-05 21:50:01 +01006298 // Ignore the "abort" flag if the abortion behavior has been changed due to
6299 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006300 return (((fcp->fc_func->uf_flags & FC_ABORT)
6301 && did_emsg && !aborted_in_try())
6302 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006303}
6304
6305/*
6306 * return TRUE if cookie indicates a function which "abort"s on errors.
6307 */
6308 int
6309func_has_abort(
6310 void *cookie)
6311{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006312 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006313}
6314
6315
6316/*
6317 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6318 * Don't do this when "Func" is already a partial that was bound
6319 * explicitly (pt_auto is FALSE).
6320 * Changes "rettv" in-place.
6321 * Returns the updated "selfdict_in".
6322 */
6323 dict_T *
6324make_partial(dict_T *selfdict_in, typval_T *rettv)
6325{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006326 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006327 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006328 char_u fname_buf[FLEN_FIXED + 1];
6329 int error;
6330 dict_T *selfdict = selfdict_in;
6331
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006332 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6333 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006334 fp = rettv->vval.v_partial->pt_func;
6335 else
6336 {
6337 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006338 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006339 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006340 if (fname == NULL)
6341 {
6342 // There is no point binding a dict to a NULL function, just create
6343 // a function reference.
6344 rettv->v_type = VAR_FUNC;
6345 rettv->vval.v_string = NULL;
6346 }
6347 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006348 {
6349 char_u *tofree = NULL;
6350
6351 // Translate "s:func" to the stored function name.
6352 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6353 fp = find_func(fname, FALSE);
6354 vim_free(tofree);
6355 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006356 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006357
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006358 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006359 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006360 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006361
6362 if (pt != NULL)
6363 {
6364 pt->pt_refcount = 1;
6365 pt->pt_dict = selfdict;
6366 pt->pt_auto = TRUE;
6367 selfdict = NULL;
6368 if (rettv->v_type == VAR_FUNC)
6369 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006370 // Just a function: Take over the function name and use
6371 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006372 pt->pt_name = rettv->vval.v_string;
6373 }
6374 else
6375 {
6376 partial_T *ret_pt = rettv->vval.v_partial;
6377 int i;
6378
Bram Moolenaare38eab22019-12-05 21:50:01 +01006379 // Partial: copy the function name, use selfdict and copy
6380 // args. Can't take over name or args, the partial might
6381 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006382 if (ret_pt->pt_name != NULL)
6383 {
6384 pt->pt_name = vim_strsave(ret_pt->pt_name);
6385 func_ref(pt->pt_name);
6386 }
6387 else
6388 {
6389 pt->pt_func = ret_pt->pt_func;
6390 func_ptr_ref(pt->pt_func);
6391 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006392 if (ret_pt->pt_argc > 0)
6393 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006394 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006395 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006396 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006397 pt->pt_argc = 0;
6398 else
6399 {
6400 pt->pt_argc = ret_pt->pt_argc;
6401 for (i = 0; i < pt->pt_argc; i++)
6402 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6403 }
6404 }
6405 partial_unref(ret_pt);
6406 }
6407 rettv->v_type = VAR_PARTIAL;
6408 rettv->vval.v_partial = pt;
6409 }
6410 }
6411 return selfdict;
6412}
6413
6414/*
6415 * Return the name of the executed function.
6416 */
6417 char_u *
6418func_name(void *cookie)
6419{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006420 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006421}
6422
6423/*
6424 * Return the address holding the next breakpoint line for a funccall cookie.
6425 */
6426 linenr_T *
6427func_breakpoint(void *cookie)
6428{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006429 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006430}
6431
6432/*
6433 * Return the address holding the debug tick for a funccall cookie.
6434 */
6435 int *
6436func_dbg_tick(void *cookie)
6437{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006438 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006439}
6440
6441/*
6442 * Return the nesting level for a funccall cookie.
6443 */
6444 int
6445func_level(void *cookie)
6446{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006447 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006448}
6449
6450/*
6451 * Return TRUE when a function was ended by a ":return" command.
6452 */
6453 int
6454current_func_returned(void)
6455{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006456 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006457}
6458
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006459 int
6460free_unref_funccal(int copyID, int testing)
6461{
6462 int did_free = FALSE;
6463 int did_free_funccal = FALSE;
6464 funccall_T *fc, **pfc;
6465
6466 for (pfc = &previous_funccal; *pfc != NULL; )
6467 {
6468 if (can_free_funccal(*pfc, copyID))
6469 {
6470 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006471 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006472 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006473 did_free = TRUE;
6474 did_free_funccal = TRUE;
6475 }
6476 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006477 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006478 }
6479 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006480 // When a funccal was freed some more items might be garbage
6481 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006482 (void)garbage_collect(testing);
6483
6484 return did_free;
6485}
6486
6487/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006488 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006489 */
6490 static funccall_T *
6491get_funccal(void)
6492{
6493 int i;
6494 funccall_T *funccal;
6495 funccall_T *temp_funccal;
6496
6497 funccal = current_funccal;
6498 if (debug_backtrace_level > 0)
6499 {
6500 for (i = 0; i < debug_backtrace_level; i++)
6501 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006502 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006503 if (temp_funccal)
6504 funccal = temp_funccal;
6505 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006506 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006507 debug_backtrace_level = i;
6508 }
6509 }
6510 return funccal;
6511}
6512
6513/*
6514 * Return the hashtable used for local variables in the current funccal.
6515 * Return NULL if there is no current funccal.
6516 */
6517 hashtab_T *
6518get_funccal_local_ht()
6519{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006520 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006521 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006522 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006523}
6524
6525/*
6526 * Return the l: scope variable.
6527 * Return NULL if there is no current funccal.
6528 */
6529 dictitem_T *
6530get_funccal_local_var()
6531{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006532 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006533 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006534 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006535}
6536
6537/*
6538 * Return the hashtable used for argument in the current funccal.
6539 * Return NULL if there is no current funccal.
6540 */
6541 hashtab_T *
6542get_funccal_args_ht()
6543{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006544 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006545 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006546 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006547}
6548
6549/*
6550 * Return the a: scope variable.
6551 * Return NULL if there is no current funccal.
6552 */
6553 dictitem_T *
6554get_funccal_args_var()
6555{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006556 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006557 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006558 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006559}
6560
6561/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006562 * List function variables, if there is a function.
6563 */
6564 void
6565list_func_vars(int *first)
6566{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006567 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6568 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006569 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006570}
6571
6572/*
6573 * If "ht" is the hashtable for local variables in the current funccal, return
6574 * the dict that contains it.
6575 * Otherwise return NULL.
6576 */
6577 dict_T *
6578get_current_funccal_dict(hashtab_T *ht)
6579{
6580 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006581 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6582 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006583 return NULL;
6584}
6585
6586/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006587 * Search hashitem in parent scope.
6588 */
6589 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006590find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006591{
6592 funccall_T *old_current_funccal = current_funccal;
6593 hashtab_T *ht;
6594 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006595 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006596
Bram Moolenaarca16c602022-09-06 18:57:08 +01006597 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006598 return NULL;
6599
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006600 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006601 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006602 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006603 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006604 ht = find_var_ht(name, &varname);
6605 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006606 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006607 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006608 if (!HASHITEM_EMPTY(hi))
6609 {
6610 *pht = ht;
6611 break;
6612 }
6613 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006614 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006615 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006616 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006617 }
6618 current_funccal = old_current_funccal;
6619
6620 return hi;
6621}
6622
6623/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006624 * Search variable in parent scope.
6625 */
6626 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006627find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006628{
6629 dictitem_T *v = NULL;
6630 funccall_T *old_current_funccal = current_funccal;
6631 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006632 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006633
Bram Moolenaarca16c602022-09-06 18:57:08 +01006634 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006635 return NULL;
6636
Bram Moolenaare38eab22019-12-05 21:50:01 +01006637 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01006638 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006639 while (current_funccal)
6640 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006641 ht = find_var_ht(name, &varname);
6642 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006643 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006644 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006645 if (v != NULL)
6646 break;
6647 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006648 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006649 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006650 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006651 }
6652 current_funccal = old_current_funccal;
6653
6654 return v;
6655}
6656
6657/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006658 * Set "copyID + 1" in previous_funccal and callers.
6659 */
6660 int
6661set_ref_in_previous_funccal(int copyID)
6662{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006663 funccall_T *fc;
6664
Bram Moolenaarca16c602022-09-06 18:57:08 +01006665 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006666 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006667 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006668 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
6669 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
6670 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006671 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006672 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006673 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006674}
6675
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006676 static int
6677set_ref_in_funccal(funccall_T *fc, int copyID)
6678{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006679 if (fc->fc_copyID != copyID)
6680 {
6681 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006682 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
6683 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
6684 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
6685 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006686 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006687 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006688 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006689}
6690
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006691/*
6692 * Set "copyID" in all local vars and arguments in the call stack.
6693 */
6694 int
6695set_ref_in_call_stack(int copyID)
6696{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006697 funccall_T *fc;
6698 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006699
Bram Moolenaarca16c602022-09-06 18:57:08 +01006700 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006701 if (set_ref_in_funccal(fc, copyID))
6702 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006703
6704 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006705 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006706 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006707 if (set_ref_in_funccal(fc, copyID))
6708 return TRUE;
6709 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006710}
6711
6712/*
6713 * Set "copyID" in all functions available by name.
6714 */
6715 int
6716set_ref_in_functions(int copyID)
6717{
6718 int todo;
6719 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006720 ufunc_T *fp;
6721
6722 todo = (int)func_hashtab.ht_used;
6723 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006724 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006725 if (!HASHITEM_EMPTY(hi))
6726 {
6727 --todo;
6728 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006729 if (!func_name_refcount(fp->uf_name)
6730 && set_ref_in_func(NULL, fp, copyID))
6731 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006732 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006733 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006734 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006735}
6736
6737/*
6738 * Set "copyID" in all function arguments.
6739 */
6740 int
6741set_ref_in_func_args(int copyID)
6742{
6743 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006744
6745 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006746 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
6747 copyID, NULL, NULL))
6748 return TRUE;
6749 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006750}
6751
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006752/*
6753 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006754 * Returns TRUE if setting references failed somehow.
6755 */
6756 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006757set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006758{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006759 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006760 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01006761 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006762 char_u fname_buf[FLEN_FIXED + 1];
6763 char_u *tofree = NULL;
6764 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006765 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006766
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006767 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006768 return FALSE;
6769
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006770 if (fp_in == NULL)
6771 {
6772 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006773 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006774 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006775 if (fp != NULL)
6776 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006777 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006778 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006779 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02006780
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006781 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006782 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006783}
6784
Bram Moolenaare38eab22019-12-05 21:50:01 +01006785#endif // FEAT_EVAL