blob: 5db0b709c2a67493c2a9b1bc839695d56060865e [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
191 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
192 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000193 *eap->cmdlinep = theline;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000194 ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000195 }
196
197 return theline;
198}
199
200/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200201 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100202 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100203 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200204 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100205 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200206get_function_args(
207 char_u **argp,
208 char_u endchar,
209 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100210 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100211 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100212 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200213 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200214 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200215 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000216 exarg_T *eap, // can be NULL
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000217 class_T *class_arg,
218 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000219 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200220{
221 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100222 char_u *arg;
223 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200224 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200225 int any_default = FALSE;
226 char_u *expr;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100227 char_u *whitep = *argp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200228
229 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000230 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100231 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000232 ga_init2(argtypes, sizeof(char_u *), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200233 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000234 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200235
236 if (varargs != NULL)
237 *varargs = FALSE;
238
239 /*
240 * Isolate the arguments: "arg1, arg2, ...)"
241 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100242 arg = skipwhite(*argp);
243 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200244 while (*p != endchar)
245 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200246 while (eap != NULL && eap->getline != NULL
247 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200248 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200249 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000250 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000251 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000252
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200253 if (theline == NULL)
254 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200255 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200256 p = skipwhite(theline);
257 }
258
259 if (mustend && *p != endchar)
260 {
261 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000262 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100263 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200264 }
265 if (*p == endchar)
266 break;
267
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200268 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
269 {
270 if (varargs != NULL)
271 *varargs = TRUE;
272 p += 3;
273 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274
275 if (argtypes != NULL)
276 {
277 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200278 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100280 if (!skip)
281 emsg(_(e_missing_name_after_dots));
282 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100283 }
284
285 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100286 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaardce24412022-02-08 20:35:30 +0000287 evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100288 if (p == arg)
289 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100290 if (*skipwhite(p) == '=')
291 {
292 emsg(_(e_cannot_use_default_for_variable_arguments));
293 break;
294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200296 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000297 else if (class_arg != NULL && STRNCMP(p, "this.", 5) == 0)
298 {
299 // this.memberName
300 p += 5;
301 arg = p;
302 while (ASCII_ISALNUM(*p) || *p == '_')
303 ++p;
304
305 // TODO: check the argument is indeed a member
306 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
307 return FAIL;
308 if (newargs != NULL)
309 {
310 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
311 vim_strnsave(arg, p - arg);
312 newargs->ga_len++;
313
314 if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
315 {
316 // TODO: use the actual type
317 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
318 vim_strsave((char_u *)"any");
319
320 // Add a line to the function body for the assignment.
321 if (ga_grow(newlines, 1) == OK)
322 {
323 // "this.name = name"
324 int len = 5 + (p - arg) + 3 + (p - arg) + 1;
325 char_u *assignment = alloc(len);
326 if (assignment != NULL)
327 {
328 c = *p;
329 *p = NUL;
330 vim_snprintf((char *)assignment, len,
331 "this.%s = %s", arg, arg);
332 *p = c;
333 ((char_u **)(newlines->ga_data))[
334 newlines->ga_len++] = assignment;
335 }
336 }
337 }
338 }
339 if (*p == ',')
340 ++p;
341 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200342 else
343 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200344 char_u *np;
345
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200346 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100347 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaardce24412022-02-08 20:35:30 +0000348 evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100349 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200350 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200351
Bram Moolenaar015cf102021-06-26 21:52:02 +0200352 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200353 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200354 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200355 if (*np == '=' && np[1] != '=' && np[1] != '~'
356 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200357 {
358 typval_T rettv;
359
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100360 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200361 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100362 p = skipwhite(np + 1);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200363 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200364 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200365 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200366 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200367 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200368 if (ga_grow(default_args, 1) == FAIL)
369 goto err_ret;
370
371 // trim trailing whitespace
372 while (p > expr && VIM_ISWHITE(p[-1]))
373 p--;
374 c = *p;
375 *p = NUL;
376 expr = vim_strsave(expr);
377 if (expr == NULL)
378 {
379 *p = c;
380 goto err_ret;
381 }
382 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200383 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200384 default_args->ga_len++;
385 *p = c;
386 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200387 }
388 else
389 mustend = TRUE;
390 }
391 else if (any_default)
392 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000393 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200394 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200395 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200396
397 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
398 {
399 // Be tolerant when skipping
400 if (!skip)
401 {
402 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
403 goto err_ret;
404 }
405 p = skipwhite(p);
406 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200407 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200408 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200409 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200410 // Don't give this error when skipping, it makes the "->" not
411 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100412 // Allow missing space after comma in legacy functions.
413 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200414 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
415 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100416 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200417 goto err_ret;
418 }
419 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200420 else
421 mustend = TRUE;
422 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200423 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200424 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200425 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200426
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200427 if (*p != endchar)
428 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100429 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200430
431 *argp = p;
432 return OK;
433
434err_ret:
435 if (newargs != NULL)
436 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200437 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200438 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200439 return FAIL;
440}
441
442/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100443 * Parse the argument types, filling "fp->uf_arg_types".
444 * Return OK or FAIL.
445 */
446 static int
447parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
448{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200449 int len = 0;
450
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100451 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
452 if (argtypes->ga_len > 0)
453 {
454 // When "varargs" is set the last name/type goes into uf_va_name
455 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200456 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100457
458 if (len > 0)
459 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
460 if (fp->uf_arg_types != NULL)
461 {
462 int i;
463 type_T *type;
464
465 for (i = 0; i < len; ++ i)
466 {
467 char_u *p = ((char_u **)argtypes->ga_data)[i];
468
469 if (p == NULL)
470 // will get the type from the default value
471 type = &t_unknown;
472 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100473 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100474 if (type == NULL)
475 return FAIL;
476 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000477 if (i < fp->uf_args.ga_len
478 && (type->tt_type == VAR_FUNC
479 || type->tt_type == VAR_PARTIAL)
480 && var_wrong_func_name(
481 ((char_u **)fp->uf_args.ga_data)[i], TRUE))
482 return FAIL;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100483 }
484 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100485 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200486
487 if (varargs)
488 {
489 char_u *p;
490
491 // Move the last argument "...name: type" to uf_va_name and
492 // uf_va_type.
493 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
494 [fp->uf_args.ga_len - 1];
495 --fp->uf_args.ga_len;
496 p = ((char_u **)argtypes->ga_data)[len];
497 if (p == NULL)
498 // TODO: get type from default value
499 fp->uf_va_type = &t_list_any;
500 else
501 {
502 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
503 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
504 {
505 semsg(_(e_variable_arguments_type_must_be_list_str),
506 ((char_u **)argtypes->ga_data)[len]);
507 return FAIL;
508 }
509 }
510 if (fp->uf_va_type == NULL)
511 return FAIL;
512 }
513
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100514 return OK;
515}
516
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100517 static int
518parse_return_type(ufunc_T *fp, char_u *ret_type)
519{
520 if (ret_type == NULL)
521 fp->uf_ret_type = &t_void;
522 else
523 {
524 char_u *p = ret_type;
525
526 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
527 if (fp->uf_ret_type == NULL)
528 {
529 fp->uf_ret_type = &t_void;
530 return FAIL;
531 }
532 }
533 return OK;
534}
535
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100536/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200537 * Register function "fp" as using "current_funccal" as its scope.
538 */
539 static int
540register_closure(ufunc_T *fp)
541{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200542 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100543 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200544 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200545 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200546 fp->uf_scoped = current_funccal;
547 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200548
Bram Moolenaarca16c602022-09-06 18:57:08 +0100549 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200550 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100551 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
552 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200553 return OK;
554}
555
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100556 static void
557set_ufunc_name(ufunc_T *fp, char_u *name)
558{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100559 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
560 // actually extends beyond the struct.
561 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100562
563 if (name[0] == K_SPECIAL)
564 {
565 fp->uf_name_exp = alloc(STRLEN(name) + 3);
566 if (fp->uf_name_exp != NULL)
567 {
568 STRCPY(fp->uf_name_exp, "<SNR>");
569 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
570 }
571 }
572}
573
Bram Moolenaar58016442016-07-31 18:30:22 +0200574/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100575 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
576 * return "buf" filled with a readable function name.
577 * Otherwise just return "name", thus the return value can always be used.
578 * "name" and "buf" may be equal.
579 */
580 char_u *
581make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
582{
583 size_t len;
584
585 if (name[0] != K_SPECIAL)
586 return name;
587 len = STRLEN(name);
588 if (len + 3 > bufsize)
589 return name;
590
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100591 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100592 mch_memmove(buf, "<SNR>", 5);
593 return buf;
594}
595
596/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200597 * Get a name for a lambda. Returned in static memory.
598 */
599 char_u *
600get_lambda_name(void)
601{
602 static char_u name[30];
603 static int lambda_no = 0;
604
605 sprintf((char*)name, "<lambda>%d", ++lambda_no);
606 return name;
607}
608
Bram Moolenaar801ab062020-06-25 19:27:56 +0200609#if defined(FEAT_LUA) || defined(PROTO)
610/*
611 * Registers a native C callback which can be called from Vim script.
612 * Returns the name of the Vim script function.
613 */
614 char_u *
615register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
616{
617 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200618 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200619
620 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
621 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200622 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200623
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200624 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200625 fp->uf_refcount = 1;
626 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000627 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200628 fp->uf_calls = 0;
629 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200630 fp->uf_cb = cb;
631 fp->uf_cb_free = cb_free;
632 fp->uf_cb_state = state;
633
634 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000635 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200636
637 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200638}
639#endif
640
Bram Moolenaar04b12692020-05-04 23:24:44 +0200641/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100642 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100643 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100644 * If "white_error" is not NULL check for correct use of white space and set
645 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100646 * Return NULL if no valid arrow found.
647 */
648 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100649skip_arrow(
650 char_u *start,
651 int equal_arrow,
652 char_u **ret_type,
653 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100654{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100655 char_u *s = start;
656 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100657
658 if (equal_arrow)
659 {
660 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100661 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100662 if (white_error != NULL && !VIM_ISWHITE(s[1]))
663 {
664 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100665 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100666 return NULL;
667 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100668 s = skipwhite(s + 1);
669 *ret_type = s;
670 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100671 if (s == *ret_type)
672 {
673 emsg(_(e_missing_return_type));
674 return NULL;
675 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100676 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100677 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100678 s = skipwhite(s);
679 if (*s != '=')
680 return NULL;
681 ++s;
682 }
683 if (*s != '>')
684 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100685 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
686 || !IS_WHITE_OR_NUL(s[1])))
687 {
688 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100689 semsg(_(e_white_space_required_before_and_after_str_at_str),
690 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100691 return NULL;
692 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100693 return skipwhite(s + 1);
694}
695
696/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100697 * Check if "*cmd" points to a function command and if so advance "*cmd" and
698 * return TRUE.
699 * Otherwise return FALSE;
700 * Do not consider "function(" to be a command.
701 */
702 static int
703is_function_cmd(char_u **cmd)
704{
705 char_u *p = *cmd;
706
707 if (checkforcmd(&p, "function", 2))
708 {
709 if (*p == '(')
710 return FALSE;
711 *cmd = p;
712 return TRUE;
713 }
714 return FALSE;
715}
716
717/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200718 * Called when defining a function: The context may be needed for script
719 * variables declared in a block that is visible now but not when the function
720 * is compiled or called later.
721 */
722 static void
723function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
724{
725 if (cstack != NULL && cstack->cs_idx >= 0)
726 {
727 int count = cstack->cs_idx + 1;
728 int i;
729
730 fp->uf_block_ids = ALLOC_MULT(int, count);
731 if (fp->uf_block_ids != NULL)
732 {
733 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
734 sizeof(int) * count);
735 fp->uf_block_depth = count;
736 }
737
738 // Set flag in each block to indicate a function was defined. This
739 // is used to keep the variable when leaving the block, see
740 // hide_script_var().
741 for (i = 0; i <= cstack->cs_idx; ++i)
742 cstack->cs_flags[i] |= CSF_FUNC_DEF;
743 }
744}
745
746/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100747 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200748 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100749 * "newlines" must already have been initialized.
750 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
751 */
752 static int
753get_function_body(
754 exarg_T *eap,
755 garray_T *newlines,
756 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000757 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100758{
759 linenr_T sourcing_lnum_top = SOURCING_LNUM;
760 linenr_T sourcing_lnum_off;
761 int saved_wait_return = need_wait_return;
762 char_u *line_arg = line_arg_in;
763 int vim9_function = eap->cmdidx == CMD_def
764 || eap->cmdidx == CMD_block;
765#define MAX_FUNC_NESTING 50
766 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200767 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100768 int nesting = 0;
769 getline_opt_T getline_options;
770 int indent = 2;
771 char_u *skip_until = NULL;
772 int ret = FAIL;
773 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200774 int heredoc_concat_len = 0;
775 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100776 char_u *heredoc_trimmed = NULL;
777
Bram Moolenaar20677332021-06-06 17:02:53 +0200778 ga_init2(&heredoc_ga, 1, 500);
779
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100780 // Detect having skipped over comment lines to find the return
781 // type. Add NULL lines to keep the line count correct.
782 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
783 if (SOURCING_LNUM < sourcing_lnum_off)
784 {
785 sourcing_lnum_off -= SOURCING_LNUM;
786 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
787 goto theend;
788 while (sourcing_lnum_off-- > 0)
789 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
790 }
791
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200792 nesting_def[0] = vim9_function;
793 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100794 getline_options = vim9_function
795 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
796 for (;;)
797 {
798 char_u *theline;
799 char_u *p;
800 char_u *arg;
801
802 if (KeyTyped)
803 {
804 msg_scroll = TRUE;
805 saved_wait_return = FALSE;
806 }
807 need_wait_return = FALSE;
808
809 if (line_arg != NULL)
810 {
811 // Use eap->arg, split up in parts by line breaks.
812 theline = line_arg;
813 p = vim_strchr(theline, '\n');
814 if (p == NULL)
815 line_arg += STRLEN(line_arg);
816 else
817 {
818 *p = NUL;
819 line_arg = p + 1;
820 }
821 }
822 else
823 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000824 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100825 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100826 }
827 if (KeyTyped)
828 lines_left = Rows - 1;
829 if (theline == NULL)
830 {
831 // Use the start of the function for the line number.
832 SOURCING_LNUM = sourcing_lnum_top;
833 if (skip_until != NULL)
834 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200835 else if (nesting_inline[nesting])
836 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100837 else if (eap->cmdidx == CMD_def)
838 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100839 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000840 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100841 goto theend;
842 }
843
844 // Detect line continuation: SOURCING_LNUM increased more than one.
845 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
846 if (SOURCING_LNUM < sourcing_lnum_off)
847 sourcing_lnum_off -= SOURCING_LNUM;
848 else
849 sourcing_lnum_off = 0;
850
851 if (skip_until != NULL)
852 {
853 // Don't check for ":endfunc"/":enddef" between
854 // * ":append" and "."
855 // * ":python <<EOF" and "EOF"
856 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
857 if (heredoc_trimmed == NULL
858 || (is_heredoc && skipwhite(theline) == theline)
859 || STRNCMP(theline, heredoc_trimmed,
860 STRLEN(heredoc_trimmed)) == 0)
861 {
862 if (heredoc_trimmed == NULL)
863 p = theline;
864 else if (is_heredoc)
865 p = skipwhite(theline) == theline
866 ? theline : theline + STRLEN(heredoc_trimmed);
867 else
868 p = theline + STRLEN(heredoc_trimmed);
869 if (STRCMP(p, skip_until) == 0)
870 {
871 VIM_CLEAR(skip_until);
872 VIM_CLEAR(heredoc_trimmed);
873 getline_options = vim9_function
874 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
875 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200876
877 if (heredoc_concat_len > 0)
878 {
879 // Replace the starting line with all the concatenated
880 // lines.
881 ga_concat(&heredoc_ga, theline);
882 vim_free(((char_u **)(newlines->ga_data))[
883 heredoc_concat_len - 1]);
884 ((char_u **)(newlines->ga_data))[
885 heredoc_concat_len - 1] = heredoc_ga.ga_data;
886 ga_init(&heredoc_ga);
887 heredoc_concat_len = 0;
888 theline += STRLEN(theline); // skip the "EOF"
889 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100890 }
891 }
892 }
893 else
894 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200895 int c;
896 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +0000897 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100898
899 // skip ':' and blanks
900 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
901 ;
902
903 // Check for "endfunction", "enddef" or "}".
904 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +0000905 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200906 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100907 ? *p == '}'
908 : (checkforcmd(&p, nesting_def[nesting]
909 ? "enddef" : "endfunction", 4)
910 && *p != ':'))
911 {
Bram Moolenaarb2175222022-03-05 20:24:41 +0000912 if (!nesting_inline[nesting] && nesting_def[nesting]
913 && p < cmd + 6)
914 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100915 if (nesting-- == 0)
916 {
917 char_u *nextcmd = NULL;
918
919 if (*p == '|' || *p == '}')
920 nextcmd = p + 1;
921 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
922 nextcmd = line_arg;
923 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100924 && (vim9_function || p_verbose > 0))
925 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200926 SOURCING_LNUM = sourcing_lnum_top
927 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100928 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000929 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100930 else
931 give_warning2((char_u *)
932 _("W22: Text found after :endfunction: %s"),
933 p, TRUE);
934 }
935 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100936 {
937 // Another command follows. If the line came from "eap"
938 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000939 // change "eap->cmdlinep" to point to the last fetched
940 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100941 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000942 if (lines_to_free->ga_len > 0
943 && *eap->cmdlinep !=
944 ((char_u **)lines_to_free->ga_data)
945 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100946 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000947 // *cmdlinep will be freed later, thus remove the
948 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100949 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000950 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
951 [lines_to_free->ga_len - 1];
952 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100953 }
954 }
955 break;
956 }
957 }
958
959 // Check for mismatched "endfunc" or "enddef".
960 // We don't check for "def" inside "func" thus we also can't check
961 // for "enddef".
962 // We continue to find the end of the function, although we might
963 // not find it.
964 else if (nesting_def[nesting])
965 {
966 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
967 emsg(_(e_mismatched_endfunction));
968 }
969 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
970 emsg(_(e_mismatched_enddef));
971
972 // Increase indent inside "if", "while", "for" and "try", decrease
973 // at "end".
974 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
975 indent -= 2;
976 else if (STRNCMP(p, "if", 2) == 0
977 || STRNCMP(p, "wh", 2) == 0
978 || STRNCMP(p, "for", 3) == 0
979 || STRNCMP(p, "try", 3) == 0)
980 indent += 2;
981
982 // Check for defining a function inside this function.
983 // Only recognize "def" inside "def", not inside "function",
984 // For backwards compatibility, see Test_function_python().
985 c = *p;
986 if (is_function_cmd(&p)
987 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
988 {
989 if (*p == '!')
990 p = skipwhite(p + 1);
991 p += eval_fname_script(p);
992 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
993 NULL, NULL));
994 if (*skipwhite(p) == '(')
995 {
996 if (nesting == MAX_FUNC_NESTING - 1)
997 emsg(_(e_function_nesting_too_deep));
998 else
999 {
1000 ++nesting;
1001 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001002 nesting_inline[nesting] = FALSE;
1003 indent += 2;
1004 }
1005 }
1006 }
1007
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001008 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001009 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001010 // Not a comment line: check for nested inline function.
1011 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001012 while (end > p && VIM_ISWHITE(*end))
1013 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001014 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001015 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001016 int is_block;
1017
1018 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001019 --end;
1020 while (end > p && VIM_ISWHITE(*end))
1021 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001022 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1023 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001024 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001025 char_u *s = p;
1026
1027 // check for line starting with "au" for :autocmd or
1028 // "com" for :command, these can use a {} block
1029 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1030 || checkforcmd_noparen(&s, "command", 3);
1031 }
1032
1033 if (is_block)
1034 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001035 if (nesting == MAX_FUNC_NESTING - 1)
1036 emsg(_(e_function_nesting_too_deep));
1037 else
1038 {
1039 ++nesting;
1040 nesting_def[nesting] = TRUE;
1041 nesting_inline[nesting] = TRUE;
1042 indent += 2;
1043 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001044 }
1045 }
1046 }
1047
1048 // Check for ":append", ":change", ":insert". Not for :def.
1049 p = skip_range(p, FALSE, NULL);
1050 if (!vim9_function
1051 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1052 || (p[0] == 'c'
1053 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1054 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1055 && (STRNCMP(&p[3], "nge", 3) != 0
1056 || !ASCII_ISALPHA(p[6])))))))
1057 || (p[0] == 'i'
1058 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1059 && (!ASCII_ISALPHA(p[2])
1060 || (p[2] == 's'
1061 && (!ASCII_ISALPHA(p[3])
1062 || p[3] == 'e'))))))))
1063 skip_until = vim_strsave((char_u *)".");
1064
1065 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1066 arg = skipwhite(skiptowhite(p));
1067 if (arg[0] == '<' && arg[1] =='<'
1068 && ((p[0] == 'p' && p[1] == 'y'
1069 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1070 || ((p[2] == '3' || p[2] == 'x')
1071 && !ASCII_ISALPHA(p[3]))))
1072 || (p[0] == 'p' && p[1] == 'e'
1073 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1074 || (p[0] == 't' && p[1] == 'c'
1075 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1076 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1077 && !ASCII_ISALPHA(p[3]))
1078 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1079 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1080 || (p[0] == 'm' && p[1] == 'z'
1081 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1082 ))
1083 {
1084 // ":python <<" continues until a dot, like ":append"
1085 p = skipwhite(arg + 2);
1086 if (STRNCMP(p, "trim", 4) == 0)
1087 {
1088 // Ignore leading white space.
1089 p = skipwhite(p + 4);
1090 heredoc_trimmed = vim_strnsave(theline,
1091 skipwhite(theline) - theline);
1092 }
1093 if (*p == NUL)
1094 skip_until = vim_strsave((char_u *)".");
1095 else
1096 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1097 getline_options = GETLINE_NONE;
1098 is_heredoc = TRUE;
Bram Moolenaard881d152022-05-13 13:50:36 +01001099 if (eap->cmdidx == CMD_def && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001100 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001101 }
1102
Bram Moolenaard881d152022-05-13 13:50:36 +01001103 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001104 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001105 // Check for ":cmd v =<< [trim] EOF"
1106 // and ":cmd [a, b] =<< [trim] EOF"
1107 // and "lines =<< [trim] EOF" for Vim9
1108 // Where "cmd" can be "let", "var", "final" or "const".
1109 arg = skipwhite(skiptowhite(p));
1110 if (*arg == '[')
1111 arg = vim_strchr(arg, ']');
1112 if (arg != NULL)
1113 {
1114 int found = (eap->cmdidx == CMD_def && arg[0] == '='
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001115 && arg[1] == '<' && arg[2] =='<');
1116
Bram Moolenaard881d152022-05-13 13:50:36 +01001117 if (!found)
1118 // skip over the argument after "cmd"
1119 arg = skipwhite(skiptowhite(arg));
1120 if (found || (arg[0] == '=' && arg[1] == '<'
1121 && arg[2] =='<'
1122 && (checkforcmd(&p, "let", 2)
1123 || checkforcmd(&p, "var", 3)
1124 || checkforcmd(&p, "final", 5)
1125 || checkforcmd(&p, "const", 5))))
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001126 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001127 p = skipwhite(arg + 3);
1128 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001129 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001130 if (STRNCMP(p, "trim", 4) == 0)
1131 {
1132 // Ignore leading white space.
1133 p = skipwhite(p + 4);
1134 heredoc_trimmed = vim_strnsave(theline,
1135 skipwhite(theline) - theline);
1136 continue;
1137 }
1138 if (STRNCMP(p, "eval", 4) == 0)
1139 {
1140 // Ignore leading white space.
1141 p = skipwhite(p + 4);
1142 continue;
1143 }
1144 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001145 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001146 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1147 getline_options = GETLINE_NONE;
1148 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001149 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001150 }
1151 }
1152 }
1153
1154 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001155 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001156 goto theend;
1157
Bram Moolenaar20677332021-06-06 17:02:53 +02001158 if (heredoc_concat_len > 0)
1159 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001160 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001161 // to be used for the instruction later.
1162 ga_concat(&heredoc_ga, theline);
1163 ga_concat(&heredoc_ga, (char_u *)"\n");
1164 p = vim_strsave((char_u *)"");
1165 }
1166 else
1167 {
1168 // Copy the line to newly allocated memory. get_one_sourceline()
1169 // allocates 250 bytes per line, this saves 80% on average. The
1170 // cost is an extra alloc/free.
1171 p = vim_strsave(theline);
1172 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001173 if (p == NULL)
1174 goto theend;
1175 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1176
1177 // Add NULL lines for continuation lines, so that the line count is
1178 // equal to the index in the growarray.
1179 while (sourcing_lnum_off-- > 0)
1180 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1181
1182 // Check for end of eap->arg.
1183 if (line_arg != NULL && *line_arg == NUL)
1184 line_arg = NULL;
1185 }
1186
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001187 // Return OK when no error was detected.
1188 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001189 ret = OK;
1190
1191theend:
1192 vim_free(skip_until);
1193 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001194 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001195 need_wait_return |= saved_wait_return;
1196 return ret;
1197}
1198
1199/*
1200 * Handle the body of a lambda. *arg points to the "{", process statements
1201 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001202 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001203 * When successful "rettv" is set to a funcref.
1204 */
1205 static int
1206lambda_function_body(
1207 char_u **arg,
1208 typval_T *rettv,
1209 evalarg_T *evalarg,
1210 garray_T *newargs,
1211 garray_T *argtypes,
1212 int varargs,
1213 garray_T *default_args,
1214 char_u *ret_type)
1215{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001216 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001217 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001218 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001219 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001220 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001221 exarg_T eap;
1222 garray_T newlines;
1223 char_u *cmdline = NULL;
1224 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001225 partial_T *pt;
1226 char_u *name;
1227 int lnum_save = -1;
1228 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1229
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001230 *arg = skipwhite(*arg + 1);
1231 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001232 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001233 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001234 return FAIL;
1235 }
1236
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001237 CLEAR_FIELD(eap);
1238 eap.cmdidx = CMD_block;
1239 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001240 eap.cmdlinep = &cmdline;
1241 eap.skip = !evaluate;
1242 if (evalarg->eval_cctx != NULL)
1243 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1244 else
1245 {
1246 eap.getline = evalarg->eval_getline;
1247 eap.cookie = evalarg->eval_cookie;
1248 }
1249
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001250 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001251 if (get_function_body(&eap, &newlines, NULL,
1252 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001253 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001254
1255 // When inside a lambda must add the function lines to evalarg.eval_ga.
1256 evalarg->eval_break_count += newlines.ga_len;
1257 if (gap->ga_itemsize > 0)
1258 {
1259 int idx;
1260 char_u *last;
1261 size_t plen;
1262 char_u *pnl;
1263
1264 for (idx = 0; idx < newlines.ga_len; ++idx)
1265 {
1266 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1267
Bram Moolenaarecb66452021-05-18 15:09:18 +02001268 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001269 goto erret;
1270
1271 // Going to concatenate the lines after parsing. For an empty or
1272 // comment line use an empty string.
1273 // Insert NL characters at the start of each line, the string will
1274 // be split again later in .get_lambda_tv().
1275 if (*p == NUL || vim9_comment_start(p))
1276 p = (char_u *)"";
1277 plen = STRLEN(p);
1278 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1279 if (pnl != NULL)
1280 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001281 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1282 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001283 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001284 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001285 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001286 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001287 // more is following after the "}", which was skipped
1288 last = cmdline;
1289 else
1290 // nothing is following the "}"
1291 last = (char_u *)"}";
1292 plen = STRLEN(last);
1293 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1294 if (pnl != NULL)
1295 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001296 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1297 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001298 }
1299
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001300 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001301 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001302 garray_T *tfgap = &evalarg->eval_tofree_ga;
1303
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001304 // Something comes after the "}".
1305 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001306
1307 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001308 if (ga_grow(tfgap, 1) == OK)
1309 {
1310 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1311 evalarg->eval_using_cmdline = TRUE;
1312 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001313 }
1314 else
1315 *arg = (char_u *)"";
1316
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001317 if (!evaluate)
1318 {
1319 ret = OK;
1320 goto erret;
1321 }
1322
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001323 name = get_lambda_name();
1324 ufunc = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
1325 if (ufunc == NULL)
1326 goto erret;
1327 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001328 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001329 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001330 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001331 ufunc->uf_refcount = 1;
1332 ufunc->uf_args = *newargs;
1333 newargs->ga_data = NULL;
1334 ufunc->uf_def_args = *default_args;
1335 default_args->ga_data = NULL;
1336 ufunc->uf_func_type = &t_func_any;
1337
1338 // error messages are for the first function line
1339 lnum_save = SOURCING_LNUM;
1340 SOURCING_LNUM = sourcing_lnum_top;
1341
1342 // parse argument types
1343 if (parse_argument_types(ufunc, argtypes, varargs) == FAIL)
1344 {
1345 SOURCING_LNUM = lnum_save;
1346 goto erret;
1347 }
1348
1349 // parse the return type, if any
1350 if (parse_return_type(ufunc, ret_type) == FAIL)
1351 goto erret;
1352
1353 pt = ALLOC_CLEAR_ONE(partial_T);
1354 if (pt == NULL)
1355 goto erret;
1356 pt->pt_func = ufunc;
1357 pt->pt_refcount = 1;
1358
1359 ufunc->uf_lines = newlines;
1360 newlines.ga_data = NULL;
1361 if (sandbox)
1362 ufunc->uf_flags |= FC_SANDBOX;
1363 if (!ASCII_ISUPPER(*ufunc->uf_name))
1364 ufunc->uf_flags |= FC_VIM9;
1365 ufunc->uf_script_ctx = current_sctx;
1366 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1367 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1368 set_function_type(ufunc);
1369
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001370 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1371
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001372 rettv->vval.v_partial = pt;
1373 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001374 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001375 ret = OK;
1376
1377erret:
1378 if (lnum_save >= 0)
1379 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001380 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001381 if (newargs != NULL)
1382 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001383 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001384 if (ufunc != NULL)
1385 {
1386 func_clear(ufunc, TRUE);
1387 func_free(ufunc, TRUE);
1388 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001389 return ret;
1390}
1391
1392/*
1393 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001394 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001395 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001396 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1397 */
1398 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001399get_lambda_tv(
1400 char_u **arg,
1401 typval_T *rettv,
1402 int types_optional,
1403 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001404{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001405 int evaluate = evalarg != NULL
1406 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001407 garray_T newargs;
1408 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001409 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001410 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001411 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001412 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001413 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001414 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001415 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001416 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001417 char_u *s;
1418 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001419 int *old_eval_lavars = eval_lavars_used;
1420 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001421 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001422 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001423 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001424 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001425 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001426 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001427
Bram Moolenaar4525a572022-02-13 11:57:33 +00001428 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001429 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001430
1431 ga_init(&newargs);
1432 ga_init(&newlines);
1433
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001434 // First, check if this is really a lambda expression. "->" or "=>" must
1435 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001436 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001437 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001438 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001439 NULL, &default_args, TRUE, NULL, NULL, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001440 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001441 {
1442 if (types_optional)
1443 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +01001444 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001445 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001446
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001447 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001448 if (evaluate)
1449 pnewargs = &newargs;
1450 else
1451 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001452 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001453 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001454 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001455 &varargs, &default_args,
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001456 FALSE, NULL, NULL, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001457 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001458 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001459 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001460 {
1461 if (types_optional)
1462 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001463 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001464 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001465 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001466 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001467
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001468 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1469 if (ret_type != NULL)
1470 {
1471 ret_type = vim_strsave(ret_type);
1472 tofree2 = ret_type;
1473 }
1474
Bram Moolenaare38eab22019-12-05 21:50:01 +01001475 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001476 if (evaluate)
1477 eval_lavars_used = &eval_lavars;
1478
Bram Moolenaar65c44152020-12-24 15:14:01 +01001479 *arg = skipwhite_and_linebreak(*arg, evalarg);
1480
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001481 // Recognize "{" as the start of a function body.
1482 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001483 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001484 if (evalarg == NULL)
1485 // cannot happen?
1486 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001487 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001488 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1489 types_optional ? &argtypes : NULL, varargs,
1490 &default_args, ret_type) == FAIL)
1491 goto errret;
1492 goto theend;
1493 }
1494 if (default_args.ga_len > 0)
1495 {
1496 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001497 goto errret;
1498 }
1499
Bram Moolenaare38eab22019-12-05 21:50:01 +01001500 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001501 start = *arg;
1502 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001503 if (ret == FAIL)
1504 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001505
Bram Moolenaar65c44152020-12-24 15:14:01 +01001506 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001507 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001508 *arg = skipwhite_and_linebreak(*arg, evalarg);
1509 if (**arg != '}')
1510 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001511 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001512 goto errret;
1513 }
1514 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001515 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001516
1517 if (evaluate)
1518 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001519 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001520 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001521 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001522 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001523 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001524
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001525 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001526 if (fp == NULL)
1527 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001528 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001529 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001530 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001531 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001532
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001533 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001534 if (ga_grow(&newlines, 1) == FAIL)
1535 goto errret;
1536
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001537 // If there are line breaks, we need to split up the string.
1538 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001539 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001540 line_end = end;
1541
1542 // Add "return " before the expression (or the first line).
1543 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001544 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001545 if (p == NULL)
1546 goto errret;
1547 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1548 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001549 vim_strncpy(p + 7, start, line_end - start);
1550
1551 if (line_end != end)
1552 {
1553 // Add more lines, split by line breaks. Thus is used when a
1554 // lambda with { cmds } is encountered.
1555 while (*line_end == '\n')
1556 {
1557 if (ga_grow(&newlines, 1) == FAIL)
1558 goto errret;
1559 start = line_end + 1;
1560 line_end = vim_strchr(start, '\n');
1561 if (line_end == NULL)
1562 line_end = end;
1563 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1564 vim_strnsave(start, line_end - start);
1565 }
1566 }
1567
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001568 if (strstr((char *)p + 7, "a:") == NULL)
1569 // No a: variables are used for sure.
1570 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001571
1572 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001573 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001574 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001575 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001576 if (types_optional)
1577 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001578 if (parse_argument_types(fp, &argtypes,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001579 vim9script && varargs) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001580 goto errret;
1581 if (ret_type != NULL)
1582 {
1583 fp->uf_ret_type = parse_type(&ret_type,
1584 &fp->uf_type_list, TRUE);
1585 if (fp->uf_ret_type == NULL)
1586 goto errret;
1587 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001588 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001589 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001590 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001591
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001592 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001593 if (current_funccal != NULL && eval_lavars)
1594 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001595 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001596 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001597 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001598 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001599
1600#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001601 if (prof_def_func())
1602 func_do_profile(fp);
1603#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001604 if (sandbox)
1605 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001606 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001607 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001608 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001609 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001610 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001611 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001612 // Use the line number of the arguments.
1613 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001614
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001615 function_using_block_scopes(fp, evalarg->eval_cstack);
1616
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001617 pt->pt_func = fp;
1618 pt->pt_refcount = 1;
1619 rettv->vval.v_partial = pt;
1620 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001621
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001622 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001623 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001624
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001625theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001626 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001627 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001628 if (types_optional)
1629 ga_clear_strings(&argtypes);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001630
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001631 return OK;
1632
1633errret:
1634 ga_clear_strings(&newargs);
1635 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001636 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001637 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001638 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001639 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001640 if (fp != NULL)
1641 vim_free(fp->uf_arg_types);
1642 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001643 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001644 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001645 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001646 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001647 return FAIL;
1648}
1649
1650/*
1651 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1652 * name it contains, otherwise return "name".
1653 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1654 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001655 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1656 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001657 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001658 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001659 */
1660 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001661deref_func_name(
1662 char_u *name,
1663 int *lenp,
1664 partial_T **partialp,
1665 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001666 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001667 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001668 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001669{
1670 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001671 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001672 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001673 char_u *s = NULL;
1674 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001675 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001676
1677 if (partialp != NULL)
1678 *partialp = NULL;
1679
1680 cc = name[*lenp];
1681 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001682
Bram Moolenaar71f21932022-01-07 18:20:55 +00001683 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001684 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001685 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001686 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001687 tv = &v->di_tv;
1688 }
1689 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1690 {
1691 imported_T *import;
1692 char_u *p = name;
1693 int len = *lenp;
1694
1695 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001696 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001697 p = name + 2;
1698 len -= 2;
1699 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001700 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001701
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001702 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001703 if (import != NULL)
1704 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001705 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001706 if (new_function)
1707 semsg(_(e_redefining_imported_item_str), name);
1708 else
1709 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001710 name[len] = cc;
1711 *lenp = 0;
1712 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001713 }
1714 }
1715
1716 if (tv != NULL)
1717 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001718 if (found_var != NULL)
1719 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001720 if (tv->v_type == VAR_FUNC)
1721 {
1722 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001723 {
1724 *lenp = 0;
1725 return (char_u *)""; // just in case
1726 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001727 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001728 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001729 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001730
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001731 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001732 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001733 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001734
1735 if (pt == NULL)
1736 {
1737 *lenp = 0;
1738 return (char_u *)""; // just in case
1739 }
1740 if (partialp != NULL)
1741 *partialp = pt;
1742 s = partial_name(pt);
1743 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001744 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001745
1746 if (s != NULL)
1747 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001748 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001749 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001750 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001751
1752 if (sv != NULL)
1753 *type = sv->sv_type;
1754 }
1755 return s;
1756 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001757 }
1758
1759 return name;
1760}
1761
1762/*
1763 * Give an error message with a function name. Handle <SNR> things.
1764 * "ermsg" is to be passed without translation, use N_() instead of _().
1765 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001766 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001767emsg_funcname(char *ermsg, char_u *name)
1768{
Bram Moolenaar54598062022-01-13 12:05:09 +00001769 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001770
Bram Moolenaar54598062022-01-13 12:05:09 +00001771 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001772 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001773 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001774 if (p != name)
1775 vim_free(p);
1776}
1777
1778/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001779 * Get function arguments at "*arg" and advance it.
1780 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001781 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001782 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001783 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001784get_func_arguments(
1785 char_u **arg,
1786 evalarg_T *evalarg,
1787 int partial_argc,
1788 typval_T *argvars,
1789 int *argcount)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001790{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001791 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001792 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001793 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001794 int evaluate = evalarg == NULL
1795 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001796
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001797 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001798 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001799 // skip the '(' or ',' and possibly line breaks
1800 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1801
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001802 if (*argp == ')' || *argp == ',' || *argp == NUL)
1803 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001804 if (eval1(&argp, &argvars[*argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 {
1806 ret = FAIL;
1807 break;
1808 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001809 ++*argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001810 // The comma should come right after the argument, but this wasn't
1811 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001812 if (vim9script)
1813 {
1814 if (*argp != ',' && *skipwhite(argp) == ',')
1815 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001816 if (evaluate)
1817 semsg(_(e_no_white_space_allowed_before_str_str),
1818 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001819 ret = FAIL;
1820 break;
1821 }
1822 }
1823 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001824 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001825 if (*argp != ',')
1826 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001827 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1828 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001829 if (evaluate)
1830 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001831 ret = FAIL;
1832 break;
1833 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001834 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001835
Bram Moolenaare6b53242020-07-01 17:28:33 +02001836 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001837 if (*argp == ')')
1838 ++argp;
1839 else
1840 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001841 *arg = argp;
1842 return ret;
1843}
1844
1845/*
1846 * Call a function and put the result in "rettv".
1847 * Return OK or FAIL.
1848 */
1849 int
1850get_func_tv(
1851 char_u *name, // name of the function
1852 int len, // length of "name" or -1 to use strlen()
1853 typval_T *rettv,
1854 char_u **arg, // argument, pointing to the '('
1855 evalarg_T *evalarg, // for line continuation
1856 funcexe_T *funcexe) // various values
1857{
1858 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001859 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001860 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1861 int argcount = 0; // number of arguments found
1862 int vim9script = in_vim9script();
1863 int evaluate = evalarg == NULL
1864 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1865
1866 argp = *arg;
1867 ret = get_func_arguments(&argp, evalarg,
1868 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
1869 argvars, &argcount);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001870
1871 if (ret == OK)
1872 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001873 int i = 0;
1874 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001875
1876 if (get_vim_var_nr(VV_TESTING))
1877 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001878 // Prepare for calling test_garbagecollect_now(), need to know
1879 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001880 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001881 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001882 for (i = 0; i < argcount; ++i)
1883 if (ga_grow(&funcargs, 1) == OK)
1884 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1885 &argvars[i];
1886 }
1887
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001888 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00001889 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001890 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001891 // An error in a builtin function does not return FAIL, but we do
1892 // want to abort further processing if an error was given.
1893 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001894 clear_tv(rettv);
1895 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001896
1897 funcargs.ga_len -= i;
1898 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001899 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001900 {
1901 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001902 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001903 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001904 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001905 }
1906
1907 while (--argcount >= 0)
1908 clear_tv(&argvars[argcount]);
1909
Bram Moolenaar4525a572022-02-13 11:57:33 +00001910 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02001911 *arg = argp;
1912 else
1913 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001914 return ret;
1915}
1916
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001917/*
1918 * Return TRUE if "p" starts with "<SID>" or "s:".
1919 * Only works if eval_fname_script() returned non-zero for "p"!
1920 */
1921 static int
1922eval_fname_sid(char_u *p)
1923{
1924 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1925}
1926
1927/*
1928 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1929 * Change <SNR>123_name() to K_SNR 123_name().
1930 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001931 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001932 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001933 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001934fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
1935{
1936 int llen;
1937 char_u *fname;
1938 int i;
1939
1940 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001941 if (llen == 0)
1942 return name; // no prefix
1943
1944 fname_buf[0] = K_SPECIAL;
1945 fname_buf[1] = KS_EXTRA;
1946 fname_buf[2] = (int)KE_SNR;
1947 i = 3;
1948 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001949 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001950 if (current_sctx.sc_sid <= 0)
1951 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001952 else
1953 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001954 sprintf((char *)fname_buf + 3, "%ld_",
1955 (long)current_sctx.sc_sid);
1956 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001957 }
1958 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001959 if (i + STRLEN(name + llen) < FLEN_FIXED)
1960 {
1961 STRCPY(fname_buf + i, name + llen);
1962 fname = fname_buf;
1963 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001964 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001965 {
1966 fname = alloc(i + STRLEN(name + llen) + 1);
1967 if (fname == NULL)
1968 *error = FCERR_OTHER;
1969 else
1970 {
1971 *tofree = fname;
1972 mch_memmove(fname, fname_buf, (size_t)i);
1973 STRCPY(fname + i, name + llen);
1974 }
1975 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001976 return fname;
1977}
1978
1979/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001980 * Concatenate the script ID and function name into "<SNR>99_name".
1981 * "buffer" must have size MAX_FUNC_NAME_LEN.
1982 */
1983 void
1984func_name_with_sid(char_u *name, int sid, char_u *buffer)
1985{
1986 // A script-local function is stored as "<SNR>99_name".
1987 buffer[0] = K_SPECIAL;
1988 buffer[1] = KS_EXTRA;
1989 buffer[2] = (int)KE_SNR;
1990 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
1991 (long)sid, name);
1992}
1993
1994/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 * Find a function "name" in script "sid".
1996 */
1997 static ufunc_T *
1998find_func_with_sid(char_u *name, int sid)
1999{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002000 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002001 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002
Bram Moolenaarb8822442022-01-11 15:24:05 +00002003 if (!SCRIPT_ID_VALID(sid))
2004 return NULL; // not in a script
2005
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002006 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 hi = hash_find(&func_hashtab, buffer);
2008 if (!HASHITEM_EMPTY(hi))
2009 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002010 return NULL;
2011}
2012
2013/*
2014 * Find a function "name" in script "sid" prefixing the autoload prefix.
2015 */
2016 static ufunc_T *
2017find_func_with_prefix(char_u *name, int sid)
2018{
2019 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002020 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002021 scriptitem_T *si;
2022
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002023 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002024 return NULL; // already has the prefix
2025 if (!SCRIPT_ID_VALID(sid))
2026 return NULL; // not in a script
2027 si = SCRIPT_ITEM(sid);
2028 if (si->sn_autoload_prefix != NULL)
2029 {
2030 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2031 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002032 char_u *namep;
2033
2034 // skip a "<SNR>99_" prefix
2035 namep = untrans_function_name(name);
2036 if (namep == NULL)
2037 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002038
2039 // An exported function in an autoload script is stored as
2040 // "dir#path#name".
2041 if (len < sizeof(buffer))
2042 auto_name = buffer;
2043 else
2044 auto_name = alloc(len);
2045 if (auto_name != NULL)
2046 {
2047 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002048 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002049 hi = hash_find(&func_hashtab, auto_name);
2050 if (auto_name != buffer)
2051 vim_free(auto_name);
2052 if (!HASHITEM_EMPTY(hi))
2053 return HI2UF(hi);
2054 }
2055 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056
2057 return NULL;
2058}
2059
2060/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002061 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002062 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2063 * functions.
2064 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002065 * Return NULL for unknown function.
2066 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002067 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002068find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002069{
2070 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002073 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002075 // Find script-local function before global one.
2076 if (in_vim9script() && eval_isnamec1(*name)
2077 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002079 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2080 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002081 if (func != NULL)
2082 return func;
2083 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002084 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2085 {
2086 char_u *p = name + 5;
2087 long sid;
2088
2089 // printable "<SNR>123_Name" form
2090 sid = getdigits(&p);
2091 if (*p == '_')
2092 {
2093 func = find_func_with_sid(p + 1, (int)sid);
2094 if (func != NULL)
2095 return func;
2096 }
2097 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002099
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002100 if ((flags & FFED_NO_GLOBAL) == 0)
2101 {
2102 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002103 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002104 if (!HASHITEM_EMPTY(hi))
2105 return HI2UF(hi);
2106 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107
Bram Moolenaarb8822442022-01-11 15:24:05 +00002108 // Find autoload function if this is an autoload script.
2109 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2110 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111}
2112
2113/*
2114 * Find a function by name, return pointer to it in ufuncs.
2115 * "cctx" is passed in a :def function to find imported functions.
2116 * Return NULL for unknown or dead function.
2117 */
2118 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002119find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002121 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122
2123 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2124 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002125 return NULL;
2126}
2127
2128/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002129 * Return TRUE if "ufunc" is a global function.
2130 */
2131 int
2132func_is_global(ufunc_T *ufunc)
2133{
2134 return ufunc->uf_name[0] != K_SPECIAL;
2135}
2136
2137/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002138 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2139 */
2140 int
2141func_requires_g_prefix(ufunc_T *ufunc)
2142{
2143 return ufunc->uf_name[0] != K_SPECIAL
2144 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002145 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
2146 && !isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002147}
2148
2149/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002150 * Copy the function name of "fp" to buffer "buf".
2151 * "buf" must be able to hold the function name plus three bytes.
2152 * Takes care of script-local function names.
2153 */
2154 static void
2155cat_func_name(char_u *buf, ufunc_T *fp)
2156{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002157 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002158 {
2159 STRCPY(buf, "<SNR>");
2160 STRCAT(buf, fp->uf_name + 3);
2161 }
2162 else
2163 STRCPY(buf, fp->uf_name);
2164}
2165
2166/*
2167 * Add a number variable "name" to dict "dp" with value "nr".
2168 */
2169 static void
2170add_nr_var(
2171 dict_T *dp,
2172 dictitem_T *v,
2173 char *name,
2174 varnumber_T nr)
2175{
2176 STRCPY(v->di_key, name);
2177 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002178 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002179 v->di_tv.v_type = VAR_NUMBER;
2180 v->di_tv.v_lock = VAR_FIXED;
2181 v->di_tv.vval.v_number = nr;
2182}
2183
2184/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002185 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002186 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002187 static void
2188free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002189{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002190 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002191
Bram Moolenaarca16c602022-09-06 18:57:08 +01002192 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002193 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002194 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002195
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002196 // When garbage collecting a funccall_T may be freed before the
2197 // function that references it, clear its uf_scoped field.
2198 // The function may have been redefined and point to another
2199 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002200 if (fp != NULL && fp->uf_scoped == fc)
2201 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002202 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002203 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002204
Bram Moolenaarca16c602022-09-06 18:57:08 +01002205 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002206 vim_free(fc);
2207}
2208
2209/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002210 * Free "fc" and what it contains.
2211 * Can be called only when "fc" is kept beyond the period of it called,
2212 * i.e. after cleanup_function_call(fc).
2213 */
2214 static void
2215free_funccal_contents(funccall_T *fc)
2216{
2217 listitem_T *li;
2218
2219 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002220 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002221
2222 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002223 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002224
2225 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002226 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002227 clear_tv(&li->li_tv);
2228
2229 free_funccal(fc);
2230}
2231
2232/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002233 * Handle the last part of returning from a function: free the local hashtable.
2234 * Unless it is still in use by a closure.
2235 */
2236 static void
2237cleanup_function_call(funccall_T *fc)
2238{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002239 int may_free_fc = fc->fc_refcount <= 0;
2240 int free_fc = TRUE;
2241
Bram Moolenaarca16c602022-09-06 18:57:08 +01002242 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002243
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002244 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002245 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2246 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002247 else
2248 free_fc = FALSE;
2249
2250 // If the a:000 list and the l: and a: dicts are not referenced and
2251 // there is no closure using it, we can free the funccall_T and what's
2252 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002253 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2254 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002255 else
2256 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002257 int todo;
2258 hashitem_T *hi;
2259 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002260
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002261 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002262
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002263 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002264 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
2265 for (hi = fc->fc_l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002266 {
2267 if (!HASHITEM_EMPTY(hi))
2268 {
2269 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002270 di = HI2DI(hi);
2271 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002272 }
2273 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002274 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002275
Bram Moolenaarca16c602022-09-06 18:57:08 +01002276 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2277 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002278 else
2279 {
2280 listitem_T *li;
2281
2282 free_fc = FALSE;
2283
2284 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002285 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002286 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002287 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002288
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002289 if (free_fc)
2290 free_funccal(fc);
2291 else
2292 {
2293 static int made_copy = 0;
2294
2295 // "fc" is still in use. This can happen when returning "a:000",
2296 // assigning "l:" to a global variable or defining a closure.
2297 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002298 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002299 previous_funccal = fc;
2300
2301 if (want_garbage_collect)
2302 // If garbage collector is ready, clear count.
2303 made_copy = 0;
2304 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002305 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002306 // We have made a lot of copies, worth 4 Mbyte. This can happen
2307 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002308 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002309 // too much memory.
2310 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002311 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002312 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002313 }
2314}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002315
2316/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002317 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2318 */
2319 static int
2320numbered_function(char_u *name)
2321{
2322 return isdigit(*name)
2323 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2324}
2325
2326/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002327 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002328 * 1. ordinary names, function defined with :function or :def;
2329 * can start with "<SNR>123_" literally or with K_SPECIAL.
2330 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002331 * For the first we only count the name stored in func_hashtab as a reference,
2332 * using function() does not count as a reference, because the function is
2333 * looked up by name.
2334 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002335 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002336func_name_refcount(char_u *name)
2337{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002338 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002339}
2340
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341/*
2342 * Unreference "fc": decrement the reference count and free it when it
2343 * becomes zero. "fp" is detached from "fc".
2344 * When "force" is TRUE we are exiting.
2345 */
2346 static void
2347funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2348{
2349 funccall_T **pfc;
2350 int i;
2351
2352 if (fc == NULL)
2353 return;
2354
2355 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002356 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2357 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2358 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2359 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002360 {
2361 if (fc == *pfc)
2362 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002363 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 free_funccal_contents(fc);
2365 return;
2366 }
2367 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002368 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2369 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2370 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371}
2372
2373/*
2374 * Remove the function from the function hashtable. If the function was
2375 * deleted while it still has references this was already done.
2376 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2377 */
2378 static int
2379func_remove(ufunc_T *fp)
2380{
2381 hashitem_T *hi;
2382
2383 // Return if it was already virtually deleted.
2384 if (fp->uf_flags & FC_DEAD)
2385 return FALSE;
2386
2387 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
2388 if (!HASHITEM_EMPTY(hi))
2389 {
2390 // When there is a def-function index do not actually remove the
2391 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002392 // Do remove it when it's a copy.
2393 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaarc970e422021-03-17 15:03:04 +01002394 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002396 return FALSE;
2397 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002398 hash_remove(&func_hashtab, hi, "remove function");
Bram Moolenaarc970e422021-03-17 15:03:04 +01002399 fp->uf_flags |= FC_DELETED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400 return TRUE;
2401 }
2402 return FALSE;
2403}
2404
2405 static void
2406func_clear_items(ufunc_T *fp)
2407{
2408 ga_clear_strings(&(fp->uf_args));
2409 ga_clear_strings(&(fp->uf_def_args));
2410 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002412 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002413 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002414 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002415
2416 // Increment the refcount of this function to avoid it being freed
2417 // recursively when the partial is freed.
2418 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002419 partial_unref(fp->uf_partial);
2420 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002421 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002422
2423#ifdef FEAT_LUA
2424 if (fp->uf_cb_free != NULL)
2425 {
2426 fp->uf_cb_free(fp->uf_cb_state);
2427 fp->uf_cb_free = NULL;
2428 }
2429
2430 fp->uf_cb_state = NULL;
2431 fp->uf_cb = NULL;
2432#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433#ifdef FEAT_PROFILE
2434 VIM_CLEAR(fp->uf_tml_count);
2435 VIM_CLEAR(fp->uf_tml_total);
2436 VIM_CLEAR(fp->uf_tml_self);
2437#endif
2438}
2439
2440/*
2441 * Free all things that a function contains. Does not free the function
2442 * itself, use func_free() for that.
2443 * When "force" is TRUE we are exiting.
2444 */
2445 static void
2446func_clear(ufunc_T *fp, int force)
2447{
2448 if (fp->uf_cleared)
2449 return;
2450 fp->uf_cleared = TRUE;
2451
2452 // clear this function
2453 func_clear_items(fp);
2454 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002455 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456}
2457
2458/*
2459 * Free a function and remove it from the list of functions. Does not free
2460 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002461 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002462 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002464 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002465func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466{
2467 // Only remove it when not done already, otherwise we would remove a newer
2468 // version of the function with the same name.
2469 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2470 func_remove(fp);
2471
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002472 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002473 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002474 if (fp->uf_dfunc_idx > 0)
2475 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002476 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002478 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002479 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002480 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481}
2482
2483/*
2484 * Free all things that a function contains and free the function itself.
2485 * When "force" is TRUE we are exiting.
2486 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002487 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488func_clear_free(ufunc_T *fp, int force)
2489{
2490 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002491 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2492 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002493 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002494 else
2495 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496}
2497
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002498/*
2499 * Copy already defined function "lambda" to a new function with name "global".
2500 * This is for when a compiled function defines a global function.
2501 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002502 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002503copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002504 char_u *lambda,
2505 char_u *global,
2506 loopvarinfo_T *loopvarinfo,
2507 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002508{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002509 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002510 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002511
2512 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002513 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002514 semsg(_(e_lambda_function_not_found_str), lambda);
2515 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002516 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002517
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002518 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002519 if (fp != NULL)
2520 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002521 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002522 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002523 return FAIL;
2524 }
2525
2526 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
2527 if (fp == NULL)
2528 return FAIL;
2529
2530 fp->uf_varargs = ufunc->uf_varargs;
2531 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2532 fp->uf_def_status = ufunc->uf_def_status;
2533 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2534 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2535 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2536 == FAIL
2537 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2538 goto failed;
2539
2540 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
2541 : vim_strsave(ufunc->uf_name_exp);
2542 if (ufunc->uf_arg_types != NULL)
2543 {
2544 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2545 if (fp->uf_arg_types == NULL)
2546 goto failed;
2547 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2548 sizeof(type_T *) * fp->uf_args.ga_len);
2549 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002550 if (ufunc->uf_va_name != NULL)
2551 {
2552 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2553 if (fp->uf_va_name == NULL)
2554 goto failed;
2555 }
2556 fp->uf_ret_type = ufunc->uf_ret_type;
2557
2558 fp->uf_refcount = 1;
2559 STRCPY(fp->uf_name, global);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002560 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002561
2562 // the referenced dfunc_T is now used one more time
2563 link_def_function(fp);
2564
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002565 // Create a partial to store the context of the function where it was
2566 // instantiated. Only needs to be done once. Do this on the original
2567 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002568 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2569 {
2570 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2571
2572 if (pt == NULL)
2573 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002574 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002575 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002576 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002577 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002578 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002579 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002580 }
2581
2582 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002583
2584failed:
2585 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002586 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002587}
2588
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002589static int funcdepth = 0;
2590
2591/*
2592 * Increment the function call depth count.
2593 * Return FAIL when going over 'maxfuncdepth'.
2594 * Otherwise return OK, must call funcdepth_decrement() later!
2595 */
2596 int
2597funcdepth_increment(void)
2598{
2599 if (funcdepth >= p_mfd)
2600 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002601 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002602 return FAIL;
2603 }
2604 ++funcdepth;
2605 return OK;
2606}
2607
2608 void
2609funcdepth_decrement(void)
2610{
2611 --funcdepth;
2612}
2613
2614/*
2615 * Get the current function call depth.
2616 */
2617 int
2618funcdepth_get(void)
2619{
2620 return funcdepth;
2621}
2622
2623/*
2624 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002625 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002626 * times as funcdepth_increment().
2627 */
2628 void
2629funcdepth_restore(int depth)
2630{
2631 funcdepth = depth;
2632}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002633
2634/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002635 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2636 * "rettv".
2637 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2638 * Returns NULL when allocation fails.
2639 */
2640 funccall_T *
2641create_funccal(ufunc_T *fp, typval_T *rettv)
2642{
2643 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2644
2645 if (fc == NULL)
2646 return NULL;
2647 fc->fc_caller = current_funccal;
2648 current_funccal = fc;
2649 fc->fc_func = fp;
2650 func_ptr_ref(fp);
2651 fc->fc_rettv = rettv;
2652 return fc;
2653}
2654
2655/*
2656 * To be called when returning from a compiled function; restores
2657 * current_funccal.
2658 */
2659 void
2660remove_funccal()
2661{
2662 funccall_T *fc = current_funccal;
2663
2664 current_funccal = fc->fc_caller;
2665 free_funccal(fc);
2666}
2667
2668/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002669 * Call a user function.
2670 */
2671 static void
2672call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002673 ufunc_T *fp, // pointer to function
2674 int argcount, // nr of args
2675 typval_T *argvars, // arguments
2676 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002677 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002678 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002679{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002680 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002681 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002682 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002683 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002684 funccall_T *fc;
2685 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002686 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002687 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002688 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002689 int i;
2690 int ai;
2691 int islambda = FALSE;
2692 char_u numbuf[NUMBUFLEN];
2693 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002694 typval_T *tv_to_free[MAX_FUNC_ARGS];
2695 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002696#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002697 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002698#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002699 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002700
Bram Moolenaarb2049902021-01-24 12:53:53 +01002701#ifdef FEAT_PROFILE
2702 CLEAR_FIELD(profile_info);
2703#endif
2704
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002705 // If depth of calling is getting too high, don't execute the function.
2706 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002707 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002708 rettv->v_type = VAR_NUMBER;
2709 rettv->vval.v_number = -1;
2710 return;
2711 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002712
Bram Moolenaare38eab22019-12-05 21:50:01 +01002713 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002714
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002715 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002716 if (fc == NULL)
2717 return;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002718 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002719 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002720 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2721 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002722 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002723 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002724
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002725 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002727#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002728 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002729#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002730 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002731#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002732 if (do_profiling == PROF_YES)
2733 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002734#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002735 sticky_cmdmod_flags = 0;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01002736 call_def_function(fp, argcount, argvars, 0, funcexe->fe_partial,
Bram Moolenaar58779852022-09-06 18:31:14 +01002737 fc, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002738 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002739#ifdef FEAT_PROFILE
2740 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002741 || (caller != NULL && caller->uf_profiling)))
2742 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002743#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002744 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002745 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 return;
2747 }
2748
Bram Moolenaar38453522021-11-28 22:00:12 +00002749 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002750
2751 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002752 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2753 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2754 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002755 */
2756 /*
2757 * Init l: variables.
2758 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002759 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002760 if (selfdict != NULL)
2761 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002762 // Set l:self to "selfdict". Use "name" to avoid a warning from
2763 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002764 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002765 name = v->di_key;
2766 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002767 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002768 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002769 v->di_tv.v_type = VAR_DICT;
2770 v->di_tv.v_lock = 0;
2771 v->di_tv.vval.v_dict = selfdict;
2772 ++selfdict->dv_refcount;
2773 }
2774
2775 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002776 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002777 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002778 * Set a:000 to a list with room for the "..." arguments.
2779 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002780 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002781 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002782 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002783 (varnumber_T)(argcount >= fp->uf_args.ga_len
2784 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002785 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002786 if ((fp->uf_flags & FC_NOARGS) == 0)
2787 {
2788 // Use "name" to avoid a warning from some compiler that checks the
2789 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002790 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002791 name = v->di_key;
2792 STRCPY(name, "000");
2793 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002794 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002795 v->di_tv.v_type = VAR_LIST;
2796 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002797 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002798 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002799 CLEAR_FIELD(fc->fc_l_varlist);
2800 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2801 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002802
2803 /*
2804 * Set a:firstline to "firstline" and a:lastline to "lastline".
2805 * Set a:name to named arguments.
2806 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002807 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002808 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002809 if ((fp->uf_flags & FC_NOARGS) == 0)
2810 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002811 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2812 "firstline", (varnumber_T)funcexe->fe_firstline);
2813 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2814 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002815 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002816 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002817 {
2818 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002819 typval_T def_rettv;
2820 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821
2822 ai = i - fp->uf_args.ga_len;
2823 if (ai < 0)
2824 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002825 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002826 name = FUNCARG(fp, i);
2827 if (islambda)
2828 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002829
2830 // evaluate named argument default expression
2831 isdefault = ai + fp->uf_def_args.ga_len >= 0
2832 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2833 && argvars[i].vval.v_number == VVAL_NONE));
2834 if (isdefault)
2835 {
2836 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002837
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002838 def_rettv.v_type = VAR_NUMBER;
2839 def_rettv.vval.v_number = -1;
2840
2841 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2842 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002843 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002844 {
2845 default_arg_err = 1;
2846 break;
2847 }
2848 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002849 }
2850 else
2851 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002852 if ((fp->uf_flags & FC_NOARGS) != 0)
2853 // Bail out if no a: arguments used (in lambda).
2854 break;
2855
Bram Moolenaare38eab22019-12-05 21:50:01 +01002856 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002857 sprintf((char *)numbuf, "%d", ai + 1);
2858 name = numbuf;
2859 }
2860 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2861 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002862 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002863 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002864 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002865 }
2866 else
2867 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002868 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002869 if (v == NULL)
2870 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002871 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002872 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002873
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002874 // Note: the values are copied directly to avoid alloc/free.
2875 // "argvars" must have VAR_FIXED for v_lock.
2876 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002877 v->di_tv.v_lock = VAR_FIXED;
2878
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002879 if (isdefault)
2880 // Need to free this later, no matter where it's stored.
2881 tv_to_free[tv_to_free_len++] = &v->di_tv;
2882
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002883 if (addlocal)
2884 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002885 // Named arguments should be accessed without the "a:" prefix in
2886 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002887 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002888 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002889 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002890 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002891 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002892
2893 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2894 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002895 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002896
2897 li->li_tv = argvars[i];
2898 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002899 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002900 }
2901 }
2902
Bram Moolenaare38eab22019-12-05 21:50:01 +01002903 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002904 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002905
2906 if (fp->uf_flags & FC_SANDBOX)
2907 {
2908 using_sandbox = TRUE;
2909 ++sandbox;
2910 }
2911
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002912 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002913 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002914 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002915 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002916 ++no_wait_return;
2917 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002918
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002919 smsg(_("calling %s"), SOURCING_NAME);
2920 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002921 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002922 char_u buf[MSG_BUF_LEN];
2923 char_u numbuf2[NUMBUFLEN];
2924 char_u *tofree;
2925 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002926
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002927 msg_puts("(");
2928 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002929 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002930 if (i > 0)
2931 msg_puts(", ");
2932 if (argvars[i].v_type == VAR_NUMBER)
2933 msg_outnum((long)argvars[i].vval.v_number);
2934 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002935 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002936 // Do not want errors such as E724 here.
2937 ++emsg_off;
2938 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2939 --emsg_off;
2940 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002941 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002942 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002943 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002944 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2945 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002946 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002947 msg_puts((char *)s);
2948 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002949 }
2950 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002951 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002952 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002953 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002954 msg_puts("\n"); // don't overwrite this either
2955
2956 verbose_leave_scroll();
2957 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002958 }
2959#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002960 if (do_profiling == PROF_YES)
2961 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01002962 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002963#endif
2964
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002965 // "legacy" does not apply to commands in the function
2966 sticky_cmdmod_flags = 0;
2967
Bram Moolenaar98aff652022-09-06 21:02:35 +01002968 // If called from a compiled :def function the execution context must be
2969 // hidden, any deferred functions need to be added to the function being
2970 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00002971 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01002972
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002973 save_current_sctx = current_sctx;
2974 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002975 save_did_emsg = did_emsg;
2976 did_emsg = FALSE;
2977
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002978 if (default_arg_err && (fp->uf_flags & FC_ABORT))
2979 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002980 else if (islambda)
2981 {
2982 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
2983
2984 // A Lambda always has the command "return {expr}". It is much faster
2985 // to evaluate {expr} directly.
2986 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002987 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002988 --ex_nesting_level;
2989 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002990 else
2991 // call do_cmdline() to execute the lines
2992 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002993 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
2994
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002995 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01002996 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002997
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002998 --RedrawingDisabled;
2999
Bram Moolenaare38eab22019-12-05 21:50:01 +01003000 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003001 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3002 {
3003 clear_tv(rettv);
3004 rettv->v_type = VAR_NUMBER;
3005 rettv->vval.v_number = -1;
3006 }
3007
3008#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003009 if (do_profiling == PROF_YES)
3010 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003011 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003012
3013 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3014 profile_may_end_func(&profile_info, fp, caller);
3015 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003016#endif
3017
Bram Moolenaare38eab22019-12-05 21:50:01 +01003018 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003019 if (p_verbose >= 12)
3020 {
3021 ++no_wait_return;
3022 verbose_enter_scroll();
3023
3024 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003025 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003026 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003027 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003028 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003029 else
3030 {
3031 char_u buf[MSG_BUF_LEN];
3032 char_u numbuf2[NUMBUFLEN];
3033 char_u *tofree;
3034 char_u *s;
3035
Bram Moolenaare38eab22019-12-05 21:50:01 +01003036 // The value may be very long. Skip the middle part, so that we
3037 // have some idea how it starts and ends. smsg() would always
3038 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003039 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003040 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003041 --emsg_off;
3042 if (s != NULL)
3043 {
3044 if (vim_strsize(s) > MSG_BUF_CLEN)
3045 {
3046 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3047 s = buf;
3048 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003049 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003050 vim_free(tofree);
3051 }
3052 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003053 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003054
3055 verbose_leave_scroll();
3056 --no_wait_return;
3057 }
3058
Bram Moolenaare31ee862020-01-07 20:59:34 +01003059 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003060 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003061 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003062 restore_current_ectx(save_current_ectx);
3063
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003064#ifdef FEAT_PROFILE
3065 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003066 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003067#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003068 if (using_sandbox)
3069 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003070 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003071
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003072 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003073 {
3074 ++no_wait_return;
3075 verbose_enter_scroll();
3076
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003077 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003078 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003079
3080 verbose_leave_scroll();
3081 --no_wait_return;
3082 }
3083
3084 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003085 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003086 for (i = 0; i < tv_to_free_len; ++i)
3087 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003088 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003089}
3090
3091/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003092 * Check the argument count for user function "fp".
3093 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3094 */
3095 int
3096check_user_func_argcount(ufunc_T *fp, int argcount)
3097{
3098 int regular_args = fp->uf_args.ga_len;
3099
3100 if (argcount < regular_args - fp->uf_def_args.ga_len)
3101 return FCERR_TOOFEW;
3102 else if (!has_varargs(fp) && argcount > regular_args)
3103 return FCERR_TOOMANY;
3104 return FCERR_UNKNOWN;
3105}
3106
3107/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003109 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 int
3111call_user_func_check(
3112 ufunc_T *fp,
3113 int argcount,
3114 typval_T *argvars,
3115 typval_T *rettv,
3116 funcexe_T *funcexe,
3117 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003118{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003120
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003121#ifdef FEAT_LUA
3122 if (fp->uf_flags & FC_CFUNC)
3123 {
3124 cfunc_T cb = fp->uf_cb;
3125
3126 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3127 }
3128#endif
3129
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003130 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3131 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003132 error = check_user_func_argcount(fp, argcount);
3133 if (error != FCERR_UNKNOWN)
3134 return error;
3135 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 error = FCERR_DICT;
3137 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003138 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 int did_save_redo = FALSE;
3140 save_redo_T save_redo;
3141
3142 /*
3143 * Call the user function.
3144 * Save and restore search patterns, script variables and
3145 * redo buffer.
3146 */
3147 save_search_patterns();
3148 if (!ins_compl_active())
3149 {
3150 saveRedobuff(&save_redo);
3151 did_save_redo = TRUE;
3152 }
3153 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003154 call_user_func(fp, argcount, argvars, rettv, funcexe,
3155 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3157 // Function was unreferenced while being used, free it now.
3158 func_clear_free(fp, FALSE);
3159 if (did_save_redo)
3160 restoreRedobuff(&save_redo);
3161 restore_search_patterns();
3162 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003163 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003165}
3166
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003167static funccal_entry_T *funccal_stack = NULL;
3168
3169/*
3170 * Save the current function call pointer, and set it to NULL.
3171 * Used when executing autocommands and for ":source".
3172 */
3173 void
3174save_funccal(funccal_entry_T *entry)
3175{
3176 entry->top_funccal = current_funccal;
3177 entry->next = funccal_stack;
3178 funccal_stack = entry;
3179 current_funccal = NULL;
3180}
3181
3182 void
3183restore_funccal(void)
3184{
3185 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003186 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003187 else
3188 {
3189 current_funccal = funccal_stack->top_funccal;
3190 funccal_stack = funccal_stack->next;
3191 }
3192}
3193
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003194 funccall_T *
3195get_current_funccal(void)
3196{
3197 return current_funccal;
3198}
3199
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003200/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003201 * Return TRUE when currently at the script level:
3202 * - not in a function
3203 * - not executing an autocommand
3204 * Note that when an autocommand sources a script the result is FALSE;
3205 */
3206 int
3207at_script_level(void)
3208{
3209 return current_funccal == NULL && autocmd_match == NULL;
3210}
3211
3212/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003213 * Mark all functions of script "sid" as deleted.
3214 */
3215 void
3216delete_script_functions(int sid)
3217{
3218 hashitem_T *hi;
3219 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003220 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003221 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003222 size_t len;
3223
3224 buf[0] = K_SPECIAL;
3225 buf[1] = KS_EXTRA;
3226 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003227 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003228 len = STRLEN(buf);
3229
Bram Moolenaarce658352020-07-31 23:47:12 +02003230 while (todo > 0)
3231 {
3232 todo = func_hashtab.ht_used;
3233 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3234 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003235 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003236 fp = HI2UF(hi);
3237 if (STRNCMP(fp->uf_name, buf, len) == 0)
3238 {
3239 int changed = func_hashtab.ht_changed;
3240
3241 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003242
3243 if (fp->uf_calls > 0)
3244 {
3245 // Function is executing, don't free it but do remove
3246 // it from the hashtable.
3247 if (func_remove(fp))
3248 fp->uf_refcount--;
3249 }
3250 else
3251 {
3252 func_clear(fp, TRUE);
3253 // When clearing a function another function can be
3254 // cleared as a side effect. When that happens start
3255 // over.
3256 if (changed != func_hashtab.ht_changed)
3257 break;
3258 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003259 }
3260 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003261 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003262 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003263}
3264
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003265#if defined(EXITFREE) || defined(PROTO)
3266 void
3267free_all_functions(void)
3268{
3269 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003270 ufunc_T *fp;
3271 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003272 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003273 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003274
Bram Moolenaare38eab22019-12-05 21:50:01 +01003275 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003276 while (current_funccal != NULL)
3277 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003278 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003279 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003280 if (current_funccal == NULL && funccal_stack != NULL)
3281 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003282 }
3283
Bram Moolenaare38eab22019-12-05 21:50:01 +01003284 // First clear what the functions contain. Since this may lower the
3285 // reference count of a function, it may also free a function and change
3286 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003287 while (todo > 0)
3288 {
3289 todo = func_hashtab.ht_used;
3290 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3291 if (!HASHITEM_EMPTY(hi))
3292 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 // clear the def function index now
3294 fp = HI2UF(hi);
3295 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003296 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297
Bram Moolenaare38eab22019-12-05 21:50:01 +01003298 // Only free functions that are not refcounted, those are
3299 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003300 if (func_name_refcount(fp->uf_name))
3301 ++skipped;
3302 else
3303 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003304 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003305 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003306 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003307 {
3308 skipped = 0;
3309 break;
3310 }
3311 }
3312 --todo;
3313 }
3314 }
3315
Bram Moolenaare38eab22019-12-05 21:50:01 +01003316 // Now actually free the functions. Need to start all over every time,
3317 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003318 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003319 while (func_hashtab.ht_used > skipped)
3320 {
3321 todo = func_hashtab.ht_used;
3322 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003323 if (!HASHITEM_EMPTY(hi))
3324 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003325 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003326 // Only free functions that are not refcounted, those are
3327 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003328 fp = HI2UF(hi);
3329 if (func_name_refcount(fp->uf_name))
3330 ++skipped;
3331 else
3332 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003333 if (func_free(fp, FALSE) == OK)
3334 {
3335 skipped = 0;
3336 break;
3337 }
3338 // did not actually free it
3339 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003340 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003341 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003342 }
3343 if (skipped == 0)
3344 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345
3346 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003347}
3348#endif
3349
3350/*
3351 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003352 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3353 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003354 * "len" is the length of "name", or -1 for NUL terminated.
3355 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003357builtin_function(char_u *name, int len)
3358{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003359 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003360
Bram Moolenaara26b9702020-04-18 19:53:28 +02003361 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003362 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003363 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3364 {
3365 if (name[i] == AUTOLOAD_CHAR)
3366 return FALSE;
3367 if (!eval_isnamec(name[i]))
3368 {
3369 // "name.something" is not a builtin function
3370 if (name[i] == '.')
3371 return FALSE;
3372 break;
3373 }
3374 }
3375 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003376}
3377
3378 int
3379func_call(
3380 char_u *name,
3381 typval_T *args,
3382 partial_T *partial,
3383 dict_T *selfdict,
3384 typval_T *rettv)
3385{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003386 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003387 listitem_T *item;
3388 typval_T argv[MAX_FUNC_ARGS + 1];
3389 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003390 int r = 0;
3391
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003392 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003393 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003394 {
3395 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3396 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003397 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003398 break;
3399 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003400 // Make a copy of each argument. This is needed to be able to set
3401 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003402 copy_tv(&item->li_tv, &argv[argc++]);
3403 }
3404
3405 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003406 {
3407 funcexe_T funcexe;
3408
Bram Moolenaara80faa82020-04-12 19:37:17 +02003409 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003410 funcexe.fe_firstline = curwin->w_cursor.lnum;
3411 funcexe.fe_lastline = curwin->w_cursor.lnum;
3412 funcexe.fe_evaluate = TRUE;
3413 funcexe.fe_partial = partial;
3414 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003415 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3416 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003417
Bram Moolenaare38eab22019-12-05 21:50:01 +01003418 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003419 while (argc > 0)
3420 clear_tv(&argv[--argc]);
3421
3422 return r;
3423}
3424
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003425static int callback_depth = 0;
3426
3427 int
3428get_callback_depth(void)
3429{
3430 return callback_depth;
3431}
3432
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003433/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003434 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003435 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003436 */
3437 int
3438call_callback(
3439 callback_T *callback,
3440 int len, // length of "name" or -1 to use strlen()
3441 typval_T *rettv, // return value goes here
3442 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003443 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003444 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003445{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003446 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003447 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003448
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003449 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3450 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003451 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003452 funcexe.fe_evaluate = TRUE;
3453 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003454 ++callback_depth;
3455 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3456 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003457
3458 // When a :def function was called that uses :try an error would be turned
3459 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003460 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003461 {
3462 need_rethrow = FALSE;
3463 handle_did_throw();
3464 }
3465
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003466 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003467}
3468
3469/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003470 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003471 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003472 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3473 */
3474 varnumber_T
3475call_callback_retnr(
3476 callback_T *callback,
3477 int argcount, // number of "argvars"
3478 typval_T *argvars) // vars for arguments, must have "argcount"
3479 // PLUS ONE elements!
3480{
3481 typval_T rettv;
3482 varnumber_T retval;
3483
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003484 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003485 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003486
3487 retval = tv_get_number_chk(&rettv, NULL);
3488 clear_tv(&rettv);
3489 return retval;
3490}
3491
3492/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 * Give an error message for the result of a function.
3494 * Nothing if "error" is FCERR_NONE.
3495 */
3496 void
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003497user_func_error(int error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498{
3499 switch (error)
3500 {
3501 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003502 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003503 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003504 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003505 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 break;
3507 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003508 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 break;
3510 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003511 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 break;
3513 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003514 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 break;
3516 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003517 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 break;
3519 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003520 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 break;
3522 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003523 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3524 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 break;
3526 }
3527}
3528
3529/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003530 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003531 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003532 * Return FAIL when the function can't be called, OK otherwise.
3533 * Also returns OK when an error was encountered while executing the function.
3534 */
3535 int
3536call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003537 char_u *funcname, // name of the function
3538 int len, // length of "name" or -1 to use strlen()
3539 typval_T *rettv, // return value goes here
3540 int argcount_in, // number of "argvars"
3541 typval_T *argvars_in, // vars for arguments, must have "argcount"
3542 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003543 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003544{
3545 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01003546 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003547 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003548 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003549 char_u fname_buf[FLEN_FIXED + 1];
3550 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003551 char_u *fname = NULL;
3552 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003553 int argcount = argcount_in;
3554 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003555 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003556 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003557 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003558 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003559 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003560 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003561 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003562 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003563
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003564 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3565 // even when call_func() returns FAIL.
3566 rettv->v_type = VAR_UNKNOWN;
3567
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003568 if (partial != NULL)
3569 fp = partial->pt_func;
3570 if (fp == NULL)
3571 {
3572 // Make a copy of the name, if it comes from a funcref variable it
3573 // could be changed or deleted in the called function.
3574 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3575 if (name == NULL)
3576 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003577
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003578 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3579 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003580
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003581 if (funcexe->fe_doesrange != NULL)
3582 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003583
3584 if (partial != NULL)
3585 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003586 // When the function has a partial with a dict and there is a dict
3587 // argument, use the dict argument. That is backwards compatible.
3588 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003589 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003590 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003591 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003592 {
3593 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003594 {
3595 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3596 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003597 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003598 goto theend;
3599 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003600 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003601 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003602 for (i = 0; i < argcount_in; ++i)
3603 argv[i + argv_clear] = argvars_in[i];
3604 argvars = argv;
3605 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003606
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003607 if (funcexe->fe_check_type != NULL
3608 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003609 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003610 // Now funcexe->fe_check_type is missing the added arguments,
3611 // make a copy of the type with the correction.
3612 check_type = *funcexe->fe_check_type;
3613 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003614 check_type.tt_args = check_type_args;
3615 CLEAR_FIELD(check_type_args);
3616 for (i = 0; i < check_type.tt_argcount; ++i)
3617 check_type_args[i + partial->pt_argc] =
3618 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003619 check_type.tt_argcount += partial->pt_argc;
3620 check_type.tt_min_argcount += partial->pt_argc;
3621 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003622 }
3623 }
3624
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003625 if (error == FCERR_NONE && funcexe->fe_check_type != NULL
3626 && funcexe->fe_evaluate)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003627 {
3628 // Check that the argument types are OK for the types of the funcref.
Bram Moolenaarc84287d2022-01-16 18:06:21 +00003629 if (check_argument_types(funcexe->fe_check_type,
3630 argvars, argcount, funcexe->fe_basetv,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003631 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003632 error = FCERR_OTHER;
3633 }
3634
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003635 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003636 {
3637 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003638 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003639
Bram Moolenaar333894b2020-08-01 18:53:07 +02003640 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003641 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003642 {
3643 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003644 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003645 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003646
Bram Moolenaare38eab22019-12-05 21:50:01 +01003647 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003648 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003649 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003650
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003651 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003652 {
3653 /*
3654 * User defined function.
3655 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003656 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003657 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003658 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003659 if (fp != NULL && !is_global && in_vim9script()
3660 && func_requires_g_prefix(fp))
3661 // In Vim9 script g: is required to find a global
3662 // non-autoload function.
3663 fp = NULL;
3664 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003665
Bram Moolenaare38eab22019-12-05 21:50:01 +01003666 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003667 if (fp == NULL
3668 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003669 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003670 && !aborting())
3671 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003672 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003673 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003674 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003675 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003676 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3677 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003678 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003679 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003680 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003681 if (fp == NULL)
3682 {
3683 char_u *p = untrans_function_name(rfname);
3684
3685 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003686 // Don't do this if the name starts with "s:".
3687 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003688 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003689 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003690
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003691 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003692 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003693 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003694 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003695 if (funcexe->fe_argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003696 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003697 argcount = funcexe->fe_argv_func(argcount, argvars,
zeertzjq48db5da2022-09-16 12:10:03 +01003698 argv_clear, fp);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003699
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003700 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003701 {
3702 // Method call: base->Method()
3703 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003704 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003705 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003706 argvars = argv;
3707 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003708 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003709
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 error = call_user_func_check(fp, argcount, argvars, rettv,
3711 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003712 }
3713 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003714 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003715 {
3716 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003717 * expr->method(): Find the method name in the table, call its
3718 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003719 */
3720 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003721 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003722 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003723 else
3724 {
3725 /*
3726 * Find the function name in the table, call its implementation.
3727 */
3728 error = call_internal_func(fname, argcount, argvars, rettv);
3729 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003730
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003731 /*
3732 * The function call (or "FuncUndefined" autocommand sequence) might
3733 * have been aborted by an error, an interrupt, or an explicitly thrown
3734 * exception that has not been caught so far. This situation can be
3735 * tested for by calling aborting(). For an error in an internal
3736 * function or for the "E132" error in call_user_func(), however, the
3737 * throw point at which the "force_abort" flag (temporarily reset by
3738 * emsg()) is normally updated has not been reached yet. We need to
3739 * update that flag first to make aborting() reliable.
3740 */
3741 update_force_abort();
3742 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003743 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003744 ret = OK;
3745
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003746theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003747 /*
3748 * Report an error unless the argument evaluation or function call has been
3749 * cancelled due to an aborting error, an interrupt, or an exception.
3750 */
3751 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003752 user_func_error(error, (name != NULL) ? name : funcname,
3753 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003754
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003755 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003756 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003757 clear_tv(&argv[--argv_clear + argv_base]);
3758
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003759 vim_free(tofree);
3760 vim_free(name);
3761
3762 return ret;
3763}
3764
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003765/*
3766 * Call a function without arguments, partial or dict.
3767 * This is like call_func() when the call is only "FuncName()".
3768 * To be used by "expr" options.
3769 * Returns NOTDONE when the function could not be found.
3770 */
3771 int
3772call_simple_func(
3773 char_u *funcname, // name of the function
3774 int len, // length of "name" or -1 to use strlen()
3775 typval_T *rettv) // return value goes here
3776{
3777 int ret = FAIL;
3778 int error = FCERR_NONE;
3779 char_u fname_buf[FLEN_FIXED + 1];
3780 char_u *tofree = NULL;
3781 char_u *name;
3782 char_u *fname;
3783 char_u *rfname;
3784 int is_global = FALSE;
3785 ufunc_T *fp;
3786
3787 rettv->v_type = VAR_NUMBER; // default rettv is number zero
3788 rettv->vval.v_number = 0;
3789
3790 // Make a copy of the name, an option can be changed in the function.
3791 name = vim_strnsave(funcname, len);
3792 if (name == NULL)
3793 return ret;
3794
3795 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3796
3797 // Skip "g:" before a function name.
3798 if (fname[0] == 'g' && fname[1] == ':')
3799 {
3800 is_global = TRUE;
3801 rfname = fname + 2;
3802 }
3803 else
3804 rfname = fname;
3805 fp = find_func(rfname, is_global);
3806 if (fp != NULL && !is_global && in_vim9script()
3807 && func_requires_g_prefix(fp))
3808 // In Vim9 script g: is required to find a global non-autoload
3809 // function.
3810 fp = NULL;
3811 if (fp == NULL)
3812 ret = NOTDONE;
3813 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
3814 error = FCERR_DELETED;
3815 else if (fp != NULL)
3816 {
3817 typval_T argvars[1];
3818 funcexe_T funcexe;
3819
3820 argvars[0].v_type = VAR_UNKNOWN;
3821 CLEAR_FIELD(funcexe);
3822 funcexe.fe_evaluate = TRUE;
3823
3824 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
3825 if (error == FCERR_NONE)
3826 ret = OK;
3827 }
3828
3829 user_func_error(error, name, FALSE);
3830 vim_free(tofree);
3831 vim_free(name);
3832
3833 return ret;
3834}
3835
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003836 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837printable_func_name(ufunc_T *fp)
3838{
3839 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3840}
3841
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003842/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003843 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
3844 * TRUE. Otherwise return FALSE.
3845 */
3846 static int
3847function_list_modified(int prev_ht_changed)
3848{
3849 if (prev_ht_changed != func_hashtab.ht_changed)
3850 {
3851 emsg(_(e_function_list_was_modified));
3852 return TRUE;
3853 }
3854 return FALSE;
3855}
3856
3857/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003858 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003859 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003860 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003861list_func_head(ufunc_T *fp, int indent)
3862{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003863 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003864 int j;
3865
3866 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003867
3868 // a timer at the more prompt may have deleted the function
3869 if (function_list_modified(prev_ht_changed))
3870 return FAIL;
3871
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003872 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003873 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003874 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003875 msg_puts("def ");
3876 else
3877 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003879 msg_putchar('(');
3880 for (j = 0; j < fp->uf_args.ga_len; ++j)
3881 {
3882 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003883 msg_puts(", ");
3884 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 if (fp->uf_arg_types != NULL)
3886 {
3887 char *tofree;
3888
3889 msg_puts(": ");
3890 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3891 vim_free(tofree);
3892 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003893 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3894 {
3895 msg_puts(" = ");
3896 msg_puts(((char **)(fp->uf_def_args.ga_data))
3897 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3898 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003899 }
3900 if (fp->uf_varargs)
3901 {
3902 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003903 msg_puts(", ");
3904 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 if (fp->uf_va_name != NULL)
3907 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01003908 if (!fp->uf_varargs)
3909 {
3910 if (j)
3911 msg_puts(", ");
3912 msg_puts("...");
3913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003915 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 {
3917 char *tofree;
3918
3919 msg_puts(": ");
3920 msg_puts(type_name(fp->uf_va_type, &tofree));
3921 vim_free(tofree);
3922 }
3923 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003924 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003925
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003926 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003927 {
3928 if (fp->uf_ret_type != &t_void)
3929 {
3930 char *tofree;
3931
3932 msg_puts(": ");
3933 msg_puts(type_name(fp->uf_ret_type, &tofree));
3934 vim_free(tofree);
3935 }
3936 }
3937 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003938 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003939 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003940 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003941 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003942 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003943 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003944 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003945 msg_clr_eos();
3946 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003947 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003948
3949 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003950}
3951
3952/*
3953 * Get a function name, translating "<SID>" and "<SNR>".
3954 * Also handles a Funcref in a List or Dictionary.
3955 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003956 * Set "*is_global" to TRUE when the function must be global, unless
3957 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003958 * flags:
3959 * TFN_INT: internal function name OK
3960 * TFN_QUIET: be quiet
3961 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003962 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003963 * Advances "pp" to just after the function name (if no error).
3964 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003965 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003966trans_function_name(
3967 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003968 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003969 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003970 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003971 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003972 partial_T **partial, // return: partial of a FuncRef
3973 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003974{
3975 char_u *name = NULL;
3976 char_u *start;
3977 char_u *end;
3978 int lead;
3979 char_u sid_buf[20];
3980 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003982 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003983 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003984 int vim9script = in_vim9script();
3985 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003986
3987 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003988 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003989 start = *pp;
3990
Bram Moolenaare38eab22019-12-05 21:50:01 +01003991 // Check for hard coded <SNR>: already translated function ID (from a user
3992 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003993 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
3994 && (*pp)[2] == (int)KE_SNR)
3995 {
3996 *pp += 3;
3997 len = get_id_len(pp) + 3;
3998 return vim_strnsave(start, len);
3999 }
4000
Bram Moolenaare38eab22019-12-05 21:50:01 +01004001 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4002 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004003 lead = eval_fname_script(start);
4004 if (lead > 2)
4005 start += lead;
4006
Bram Moolenaare38eab22019-12-05 21:50:01 +01004007 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01004008 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004009 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004010 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004011 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004012 {
4013 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004014 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004015 goto theend;
4016 }
4017 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4018 {
4019 /*
4020 * Report an invalid expression in braces, unless the expression
4021 * evaluation has been cancelled due to an aborting error, an
4022 * interrupt, or an exception.
4023 */
4024 if (!aborting())
4025 {
4026 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004027 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004028 }
4029 else
4030 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4031 goto theend;
4032 }
4033
4034 if (lv.ll_tv != NULL)
4035 {
4036 if (fdp != NULL)
4037 {
4038 fdp->fd_dict = lv.ll_dict;
4039 fdp->fd_newkey = lv.ll_newkey;
4040 lv.ll_newkey = NULL;
4041 fdp->fd_di = lv.ll_di;
4042 }
4043 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4044 {
4045 name = vim_strsave(lv.ll_tv->vval.v_string);
4046 *pp = end;
4047 }
4048 else if (lv.ll_tv->v_type == VAR_PARTIAL
4049 && lv.ll_tv->vval.v_partial != NULL)
4050 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004051 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004052 *pp = end;
4053 if (partial != NULL)
4054 *partial = lv.ll_tv->vval.v_partial;
4055 }
4056 else
4057 {
4058 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4059 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004060 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004061 else
4062 *pp = end;
4063 name = NULL;
4064 }
4065 goto theend;
4066 }
4067
4068 if (lv.ll_name == NULL)
4069 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004070 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004071 *pp = end;
4072 goto theend;
4073 }
4074
Bram Moolenaare38eab22019-12-05 21:50:01 +01004075 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004076 if (lv.ll_exp_name != NULL)
4077 {
4078 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004079 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004080 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004081 if (name == lv.ll_exp_name)
4082 name = NULL;
4083 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004084 else if (lv.ll_sid > 0)
4085 {
4086 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4087 int cc = *lv.ll_name_end;
4088
4089 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4090 *lv.ll_name_end = NUL;
4091 if (si->sn_autoload_prefix != NULL)
4092 {
4093 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4094 }
4095 else
4096 {
4097 sid_buf[0] = K_SPECIAL;
4098 sid_buf[1] = KS_EXTRA;
4099 sid_buf[2] = (int)KE_SNR;
4100 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004101 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004102 name = concat_str(sid_buf, lv.ll_name);
4103 }
4104 *lv.ll_name_end = cc;
4105 *pp = end;
4106 goto theend;
4107 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004108 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004109 {
4110 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004111 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004112 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004113 if (name == *pp)
4114 name = NULL;
4115 }
4116 if (name != NULL)
4117 {
4118 name = vim_strsave(name);
4119 *pp = end;
4120 if (STRNCMP(name, "<SNR>", 5) == 0)
4121 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004122 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004123 name[0] = K_SPECIAL;
4124 name[1] = KS_EXTRA;
4125 name[2] = (int)KE_SNR;
4126 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4127 }
4128 goto theend;
4129 }
4130
4131 if (lv.ll_exp_name != NULL)
4132 {
4133 len = (int)STRLEN(lv.ll_exp_name);
4134 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4135 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4136 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004137 // When there was "s:" already or the name expanded to get a
4138 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004139 lv.ll_name += 2;
4140 len -= 2;
4141 lead = 2;
4142 }
4143 }
4144 else
4145 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004146 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004147 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004148 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004149 if (lv.ll_name[0] == 'g')
4150 {
4151 if (is_global != NULL)
4152 {
4153 *is_global = TRUE;
4154 }
4155 else
4156 {
4157 // dropping "g:" without setting "is_global" won't work in
4158 // Vim9script, put it back later
4159 prefix_g = TRUE;
4160 extra = 2;
4161 }
4162 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004163 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004164 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004165 len = (int)(end - lv.ll_name);
4166 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004167 if (len <= 0)
4168 {
4169 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004170 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004171 goto theend;
4172 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004173
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004174 // In Vim9 script a user function is script-local by default, unless it
4175 // starts with a lower case character: dict.func().
Bram Moolenaar4525a572022-02-13 11:57:33 +00004176 vim9_local = ASCII_ISUPPER(*start) && vim9script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004178 /*
4179 * Copy the function name to allocated memory.
4180 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4181 * Accept <SNR>123_name() outside a script.
4182 */
4183 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004184 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004185 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004186 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004187 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004188 {
Kota Kato948a3892022-08-16 16:09:59 +01004189 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4190 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004191 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004192 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004193 goto theend;
4194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004196 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004197 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004199 || eval_fname_sid(*pp))
4200 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004202 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004203 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004204 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004205 goto theend;
4206 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004207 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004208 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 extra = 3 + (int)STRLEN(sid_buf);
4210 else
4211 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004212 }
4213 }
Bram Moolenaar22f17a22021-06-21 20:48:58 +02004214 else if (!(flags & TFN_INT) && (builtin_function(lv.ll_name, len)
Bram Moolenaar4525a572022-02-13 11:57:33 +00004215 || (vim9script && *lv.ll_name == '_')))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004216 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004217 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004218 : e_function_name_must_start_with_capital_or_s_str),
4219 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004220 goto theend;
4221 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004222 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004223 {
4224 char_u *cp = vim_strchr(lv.ll_name, ':');
4225
4226 if (cp != NULL && cp < end)
4227 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004228 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004229 goto theend;
4230 }
4231 }
4232
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004234 if (name != NULL)
4235 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004236 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004237 {
4238 name[0] = K_SPECIAL;
4239 name[1] = KS_EXTRA;
4240 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004241 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004242 STRCPY(name + 3, sid_buf);
4243 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004244 else if (prefix_g)
4245 {
4246 name[0] = 'g';
4247 name[1] = ':';
4248 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4250 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004251 }
4252 *pp = end;
4253
4254theend:
4255 clear_lval(&lv);
4256 return name;
4257}
4258
4259/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004260 * Assuming "name" is the result of trans_function_name() and it was prefixed
4261 * to use the script-local name, return the unmodified name (points into
4262 * "name"). Otherwise return NULL.
4263 * This can be used to first search for a script-local function and fall back
4264 * to the global function if not found.
4265 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004266 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004267untrans_function_name(char_u *name)
4268{
4269 char_u *p;
4270
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004271 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004272 {
4273 p = vim_strchr(name, '_');
4274 if (p != NULL)
4275 return p + 1;
4276 }
4277 return NULL;
4278}
4279
4280/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004281 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4282 * current script ID and returns the expanded function name. The caller should
4283 * free the returned name. If not called from a script context or the function
4284 * name doesn't start with these prefixes, then returns NULL.
4285 * This doesn't check whether the script-local function exists or not.
4286 */
4287 char_u *
4288get_scriptlocal_funcname(char_u *funcname)
4289{
4290 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004291 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004292 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004293 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004294
4295 if (funcname == NULL)
4296 return NULL;
4297
4298 if (STRNCMP(funcname, "s:", 2) != 0
4299 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004300 {
4301 ufunc_T *ufunc;
4302
4303 // The function name does not have a script-local prefix. Try finding
4304 // it when in a Vim9 script and there is no "g:" prefix.
4305 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4306 return NULL;
4307 ufunc = find_func(funcname, FALSE);
4308 if (ufunc == NULL || func_is_global(ufunc)
4309 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4310 return NULL;
4311 ++p;
4312 off = 0;
4313 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004314 else
4315 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004316
4317 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4318 {
4319 emsg(_(e_using_sid_not_in_script_context));
4320 return NULL;
4321 }
4322 // Expand s: prefix into <SNR>nr_<name>
4323 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4324 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004325 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004326 if (newname == NULL)
4327 return NULL;
4328 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004329 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004330
4331 return newname;
4332}
4333
4334/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004335 * Return script-local "fname" with the 3-byte sequence replaced by
4336 * printable <SNR> in allocated memory.
4337 */
4338 char_u *
4339alloc_printable_func_name(char_u *fname)
4340{
4341 char_u *n = alloc(STRLEN(fname + 3) + 6);
4342
4343 if (n != NULL)
4344 {
4345 STRCPY(n, "<SNR>");
4346 STRCPY(n + 5, fname + 3);
4347 }
4348 return n;
4349}
4350
4351/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004352 * Call trans_function_name(), except that a lambda is returned as-is.
4353 * Returns the name in allocated memory.
4354 */
4355 char_u *
4356save_function_name(
4357 char_u **name,
4358 int *is_global,
4359 int skip,
4360 int flags,
4361 funcdict_T *fudi)
4362{
4363 char_u *p = *name;
4364 char_u *saved;
4365
4366 if (STRNCMP(p, "<lambda>", 8) == 0)
4367 {
4368 p += 8;
4369 (void)getdigits(&p);
4370 saved = vim_strnsave(*name, p - *name);
4371 if (fudi != NULL)
4372 CLEAR_POINTER(fudi);
4373 }
4374 else
4375 saved = trans_function_name(&p, is_global, skip,
4376 flags, fudi, NULL, NULL);
4377 *name = p;
4378 return saved;
4379}
4380
4381/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004382 * List functions. When "regmatch" is NULL all of then.
4383 * Otherwise functions matching "regmatch".
4384 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004385 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004386list_functions(regmatch_T *regmatch)
4387{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004388 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004389 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004390 hashitem_T *hi;
4391
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004392 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004393 {
4394 if (!HASHITEM_EMPTY(hi))
4395 {
4396 ufunc_T *fp = HI2UF(hi);
4397
4398 --todo;
4399 if ((fp->uf_flags & FC_DEAD) == 0
4400 && (regmatch == NULL
4401 ? !message_filtered(fp->uf_name)
4402 && !func_name_refcount(fp->uf_name)
4403 : !isdigit(*fp->uf_name)
4404 && vim_regexec(regmatch, fp->uf_name, 0)))
4405 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004406 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004407 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004408 if (function_list_modified(prev_ht_changed))
4409 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004410 }
4411 }
4412 }
4413}
4414
4415/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004416 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004417 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4418 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004419 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004420 * If "class_arg" is not NULL then the function is defined in this class.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004421 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004422 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004423 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004424define_function(
4425 exarg_T *eap,
4426 char_u *name_arg,
4427 garray_T *lines_to_free,
4428 class_T *class_arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004429{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004430 int j;
4431 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004432 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004433 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004434 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004435 char_u *p;
4436 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004437 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004438 char_u *line_arg = NULL;
4439 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004441 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004442 garray_T newlines;
4443 int varargs = FALSE;
4444 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004446 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004447 int fp_allocated = FALSE;
4448 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004449 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004450 dictitem_T *v;
4451 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004452 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004453 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004454 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004455 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004456 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004457 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004458
4459 /*
4460 * ":function" without argument: list functions.
4461 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004462 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004463 {
4464 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004465 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004466 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004467 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004468 }
4469
4470 /*
4471 * ":function /pat": list functions matching pattern.
4472 */
4473 if (*eap->arg == '/')
4474 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004475 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004476 if (!eap->skip)
4477 {
4478 regmatch_T regmatch;
4479
4480 c = *p;
4481 *p = NUL;
4482 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4483 *p = c;
4484 if (regmatch.regprog != NULL)
4485 {
4486 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004487 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004488 vim_regfree(regmatch.regprog);
4489 }
4490 }
4491 if (*p == '/')
4492 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004493 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004494 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004495 }
4496
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497 ga_init(&newargs);
4498 ga_init(&argtypes);
4499 ga_init(&default_args);
4500
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004501 /*
4502 * Get the function name. There are these situations:
4503 * func normal function name
4504 * "name" == func, "fudi.fd_dict" == NULL
4505 * dict.func new dictionary entry
4506 * "name" == NULL, "fudi.fd_dict" set,
4507 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4508 * dict.func existing dict entry with a Funcref
4509 * "name" == func, "fudi.fd_dict" set,
4510 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4511 * dict.func existing dict entry that's not a Funcref
4512 * "name" == NULL, "fudi.fd_dict" set,
4513 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4514 * s:func script-local function name
4515 * g:func global function name, same as "func"
4516 */
4517 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004518 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004519 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004520 // nested function, argument is (args).
4521 paren = TRUE;
4522 CLEAR_FIELD(fudi);
4523 }
4524 else
4525 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004526 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004527 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004528 if (p[0] == 's' && p[1] == ':')
4529 {
4530 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4531 return NULL;
4532 }
4533 p = to_name_end(p, TRUE);
4534 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4535 {
4536 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4537 eap->arg);
4538 return NULL;
4539 }
4540 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004541 }
4542
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004543 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
4544 | (class_arg == 0 ? 0 : TFN_INT);
4545 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004546 paren = (vim_strchr(p, '(') != NULL);
4547 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004548 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004549 /*
4550 * Return on an invalid expression in braces, unless the expression
4551 * evaluation has been cancelled due to an aborting error, an
4552 * interrupt, or an exception.
4553 */
4554 if (!aborting())
4555 {
4556 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004557 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004558 vim_free(fudi.fd_newkey);
4559 return NULL;
4560 }
4561 else
4562 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004563 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004564
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004565 // For "export def FuncName()" in an autoload script the function name
4566 // is stored with the legacy autoload name "dir#script#FuncName" so
4567 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004568 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004569 {
4570 char_u *prefixed = may_prefix_autoload(name);
4571
4572 if (prefixed != NULL && prefixed != name)
4573 {
4574 vim_free(name);
4575 name = prefixed;
4576 }
4577 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004578 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004579 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004580 {
4581 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4582 goto ret_free;
4583 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004584 }
4585
Bram Moolenaare38eab22019-12-05 21:50:01 +01004586 // An error in a function call during evaluation of an expression in magic
4587 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004588 saved_did_emsg = did_emsg;
4589 did_emsg = FALSE;
4590
4591 /*
4592 * ":function func" with only function name: list function.
4593 */
4594 if (!paren)
4595 {
4596 if (!ends_excmd(*skipwhite(p)))
4597 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004598 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004599 goto ret_free;
4600 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004601 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004602 if (eap->nextcmd != NULL)
4603 *p = NUL;
4604 if (!eap->skip && !got_int)
4605 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004606 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004607 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4608 {
4609 char_u *up = untrans_function_name(name);
4610
4611 // With Vim9 script the name was made script-local, if not
4612 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004613 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004614 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004615 }
4616
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004617 if (fp != NULL)
4618 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004619 // Check no function was added or removed from a timer, e.g. at
4620 // the more prompt. "fp" may then be invalid.
4621 int prev_ht_changed = func_hashtab.ht_changed;
4622
4623 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004624 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004625 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4626 {
4627 if (FUNCLINE(fp, j) == NULL)
4628 continue;
4629 msg_putchar('\n');
4630 msg_outnum((long)(j + 1));
4631 if (j < 9)
4632 msg_putchar(' ');
4633 if (j < 99)
4634 msg_putchar(' ');
4635 if (function_list_modified(prev_ht_changed))
4636 break;
4637 msg_prt_line(FUNCLINE(fp, j), FALSE);
4638 out_flush(); // show a line at a time
4639 ui_breakcheck();
4640 }
4641 if (!got_int)
4642 {
4643 msg_putchar('\n');
4644 if (!function_list_modified(prev_ht_changed))
4645 {
4646 if (fp->uf_def_status != UF_NOT_COMPILED)
4647 msg_puts(" enddef");
4648 else
4649 msg_puts(" endfunction");
4650 }
4651 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004652 }
4653 }
4654 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004655 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004656 }
4657 goto ret_free;
4658 }
4659
4660 /*
4661 * ":function name(arg1, arg2)" Define function.
4662 */
4663 p = skipwhite(p);
4664 if (*p != '(')
4665 {
4666 if (!eap->skip)
4667 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004668 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004669 goto ret_free;
4670 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004671 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004672 if (vim_strchr(p, '(') != NULL)
4673 p = vim_strchr(p, '(');
4674 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004675
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004676 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4677 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004678 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004679 goto ret_free;
4680 }
4681
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004682 // In Vim9 script only global functions can be redefined.
4683 if (vim9script && eap->forceit && !is_global)
4684 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004685 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004686 goto ret_free;
4687 }
4688
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004689 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004690
Bram Moolenaar04b12692020-05-04 23:24:44 +02004691 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004692 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004693 // Check the name of the function. Unless it's a dictionary function
4694 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004695 if (name != NULL)
4696 arg = name;
4697 else
4698 arg = fudi.fd_newkey;
4699 if (arg != NULL && (fudi.fd_di == NULL
4700 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4701 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4702 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004703 char_u *name_base = arg;
4704 int i;
4705
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004706 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004707 {
4708 name_base = vim_strchr(arg, '_');
4709 if (name_base == NULL)
4710 name_base = arg + 3;
4711 else
4712 ++name_base;
4713 }
4714 for (i = 0; name_base[i] != NUL && (i == 0
4715 ? eval_isnamec1(name_base[i])
4716 : eval_isnamec(name_base[i])); ++i)
4717 ;
4718 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004719 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004720
4721 // In Vim9 script a function cannot have the same name as a
4722 // variable.
4723 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004724 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4725 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004726 + EVAL_VAR_NO_FUNC) == OK)
4727 {
4728 semsg(_(e_redefining_script_item_str), name_base);
4729 goto ret_free;
4730 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004731 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004732 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004733 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004734 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004735 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004736 goto ret_free;
4737 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004738 }
4739
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004740 // This may get more lines and make the pointers into the first line
4741 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004742 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004744 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004745 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004746 eap, class_arg, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004747 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004748 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004751 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004752 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004753 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004754 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004755 if (*p != ':')
4756 {
4757 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4758 p = skipwhite(p);
4759 }
4760 else if (!IS_WHITE_OR_NUL(p[1]))
4761 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004763 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004765 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004766 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004767 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004769 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004771 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004772 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004773 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004774 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004775 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004776 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004777 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 else
4779 // find extra arguments "range", "dict", "abort" and "closure"
4780 for (;;)
4781 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004782 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783 p = skipwhite(p);
4784 if (STRNCMP(p, "range", 5) == 0)
4785 {
4786 flags |= FC_RANGE;
4787 p += 5;
4788 }
4789 else if (STRNCMP(p, "dict", 4) == 0)
4790 {
4791 flags |= FC_DICT;
4792 p += 4;
4793 }
4794 else if (STRNCMP(p, "abort", 5) == 0)
4795 {
4796 flags |= FC_ABORT;
4797 p += 5;
4798 }
4799 else if (STRNCMP(p, "closure", 7) == 0)
4800 {
4801 flags |= FC_CLOSURE;
4802 p += 7;
4803 if (current_funccal == NULL)
4804 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004805 emsg_funcname(e_closure_function_should_not_be_at_top_level,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806 name == NULL ? (char_u *)"" : name);
4807 goto erret;
4808 }
4809 }
4810 else
4811 break;
4812 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004813
Bram Moolenaare38eab22019-12-05 21:50:01 +01004814 // When there is a line break use what follows for the function body.
4815 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004816 if (*p == '\n')
4817 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004818 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004819 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4820 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004821 && !(VIM_ISWHITE(*whitep) && *p == '#'
4822 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004823 && !eap->skip
4824 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004825 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004826
4827 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004828 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4829 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004830 */
4831 if (KeyTyped)
4832 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004833 // Check if the function already exists, don't let the user type the
4834 // whole function before telling him it doesn't work! For a script we
4835 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004836 if (!eap->skip && !eap->forceit)
4837 {
4838 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004839 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004840 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00004841 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004842 }
4843
4844 if (!eap->skip && did_emsg)
4845 goto erret;
4846
Bram Moolenaare38eab22019-12-05 21:50:01 +01004847 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004848 cmdline_row = msg_row;
4849 }
4850
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004851 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004852 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004853
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004854 // Do not define the function when getting the body fails and when
4855 // skipping.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004856 if (get_function_body(eap, &newlines, line_arg, lines_to_free) == FAIL
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004857 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004858 goto erret;
4859
4860 /*
4861 * If there are no errors, add the function
4862 */
4863 if (fudi.fd_dict == NULL)
4864 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004866 char_u *find_name = name;
4867 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004868 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004870 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004871 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004872 var_conflict = TRUE;
4873
4874 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
4875 {
4876 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4877
4878 if (si->sn_autoload_prefix != NULL)
4879 {
4880 if (is_export)
4881 {
4882 find_name = name + STRLEN(si->sn_autoload_prefix);
4883 v = find_var(find_name, &ht, TRUE);
4884 if (v != NULL)
4885 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004886 // Only check if the function already exists in the script,
4887 // global functions can be shadowed.
4888 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004889 }
4890 else
4891 {
4892 char_u *prefixed = may_prefix_autoload(name);
4893
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00004894 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004895 {
4896 v = find_var(prefixed, &ht, TRUE);
4897 if (v != NULL)
4898 var_conflict = TRUE;
4899 vim_free(prefixed);
4900 }
4901 }
4902 }
4903 }
4904 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004905 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004906 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004907 goto erret;
4908 }
4909
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004910 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02004911 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004912 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004913 char_u *uname = untrans_function_name(name);
4914
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004915 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004916 }
4917
4918 if (fp != NULL || import != NULL)
4919 {
4920 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004921
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004922 // Function can be replaced with "function!" and when sourcing the
4923 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02004924 // A name that is used by an import can not be overruled.
4925 if (import != NULL
4926 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004927 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02004928 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004929 {
Bram Moolenaard604d782021-11-20 21:46:20 +00004930 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02004931 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004932 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02004933 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00004934 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004935 goto erret;
4936 }
4937 if (fp->uf_calls > 0)
4938 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004939 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00004940 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004941 goto erret;
4942 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004943 if (fp->uf_refcount > 1)
4944 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004945 // This function is referenced somewhere, don't redefine it but
4946 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004947 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004948 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004949 fp = NULL;
4950 overwrite = TRUE;
4951 }
4952 else
4953 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004954 char_u *exp_name = fp->uf_name_exp;
4955
4956 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01004957 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004958 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004959 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004960 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004962#ifdef FEAT_PROFILE
4963 fp->uf_profiling = FALSE;
4964 fp->uf_prof_initialized = FALSE;
4965#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01004966 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004967 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004968 }
4969 }
4970 else
4971 {
4972 char numbuf[20];
4973
4974 fp = NULL;
4975 if (fudi.fd_newkey == NULL && !eap->forceit)
4976 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004977 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004978 goto erret;
4979 }
4980 if (fudi.fd_di == NULL)
4981 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004982 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02004983 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004984 goto erret;
4985 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004986 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02004987 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004988 goto erret;
4989
Bram Moolenaare38eab22019-12-05 21:50:01 +01004990 // Give the function a sequential number. Can only be used with a
4991 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004992 vim_free(name);
4993 sprintf(numbuf, "%d", ++func_nr);
4994 name = vim_strsave((char_u *)numbuf);
4995 if (name == NULL)
4996 goto erret;
4997 }
4998
4999 if (fp == NULL)
5000 {
5001 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5002 {
5003 int slen, plen;
5004 char_u *scriptname;
5005
Bram Moolenaare38eab22019-12-05 21:50:01 +01005006 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005007 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005008 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005009 {
5010 scriptname = autoload_name(name);
5011 if (scriptname != NULL)
5012 {
5013 p = vim_strchr(scriptname, '/');
5014 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005015 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005016 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005017 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005018 j = OK;
5019 vim_free(scriptname);
5020 }
5021 }
5022 if (j == FAIL)
5023 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005024 linenr_T save_lnum = SOURCING_LNUM;
5025
5026 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005027 semsg(_(e_function_name_does_not_match_script_file_name_str),
5028 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005029 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005030 goto erret;
5031 }
5032 }
5033
Bram Moolenaar47ed5532019-08-08 20:49:14 +02005034 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005035 if (fp == NULL)
5036 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005037 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005038
5039 if (fudi.fd_dict != NULL)
5040 {
5041 if (fudi.fd_di == NULL)
5042 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005043 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005044 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5045 if (fudi.fd_di == NULL)
5046 {
5047 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02005048 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005049 goto erret;
5050 }
5051 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5052 {
5053 vim_free(fudi.fd_di);
5054 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02005055 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005056 goto erret;
5057 }
5058 }
5059 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005060 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005061 clear_tv(&fudi.fd_di->di_tv);
5062 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005063 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005064
Bram Moolenaare38eab22019-12-05 21:50:01 +01005065 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005066 flags |= FC_DICT;
5067 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005068 }
5069 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005070 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005071 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005072 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073
5074 if (eap->cmdidx == CMD_def)
5075 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005076 int lnum_save = SOURCING_LNUM;
5077 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005078
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005079 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005080
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005081 // error messages are for the first function line
5082 SOURCING_LNUM = sourcing_lnum_top;
5083
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005084 // The function may use script variables from the context.
5085 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005086
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005087 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005089 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005090 free_fp = fp_allocated;
5091 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005092 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005093 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094
5095 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005096 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005098 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005099 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005100 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005102 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005104 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005105 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005107 if (fp_allocated)
5108 {
5109 // insert the new function in the function list
5110 set_ufunc_name(fp, name);
5111 if (overwrite)
5112 {
5113 hi = hash_find(&func_hashtab, name);
5114 hi->hi_key = UF2HIKEY(fp);
5115 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005116 else if (hash_add(&func_hashtab, UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005117 {
5118 free_fp = TRUE;
5119 goto erret;
5120 }
5121 fp->uf_refcount = 1;
5122 }
5123
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005124 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005125 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005126 if ((flags & FC_CLOSURE) != 0)
5127 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005128 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005129 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005130 }
5131 else
5132 fp->uf_scoped = NULL;
5133
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005134#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005135 if (prof_def_func())
5136 func_do_profile(fp);
5137#endif
5138 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005139 if (sandbox)
5140 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005141 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005142 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005143 fp->uf_flags = flags;
5144 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005145 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005146 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005147 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005148 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005149 if (is_export)
5150 {
5151 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005152 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005153 is_export = FALSE;
5154 }
5155
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005156 if (eap->cmdidx == CMD_def)
5157 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005158 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5159 // :func does not use Vim9 script syntax, even in a Vim9 script file
5160 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005161
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005162 goto ret_free;
5163
5164erret:
5165 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005166 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005167 if (fp != NULL)
5168 {
5169 ga_init(&fp->uf_args);
5170 ga_init(&fp->uf_def_args);
5171 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005172errret_2:
5173 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005174 if (fp != NULL)
5175 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005176 if (free_fp)
5177 {
5178 vim_free(fp);
5179 fp = NULL;
5180 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005181ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005182 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005183 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005184 if (name != name_arg)
5185 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005186 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005187 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005188
5189 return fp;
5190}
5191
5192/*
5193 * ":function"
5194 */
5195 void
5196ex_function(exarg_T *eap)
5197{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005198 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005199
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005200 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005201 (void)define_function(eap, NULL, &lines_to_free, NULL);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005202 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005203}
5204
5205/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005206 * Find a function by name, including "<lambda>123".
5207 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005208 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005209 * Return NULL if not found.
5210 */
5211 ufunc_T *
5212find_func_by_name(char_u *name, compiletype_T *compile_type)
5213{
5214 char_u *arg = name;
5215 char_u *fname;
5216 ufunc_T *ufunc;
5217 int is_global = FALSE;
5218
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005219 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5220 {
5221 *compile_type = CT_PROFILE;
5222 arg = skipwhite(arg + 7);
5223 }
5224 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5225 {
5226 *compile_type = CT_DEBUG;
5227 arg = skipwhite(arg + 5);
5228 }
5229
5230 if (STRNCMP(arg, "<lambda>", 8) == 0)
5231 {
5232 arg += 8;
5233 (void)getdigits(&arg);
5234 fname = vim_strnsave(name, arg - name);
5235 }
5236 else
5237 fname = trans_function_name(&arg, &is_global, FALSE,
5238 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
5239 if (fname == NULL)
5240 {
5241 semsg(_(e_invalid_argument_str), name);
5242 return NULL;
5243 }
5244 if (!ends_excmd2(name, arg))
5245 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005246 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005247 emsg(ex_errmsg(e_trailing_characters_str, arg));
5248 return NULL;
5249 }
5250
5251 ufunc = find_func(fname, is_global);
5252 if (ufunc == NULL)
5253 {
5254 char_u *p = untrans_function_name(fname);
5255
5256 if (p != NULL)
5257 // Try again without making it script-local.
5258 ufunc = find_func(p, FALSE);
5259 }
5260 vim_free(fname);
5261 if (ufunc == NULL)
5262 semsg(_(e_cannot_find_function_str), name);
5263 return ufunc;
5264}
5265
5266/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005267 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005268 * be compiled or the one specified by the argument.
5269 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005270 */
5271 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005272ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005273{
Bram Moolenaar822ba242020-05-24 23:00:18 +02005274 ufunc_T *ufunc;
5275
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005276 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005277 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005278 compiletype_T compile_type = CT_NONE;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005279
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005280 ufunc = find_func_by_name(eap->arg, &compile_type);
5281 if (ufunc != NULL)
5282 {
5283 if (func_needs_compiling(ufunc, compile_type))
5284 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5285 else
5286 smsg(_("Function %s does not need compiling"), eap->arg);
5287 }
5288 }
5289 else
5290 {
5291 long todo = (long)func_hashtab.ht_used;
5292 int changed = func_hashtab.ht_changed;
5293 hashitem_T *hi;
5294
5295 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5296 {
5297 if (!HASHITEM_EMPTY(hi))
5298 {
5299 --todo;
5300 ufunc = HI2UF(hi);
5301 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5302 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5303 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005304 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005305 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5306
5307 if (func_hashtab.ht_changed != changed)
5308 {
5309 // a function has been added or removed, need to start
5310 // over
5311 todo = (long)func_hashtab.ht_used;
5312 changed = func_hashtab.ht_changed;
5313 hi = func_hashtab.ht_array;
5314 --hi;
5315 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005316 }
5317 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005318 }
5319 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005320}
5321
5322/*
5323 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5324 * Return 2 if "p" starts with "s:".
5325 * Return 0 otherwise.
5326 */
5327 int
5328eval_fname_script(char_u *p)
5329{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005330 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5331 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005332 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5333 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5334 return 5;
5335 if (p[0] == 's' && p[1] == ':')
5336 return 2;
5337 return 0;
5338}
5339
5340 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005341translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005342{
5343 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005344 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005345 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005346}
5347
5348/*
5349 * Return TRUE when "ufunc" has old-style "..." varargs
5350 * or named varargs "...name: type".
5351 */
5352 int
5353has_varargs(ufunc_T *ufunc)
5354{
5355 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005356}
5357
5358/*
5359 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005360 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005361 */
5362 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005363function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005364{
5365 char_u *nm = name;
5366 char_u *p;
5367 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005368 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005369 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005370
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005371 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5372 if (no_deref)
5373 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005374 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005375 nm = skipwhite(nm);
5376
Bram Moolenaare38eab22019-12-05 21:50:01 +01005377 // Only accept "funcname", "funcname ", "funcname (..." and
5378 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005379 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005380 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005381 vim_free(p);
5382 return n;
5383}
5384
Bram Moolenaar113e1072019-01-20 15:30:40 +01005385#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005386 char_u *
5387get_expanded_name(char_u *name, int check)
5388{
5389 char_u *nm = name;
5390 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005391 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005392
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005393 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005394 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005395
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005396 if (p != NULL && *nm == NUL
5397 && (!check || translated_function_exists(p, is_global)))
5398 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005399
5400 vim_free(p);
5401 return NULL;
5402}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005403#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005404
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005405/*
5406 * Function given to ExpandGeneric() to obtain the list of user defined
5407 * function names.
5408 */
5409 char_u *
5410get_user_func_name(expand_T *xp, int idx)
5411{
5412 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005413 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005414 static hashitem_T *hi;
5415 ufunc_T *fp;
5416
5417 if (idx == 0)
5418 {
5419 done = 0;
5420 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005421 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005422 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005423 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005424 {
5425 if (done++ > 0)
5426 ++hi;
5427 while (HASHITEM_EMPTY(hi))
5428 ++hi;
5429 fp = HI2UF(hi);
5430
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431 // don't show dead, dict and lambda functions
5432 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005433 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005434 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005435
5436 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005437 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005438
5439 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005440 if (xp->xp_context != EXPAND_USER_FUNC
5441 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005442 {
5443 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005444 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005445 STRCAT(IObuff, ")");
5446 }
5447 return IObuff;
5448 }
5449 return NULL;
5450}
5451
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005452/*
5453 * ":delfunction {name}"
5454 */
5455 void
5456ex_delfunction(exarg_T *eap)
5457{
5458 ufunc_T *fp = NULL;
5459 char_u *p;
5460 char_u *name;
5461 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005462 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005463
5464 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005465 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
5466 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005467 vim_free(fudi.fd_newkey);
5468 if (name == NULL)
5469 {
5470 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005471 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005472 return;
5473 }
5474 if (!ends_excmd(*skipwhite(p)))
5475 {
5476 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005477 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005478 return;
5479 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005480 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005481 if (eap->nextcmd != NULL)
5482 *p = NUL;
5483
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005484 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005485 {
5486 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005487 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005488 vim_free(name);
5489 return;
5490 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005491 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005492 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005493 vim_free(name);
5494
5495 if (!eap->skip)
5496 {
5497 if (fp == NULL)
5498 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005499 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005500 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005501 return;
5502 }
5503 if (fp->uf_calls > 0)
5504 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005505 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005506 return;
5507 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005508 if (fp->uf_flags & FC_VIM9)
5509 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005510 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005511 return;
5512 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005513
5514 if (fudi.fd_dict != NULL)
5515 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005516 // Delete the dict item that refers to the function, it will
5517 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005518 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005519 }
5520 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005521 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005522 // A normal function (not a numbered function or lambda) has a
5523 // refcount of 1 for the entry in the hashtable. When deleting
5524 // it and the refcount is more than one, it should be kept.
5525 // A numbered function and lambda should be kept if the refcount is
5526 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005527 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005528 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005529 // Function is still referenced somewhere. Don't free it but
5530 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005531 if (func_remove(fp))
5532 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005533 }
5534 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005535 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005536 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005537 }
5538}
5539
5540/*
5541 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005542 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005543 */
5544 void
5545func_unref(char_u *name)
5546{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005547 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005548
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005549 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005550 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005551 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005552 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005553 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005554#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005555 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005556#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005557 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005558 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005559 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005560}
5561
5562/*
5563 * Unreference a Function: decrement the reference count and free it when it
5564 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005565 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005566 */
5567 void
5568func_ptr_unref(ufunc_T *fp)
5569{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005570 if (fp != NULL && (--fp->uf_refcount <= 0
5571 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5572 && fp->uf_partial->pt_refcount <= 1
5573 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005574 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005575 // Only delete it when it's not being used. Otherwise it's done
5576 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005577 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005578 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005579 }
5580}
5581
5582/*
5583 * Count a reference to a Function.
5584 */
5585 void
5586func_ref(char_u *name)
5587{
5588 ufunc_T *fp;
5589
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005590 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005591 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005592 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005593 if (fp != NULL)
5594 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005595 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005596 // Only give an error for a numbered function.
5597 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005598 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005599}
5600
5601/*
5602 * Count a reference to a Function.
5603 */
5604 void
5605func_ptr_ref(ufunc_T *fp)
5606{
5607 if (fp != NULL)
5608 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005609}
5610
5611/*
5612 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5613 * referenced from anywhere that is in use.
5614 */
5615 static int
5616can_free_funccal(funccall_T *fc, int copyID)
5617{
Bram Moolenaarca16c602022-09-06 18:57:08 +01005618 return (fc->fc_l_varlist.lv_copyID != copyID
5619 && fc->fc_l_vars.dv_copyID != copyID
5620 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005621 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005622}
5623
5624/*
5625 * ":return [expr]"
5626 */
5627 void
5628ex_return(exarg_T *eap)
5629{
5630 char_u *arg = eap->arg;
5631 typval_T rettv;
5632 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005633 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005634
5635 if (current_funccal == NULL)
5636 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005637 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005638 return;
5639 }
5640
Bram Moolenaar844fb642021-10-23 13:32:30 +01005641 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005642 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5643
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005644 if (eap->skip)
5645 ++emsg_skip;
5646
5647 eap->nextcmd = NULL;
5648 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005649 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005650 {
5651 if (!eap->skip)
5652 returning = do_return(eap, FALSE, TRUE, &rettv);
5653 else
5654 clear_tv(&rettv);
5655 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005656 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005657 else if (!eap->skip)
5658 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005659 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005660 update_force_abort();
5661
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005662 /*
5663 * Return unless the expression evaluation has been cancelled due to an
5664 * aborting error, an interrupt, or an exception.
5665 */
5666 if (!aborting())
5667 returning = do_return(eap, FALSE, TRUE, NULL);
5668 }
5669
Bram Moolenaare38eab22019-12-05 21:50:01 +01005670 // When skipping or the return gets pending, advance to the next command
5671 // in this line (!returning). Otherwise, ignore the rest of the line.
5672 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005673 if (returning)
5674 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005675 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005676 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005677
5678 if (eap->skip)
5679 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005680 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005681}
5682
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005683 static int
5684ex_call_inner(
5685 exarg_T *eap,
5686 char_u *name,
5687 char_u **arg,
5688 char_u *startarg,
5689 funcexe_T *funcexe_init,
5690 evalarg_T *evalarg)
5691{
5692 linenr_T lnum;
5693 int doesrange;
5694 typval_T rettv;
5695 int failed = FALSE;
5696
5697 /*
5698 * When skipping, evaluate the function once, to find the end of the
5699 * arguments.
5700 * When the function takes a range, this is discovered after the first
5701 * call, and the loop is broken.
5702 */
5703 if (eap->skip)
5704 {
5705 ++emsg_skip;
5706 lnum = eap->line2; // do it once, also with an invalid range
5707 }
5708 else
5709 lnum = eap->line1;
5710 for ( ; lnum <= eap->line2; ++lnum)
5711 {
5712 funcexe_T funcexe;
5713
5714 if (!eap->skip && eap->addr_count > 0)
5715 {
5716 if (lnum > curbuf->b_ml.ml_line_count)
5717 {
5718 // If the function deleted lines or switched to another buffer
5719 // the line number may become invalid.
5720 emsg(_(e_invalid_range));
5721 break;
5722 }
5723 curwin->w_cursor.lnum = lnum;
5724 curwin->w_cursor.col = 0;
5725 curwin->w_cursor.coladd = 0;
5726 }
5727 *arg = startarg;
5728
5729 funcexe = *funcexe_init;
5730 funcexe.fe_doesrange = &doesrange;
5731 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
5732 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
5733 {
5734 failed = TRUE;
5735 break;
5736 }
5737 if (has_watchexpr())
5738 dbg_check_breakpoint(eap);
5739
5740 // Handle a function returning a Funcref, Dictionary or List.
5741 if (handle_subscript(arg, NULL, &rettv,
5742 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
5743 {
5744 failed = TRUE;
5745 break;
5746 }
5747
5748 clear_tv(&rettv);
5749 if (doesrange || eap->skip)
5750 break;
5751
5752 // Stop when immediately aborting on error, or when an interrupt
5753 // occurred or an exception was thrown but not caught.
5754 // get_func_tv() returned OK, so that the check for trailing
5755 // characters below is executed.
5756 if (aborting())
5757 break;
5758 }
5759 if (eap->skip)
5760 --emsg_skip;
5761 return failed;
5762}
5763
5764/*
5765 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
5766 * Returns FAIL or OK.
5767 */
5768 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01005769ex_defer_inner(
5770 char_u *name,
5771 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01005772 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01005773 partial_T *partial,
5774 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005775{
5776 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01005777 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005778 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01005779 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005780
5781 if (current_funccal == NULL)
5782 {
5783 semsg(_(e_str_not_inside_function), "defer");
5784 return FAIL;
5785 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01005786 if (partial != NULL)
5787 {
5788 if (partial->pt_dict != NULL)
5789 {
5790 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
5791 return FAIL;
5792 }
5793 if (partial->pt_argc > 0)
5794 {
5795 int i;
5796
5797 partial_argc = partial->pt_argc;
5798 for (i = 0; i < partial_argc; ++i)
5799 copy_tv(&partial->pt_argv[i], &argvars[i]);
5800 }
5801 }
5802 r = get_func_arguments(arg, evalarg, FALSE,
5803 argvars + partial_argc, &argcount);
5804 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01005805
5806 if (r == OK)
5807 {
5808 if (type != NULL)
5809 {
5810 // Check that the arguments are OK for the types of the funcref.
5811 r = check_argument_types(type, argvars, argcount, NULL, name);
5812 }
5813 else if (builtin_function(name, -1))
5814 {
5815 int idx = find_internal_func(name);
5816
5817 if (idx < 0)
5818 {
5819 emsg_funcname(e_unknown_function_str, name);
5820 r = FAIL;
5821 }
5822 else if (check_internal_func(idx, argcount) == -1)
5823 r = FAIL;
5824 }
5825 else
5826 {
5827 ufunc_T *ufunc = find_func(name, FALSE);
5828
5829 // we tolerate an unknown function here, it might be defined later
5830 if (ufunc != NULL)
5831 {
5832 int error = check_user_func_argcount(ufunc, argcount);
5833
5834 if (error != FCERR_UNKNOWN)
5835 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01005836 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01005837 r = FAIL;
5838 }
5839 }
5840 }
5841 }
5842
Bram Moolenaar86d87252022-09-05 21:21:25 +01005843 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01005844 {
5845 while (--argcount >= 0)
5846 clear_tv(&argvars[argcount]);
5847 return FAIL;
5848 }
5849 return add_defer(name, argcount, argvars);
5850}
5851
5852/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01005853 * Return TRUE if currently inside a function call.
5854 * Give an error message and return FALSE when not.
5855 */
5856 int
5857can_add_defer(void)
5858{
5859 if (!in_def_function() && get_current_funccal() == NULL)
5860 {
5861 semsg(_(e_str_not_inside_function), "defer");
5862 return FALSE;
5863 }
5864 return TRUE;
5865}
5866
5867/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005868 * Add a deferred call for "name" with arguments "argvars[argcount]".
5869 * Consumes "argvars[]".
5870 * Caller must check that in_def_function() returns TRUE or current_funccal is
5871 * not NULL.
5872 * Returns OK or FAIL.
5873 */
5874 int
5875add_defer(char_u *name, int argcount_arg, typval_T *argvars)
5876{
5877 char_u *saved_name = vim_strsave(name);
5878 int argcount = argcount_arg;
5879 defer_T *dr;
5880 int ret = FAIL;
5881
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005882 if (saved_name == NULL)
5883 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01005884 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005885 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01005886 if (add_defer_function(saved_name, argcount, argvars) == OK)
5887 argcount = 0;
5888 }
5889 else
5890 {
5891 if (current_funccal->fc_defer.ga_itemsize == 0)
5892 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
5893 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
5894 goto theend;
5895 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
5896 + current_funccal->fc_defer.ga_len++;
5897 dr->dr_name = saved_name;
5898 dr->dr_argcount = argcount;
5899 while (argcount > 0)
5900 {
5901 --argcount;
5902 dr->dr_argvars[argcount] = argvars[argcount];
5903 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005904 }
5905 ret = OK;
5906
5907theend:
5908 while (--argcount >= 0)
5909 clear_tv(&argvars[argcount]);
5910 return ret;
5911}
5912
5913/*
5914 * Invoked after a functions has finished: invoke ":defer" functions.
5915 */
Bram Moolenaar58779852022-09-06 18:31:14 +01005916 static void
5917handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005918{
5919 int idx;
5920
Bram Moolenaar58779852022-09-06 18:31:14 +01005921 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005922 {
5923 funcexe_T funcexe;
5924 typval_T rettv;
Bram Moolenaar58779852022-09-06 18:31:14 +01005925 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005926 int i;
5927
5928 CLEAR_FIELD(funcexe);
5929 funcexe.fe_evaluate = TRUE;
5930
5931 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
5932 call_func(dr->dr_name, -1, &rettv,
5933 dr->dr_argcount, dr->dr_argvars, &funcexe);
5934 clear_tv(&rettv);
5935 vim_free(dr->dr_name);
5936 for (i = dr->dr_argcount - 1; i >= 0; --i)
5937 clear_tv(&dr->dr_argvars[i]);
5938 }
Bram Moolenaar58779852022-09-06 18:31:14 +01005939 ga_clear(&funccal->fc_defer);
5940}
5941
5942/*
5943 * Called when exiting: call all defer functions.
5944 */
5945 void
5946invoke_all_defer(void)
5947{
5948 funccall_T *funccal;
5949
Bram Moolenaarca16c602022-09-06 18:57:08 +01005950 for (funccal = current_funccal; funccal != NULL;
5951 funccal = funccal->fc_caller)
Bram Moolenaar58779852022-09-06 18:31:14 +01005952 if (funccal->fc_ectx != NULL)
5953 {
5954 // :def function
5955 unwind_def_callstack(funccal->fc_ectx);
5956 may_invoke_defer_funcs(funccal->fc_ectx);
5957 }
5958 else
5959 {
5960 // legacy function
5961 handle_defer_one(funccal);
5962 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005963}
5964
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005965/*
5966 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005967 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005968 */
5969 void
5970ex_call(exarg_T *eap)
5971{
5972 char_u *arg = eap->arg;
5973 char_u *startarg;
5974 char_u *name;
5975 char_u *tofree;
5976 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005977 int failed = FALSE;
5978 funcdict_T fudi;
5979 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005980 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005981 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005982 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005983 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005984
Bram Moolenaare6b53242020-07-01 17:28:33 +02005985 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005986 if (eap->skip)
5987 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005988 typval_T rettv;
5989
Bram Moolenaare38eab22019-12-05 21:50:01 +01005990 // trans_function_name() doesn't work well when skipping, use eval0()
5991 // instead to skip to any following command, e.g. for:
5992 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005993 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005994 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005995 clear_tv(&rettv);
5996 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005997 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005998 return;
5999 }
6000
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006001 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006002 &fudi, &partial, vim9script ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006003 if (fudi.fd_newkey != NULL)
6004 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006005 // Still need to give an error message for missing key.
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006006 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006007 vim_free(fudi.fd_newkey);
6008 }
6009 if (tofree == NULL)
6010 return;
6011
Bram Moolenaare38eab22019-12-05 21:50:01 +01006012 // Increase refcount on dictionary, it could get deleted when evaluating
6013 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006014 if (fudi.fd_dict != NULL)
6015 ++fudi.fd_dict->dv_refcount;
6016
Bram Moolenaare38eab22019-12-05 21:50:01 +01006017 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6018 // contents. For VAR_PARTIAL get its partial, unless we already have one
6019 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006020 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006021 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006022 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006023 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006024
Bram Moolenaare38eab22019-12-05 21:50:01 +01006025 // Skip white space to allow ":call func ()". Not good, but required for
6026 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006027 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006028 if (*startarg != '(')
6029 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006030 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006031 goto end;
6032 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006033 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006034 {
6035 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6036 goto end;
6037 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006038
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006039 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006040 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006041 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006042 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006043 }
6044 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006045 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006046 funcexe_T funcexe;
6047
Bram Moolenaara80faa82020-04-12 19:37:17 +02006048 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006049 funcexe.fe_check_type = type;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006050 funcexe.fe_partial = partial;
6051 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006052 funcexe.fe_firstline = eap->line1;
6053 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006054 funcexe.fe_found_var = found_var;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006055 funcexe.fe_evaluate = !eap->skip;
6056 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006057 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006058
Bram Moolenaar1d341892021-09-18 15:25:52 +02006059 // When inside :try we need to check for following "| catch" or "| endtry".
6060 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006061 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006062 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006063 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006064 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006065 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006066 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006067 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006068 {
6069 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006070 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006071 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006072 }
6073 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006074 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006075 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006076 // Must be after using "arg", it may point into memory cleared here.
6077 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006078
6079end:
6080 dict_unref(fudi.fd_dict);
6081 vim_free(tofree);
6082}
6083
6084/*
6085 * Return from a function. Possibly makes the return pending. Also called
6086 * for a pending return at the ":endtry" or after returning from an extra
6087 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6088 * when called due to a ":return" command. "rettv" may point to a typval_T
6089 * with the return rettv. Returns TRUE when the return can be carried out,
6090 * FALSE when the return gets pending.
6091 */
6092 int
6093do_return(
6094 exarg_T *eap,
6095 int reanimate,
6096 int is_cmd,
6097 void *rettv)
6098{
6099 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006100 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006101
6102 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006103 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006104 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006105
6106 /*
6107 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6108 * not in its finally clause (which then is to be executed next) is found.
6109 * In this case, make the ":return" pending for execution at the ":endtry".
6110 * Otherwise, return normally.
6111 */
6112 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6113 if (idx >= 0)
6114 {
6115 cstack->cs_pending[idx] = CSTP_RETURN;
6116
6117 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006118 // A pending return again gets pending. "rettv" points to an
6119 // allocated variable with the rettv of the original ":return"'s
6120 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006121 cstack->cs_rettv[idx] = rettv;
6122 else
6123 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006124 // When undoing a return in order to make it pending, get the stored
6125 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006126 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006127 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006128
6129 if (rettv != NULL)
6130 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006131 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006132 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6133 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6134 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006135 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006136 }
6137 else
6138 cstack->cs_rettv[idx] = NULL;
6139
6140 if (reanimate)
6141 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006142 // The pending return value could be overwritten by a ":return"
6143 // without argument in a finally clause; reset the default
6144 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006145 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6146 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006147 }
6148 }
6149 report_make_pending(CSTP_RETURN, rettv);
6150 }
6151 else
6152 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006153 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006154
Bram Moolenaare38eab22019-12-05 21:50:01 +01006155 // If the return is carried out now, store the return value. For
6156 // a return immediately after reanimation, the value is already
6157 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006158 if (!reanimate && rettv != NULL)
6159 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006160 clear_tv(current_funccal->fc_rettv);
6161 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006162 if (!is_cmd)
6163 vim_free(rettv);
6164 }
6165 }
6166
6167 return idx < 0;
6168}
6169
6170/*
6171 * Free the variable with a pending return value.
6172 */
6173 void
6174discard_pending_return(void *rettv)
6175{
6176 free_tv((typval_T *)rettv);
6177}
6178
6179/*
6180 * Generate a return command for producing the value of "rettv". The result
6181 * is an allocated string. Used by report_pending() for verbose messages.
6182 */
6183 char_u *
6184get_return_cmd(void *rettv)
6185{
6186 char_u *s = NULL;
6187 char_u *tofree = NULL;
6188 char_u numbuf[NUMBUFLEN];
6189
6190 if (rettv != NULL)
6191 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6192 if (s == NULL)
6193 s = (char_u *)"";
6194
6195 STRCPY(IObuff, ":return ");
6196 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6197 if (STRLEN(s) + 8 >= IOSIZE)
6198 STRCPY(IObuff + IOSIZE - 4, "...");
6199 vim_free(tofree);
6200 return vim_strsave(IObuff);
6201}
6202
6203/*
6204 * Get next function line.
6205 * Called by do_cmdline() to get the next line.
6206 * Returns allocated string, or NULL for end of function.
6207 */
6208 char_u *
6209get_func_line(
6210 int c UNUSED,
6211 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006212 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006213 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006214{
6215 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006216 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006217 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006218 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006219
Bram Moolenaare38eab22019-12-05 21:50:01 +01006220 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006221 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006222 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006223 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006224 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006225 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006226 }
6227#ifdef FEAT_PROFILE
6228 if (do_profiling == PROF_YES)
6229 func_line_end(cookie);
6230#endif
6231
6232 gap = &fp->uf_lines;
6233 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006234 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006235 retval = NULL;
6236 else
6237 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006238 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006239 while (fcp->fc_linenr < gap->ga_len
6240 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6241 ++fcp->fc_linenr;
6242 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006243 retval = NULL;
6244 else
6245 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006246 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6247 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006248#ifdef FEAT_PROFILE
6249 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006250 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006251#endif
6252 }
6253 }
6254
Bram Moolenaare38eab22019-12-05 21:50:01 +01006255 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006256 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006257 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006258 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006259 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006260 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006261 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006262 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006263 }
6264
6265 return retval;
6266}
6267
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006268/*
6269 * Return TRUE if the currently active function should be ended, because a
6270 * return was encountered or an error occurred. Used inside a ":while".
6271 */
6272 int
6273func_has_ended(void *cookie)
6274{
6275 funccall_T *fcp = (funccall_T *)cookie;
6276
Bram Moolenaare38eab22019-12-05 21:50:01 +01006277 // Ignore the "abort" flag if the abortion behavior has been changed due to
6278 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006279 return (((fcp->fc_func->uf_flags & FC_ABORT)
6280 && did_emsg && !aborted_in_try())
6281 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006282}
6283
6284/*
6285 * return TRUE if cookie indicates a function which "abort"s on errors.
6286 */
6287 int
6288func_has_abort(
6289 void *cookie)
6290{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006291 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006292}
6293
6294
6295/*
6296 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6297 * Don't do this when "Func" is already a partial that was bound
6298 * explicitly (pt_auto is FALSE).
6299 * Changes "rettv" in-place.
6300 * Returns the updated "selfdict_in".
6301 */
6302 dict_T *
6303make_partial(dict_T *selfdict_in, typval_T *rettv)
6304{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006305 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006306 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006307 char_u fname_buf[FLEN_FIXED + 1];
6308 int error;
6309 dict_T *selfdict = selfdict_in;
6310
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006311 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6312 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006313 fp = rettv->vval.v_partial->pt_func;
6314 else
6315 {
6316 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006317 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006318 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006319 if (fname == NULL)
6320 {
6321 // There is no point binding a dict to a NULL function, just create
6322 // a function reference.
6323 rettv->v_type = VAR_FUNC;
6324 rettv->vval.v_string = NULL;
6325 }
6326 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006327 {
6328 char_u *tofree = NULL;
6329
6330 // Translate "s:func" to the stored function name.
6331 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6332 fp = find_func(fname, FALSE);
6333 vim_free(tofree);
6334 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006335 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006336
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006337 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006338 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006339 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006340
6341 if (pt != NULL)
6342 {
6343 pt->pt_refcount = 1;
6344 pt->pt_dict = selfdict;
6345 pt->pt_auto = TRUE;
6346 selfdict = NULL;
6347 if (rettv->v_type == VAR_FUNC)
6348 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006349 // Just a function: Take over the function name and use
6350 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006351 pt->pt_name = rettv->vval.v_string;
6352 }
6353 else
6354 {
6355 partial_T *ret_pt = rettv->vval.v_partial;
6356 int i;
6357
Bram Moolenaare38eab22019-12-05 21:50:01 +01006358 // Partial: copy the function name, use selfdict and copy
6359 // args. Can't take over name or args, the partial might
6360 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006361 if (ret_pt->pt_name != NULL)
6362 {
6363 pt->pt_name = vim_strsave(ret_pt->pt_name);
6364 func_ref(pt->pt_name);
6365 }
6366 else
6367 {
6368 pt->pt_func = ret_pt->pt_func;
6369 func_ptr_ref(pt->pt_func);
6370 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006371 if (ret_pt->pt_argc > 0)
6372 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006373 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006374 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006375 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006376 pt->pt_argc = 0;
6377 else
6378 {
6379 pt->pt_argc = ret_pt->pt_argc;
6380 for (i = 0; i < pt->pt_argc; i++)
6381 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6382 }
6383 }
6384 partial_unref(ret_pt);
6385 }
6386 rettv->v_type = VAR_PARTIAL;
6387 rettv->vval.v_partial = pt;
6388 }
6389 }
6390 return selfdict;
6391}
6392
6393/*
6394 * Return the name of the executed function.
6395 */
6396 char_u *
6397func_name(void *cookie)
6398{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006399 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006400}
6401
6402/*
6403 * Return the address holding the next breakpoint line for a funccall cookie.
6404 */
6405 linenr_T *
6406func_breakpoint(void *cookie)
6407{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006408 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006409}
6410
6411/*
6412 * Return the address holding the debug tick for a funccall cookie.
6413 */
6414 int *
6415func_dbg_tick(void *cookie)
6416{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006417 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006418}
6419
6420/*
6421 * Return the nesting level for a funccall cookie.
6422 */
6423 int
6424func_level(void *cookie)
6425{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006426 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006427}
6428
6429/*
6430 * Return TRUE when a function was ended by a ":return" command.
6431 */
6432 int
6433current_func_returned(void)
6434{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006435 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006436}
6437
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006438 int
6439free_unref_funccal(int copyID, int testing)
6440{
6441 int did_free = FALSE;
6442 int did_free_funccal = FALSE;
6443 funccall_T *fc, **pfc;
6444
6445 for (pfc = &previous_funccal; *pfc != NULL; )
6446 {
6447 if (can_free_funccal(*pfc, copyID))
6448 {
6449 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006450 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006451 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006452 did_free = TRUE;
6453 did_free_funccal = TRUE;
6454 }
6455 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006456 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006457 }
6458 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006459 // When a funccal was freed some more items might be garbage
6460 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006461 (void)garbage_collect(testing);
6462
6463 return did_free;
6464}
6465
6466/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006467 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006468 */
6469 static funccall_T *
6470get_funccal(void)
6471{
6472 int i;
6473 funccall_T *funccal;
6474 funccall_T *temp_funccal;
6475
6476 funccal = current_funccal;
6477 if (debug_backtrace_level > 0)
6478 {
6479 for (i = 0; i < debug_backtrace_level; i++)
6480 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006481 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006482 if (temp_funccal)
6483 funccal = temp_funccal;
6484 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006485 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006486 debug_backtrace_level = i;
6487 }
6488 }
6489 return funccal;
6490}
6491
6492/*
6493 * Return the hashtable used for local variables in the current funccal.
6494 * Return NULL if there is no current funccal.
6495 */
6496 hashtab_T *
6497get_funccal_local_ht()
6498{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006499 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006500 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006501 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006502}
6503
6504/*
6505 * Return the l: scope variable.
6506 * Return NULL if there is no current funccal.
6507 */
6508 dictitem_T *
6509get_funccal_local_var()
6510{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006511 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006512 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006513 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006514}
6515
6516/*
6517 * Return the hashtable used for argument in the current funccal.
6518 * Return NULL if there is no current funccal.
6519 */
6520 hashtab_T *
6521get_funccal_args_ht()
6522{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006523 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006524 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006525 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006526}
6527
6528/*
6529 * Return the a: scope variable.
6530 * Return NULL if there is no current funccal.
6531 */
6532 dictitem_T *
6533get_funccal_args_var()
6534{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006535 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006536 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006537 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006538}
6539
6540/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006541 * List function variables, if there is a function.
6542 */
6543 void
6544list_func_vars(int *first)
6545{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006546 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6547 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006548 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006549}
6550
6551/*
6552 * If "ht" is the hashtable for local variables in the current funccal, return
6553 * the dict that contains it.
6554 * Otherwise return NULL.
6555 */
6556 dict_T *
6557get_current_funccal_dict(hashtab_T *ht)
6558{
6559 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006560 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6561 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006562 return NULL;
6563}
6564
6565/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006566 * Search hashitem in parent scope.
6567 */
6568 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006569find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006570{
6571 funccall_T *old_current_funccal = current_funccal;
6572 hashtab_T *ht;
6573 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006574 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006575
Bram Moolenaarca16c602022-09-06 18:57:08 +01006576 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006577 return NULL;
6578
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006579 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006580 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006581 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006582 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006583 ht = find_var_ht(name, &varname);
6584 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006585 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006586 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006587 if (!HASHITEM_EMPTY(hi))
6588 {
6589 *pht = ht;
6590 break;
6591 }
6592 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006593 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006594 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006595 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006596 }
6597 current_funccal = old_current_funccal;
6598
6599 return hi;
6600}
6601
6602/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006603 * Search variable in parent scope.
6604 */
6605 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006606find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006607{
6608 dictitem_T *v = NULL;
6609 funccall_T *old_current_funccal = current_funccal;
6610 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006611 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006612
Bram Moolenaarca16c602022-09-06 18:57:08 +01006613 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006614 return NULL;
6615
Bram Moolenaare38eab22019-12-05 21:50:01 +01006616 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01006617 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006618 while (current_funccal)
6619 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006620 ht = find_var_ht(name, &varname);
6621 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006622 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006623 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006624 if (v != NULL)
6625 break;
6626 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006627 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006628 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006629 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006630 }
6631 current_funccal = old_current_funccal;
6632
6633 return v;
6634}
6635
6636/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006637 * Set "copyID + 1" in previous_funccal and callers.
6638 */
6639 int
6640set_ref_in_previous_funccal(int copyID)
6641{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006642 funccall_T *fc;
6643
Bram Moolenaarca16c602022-09-06 18:57:08 +01006644 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006645 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006646 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006647 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
6648 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
6649 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006650 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006651 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006652 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006653}
6654
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006655 static int
6656set_ref_in_funccal(funccall_T *fc, int copyID)
6657{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006658 if (fc->fc_copyID != copyID)
6659 {
6660 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006661 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
6662 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
6663 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
6664 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006665 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006666 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006667 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006668}
6669
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006670/*
6671 * Set "copyID" in all local vars and arguments in the call stack.
6672 */
6673 int
6674set_ref_in_call_stack(int copyID)
6675{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006676 funccall_T *fc;
6677 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006678
Bram Moolenaarca16c602022-09-06 18:57:08 +01006679 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006680 if (set_ref_in_funccal(fc, copyID))
6681 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006682
6683 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006684 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006685 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006686 if (set_ref_in_funccal(fc, copyID))
6687 return TRUE;
6688 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006689}
6690
6691/*
6692 * Set "copyID" in all functions available by name.
6693 */
6694 int
6695set_ref_in_functions(int copyID)
6696{
6697 int todo;
6698 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006699 ufunc_T *fp;
6700
6701 todo = (int)func_hashtab.ht_used;
6702 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006703 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006704 if (!HASHITEM_EMPTY(hi))
6705 {
6706 --todo;
6707 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006708 if (!func_name_refcount(fp->uf_name)
6709 && set_ref_in_func(NULL, fp, copyID))
6710 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006711 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006712 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006713 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006714}
6715
6716/*
6717 * Set "copyID" in all function arguments.
6718 */
6719 int
6720set_ref_in_func_args(int copyID)
6721{
6722 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006723
6724 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006725 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
6726 copyID, NULL, NULL))
6727 return TRUE;
6728 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006729}
6730
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006731/*
6732 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006733 * Returns TRUE if setting references failed somehow.
6734 */
6735 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006736set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006737{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006738 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006739 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01006740 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006741 char_u fname_buf[FLEN_FIXED + 1];
6742 char_u *tofree = NULL;
6743 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006744 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006745
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006746 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006747 return FALSE;
6748
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006749 if (fp_in == NULL)
6750 {
6751 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006752 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006753 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006754 if (fp != NULL)
6755 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006756 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006757 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006758 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02006759
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006760 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006761 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006762}
6763
Bram Moolenaare38eab22019-12-05 21:50:01 +01006764#endif // FEAT_EVAL