blob: e2b1bc3226d52444f2031d241e426cbf142d0c6e [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)
Christian Brabandt20764632023-11-12 16:59:29 +010017
18#define MAX_CALLBACK_DEPTH 20
19
Bram Moolenaara9b579f2016-07-17 18:29:19 +020020/*
21 * All user-defined functions are found in this hashtable.
22 */
23static hashtab_T func_hashtab;
24
Bram Moolenaare38eab22019-12-05 21:50:01 +010025// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020026static garray_T funcargs = GA_EMPTY;
27
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// pointer to funccal for currently active function
29static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020030
Bram Moolenaar209b8e32019-03-14 13:43:24 +010031// Pointer to list of previously used funccal, still around because some
32// item in it is still being used.
33static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020034
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020035static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010036static void func_clear(ufunc_T *fp, int force);
37static int func_free(ufunc_T *fp, int force);
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +010038static char_u *untrans_function_name(char_u *name);
Bram Moolenaar58779852022-09-06 18:31:14 +010039static void handle_defer_one(funccall_T *funccal);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020040
41 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +000042func_init(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020043{
44 hash_init(&func_hashtab);
45}
46
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020047/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020048 * Return the function hash table
49 */
50 hashtab_T *
51func_tbl_get(void)
52{
53 return &func_hashtab;
54}
55
56/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020057 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020058 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010059 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010060 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000061 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010062 * Return a pointer to after the type.
63 * When something is wrong return "arg".
64 */
65 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010066one_function_arg(
67 char_u *arg,
68 garray_T *newargs,
69 garray_T *argtypes,
70 int types_optional,
h-eastb895b0f2023-09-24 15:46:31 +020071 garray_T *arg_objm,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010072 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000073 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020074 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010075 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010076{
Bram Moolenaar6e949782020-04-13 17:21:00 +020077 char_u *p = arg;
78 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020079 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010080
81 while (ASCII_ISALNUM(*p) || *p == '_')
82 ++p;
83 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020084 || (argtypes == NULL
85 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
86 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087 {
88 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000089 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 return arg;
91 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010092
Bram Moolenaarce723f32023-06-10 19:00:12 +010093 // Extra checks in Vim9 script.
94 if (!skip && argtypes != NULL)
95 {
96 int c = *p;
97 *p = NUL;
98 int r = check_reserved_name(arg, FALSE);
99 *p = c;
100 if (r == FAIL)
101 return arg;
102
103 // Cannot use script var name for argument. In function: also check
104 // local vars and arguments.
105 if (check_defined(arg, p - arg,
106 evalarg == NULL ? NULL : evalarg->eval_cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +0000107 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarce723f32023-06-10 19:00:12 +0100108 return arg;
109 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100110
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100111 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
112 return arg;
113 if (newargs != NULL)
114 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 int c;
116 int i;
117
118 c = *p;
119 *p = NUL;
120 arg_copy = vim_strsave(arg);
121 if (arg_copy == NULL)
122 {
123 *p = c;
124 return arg;
125 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200126 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200127 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200128 // Check for duplicate argument name.
129 for (i = 0; i < newargs->ga_len; ++i)
130 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
131 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000132 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200133 vim_free(arg_copy);
134 return arg;
135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100136 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
137 newargs->ga_len++;
138
139 *p = c;
140 }
141
142 // get any type from "arg: type"
h-eastb895b0f2023-09-24 15:46:31 +0200143 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK)
144 && arg_objm != NULL && (skip || ga_grow(arg_objm, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145 {
146 char_u *type = NULL;
147
Bram Moolenaar6e949782020-04-13 17:21:00 +0200148 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
149 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200150 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200151 arg_copy == NULL ? arg : arg_copy);
152 p = skipwhite(p);
153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100154 if (*p == ':')
155 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200156 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100157 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200158 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100159 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200160 return arg;
161 }
162 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200163 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100164 if (!skip)
165 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200167 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200168 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200169 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200170 arg_copy == NULL ? arg : arg_copy);
171 return arg;
172 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100173 if (!skip)
174 {
175 if (type == NULL && types_optional)
176 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200177 type = vim_strsave((char_u *)
178 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100179 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
h-eastb895b0f2023-09-24 15:46:31 +0200180 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = FALSE;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 }
183
184 return p;
185}
186
187/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000188 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000189 * Get a next line, store it in "eap" if appropriate and put the line in
190 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000191 */
192 static char_u *
193get_function_line(
194 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000195 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000196 int indent,
197 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000198{
199 char_u *theline;
200
201 if (eap->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000202 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000203 else
204 theline = eap->getline(':', eap->cookie, indent, getline_options);
205 if (theline != NULL)
206 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000207 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000208 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000209 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
210 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000211 *eap->cmdlinep = theline;
Bram Moolenaarb5820102023-01-25 12:27:13 +0000212 (void)ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000213 }
214
215 return theline;
216}
217
218/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200219 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100220 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100221 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200222 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100223 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200224get_function_args(
225 char_u **argp,
226 char_u endchar,
227 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100229 int types_optional, // types optional if "argtypes" is not NULL
h-eastb895b0f2023-09-24 15:46:31 +0200230 garray_T *arg_objm, // NULL unless using :def
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100231 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200232 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200233 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200234 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000235 exarg_T *eap, // can be NULL
Bram Moolenaar554d0312023-01-05 19:59:18 +0000236 int in_class, // non-zero when inside a class or interface
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000237 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000238 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200239{
240 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100241 char_u *arg;
242 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200243 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200244 int any_default = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100245 char_u *whitep = *argp;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100246 int need_expr = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200247
248 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000249 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100250 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000251 ga_init2(argtypes, sizeof(char_u *), 3);
h-eastb895b0f2023-09-24 15:46:31 +0200252 if (arg_objm != NULL)
253 ga_init2(arg_objm, sizeof(int8_T), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200254 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000255 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200256
257 if (varargs != NULL)
258 *varargs = FALSE;
259
260 /*
261 * Isolate the arguments: "arg1, arg2, ...)"
262 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100263 arg = skipwhite(*argp);
264 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200265 while (*p != endchar)
266 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200267 while (eap != NULL && eap->getline != NULL
268 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200269 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200270 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000271 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000272 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000273
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200274 if (theline == NULL)
275 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200276 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200277 p = skipwhite(theline);
278 }
279
280 if (mustend && *p != endchar)
281 {
282 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000283 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100284 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200285 }
Christian Brabandte4a450a2023-12-08 20:57:38 +0100286 if (*p == endchar && !need_expr)
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200287 break;
288
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200289 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
290 {
291 if (varargs != NULL)
292 *varargs = TRUE;
293 p += 3;
294 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295
296 if (argtypes != NULL)
297 {
298 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200299 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100300 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100301 if (!skip)
302 emsg(_(e_missing_name_after_dots));
303 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100304 }
305
306 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100307 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200308 arg_objm, evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100309 if (p == arg)
310 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100311 if (*skipwhite(p) == '=')
312 {
313 emsg(_(e_cannot_use_default_for_variable_arguments));
314 break;
315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100316 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200317 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000318 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000319 {
320 // this.memberName
321 p += 5;
322 arg = p;
323 while (ASCII_ISALNUM(*p) || *p == '_')
324 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000325 char_u *argend = p;
326
h-eastdb385522023-09-28 22:18:19 +0200327 // object variable this. can be used only in a constructor
328 if (STRNCMP(eap->arg, "new", 3) != 0)
329 {
330 c = *argend;
331 *argend = NUL;
332 semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
333 *argend = c;
334 break;
335 }
336
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000337 if (*skipwhite(p) == '=')
338 {
339 char_u *defval = skipwhite(skipwhite(p) + 1);
340 if (STRNCMP(defval, "v:none", 6) != 0)
341 {
342 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
343 goto err_ret;
344 }
345 any_default = TRUE;
346 p = defval + 6;
347
348 if (ga_grow(default_args, 1) == FAIL)
349 goto err_ret;
350
351 char_u *expr = vim_strsave((char_u *)"v:none");
352 if (expr == NULL)
353 goto err_ret;
354 ((char_u **)(default_args->ga_data))
355 [default_args->ga_len] = expr;
356 default_args->ga_len++;
357 }
358 else if (any_default)
359 {
360 emsg(_(e_non_default_argument_follows_default_argument));
361 goto err_ret;
362 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000363
364 // TODO: check the argument is indeed a member
365 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
366 return FAIL;
367 if (newargs != NULL)
368 {
369 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000370 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000371 newargs->ga_len++;
372
h-eastb895b0f2023-09-24 15:46:31 +0200373 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
374 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000375 {
376 // TODO: use the actual type
377 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
378 vim_strsave((char_u *)"any");
h-eastb895b0f2023-09-24 15:46:31 +0200379 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000380
381 // Add a line to the function body for the assignment.
382 if (ga_grow(newlines, 1) == OK)
383 {
384 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000385 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
386 if (any_default)
387 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000388 char_u *assignment = alloc(len);
389 if (assignment != NULL)
390 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000391 c = *argend;
392 *argend = NUL;
393 if (any_default)
394 vim_snprintf((char *)assignment, len,
395 "ifargisset %d this.%s = %s",
396 default_args->ga_len - 1, arg, arg);
397 else
398 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000399 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000400 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000401 ((char_u **)(newlines->ga_data))[
402 newlines->ga_len++] = assignment;
403 }
404 }
405 }
406 }
407 if (*p == ',')
408 ++p;
409 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200410 else
411 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200412 char_u *np;
413
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200414 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100415 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200416 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100417 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200418 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200419
Bram Moolenaar015cf102021-06-26 21:52:02 +0200420 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200421 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200422 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200423 if (*np == '=' && np[1] != '=' && np[1] != '~'
424 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200425 {
426 typval_T rettv;
427
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100428 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200429 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100430 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000431 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200432 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200433 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200434 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200435 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200436 if (ga_grow(default_args, 1) == FAIL)
437 goto err_ret;
438
Christian Brabandte4a450a2023-12-08 20:57:38 +0100439 if (need_expr)
440 need_expr = FALSE;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200441 // trim trailing whitespace
442 while (p > expr && VIM_ISWHITE(p[-1]))
443 p--;
444 c = *p;
445 *p = NUL;
446 expr = vim_strsave(expr);
447 if (expr == NULL)
448 {
449 *p = c;
450 goto err_ret;
451 }
452 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200453 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200454 default_args->ga_len++;
455 *p = c;
456 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200457 }
458 else
Christian Brabandte4a450a2023-12-08 20:57:38 +0100459 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200460 mustend = TRUE;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100461 if (*skipwhite(p) == NUL)
462 need_expr = TRUE;
463 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200464 }
465 else if (any_default)
466 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000467 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200468 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200469 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200470
471 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
472 {
473 // Be tolerant when skipping
474 if (!skip)
475 {
476 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
477 goto err_ret;
478 }
479 p = skipwhite(p);
480 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200481 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200482 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200483 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200484 // Don't give this error when skipping, it makes the "->" not
485 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100486 // Allow missing space after comma in legacy functions.
487 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200488 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
489 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100490 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200491 goto err_ret;
492 }
493 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200494 else
495 mustend = TRUE;
496 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200497 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200498 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200499 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200500
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200501 if (*p != endchar)
502 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100503 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200504
505 *argp = p;
506 return OK;
507
508err_ret:
509 if (newargs != NULL)
510 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200511 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200512 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200513 return FAIL;
514}
515
516/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100517 * Parse the argument types, filling "fp->uf_arg_types".
518 * Return OK or FAIL.
519 */
520 static int
h-eastb895b0f2023-09-24 15:46:31 +0200521parse_argument_types(
522 ufunc_T *fp,
523 garray_T *argtypes,
524 int varargs,
525 garray_T *arg_objm,
526 ocmember_T *obj_members,
527 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100528{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200529 int len = 0;
530
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100531 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
532 if (argtypes->ga_len > 0)
533 {
534 // When "varargs" is set the last name/type goes into uf_va_name
535 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200536 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100537
538 if (len > 0)
539 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
540 if (fp->uf_arg_types != NULL)
541 {
542 int i;
543 type_T *type;
544
545 for (i = 0; i < len; ++ i)
546 {
547 char_u *p = ((char_u **)argtypes->ga_data)[i];
548
549 if (p == NULL)
550 // will get the type from the default value
551 type = &t_unknown;
552 else
h-eastb895b0f2023-09-24 15:46:31 +0200553 {
554 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
555 {
556 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
557
558 type = &t_any;
559 for (int om = 0; om < obj_member_count; ++om)
560 {
561 if (STRCMP(aname, obj_members[om].ocm_name) == 0)
562 {
563 type = obj_members[om].ocm_type;
564 break;
565 }
566 }
567 }
568 else
569 type = parse_type(&p, &fp->uf_type_list, TRUE);
570 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100571 if (type == NULL)
572 return FAIL;
573 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000574 if (i < fp->uf_args.ga_len
575 && (type->tt_type == VAR_FUNC
576 || type->tt_type == VAR_PARTIAL)
577 && var_wrong_func_name(
578 ((char_u **)fp->uf_args.ga_data)[i], TRUE))
579 return FAIL;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100580 }
581 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100582 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200583
584 if (varargs)
585 {
586 char_u *p;
587
588 // Move the last argument "...name: type" to uf_va_name and
589 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200590 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000591 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
592 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200593 p = ((char_u **)argtypes->ga_data)[len];
594 if (p == NULL)
595 // TODO: get type from default value
596 fp->uf_va_type = &t_list_any;
597 else
598 {
599 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
600 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
601 {
602 semsg(_(e_variable_arguments_type_must_be_list_str),
603 ((char_u **)argtypes->ga_data)[len]);
604 return FAIL;
605 }
606 }
607 if (fp->uf_va_type == NULL)
608 return FAIL;
609 }
610
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100611 return OK;
612}
613
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100614 static int
615parse_return_type(ufunc_T *fp, char_u *ret_type)
616{
617 if (ret_type == NULL)
618 fp->uf_ret_type = &t_void;
619 else
620 {
621 char_u *p = ret_type;
622
623 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
624 if (fp->uf_ret_type == NULL)
625 {
626 fp->uf_ret_type = &t_void;
627 return FAIL;
628 }
629 }
630 return OK;
631}
632
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100633/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200634 * Register function "fp" as using "current_funccal" as its scope.
635 */
636 static int
637register_closure(ufunc_T *fp)
638{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200639 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100640 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200641 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200642 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200643 fp->uf_scoped = current_funccal;
644 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200645
Bram Moolenaarca16c602022-09-06 18:57:08 +0100646 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200647 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100648 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
649 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200650 return OK;
651}
652
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100653 static void
654set_ufunc_name(ufunc_T *fp, char_u *name)
655{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100656 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
657 // actually extends beyond the struct.
658 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100659
660 if (name[0] == K_SPECIAL)
661 {
662 fp->uf_name_exp = alloc(STRLEN(name) + 3);
663 if (fp->uf_name_exp != NULL)
664 {
665 STRCPY(fp->uf_name_exp, "<SNR>");
666 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
667 }
668 }
669}
670
Bram Moolenaar58016442016-07-31 18:30:22 +0200671/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100672 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
673 * return "buf" filled with a readable function name.
674 * Otherwise just return "name", thus the return value can always be used.
675 * "name" and "buf" may be equal.
676 */
677 char_u *
678make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
679{
680 size_t len;
681
682 if (name[0] != K_SPECIAL)
683 return name;
684 len = STRLEN(name);
685 if (len + 3 > bufsize)
686 return name;
687
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100688 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100689 mch_memmove(buf, "<SNR>", 5);
690 return buf;
691}
692
693/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200694 * Get a name for a lambda. Returned in static memory.
695 */
696 char_u *
697get_lambda_name(void)
698{
699 static char_u name[30];
700 static int lambda_no = 0;
701
702 sprintf((char*)name, "<lambda>%d", ++lambda_no);
703 return name;
704}
705
Bram Moolenaare01e5212023-01-08 20:31:18 +0000706/*
707 * Allocate a "ufunc_T" for a function called "name".
708 * Makes sure the size is right.
709 */
710 static ufunc_T *
711alloc_ufunc(char_u *name)
712{
713 // When the name is short we need to make sure we allocate enough bytes for
714 // the whole struct, including any padding.
715 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
716 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
717}
718
Bram Moolenaar801ab062020-06-25 19:27:56 +0200719#if defined(FEAT_LUA) || defined(PROTO)
720/*
721 * Registers a native C callback which can be called from Vim script.
722 * Returns the name of the Vim script function.
723 */
724 char_u *
725register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
726{
727 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200728 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200729
Bram Moolenaare01e5212023-01-08 20:31:18 +0000730 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200731 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200732 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200733
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200734 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200735 fp->uf_refcount = 1;
736 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000737 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200738 fp->uf_calls = 0;
739 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200740 fp->uf_cb = cb;
741 fp->uf_cb_free = cb_free;
742 fp->uf_cb_state = state;
743
744 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000745 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200746
747 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200748}
749#endif
750
Bram Moolenaar04b12692020-05-04 23:24:44 +0200751/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100752 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100753 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100754 * If "white_error" is not NULL check for correct use of white space and set
755 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100756 * Return NULL if no valid arrow found.
757 */
758 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100759skip_arrow(
760 char_u *start,
761 int equal_arrow,
762 char_u **ret_type,
763 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100764{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100765 char_u *s = start;
766 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100767
768 if (equal_arrow)
769 {
770 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100771 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100772 if (white_error != NULL && !VIM_ISWHITE(s[1]))
773 {
774 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100775 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100776 return NULL;
777 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100778 s = skipwhite(s + 1);
779 *ret_type = s;
780 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100781 if (s == *ret_type)
782 {
783 emsg(_(e_missing_return_type));
784 return NULL;
785 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100786 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100787 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100788 s = skipwhite(s);
789 if (*s != '=')
790 return NULL;
791 ++s;
792 }
793 if (*s != '>')
794 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100795 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
796 || !IS_WHITE_OR_NUL(s[1])))
797 {
798 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100799 semsg(_(e_white_space_required_before_and_after_str_at_str),
800 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100801 return NULL;
802 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100803 return skipwhite(s + 1);
804}
805
806/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100807 * Check if "*cmd" points to a function command and if so advance "*cmd" and
808 * return TRUE.
809 * Otherwise return FALSE;
810 * Do not consider "function(" to be a command.
811 */
812 static int
813is_function_cmd(char_u **cmd)
814{
815 char_u *p = *cmd;
816
817 if (checkforcmd(&p, "function", 2))
818 {
819 if (*p == '(')
820 return FALSE;
821 *cmd = p;
822 return TRUE;
823 }
824 return FALSE;
825}
826
827/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200828 * Called when defining a function: The context may be needed for script
829 * variables declared in a block that is visible now but not when the function
830 * is compiled or called later.
831 */
832 static void
833function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
834{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000835 if (cstack == NULL || cstack->cs_idx < 0)
836 return;
837
838 int count = cstack->cs_idx + 1;
839 int i;
840
841 fp->uf_block_ids = ALLOC_MULT(int, count);
842 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200843 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000844 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
845 sizeof(int) * count);
846 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200847 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000848
849 // Set flag in each block to indicate a function was defined. This
850 // is used to keep the variable when leaving the block, see
851 // hide_script_var().
852 for (i = 0; i <= cstack->cs_idx; ++i)
853 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200854}
855
856/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100857 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200858 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100859 * "newlines" must already have been initialized.
860 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
861 */
862 static int
863get_function_body(
864 exarg_T *eap,
865 garray_T *newlines,
866 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000867 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100868{
869 linenr_T sourcing_lnum_top = SOURCING_LNUM;
870 linenr_T sourcing_lnum_off;
871 int saved_wait_return = need_wait_return;
872 char_u *line_arg = line_arg_in;
873 int vim9_function = eap->cmdidx == CMD_def
874 || eap->cmdidx == CMD_block;
875#define MAX_FUNC_NESTING 50
876 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200877 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100878 int nesting = 0;
879 getline_opt_T getline_options;
880 int indent = 2;
881 char_u *skip_until = NULL;
882 int ret = FAIL;
883 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200884 int heredoc_concat_len = 0;
885 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100886 char_u *heredoc_trimmed = NULL;
887
Bram Moolenaar20677332021-06-06 17:02:53 +0200888 ga_init2(&heredoc_ga, 1, 500);
889
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100890 // Detect having skipped over comment lines to find the return
891 // type. Add NULL lines to keep the line count correct.
892 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
893 if (SOURCING_LNUM < sourcing_lnum_off)
894 {
895 sourcing_lnum_off -= SOURCING_LNUM;
896 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
897 goto theend;
898 while (sourcing_lnum_off-- > 0)
899 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
900 }
901
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200902 nesting_def[0] = vim9_function;
903 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100904 getline_options = vim9_function
905 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
906 for (;;)
907 {
908 char_u *theline;
909 char_u *p;
910 char_u *arg;
911
912 if (KeyTyped)
913 {
914 msg_scroll = TRUE;
915 saved_wait_return = FALSE;
916 }
917 need_wait_return = FALSE;
918
919 if (line_arg != NULL)
920 {
921 // Use eap->arg, split up in parts by line breaks.
922 theline = line_arg;
923 p = vim_strchr(theline, '\n');
924 if (p == NULL)
925 line_arg += STRLEN(line_arg);
926 else
927 {
928 *p = NUL;
929 line_arg = p + 1;
930 }
931 }
932 else
933 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000934 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100935 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100936 }
937 if (KeyTyped)
938 lines_left = Rows - 1;
939 if (theline == NULL)
940 {
941 // Use the start of the function for the line number.
942 SOURCING_LNUM = sourcing_lnum_top;
943 if (skip_until != NULL)
944 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200945 else if (nesting_inline[nesting])
946 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100947 else if (eap->cmdidx == CMD_def)
948 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100949 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000950 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100951 goto theend;
952 }
953
954 // Detect line continuation: SOURCING_LNUM increased more than one.
955 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
956 if (SOURCING_LNUM < sourcing_lnum_off)
957 sourcing_lnum_off -= SOURCING_LNUM;
958 else
959 sourcing_lnum_off = 0;
960
961 if (skip_until != NULL)
962 {
963 // Don't check for ":endfunc"/":enddef" between
964 // * ":append" and "."
965 // * ":python <<EOF" and "EOF"
966 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
967 if (heredoc_trimmed == NULL
968 || (is_heredoc && skipwhite(theline) == theline)
969 || STRNCMP(theline, heredoc_trimmed,
970 STRLEN(heredoc_trimmed)) == 0)
971 {
972 if (heredoc_trimmed == NULL)
973 p = theline;
974 else if (is_heredoc)
975 p = skipwhite(theline) == theline
976 ? theline : theline + STRLEN(heredoc_trimmed);
977 else
978 p = theline + STRLEN(heredoc_trimmed);
979 if (STRCMP(p, skip_until) == 0)
980 {
981 VIM_CLEAR(skip_until);
982 VIM_CLEAR(heredoc_trimmed);
983 getline_options = vim9_function
984 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
985 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200986
987 if (heredoc_concat_len > 0)
988 {
989 // Replace the starting line with all the concatenated
990 // lines.
991 ga_concat(&heredoc_ga, theline);
992 vim_free(((char_u **)(newlines->ga_data))[
993 heredoc_concat_len - 1]);
994 ((char_u **)(newlines->ga_data))[
995 heredoc_concat_len - 1] = heredoc_ga.ga_data;
996 ga_init(&heredoc_ga);
997 heredoc_concat_len = 0;
998 theline += STRLEN(theline); // skip the "EOF"
999 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001000 }
1001 }
1002 }
1003 else
1004 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001005 int c;
1006 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001007 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001008
1009 // skip ':' and blanks
1010 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1011 ;
1012
1013 // Check for "endfunction", "enddef" or "}".
1014 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001015 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001016 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001017 ? *p == '}'
1018 : (checkforcmd(&p, nesting_def[nesting]
1019 ? "enddef" : "endfunction", 4)
1020 && *p != ':'))
1021 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001022 if (!nesting_inline[nesting] && nesting_def[nesting]
1023 && p < cmd + 6)
1024 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001025 if (nesting-- == 0)
1026 {
1027 char_u *nextcmd = NULL;
1028
1029 if (*p == '|' || *p == '}')
1030 nextcmd = p + 1;
1031 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1032 nextcmd = line_arg;
1033 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001034 && (vim9_function || p_verbose > 0))
1035 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001036 SOURCING_LNUM = sourcing_lnum_top
1037 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001038 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001039 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001040 else
1041 give_warning2((char_u *)
1042 _("W22: Text found after :endfunction: %s"),
1043 p, TRUE);
1044 }
1045 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001046 {
1047 // Another command follows. If the line came from "eap"
1048 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001049 // change "eap->cmdlinep" to point to the last fetched
1050 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001051 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001052 if (lines_to_free->ga_len > 0
1053 && *eap->cmdlinep !=
1054 ((char_u **)lines_to_free->ga_data)
1055 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001056 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001057 // *cmdlinep will be freed later, thus remove the
1058 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001059 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001060 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1061 [lines_to_free->ga_len - 1];
1062 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001063 }
1064 }
1065 break;
1066 }
1067 }
1068
1069 // Check for mismatched "endfunc" or "enddef".
1070 // We don't check for "def" inside "func" thus we also can't check
1071 // for "enddef".
1072 // We continue to find the end of the function, although we might
1073 // not find it.
1074 else if (nesting_def[nesting])
1075 {
1076 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1077 emsg(_(e_mismatched_endfunction));
1078 }
1079 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1080 emsg(_(e_mismatched_enddef));
1081
1082 // Increase indent inside "if", "while", "for" and "try", decrease
1083 // at "end".
1084 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1085 indent -= 2;
1086 else if (STRNCMP(p, "if", 2) == 0
1087 || STRNCMP(p, "wh", 2) == 0
1088 || STRNCMP(p, "for", 3) == 0
1089 || STRNCMP(p, "try", 3) == 0)
1090 indent += 2;
1091
1092 // Check for defining a function inside this function.
1093 // Only recognize "def" inside "def", not inside "function",
1094 // For backwards compatibility, see Test_function_python().
1095 c = *p;
1096 if (is_function_cmd(&p)
1097 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1098 {
1099 if (*p == '!')
1100 p = skipwhite(p + 1);
1101 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001102 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001103 if (*skipwhite(p) == '(')
1104 {
1105 if (nesting == MAX_FUNC_NESTING - 1)
1106 emsg(_(e_function_nesting_too_deep));
1107 else
1108 {
1109 ++nesting;
1110 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001111 nesting_inline[nesting] = FALSE;
1112 indent += 2;
1113 }
1114 }
1115 }
1116
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001117 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001118 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001119 // Not a comment line: check for nested inline function.
1120 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001121 while (end > p && VIM_ISWHITE(*end))
1122 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001123 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001124 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001125 int is_block;
1126
1127 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001128 --end;
1129 while (end > p && VIM_ISWHITE(*end))
1130 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001131 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1132 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001133 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001134 char_u *s = p;
1135
1136 // check for line starting with "au" for :autocmd or
1137 // "com" for :command, these can use a {} block
1138 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1139 || checkforcmd_noparen(&s, "command", 3);
1140 }
1141
1142 if (is_block)
1143 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001144 if (nesting == MAX_FUNC_NESTING - 1)
1145 emsg(_(e_function_nesting_too_deep));
1146 else
1147 {
1148 ++nesting;
1149 nesting_def[nesting] = TRUE;
1150 nesting_inline[nesting] = TRUE;
1151 indent += 2;
1152 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001153 }
1154 }
1155 }
1156
1157 // Check for ":append", ":change", ":insert". Not for :def.
1158 p = skip_range(p, FALSE, NULL);
1159 if (!vim9_function
1160 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1161 || (p[0] == 'c'
1162 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1163 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1164 && (STRNCMP(&p[3], "nge", 3) != 0
1165 || !ASCII_ISALPHA(p[6])))))))
1166 || (p[0] == 'i'
1167 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1168 && (!ASCII_ISALPHA(p[2])
1169 || (p[2] == 's'
1170 && (!ASCII_ISALPHA(p[3])
1171 || p[3] == 'e'))))))))
1172 skip_until = vim_strsave((char_u *)".");
1173
1174 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1175 arg = skipwhite(skiptowhite(p));
1176 if (arg[0] == '<' && arg[1] =='<'
1177 && ((p[0] == 'p' && p[1] == 'y'
1178 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1179 || ((p[2] == '3' || p[2] == 'x')
1180 && !ASCII_ISALPHA(p[3]))))
1181 || (p[0] == 'p' && p[1] == 'e'
1182 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1183 || (p[0] == 't' && p[1] == 'c'
1184 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1185 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1186 && !ASCII_ISALPHA(p[3]))
1187 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1188 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1189 || (p[0] == 'm' && p[1] == 'z'
1190 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1191 ))
1192 {
1193 // ":python <<" continues until a dot, like ":append"
1194 p = skipwhite(arg + 2);
1195 if (STRNCMP(p, "trim", 4) == 0)
1196 {
1197 // Ignore leading white space.
1198 p = skipwhite(p + 4);
1199 heredoc_trimmed = vim_strnsave(theline,
1200 skipwhite(theline) - theline);
1201 }
1202 if (*p == NUL)
1203 skip_until = vim_strsave((char_u *)".");
1204 else
1205 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1206 getline_options = GETLINE_NONE;
1207 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001208 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001209 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001210 }
1211
Bram Moolenaard881d152022-05-13 13:50:36 +01001212 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001213 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001214 // Check for ":cmd v =<< [trim] EOF"
1215 // and ":cmd [a, b] =<< [trim] EOF"
1216 // and "lines =<< [trim] EOF" for Vim9
1217 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001218 arg = p;
1219 if (checkforcmd(&arg, "let", 2)
1220 || checkforcmd(&arg, "var", 3)
1221 || checkforcmd(&arg, "final", 5)
1222 || checkforcmd(&arg, "const", 5)
1223 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001224 {
zeertzjqa93d9cd2023-05-02 16:25:47 +01001225 while (vim_strchr((char_u *)"$@&", *arg) != NULL)
1226 ++arg;
1227 arg = skipwhite(find_name_end(arg, NULL, NULL,
1228 FNE_INCL_BR | FNE_ALLOW_CURLY));
1229 if (vim9_function && *arg == ':')
1230 arg = skipwhite(skip_type(skipwhite(arg + 1), FALSE));
1231 if (arg[0] == '=' && arg[1] == '<' && arg[2] =='<')
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001232 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001233 p = skipwhite(arg + 3);
1234 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001235 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001236 if (STRNCMP(p, "trim", 4) == 0)
1237 {
1238 // Ignore leading white space.
1239 p = skipwhite(p + 4);
1240 heredoc_trimmed = vim_strnsave(theline,
1241 skipwhite(theline) - theline);
1242 continue;
1243 }
1244 if (STRNCMP(p, "eval", 4) == 0)
1245 {
1246 // Ignore leading white space.
1247 p = skipwhite(p + 4);
1248 continue;
1249 }
1250 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001251 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001252 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1253 getline_options = GETLINE_NONE;
1254 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001255 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001256 }
1257 }
1258 }
1259
1260 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001261 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001262 goto theend;
1263
Bram Moolenaar20677332021-06-06 17:02:53 +02001264 if (heredoc_concat_len > 0)
1265 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001266 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001267 // to be used for the instruction later.
1268 ga_concat(&heredoc_ga, theline);
1269 ga_concat(&heredoc_ga, (char_u *)"\n");
1270 p = vim_strsave((char_u *)"");
1271 }
1272 else
1273 {
1274 // Copy the line to newly allocated memory. get_one_sourceline()
1275 // allocates 250 bytes per line, this saves 80% on average. The
1276 // cost is an extra alloc/free.
1277 p = vim_strsave(theline);
1278 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001279 if (p == NULL)
1280 goto theend;
1281 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1282
1283 // Add NULL lines for continuation lines, so that the line count is
1284 // equal to the index in the growarray.
1285 while (sourcing_lnum_off-- > 0)
1286 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1287
1288 // Check for end of eap->arg.
1289 if (line_arg != NULL && *line_arg == NUL)
1290 line_arg = NULL;
1291 }
1292
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001293 // Return OK when no error was detected.
1294 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001295 ret = OK;
1296
1297theend:
1298 vim_free(skip_until);
1299 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001300 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001301 need_wait_return |= saved_wait_return;
1302 return ret;
1303}
1304
1305/*
1306 * Handle the body of a lambda. *arg points to the "{", process statements
1307 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001308 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001309 * When successful "rettv" is set to a funcref.
1310 */
1311 static int
1312lambda_function_body(
1313 char_u **arg,
1314 typval_T *rettv,
1315 evalarg_T *evalarg,
1316 garray_T *newargs,
1317 garray_T *argtypes,
1318 int varargs,
1319 garray_T *default_args,
1320 char_u *ret_type)
1321{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001322 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001323 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001324 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001325 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001326 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001327 exarg_T eap;
1328 garray_T newlines;
1329 char_u *cmdline = NULL;
1330 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001331 partial_T *pt;
1332 char_u *name;
1333 int lnum_save = -1;
1334 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1335
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001336 *arg = skipwhite(*arg + 1);
1337 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001338 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001339 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001340 return FAIL;
1341 }
1342
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001343 CLEAR_FIELD(eap);
1344 eap.cmdidx = CMD_block;
1345 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001346 eap.cmdlinep = &cmdline;
1347 eap.skip = !evaluate;
1348 if (evalarg->eval_cctx != NULL)
1349 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1350 else
1351 {
1352 eap.getline = evalarg->eval_getline;
1353 eap.cookie = evalarg->eval_cookie;
1354 }
1355
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001356 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001357 if (get_function_body(&eap, &newlines, NULL,
1358 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001359 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001360
1361 // When inside a lambda must add the function lines to evalarg.eval_ga.
1362 evalarg->eval_break_count += newlines.ga_len;
1363 if (gap->ga_itemsize > 0)
1364 {
1365 int idx;
1366 char_u *last;
1367 size_t plen;
1368 char_u *pnl;
1369
1370 for (idx = 0; idx < newlines.ga_len; ++idx)
1371 {
1372 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1373
Bram Moolenaarecb66452021-05-18 15:09:18 +02001374 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001375 goto erret;
1376
1377 // Going to concatenate the lines after parsing. For an empty or
1378 // comment line use an empty string.
1379 // Insert NL characters at the start of each line, the string will
1380 // be split again later in .get_lambda_tv().
1381 if (*p == NUL || vim9_comment_start(p))
1382 p = (char_u *)"";
1383 plen = STRLEN(p);
1384 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1385 if (pnl != NULL)
1386 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001387 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1388 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001389 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001390 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001391 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001392 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001393 // more is following after the "}", which was skipped
1394 last = cmdline;
1395 else
1396 // nothing is following the "}"
1397 last = (char_u *)"}";
1398 plen = STRLEN(last);
1399 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1400 if (pnl != NULL)
1401 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001402 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1403 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001404 }
1405
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001406 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001407 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001408 garray_T *tfgap = &evalarg->eval_tofree_ga;
1409
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001410 // Something comes after the "}".
1411 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001412
1413 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001414 if (ga_grow(tfgap, 1) == OK)
1415 {
1416 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1417 evalarg->eval_using_cmdline = TRUE;
1418 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001419 }
1420 else
1421 *arg = (char_u *)"";
1422
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001423 if (!evaluate)
1424 {
1425 ret = OK;
1426 goto erret;
1427 }
1428
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001429 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001430 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001431 if (ufunc == NULL)
1432 goto erret;
1433 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001434 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001435 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001436 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001437 ufunc->uf_refcount = 1;
1438 ufunc->uf_args = *newargs;
1439 newargs->ga_data = NULL;
1440 ufunc->uf_def_args = *default_args;
1441 default_args->ga_data = NULL;
1442 ufunc->uf_func_type = &t_func_any;
1443
1444 // error messages are for the first function line
1445 lnum_save = SOURCING_LNUM;
1446 SOURCING_LNUM = sourcing_lnum_top;
1447
1448 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001449 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001450 {
1451 SOURCING_LNUM = lnum_save;
1452 goto erret;
1453 }
1454
1455 // parse the return type, if any
1456 if (parse_return_type(ufunc, ret_type) == FAIL)
1457 goto erret;
1458
1459 pt = ALLOC_CLEAR_ONE(partial_T);
1460 if (pt == NULL)
1461 goto erret;
1462 pt->pt_func = ufunc;
1463 pt->pt_refcount = 1;
1464
1465 ufunc->uf_lines = newlines;
1466 newlines.ga_data = NULL;
1467 if (sandbox)
1468 ufunc->uf_flags |= FC_SANDBOX;
1469 if (!ASCII_ISUPPER(*ufunc->uf_name))
1470 ufunc->uf_flags |= FC_VIM9;
1471 ufunc->uf_script_ctx = current_sctx;
1472 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1473 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1474 set_function_type(ufunc);
1475
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001476 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1477
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001478 rettv->vval.v_partial = pt;
1479 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001480 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001481 ret = OK;
1482
1483erret:
1484 if (lnum_save >= 0)
1485 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001486 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001487 if (newargs != NULL)
1488 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001489 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001490 if (ufunc != NULL)
1491 {
1492 func_clear(ufunc, TRUE);
1493 func_free(ufunc, TRUE);
1494 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001495 return ret;
1496}
1497
1498/*
1499 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001500 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001501 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001502 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1503 */
1504 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001505get_lambda_tv(
1506 char_u **arg,
1507 typval_T *rettv,
1508 int types_optional,
1509 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001510{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001511 int evaluate = evalarg != NULL
1512 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001513 garray_T newargs;
1514 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001515 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001516 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001517 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001518 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001519 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001520 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001521 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001522 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001523 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001524 char_u *s;
1525 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001526 int *old_eval_lavars = eval_lavars_used;
1527 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001528 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001529 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001530 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001531 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001532 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001533 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001534
Bram Moolenaar4525a572022-02-13 11:57:33 +00001535 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001536 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001537
1538 ga_init(&newargs);
1539 ga_init(&newlines);
1540
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001541 // First, check if this is really a lambda expression. "->" or "=>" must
1542 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001543 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001544 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001545 types_optional ? &argtypes : NULL, types_optional,
1546 types_optional ? &arg_objm : NULL, evalarg,
1547 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001548 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001549 {
1550 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001551 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001552 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001553 ga_clear(&arg_objm);
1554 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001555 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001556 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001557
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001558 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001559 if (evaluate)
1560 pnewargs = &newargs;
1561 else
1562 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001563 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001564 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001565 types_optional ? &argtypes : NULL, types_optional,
1566 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001567 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001568 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001569 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001570 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001571 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001572 {
1573 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001574 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001575 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001576 ga_clear(&arg_objm);
1577 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001578 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001579 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001580 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001581 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001582
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001583 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1584 if (ret_type != NULL)
1585 {
1586 ret_type = vim_strsave(ret_type);
1587 tofree2 = ret_type;
1588 }
1589
Bram Moolenaare38eab22019-12-05 21:50:01 +01001590 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001591 if (evaluate)
1592 eval_lavars_used = &eval_lavars;
1593
Bram Moolenaar65c44152020-12-24 15:14:01 +01001594 *arg = skipwhite_and_linebreak(*arg, evalarg);
1595
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001596 // Recognize "{" as the start of a function body.
1597 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001598 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001599 if (evalarg == NULL)
1600 // cannot happen?
1601 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001602 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001603 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1604 types_optional ? &argtypes : NULL, varargs,
1605 &default_args, ret_type) == FAIL)
1606 goto errret;
1607 goto theend;
1608 }
1609 if (default_args.ga_len > 0)
1610 {
1611 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001612 goto errret;
1613 }
1614
Bram Moolenaare38eab22019-12-05 21:50:01 +01001615 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001616 start = *arg;
1617 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001618 if (ret == FAIL)
1619 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001620
Bram Moolenaar65c44152020-12-24 15:14:01 +01001621 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001622 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001623 *arg = skipwhite_and_linebreak(*arg, evalarg);
1624 if (**arg != '}')
1625 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001626 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001627 goto errret;
1628 }
1629 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001630 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001631
1632 if (evaluate)
1633 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001634 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001635 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001636 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001637 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001638 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001639
Bram Moolenaare01e5212023-01-08 20:31:18 +00001640 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001641 if (fp == NULL)
1642 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001643 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001644 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001645 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001646 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001647
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001648 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001649 if (ga_grow(&newlines, 1) == FAIL)
1650 goto errret;
1651
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001652 // If there are line breaks, we need to split up the string.
1653 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001654 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001655 line_end = end;
1656
1657 // Add "return " before the expression (or the first line).
1658 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001659 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001660 if (p == NULL)
1661 goto errret;
1662 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1663 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001664 vim_strncpy(p + 7, start, line_end - start);
1665
1666 if (line_end != end)
1667 {
1668 // Add more lines, split by line breaks. Thus is used when a
1669 // lambda with { cmds } is encountered.
1670 while (*line_end == '\n')
1671 {
1672 if (ga_grow(&newlines, 1) == FAIL)
1673 goto errret;
1674 start = line_end + 1;
1675 line_end = vim_strchr(start, '\n');
1676 if (line_end == NULL)
1677 line_end = end;
1678 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1679 vim_strnsave(start, line_end - start);
1680 }
1681 }
1682
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001683 if (strstr((char *)p + 7, "a:") == NULL)
1684 // No a: variables are used for sure.
1685 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001686
1687 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001688 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001689 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001690 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001691 if (types_optional)
1692 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001693 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001694 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001695 goto errret;
1696 if (ret_type != NULL)
1697 {
1698 fp->uf_ret_type = parse_type(&ret_type,
1699 &fp->uf_type_list, TRUE);
1700 if (fp->uf_ret_type == NULL)
1701 goto errret;
1702 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001703 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001704 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001705 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001706
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001707 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001708 if (current_funccal != NULL && eval_lavars)
1709 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001710 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001711 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001712 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001713 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001714
1715#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001716 if (prof_def_func())
1717 func_do_profile(fp);
1718#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001719 if (sandbox)
1720 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001721 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001722 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001723 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001724 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001725 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001726 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001727 // Use the line number of the arguments.
1728 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001729
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001730 function_using_block_scopes(fp, evalarg->eval_cstack);
1731
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001732 pt->pt_func = fp;
1733 pt->pt_refcount = 1;
1734 rettv->vval.v_partial = pt;
1735 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001736
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001737 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001738 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001739
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001740theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001741 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001742 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001743 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001744 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001745 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001746 ga_clear(&arg_objm);
1747 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001748
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001749 return OK;
1750
1751errret:
1752 ga_clear_strings(&newargs);
1753 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001754 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001755 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001756 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001757 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001758 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001759 if (fp != NULL)
1760 vim_free(fp->uf_arg_types);
1761 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001762 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001763 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001764 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001765 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001766 return FAIL;
1767}
1768
1769/*
1770 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1771 * name it contains, otherwise return "name".
1772 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1773 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001774 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1775 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001776 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001777 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001778 */
1779 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001780deref_func_name(
1781 char_u *name,
1782 int *lenp,
1783 partial_T **partialp,
1784 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001785 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001786 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001787 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788{
1789 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001790 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001791 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001792 char_u *s = NULL;
1793 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001794 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001795
1796 if (partialp != NULL)
1797 *partialp = NULL;
1798
1799 cc = name[*lenp];
1800 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001801
Bram Moolenaar71f21932022-01-07 18:20:55 +00001802 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001803 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001804 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001806 tv = &v->di_tv;
1807 }
1808 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1809 {
1810 imported_T *import;
1811 char_u *p = name;
1812 int len = *lenp;
1813
1814 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001815 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001816 p = name + 2;
1817 len -= 2;
1818 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001819 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001820
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001821 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001822 if (import != NULL)
1823 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001824 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001825 if (new_function)
1826 semsg(_(e_redefining_imported_item_str), name);
1827 else
1828 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001829 name[len] = cc;
1830 *lenp = 0;
1831 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001832 }
1833 }
1834
1835 if (tv != NULL)
1836 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001837 if (found_var != NULL)
1838 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001839 if (tv->v_type == VAR_FUNC)
1840 {
1841 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001842 {
1843 *lenp = 0;
1844 return (char_u *)""; // just in case
1845 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001846 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001847 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001848 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001849
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001850 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001851 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001852 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001853
1854 if (pt == NULL)
1855 {
1856 *lenp = 0;
1857 return (char_u *)""; // just in case
1858 }
1859 if (partialp != NULL)
1860 *partialp = pt;
1861 s = partial_name(pt);
1862 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001863 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001864
1865 if (s != NULL)
1866 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001867 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001868 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001869 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001870
1871 if (sv != NULL)
1872 *type = sv->sv_type;
1873 }
1874 return s;
1875 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001876 }
1877
1878 return name;
1879}
1880
1881/*
1882 * Give an error message with a function name. Handle <SNR> things.
1883 * "ermsg" is to be passed without translation, use N_() instead of _().
1884 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001885 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001886emsg_funcname(char *ermsg, char_u *name)
1887{
Bram Moolenaar54598062022-01-13 12:05:09 +00001888 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001889
Bram Moolenaar54598062022-01-13 12:05:09 +00001890 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001891 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001892 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001893 if (p != name)
1894 vim_free(p);
1895}
1896
1897/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001898 * Get function arguments at "*arg" and advance it.
1899 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001900 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001901 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001902 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001903get_func_arguments(
1904 char_u **arg,
1905 evalarg_T *evalarg,
1906 int partial_argc,
1907 typval_T *argvars,
1908 int *argcount)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001909{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001910 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001911 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001912 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001913 int evaluate = evalarg == NULL
1914 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001915
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001916 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001917 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001918 // skip the '(' or ',' and possibly line breaks
1919 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1920
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001921 if (*argp == ')' || *argp == ',' || *argp == NUL)
1922 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001923 if (eval1(&argp, &argvars[*argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001924 {
1925 ret = FAIL;
1926 break;
1927 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001928 ++*argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001929 // The comma should come right after the argument, but this wasn't
1930 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001931 if (vim9script)
1932 {
1933 if (*argp != ',' && *skipwhite(argp) == ',')
1934 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001935 if (evaluate)
1936 semsg(_(e_no_white_space_allowed_before_str_str),
1937 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001938 ret = FAIL;
1939 break;
1940 }
1941 }
1942 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001943 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001944 if (*argp != ',')
1945 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001946 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001947 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001948 if (evaluate)
1949 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001950 ret = FAIL;
1951 break;
1952 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001953 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001954
Bram Moolenaare6b53242020-07-01 17:28:33 +02001955 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001956 if (*argp == ')')
1957 ++argp;
1958 else
1959 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001960 *arg = argp;
1961 return ret;
1962}
1963
1964/*
1965 * Call a function and put the result in "rettv".
1966 * Return OK or FAIL.
1967 */
1968 int
1969get_func_tv(
1970 char_u *name, // name of the function
1971 int len, // length of "name" or -1 to use strlen()
1972 typval_T *rettv,
1973 char_u **arg, // argument, pointing to the '('
1974 evalarg_T *evalarg, // for line continuation
1975 funcexe_T *funcexe) // various values
1976{
1977 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001978 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001979 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1980 int argcount = 0; // number of arguments found
1981 int vim9script = in_vim9script();
1982 int evaluate = evalarg == NULL
1983 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1984
1985 argp = *arg;
1986 ret = get_func_arguments(&argp, evalarg,
1987 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
1988 argvars, &argcount);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001989
1990 if (ret == OK)
1991 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001992 int i = 0;
1993 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001994
1995 if (get_vim_var_nr(VV_TESTING))
1996 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001997 // Prepare for calling test_garbagecollect_now(), need to know
1998 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001999 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002000 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002001 for (i = 0; i < argcount; ++i)
2002 if (ga_grow(&funcargs, 1) == OK)
2003 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2004 &argvars[i];
2005 }
2006
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002007 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002008 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002009 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002010 // An error in a builtin function does not return FAIL, but we do
2011 // want to abort further processing if an error was given.
2012 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002013 clear_tv(rettv);
2014 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002015
2016 funcargs.ga_len -= i;
2017 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002018 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002019 {
2020 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002021 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002022 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002023 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002024 }
2025
2026 while (--argcount >= 0)
2027 clear_tv(&argvars[argcount]);
2028
Bram Moolenaar4525a572022-02-13 11:57:33 +00002029 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002030 *arg = argp;
2031 else
2032 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002033 return ret;
2034}
2035
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002036/*
2037 * Return TRUE if "p" starts with "<SID>" or "s:".
2038 * Only works if eval_fname_script() returned non-zero for "p"!
2039 */
2040 static int
2041eval_fname_sid(char_u *p)
2042{
2043 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2044}
2045
2046/*
2047 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2048 * Change <SNR>123_name() to K_SNR 123_name().
2049 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002050 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002051 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002052 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002053fname_trans_sid(
2054 char_u *name,
2055 char_u *fname_buf,
2056 char_u **tofree,
2057 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002058{
2059 int llen;
2060 char_u *fname;
2061 int i;
2062
2063 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002064 if (llen == 0)
2065 return name; // no prefix
2066
2067 fname_buf[0] = K_SPECIAL;
2068 fname_buf[1] = KS_EXTRA;
2069 fname_buf[2] = (int)KE_SNR;
2070 i = 3;
2071 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002072 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002073 if (current_sctx.sc_sid <= 0)
2074 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002075 else
2076 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002077 sprintf((char *)fname_buf + 3, "%ld_",
2078 (long)current_sctx.sc_sid);
2079 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002080 }
2081 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002082 if (i + STRLEN(name + llen) < FLEN_FIXED)
2083 {
2084 STRCPY(fname_buf + i, name + llen);
2085 fname = fname_buf;
2086 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002087 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002088 {
2089 fname = alloc(i + STRLEN(name + llen) + 1);
2090 if (fname == NULL)
2091 *error = FCERR_OTHER;
2092 else
2093 {
2094 *tofree = fname;
2095 mch_memmove(fname, fname_buf, (size_t)i);
2096 STRCPY(fname + i, name + llen);
2097 }
2098 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002099 return fname;
2100}
2101
2102/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002103 * Concatenate the script ID and function name into "<SNR>99_name".
2104 * "buffer" must have size MAX_FUNC_NAME_LEN.
2105 */
2106 void
2107func_name_with_sid(char_u *name, int sid, char_u *buffer)
2108{
2109 // A script-local function is stored as "<SNR>99_name".
2110 buffer[0] = K_SPECIAL;
2111 buffer[1] = KS_EXTRA;
2112 buffer[2] = (int)KE_SNR;
2113 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2114 (long)sid, name);
2115}
2116
2117/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 * Find a function "name" in script "sid".
2119 */
2120 static ufunc_T *
2121find_func_with_sid(char_u *name, int sid)
2122{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002123 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002124 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125
Bram Moolenaarb8822442022-01-11 15:24:05 +00002126 if (!SCRIPT_ID_VALID(sid))
2127 return NULL; // not in a script
2128
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002129 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002130 hi = hash_find(&func_hashtab, buffer);
2131 if (!HASHITEM_EMPTY(hi))
2132 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002133 return NULL;
2134}
2135
2136/*
2137 * Find a function "name" in script "sid" prefixing the autoload prefix.
2138 */
2139 static ufunc_T *
2140find_func_with_prefix(char_u *name, int sid)
2141{
2142 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002143 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002144 scriptitem_T *si;
2145
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002146 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002147 return NULL; // already has the prefix
2148 if (!SCRIPT_ID_VALID(sid))
2149 return NULL; // not in a script
2150 si = SCRIPT_ITEM(sid);
2151 if (si->sn_autoload_prefix != NULL)
2152 {
2153 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2154 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002155 char_u *namep;
2156
2157 // skip a "<SNR>99_" prefix
2158 namep = untrans_function_name(name);
2159 if (namep == NULL)
2160 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002161
2162 // An exported function in an autoload script is stored as
2163 // "dir#path#name".
2164 if (len < sizeof(buffer))
2165 auto_name = buffer;
2166 else
2167 auto_name = alloc(len);
2168 if (auto_name != NULL)
2169 {
2170 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002171 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002172 hi = hash_find(&func_hashtab, auto_name);
2173 if (auto_name != buffer)
2174 vim_free(auto_name);
2175 if (!HASHITEM_EMPTY(hi))
2176 return HI2UF(hi);
2177 }
2178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179
2180 return NULL;
2181}
2182
2183/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002184 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002185 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2186 * functions.
2187 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002188 * Return NULL for unknown function.
2189 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002190 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002191find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002192{
2193 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002196 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002198 // Find script-local function before global one.
2199 if (in_vim9script() && eval_isnamec1(*name)
2200 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002202 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2203 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002204 if (func != NULL)
2205 return func;
2206 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002207 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2208 {
2209 char_u *p = name + 5;
2210 long sid;
2211
2212 // printable "<SNR>123_Name" form
2213 sid = getdigits(&p);
2214 if (*p == '_')
2215 {
2216 func = find_func_with_sid(p + 1, (int)sid);
2217 if (func != NULL)
2218 return func;
2219 }
2220 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002221 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002222
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002223 if ((flags & FFED_NO_GLOBAL) == 0)
2224 {
2225 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002226 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002227 if (!HASHITEM_EMPTY(hi))
2228 return HI2UF(hi);
2229 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230
Bram Moolenaarb8822442022-01-11 15:24:05 +00002231 // Find autoload function if this is an autoload script.
2232 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2233 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002234}
2235
2236/*
2237 * Find a function by name, return pointer to it in ufuncs.
2238 * "cctx" is passed in a :def function to find imported functions.
2239 * Return NULL for unknown or dead function.
2240 */
2241 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002242find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002244 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245
2246 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2247 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002248 return NULL;
2249}
2250
2251/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002252 * Return TRUE if "ufunc" is a global function.
2253 */
2254 int
2255func_is_global(ufunc_T *ufunc)
2256{
2257 return ufunc->uf_name[0] != K_SPECIAL;
2258}
2259
2260/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002261 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2262 */
2263 int
2264func_requires_g_prefix(ufunc_T *ufunc)
2265{
2266 return ufunc->uf_name[0] != K_SPECIAL
2267 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002268 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
2269 && !isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002270}
2271
2272/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002273 * Copy the function name of "fp" to buffer "buf".
2274 * "buf" must be able to hold the function name plus three bytes.
2275 * Takes care of script-local function names.
2276 */
2277 static void
2278cat_func_name(char_u *buf, ufunc_T *fp)
2279{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002280 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002281 {
2282 STRCPY(buf, "<SNR>");
2283 STRCAT(buf, fp->uf_name + 3);
2284 }
2285 else
2286 STRCPY(buf, fp->uf_name);
2287}
2288
2289/*
2290 * Add a number variable "name" to dict "dp" with value "nr".
2291 */
2292 static void
2293add_nr_var(
2294 dict_T *dp,
2295 dictitem_T *v,
2296 char *name,
2297 varnumber_T nr)
2298{
2299 STRCPY(v->di_key, name);
2300 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002301 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002302 v->di_tv.v_type = VAR_NUMBER;
2303 v->di_tv.v_lock = VAR_FIXED;
2304 v->di_tv.vval.v_number = nr;
2305}
2306
2307/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002308 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002309 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002310 static void
2311free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002312{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002313 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002314
Bram Moolenaarca16c602022-09-06 18:57:08 +01002315 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002316 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002317 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002318
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002319 // When garbage collecting a funccall_T may be freed before the
2320 // function that references it, clear its uf_scoped field.
2321 // The function may have been redefined and point to another
2322 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002323 if (fp != NULL && fp->uf_scoped == fc)
2324 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002325 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002326 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002327
Bram Moolenaarca16c602022-09-06 18:57:08 +01002328 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002329 vim_free(fc);
2330}
2331
2332/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002333 * Free "fc" and what it contains.
2334 * Can be called only when "fc" is kept beyond the period of it called,
2335 * i.e. after cleanup_function_call(fc).
2336 */
2337 static void
2338free_funccal_contents(funccall_T *fc)
2339{
2340 listitem_T *li;
2341
2342 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002343 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002344
2345 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002346 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002347
2348 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002349 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002350 clear_tv(&li->li_tv);
2351
2352 free_funccal(fc);
2353}
2354
2355/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002356 * Handle the last part of returning from a function: free the local hashtable.
2357 * Unless it is still in use by a closure.
2358 */
2359 static void
2360cleanup_function_call(funccall_T *fc)
2361{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002362 int may_free_fc = fc->fc_refcount <= 0;
2363 int free_fc = TRUE;
2364
Bram Moolenaarca16c602022-09-06 18:57:08 +01002365 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002366
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002367 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002368 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2369 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002370 else
2371 free_fc = FALSE;
2372
2373 // If the a:000 list and the l: and a: dicts are not referenced and
2374 // there is no closure using it, we can free the funccall_T and what's
2375 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002376 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2377 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002378 else
2379 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002380 int todo;
2381 hashitem_T *hi;
2382 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002383
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002384 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002385
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002386 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002387 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002388 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002389 {
2390 if (!HASHITEM_EMPTY(hi))
2391 {
2392 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002393 di = HI2DI(hi);
2394 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002395 }
2396 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002397 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002398
Bram Moolenaarca16c602022-09-06 18:57:08 +01002399 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2400 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002401 else
2402 {
2403 listitem_T *li;
2404
2405 free_fc = FALSE;
2406
2407 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002408 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002409 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002410 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002411
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002412 if (free_fc)
2413 free_funccal(fc);
2414 else
2415 {
2416 static int made_copy = 0;
2417
2418 // "fc" is still in use. This can happen when returning "a:000",
2419 // assigning "l:" to a global variable or defining a closure.
2420 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002421 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002422 previous_funccal = fc;
2423
2424 if (want_garbage_collect)
2425 // If garbage collector is ready, clear count.
2426 made_copy = 0;
2427 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002428 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002429 // We have made a lot of copies, worth 4 Mbyte. This can happen
2430 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002431 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002432 // too much memory.
2433 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002434 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002435 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002436 }
2437}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002438
2439/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002440 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2441 */
2442 static int
2443numbered_function(char_u *name)
2444{
2445 return isdigit(*name)
2446 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2447}
2448
2449/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002450 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002451 * 1. ordinary names, function defined with :function or :def;
2452 * can start with "<SNR>123_" literally or with K_SPECIAL.
2453 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002454 * For the first we only count the name stored in func_hashtab as a reference,
2455 * using function() does not count as a reference, because the function is
2456 * looked up by name.
2457 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002458 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002459func_name_refcount(char_u *name)
2460{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002461 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002462}
2463
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464/*
2465 * Unreference "fc": decrement the reference count and free it when it
2466 * becomes zero. "fp" is detached from "fc".
2467 * When "force" is TRUE we are exiting.
2468 */
2469 static void
2470funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2471{
2472 funccall_T **pfc;
2473 int i;
2474
2475 if (fc == NULL)
2476 return;
2477
2478 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002479 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2480 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2481 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2482 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483 {
2484 if (fc == *pfc)
2485 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002486 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 free_funccal_contents(fc);
2488 return;
2489 }
2490 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002491 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2492 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2493 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494}
2495
2496/*
2497 * Remove the function from the function hashtable. If the function was
2498 * deleted while it still has references this was already done.
2499 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2500 */
2501 static int
2502func_remove(ufunc_T *fp)
2503{
2504 hashitem_T *hi;
2505
2506 // Return if it was already virtually deleted.
2507 if (fp->uf_flags & FC_DEAD)
2508 return FALSE;
2509
2510 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002511 if (HASHITEM_EMPTY(hi))
2512 return FALSE;
2513
2514 // When there is a def-function index do not actually remove the
2515 // function, so we can find the index when defining the function again.
2516 // Do remove it when it's a copy.
2517 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002519 fp->uf_flags |= FC_DEAD;
2520 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002522 hash_remove(&func_hashtab, hi, "remove function");
2523 fp->uf_flags |= FC_DELETED;
2524 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525}
2526
2527 static void
2528func_clear_items(ufunc_T *fp)
2529{
2530 ga_clear_strings(&(fp->uf_args));
2531 ga_clear_strings(&(fp->uf_def_args));
2532 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002534 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002535 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002536 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002537
2538 // Increment the refcount of this function to avoid it being freed
2539 // recursively when the partial is freed.
2540 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002541 partial_unref(fp->uf_partial);
2542 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002543 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002544
2545#ifdef FEAT_LUA
2546 if (fp->uf_cb_free != NULL)
2547 {
2548 fp->uf_cb_free(fp->uf_cb_state);
2549 fp->uf_cb_free = NULL;
2550 }
2551
2552 fp->uf_cb_state = NULL;
2553 fp->uf_cb = NULL;
2554#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555#ifdef FEAT_PROFILE
2556 VIM_CLEAR(fp->uf_tml_count);
2557 VIM_CLEAR(fp->uf_tml_total);
2558 VIM_CLEAR(fp->uf_tml_self);
2559#endif
2560}
2561
2562/*
2563 * Free all things that a function contains. Does not free the function
2564 * itself, use func_free() for that.
2565 * When "force" is TRUE we are exiting.
2566 */
2567 static void
2568func_clear(ufunc_T *fp, int force)
2569{
2570 if (fp->uf_cleared)
2571 return;
2572 fp->uf_cleared = TRUE;
2573
2574 // clear this function
2575 func_clear_items(fp);
2576 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002577 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578}
2579
2580/*
2581 * Free a function and remove it from the list of functions. Does not free
2582 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002583 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002584 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002586 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002587func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588{
2589 // Only remove it when not done already, otherwise we would remove a newer
2590 // version of the function with the same name.
2591 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2592 func_remove(fp);
2593
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002594 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002595 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002596 if (fp->uf_dfunc_idx > 0)
2597 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002598 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002600 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002601 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002602 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603}
2604
2605/*
2606 * Free all things that a function contains and free the function itself.
2607 * When "force" is TRUE we are exiting.
2608 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002609 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610func_clear_free(ufunc_T *fp, int force)
2611{
2612 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002613 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2614 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002615 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002616 else
2617 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618}
2619
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002620/*
2621 * Copy already defined function "lambda" to a new function with name "global".
2622 * This is for when a compiled function defines a global function.
2623 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002624 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002625copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002626 char_u *lambda,
2627 char_u *global,
2628 loopvarinfo_T *loopvarinfo,
2629 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002630{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002631 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002632 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002633
2634 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002635 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002636 semsg(_(e_lambda_function_not_found_str), lambda);
2637 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002638 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002639
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002640 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002641 if (fp != NULL)
2642 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002643 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002644 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002645 return FAIL;
2646 }
2647
Bram Moolenaare01e5212023-01-08 20:31:18 +00002648 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002649 if (fp == NULL)
2650 return FAIL;
2651
2652 fp->uf_varargs = ufunc->uf_varargs;
2653 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2654 fp->uf_def_status = ufunc->uf_def_status;
2655 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2656 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2657 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2658 == FAIL
2659 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2660 goto failed;
2661
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002662 if (ufunc->uf_arg_types != NULL)
2663 {
2664 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2665 if (fp->uf_arg_types == NULL)
2666 goto failed;
2667 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2668 sizeof(type_T *) * fp->uf_args.ga_len);
2669 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002670 if (ufunc->uf_va_name != NULL)
2671 {
2672 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2673 if (fp->uf_va_name == NULL)
2674 goto failed;
2675 }
2676 fp->uf_ret_type = ufunc->uf_ret_type;
2677
2678 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002679
2680 fp->uf_name_exp = NULL;
2681 set_ufunc_name(fp, global);
2682
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002683 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002684
2685 // the referenced dfunc_T is now used one more time
2686 link_def_function(fp);
2687
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002688 // Create a partial to store the context of the function where it was
2689 // instantiated. Only needs to be done once. Do this on the original
2690 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002691 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2692 {
2693 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2694
2695 if (pt == NULL)
2696 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002697 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002698 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002699 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002700 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002701 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002702 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002703 }
2704
2705 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002706
2707failed:
2708 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002709 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002710}
2711
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002712static int funcdepth = 0;
2713
2714/*
2715 * Increment the function call depth count.
2716 * Return FAIL when going over 'maxfuncdepth'.
2717 * Otherwise return OK, must call funcdepth_decrement() later!
2718 */
2719 int
2720funcdepth_increment(void)
2721{
2722 if (funcdepth >= p_mfd)
2723 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002724 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002725 return FAIL;
2726 }
2727 ++funcdepth;
2728 return OK;
2729}
2730
2731 void
2732funcdepth_decrement(void)
2733{
2734 --funcdepth;
2735}
2736
2737/*
2738 * Get the current function call depth.
2739 */
2740 int
2741funcdepth_get(void)
2742{
2743 return funcdepth;
2744}
2745
2746/*
2747 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002748 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002749 * times as funcdepth_increment().
2750 */
2751 void
2752funcdepth_restore(int depth)
2753{
2754 funcdepth = depth;
2755}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002756
2757/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002758 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2759 * "rettv".
2760 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2761 * Returns NULL when allocation fails.
2762 */
2763 funccall_T *
2764create_funccal(ufunc_T *fp, typval_T *rettv)
2765{
2766 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2767
2768 if (fc == NULL)
2769 return NULL;
2770 fc->fc_caller = current_funccal;
2771 current_funccal = fc;
2772 fc->fc_func = fp;
2773 func_ptr_ref(fp);
2774 fc->fc_rettv = rettv;
2775 return fc;
2776}
2777
2778/*
2779 * To be called when returning from a compiled function; restores
2780 * current_funccal.
2781 */
2782 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002783remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002784{
2785 funccall_T *fc = current_funccal;
2786
2787 current_funccal = fc->fc_caller;
2788 free_funccal(fc);
2789}
2790
2791/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002792 * Call a user function.
2793 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002794 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002795call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002796 ufunc_T *fp, // pointer to function
2797 int argcount, // nr of args
2798 typval_T *argvars, // arguments
2799 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002800 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002801 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002802{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002803 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002804 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002805 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002806 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002807 funccall_T *fc;
2808 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002809 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002810 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002811 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002812 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002813 int i;
2814 int ai;
2815 int islambda = FALSE;
2816 char_u numbuf[NUMBUFLEN];
2817 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002818 typval_T *tv_to_free[MAX_FUNC_ARGS];
2819 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002820#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002821 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002822#endif
ichizok7e5fe382023-04-15 13:17:50 +01002823 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002824
Bram Moolenaarb2049902021-01-24 12:53:53 +01002825#ifdef FEAT_PROFILE
2826 CLEAR_FIELD(profile_info);
2827#endif
2828
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002829 // If depth of calling is getting too high, don't execute the function.
2830 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002831 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002832 rettv->v_type = VAR_NUMBER;
2833 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002834 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002835 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002836
Bram Moolenaare38eab22019-12-05 21:50:01 +01002837 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002838
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002839 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002840 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002841 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002842 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002843 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002844 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2845 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002846 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002847 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002848
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002849 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002851#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002852 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002853#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002854 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002855#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002856 if (do_profiling == PROF_YES)
2857 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002858#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002859 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002860 if (call_def_function(fp, argcount, argvars, 0,
2861 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2862 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002863 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002864#ifdef FEAT_PROFILE
2865 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002866 || (caller != NULL && caller->uf_profiling)))
2867 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002868#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002869 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002870 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002871 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 }
2873
Bram Moolenaar38453522021-11-28 22:00:12 +00002874 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002875
2876 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002877 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2878 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2879 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002880 */
2881 /*
2882 * Init l: variables.
2883 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002884 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002885 if (selfdict != NULL)
2886 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002887 // Set l:self to "selfdict". Use "name" to avoid a warning from
2888 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002889 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002890 name = v->di_key;
2891 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002892 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002893 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002894 v->di_tv.v_type = VAR_DICT;
2895 v->di_tv.v_lock = 0;
2896 v->di_tv.vval.v_dict = selfdict;
2897 ++selfdict->dv_refcount;
2898 }
2899
2900 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002901 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002902 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002903 * Set a:000 to a list with room for the "..." arguments.
2904 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002905 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002906 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002907 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002908 (varnumber_T)(argcount >= fp->uf_args.ga_len
2909 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002910 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002911 if ((fp->uf_flags & FC_NOARGS) == 0)
2912 {
2913 // Use "name" to avoid a warning from some compiler that checks the
2914 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002915 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002916 name = v->di_key;
2917 STRCPY(name, "000");
2918 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002919 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002920 v->di_tv.v_type = VAR_LIST;
2921 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002922 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002923 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002924 CLEAR_FIELD(fc->fc_l_varlist);
2925 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2926 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002927
2928 /*
2929 * Set a:firstline to "firstline" and a:lastline to "lastline".
2930 * Set a:name to named arguments.
2931 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002932 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002933 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002934 if ((fp->uf_flags & FC_NOARGS) == 0)
2935 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002936 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2937 "firstline", (varnumber_T)funcexe->fe_firstline);
2938 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2939 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002940 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002941 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002942 {
2943 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002944 typval_T def_rettv;
2945 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002946
2947 ai = i - fp->uf_args.ga_len;
2948 if (ai < 0)
2949 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002950 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002951 name = FUNCARG(fp, i);
2952 if (islambda)
2953 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002954
2955 // evaluate named argument default expression
2956 isdefault = ai + fp->uf_def_args.ga_len >= 0
2957 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2958 && argvars[i].vval.v_number == VVAL_NONE));
2959 if (isdefault)
2960 {
2961 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002962
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002963 def_rettv.v_type = VAR_NUMBER;
2964 def_rettv.vval.v_number = -1;
2965
2966 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2967 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002968 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002969 {
2970 default_arg_err = 1;
2971 break;
2972 }
2973 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002974 }
2975 else
2976 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002977 if ((fp->uf_flags & FC_NOARGS) != 0)
2978 // Bail out if no a: arguments used (in lambda).
2979 break;
2980
Bram Moolenaare38eab22019-12-05 21:50:01 +01002981 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002982 sprintf((char *)numbuf, "%d", ai + 1);
2983 name = numbuf;
2984 }
2985 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2986 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002987 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002988 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002989 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002990 }
2991 else
2992 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002993 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994 if (v == NULL)
2995 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002996 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002997 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002998
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002999 // Note: the values are copied directly to avoid alloc/free.
3000 // "argvars" must have VAR_FIXED for v_lock.
3001 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003002 v->di_tv.v_lock = VAR_FIXED;
3003
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003004 if (isdefault)
3005 // Need to free this later, no matter where it's stored.
3006 tv_to_free[tv_to_free_len++] = &v->di_tv;
3007
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003008 if (addlocal)
3009 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003010 // Named arguments should be accessed without the "a:" prefix in
3011 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003012 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003013 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003014 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003015 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003016 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003017
3018 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3019 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003020 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003021
3022 li->li_tv = argvars[i];
3023 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003024 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003025 }
3026 }
3027
Bram Moolenaare38eab22019-12-05 21:50:01 +01003028 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003029 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003030
3031 if (fp->uf_flags & FC_SANDBOX)
3032 {
3033 using_sandbox = TRUE;
3034 ++sandbox;
3035 }
3036
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003037 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003038 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003039 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003040 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003041 ++no_wait_return;
3042 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003043
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003044 smsg(_("calling %s"), SOURCING_NAME);
3045 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003046 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003047 char_u buf[MSG_BUF_LEN];
3048 char_u numbuf2[NUMBUFLEN];
3049 char_u *tofree;
3050 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003051
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003052 msg_puts("(");
3053 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003054 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003055 if (i > 0)
3056 msg_puts(", ");
3057 if (argvars[i].v_type == VAR_NUMBER)
3058 msg_outnum((long)argvars[i].vval.v_number);
3059 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003060 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003061 // Do not want errors such as E724 here.
3062 ++emsg_off;
3063 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3064 --emsg_off;
3065 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003066 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003067 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003068 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003069 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3070 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003071 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003072 msg_puts((char *)s);
3073 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003074 }
3075 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003076 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003077 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003078 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003079 msg_puts("\n"); // don't overwrite this either
3080
3081 verbose_leave_scroll();
3082 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003083 }
3084#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003085 if (do_profiling == PROF_YES)
3086 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003087 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003088#endif
3089
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003090 // "legacy" does not apply to commands in the function
3091 sticky_cmdmod_flags = 0;
3092
Bram Moolenaar98aff652022-09-06 21:02:35 +01003093 // If called from a compiled :def function the execution context must be
3094 // hidden, any deferred functions need to be added to the function being
3095 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003096 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003097
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003098 save_current_sctx = current_sctx;
3099 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003100 save_did_emsg = did_emsg;
3101 did_emsg = FALSE;
3102
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003103 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003104 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003105 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003106 retval = FCERR_FAILED;
3107 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003108 else if (islambda)
3109 {
3110 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3111
3112 // A Lambda always has the command "return {expr}". It is much faster
3113 // to evaluate {expr} directly.
3114 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003115 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003116 --ex_nesting_level;
3117 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003118 else
3119 // call do_cmdline() to execute the lines
3120 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003121 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3122
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003123 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003124 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003125
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003126 if (RedrawingDisabled > 0)
3127 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003128
Bram Moolenaare38eab22019-12-05 21:50:01 +01003129 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003130 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3131 {
3132 clear_tv(rettv);
3133 rettv->v_type = VAR_NUMBER;
3134 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003135
3136 // In corner cases returning a "failed" value is not backwards
3137 // compatible. Only do this for Vim9 script.
3138 if (in_vim9script())
3139 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003140 }
3141
3142#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003143 if (do_profiling == PROF_YES)
3144 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003145 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003146
3147 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3148 profile_may_end_func(&profile_info, fp, caller);
3149 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003150#endif
3151
Bram Moolenaare38eab22019-12-05 21:50:01 +01003152 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003153 if (p_verbose >= 12)
3154 {
3155 ++no_wait_return;
3156 verbose_enter_scroll();
3157
3158 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003159 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003160 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003161 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003162 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003163 else
3164 {
3165 char_u buf[MSG_BUF_LEN];
3166 char_u numbuf2[NUMBUFLEN];
3167 char_u *tofree;
3168 char_u *s;
3169
Bram Moolenaare38eab22019-12-05 21:50:01 +01003170 // The value may be very long. Skip the middle part, so that we
3171 // have some idea how it starts and ends. smsg() would always
3172 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003173 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003174 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003175 --emsg_off;
3176 if (s != NULL)
3177 {
3178 if (vim_strsize(s) > MSG_BUF_CLEN)
3179 {
3180 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3181 s = buf;
3182 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003183 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003184 vim_free(tofree);
3185 }
3186 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003187 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003188
3189 verbose_leave_scroll();
3190 --no_wait_return;
3191 }
3192
ichizok7e5fe382023-04-15 13:17:50 +01003193 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003194 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003195 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003196 restore_current_ectx(save_current_ectx);
3197
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003198#ifdef FEAT_PROFILE
3199 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003200 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003201#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003202 if (using_sandbox)
3203 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003204 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003205
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003206 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003207 {
3208 ++no_wait_return;
3209 verbose_enter_scroll();
3210
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003211 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003212 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003213
3214 verbose_leave_scroll();
3215 --no_wait_return;
3216 }
3217
3218 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003219 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003220 for (i = 0; i < tv_to_free_len; ++i)
3221 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003222 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003223
3224 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003225}
3226
3227/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003228 * Check the argument count for user function "fp".
3229 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3230 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003231 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003232check_user_func_argcount(ufunc_T *fp, int argcount)
3233{
3234 int regular_args = fp->uf_args.ga_len;
3235
3236 if (argcount < regular_args - fp->uf_def_args.ga_len)
3237 return FCERR_TOOFEW;
3238 else if (!has_varargs(fp) && argcount > regular_args)
3239 return FCERR_TOOMANY;
3240 return FCERR_UNKNOWN;
3241}
3242
3243/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003245 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003246 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247call_user_func_check(
3248 ufunc_T *fp,
3249 int argcount,
3250 typval_T *argvars,
3251 typval_T *rettv,
3252 funcexe_T *funcexe,
3253 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003254{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003255 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003256
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003257#ifdef FEAT_LUA
3258 if (fp->uf_flags & FC_CFUNC)
3259 {
3260 cfunc_T cb = fp->uf_cb;
3261
3262 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3263 }
3264#endif
3265
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003266 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3267 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003268 error = check_user_func_argcount(fp, argcount);
3269 if (error != FCERR_UNKNOWN)
3270 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003271
Bram Moolenaar50824712020-12-20 21:10:17 +01003272 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003273 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003275 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003277 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 int did_save_redo = FALSE;
3279 save_redo_T save_redo;
3280
3281 /*
3282 * Call the user function.
3283 * Save and restore search patterns, script variables and
3284 * redo buffer.
3285 */
3286 save_search_patterns();
3287 if (!ins_compl_active())
3288 {
3289 saveRedobuff(&save_redo);
3290 did_save_redo = TRUE;
3291 }
3292 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003293 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003294 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3296 // Function was unreferenced while being used, free it now.
3297 func_clear_free(fp, FALSE);
3298 if (did_save_redo)
3299 restoreRedobuff(&save_redo);
3300 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003301 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003304}
3305
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003306static funccal_entry_T *funccal_stack = NULL;
3307
3308/*
3309 * Save the current function call pointer, and set it to NULL.
3310 * Used when executing autocommands and for ":source".
3311 */
3312 void
3313save_funccal(funccal_entry_T *entry)
3314{
3315 entry->top_funccal = current_funccal;
3316 entry->next = funccal_stack;
3317 funccal_stack = entry;
3318 current_funccal = NULL;
3319}
3320
3321 void
3322restore_funccal(void)
3323{
3324 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003325 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003326 else
3327 {
3328 current_funccal = funccal_stack->top_funccal;
3329 funccal_stack = funccal_stack->next;
3330 }
3331}
3332
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003333 funccall_T *
3334get_current_funccal(void)
3335{
3336 return current_funccal;
3337}
3338
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003339/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003340 * Return TRUE when currently at the script level:
3341 * - not in a function
3342 * - not executing an autocommand
3343 * Note that when an autocommand sources a script the result is FALSE;
3344 */
3345 int
3346at_script_level(void)
3347{
3348 return current_funccal == NULL && autocmd_match == NULL;
3349}
3350
3351/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003352 * Mark all functions of script "sid" as deleted.
3353 */
3354 void
3355delete_script_functions(int sid)
3356{
3357 hashitem_T *hi;
3358 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003359 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003360 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003361 size_t len;
3362
3363 buf[0] = K_SPECIAL;
3364 buf[1] = KS_EXTRA;
3365 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003366 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003367 len = STRLEN(buf);
3368
Bram Moolenaarce658352020-07-31 23:47:12 +02003369 while (todo > 0)
3370 {
3371 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003372 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003373 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003374 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003375 fp = HI2UF(hi);
3376 if (STRNCMP(fp->uf_name, buf, len) == 0)
3377 {
3378 int changed = func_hashtab.ht_changed;
3379
3380 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003381
3382 if (fp->uf_calls > 0)
3383 {
3384 // Function is executing, don't free it but do remove
3385 // it from the hashtable.
3386 if (func_remove(fp))
3387 fp->uf_refcount--;
3388 }
3389 else
3390 {
3391 func_clear(fp, TRUE);
3392 // When clearing a function another function can be
3393 // cleared as a side effect. When that happens start
3394 // over.
3395 if (changed != func_hashtab.ht_changed)
3396 break;
3397 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003398 }
3399 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003400 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003401 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003402}
3403
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003404#if defined(EXITFREE) || defined(PROTO)
3405 void
3406free_all_functions(void)
3407{
3408 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003409 ufunc_T *fp;
3410 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003411 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003412 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003413
Bram Moolenaare38eab22019-12-05 21:50:01 +01003414 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003415 while (current_funccal != NULL)
3416 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003417 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003418 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003419 if (current_funccal == NULL && funccal_stack != NULL)
3420 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003421 }
3422
Bram Moolenaare38eab22019-12-05 21:50:01 +01003423 // First clear what the functions contain. Since this may lower the
3424 // reference count of a function, it may also free a function and change
3425 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003426 while (todo > 0)
3427 {
3428 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003429 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003430 if (!HASHITEM_EMPTY(hi))
3431 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 // clear the def function index now
3433 fp = HI2UF(hi);
3434 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003435 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436
Bram Moolenaare38eab22019-12-05 21:50:01 +01003437 // Only free functions that are not refcounted, those are
3438 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003439 if (func_name_refcount(fp->uf_name))
3440 ++skipped;
3441 else
3442 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003443 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003444 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003445 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003446 {
3447 skipped = 0;
3448 break;
3449 }
3450 }
3451 --todo;
3452 }
3453 }
3454
Bram Moolenaare38eab22019-12-05 21:50:01 +01003455 // Now actually free the functions. Need to start all over every time,
3456 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003457 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003458 while (func_hashtab.ht_used > skipped)
3459 {
3460 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003461 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003462 if (!HASHITEM_EMPTY(hi))
3463 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003464 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003465 // Only free functions that are not refcounted, those are
3466 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003467 fp = HI2UF(hi);
3468 if (func_name_refcount(fp->uf_name))
3469 ++skipped;
3470 else
3471 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003472 if (func_free(fp, FALSE) == OK)
3473 {
3474 skipped = 0;
3475 break;
3476 }
3477 // did not actually free it
3478 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003479 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003480 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003481 }
3482 if (skipped == 0)
3483 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484
3485 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003486}
3487#endif
3488
3489/*
3490 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003491 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3492 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003493 * "len" is the length of "name", or -1 for NUL terminated.
3494 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003496builtin_function(char_u *name, int len)
3497{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003498 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003499
Bram Moolenaara26b9702020-04-18 19:53:28 +02003500 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003501 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003502 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3503 {
3504 if (name[i] == AUTOLOAD_CHAR)
3505 return FALSE;
3506 if (!eval_isnamec(name[i]))
3507 {
3508 // "name.something" is not a builtin function
3509 if (name[i] == '.')
3510 return FALSE;
3511 break;
3512 }
3513 }
3514 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003515}
3516
3517 int
3518func_call(
3519 char_u *name,
3520 typval_T *args,
3521 partial_T *partial,
3522 dict_T *selfdict,
3523 typval_T *rettv)
3524{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003525 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003526 listitem_T *item;
3527 typval_T argv[MAX_FUNC_ARGS + 1];
3528 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003529 int r = 0;
3530
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003531 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003532 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003533 {
3534 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3535 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003536 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003537 break;
3538 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003539 // Make a copy of each argument. This is needed to be able to set
3540 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003541 copy_tv(&item->li_tv, &argv[argc++]);
3542 }
3543
3544 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003545 {
3546 funcexe_T funcexe;
3547
Bram Moolenaara80faa82020-04-12 19:37:17 +02003548 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003549 funcexe.fe_firstline = curwin->w_cursor.lnum;
3550 funcexe.fe_lastline = curwin->w_cursor.lnum;
3551 funcexe.fe_evaluate = TRUE;
3552 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003553 if (partial != NULL)
3554 {
3555 funcexe.fe_object = partial->pt_obj;
3556 if (funcexe.fe_object != NULL)
3557 ++funcexe.fe_object->obj_refcount;
3558 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003559 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003560 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3561 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003562
Bram Moolenaare38eab22019-12-05 21:50:01 +01003563 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003564 while (argc > 0)
3565 clear_tv(&argv[--argc]);
3566
3567 return r;
3568}
3569
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003570static int callback_depth = 0;
3571
3572 int
3573get_callback_depth(void)
3574{
3575 return callback_depth;
3576}
3577
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003578/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003579 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003580 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003581 */
3582 int
3583call_callback(
3584 callback_T *callback,
3585 int len, // length of "name" or -1 to use strlen()
3586 typval_T *rettv, // return value goes here
3587 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003588 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003589 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003590{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003591 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003592 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003593
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003594 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3595 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003596
Christian Brabandt20764632023-11-12 16:59:29 +01003597 if (callback_depth > MAX_CALLBACK_DEPTH)
Christian Brabandt47510f32023-10-15 09:56:16 +02003598 {
3599 emsg(_(e_command_too_recursive));
3600 return FAIL;
3601 }
3602
Bram Moolenaara80faa82020-04-12 19:37:17 +02003603 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003604 funcexe.fe_evaluate = TRUE;
3605 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003606 if (callback->cb_partial != NULL)
3607 {
3608 funcexe.fe_object = callback->cb_partial->pt_obj;
3609 if (funcexe.fe_object != NULL)
3610 ++funcexe.fe_object->obj_refcount;
3611 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003612 ++callback_depth;
3613 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3614 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003615
3616 // When a :def function was called that uses :try an error would be turned
3617 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003618 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003619 {
3620 need_rethrow = FALSE;
3621 handle_did_throw();
3622 }
3623
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003624 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003625}
3626
3627/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003628 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003629 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003630 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3631 */
3632 varnumber_T
3633call_callback_retnr(
3634 callback_T *callback,
3635 int argcount, // number of "argvars"
3636 typval_T *argvars) // vars for arguments, must have "argcount"
3637 // PLUS ONE elements!
3638{
3639 typval_T rettv;
3640 varnumber_T retval;
3641
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003642 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003643 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003644
3645 retval = tv_get_number_chk(&rettv, NULL);
3646 clear_tv(&rettv);
3647 return retval;
3648}
3649
3650/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 * Give an error message for the result of a function.
3652 * Nothing if "error" is FCERR_NONE.
3653 */
3654 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003655user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656{
3657 switch (error)
3658 {
3659 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003660 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003661 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003662 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003663 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 break;
3665 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003666 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 break;
3668 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003669 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 break;
3671 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003672 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 break;
3674 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003675 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 break;
3677 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003678 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 break;
3680 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003681 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3682 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003684 case FCERR_OTHER:
3685 case FCERR_FAILED:
3686 // assume the error message was already given
3687 break;
3688 case FCERR_NONE:
3689 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 }
3691}
3692
3693/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003694 * Check the argument types "argvars[argcount]" for "name" using the
3695 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3696 * is already included in "argvars[]".
3697 * Will do nothing if "funcexe->fe_check_type" is NULL or
3698 * "funcexe->fe_evaluate" is FALSE;
3699 * Returns an FCERR_ value.
3700 */
3701 static funcerror_T
3702may_check_argument_types(
3703 funcexe_T *funcexe,
3704 typval_T *argvars,
3705 int argcount,
3706 int base_included,
3707 char_u *name)
3708{
3709 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3710 {
3711 // Check that the argument types are OK for the types of the funcref.
3712 if (check_argument_types(funcexe->fe_check_type,
3713 argvars, argcount,
3714 base_included ? NULL : funcexe->fe_basetv,
3715 name) == FAIL)
3716 return FCERR_OTHER;
3717 }
3718 return FCERR_NONE;
3719}
3720
3721/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003722 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003723 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003724 * Return FAIL when the function can't be called, OK otherwise.
3725 * Also returns OK when an error was encountered while executing the function.
3726 */
3727 int
3728call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003729 char_u *funcname, // name of the function
3730 int len, // length of "name" or -1 to use strlen()
3731 typval_T *rettv, // return value goes here
3732 int argcount_in, // number of "argvars"
3733 typval_T *argvars_in, // vars for arguments, must have "argcount"
3734 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003735 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003736{
3737 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003738 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003739 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003740 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003741 char_u fname_buf[FLEN_FIXED + 1];
3742 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003743 char_u *fname = NULL;
3744 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003745 int argcount = argcount_in;
3746 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003747 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003748 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003749 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003750 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003751 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003752 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003753 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003754 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003755
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003756 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3757 // even when call_func() returns FAIL.
3758 rettv->v_type = VAR_UNKNOWN;
3759
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003760 if (partial != NULL)
3761 fp = partial->pt_func;
3762 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003763 fp = funcexe->fe_ufunc;
3764
3765 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003766 {
3767 // Make a copy of the name, if it comes from a funcref variable it
3768 // could be changed or deleted in the called function.
3769 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3770 if (name == NULL)
3771 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003772
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003773 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3774 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003775
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003776 if (funcexe->fe_doesrange != NULL)
3777 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003778
3779 if (partial != NULL)
3780 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003781 // When the function has a partial with a dict and there is a dict
3782 // argument, use the dict argument. That is backwards compatible.
3783 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003784 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003785 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003786 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003787 {
3788 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003789 {
3790 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3791 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003792 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003793 goto theend;
3794 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003795 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003796 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003797 for (i = 0; i < argcount_in; ++i)
3798 argv[i + argv_clear] = argvars_in[i];
3799 argvars = argv;
3800 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003801
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003802 if (funcexe->fe_check_type != NULL
3803 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003804 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003805 // Now funcexe->fe_check_type is missing the added arguments,
3806 // make a copy of the type with the correction.
3807 check_type = *funcexe->fe_check_type;
3808 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003809 check_type.tt_args = check_type_args;
3810 CLEAR_FIELD(check_type_args);
3811 for (i = 0; i < check_type.tt_argcount; ++i)
3812 check_type_args[i + partial->pt_argc] =
3813 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003814 check_type.tt_argcount += partial->pt_argc;
3815 check_type.tt_min_argcount += partial->pt_argc;
3816 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003817 }
3818 }
3819
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003820 if (error == FCERR_NONE)
3821 // check the argument types if possible
3822 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3823 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003824
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003825 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003826 {
3827 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003828 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003829
Bram Moolenaar333894b2020-08-01 18:53:07 +02003830 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003831 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003832 {
3833 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003834 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003835 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003836
Bram Moolenaare38eab22019-12-05 21:50:01 +01003837 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003838 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003839 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003840
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003841 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003842 {
3843 /*
3844 * User defined function.
3845 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003846 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003847 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003848 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003849 if (fp != NULL && !is_global && in_vim9script()
3850 && func_requires_g_prefix(fp))
3851 // In Vim9 script g: is required to find a global
3852 // non-autoload function.
3853 fp = NULL;
3854 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003855
Bram Moolenaare38eab22019-12-05 21:50:01 +01003856 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003857 if (fp == NULL
3858 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003859 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003860 && !aborting())
3861 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003862 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003863 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003864 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003865 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003866 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3867 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003868 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003869 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003870 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003871 if (fp == NULL)
3872 {
3873 char_u *p = untrans_function_name(rfname);
3874
3875 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003876 // Don't do this if the name starts with "s:".
3877 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003878 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003879 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003880
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003881 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003882 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003883 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003884 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003885 int need_arg_check = FALSE;
3886 if (funcexe->fe_check_type == NULL)
3887 {
3888 funcexe->fe_check_type = fp->uf_func_type;
3889 need_arg_check = TRUE;
3890 }
3891
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003892 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003893 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003894 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003895 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003896 argv_clear, fp);
3897 need_arg_check = TRUE;
3898 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003899
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003900 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003901 {
3902 // Method call: base->Method()
3903 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003904 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003905 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003906 argvars = argv;
3907 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003908 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003909 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003910
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003911 // Check the argument types now that the function type and all
3912 // argument values are known, if not done above.
3913 if (need_arg_check)
3914 error = may_check_argument_types(funcexe, argvars, argcount,
3915 TRUE, (name != NULL) ? name : funcname);
3916 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3917 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003919 }
3920 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003921 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003922 {
3923 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003924 * expr->method(): Find the method name in the table, call its
3925 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003926 */
3927 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003928 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003929 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003930 else
3931 {
3932 /*
3933 * Find the function name in the table, call its implementation.
3934 */
3935 error = call_internal_func(fname, argcount, argvars, rettv);
3936 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003937
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003938 /*
3939 * The function call (or "FuncUndefined" autocommand sequence) might
3940 * have been aborted by an error, an interrupt, or an explicitly thrown
3941 * exception that has not been caught so far. This situation can be
3942 * tested for by calling aborting(). For an error in an internal
3943 * function or for the "E132" error in call_user_func(), however, the
3944 * throw point at which the "force_abort" flag (temporarily reset by
3945 * emsg()) is normally updated has not been reached yet. We need to
3946 * update that flag first to make aborting() reliable.
3947 */
3948 update_force_abort();
3949 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003950 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003951 ret = OK;
3952
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003953theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003954 /*
3955 * Report an error unless the argument evaluation or function call has been
3956 * cancelled due to an aborting error, an interrupt, or an exception.
3957 */
3958 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003959 user_func_error(error, (name != NULL) ? name : funcname,
3960 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003961
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003962 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003963 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003964 clear_tv(&argv[--argv_clear + argv_base]);
3965
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003966 vim_free(tofree);
3967 vim_free(name);
3968
3969 return ret;
3970}
3971
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003972/*
3973 * Call a function without arguments, partial or dict.
3974 * This is like call_func() when the call is only "FuncName()".
3975 * To be used by "expr" options.
3976 * Returns NOTDONE when the function could not be found.
3977 */
3978 int
3979call_simple_func(
3980 char_u *funcname, // name of the function
3981 int len, // length of "name" or -1 to use strlen()
3982 typval_T *rettv) // return value goes here
3983{
3984 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003985 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003986 char_u fname_buf[FLEN_FIXED + 1];
3987 char_u *tofree = NULL;
3988 char_u *name;
3989 char_u *fname;
3990 char_u *rfname;
3991 int is_global = FALSE;
3992 ufunc_T *fp;
3993
3994 rettv->v_type = VAR_NUMBER; // default rettv is number zero
3995 rettv->vval.v_number = 0;
3996
3997 // Make a copy of the name, an option can be changed in the function.
3998 name = vim_strnsave(funcname, len);
3999 if (name == NULL)
4000 return ret;
4001
4002 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4003
4004 // Skip "g:" before a function name.
4005 if (fname[0] == 'g' && fname[1] == ':')
4006 {
4007 is_global = TRUE;
4008 rfname = fname + 2;
4009 }
4010 else
4011 rfname = fname;
4012 fp = find_func(rfname, is_global);
4013 if (fp != NULL && !is_global && in_vim9script()
4014 && func_requires_g_prefix(fp))
4015 // In Vim9 script g: is required to find a global non-autoload
4016 // function.
4017 fp = NULL;
4018 if (fp == NULL)
4019 ret = NOTDONE;
4020 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4021 error = FCERR_DELETED;
4022 else if (fp != NULL)
4023 {
4024 typval_T argvars[1];
4025 funcexe_T funcexe;
4026
4027 argvars[0].v_type = VAR_UNKNOWN;
4028 CLEAR_FIELD(funcexe);
4029 funcexe.fe_evaluate = TRUE;
4030
4031 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4032 if (error == FCERR_NONE)
4033 ret = OK;
4034 }
4035
4036 user_func_error(error, name, FALSE);
4037 vim_free(tofree);
4038 vim_free(name);
4039
4040 return ret;
4041}
4042
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004043 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044printable_func_name(ufunc_T *fp)
4045{
4046 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4047}
4048
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004049/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004050 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4051 * TRUE. Otherwise return FALSE.
4052 */
4053 static int
4054function_list_modified(int prev_ht_changed)
4055{
4056 if (prev_ht_changed != func_hashtab.ht_changed)
4057 {
4058 emsg(_(e_function_list_was_modified));
4059 return TRUE;
4060 }
4061 return FALSE;
4062}
4063
4064/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004065 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004066 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004067 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004068list_func_head(ufunc_T *fp, int indent)
4069{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004070 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004071 int j;
4072
4073 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004074
4075 // a timer at the more prompt may have deleted the function
4076 if (function_list_modified(prev_ht_changed))
4077 return FAIL;
4078
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004079 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004080 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004081 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004082 msg_puts("def ");
4083 else
4084 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004086 msg_putchar('(');
4087 for (j = 0; j < fp->uf_args.ga_len; ++j)
4088 {
4089 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004090 msg_puts(", ");
4091 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 if (fp->uf_arg_types != NULL)
4093 {
4094 char *tofree;
4095
4096 msg_puts(": ");
4097 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4098 vim_free(tofree);
4099 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004100 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4101 {
4102 msg_puts(" = ");
4103 msg_puts(((char **)(fp->uf_def_args.ga_data))
4104 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4105 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004106 }
4107 if (fp->uf_varargs)
4108 {
4109 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004110 msg_puts(", ");
4111 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004112 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 if (fp->uf_va_name != NULL)
4114 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004115 if (!fp->uf_varargs)
4116 {
4117 if (j)
4118 msg_puts(", ");
4119 msg_puts("...");
4120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004122 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 {
4124 char *tofree;
4125
4126 msg_puts(": ");
4127 msg_puts(type_name(fp->uf_va_type, &tofree));
4128 vim_free(tofree);
4129 }
4130 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004131 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004132
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004133 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004134 {
4135 if (fp->uf_ret_type != &t_void)
4136 {
4137 char *tofree;
4138
4139 msg_puts(": ");
4140 msg_puts(type_name(fp->uf_ret_type, &tofree));
4141 vim_free(tofree);
4142 }
4143 }
4144 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004145 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004146 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004147 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004148 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004149 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004150 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004151 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004152 msg_clr_eos();
4153 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004154 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004155
4156 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004157}
4158
4159/*
4160 * Get a function name, translating "<SID>" and "<SNR>".
4161 * Also handles a Funcref in a List or Dictionary.
4162 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004163 * Set "*is_global" to TRUE when the function must be global, unless
4164 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004165 * flags:
4166 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004167 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004168 * TFN_QUIET: be quiet
4169 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004170 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004171 * Advances "pp" to just after the function name (if no error).
4172 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004173 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004174trans_function_name(
4175 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004176 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004177 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004178 int flags)
4179{
4180 return trans_function_name_ext(pp, is_global, skip, flags,
4181 NULL, NULL, NULL, NULL);
4182}
4183
4184/*
4185 * trans_function_name() with extra arguments.
4186 * "fdp", "partial", "type" and "ufunc" can be NULL.
4187 */
4188 char_u *
4189trans_function_name_ext(
4190 char_u **pp,
4191 int *is_global,
4192 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004193 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004194 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004195 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004196 type_T **type, // return: type of funcref
4197 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004198{
4199 char_u *name = NULL;
4200 char_u *start;
4201 char_u *end;
4202 int lead;
4203 char_u sid_buf[20];
4204 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004206 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004207 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004208 int vim9script = in_vim9script();
4209 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004210
4211 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004212 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004213 start = *pp;
4214
Bram Moolenaare38eab22019-12-05 21:50:01 +01004215 // Check for hard coded <SNR>: already translated function ID (from a user
4216 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004217 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4218 && (*pp)[2] == (int)KE_SNR)
4219 {
4220 *pp += 3;
4221 len = get_id_len(pp) + 3;
4222 return vim_strnsave(start, len);
4223 }
4224
Bram Moolenaare38eab22019-12-05 21:50:01 +01004225 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4226 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004227 lead = eval_fname_script(start);
4228 if (lead > 2)
4229 start += lead;
4230
Bram Moolenaare38eab22019-12-05 21:50:01 +01004231 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004232 end = get_lval(start, NULL, &lv, FALSE, skip,
4233 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004234 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004235 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004236 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004237 {
4238 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004239 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004240 goto theend;
4241 }
4242 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4243 {
4244 /*
4245 * Report an invalid expression in braces, unless the expression
4246 * evaluation has been cancelled due to an aborting error, an
4247 * interrupt, or an exception.
4248 */
4249 if (!aborting())
4250 {
4251 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004252 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004253 }
4254 else
4255 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4256 goto theend;
4257 }
4258
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004259 if (lv.ll_ufunc != NULL)
4260 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004261 if (ufunc != NULL)
4262 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004263 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004264 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004265 goto theend;
4266 }
4267
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004268 if (lv.ll_tv != NULL)
4269 {
4270 if (fdp != NULL)
4271 {
4272 fdp->fd_dict = lv.ll_dict;
4273 fdp->fd_newkey = lv.ll_newkey;
4274 lv.ll_newkey = NULL;
4275 fdp->fd_di = lv.ll_di;
4276 }
4277 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4278 {
4279 name = vim_strsave(lv.ll_tv->vval.v_string);
4280 *pp = end;
4281 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004282 else if (lv.ll_tv->v_type == VAR_CLASS
4283 && lv.ll_tv->vval.v_class != NULL)
4284 {
4285 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4286 *pp = end;
4287 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004288 else if (lv.ll_tv->v_type == VAR_PARTIAL
4289 && lv.ll_tv->vval.v_partial != NULL)
4290 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004291 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004292 *pp = end;
4293 if (partial != NULL)
4294 *partial = lv.ll_tv->vval.v_partial;
4295 }
4296 else
4297 {
4298 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4299 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004300 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004301 else
4302 *pp = end;
4303 name = NULL;
4304 }
4305 goto theend;
4306 }
4307
4308 if (lv.ll_name == NULL)
4309 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004310 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004311 *pp = end;
4312 goto theend;
4313 }
4314
Bram Moolenaare38eab22019-12-05 21:50:01 +01004315 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004316 if (lv.ll_exp_name != NULL)
4317 {
4318 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004319 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004320 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004321 if (name == lv.ll_exp_name)
4322 name = NULL;
4323 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004324 else if (lv.ll_sid > 0)
4325 {
4326 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4327 int cc = *lv.ll_name_end;
4328
4329 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4330 *lv.ll_name_end = NUL;
4331 if (si->sn_autoload_prefix != NULL)
4332 {
4333 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4334 }
4335 else
4336 {
4337 sid_buf[0] = K_SPECIAL;
4338 sid_buf[1] = KS_EXTRA;
4339 sid_buf[2] = (int)KE_SNR;
4340 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004341 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004342 name = concat_str(sid_buf, lv.ll_name);
4343 }
4344 *lv.ll_name_end = cc;
4345 *pp = end;
4346 goto theend;
4347 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004348 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004349 {
4350 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004351 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004352 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004353 if (name == *pp)
4354 name = NULL;
4355 }
4356 if (name != NULL)
4357 {
4358 name = vim_strsave(name);
4359 *pp = end;
4360 if (STRNCMP(name, "<SNR>", 5) == 0)
4361 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004362 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004363 name[0] = K_SPECIAL;
4364 name[1] = KS_EXTRA;
4365 name[2] = (int)KE_SNR;
4366 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4367 }
4368 goto theend;
4369 }
4370
4371 if (lv.ll_exp_name != NULL)
4372 {
4373 len = (int)STRLEN(lv.ll_exp_name);
4374 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4375 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4376 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004377 // When there was "s:" already or the name expanded to get a
4378 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004379 lv.ll_name += 2;
4380 len -= 2;
4381 lead = 2;
4382 }
4383 }
4384 else
4385 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004386 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004387 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004388 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004389 if (lv.ll_name[0] == 'g')
4390 {
4391 if (is_global != NULL)
4392 {
4393 *is_global = TRUE;
4394 }
4395 else
4396 {
4397 // dropping "g:" without setting "is_global" won't work in
4398 // Vim9script, put it back later
4399 prefix_g = TRUE;
4400 extra = 2;
4401 }
4402 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004403 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004404 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004405 len = (int)(end - lv.ll_name);
4406 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004407 if (len <= 0)
4408 {
4409 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004410 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004411 goto theend;
4412 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004413
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004414 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004415 // starts with a lower case character: dict.func(). Or when in a class.
4416 vim9_local = ASCII_ISUPPER(*start) && vim9script
4417 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004419 /*
4420 * Copy the function name to allocated memory.
4421 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4422 * Accept <SNR>123_name() outside a script.
4423 */
4424 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004425 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004426 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004427 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004428 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004429 {
Kota Kato948a3892022-08-16 16:09:59 +01004430 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4431 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004432 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004433 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004434 goto theend;
4435 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004437 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004438 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004440 || eval_fname_sid(*pp))
4441 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004443 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004444 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004445 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004446 goto theend;
4447 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004448 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004449 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 extra = 3 + (int)STRLEN(sid_buf);
4451 else
4452 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004453 }
4454 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004455 // The function name must start with an upper case letter (unless it is a
4456 // Vim9 class new() function or a Vim9 class private method)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004457 else if (!(flags & TFN_INT)
4458 && (builtin_function(lv.ll_name, len)
4459 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004460 && !((flags & TFN_IN_CLASS)
4461 && (STRNCMP(lv.ll_name, "new", 3) == 0
4462 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004463 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004464 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004465 : e_function_name_must_start_with_capital_or_s_str),
4466 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004467 goto theend;
4468 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004469 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004470 {
4471 char_u *cp = vim_strchr(lv.ll_name, ':');
4472
4473 if (cp != NULL && cp < end)
4474 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004475 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004476 goto theend;
4477 }
4478 }
4479
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004481 if (name != NULL)
4482 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004483 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004484 {
4485 name[0] = K_SPECIAL;
4486 name[1] = KS_EXTRA;
4487 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004488 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004489 STRCPY(name + 3, sid_buf);
4490 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004491 else if (prefix_g)
4492 {
4493 name[0] = 'g';
4494 name[1] = ':';
4495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4497 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004498 }
4499 *pp = end;
4500
4501theend:
4502 clear_lval(&lv);
4503 return name;
4504}
4505
4506/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004507 * Assuming "name" is the result of trans_function_name() and it was prefixed
4508 * to use the script-local name, return the unmodified name (points into
4509 * "name"). Otherwise return NULL.
4510 * This can be used to first search for a script-local function and fall back
4511 * to the global function if not found.
4512 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004513 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004514untrans_function_name(char_u *name)
4515{
4516 char_u *p;
4517
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004518 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004519 {
4520 p = vim_strchr(name, '_');
4521 if (p != NULL)
4522 return p + 1;
4523 }
4524 return NULL;
4525}
4526
4527/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004528 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4529 * current script ID and returns the expanded function name. The caller should
4530 * free the returned name. If not called from a script context or the function
4531 * name doesn't start with these prefixes, then returns NULL.
4532 * This doesn't check whether the script-local function exists or not.
4533 */
4534 char_u *
4535get_scriptlocal_funcname(char_u *funcname)
4536{
4537 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004538 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004539 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004540 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004541
4542 if (funcname == NULL)
4543 return NULL;
4544
4545 if (STRNCMP(funcname, "s:", 2) != 0
4546 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004547 {
4548 ufunc_T *ufunc;
4549
4550 // The function name does not have a script-local prefix. Try finding
4551 // it when in a Vim9 script and there is no "g:" prefix.
4552 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4553 return NULL;
4554 ufunc = find_func(funcname, FALSE);
4555 if (ufunc == NULL || func_is_global(ufunc)
4556 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4557 return NULL;
4558 ++p;
4559 off = 0;
4560 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004561 else
4562 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004563
4564 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4565 {
4566 emsg(_(e_using_sid_not_in_script_context));
4567 return NULL;
4568 }
4569 // Expand s: prefix into <SNR>nr_<name>
4570 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4571 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004572 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004573 if (newname == NULL)
4574 return NULL;
4575 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004576 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004577
4578 return newname;
4579}
4580
4581/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004582 * Return script-local "fname" with the 3-byte sequence replaced by
4583 * printable <SNR> in allocated memory.
4584 */
4585 char_u *
4586alloc_printable_func_name(char_u *fname)
4587{
4588 char_u *n = alloc(STRLEN(fname + 3) + 6);
4589
4590 if (n != NULL)
4591 {
4592 STRCPY(n, "<SNR>");
4593 STRCPY(n + 5, fname + 3);
4594 }
4595 return n;
4596}
4597
4598/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004599 * Call trans_function_name(), except that a lambda is returned as-is.
4600 * Returns the name in allocated memory.
4601 */
4602 char_u *
4603save_function_name(
4604 char_u **name,
4605 int *is_global,
4606 int skip,
4607 int flags,
4608 funcdict_T *fudi)
4609{
4610 char_u *p = *name;
4611 char_u *saved;
4612
4613 if (STRNCMP(p, "<lambda>", 8) == 0)
4614 {
4615 p += 8;
4616 (void)getdigits(&p);
4617 saved = vim_strnsave(*name, p - *name);
4618 if (fudi != NULL)
4619 CLEAR_POINTER(fudi);
4620 }
4621 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004622 saved = trans_function_name_ext(&p, is_global, skip,
4623 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004624 *name = p;
4625 return saved;
4626}
4627
4628/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004629 * List functions. When "regmatch" is NULL all of then.
4630 * Otherwise functions matching "regmatch".
4631 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004632 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004633list_functions(regmatch_T *regmatch)
4634{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004635 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004636 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004637 hashitem_T *hi;
4638
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004639 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004640 {
4641 if (!HASHITEM_EMPTY(hi))
4642 {
4643 ufunc_T *fp = HI2UF(hi);
4644
4645 --todo;
4646 if ((fp->uf_flags & FC_DEAD) == 0
4647 && (regmatch == NULL
4648 ? !message_filtered(fp->uf_name)
4649 && !func_name_refcount(fp->uf_name)
4650 : !isdigit(*fp->uf_name)
4651 && vim_regexec(regmatch, fp->uf_name, 0)))
4652 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004653 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004654 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004655 if (function_list_modified(prev_ht_changed))
4656 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004657 }
4658 }
4659 }
4660}
4661
4662/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004663 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004664 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4665 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004666 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004667 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4668 * With CF_INTERFACE the function is define inside an interface, only the
4669 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004670 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004671 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004672 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004673define_function(
4674 exarg_T *eap,
4675 char_u *name_arg,
4676 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004677 int class_flags,
4678 ocmember_T *obj_members,
4679 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004680{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004681 int j;
4682 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004683 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004684 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004685 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004686 char_u *p;
4687 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004688 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004689 char_u *line_arg = NULL;
4690 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004692 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004693 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004694 garray_T newlines;
4695 int varargs = FALSE;
4696 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004698 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004699 int fp_allocated = FALSE;
4700 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004701 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004702 dictitem_T *v;
4703 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004704 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004705 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004706 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004707 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004708 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004709 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004710
4711 /*
4712 * ":function" without argument: list functions.
4713 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004714 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004715 {
4716 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004717 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004718 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004719 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004720 }
4721
4722 /*
4723 * ":function /pat": list functions matching pattern.
4724 */
4725 if (*eap->arg == '/')
4726 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004727 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004728 if (!eap->skip)
4729 {
4730 regmatch_T regmatch;
4731
4732 c = *p;
4733 *p = NUL;
4734 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4735 *p = c;
4736 if (regmatch.regprog != NULL)
4737 {
4738 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004739 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004740 vim_regfree(regmatch.regprog);
4741 }
4742 }
4743 if (*p == '/')
4744 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004745 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004746 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004747 }
4748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 ga_init(&newargs);
4750 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004751 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004752 ga_init(&default_args);
4753
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004754 /*
4755 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004756 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004757 * "name" == func, "fudi.fd_dict" == NULL
4758 * dict.func new dictionary entry
4759 * "name" == NULL, "fudi.fd_dict" set,
4760 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4761 * dict.func existing dict entry with a Funcref
4762 * "name" == func, "fudi.fd_dict" set,
4763 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4764 * dict.func existing dict entry that's not a Funcref
4765 * "name" == NULL, "fudi.fd_dict" set,
4766 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4767 * s:func script-local function name
4768 * g:func global function name, same as "func"
4769 */
4770 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004771 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004772 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004773 // nested function, argument is (args).
4774 paren = TRUE;
4775 CLEAR_FIELD(fudi);
4776 }
4777 else
4778 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004779 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004780 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004781 if (p[0] == 's' && p[1] == ':')
4782 {
4783 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4784 return NULL;
4785 }
4786 p = to_name_end(p, TRUE);
4787 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4788 {
4789 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4790 eap->arg);
4791 return NULL;
4792 }
4793 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004794 }
4795
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004796 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004797 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004798 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004799 paren = (vim_strchr(p, '(') != NULL);
4800 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004801 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004802 /*
4803 * Return on an invalid expression in braces, unless the expression
4804 * evaluation has been cancelled due to an aborting error, an
4805 * interrupt, or an exception.
4806 */
4807 if (!aborting())
4808 {
4809 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004810 semsg(_(e_key_not_present_in_dictionary_str),
4811 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004812 vim_free(fudi.fd_newkey);
4813 return NULL;
4814 }
4815 else
4816 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004817 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004818
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004819 // For "export def FuncName()" in an autoload script the function name
4820 // is stored with the legacy autoload name "dir#script#FuncName" so
4821 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004822 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004823 {
4824 char_u *prefixed = may_prefix_autoload(name);
4825
4826 if (prefixed != NULL && prefixed != name)
4827 {
4828 vim_free(name);
4829 name = prefixed;
4830 }
4831 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004832 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004833 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004834 {
4835 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4836 goto ret_free;
4837 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004838 }
4839
Bram Moolenaare38eab22019-12-05 21:50:01 +01004840 // An error in a function call during evaluation of an expression in magic
4841 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004842 saved_did_emsg = did_emsg;
4843 did_emsg = FALSE;
4844
4845 /*
4846 * ":function func" with only function name: list function.
4847 */
4848 if (!paren)
4849 {
4850 if (!ends_excmd(*skipwhite(p)))
4851 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004852 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004853 goto ret_free;
4854 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004855 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004856 if (eap->nextcmd != NULL)
4857 *p = NUL;
4858 if (!eap->skip && !got_int)
4859 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004860 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004861 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4862 {
4863 char_u *up = untrans_function_name(name);
4864
4865 // With Vim9 script the name was made script-local, if not
4866 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004867 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004868 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004869 }
4870
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004871 if (fp != NULL)
4872 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004873 // Check no function was added or removed from a timer, e.g. at
4874 // the more prompt. "fp" may then be invalid.
4875 int prev_ht_changed = func_hashtab.ht_changed;
4876
4877 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004878 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004879 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4880 {
4881 if (FUNCLINE(fp, j) == NULL)
4882 continue;
4883 msg_putchar('\n');
4884 msg_outnum((long)(j + 1));
4885 if (j < 9)
4886 msg_putchar(' ');
4887 if (j < 99)
4888 msg_putchar(' ');
4889 if (function_list_modified(prev_ht_changed))
4890 break;
4891 msg_prt_line(FUNCLINE(fp, j), FALSE);
4892 out_flush(); // show a line at a time
4893 ui_breakcheck();
4894 }
4895 if (!got_int)
4896 {
4897 msg_putchar('\n');
4898 if (!function_list_modified(prev_ht_changed))
4899 {
4900 if (fp->uf_def_status != UF_NOT_COMPILED)
4901 msg_puts(" enddef");
4902 else
4903 msg_puts(" endfunction");
4904 }
4905 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004906 }
4907 }
4908 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004909 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004910 }
4911 goto ret_free;
4912 }
4913
4914 /*
4915 * ":function name(arg1, arg2)" Define function.
4916 */
4917 p = skipwhite(p);
4918 if (*p != '(')
4919 {
4920 if (!eap->skip)
4921 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004922 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004923 goto ret_free;
4924 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004925 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004926 if (vim_strchr(p, '(') != NULL)
4927 p = vim_strchr(p, '(');
4928 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004929
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004930 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4931 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004932 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004933 goto ret_free;
4934 }
4935
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004936 // In Vim9 script only global functions can be redefined.
4937 if (vim9script && eap->forceit && !is_global)
4938 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004939 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004940 goto ret_free;
4941 }
4942
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004943 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004944
Bram Moolenaar04b12692020-05-04 23:24:44 +02004945 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004946 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004947 // Check the name of the function. Unless it's a dictionary function
4948 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004949 if (name != NULL)
4950 arg = name;
4951 else
4952 arg = fudi.fd_newkey;
4953 if (arg != NULL && (fudi.fd_di == NULL
4954 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4955 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4956 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004957 char_u *name_base = arg;
4958 int i;
4959
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004960 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004961 {
4962 name_base = vim_strchr(arg, '_');
4963 if (name_base == NULL)
4964 name_base = arg + 3;
4965 else
4966 ++name_base;
4967 }
4968 for (i = 0; name_base[i] != NUL && (i == 0
4969 ? eval_isnamec1(name_base[i])
4970 : eval_isnamec(name_base[i])); ++i)
4971 ;
4972 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004973 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004974
4975 // In Vim9 script a function cannot have the same name as a
4976 // variable.
4977 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004978 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4979 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004980 + EVAL_VAR_NO_FUNC) == OK)
4981 {
4982 semsg(_(e_redefining_script_item_str), name_base);
4983 goto ret_free;
4984 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004985 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004986 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004987 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004988 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004989 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004990 goto ret_free;
4991 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004992 }
4993
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004994 // This may get more lines and make the pointers into the first line
4995 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004996 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004997 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00004998 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02004999 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005000 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005001 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005002 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005003 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005006 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005008 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005009 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005010 if (*p != ':')
5011 {
5012 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5013 p = skipwhite(p);
5014 }
5015 else if (!IS_WHITE_OR_NUL(p[1]))
5016 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005017 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005018 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005020 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005021 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005022 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005024 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005026 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005027 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005028 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005029 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005030 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005031 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005032 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 else
5034 // find extra arguments "range", "dict", "abort" and "closure"
5035 for (;;)
5036 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005037 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038 p = skipwhite(p);
5039 if (STRNCMP(p, "range", 5) == 0)
5040 {
5041 flags |= FC_RANGE;
5042 p += 5;
5043 }
5044 else if (STRNCMP(p, "dict", 4) == 0)
5045 {
5046 flags |= FC_DICT;
5047 p += 4;
5048 }
5049 else if (STRNCMP(p, "abort", 5) == 0)
5050 {
5051 flags |= FC_ABORT;
5052 p += 5;
5053 }
5054 else if (STRNCMP(p, "closure", 7) == 0)
5055 {
5056 flags |= FC_CLOSURE;
5057 p += 7;
5058 if (current_funccal == NULL)
5059 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005060 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061 name == NULL ? (char_u *)"" : name);
5062 goto erret;
5063 }
5064 }
5065 else
5066 break;
5067 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005068
Bram Moolenaare38eab22019-12-05 21:50:01 +01005069 // When there is a line break use what follows for the function body.
5070 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005071 if (*p == '\n')
5072 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005073 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005074 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5075 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005076 && !(VIM_ISWHITE(*whitep) && *p == '#'
5077 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005078 && !eap->skip
5079 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005080 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005081
5082 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005083 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5084 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005085 */
5086 if (KeyTyped)
5087 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005088 // Check if the function already exists, don't let the user type the
5089 // whole function before telling him it doesn't work! For a script we
5090 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005091 if (!eap->skip && !eap->forceit)
5092 {
5093 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005094 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005095 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005096 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005097 }
5098
5099 if (!eap->skip && did_emsg)
5100 goto erret;
5101
Bram Moolenaare38eab22019-12-05 21:50:01 +01005102 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005103 cmdline_row = msg_row;
5104 }
5105
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005106 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005107 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005108
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005109 // Do not define the function when getting the body fails and when
5110 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005111 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005112 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005113 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5114 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005115 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005116 goto erret;
5117
5118 /*
5119 * If there are no errors, add the function
5120 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005121 if (fudi.fd_dict != NULL)
5122 {
5123 char numbuf[20];
5124
5125 fp = NULL;
5126 if (fudi.fd_newkey == NULL && !eap->forceit)
5127 {
5128 emsg(_(e_dictionary_entry_already_exists));
5129 goto erret;
5130 }
5131 if (fudi.fd_di == NULL)
5132 {
5133 // Can't add a function to a locked dictionary
5134 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5135 goto erret;
5136 }
5137 // Can't change an existing function if it is locked
5138 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5139 goto erret;
5140
5141 // Give the function a sequential number. Can only be used with a
5142 // Funcref!
5143 vim_free(name);
5144 sprintf(numbuf, "%d", ++func_nr);
5145 name = vim_strsave((char_u *)numbuf);
5146 if (name == NULL)
5147 goto erret;
5148 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005149 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005150 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005152 char_u *find_name = name;
5153 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005154 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005155
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005156 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005157 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005158 var_conflict = TRUE;
5159
5160 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5161 {
5162 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5163
5164 if (si->sn_autoload_prefix != NULL)
5165 {
5166 if (is_export)
5167 {
5168 find_name = name + STRLEN(si->sn_autoload_prefix);
5169 v = find_var(find_name, &ht, TRUE);
5170 if (v != NULL)
5171 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005172 // Only check if the function already exists in the script,
5173 // global functions can be shadowed.
5174 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005175 }
5176 else
5177 {
5178 char_u *prefixed = may_prefix_autoload(name);
5179
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005180 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005181 {
5182 v = find_var(prefixed, &ht, TRUE);
5183 if (v != NULL)
5184 var_conflict = TRUE;
5185 vim_free(prefixed);
5186 }
5187 }
5188 }
5189 }
5190 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005191 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005192 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005193 goto erret;
5194 }
5195
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005196 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005197 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005198 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005199 char_u *uname = untrans_function_name(name);
5200
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005201 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005202 }
5203
5204 if (fp != NULL || import != NULL)
5205 {
5206 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005208 // Function can be replaced with "function!" and when sourcing the
5209 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005210 // A name that is used by an import can not be overruled.
5211 if (import != NULL
5212 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005213 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005214 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005215 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005216 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005217 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005218 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005219 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005220 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005221 goto erret;
5222 }
5223 if (fp->uf_calls > 0)
5224 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005225 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005226 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005227 goto erret;
5228 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005229 if (fp->uf_refcount > 1)
5230 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005231 // This function is referenced somewhere, don't redefine it but
5232 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005233 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005234 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005235 fp = NULL;
5236 overwrite = TRUE;
5237 }
5238 else
5239 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005240 char_u *exp_name = fp->uf_name_exp;
5241
5242 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005243 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005244 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005245 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005246 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005247 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005248#ifdef FEAT_PROFILE
5249 fp->uf_profiling = FALSE;
5250 fp->uf_prof_initialized = FALSE;
5251#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005252 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005253 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005254 }
5255 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005256
5257 if (fp == NULL)
5258 {
5259 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5260 {
5261 int slen, plen;
5262 char_u *scriptname;
5263
Bram Moolenaare38eab22019-12-05 21:50:01 +01005264 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005265 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005266 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005267 {
5268 scriptname = autoload_name(name);
5269 if (scriptname != NULL)
5270 {
5271 p = vim_strchr(scriptname, '/');
5272 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005273 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005274 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005275 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005276 j = OK;
5277 vim_free(scriptname);
5278 }
5279 }
5280 if (j == FAIL)
5281 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005282 linenr_T save_lnum = SOURCING_LNUM;
5283
5284 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005285 semsg(_(e_function_name_does_not_match_script_file_name_str),
5286 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005287 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005288 goto erret;
5289 }
5290 }
5291
Bram Moolenaare01e5212023-01-08 20:31:18 +00005292 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005293 if (fp == NULL)
5294 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005295 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005296
5297 if (fudi.fd_dict != NULL)
5298 {
5299 if (fudi.fd_di == NULL)
5300 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005301 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005302 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5303 if (fudi.fd_di == NULL)
5304 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005305 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005306 goto erret;
5307 }
5308 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5309 {
5310 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005311 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005312 goto erret;
5313 }
5314 }
5315 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005316 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005317 clear_tv(&fudi.fd_di->di_tv);
5318 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005319 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005320
Bram Moolenaare38eab22019-12-05 21:50:01 +01005321 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005322 flags |= FC_DICT;
5323 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005324 }
5325 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005326 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005327 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005328 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005329
5330 if (eap->cmdidx == CMD_def)
5331 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005332 int lnum_save = SOURCING_LNUM;
5333 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005334
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005335 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005336
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005337 // error messages are for the first function line
5338 SOURCING_LNUM = sourcing_lnum_top;
5339
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005340 // The function may use script variables from the context.
5341 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005342
h-eastb895b0f2023-09-24 15:46:31 +02005343 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5344 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005345 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005346 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005347 free_fp = fp_allocated;
5348 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005349 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005350 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005351
5352 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005353 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005354 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005355 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005356 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005357 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005358 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005359 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005361 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005362 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005363
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005364 if (fp_allocated)
5365 {
5366 // insert the new function in the function list
5367 set_ufunc_name(fp, name);
5368 if (overwrite)
5369 {
5370 hi = hash_find(&func_hashtab, name);
5371 hi->hi_key = UF2HIKEY(fp);
5372 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005373 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005374 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005375 {
5376 free_fp = TRUE;
5377 goto erret;
5378 }
5379 fp->uf_refcount = 1;
5380 }
5381
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005382 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005383 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005384 if ((flags & FC_CLOSURE) != 0)
5385 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005386 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005387 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005388 }
5389 else
5390 fp->uf_scoped = NULL;
5391
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005392#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005393 if (prof_def_func())
5394 func_do_profile(fp);
5395#endif
5396 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005397 if (sandbox)
5398 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005399 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005400 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005401 fp->uf_flags = flags;
5402 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005403 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005404 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005405 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005406 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005407 if (is_export)
5408 {
5409 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005410 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005411 is_export = FALSE;
5412 }
5413
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005414 if (eap->cmdidx == CMD_def)
5415 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005416 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5417 // :func does not use Vim9 script syntax, even in a Vim9 script file
5418 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005419
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005420 goto ret_free;
5421
5422erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005423 if (fp != NULL)
5424 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005425 // these were set to "newargs" and "default_args", which are cleared
5426 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005427 ga_init(&fp->uf_args);
5428 ga_init(&fp->uf_def_args);
5429 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005430errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005431 ga_clear_strings(&newargs);
5432 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005433 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005434 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005435 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005436 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005437 VIM_CLEAR(fp->uf_va_name);
5438 clear_type_list(&fp->uf_type_list);
5439 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005440 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005441 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005442ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005443 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005444 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005445 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005446 if (name != name_arg)
5447 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005448 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005449 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005450
5451 return fp;
5452}
5453
5454/*
5455 * ":function"
5456 */
5457 void
5458ex_function(exarg_T *eap)
5459{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005460 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005461
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005462 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005463 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005464 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005465}
5466
5467/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005468 * Find a function by name, including "<lambda>123".
5469 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005470 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005471 * Return NULL if not found.
5472 */
5473 ufunc_T *
5474find_func_by_name(char_u *name, compiletype_T *compile_type)
5475{
5476 char_u *arg = name;
5477 char_u *fname;
5478 ufunc_T *ufunc;
5479 int is_global = FALSE;
5480
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005481 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5482 {
5483 *compile_type = CT_PROFILE;
5484 arg = skipwhite(arg + 7);
5485 }
5486 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5487 {
5488 *compile_type = CT_DEBUG;
5489 arg = skipwhite(arg + 5);
5490 }
5491
5492 if (STRNCMP(arg, "<lambda>", 8) == 0)
5493 {
5494 arg += 8;
5495 (void)getdigits(&arg);
5496 fname = vim_strnsave(name, arg - name);
5497 }
5498 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005499 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005500 // First try finding a method in a class, trans_function_name() will
5501 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005502 ufunc = find_class_func(&arg);
5503 if (ufunc != NULL)
5504 return ufunc;
5505
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005506 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005507 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005508 NULL, NULL, NULL, &ufunc);
5509 if (ufunc != NULL)
5510 {
5511 vim_free(fname);
5512 return ufunc;
5513 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005514 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005515 if (fname == NULL)
5516 {
5517 semsg(_(e_invalid_argument_str), name);
5518 return NULL;
5519 }
5520 if (!ends_excmd2(name, arg))
5521 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005522 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005523 emsg(ex_errmsg(e_trailing_characters_str, arg));
5524 return NULL;
5525 }
5526
5527 ufunc = find_func(fname, is_global);
5528 if (ufunc == NULL)
5529 {
5530 char_u *p = untrans_function_name(fname);
5531
5532 if (p != NULL)
5533 // Try again without making it script-local.
5534 ufunc = find_func(p, FALSE);
5535 }
5536 vim_free(fname);
5537 if (ufunc == NULL)
5538 semsg(_(e_cannot_find_function_str), name);
5539 return ufunc;
5540}
5541
5542/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005543 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005544 * be compiled or the one specified by the argument.
5545 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005546 */
5547 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005548ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005549{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005550 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005551 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005552 compiletype_T compile_type = CT_NONE;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005553 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005554 if (ufunc != NULL)
5555 {
5556 if (func_needs_compiling(ufunc, compile_type))
5557 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5558 else
5559 smsg(_("Function %s does not need compiling"), eap->arg);
5560 }
5561 }
5562 else
5563 {
5564 long todo = (long)func_hashtab.ht_used;
5565 int changed = func_hashtab.ht_changed;
5566 hashitem_T *hi;
5567
5568 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5569 {
5570 if (!HASHITEM_EMPTY(hi))
5571 {
5572 --todo;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005573 ufunc_T *ufunc = HI2UF(hi);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005574 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5575 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5576 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005577 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005578 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5579
5580 if (func_hashtab.ht_changed != changed)
5581 {
5582 // a function has been added or removed, need to start
5583 // over
5584 todo = (long)func_hashtab.ht_used;
5585 changed = func_hashtab.ht_changed;
5586 hi = func_hashtab.ht_array;
5587 --hi;
5588 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005589 }
5590 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005591 }
5592 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005593}
5594
5595/*
5596 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5597 * Return 2 if "p" starts with "s:".
5598 * Return 0 otherwise.
5599 */
5600 int
5601eval_fname_script(char_u *p)
5602{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005603 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5604 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005605 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5606 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5607 return 5;
5608 if (p[0] == 's' && p[1] == ':')
5609 return 2;
5610 return 0;
5611}
5612
5613 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005614translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005615{
5616 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005617 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005618 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005619}
5620
5621/*
5622 * Return TRUE when "ufunc" has old-style "..." varargs
5623 * or named varargs "...name: type".
5624 */
5625 int
5626has_varargs(ufunc_T *ufunc)
5627{
5628 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005629}
5630
5631/*
5632 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005633 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005634 */
5635 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005636function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005637{
5638 char_u *nm = name;
5639 char_u *p;
5640 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005641 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005642 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005643
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005644 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5645 if (no_deref)
5646 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005647 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005648 nm = skipwhite(nm);
5649
Bram Moolenaare38eab22019-12-05 21:50:01 +01005650 // Only accept "funcname", "funcname ", "funcname (..." and
5651 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005652 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005653 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005654 vim_free(p);
5655 return n;
5656}
5657
Bram Moolenaar113e1072019-01-20 15:30:40 +01005658#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005659 char_u *
5660get_expanded_name(char_u *name, int check)
5661{
5662 char_u *nm = name;
5663 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005664 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005665
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005666 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005667
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005668 if (p != NULL && *nm == NUL
5669 && (!check || translated_function_exists(p, is_global)))
5670 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005671
5672 vim_free(p);
5673 return NULL;
5674}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005675#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005676
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005677/*
5678 * Function given to ExpandGeneric() to obtain the list of user defined
5679 * function names.
5680 */
5681 char_u *
5682get_user_func_name(expand_T *xp, int idx)
5683{
5684 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005685 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005686 static hashitem_T *hi;
5687 ufunc_T *fp;
5688
5689 if (idx == 0)
5690 {
5691 done = 0;
5692 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005693 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005694 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005695 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005696 {
5697 if (done++ > 0)
5698 ++hi;
5699 while (HASHITEM_EMPTY(hi))
5700 ++hi;
5701 fp = HI2UF(hi);
5702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005703 // don't show dead, dict and lambda functions
5704 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005705 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005706 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005707
5708 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005709 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005710
5711 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005712 if (xp->xp_context != EXPAND_USER_FUNC
5713 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005714 {
5715 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005716 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005717 STRCAT(IObuff, ")");
5718 }
5719 return IObuff;
5720 }
5721 return NULL;
5722}
5723
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005724/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005725 * Make a copy of a function.
5726 * Intended to be used for a function defined on a base class that has a copy
5727 * on the child class.
5728 * The copy has uf_refcount set to one.
5729 * Returns NULL when out of memory.
5730 */
5731 ufunc_T *
5732copy_function(ufunc_T *fp)
5733{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005734 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005735 if (ufunc == NULL)
5736 return NULL;
5737
5738 // Most things can just be copied.
5739 *ufunc = *fp;
5740
5741 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5742 ufunc->uf_dfunc_idx = 0;
5743 ufunc->uf_class = NULL;
5744
5745 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5746 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5747
5748 if (ufunc->uf_arg_types != NULL)
5749 {
5750 // "uf_arg_types" is an allocated array, make a copy.
5751 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5752 if (at != NULL)
5753 {
5754 mch_memmove(at, ufunc->uf_arg_types,
5755 sizeof(type_T *) * ufunc->uf_args.ga_len);
5756 ufunc->uf_arg_types = at;
5757 }
5758 }
5759
5760 // TODO: how about the types themselves? they can be freed when the
5761 // original function is freed:
5762 // type_T **uf_arg_types;
5763 // type_T *uf_ret_type;
5764
Ernie Rael114ec812023-06-04 18:11:35 +01005765 // make uf_type_list empty
5766 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005767
5768 // TODO: partial_T *uf_partial;
5769
5770 if (ufunc->uf_va_name != NULL)
5771 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5772
5773 // TODO:
5774 // type_T *uf_va_type;
5775 // type_T *uf_func_type;
5776
5777 ufunc->uf_block_depth = 0;
5778 ufunc->uf_block_ids = NULL;
5779
5780 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5781
5782 ufunc->uf_refcount = 1;
5783 ufunc->uf_name_exp = NULL;
5784 STRCPY(ufunc->uf_name, fp->uf_name);
5785
5786 return ufunc;
5787}
5788
5789/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005790 * ":delfunction {name}"
5791 */
5792 void
5793ex_delfunction(exarg_T *eap)
5794{
5795 ufunc_T *fp = NULL;
5796 char_u *p;
5797 char_u *name;
5798 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005799 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005800
5801 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005802 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5803 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005804 vim_free(fudi.fd_newkey);
5805 if (name == NULL)
5806 {
5807 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005808 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005809 return;
5810 }
5811 if (!ends_excmd(*skipwhite(p)))
5812 {
5813 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005814 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005815 return;
5816 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005817 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005818 if (eap->nextcmd != NULL)
5819 *p = NUL;
5820
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005821 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005822 {
5823 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005824 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005825 vim_free(name);
5826 return;
5827 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005828 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005829 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005830 vim_free(name);
5831
5832 if (!eap->skip)
5833 {
5834 if (fp == NULL)
5835 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005836 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005837 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005838 return;
5839 }
5840 if (fp->uf_calls > 0)
5841 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005842 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005843 return;
5844 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005845 if (fp->uf_flags & FC_VIM9)
5846 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005847 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005848 return;
5849 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005850
5851 if (fudi.fd_dict != NULL)
5852 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005853 // Delete the dict item that refers to the function, it will
5854 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005855 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005856 }
5857 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005858 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005859 // A normal function (not a numbered function or lambda) has a
5860 // refcount of 1 for the entry in the hashtable. When deleting
5861 // it and the refcount is more than one, it should be kept.
5862 // A numbered function and lambda should be kept if the refcount is
5863 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005864 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005865 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005866 // Function is still referenced somewhere. Don't free it but
5867 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005868 if (func_remove(fp))
5869 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005870 }
5871 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005872 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005873 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005874 }
5875}
5876
5877/*
5878 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005879 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005880 */
5881 void
5882func_unref(char_u *name)
5883{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005884 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005885
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005886 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005887 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005888 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005889 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005890 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005891#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005892 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005893#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005894 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005895 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005896 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005897}
5898
5899/*
5900 * Unreference a Function: decrement the reference count and free it when it
5901 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005902 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005903 */
5904 void
5905func_ptr_unref(ufunc_T *fp)
5906{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005907 if (fp != NULL && (--fp->uf_refcount <= 0
5908 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5909 && fp->uf_partial->pt_refcount <= 1
5910 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005911 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005912 // Only delete it when it's not being used. Otherwise it's done
5913 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005914 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005915 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005916 }
5917}
5918
5919/*
5920 * Count a reference to a Function.
5921 */
5922 void
5923func_ref(char_u *name)
5924{
5925 ufunc_T *fp;
5926
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005927 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005928 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005929 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005930 if (fp != NULL)
5931 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005932 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005933 // Only give an error for a numbered function.
5934 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005935 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005936}
5937
5938/*
5939 * Count a reference to a Function.
5940 */
5941 void
5942func_ptr_ref(ufunc_T *fp)
5943{
5944 if (fp != NULL)
5945 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005946}
5947
5948/*
5949 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5950 * referenced from anywhere that is in use.
5951 */
5952 static int
5953can_free_funccal(funccall_T *fc, int copyID)
5954{
Bram Moolenaarca16c602022-09-06 18:57:08 +01005955 return (fc->fc_l_varlist.lv_copyID != copyID
5956 && fc->fc_l_vars.dv_copyID != copyID
5957 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005958 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005959}
5960
5961/*
5962 * ":return [expr]"
5963 */
5964 void
5965ex_return(exarg_T *eap)
5966{
5967 char_u *arg = eap->arg;
5968 typval_T rettv;
5969 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005970 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005971
5972 if (current_funccal == NULL)
5973 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005974 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005975 return;
5976 }
5977
Bram Moolenaar844fb642021-10-23 13:32:30 +01005978 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005979 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5980
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005981 if (eap->skip)
5982 ++emsg_skip;
5983
5984 eap->nextcmd = NULL;
5985 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005986 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005987 {
5988 if (!eap->skip)
5989 returning = do_return(eap, FALSE, TRUE, &rettv);
5990 else
5991 clear_tv(&rettv);
5992 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005993 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005994 else if (!eap->skip)
5995 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005996 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005997 update_force_abort();
5998
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005999 /*
6000 * Return unless the expression evaluation has been cancelled due to an
6001 * aborting error, an interrupt, or an exception.
6002 */
6003 if (!aborting())
6004 returning = do_return(eap, FALSE, TRUE, NULL);
6005 }
6006
Bram Moolenaare38eab22019-12-05 21:50:01 +01006007 // When skipping or the return gets pending, advance to the next command
6008 // in this line (!returning). Otherwise, ignore the rest of the line.
6009 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006010 if (returning)
6011 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006012 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006013 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006014
6015 if (eap->skip)
6016 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006017 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006018}
6019
zeertzjq5299c092023-04-12 20:48:16 +01006020/*
6021 * Lower level implementation of "call". Only called when not skipping.
6022 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006023 static int
6024ex_call_inner(
6025 exarg_T *eap,
6026 char_u *name,
6027 char_u **arg,
6028 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006029 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006030 evalarg_T *evalarg)
6031{
6032 linenr_T lnum;
6033 int doesrange;
6034 typval_T rettv;
6035 int failed = FALSE;
6036
zeertzjq5299c092023-04-12 20:48:16 +01006037 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006038 for ( ; lnum <= eap->line2; ++lnum)
6039 {
6040 funcexe_T funcexe;
6041
zeertzjq5299c092023-04-12 20:48:16 +01006042 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006043 {
6044 if (lnum > curbuf->b_ml.ml_line_count)
6045 {
6046 // If the function deleted lines or switched to another buffer
6047 // the line number may become invalid.
6048 emsg(_(e_invalid_range));
6049 break;
6050 }
6051 curwin->w_cursor.lnum = lnum;
6052 curwin->w_cursor.col = 0;
6053 curwin->w_cursor.coladd = 0;
6054 }
6055 *arg = startarg;
6056
6057 funcexe = *funcexe_init;
6058 funcexe.fe_doesrange = &doesrange;
6059 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6060 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6061 {
6062 failed = TRUE;
6063 break;
6064 }
6065 if (has_watchexpr())
6066 dbg_check_breakpoint(eap);
6067
6068 // Handle a function returning a Funcref, Dictionary or List.
6069 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006070 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006071 {
6072 failed = TRUE;
6073 break;
6074 }
6075
6076 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006077 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006078 break;
6079
6080 // Stop when immediately aborting on error, or when an interrupt
6081 // occurred or an exception was thrown but not caught.
6082 // get_func_tv() returned OK, so that the check for trailing
6083 // characters below is executed.
6084 if (aborting())
6085 break;
6086 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006087 return failed;
6088}
6089
6090/*
6091 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6092 * Returns FAIL or OK.
6093 */
6094 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006095ex_defer_inner(
6096 char_u *name,
6097 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006098 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006099 partial_T *partial,
6100 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006101{
6102 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006103 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006104 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006105 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006106
6107 if (current_funccal == NULL)
6108 {
6109 semsg(_(e_str_not_inside_function), "defer");
6110 return FAIL;
6111 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006112 if (partial != NULL)
6113 {
6114 if (partial->pt_dict != NULL)
6115 {
6116 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6117 return FAIL;
6118 }
6119 if (partial->pt_argc > 0)
6120 {
6121 int i;
6122
6123 partial_argc = partial->pt_argc;
6124 for (i = 0; i < partial_argc; ++i)
6125 copy_tv(&partial->pt_argv[i], &argvars[i]);
6126 }
6127 }
6128 r = get_func_arguments(arg, evalarg, FALSE,
6129 argvars + partial_argc, &argcount);
6130 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006131
6132 if (r == OK)
6133 {
6134 if (type != NULL)
6135 {
6136 // Check that the arguments are OK for the types of the funcref.
6137 r = check_argument_types(type, argvars, argcount, NULL, name);
6138 }
6139 else if (builtin_function(name, -1))
6140 {
6141 int idx = find_internal_func(name);
6142
6143 if (idx < 0)
6144 {
6145 emsg_funcname(e_unknown_function_str, name);
6146 r = FAIL;
6147 }
6148 else if (check_internal_func(idx, argcount) == -1)
6149 r = FAIL;
6150 }
6151 else
6152 {
6153 ufunc_T *ufunc = find_func(name, FALSE);
6154
6155 // we tolerate an unknown function here, it might be defined later
6156 if (ufunc != NULL)
6157 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006158 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006159 if (error != FCERR_UNKNOWN)
6160 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006161 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006162 r = FAIL;
6163 }
6164 }
6165 }
6166 }
6167
Bram Moolenaar86d87252022-09-05 21:21:25 +01006168 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006169 {
6170 while (--argcount >= 0)
6171 clear_tv(&argvars[argcount]);
6172 return FAIL;
6173 }
6174 return add_defer(name, argcount, argvars);
6175}
6176
6177/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006178 * Return TRUE if currently inside a function call.
6179 * Give an error message and return FALSE when not.
6180 */
6181 int
6182can_add_defer(void)
6183{
6184 if (!in_def_function() && get_current_funccal() == NULL)
6185 {
6186 semsg(_(e_str_not_inside_function), "defer");
6187 return FALSE;
6188 }
6189 return TRUE;
6190}
6191
6192/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006193 * Add a deferred call for "name" with arguments "argvars[argcount]".
6194 * Consumes "argvars[]".
6195 * Caller must check that in_def_function() returns TRUE or current_funccal is
6196 * not NULL.
6197 * Returns OK or FAIL.
6198 */
6199 int
6200add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6201{
6202 char_u *saved_name = vim_strsave(name);
6203 int argcount = argcount_arg;
6204 defer_T *dr;
6205 int ret = FAIL;
6206
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006207 if (saved_name == NULL)
6208 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006209 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006210 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006211 if (add_defer_function(saved_name, argcount, argvars) == OK)
6212 argcount = 0;
6213 }
6214 else
6215 {
6216 if (current_funccal->fc_defer.ga_itemsize == 0)
6217 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6218 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6219 goto theend;
6220 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6221 + current_funccal->fc_defer.ga_len++;
6222 dr->dr_name = saved_name;
6223 dr->dr_argcount = argcount;
6224 while (argcount > 0)
6225 {
6226 --argcount;
6227 dr->dr_argvars[argcount] = argvars[argcount];
6228 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006229 }
6230 ret = OK;
6231
6232theend:
6233 while (--argcount >= 0)
6234 clear_tv(&argvars[argcount]);
6235 return ret;
6236}
6237
6238/*
6239 * Invoked after a functions has finished: invoke ":defer" functions.
6240 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006241 static void
6242handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006243{
6244 int idx;
6245
Bram Moolenaar58779852022-09-06 18:31:14 +01006246 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006247 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006248 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006249
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006250 if (dr->dr_name == NULL)
6251 // already being called, can happen if function does ":qa"
6252 continue;
6253
6254 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006255 CLEAR_FIELD(funcexe);
6256 funcexe.fe_evaluate = TRUE;
6257
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006258 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006259 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006260
6261 char_u *name = dr->dr_name;
6262 dr->dr_name = NULL;
6263
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006264 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006265 // first statement in the function will be executed (because of the
6266 // exception). So save and restore the try/catch/throw exception
6267 // state.
6268 exception_state_T estate;
6269 exception_state_save(&estate);
6270 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006271
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006272 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6273
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006274 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006275
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006276 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006277 vim_free(name);
6278 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006279 clear_tv(&dr->dr_argvars[i]);
6280 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006281 ga_clear(&funccal->fc_defer);
6282}
6283
zeertzjq960cf912023-04-18 21:52:54 +01006284 static void
6285invoke_funccall_defer(funccall_T *fc)
6286{
6287 if (fc->fc_ectx != NULL)
6288 {
6289 // :def function
6290 unwind_def_callstack(fc->fc_ectx);
6291 may_invoke_defer_funcs(fc->fc_ectx);
6292 }
6293 else
6294 {
6295 // legacy function
6296 handle_defer_one(fc);
6297 }
6298}
6299
Bram Moolenaar58779852022-09-06 18:31:14 +01006300/*
6301 * Called when exiting: call all defer functions.
6302 */
6303 void
6304invoke_all_defer(void)
6305{
zeertzjq1be4b812023-04-19 14:21:24 +01006306 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6307 invoke_funccall_defer(fc);
6308
zeertzjq960cf912023-04-18 21:52:54 +01006309 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6310 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6311 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006312}
6313
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006314/*
6315 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006316 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006317 */
6318 void
6319ex_call(exarg_T *eap)
6320{
6321 char_u *arg = eap->arg;
6322 char_u *startarg;
6323 char_u *name;
6324 char_u *tofree;
6325 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006326 int failed = FALSE;
6327 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006328 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006329 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006330 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006331 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006332 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006333 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006334
Bram Moolenaare6b53242020-07-01 17:28:33 +02006335 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006336 if (eap->skip)
6337 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006338 typval_T rettv;
6339
Bram Moolenaare38eab22019-12-05 21:50:01 +01006340 // trans_function_name() doesn't work well when skipping, use eval0()
6341 // instead to skip to any following command, e.g. for:
6342 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006343 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006344 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006345 clear_tv(&rettv);
6346 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006347 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006348 return;
6349 }
6350
zeertzjq5299c092023-04-12 20:48:16 +01006351 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006352 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006353 if (fudi.fd_newkey != NULL)
6354 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006355 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006356 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006357 vim_free(fudi.fd_newkey);
6358 }
6359 if (tofree == NULL)
6360 return;
6361
Bram Moolenaare38eab22019-12-05 21:50:01 +01006362 // Increase refcount on dictionary, it could get deleted when evaluating
6363 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006364 if (fudi.fd_dict != NULL)
6365 ++fudi.fd_dict->dv_refcount;
6366
Bram Moolenaare38eab22019-12-05 21:50:01 +01006367 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6368 // contents. For VAR_PARTIAL get its partial, unless we already have one
6369 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006370 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006371 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006372 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006373 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006374
Bram Moolenaare38eab22019-12-05 21:50:01 +01006375 // Skip white space to allow ":call func ()". Not good, but required for
6376 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006377 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006378 if (*startarg != '(')
6379 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006380 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006381 goto end;
6382 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006383 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006384 {
6385 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6386 goto end;
6387 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006388
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006389 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006390 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006391 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006392 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006393 }
6394 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006395 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006396 funcexe_T funcexe;
6397
Bram Moolenaara80faa82020-04-12 19:37:17 +02006398 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006399 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006400 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006401 funcexe.fe_partial = partial;
6402 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006403 funcexe.fe_firstline = eap->line1;
6404 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006405 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006406 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006407 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006408 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006409
Bram Moolenaar1d341892021-09-18 15:25:52 +02006410 // When inside :try we need to check for following "| catch" or "| endtry".
6411 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006412 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006413 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006414 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006415 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006416 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006417 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006418 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006419 {
6420 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006421 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006422 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006423 }
6424 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006425 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006426 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006427 // Must be after using "arg", it may point into memory cleared here.
6428 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006429
6430end:
6431 dict_unref(fudi.fd_dict);
6432 vim_free(tofree);
6433}
6434
6435/*
6436 * Return from a function. Possibly makes the return pending. Also called
6437 * for a pending return at the ":endtry" or after returning from an extra
6438 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6439 * when called due to a ":return" command. "rettv" may point to a typval_T
6440 * with the return rettv. Returns TRUE when the return can be carried out,
6441 * FALSE when the return gets pending.
6442 */
6443 int
6444do_return(
6445 exarg_T *eap,
6446 int reanimate,
6447 int is_cmd,
6448 void *rettv)
6449{
6450 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006451 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006452
6453 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006454 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006455 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006456
6457 /*
6458 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6459 * not in its finally clause (which then is to be executed next) is found.
6460 * In this case, make the ":return" pending for execution at the ":endtry".
6461 * Otherwise, return normally.
6462 */
6463 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6464 if (idx >= 0)
6465 {
6466 cstack->cs_pending[idx] = CSTP_RETURN;
6467
6468 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006469 // A pending return again gets pending. "rettv" points to an
6470 // allocated variable with the rettv of the original ":return"'s
6471 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006472 cstack->cs_rettv[idx] = rettv;
6473 else
6474 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006475 // When undoing a return in order to make it pending, get the stored
6476 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006477 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006478 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006479
6480 if (rettv != NULL)
6481 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006482 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006483 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6484 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6485 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006486 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006487 }
6488 else
6489 cstack->cs_rettv[idx] = NULL;
6490
6491 if (reanimate)
6492 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006493 // The pending return value could be overwritten by a ":return"
6494 // without argument in a finally clause; reset the default
6495 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006496 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6497 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006498 }
6499 }
6500 report_make_pending(CSTP_RETURN, rettv);
6501 }
6502 else
6503 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006504 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006505
Bram Moolenaare38eab22019-12-05 21:50:01 +01006506 // If the return is carried out now, store the return value. For
6507 // a return immediately after reanimation, the value is already
6508 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006509 if (!reanimate && rettv != NULL)
6510 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006511 clear_tv(current_funccal->fc_rettv);
6512 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006513 if (!is_cmd)
6514 vim_free(rettv);
6515 }
6516 }
6517
6518 return idx < 0;
6519}
6520
6521/*
6522 * Free the variable with a pending return value.
6523 */
6524 void
6525discard_pending_return(void *rettv)
6526{
6527 free_tv((typval_T *)rettv);
6528}
6529
6530/*
6531 * Generate a return command for producing the value of "rettv". The result
6532 * is an allocated string. Used by report_pending() for verbose messages.
6533 */
6534 char_u *
6535get_return_cmd(void *rettv)
6536{
6537 char_u *s = NULL;
6538 char_u *tofree = NULL;
6539 char_u numbuf[NUMBUFLEN];
6540
6541 if (rettv != NULL)
6542 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6543 if (s == NULL)
6544 s = (char_u *)"";
6545
6546 STRCPY(IObuff, ":return ");
6547 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6548 if (STRLEN(s) + 8 >= IOSIZE)
6549 STRCPY(IObuff + IOSIZE - 4, "...");
6550 vim_free(tofree);
6551 return vim_strsave(IObuff);
6552}
6553
6554/*
6555 * Get next function line.
6556 * Called by do_cmdline() to get the next line.
6557 * Returns allocated string, or NULL for end of function.
6558 */
6559 char_u *
6560get_func_line(
6561 int c UNUSED,
6562 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006563 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006564 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006565{
6566 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006567 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006568 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006569 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006570
Bram Moolenaare38eab22019-12-05 21:50:01 +01006571 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006572 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006573 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006574 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006575 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006576 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006577 }
6578#ifdef FEAT_PROFILE
6579 if (do_profiling == PROF_YES)
6580 func_line_end(cookie);
6581#endif
6582
6583 gap = &fp->uf_lines;
6584 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006585 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006586 retval = NULL;
6587 else
6588 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006589 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006590 while (fcp->fc_linenr < gap->ga_len
6591 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6592 ++fcp->fc_linenr;
6593 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006594 retval = NULL;
6595 else
6596 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006597 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6598 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006599#ifdef FEAT_PROFILE
6600 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006601 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006602#endif
6603 }
6604 }
6605
Bram Moolenaare38eab22019-12-05 21:50:01 +01006606 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006607 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006608 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006609 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006610 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006611 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006612 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006613 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006614 }
6615
6616 return retval;
6617}
6618
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006619/*
6620 * Return TRUE if the currently active function should be ended, because a
6621 * return was encountered or an error occurred. Used inside a ":while".
6622 */
6623 int
6624func_has_ended(void *cookie)
6625{
6626 funccall_T *fcp = (funccall_T *)cookie;
6627
Bram Moolenaare38eab22019-12-05 21:50:01 +01006628 // Ignore the "abort" flag if the abortion behavior has been changed due to
6629 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006630 return (((fcp->fc_func->uf_flags & FC_ABORT)
6631 && did_emsg && !aborted_in_try())
6632 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006633}
6634
6635/*
6636 * return TRUE if cookie indicates a function which "abort"s on errors.
6637 */
6638 int
6639func_has_abort(
6640 void *cookie)
6641{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006642 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006643}
6644
6645
6646/*
6647 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6648 * Don't do this when "Func" is already a partial that was bound
6649 * explicitly (pt_auto is FALSE).
6650 * Changes "rettv" in-place.
6651 * Returns the updated "selfdict_in".
6652 */
6653 dict_T *
6654make_partial(dict_T *selfdict_in, typval_T *rettv)
6655{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006656 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006657 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006658 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006659 dict_T *selfdict = selfdict_in;
6660
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006661 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6662 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006663 fp = rettv->vval.v_partial->pt_func;
6664 else
6665 {
6666 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006667 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006668 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006669 if (fname == NULL)
6670 {
6671 // There is no point binding a dict to a NULL function, just create
6672 // a function reference.
6673 rettv->v_type = VAR_FUNC;
6674 rettv->vval.v_string = NULL;
6675 }
6676 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006677 {
6678 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006679 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006680
6681 // Translate "s:func" to the stored function name.
6682 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6683 fp = find_func(fname, FALSE);
6684 vim_free(tofree);
6685 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006686 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006687
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006688 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006689 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006690 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006691
6692 if (pt != NULL)
6693 {
6694 pt->pt_refcount = 1;
6695 pt->pt_dict = selfdict;
6696 pt->pt_auto = TRUE;
6697 selfdict = NULL;
6698 if (rettv->v_type == VAR_FUNC)
6699 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006700 // Just a function: Take over the function name and use
6701 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006702 pt->pt_name = rettv->vval.v_string;
6703 }
6704 else
6705 {
6706 partial_T *ret_pt = rettv->vval.v_partial;
6707 int i;
6708
Bram Moolenaare38eab22019-12-05 21:50:01 +01006709 // Partial: copy the function name, use selfdict and copy
6710 // args. Can't take over name or args, the partial might
6711 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006712 if (ret_pt->pt_name != NULL)
6713 {
6714 pt->pt_name = vim_strsave(ret_pt->pt_name);
6715 func_ref(pt->pt_name);
6716 }
6717 else
6718 {
6719 pt->pt_func = ret_pt->pt_func;
6720 func_ptr_ref(pt->pt_func);
6721 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006722 if (ret_pt->pt_argc > 0)
6723 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006724 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006725 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006726 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006727 pt->pt_argc = 0;
6728 else
6729 {
6730 pt->pt_argc = ret_pt->pt_argc;
6731 for (i = 0; i < pt->pt_argc; i++)
6732 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6733 }
6734 }
6735 partial_unref(ret_pt);
6736 }
6737 rettv->v_type = VAR_PARTIAL;
6738 rettv->vval.v_partial = pt;
6739 }
6740 }
6741 return selfdict;
6742}
6743
6744/*
6745 * Return the name of the executed function.
6746 */
6747 char_u *
6748func_name(void *cookie)
6749{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006750 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006751}
6752
6753/*
6754 * Return the address holding the next breakpoint line for a funccall cookie.
6755 */
6756 linenr_T *
6757func_breakpoint(void *cookie)
6758{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006759 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006760}
6761
6762/*
6763 * Return the address holding the debug tick for a funccall cookie.
6764 */
6765 int *
6766func_dbg_tick(void *cookie)
6767{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006768 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006769}
6770
6771/*
6772 * Return the nesting level for a funccall cookie.
6773 */
6774 int
6775func_level(void *cookie)
6776{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006777 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006778}
6779
6780/*
6781 * Return TRUE when a function was ended by a ":return" command.
6782 */
6783 int
6784current_func_returned(void)
6785{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006786 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006787}
6788
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006789 int
6790free_unref_funccal(int copyID, int testing)
6791{
6792 int did_free = FALSE;
6793 int did_free_funccal = FALSE;
6794 funccall_T *fc, **pfc;
6795
6796 for (pfc = &previous_funccal; *pfc != NULL; )
6797 {
6798 if (can_free_funccal(*pfc, copyID))
6799 {
6800 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006801 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006802 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006803 did_free = TRUE;
6804 did_free_funccal = TRUE;
6805 }
6806 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006807 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006808 }
6809 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006810 // When a funccal was freed some more items might be garbage
6811 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006812 (void)garbage_collect(testing);
6813
6814 return did_free;
6815}
6816
6817/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006818 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006819 */
6820 static funccall_T *
6821get_funccal(void)
6822{
6823 int i;
6824 funccall_T *funccal;
6825 funccall_T *temp_funccal;
6826
6827 funccal = current_funccal;
6828 if (debug_backtrace_level > 0)
6829 {
6830 for (i = 0; i < debug_backtrace_level; i++)
6831 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006832 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006833 if (temp_funccal)
6834 funccal = temp_funccal;
6835 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006836 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006837 debug_backtrace_level = i;
6838 }
6839 }
6840 return funccal;
6841}
6842
6843/*
6844 * Return the hashtable used for local variables in the current funccal.
6845 * Return NULL if there is no current funccal.
6846 */
6847 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006848get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006849{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006850 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006851 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006852 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006853}
6854
6855/*
6856 * Return the l: scope variable.
6857 * Return NULL if there is no current funccal.
6858 */
6859 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006860get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006861{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006862 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006863 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006864 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006865}
6866
6867/*
6868 * Return the hashtable used for argument in the current funccal.
6869 * Return NULL if there is no current funccal.
6870 */
6871 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006872get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006873{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006874 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006875 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006876 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006877}
6878
6879/*
6880 * Return the a: scope variable.
6881 * Return NULL if there is no current funccal.
6882 */
6883 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006884get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006885{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006886 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006887 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006888 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006889}
6890
6891/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006892 * List function variables, if there is a function.
6893 */
6894 void
6895list_func_vars(int *first)
6896{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006897 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6898 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006899 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006900}
6901
6902/*
6903 * If "ht" is the hashtable for local variables in the current funccal, return
6904 * the dict that contains it.
6905 * Otherwise return NULL.
6906 */
6907 dict_T *
6908get_current_funccal_dict(hashtab_T *ht)
6909{
6910 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006911 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6912 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006913 return NULL;
6914}
6915
6916/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006917 * Search hashitem in parent scope.
6918 */
6919 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006920find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006921{
6922 funccall_T *old_current_funccal = current_funccal;
6923 hashtab_T *ht;
6924 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006925 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006926
Bram Moolenaarca16c602022-09-06 18:57:08 +01006927 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006928 return NULL;
6929
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006930 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006931 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006932 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006933 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006934 ht = find_var_ht(name, &varname);
6935 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006936 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006937 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006938 if (!HASHITEM_EMPTY(hi))
6939 {
6940 *pht = ht;
6941 break;
6942 }
6943 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006944 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006945 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006946 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006947 }
6948 current_funccal = old_current_funccal;
6949
6950 return hi;
6951}
6952
6953/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006954 * Search variable in parent scope.
6955 */
6956 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006957find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006958{
6959 dictitem_T *v = NULL;
6960 funccall_T *old_current_funccal = current_funccal;
6961 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006962 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006963
Bram Moolenaarca16c602022-09-06 18:57:08 +01006964 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006965 return NULL;
6966
Bram Moolenaare38eab22019-12-05 21:50:01 +01006967 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01006968 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006969 while (current_funccal)
6970 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006971 ht = find_var_ht(name, &varname);
6972 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006973 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006974 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006975 if (v != NULL)
6976 break;
6977 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006978 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006979 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006980 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006981 }
6982 current_funccal = old_current_funccal;
6983
6984 return v;
6985}
6986
6987/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006988 * Set "copyID + 1" in previous_funccal and callers.
6989 */
6990 int
6991set_ref_in_previous_funccal(int copyID)
6992{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006993 funccall_T *fc;
6994
Bram Moolenaarca16c602022-09-06 18:57:08 +01006995 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006996 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006997 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006998 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
6999 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
7000 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007001 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007002 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007003 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007004}
7005
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007006 static int
7007set_ref_in_funccal(funccall_T *fc, int copyID)
7008{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007009 if (fc->fc_copyID != copyID)
7010 {
7011 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007012 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7013 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7014 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7015 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007016 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007017 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007018 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007019}
7020
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007021/*
7022 * Set "copyID" in all local vars and arguments in the call stack.
7023 */
7024 int
7025set_ref_in_call_stack(int copyID)
7026{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007027 funccall_T *fc;
7028 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007029
Bram Moolenaarca16c602022-09-06 18:57:08 +01007030 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007031 if (set_ref_in_funccal(fc, copyID))
7032 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007033
7034 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007035 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007036 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007037 if (set_ref_in_funccal(fc, copyID))
7038 return TRUE;
7039 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007040}
7041
7042/*
7043 * Set "copyID" in all functions available by name.
7044 */
7045 int
7046set_ref_in_functions(int copyID)
7047{
7048 int todo;
7049 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007050 ufunc_T *fp;
7051
7052 todo = (int)func_hashtab.ht_used;
7053 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007054 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007055 if (!HASHITEM_EMPTY(hi))
7056 {
7057 --todo;
7058 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007059 if (!func_name_refcount(fp->uf_name)
7060 && set_ref_in_func(NULL, fp, copyID))
7061 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007062 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007063 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007064 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007065}
7066
7067/*
7068 * Set "copyID" in all function arguments.
7069 */
7070 int
7071set_ref_in_func_args(int copyID)
7072{
7073 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007074
7075 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007076 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7077 copyID, NULL, NULL))
7078 return TRUE;
7079 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007080}
7081
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007082/*
7083 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007084 * Returns TRUE if setting references failed somehow.
7085 */
7086 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007087set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007088{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007089 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007090 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007091 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007092 char_u fname_buf[FLEN_FIXED + 1];
7093 char_u *tofree = NULL;
7094 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007095 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007096
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007097 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007098 return FALSE;
7099
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007100 if (fp_in == NULL)
7101 {
7102 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007103 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007104 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007105 if (fp != NULL)
7106 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007107 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007108 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007109 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007110
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007111 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007112 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007113}
7114
Bram Moolenaare38eab22019-12-05 21:50:01 +01007115#endif // FEAT_EVAL