blob: 33e73a9a5248bff61f764a06616c8a9f13210ce6 [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;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200246
247 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000248 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000250 ga_init2(argtypes, sizeof(char_u *), 3);
h-eastb895b0f2023-09-24 15:46:31 +0200251 if (arg_objm != NULL)
252 ga_init2(arg_objm, sizeof(int8_T), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200253 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000254 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200255
256 if (varargs != NULL)
257 *varargs = FALSE;
258
259 /*
260 * Isolate the arguments: "arg1, arg2, ...)"
261 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100262 arg = skipwhite(*argp);
263 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200264 while (*p != endchar)
265 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200266 while (eap != NULL && eap->getline != NULL
267 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200268 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200269 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000270 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000271 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000272
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200273 if (theline == NULL)
274 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200275 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200276 p = skipwhite(theline);
277 }
278
279 if (mustend && *p != endchar)
280 {
281 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000282 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100283 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200284 }
285 if (*p == endchar)
286 break;
287
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200288 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
289 {
290 if (varargs != NULL)
291 *varargs = TRUE;
292 p += 3;
293 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100294
295 if (argtypes != NULL)
296 {
297 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200298 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100300 if (!skip)
301 emsg(_(e_missing_name_after_dots));
302 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303 }
304
305 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100306 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200307 arg_objm, evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308 if (p == arg)
309 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100310 if (*skipwhite(p) == '=')
311 {
312 emsg(_(e_cannot_use_default_for_variable_arguments));
313 break;
314 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100315 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200316 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000317 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000318 {
319 // this.memberName
320 p += 5;
321 arg = p;
322 while (ASCII_ISALNUM(*p) || *p == '_')
323 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000324 char_u *argend = p;
325
h-eastdb385522023-09-28 22:18:19 +0200326 // object variable this. can be used only in a constructor
327 if (STRNCMP(eap->arg, "new", 3) != 0)
328 {
329 c = *argend;
330 *argend = NUL;
331 semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
332 *argend = c;
333 break;
334 }
335
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000336 if (*skipwhite(p) == '=')
337 {
338 char_u *defval = skipwhite(skipwhite(p) + 1);
339 if (STRNCMP(defval, "v:none", 6) != 0)
340 {
341 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
342 goto err_ret;
343 }
344 any_default = TRUE;
345 p = defval + 6;
346
347 if (ga_grow(default_args, 1) == FAIL)
348 goto err_ret;
349
350 char_u *expr = vim_strsave((char_u *)"v:none");
351 if (expr == NULL)
352 goto err_ret;
353 ((char_u **)(default_args->ga_data))
354 [default_args->ga_len] = expr;
355 default_args->ga_len++;
356 }
357 else if (any_default)
358 {
359 emsg(_(e_non_default_argument_follows_default_argument));
360 goto err_ret;
361 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000362
363 // TODO: check the argument is indeed a member
364 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
365 return FAIL;
366 if (newargs != NULL)
367 {
368 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000369 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000370 newargs->ga_len++;
371
h-eastb895b0f2023-09-24 15:46:31 +0200372 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
373 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000374 {
375 // TODO: use the actual type
376 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
377 vim_strsave((char_u *)"any");
h-eastb895b0f2023-09-24 15:46:31 +0200378 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000379
380 // Add a line to the function body for the assignment.
381 if (ga_grow(newlines, 1) == OK)
382 {
383 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000384 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
385 if (any_default)
386 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000387 char_u *assignment = alloc(len);
388 if (assignment != NULL)
389 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000390 c = *argend;
391 *argend = NUL;
392 if (any_default)
393 vim_snprintf((char *)assignment, len,
394 "ifargisset %d this.%s = %s",
395 default_args->ga_len - 1, arg, arg);
396 else
397 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000398 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000399 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000400 ((char_u **)(newlines->ga_data))[
401 newlines->ga_len++] = assignment;
402 }
403 }
404 }
405 }
406 if (*p == ',')
407 ++p;
408 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200409 else
410 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200411 char_u *np;
412
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200413 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100414 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200415 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100416 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200417 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200418
Bram Moolenaar015cf102021-06-26 21:52:02 +0200419 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200420 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200421 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200422 if (*np == '=' && np[1] != '=' && np[1] != '~'
423 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200424 {
425 typval_T rettv;
426
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100427 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200428 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100429 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000430 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200431 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200432 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200433 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200434 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200435 if (ga_grow(default_args, 1) == FAIL)
436 goto err_ret;
437
438 // trim trailing whitespace
439 while (p > expr && VIM_ISWHITE(p[-1]))
440 p--;
441 c = *p;
442 *p = NUL;
443 expr = vim_strsave(expr);
444 if (expr == NULL)
445 {
446 *p = c;
447 goto err_ret;
448 }
449 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200450 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200451 default_args->ga_len++;
452 *p = c;
453 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200454 }
455 else
456 mustend = TRUE;
457 }
458 else if (any_default)
459 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000460 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200461 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200462 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200463
464 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
465 {
466 // Be tolerant when skipping
467 if (!skip)
468 {
469 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
470 goto err_ret;
471 }
472 p = skipwhite(p);
473 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200474 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200475 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200476 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200477 // Don't give this error when skipping, it makes the "->" not
478 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100479 // Allow missing space after comma in legacy functions.
480 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200481 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
482 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100483 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200484 goto err_ret;
485 }
486 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200487 else
488 mustend = TRUE;
489 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200490 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200491 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200492 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200493
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200494 if (*p != endchar)
495 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100496 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200497
498 *argp = p;
499 return OK;
500
501err_ret:
502 if (newargs != NULL)
503 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200504 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200505 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200506 return FAIL;
507}
508
509/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100510 * Parse the argument types, filling "fp->uf_arg_types".
511 * Return OK or FAIL.
512 */
513 static int
h-eastb895b0f2023-09-24 15:46:31 +0200514parse_argument_types(
515 ufunc_T *fp,
516 garray_T *argtypes,
517 int varargs,
518 garray_T *arg_objm,
519 ocmember_T *obj_members,
520 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100521{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200522 int len = 0;
523
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100524 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
525 if (argtypes->ga_len > 0)
526 {
527 // When "varargs" is set the last name/type goes into uf_va_name
528 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200529 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100530
531 if (len > 0)
532 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
533 if (fp->uf_arg_types != NULL)
534 {
535 int i;
536 type_T *type;
537
538 for (i = 0; i < len; ++ i)
539 {
540 char_u *p = ((char_u **)argtypes->ga_data)[i];
541
542 if (p == NULL)
543 // will get the type from the default value
544 type = &t_unknown;
545 else
h-eastb895b0f2023-09-24 15:46:31 +0200546 {
547 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
548 {
549 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
550
551 type = &t_any;
552 for (int om = 0; om < obj_member_count; ++om)
553 {
554 if (STRCMP(aname, obj_members[om].ocm_name) == 0)
555 {
556 type = obj_members[om].ocm_type;
557 break;
558 }
559 }
560 }
561 else
562 type = parse_type(&p, &fp->uf_type_list, TRUE);
563 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100564 if (type == NULL)
565 return FAIL;
566 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000567 if (i < fp->uf_args.ga_len
568 && (type->tt_type == VAR_FUNC
569 || type->tt_type == VAR_PARTIAL)
570 && var_wrong_func_name(
571 ((char_u **)fp->uf_args.ga_data)[i], TRUE))
572 return FAIL;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100573 }
574 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100575 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200576
577 if (varargs)
578 {
579 char_u *p;
580
581 // Move the last argument "...name: type" to uf_va_name and
582 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200583 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000584 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
585 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200586 p = ((char_u **)argtypes->ga_data)[len];
587 if (p == NULL)
588 // TODO: get type from default value
589 fp->uf_va_type = &t_list_any;
590 else
591 {
592 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
593 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
594 {
595 semsg(_(e_variable_arguments_type_must_be_list_str),
596 ((char_u **)argtypes->ga_data)[len]);
597 return FAIL;
598 }
599 }
600 if (fp->uf_va_type == NULL)
601 return FAIL;
602 }
603
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100604 return OK;
605}
606
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100607 static int
608parse_return_type(ufunc_T *fp, char_u *ret_type)
609{
610 if (ret_type == NULL)
611 fp->uf_ret_type = &t_void;
612 else
613 {
614 char_u *p = ret_type;
615
616 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
617 if (fp->uf_ret_type == NULL)
618 {
619 fp->uf_ret_type = &t_void;
620 return FAIL;
621 }
622 }
623 return OK;
624}
625
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100626/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200627 * Register function "fp" as using "current_funccal" as its scope.
628 */
629 static int
630register_closure(ufunc_T *fp)
631{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200632 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100633 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200634 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200635 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200636 fp->uf_scoped = current_funccal;
637 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200638
Bram Moolenaarca16c602022-09-06 18:57:08 +0100639 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200640 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100641 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
642 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200643 return OK;
644}
645
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100646 static void
647set_ufunc_name(ufunc_T *fp, char_u *name)
648{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100649 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
650 // actually extends beyond the struct.
651 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100652
653 if (name[0] == K_SPECIAL)
654 {
655 fp->uf_name_exp = alloc(STRLEN(name) + 3);
656 if (fp->uf_name_exp != NULL)
657 {
658 STRCPY(fp->uf_name_exp, "<SNR>");
659 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
660 }
661 }
662}
663
Bram Moolenaar58016442016-07-31 18:30:22 +0200664/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100665 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
666 * return "buf" filled with a readable function name.
667 * Otherwise just return "name", thus the return value can always be used.
668 * "name" and "buf" may be equal.
669 */
670 char_u *
671make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
672{
673 size_t len;
674
675 if (name[0] != K_SPECIAL)
676 return name;
677 len = STRLEN(name);
678 if (len + 3 > bufsize)
679 return name;
680
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100681 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100682 mch_memmove(buf, "<SNR>", 5);
683 return buf;
684}
685
686/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200687 * Get a name for a lambda. Returned in static memory.
688 */
689 char_u *
690get_lambda_name(void)
691{
692 static char_u name[30];
693 static int lambda_no = 0;
694
695 sprintf((char*)name, "<lambda>%d", ++lambda_no);
696 return name;
697}
698
Bram Moolenaare01e5212023-01-08 20:31:18 +0000699/*
700 * Allocate a "ufunc_T" for a function called "name".
701 * Makes sure the size is right.
702 */
703 static ufunc_T *
704alloc_ufunc(char_u *name)
705{
706 // When the name is short we need to make sure we allocate enough bytes for
707 // the whole struct, including any padding.
708 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
709 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
710}
711
Bram Moolenaar801ab062020-06-25 19:27:56 +0200712#if defined(FEAT_LUA) || defined(PROTO)
713/*
714 * Registers a native C callback which can be called from Vim script.
715 * Returns the name of the Vim script function.
716 */
717 char_u *
718register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
719{
720 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200721 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200722
Bram Moolenaare01e5212023-01-08 20:31:18 +0000723 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200724 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200725 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200726
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200727 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200728 fp->uf_refcount = 1;
729 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000730 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200731 fp->uf_calls = 0;
732 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200733 fp->uf_cb = cb;
734 fp->uf_cb_free = cb_free;
735 fp->uf_cb_state = state;
736
737 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000738 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200739
740 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200741}
742#endif
743
Bram Moolenaar04b12692020-05-04 23:24:44 +0200744/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100745 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100746 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100747 * If "white_error" is not NULL check for correct use of white space and set
748 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100749 * Return NULL if no valid arrow found.
750 */
751 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100752skip_arrow(
753 char_u *start,
754 int equal_arrow,
755 char_u **ret_type,
756 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100757{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100758 char_u *s = start;
759 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100760
761 if (equal_arrow)
762 {
763 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100764 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100765 if (white_error != NULL && !VIM_ISWHITE(s[1]))
766 {
767 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100768 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100769 return NULL;
770 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100771 s = skipwhite(s + 1);
772 *ret_type = s;
773 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100774 if (s == *ret_type)
775 {
776 emsg(_(e_missing_return_type));
777 return NULL;
778 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100779 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100780 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100781 s = skipwhite(s);
782 if (*s != '=')
783 return NULL;
784 ++s;
785 }
786 if (*s != '>')
787 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100788 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
789 || !IS_WHITE_OR_NUL(s[1])))
790 {
791 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100792 semsg(_(e_white_space_required_before_and_after_str_at_str),
793 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100794 return NULL;
795 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100796 return skipwhite(s + 1);
797}
798
799/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100800 * Check if "*cmd" points to a function command and if so advance "*cmd" and
801 * return TRUE.
802 * Otherwise return FALSE;
803 * Do not consider "function(" to be a command.
804 */
805 static int
806is_function_cmd(char_u **cmd)
807{
808 char_u *p = *cmd;
809
810 if (checkforcmd(&p, "function", 2))
811 {
812 if (*p == '(')
813 return FALSE;
814 *cmd = p;
815 return TRUE;
816 }
817 return FALSE;
818}
819
820/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200821 * Called when defining a function: The context may be needed for script
822 * variables declared in a block that is visible now but not when the function
823 * is compiled or called later.
824 */
825 static void
826function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
827{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000828 if (cstack == NULL || cstack->cs_idx < 0)
829 return;
830
831 int count = cstack->cs_idx + 1;
832 int i;
833
834 fp->uf_block_ids = ALLOC_MULT(int, count);
835 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200836 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000837 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
838 sizeof(int) * count);
839 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200840 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000841
842 // Set flag in each block to indicate a function was defined. This
843 // is used to keep the variable when leaving the block, see
844 // hide_script_var().
845 for (i = 0; i <= cstack->cs_idx; ++i)
846 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200847}
848
849/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100850 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200851 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100852 * "newlines" must already have been initialized.
853 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
854 */
855 static int
856get_function_body(
857 exarg_T *eap,
858 garray_T *newlines,
859 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000860 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100861{
862 linenr_T sourcing_lnum_top = SOURCING_LNUM;
863 linenr_T sourcing_lnum_off;
864 int saved_wait_return = need_wait_return;
865 char_u *line_arg = line_arg_in;
866 int vim9_function = eap->cmdidx == CMD_def
867 || eap->cmdidx == CMD_block;
868#define MAX_FUNC_NESTING 50
869 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200870 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100871 int nesting = 0;
872 getline_opt_T getline_options;
873 int indent = 2;
874 char_u *skip_until = NULL;
875 int ret = FAIL;
876 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200877 int heredoc_concat_len = 0;
878 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100879 char_u *heredoc_trimmed = NULL;
880
Bram Moolenaar20677332021-06-06 17:02:53 +0200881 ga_init2(&heredoc_ga, 1, 500);
882
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100883 // Detect having skipped over comment lines to find the return
884 // type. Add NULL lines to keep the line count correct.
885 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
886 if (SOURCING_LNUM < sourcing_lnum_off)
887 {
888 sourcing_lnum_off -= SOURCING_LNUM;
889 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
890 goto theend;
891 while (sourcing_lnum_off-- > 0)
892 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
893 }
894
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200895 nesting_def[0] = vim9_function;
896 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100897 getline_options = vim9_function
898 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
899 for (;;)
900 {
901 char_u *theline;
902 char_u *p;
903 char_u *arg;
904
905 if (KeyTyped)
906 {
907 msg_scroll = TRUE;
908 saved_wait_return = FALSE;
909 }
910 need_wait_return = FALSE;
911
912 if (line_arg != NULL)
913 {
914 // Use eap->arg, split up in parts by line breaks.
915 theline = line_arg;
916 p = vim_strchr(theline, '\n');
917 if (p == NULL)
918 line_arg += STRLEN(line_arg);
919 else
920 {
921 *p = NUL;
922 line_arg = p + 1;
923 }
924 }
925 else
926 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000927 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100928 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100929 }
930 if (KeyTyped)
931 lines_left = Rows - 1;
932 if (theline == NULL)
933 {
934 // Use the start of the function for the line number.
935 SOURCING_LNUM = sourcing_lnum_top;
936 if (skip_until != NULL)
937 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200938 else if (nesting_inline[nesting])
939 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100940 else if (eap->cmdidx == CMD_def)
941 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100942 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000943 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100944 goto theend;
945 }
946
947 // Detect line continuation: SOURCING_LNUM increased more than one.
948 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
949 if (SOURCING_LNUM < sourcing_lnum_off)
950 sourcing_lnum_off -= SOURCING_LNUM;
951 else
952 sourcing_lnum_off = 0;
953
954 if (skip_until != NULL)
955 {
956 // Don't check for ":endfunc"/":enddef" between
957 // * ":append" and "."
958 // * ":python <<EOF" and "EOF"
959 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
960 if (heredoc_trimmed == NULL
961 || (is_heredoc && skipwhite(theline) == theline)
962 || STRNCMP(theline, heredoc_trimmed,
963 STRLEN(heredoc_trimmed)) == 0)
964 {
965 if (heredoc_trimmed == NULL)
966 p = theline;
967 else if (is_heredoc)
968 p = skipwhite(theline) == theline
969 ? theline : theline + STRLEN(heredoc_trimmed);
970 else
971 p = theline + STRLEN(heredoc_trimmed);
972 if (STRCMP(p, skip_until) == 0)
973 {
974 VIM_CLEAR(skip_until);
975 VIM_CLEAR(heredoc_trimmed);
976 getline_options = vim9_function
977 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
978 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200979
980 if (heredoc_concat_len > 0)
981 {
982 // Replace the starting line with all the concatenated
983 // lines.
984 ga_concat(&heredoc_ga, theline);
985 vim_free(((char_u **)(newlines->ga_data))[
986 heredoc_concat_len - 1]);
987 ((char_u **)(newlines->ga_data))[
988 heredoc_concat_len - 1] = heredoc_ga.ga_data;
989 ga_init(&heredoc_ga);
990 heredoc_concat_len = 0;
991 theline += STRLEN(theline); // skip the "EOF"
992 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100993 }
994 }
995 }
996 else
997 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200998 int c;
999 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001000 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001001
1002 // skip ':' and blanks
1003 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1004 ;
1005
1006 // Check for "endfunction", "enddef" or "}".
1007 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001008 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001009 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001010 ? *p == '}'
1011 : (checkforcmd(&p, nesting_def[nesting]
1012 ? "enddef" : "endfunction", 4)
1013 && *p != ':'))
1014 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001015 if (!nesting_inline[nesting] && nesting_def[nesting]
1016 && p < cmd + 6)
1017 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001018 if (nesting-- == 0)
1019 {
1020 char_u *nextcmd = NULL;
1021
1022 if (*p == '|' || *p == '}')
1023 nextcmd = p + 1;
1024 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1025 nextcmd = line_arg;
1026 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001027 && (vim9_function || p_verbose > 0))
1028 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001029 SOURCING_LNUM = sourcing_lnum_top
1030 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001031 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001032 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001033 else
1034 give_warning2((char_u *)
1035 _("W22: Text found after :endfunction: %s"),
1036 p, TRUE);
1037 }
1038 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001039 {
1040 // Another command follows. If the line came from "eap"
1041 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001042 // change "eap->cmdlinep" to point to the last fetched
1043 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001044 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001045 if (lines_to_free->ga_len > 0
1046 && *eap->cmdlinep !=
1047 ((char_u **)lines_to_free->ga_data)
1048 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001049 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001050 // *cmdlinep will be freed later, thus remove the
1051 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001052 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001053 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1054 [lines_to_free->ga_len - 1];
1055 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001056 }
1057 }
1058 break;
1059 }
1060 }
1061
1062 // Check for mismatched "endfunc" or "enddef".
1063 // We don't check for "def" inside "func" thus we also can't check
1064 // for "enddef".
1065 // We continue to find the end of the function, although we might
1066 // not find it.
1067 else if (nesting_def[nesting])
1068 {
1069 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1070 emsg(_(e_mismatched_endfunction));
1071 }
1072 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1073 emsg(_(e_mismatched_enddef));
1074
1075 // Increase indent inside "if", "while", "for" and "try", decrease
1076 // at "end".
1077 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1078 indent -= 2;
1079 else if (STRNCMP(p, "if", 2) == 0
1080 || STRNCMP(p, "wh", 2) == 0
1081 || STRNCMP(p, "for", 3) == 0
1082 || STRNCMP(p, "try", 3) == 0)
1083 indent += 2;
1084
1085 // Check for defining a function inside this function.
1086 // Only recognize "def" inside "def", not inside "function",
1087 // For backwards compatibility, see Test_function_python().
1088 c = *p;
1089 if (is_function_cmd(&p)
1090 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1091 {
1092 if (*p == '!')
1093 p = skipwhite(p + 1);
1094 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001095 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001096 if (*skipwhite(p) == '(')
1097 {
1098 if (nesting == MAX_FUNC_NESTING - 1)
1099 emsg(_(e_function_nesting_too_deep));
1100 else
1101 {
1102 ++nesting;
1103 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001104 nesting_inline[nesting] = FALSE;
1105 indent += 2;
1106 }
1107 }
1108 }
1109
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001110 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001111 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001112 // Not a comment line: check for nested inline function.
1113 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001114 while (end > p && VIM_ISWHITE(*end))
1115 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001116 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001117 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001118 int is_block;
1119
1120 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001121 --end;
1122 while (end > p && VIM_ISWHITE(*end))
1123 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001124 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1125 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001126 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001127 char_u *s = p;
1128
1129 // check for line starting with "au" for :autocmd or
1130 // "com" for :command, these can use a {} block
1131 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1132 || checkforcmd_noparen(&s, "command", 3);
1133 }
1134
1135 if (is_block)
1136 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001137 if (nesting == MAX_FUNC_NESTING - 1)
1138 emsg(_(e_function_nesting_too_deep));
1139 else
1140 {
1141 ++nesting;
1142 nesting_def[nesting] = TRUE;
1143 nesting_inline[nesting] = TRUE;
1144 indent += 2;
1145 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001146 }
1147 }
1148 }
1149
1150 // Check for ":append", ":change", ":insert". Not for :def.
1151 p = skip_range(p, FALSE, NULL);
1152 if (!vim9_function
1153 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1154 || (p[0] == 'c'
1155 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1156 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1157 && (STRNCMP(&p[3], "nge", 3) != 0
1158 || !ASCII_ISALPHA(p[6])))))))
1159 || (p[0] == 'i'
1160 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1161 && (!ASCII_ISALPHA(p[2])
1162 || (p[2] == 's'
1163 && (!ASCII_ISALPHA(p[3])
1164 || p[3] == 'e'))))))))
1165 skip_until = vim_strsave((char_u *)".");
1166
1167 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1168 arg = skipwhite(skiptowhite(p));
1169 if (arg[0] == '<' && arg[1] =='<'
1170 && ((p[0] == 'p' && p[1] == 'y'
1171 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1172 || ((p[2] == '3' || p[2] == 'x')
1173 && !ASCII_ISALPHA(p[3]))))
1174 || (p[0] == 'p' && p[1] == 'e'
1175 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1176 || (p[0] == 't' && p[1] == 'c'
1177 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1178 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1179 && !ASCII_ISALPHA(p[3]))
1180 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1181 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1182 || (p[0] == 'm' && p[1] == 'z'
1183 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1184 ))
1185 {
1186 // ":python <<" continues until a dot, like ":append"
1187 p = skipwhite(arg + 2);
1188 if (STRNCMP(p, "trim", 4) == 0)
1189 {
1190 // Ignore leading white space.
1191 p = skipwhite(p + 4);
1192 heredoc_trimmed = vim_strnsave(theline,
1193 skipwhite(theline) - theline);
1194 }
1195 if (*p == NUL)
1196 skip_until = vim_strsave((char_u *)".");
1197 else
1198 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1199 getline_options = GETLINE_NONE;
1200 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001201 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001202 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001203 }
1204
Bram Moolenaard881d152022-05-13 13:50:36 +01001205 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001206 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001207 // Check for ":cmd v =<< [trim] EOF"
1208 // and ":cmd [a, b] =<< [trim] EOF"
1209 // and "lines =<< [trim] EOF" for Vim9
1210 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001211 arg = p;
1212 if (checkforcmd(&arg, "let", 2)
1213 || checkforcmd(&arg, "var", 3)
1214 || checkforcmd(&arg, "final", 5)
1215 || checkforcmd(&arg, "const", 5)
1216 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001217 {
zeertzjqa93d9cd2023-05-02 16:25:47 +01001218 while (vim_strchr((char_u *)"$@&", *arg) != NULL)
1219 ++arg;
1220 arg = skipwhite(find_name_end(arg, NULL, NULL,
1221 FNE_INCL_BR | FNE_ALLOW_CURLY));
1222 if (vim9_function && *arg == ':')
1223 arg = skipwhite(skip_type(skipwhite(arg + 1), FALSE));
1224 if (arg[0] == '=' && arg[1] == '<' && arg[2] =='<')
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001225 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001226 p = skipwhite(arg + 3);
1227 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001228 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001229 if (STRNCMP(p, "trim", 4) == 0)
1230 {
1231 // Ignore leading white space.
1232 p = skipwhite(p + 4);
1233 heredoc_trimmed = vim_strnsave(theline,
1234 skipwhite(theline) - theline);
1235 continue;
1236 }
1237 if (STRNCMP(p, "eval", 4) == 0)
1238 {
1239 // Ignore leading white space.
1240 p = skipwhite(p + 4);
1241 continue;
1242 }
1243 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001244 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001245 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1246 getline_options = GETLINE_NONE;
1247 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001248 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001249 }
1250 }
1251 }
1252
1253 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001254 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001255 goto theend;
1256
Bram Moolenaar20677332021-06-06 17:02:53 +02001257 if (heredoc_concat_len > 0)
1258 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001259 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001260 // to be used for the instruction later.
1261 ga_concat(&heredoc_ga, theline);
1262 ga_concat(&heredoc_ga, (char_u *)"\n");
1263 p = vim_strsave((char_u *)"");
1264 }
1265 else
1266 {
1267 // Copy the line to newly allocated memory. get_one_sourceline()
1268 // allocates 250 bytes per line, this saves 80% on average. The
1269 // cost is an extra alloc/free.
1270 p = vim_strsave(theline);
1271 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001272 if (p == NULL)
1273 goto theend;
1274 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1275
1276 // Add NULL lines for continuation lines, so that the line count is
1277 // equal to the index in the growarray.
1278 while (sourcing_lnum_off-- > 0)
1279 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1280
1281 // Check for end of eap->arg.
1282 if (line_arg != NULL && *line_arg == NUL)
1283 line_arg = NULL;
1284 }
1285
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001286 // Return OK when no error was detected.
1287 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001288 ret = OK;
1289
1290theend:
1291 vim_free(skip_until);
1292 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001293 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001294 need_wait_return |= saved_wait_return;
1295 return ret;
1296}
1297
1298/*
1299 * Handle the body of a lambda. *arg points to the "{", process statements
1300 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001301 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001302 * When successful "rettv" is set to a funcref.
1303 */
1304 static int
1305lambda_function_body(
1306 char_u **arg,
1307 typval_T *rettv,
1308 evalarg_T *evalarg,
1309 garray_T *newargs,
1310 garray_T *argtypes,
1311 int varargs,
1312 garray_T *default_args,
1313 char_u *ret_type)
1314{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001315 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001316 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001317 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001318 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001319 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001320 exarg_T eap;
1321 garray_T newlines;
1322 char_u *cmdline = NULL;
1323 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001324 partial_T *pt;
1325 char_u *name;
1326 int lnum_save = -1;
1327 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1328
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001329 *arg = skipwhite(*arg + 1);
1330 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001331 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001332 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001333 return FAIL;
1334 }
1335
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001336 CLEAR_FIELD(eap);
1337 eap.cmdidx = CMD_block;
1338 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001339 eap.cmdlinep = &cmdline;
1340 eap.skip = !evaluate;
1341 if (evalarg->eval_cctx != NULL)
1342 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1343 else
1344 {
1345 eap.getline = evalarg->eval_getline;
1346 eap.cookie = evalarg->eval_cookie;
1347 }
1348
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001349 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001350 if (get_function_body(&eap, &newlines, NULL,
1351 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001352 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001353
1354 // When inside a lambda must add the function lines to evalarg.eval_ga.
1355 evalarg->eval_break_count += newlines.ga_len;
1356 if (gap->ga_itemsize > 0)
1357 {
1358 int idx;
1359 char_u *last;
1360 size_t plen;
1361 char_u *pnl;
1362
1363 for (idx = 0; idx < newlines.ga_len; ++idx)
1364 {
1365 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1366
Bram Moolenaarecb66452021-05-18 15:09:18 +02001367 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001368 goto erret;
1369
1370 // Going to concatenate the lines after parsing. For an empty or
1371 // comment line use an empty string.
1372 // Insert NL characters at the start of each line, the string will
1373 // be split again later in .get_lambda_tv().
1374 if (*p == NUL || vim9_comment_start(p))
1375 p = (char_u *)"";
1376 plen = STRLEN(p);
1377 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1378 if (pnl != NULL)
1379 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001380 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1381 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001382 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001383 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001384 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001385 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001386 // more is following after the "}", which was skipped
1387 last = cmdline;
1388 else
1389 // nothing is following the "}"
1390 last = (char_u *)"}";
1391 plen = STRLEN(last);
1392 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1393 if (pnl != NULL)
1394 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001395 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1396 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001397 }
1398
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001399 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001400 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001401 garray_T *tfgap = &evalarg->eval_tofree_ga;
1402
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001403 // Something comes after the "}".
1404 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001405
1406 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001407 if (ga_grow(tfgap, 1) == OK)
1408 {
1409 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1410 evalarg->eval_using_cmdline = TRUE;
1411 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001412 }
1413 else
1414 *arg = (char_u *)"";
1415
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001416 if (!evaluate)
1417 {
1418 ret = OK;
1419 goto erret;
1420 }
1421
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001422 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001423 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001424 if (ufunc == NULL)
1425 goto erret;
1426 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001427 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001428 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001429 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001430 ufunc->uf_refcount = 1;
1431 ufunc->uf_args = *newargs;
1432 newargs->ga_data = NULL;
1433 ufunc->uf_def_args = *default_args;
1434 default_args->ga_data = NULL;
1435 ufunc->uf_func_type = &t_func_any;
1436
1437 // error messages are for the first function line
1438 lnum_save = SOURCING_LNUM;
1439 SOURCING_LNUM = sourcing_lnum_top;
1440
1441 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001442 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001443 {
1444 SOURCING_LNUM = lnum_save;
1445 goto erret;
1446 }
1447
1448 // parse the return type, if any
1449 if (parse_return_type(ufunc, ret_type) == FAIL)
1450 goto erret;
1451
1452 pt = ALLOC_CLEAR_ONE(partial_T);
1453 if (pt == NULL)
1454 goto erret;
1455 pt->pt_func = ufunc;
1456 pt->pt_refcount = 1;
1457
1458 ufunc->uf_lines = newlines;
1459 newlines.ga_data = NULL;
1460 if (sandbox)
1461 ufunc->uf_flags |= FC_SANDBOX;
1462 if (!ASCII_ISUPPER(*ufunc->uf_name))
1463 ufunc->uf_flags |= FC_VIM9;
1464 ufunc->uf_script_ctx = current_sctx;
1465 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1466 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1467 set_function_type(ufunc);
1468
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001469 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1470
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001471 rettv->vval.v_partial = pt;
1472 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001473 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001474 ret = OK;
1475
1476erret:
1477 if (lnum_save >= 0)
1478 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001479 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001480 if (newargs != NULL)
1481 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001482 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001483 if (ufunc != NULL)
1484 {
1485 func_clear(ufunc, TRUE);
1486 func_free(ufunc, TRUE);
1487 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001488 return ret;
1489}
1490
1491/*
1492 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001493 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001494 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001495 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1496 */
1497 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001498get_lambda_tv(
1499 char_u **arg,
1500 typval_T *rettv,
1501 int types_optional,
1502 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001503{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001504 int evaluate = evalarg != NULL
1505 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001506 garray_T newargs;
1507 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001508 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001509 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001510 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001511 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001512 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001513 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001514 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001515 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001516 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001517 char_u *s;
1518 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001519 int *old_eval_lavars = eval_lavars_used;
1520 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001521 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001522 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001523 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001524 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001525 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001526 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001527
Bram Moolenaar4525a572022-02-13 11:57:33 +00001528 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001529 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001530
1531 ga_init(&newargs);
1532 ga_init(&newlines);
1533
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001534 // First, check if this is really a lambda expression. "->" or "=>" must
1535 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001536 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001537 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001538 types_optional ? &argtypes : NULL, types_optional,
1539 types_optional ? &arg_objm : NULL, evalarg,
1540 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001541 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001542 {
1543 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001544 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001545 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001546 ga_clear(&arg_objm);
1547 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001548 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001549 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001550
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001551 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001552 if (evaluate)
1553 pnewargs = &newargs;
1554 else
1555 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001556 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001557 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001558 types_optional ? &argtypes : NULL, types_optional,
1559 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001560 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001561 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001562 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001563 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001564 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001565 {
1566 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001567 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001568 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001569 ga_clear(&arg_objm);
1570 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001571 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001572 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001573 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001574 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001575
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001576 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1577 if (ret_type != NULL)
1578 {
1579 ret_type = vim_strsave(ret_type);
1580 tofree2 = ret_type;
1581 }
1582
Bram Moolenaare38eab22019-12-05 21:50:01 +01001583 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001584 if (evaluate)
1585 eval_lavars_used = &eval_lavars;
1586
Bram Moolenaar65c44152020-12-24 15:14:01 +01001587 *arg = skipwhite_and_linebreak(*arg, evalarg);
1588
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001589 // Recognize "{" as the start of a function body.
1590 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001591 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001592 if (evalarg == NULL)
1593 // cannot happen?
1594 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001595 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001596 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1597 types_optional ? &argtypes : NULL, varargs,
1598 &default_args, ret_type) == FAIL)
1599 goto errret;
1600 goto theend;
1601 }
1602 if (default_args.ga_len > 0)
1603 {
1604 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001605 goto errret;
1606 }
1607
Bram Moolenaare38eab22019-12-05 21:50:01 +01001608 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001609 start = *arg;
1610 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001611 if (ret == FAIL)
1612 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001613
Bram Moolenaar65c44152020-12-24 15:14:01 +01001614 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001615 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001616 *arg = skipwhite_and_linebreak(*arg, evalarg);
1617 if (**arg != '}')
1618 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001619 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001620 goto errret;
1621 }
1622 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001623 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001624
1625 if (evaluate)
1626 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001627 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001628 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001629 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001630 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001631 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001632
Bram Moolenaare01e5212023-01-08 20:31:18 +00001633 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001634 if (fp == NULL)
1635 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001636 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001637 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001638 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001639 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001641 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001642 if (ga_grow(&newlines, 1) == FAIL)
1643 goto errret;
1644
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001645 // If there are line breaks, we need to split up the string.
1646 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001647 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001648 line_end = end;
1649
1650 // Add "return " before the expression (or the first line).
1651 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001652 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001653 if (p == NULL)
1654 goto errret;
1655 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1656 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001657 vim_strncpy(p + 7, start, line_end - start);
1658
1659 if (line_end != end)
1660 {
1661 // Add more lines, split by line breaks. Thus is used when a
1662 // lambda with { cmds } is encountered.
1663 while (*line_end == '\n')
1664 {
1665 if (ga_grow(&newlines, 1) == FAIL)
1666 goto errret;
1667 start = line_end + 1;
1668 line_end = vim_strchr(start, '\n');
1669 if (line_end == NULL)
1670 line_end = end;
1671 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1672 vim_strnsave(start, line_end - start);
1673 }
1674 }
1675
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001676 if (strstr((char *)p + 7, "a:") == NULL)
1677 // No a: variables are used for sure.
1678 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001679
1680 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001681 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001682 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001683 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001684 if (types_optional)
1685 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001686 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001687 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001688 goto errret;
1689 if (ret_type != NULL)
1690 {
1691 fp->uf_ret_type = parse_type(&ret_type,
1692 &fp->uf_type_list, TRUE);
1693 if (fp->uf_ret_type == NULL)
1694 goto errret;
1695 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001696 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001697 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001698 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001699
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001700 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001701 if (current_funccal != NULL && eval_lavars)
1702 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001703 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001704 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001705 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001706 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001707
1708#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001709 if (prof_def_func())
1710 func_do_profile(fp);
1711#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001712 if (sandbox)
1713 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001714 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001715 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001716 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001717 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001718 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001719 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001720 // Use the line number of the arguments.
1721 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001722
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001723 function_using_block_scopes(fp, evalarg->eval_cstack);
1724
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001725 pt->pt_func = fp;
1726 pt->pt_refcount = 1;
1727 rettv->vval.v_partial = pt;
1728 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001729
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001730 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001731 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001732
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001733theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001734 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001735 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001736 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001737 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001738 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001739 ga_clear(&arg_objm);
1740 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001741
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001742 return OK;
1743
1744errret:
1745 ga_clear_strings(&newargs);
1746 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001747 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001748 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001749 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001750 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001751 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001752 if (fp != NULL)
1753 vim_free(fp->uf_arg_types);
1754 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001755 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001756 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001757 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001758 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001759 return FAIL;
1760}
1761
1762/*
1763 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1764 * name it contains, otherwise return "name".
1765 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1766 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001767 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1768 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001769 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001770 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001771 */
1772 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001773deref_func_name(
1774 char_u *name,
1775 int *lenp,
1776 partial_T **partialp,
1777 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001778 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001779 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001780 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001781{
1782 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001783 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001784 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001785 char_u *s = NULL;
1786 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001787 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788
1789 if (partialp != NULL)
1790 *partialp = NULL;
1791
1792 cc = name[*lenp];
1793 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001794
Bram Moolenaar71f21932022-01-07 18:20:55 +00001795 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001796 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001797 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001798 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001799 tv = &v->di_tv;
1800 }
1801 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1802 {
1803 imported_T *import;
1804 char_u *p = name;
1805 int len = *lenp;
1806
1807 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001808 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001809 p = name + 2;
1810 len -= 2;
1811 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001812 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001813
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001814 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001815 if (import != NULL)
1816 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001817 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001818 if (new_function)
1819 semsg(_(e_redefining_imported_item_str), name);
1820 else
1821 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001822 name[len] = cc;
1823 *lenp = 0;
1824 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001825 }
1826 }
1827
1828 if (tv != NULL)
1829 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001830 if (found_var != NULL)
1831 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001832 if (tv->v_type == VAR_FUNC)
1833 {
1834 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001835 {
1836 *lenp = 0;
1837 return (char_u *)""; // just in case
1838 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001839 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001840 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001841 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001842
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001843 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001844 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001845 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001846
1847 if (pt == NULL)
1848 {
1849 *lenp = 0;
1850 return (char_u *)""; // just in case
1851 }
1852 if (partialp != NULL)
1853 *partialp = pt;
1854 s = partial_name(pt);
1855 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001856 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001857
1858 if (s != NULL)
1859 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001860 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001861 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001862 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001863
1864 if (sv != NULL)
1865 *type = sv->sv_type;
1866 }
1867 return s;
1868 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001869 }
1870
1871 return name;
1872}
1873
1874/*
1875 * Give an error message with a function name. Handle <SNR> things.
1876 * "ermsg" is to be passed without translation, use N_() instead of _().
1877 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001878 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001879emsg_funcname(char *ermsg, char_u *name)
1880{
Bram Moolenaar54598062022-01-13 12:05:09 +00001881 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001882
Bram Moolenaar54598062022-01-13 12:05:09 +00001883 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001884 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001885 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001886 if (p != name)
1887 vim_free(p);
1888}
1889
1890/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001891 * Get function arguments at "*arg" and advance it.
1892 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001893 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001894 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001895 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001896get_func_arguments(
1897 char_u **arg,
1898 evalarg_T *evalarg,
1899 int partial_argc,
1900 typval_T *argvars,
1901 int *argcount)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001902{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001903 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001904 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001905 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001906 int evaluate = evalarg == NULL
1907 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001908
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001909 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001910 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001911 // skip the '(' or ',' and possibly line breaks
1912 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1913
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001914 if (*argp == ')' || *argp == ',' || *argp == NUL)
1915 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001916 if (eval1(&argp, &argvars[*argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001917 {
1918 ret = FAIL;
1919 break;
1920 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001921 ++*argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001922 // The comma should come right after the argument, but this wasn't
1923 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001924 if (vim9script)
1925 {
1926 if (*argp != ',' && *skipwhite(argp) == ',')
1927 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001928 if (evaluate)
1929 semsg(_(e_no_white_space_allowed_before_str_str),
1930 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001931 ret = FAIL;
1932 break;
1933 }
1934 }
1935 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001936 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001937 if (*argp != ',')
1938 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001939 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001940 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001941 if (evaluate)
1942 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001943 ret = FAIL;
1944 break;
1945 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001946 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001947
Bram Moolenaare6b53242020-07-01 17:28:33 +02001948 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001949 if (*argp == ')')
1950 ++argp;
1951 else
1952 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001953 *arg = argp;
1954 return ret;
1955}
1956
1957/*
1958 * Call a function and put the result in "rettv".
1959 * Return OK or FAIL.
1960 */
1961 int
1962get_func_tv(
1963 char_u *name, // name of the function
1964 int len, // length of "name" or -1 to use strlen()
1965 typval_T *rettv,
1966 char_u **arg, // argument, pointing to the '('
1967 evalarg_T *evalarg, // for line continuation
1968 funcexe_T *funcexe) // various values
1969{
1970 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001971 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001972 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1973 int argcount = 0; // number of arguments found
1974 int vim9script = in_vim9script();
1975 int evaluate = evalarg == NULL
1976 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1977
1978 argp = *arg;
1979 ret = get_func_arguments(&argp, evalarg,
1980 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
1981 argvars, &argcount);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001982
1983 if (ret == OK)
1984 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001985 int i = 0;
1986 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001987
1988 if (get_vim_var_nr(VV_TESTING))
1989 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001990 // Prepare for calling test_garbagecollect_now(), need to know
1991 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001992 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001993 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001994 for (i = 0; i < argcount; ++i)
1995 if (ga_grow(&funcargs, 1) == OK)
1996 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1997 &argvars[i];
1998 }
1999
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002000 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002001 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002002 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002003 // An error in a builtin function does not return FAIL, but we do
2004 // want to abort further processing if an error was given.
2005 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002006 clear_tv(rettv);
2007 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002008
2009 funcargs.ga_len -= i;
2010 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002011 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002012 {
2013 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002014 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002015 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002016 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002017 }
2018
2019 while (--argcount >= 0)
2020 clear_tv(&argvars[argcount]);
2021
Bram Moolenaar4525a572022-02-13 11:57:33 +00002022 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002023 *arg = argp;
2024 else
2025 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002026 return ret;
2027}
2028
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002029/*
2030 * Return TRUE if "p" starts with "<SID>" or "s:".
2031 * Only works if eval_fname_script() returned non-zero for "p"!
2032 */
2033 static int
2034eval_fname_sid(char_u *p)
2035{
2036 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2037}
2038
2039/*
2040 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2041 * Change <SNR>123_name() to K_SNR 123_name().
2042 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002043 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002044 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002045 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002046fname_trans_sid(
2047 char_u *name,
2048 char_u *fname_buf,
2049 char_u **tofree,
2050 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002051{
2052 int llen;
2053 char_u *fname;
2054 int i;
2055
2056 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002057 if (llen == 0)
2058 return name; // no prefix
2059
2060 fname_buf[0] = K_SPECIAL;
2061 fname_buf[1] = KS_EXTRA;
2062 fname_buf[2] = (int)KE_SNR;
2063 i = 3;
2064 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002065 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002066 if (current_sctx.sc_sid <= 0)
2067 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002068 else
2069 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002070 sprintf((char *)fname_buf + 3, "%ld_",
2071 (long)current_sctx.sc_sid);
2072 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002073 }
2074 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002075 if (i + STRLEN(name + llen) < FLEN_FIXED)
2076 {
2077 STRCPY(fname_buf + i, name + llen);
2078 fname = fname_buf;
2079 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002080 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002081 {
2082 fname = alloc(i + STRLEN(name + llen) + 1);
2083 if (fname == NULL)
2084 *error = FCERR_OTHER;
2085 else
2086 {
2087 *tofree = fname;
2088 mch_memmove(fname, fname_buf, (size_t)i);
2089 STRCPY(fname + i, name + llen);
2090 }
2091 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002092 return fname;
2093}
2094
2095/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002096 * Concatenate the script ID and function name into "<SNR>99_name".
2097 * "buffer" must have size MAX_FUNC_NAME_LEN.
2098 */
2099 void
2100func_name_with_sid(char_u *name, int sid, char_u *buffer)
2101{
2102 // A script-local function is stored as "<SNR>99_name".
2103 buffer[0] = K_SPECIAL;
2104 buffer[1] = KS_EXTRA;
2105 buffer[2] = (int)KE_SNR;
2106 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2107 (long)sid, name);
2108}
2109
2110/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 * Find a function "name" in script "sid".
2112 */
2113 static ufunc_T *
2114find_func_with_sid(char_u *name, int sid)
2115{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002116 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002117 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118
Bram Moolenaarb8822442022-01-11 15:24:05 +00002119 if (!SCRIPT_ID_VALID(sid))
2120 return NULL; // not in a script
2121
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002122 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 hi = hash_find(&func_hashtab, buffer);
2124 if (!HASHITEM_EMPTY(hi))
2125 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002126 return NULL;
2127}
2128
2129/*
2130 * Find a function "name" in script "sid" prefixing the autoload prefix.
2131 */
2132 static ufunc_T *
2133find_func_with_prefix(char_u *name, int sid)
2134{
2135 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002136 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002137 scriptitem_T *si;
2138
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002139 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002140 return NULL; // already has the prefix
2141 if (!SCRIPT_ID_VALID(sid))
2142 return NULL; // not in a script
2143 si = SCRIPT_ITEM(sid);
2144 if (si->sn_autoload_prefix != NULL)
2145 {
2146 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2147 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002148 char_u *namep;
2149
2150 // skip a "<SNR>99_" prefix
2151 namep = untrans_function_name(name);
2152 if (namep == NULL)
2153 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002154
2155 // An exported function in an autoload script is stored as
2156 // "dir#path#name".
2157 if (len < sizeof(buffer))
2158 auto_name = buffer;
2159 else
2160 auto_name = alloc(len);
2161 if (auto_name != NULL)
2162 {
2163 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002164 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002165 hi = hash_find(&func_hashtab, auto_name);
2166 if (auto_name != buffer)
2167 vim_free(auto_name);
2168 if (!HASHITEM_EMPTY(hi))
2169 return HI2UF(hi);
2170 }
2171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172
2173 return NULL;
2174}
2175
2176/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002177 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002178 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2179 * functions.
2180 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002181 * Return NULL for unknown function.
2182 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002183 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002184find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002185{
2186 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002188
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002189 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002191 // Find script-local function before global one.
2192 if (in_vim9script() && eval_isnamec1(*name)
2193 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002195 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2196 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002197 if (func != NULL)
2198 return func;
2199 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002200 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2201 {
2202 char_u *p = name + 5;
2203 long sid;
2204
2205 // printable "<SNR>123_Name" form
2206 sid = getdigits(&p);
2207 if (*p == '_')
2208 {
2209 func = find_func_with_sid(p + 1, (int)sid);
2210 if (func != NULL)
2211 return func;
2212 }
2213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002215
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002216 if ((flags & FFED_NO_GLOBAL) == 0)
2217 {
2218 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002219 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002220 if (!HASHITEM_EMPTY(hi))
2221 return HI2UF(hi);
2222 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223
Bram Moolenaarb8822442022-01-11 15:24:05 +00002224 // Find autoload function if this is an autoload script.
2225 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2226 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227}
2228
2229/*
2230 * Find a function by name, return pointer to it in ufuncs.
2231 * "cctx" is passed in a :def function to find imported functions.
2232 * Return NULL for unknown or dead function.
2233 */
2234 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002235find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002237 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238
2239 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2240 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002241 return NULL;
2242}
2243
2244/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002245 * Return TRUE if "ufunc" is a global function.
2246 */
2247 int
2248func_is_global(ufunc_T *ufunc)
2249{
2250 return ufunc->uf_name[0] != K_SPECIAL;
2251}
2252
2253/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002254 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2255 */
2256 int
2257func_requires_g_prefix(ufunc_T *ufunc)
2258{
2259 return ufunc->uf_name[0] != K_SPECIAL
2260 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002261 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
2262 && !isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002263}
2264
2265/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002266 * Copy the function name of "fp" to buffer "buf".
2267 * "buf" must be able to hold the function name plus three bytes.
2268 * Takes care of script-local function names.
2269 */
2270 static void
2271cat_func_name(char_u *buf, ufunc_T *fp)
2272{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002273 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002274 {
2275 STRCPY(buf, "<SNR>");
2276 STRCAT(buf, fp->uf_name + 3);
2277 }
2278 else
2279 STRCPY(buf, fp->uf_name);
2280}
2281
2282/*
2283 * Add a number variable "name" to dict "dp" with value "nr".
2284 */
2285 static void
2286add_nr_var(
2287 dict_T *dp,
2288 dictitem_T *v,
2289 char *name,
2290 varnumber_T nr)
2291{
2292 STRCPY(v->di_key, name);
2293 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002294 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002295 v->di_tv.v_type = VAR_NUMBER;
2296 v->di_tv.v_lock = VAR_FIXED;
2297 v->di_tv.vval.v_number = nr;
2298}
2299
2300/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002301 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002302 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002303 static void
2304free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002305{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002306 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002307
Bram Moolenaarca16c602022-09-06 18:57:08 +01002308 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002309 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002310 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002311
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002312 // When garbage collecting a funccall_T may be freed before the
2313 // function that references it, clear its uf_scoped field.
2314 // The function may have been redefined and point to another
2315 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002316 if (fp != NULL && fp->uf_scoped == fc)
2317 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002318 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002319 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002320
Bram Moolenaarca16c602022-09-06 18:57:08 +01002321 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002322 vim_free(fc);
2323}
2324
2325/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002326 * Free "fc" and what it contains.
2327 * Can be called only when "fc" is kept beyond the period of it called,
2328 * i.e. after cleanup_function_call(fc).
2329 */
2330 static void
2331free_funccal_contents(funccall_T *fc)
2332{
2333 listitem_T *li;
2334
2335 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002336 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002337
2338 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002339 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002340
2341 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002342 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002343 clear_tv(&li->li_tv);
2344
2345 free_funccal(fc);
2346}
2347
2348/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002349 * Handle the last part of returning from a function: free the local hashtable.
2350 * Unless it is still in use by a closure.
2351 */
2352 static void
2353cleanup_function_call(funccall_T *fc)
2354{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002355 int may_free_fc = fc->fc_refcount <= 0;
2356 int free_fc = TRUE;
2357
Bram Moolenaarca16c602022-09-06 18:57:08 +01002358 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002359
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002360 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002361 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2362 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002363 else
2364 free_fc = FALSE;
2365
2366 // If the a:000 list and the l: and a: dicts are not referenced and
2367 // there is no closure using it, we can free the funccall_T and what's
2368 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002369 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2370 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002371 else
2372 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002373 int todo;
2374 hashitem_T *hi;
2375 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002376
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002377 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002378
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002379 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002380 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002381 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002382 {
2383 if (!HASHITEM_EMPTY(hi))
2384 {
2385 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002386 di = HI2DI(hi);
2387 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002388 }
2389 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002390 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002391
Bram Moolenaarca16c602022-09-06 18:57:08 +01002392 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2393 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002394 else
2395 {
2396 listitem_T *li;
2397
2398 free_fc = FALSE;
2399
2400 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002401 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002402 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002403 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002404
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002405 if (free_fc)
2406 free_funccal(fc);
2407 else
2408 {
2409 static int made_copy = 0;
2410
2411 // "fc" is still in use. This can happen when returning "a:000",
2412 // assigning "l:" to a global variable or defining a closure.
2413 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002414 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002415 previous_funccal = fc;
2416
2417 if (want_garbage_collect)
2418 // If garbage collector is ready, clear count.
2419 made_copy = 0;
2420 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002421 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002422 // We have made a lot of copies, worth 4 Mbyte. This can happen
2423 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002424 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002425 // too much memory.
2426 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002427 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002428 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002429 }
2430}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002431
2432/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002433 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2434 */
2435 static int
2436numbered_function(char_u *name)
2437{
2438 return isdigit(*name)
2439 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2440}
2441
2442/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002443 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002444 * 1. ordinary names, function defined with :function or :def;
2445 * can start with "<SNR>123_" literally or with K_SPECIAL.
2446 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002447 * For the first we only count the name stored in func_hashtab as a reference,
2448 * using function() does not count as a reference, because the function is
2449 * looked up by name.
2450 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002451 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002452func_name_refcount(char_u *name)
2453{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002454 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002455}
2456
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457/*
2458 * Unreference "fc": decrement the reference count and free it when it
2459 * becomes zero. "fp" is detached from "fc".
2460 * When "force" is TRUE we are exiting.
2461 */
2462 static void
2463funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2464{
2465 funccall_T **pfc;
2466 int i;
2467
2468 if (fc == NULL)
2469 return;
2470
2471 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002472 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2473 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2474 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2475 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 {
2477 if (fc == *pfc)
2478 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002479 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 free_funccal_contents(fc);
2481 return;
2482 }
2483 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002484 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2485 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2486 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487}
2488
2489/*
2490 * Remove the function from the function hashtable. If the function was
2491 * deleted while it still has references this was already done.
2492 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2493 */
2494 static int
2495func_remove(ufunc_T *fp)
2496{
2497 hashitem_T *hi;
2498
2499 // Return if it was already virtually deleted.
2500 if (fp->uf_flags & FC_DEAD)
2501 return FALSE;
2502
2503 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002504 if (HASHITEM_EMPTY(hi))
2505 return FALSE;
2506
2507 // When there is a def-function index do not actually remove the
2508 // function, so we can find the index when defining the function again.
2509 // Do remove it when it's a copy.
2510 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002512 fp->uf_flags |= FC_DEAD;
2513 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002515 hash_remove(&func_hashtab, hi, "remove function");
2516 fp->uf_flags |= FC_DELETED;
2517 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518}
2519
2520 static void
2521func_clear_items(ufunc_T *fp)
2522{
2523 ga_clear_strings(&(fp->uf_args));
2524 ga_clear_strings(&(fp->uf_def_args));
2525 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002527 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002528 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002529 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002530
2531 // Increment the refcount of this function to avoid it being freed
2532 // recursively when the partial is freed.
2533 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002534 partial_unref(fp->uf_partial);
2535 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002536 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002537
2538#ifdef FEAT_LUA
2539 if (fp->uf_cb_free != NULL)
2540 {
2541 fp->uf_cb_free(fp->uf_cb_state);
2542 fp->uf_cb_free = NULL;
2543 }
2544
2545 fp->uf_cb_state = NULL;
2546 fp->uf_cb = NULL;
2547#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548#ifdef FEAT_PROFILE
2549 VIM_CLEAR(fp->uf_tml_count);
2550 VIM_CLEAR(fp->uf_tml_total);
2551 VIM_CLEAR(fp->uf_tml_self);
2552#endif
2553}
2554
2555/*
2556 * Free all things that a function contains. Does not free the function
2557 * itself, use func_free() for that.
2558 * When "force" is TRUE we are exiting.
2559 */
2560 static void
2561func_clear(ufunc_T *fp, int force)
2562{
2563 if (fp->uf_cleared)
2564 return;
2565 fp->uf_cleared = TRUE;
2566
2567 // clear this function
2568 func_clear_items(fp);
2569 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002570 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571}
2572
2573/*
2574 * Free a function and remove it from the list of functions. Does not free
2575 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002576 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002577 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002579 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002580func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581{
2582 // Only remove it when not done already, otherwise we would remove a newer
2583 // version of the function with the same name.
2584 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2585 func_remove(fp);
2586
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002587 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002588 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002589 if (fp->uf_dfunc_idx > 0)
2590 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002591 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002593 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002594 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002595 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596}
2597
2598/*
2599 * Free all things that a function contains and free the function itself.
2600 * When "force" is TRUE we are exiting.
2601 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002602 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603func_clear_free(ufunc_T *fp, int force)
2604{
2605 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002606 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2607 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002608 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002609 else
2610 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611}
2612
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002613/*
2614 * Copy already defined function "lambda" to a new function with name "global".
2615 * This is for when a compiled function defines a global function.
2616 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002617 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002618copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002619 char_u *lambda,
2620 char_u *global,
2621 loopvarinfo_T *loopvarinfo,
2622 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002623{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002624 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002625 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002626
2627 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002628 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002629 semsg(_(e_lambda_function_not_found_str), lambda);
2630 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002631 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002632
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002633 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002634 if (fp != NULL)
2635 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002636 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002637 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002638 return FAIL;
2639 }
2640
Bram Moolenaare01e5212023-01-08 20:31:18 +00002641 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002642 if (fp == NULL)
2643 return FAIL;
2644
2645 fp->uf_varargs = ufunc->uf_varargs;
2646 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2647 fp->uf_def_status = ufunc->uf_def_status;
2648 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2649 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2650 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2651 == FAIL
2652 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2653 goto failed;
2654
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002655 if (ufunc->uf_arg_types != NULL)
2656 {
2657 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2658 if (fp->uf_arg_types == NULL)
2659 goto failed;
2660 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2661 sizeof(type_T *) * fp->uf_args.ga_len);
2662 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002663 if (ufunc->uf_va_name != NULL)
2664 {
2665 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2666 if (fp->uf_va_name == NULL)
2667 goto failed;
2668 }
2669 fp->uf_ret_type = ufunc->uf_ret_type;
2670
2671 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002672
2673 fp->uf_name_exp = NULL;
2674 set_ufunc_name(fp, global);
2675
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002676 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002677
2678 // the referenced dfunc_T is now used one more time
2679 link_def_function(fp);
2680
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002681 // Create a partial to store the context of the function where it was
2682 // instantiated. Only needs to be done once. Do this on the original
2683 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002684 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2685 {
2686 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2687
2688 if (pt == NULL)
2689 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002690 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002691 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002692 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002693 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002694 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002695 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002696 }
2697
2698 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002699
2700failed:
2701 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002702 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002703}
2704
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002705static int funcdepth = 0;
2706
2707/*
2708 * Increment the function call depth count.
2709 * Return FAIL when going over 'maxfuncdepth'.
2710 * Otherwise return OK, must call funcdepth_decrement() later!
2711 */
2712 int
2713funcdepth_increment(void)
2714{
2715 if (funcdepth >= p_mfd)
2716 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002717 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002718 return FAIL;
2719 }
2720 ++funcdepth;
2721 return OK;
2722}
2723
2724 void
2725funcdepth_decrement(void)
2726{
2727 --funcdepth;
2728}
2729
2730/*
2731 * Get the current function call depth.
2732 */
2733 int
2734funcdepth_get(void)
2735{
2736 return funcdepth;
2737}
2738
2739/*
2740 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002741 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002742 * times as funcdepth_increment().
2743 */
2744 void
2745funcdepth_restore(int depth)
2746{
2747 funcdepth = depth;
2748}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002749
2750/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002751 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2752 * "rettv".
2753 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2754 * Returns NULL when allocation fails.
2755 */
2756 funccall_T *
2757create_funccal(ufunc_T *fp, typval_T *rettv)
2758{
2759 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2760
2761 if (fc == NULL)
2762 return NULL;
2763 fc->fc_caller = current_funccal;
2764 current_funccal = fc;
2765 fc->fc_func = fp;
2766 func_ptr_ref(fp);
2767 fc->fc_rettv = rettv;
2768 return fc;
2769}
2770
2771/*
2772 * To be called when returning from a compiled function; restores
2773 * current_funccal.
2774 */
2775 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002776remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002777{
2778 funccall_T *fc = current_funccal;
2779
2780 current_funccal = fc->fc_caller;
2781 free_funccal(fc);
2782}
2783
2784/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002785 * Call a user function.
2786 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002787 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002788call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002789 ufunc_T *fp, // pointer to function
2790 int argcount, // nr of args
2791 typval_T *argvars, // arguments
2792 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002793 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002794 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002795{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002796 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002797 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002798 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002799 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002800 funccall_T *fc;
2801 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002802 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002803 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002804 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002805 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002806 int i;
2807 int ai;
2808 int islambda = FALSE;
2809 char_u numbuf[NUMBUFLEN];
2810 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002811 typval_T *tv_to_free[MAX_FUNC_ARGS];
2812 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002813#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002814 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002815#endif
ichizok7e5fe382023-04-15 13:17:50 +01002816 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002817
Bram Moolenaarb2049902021-01-24 12:53:53 +01002818#ifdef FEAT_PROFILE
2819 CLEAR_FIELD(profile_info);
2820#endif
2821
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002822 // If depth of calling is getting too high, don't execute the function.
2823 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002824 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002825 rettv->v_type = VAR_NUMBER;
2826 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002827 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002828 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002829
Bram Moolenaare38eab22019-12-05 21:50:01 +01002830 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002831
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002832 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002833 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002834 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002835 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002836 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002837 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2838 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002839 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002840 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002841
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002842 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002844#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002845 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002846#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002847 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002848#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002849 if (do_profiling == PROF_YES)
2850 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002851#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002852 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002853 if (call_def_function(fp, argcount, argvars, 0,
2854 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2855 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002856 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002857#ifdef FEAT_PROFILE
2858 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002859 || (caller != NULL && caller->uf_profiling)))
2860 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002861#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002862 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002863 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002864 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 }
2866
Bram Moolenaar38453522021-11-28 22:00:12 +00002867 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002868
2869 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002870 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2871 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2872 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002873 */
2874 /*
2875 * Init l: variables.
2876 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002877 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002878 if (selfdict != NULL)
2879 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002880 // Set l:self to "selfdict". Use "name" to avoid a warning from
2881 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002882 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002883 name = v->di_key;
2884 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002885 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002886 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002887 v->di_tv.v_type = VAR_DICT;
2888 v->di_tv.v_lock = 0;
2889 v->di_tv.vval.v_dict = selfdict;
2890 ++selfdict->dv_refcount;
2891 }
2892
2893 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002894 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002895 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002896 * Set a:000 to a list with room for the "..." arguments.
2897 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002898 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002899 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002900 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002901 (varnumber_T)(argcount >= fp->uf_args.ga_len
2902 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002903 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002904 if ((fp->uf_flags & FC_NOARGS) == 0)
2905 {
2906 // Use "name" to avoid a warning from some compiler that checks the
2907 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002908 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002909 name = v->di_key;
2910 STRCPY(name, "000");
2911 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002912 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002913 v->di_tv.v_type = VAR_LIST;
2914 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002915 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002916 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002917 CLEAR_FIELD(fc->fc_l_varlist);
2918 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2919 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002920
2921 /*
2922 * Set a:firstline to "firstline" and a:lastline to "lastline".
2923 * Set a:name to named arguments.
2924 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002925 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002926 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002927 if ((fp->uf_flags & FC_NOARGS) == 0)
2928 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002929 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2930 "firstline", (varnumber_T)funcexe->fe_firstline);
2931 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2932 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002933 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002934 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002935 {
2936 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002937 typval_T def_rettv;
2938 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002939
2940 ai = i - fp->uf_args.ga_len;
2941 if (ai < 0)
2942 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002943 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002944 name = FUNCARG(fp, i);
2945 if (islambda)
2946 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002947
2948 // evaluate named argument default expression
2949 isdefault = ai + fp->uf_def_args.ga_len >= 0
2950 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2951 && argvars[i].vval.v_number == VVAL_NONE));
2952 if (isdefault)
2953 {
2954 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002955
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002956 def_rettv.v_type = VAR_NUMBER;
2957 def_rettv.vval.v_number = -1;
2958
2959 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2960 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002961 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002962 {
2963 default_arg_err = 1;
2964 break;
2965 }
2966 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002967 }
2968 else
2969 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002970 if ((fp->uf_flags & FC_NOARGS) != 0)
2971 // Bail out if no a: arguments used (in lambda).
2972 break;
2973
Bram Moolenaare38eab22019-12-05 21:50:01 +01002974 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002975 sprintf((char *)numbuf, "%d", ai + 1);
2976 name = numbuf;
2977 }
2978 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2979 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002980 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002981 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002982 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983 }
2984 else
2985 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002986 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002987 if (v == NULL)
2988 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002989 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002990 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002991
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002992 // Note: the values are copied directly to avoid alloc/free.
2993 // "argvars" must have VAR_FIXED for v_lock.
2994 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002995 v->di_tv.v_lock = VAR_FIXED;
2996
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002997 if (isdefault)
2998 // Need to free this later, no matter where it's stored.
2999 tv_to_free[tv_to_free_len++] = &v->di_tv;
3000
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003001 if (addlocal)
3002 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003003 // Named arguments should be accessed without the "a:" prefix in
3004 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003005 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003006 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003007 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003008 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003009 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003010
3011 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3012 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003013 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003014
3015 li->li_tv = argvars[i];
3016 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003017 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003018 }
3019 }
3020
Bram Moolenaare38eab22019-12-05 21:50:01 +01003021 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003022 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003023
3024 if (fp->uf_flags & FC_SANDBOX)
3025 {
3026 using_sandbox = TRUE;
3027 ++sandbox;
3028 }
3029
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003030 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003031 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003032 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003033 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003034 ++no_wait_return;
3035 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003036
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003037 smsg(_("calling %s"), SOURCING_NAME);
3038 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003039 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003040 char_u buf[MSG_BUF_LEN];
3041 char_u numbuf2[NUMBUFLEN];
3042 char_u *tofree;
3043 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003044
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003045 msg_puts("(");
3046 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003047 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003048 if (i > 0)
3049 msg_puts(", ");
3050 if (argvars[i].v_type == VAR_NUMBER)
3051 msg_outnum((long)argvars[i].vval.v_number);
3052 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003053 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003054 // Do not want errors such as E724 here.
3055 ++emsg_off;
3056 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3057 --emsg_off;
3058 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003059 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003060 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003061 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003062 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3063 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003064 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003065 msg_puts((char *)s);
3066 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003067 }
3068 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003069 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003070 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003071 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003072 msg_puts("\n"); // don't overwrite this either
3073
3074 verbose_leave_scroll();
3075 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003076 }
3077#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003078 if (do_profiling == PROF_YES)
3079 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003080 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003081#endif
3082
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003083 // "legacy" does not apply to commands in the function
3084 sticky_cmdmod_flags = 0;
3085
Bram Moolenaar98aff652022-09-06 21:02:35 +01003086 // If called from a compiled :def function the execution context must be
3087 // hidden, any deferred functions need to be added to the function being
3088 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003089 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003090
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003091 save_current_sctx = current_sctx;
3092 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003093 save_did_emsg = did_emsg;
3094 did_emsg = FALSE;
3095
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003096 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003097 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003098 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003099 retval = FCERR_FAILED;
3100 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003101 else if (islambda)
3102 {
3103 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3104
3105 // A Lambda always has the command "return {expr}". It is much faster
3106 // to evaluate {expr} directly.
3107 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003108 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003109 --ex_nesting_level;
3110 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003111 else
3112 // call do_cmdline() to execute the lines
3113 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003114 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3115
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003116 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003117 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003118
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003119 if (RedrawingDisabled > 0)
3120 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003121
Bram Moolenaare38eab22019-12-05 21:50:01 +01003122 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003123 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3124 {
3125 clear_tv(rettv);
3126 rettv->v_type = VAR_NUMBER;
3127 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003128
3129 // In corner cases returning a "failed" value is not backwards
3130 // compatible. Only do this for Vim9 script.
3131 if (in_vim9script())
3132 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003133 }
3134
3135#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003136 if (do_profiling == PROF_YES)
3137 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003138 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003139
3140 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3141 profile_may_end_func(&profile_info, fp, caller);
3142 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003143#endif
3144
Bram Moolenaare38eab22019-12-05 21:50:01 +01003145 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003146 if (p_verbose >= 12)
3147 {
3148 ++no_wait_return;
3149 verbose_enter_scroll();
3150
3151 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003152 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003153 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003154 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003155 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156 else
3157 {
3158 char_u buf[MSG_BUF_LEN];
3159 char_u numbuf2[NUMBUFLEN];
3160 char_u *tofree;
3161 char_u *s;
3162
Bram Moolenaare38eab22019-12-05 21:50:01 +01003163 // The value may be very long. Skip the middle part, so that we
3164 // have some idea how it starts and ends. smsg() would always
3165 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003166 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003167 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003168 --emsg_off;
3169 if (s != NULL)
3170 {
3171 if (vim_strsize(s) > MSG_BUF_CLEN)
3172 {
3173 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3174 s = buf;
3175 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003176 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003177 vim_free(tofree);
3178 }
3179 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003180 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003181
3182 verbose_leave_scroll();
3183 --no_wait_return;
3184 }
3185
ichizok7e5fe382023-04-15 13:17:50 +01003186 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003187 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003188 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003189 restore_current_ectx(save_current_ectx);
3190
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003191#ifdef FEAT_PROFILE
3192 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003193 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003194#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003195 if (using_sandbox)
3196 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003197 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003198
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003199 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003200 {
3201 ++no_wait_return;
3202 verbose_enter_scroll();
3203
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003204 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003205 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003206
3207 verbose_leave_scroll();
3208 --no_wait_return;
3209 }
3210
3211 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003212 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003213 for (i = 0; i < tv_to_free_len; ++i)
3214 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003215 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003216
3217 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003218}
3219
3220/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003221 * Check the argument count for user function "fp".
3222 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3223 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003224 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003225check_user_func_argcount(ufunc_T *fp, int argcount)
3226{
3227 int regular_args = fp->uf_args.ga_len;
3228
3229 if (argcount < regular_args - fp->uf_def_args.ga_len)
3230 return FCERR_TOOFEW;
3231 else if (!has_varargs(fp) && argcount > regular_args)
3232 return FCERR_TOOMANY;
3233 return FCERR_UNKNOWN;
3234}
3235
3236/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003238 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003239 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240call_user_func_check(
3241 ufunc_T *fp,
3242 int argcount,
3243 typval_T *argvars,
3244 typval_T *rettv,
3245 funcexe_T *funcexe,
3246 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003247{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003248 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003249
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003250#ifdef FEAT_LUA
3251 if (fp->uf_flags & FC_CFUNC)
3252 {
3253 cfunc_T cb = fp->uf_cb;
3254
3255 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3256 }
3257#endif
3258
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003259 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3260 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003261 error = check_user_func_argcount(fp, argcount);
3262 if (error != FCERR_UNKNOWN)
3263 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003264
Bram Moolenaar50824712020-12-20 21:10:17 +01003265 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003266 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003270 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 int did_save_redo = FALSE;
3272 save_redo_T save_redo;
3273
3274 /*
3275 * Call the user function.
3276 * Save and restore search patterns, script variables and
3277 * redo buffer.
3278 */
3279 save_search_patterns();
3280 if (!ins_compl_active())
3281 {
3282 saveRedobuff(&save_redo);
3283 did_save_redo = TRUE;
3284 }
3285 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003286 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003287 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3289 // Function was unreferenced while being used, free it now.
3290 func_clear_free(fp, FALSE);
3291 if (did_save_redo)
3292 restoreRedobuff(&save_redo);
3293 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003294 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003297}
3298
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003299static funccal_entry_T *funccal_stack = NULL;
3300
3301/*
3302 * Save the current function call pointer, and set it to NULL.
3303 * Used when executing autocommands and for ":source".
3304 */
3305 void
3306save_funccal(funccal_entry_T *entry)
3307{
3308 entry->top_funccal = current_funccal;
3309 entry->next = funccal_stack;
3310 funccal_stack = entry;
3311 current_funccal = NULL;
3312}
3313
3314 void
3315restore_funccal(void)
3316{
3317 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003318 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003319 else
3320 {
3321 current_funccal = funccal_stack->top_funccal;
3322 funccal_stack = funccal_stack->next;
3323 }
3324}
3325
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003326 funccall_T *
3327get_current_funccal(void)
3328{
3329 return current_funccal;
3330}
3331
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003332/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003333 * Return TRUE when currently at the script level:
3334 * - not in a function
3335 * - not executing an autocommand
3336 * Note that when an autocommand sources a script the result is FALSE;
3337 */
3338 int
3339at_script_level(void)
3340{
3341 return current_funccal == NULL && autocmd_match == NULL;
3342}
3343
3344/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003345 * Mark all functions of script "sid" as deleted.
3346 */
3347 void
3348delete_script_functions(int sid)
3349{
3350 hashitem_T *hi;
3351 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003352 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003353 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003354 size_t len;
3355
3356 buf[0] = K_SPECIAL;
3357 buf[1] = KS_EXTRA;
3358 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003359 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003360 len = STRLEN(buf);
3361
Bram Moolenaarce658352020-07-31 23:47:12 +02003362 while (todo > 0)
3363 {
3364 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003365 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003366 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003367 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003368 fp = HI2UF(hi);
3369 if (STRNCMP(fp->uf_name, buf, len) == 0)
3370 {
3371 int changed = func_hashtab.ht_changed;
3372
3373 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003374
3375 if (fp->uf_calls > 0)
3376 {
3377 // Function is executing, don't free it but do remove
3378 // it from the hashtable.
3379 if (func_remove(fp))
3380 fp->uf_refcount--;
3381 }
3382 else
3383 {
3384 func_clear(fp, TRUE);
3385 // When clearing a function another function can be
3386 // cleared as a side effect. When that happens start
3387 // over.
3388 if (changed != func_hashtab.ht_changed)
3389 break;
3390 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003391 }
3392 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003393 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003394 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003395}
3396
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003397#if defined(EXITFREE) || defined(PROTO)
3398 void
3399free_all_functions(void)
3400{
3401 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003402 ufunc_T *fp;
3403 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003404 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003405 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003406
Bram Moolenaare38eab22019-12-05 21:50:01 +01003407 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003408 while (current_funccal != NULL)
3409 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003410 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003411 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003412 if (current_funccal == NULL && funccal_stack != NULL)
3413 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003414 }
3415
Bram Moolenaare38eab22019-12-05 21:50:01 +01003416 // First clear what the functions contain. Since this may lower the
3417 // reference count of a function, it may also free a function and change
3418 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003419 while (todo > 0)
3420 {
3421 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003422 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003423 if (!HASHITEM_EMPTY(hi))
3424 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 // clear the def function index now
3426 fp = HI2UF(hi);
3427 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003428 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429
Bram Moolenaare38eab22019-12-05 21:50:01 +01003430 // Only free functions that are not refcounted, those are
3431 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003432 if (func_name_refcount(fp->uf_name))
3433 ++skipped;
3434 else
3435 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003436 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003437 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003438 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003439 {
3440 skipped = 0;
3441 break;
3442 }
3443 }
3444 --todo;
3445 }
3446 }
3447
Bram Moolenaare38eab22019-12-05 21:50:01 +01003448 // Now actually free the functions. Need to start all over every time,
3449 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003450 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003451 while (func_hashtab.ht_used > skipped)
3452 {
3453 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003454 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003455 if (!HASHITEM_EMPTY(hi))
3456 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003457 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003458 // Only free functions that are not refcounted, those are
3459 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003460 fp = HI2UF(hi);
3461 if (func_name_refcount(fp->uf_name))
3462 ++skipped;
3463 else
3464 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003465 if (func_free(fp, FALSE) == OK)
3466 {
3467 skipped = 0;
3468 break;
3469 }
3470 // did not actually free it
3471 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003472 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003473 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003474 }
3475 if (skipped == 0)
3476 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477
3478 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003479}
3480#endif
3481
3482/*
3483 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003484 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3485 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003486 * "len" is the length of "name", or -1 for NUL terminated.
3487 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003489builtin_function(char_u *name, int len)
3490{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003491 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003492
Bram Moolenaara26b9702020-04-18 19:53:28 +02003493 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003494 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003495 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3496 {
3497 if (name[i] == AUTOLOAD_CHAR)
3498 return FALSE;
3499 if (!eval_isnamec(name[i]))
3500 {
3501 // "name.something" is not a builtin function
3502 if (name[i] == '.')
3503 return FALSE;
3504 break;
3505 }
3506 }
3507 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003508}
3509
3510 int
3511func_call(
3512 char_u *name,
3513 typval_T *args,
3514 partial_T *partial,
3515 dict_T *selfdict,
3516 typval_T *rettv)
3517{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003518 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003519 listitem_T *item;
3520 typval_T argv[MAX_FUNC_ARGS + 1];
3521 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003522 int r = 0;
3523
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003524 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003525 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003526 {
3527 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3528 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003529 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003530 break;
3531 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003532 // Make a copy of each argument. This is needed to be able to set
3533 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003534 copy_tv(&item->li_tv, &argv[argc++]);
3535 }
3536
3537 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003538 {
3539 funcexe_T funcexe;
3540
Bram Moolenaara80faa82020-04-12 19:37:17 +02003541 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003542 funcexe.fe_firstline = curwin->w_cursor.lnum;
3543 funcexe.fe_lastline = curwin->w_cursor.lnum;
3544 funcexe.fe_evaluate = TRUE;
3545 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003546 if (partial != NULL)
3547 {
3548 funcexe.fe_object = partial->pt_obj;
3549 if (funcexe.fe_object != NULL)
3550 ++funcexe.fe_object->obj_refcount;
3551 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003552 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003553 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3554 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003555
Bram Moolenaare38eab22019-12-05 21:50:01 +01003556 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003557 while (argc > 0)
3558 clear_tv(&argv[--argc]);
3559
3560 return r;
3561}
3562
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003563static int callback_depth = 0;
3564
3565 int
3566get_callback_depth(void)
3567{
3568 return callback_depth;
3569}
3570
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003571/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003572 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003573 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003574 */
3575 int
3576call_callback(
3577 callback_T *callback,
3578 int len, // length of "name" or -1 to use strlen()
3579 typval_T *rettv, // return value goes here
3580 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003581 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003582 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003583{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003584 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003585 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003586
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003587 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3588 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003589
Christian Brabandt20764632023-11-12 16:59:29 +01003590 if (callback_depth > MAX_CALLBACK_DEPTH)
Christian Brabandt47510f32023-10-15 09:56:16 +02003591 {
3592 emsg(_(e_command_too_recursive));
3593 return FAIL;
3594 }
3595
Bram Moolenaara80faa82020-04-12 19:37:17 +02003596 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003597 funcexe.fe_evaluate = TRUE;
3598 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003599 if (callback->cb_partial != NULL)
3600 {
3601 funcexe.fe_object = callback->cb_partial->pt_obj;
3602 if (funcexe.fe_object != NULL)
3603 ++funcexe.fe_object->obj_refcount;
3604 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003605 ++callback_depth;
3606 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3607 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003608
3609 // When a :def function was called that uses :try an error would be turned
3610 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003611 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003612 {
3613 need_rethrow = FALSE;
3614 handle_did_throw();
3615 }
3616
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003617 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003618}
3619
3620/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003621 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003622 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003623 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3624 */
3625 varnumber_T
3626call_callback_retnr(
3627 callback_T *callback,
3628 int argcount, // number of "argvars"
3629 typval_T *argvars) // vars for arguments, must have "argcount"
3630 // PLUS ONE elements!
3631{
3632 typval_T rettv;
3633 varnumber_T retval;
3634
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003635 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003636 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003637
3638 retval = tv_get_number_chk(&rettv, NULL);
3639 clear_tv(&rettv);
3640 return retval;
3641}
3642
3643/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 * Give an error message for the result of a function.
3645 * Nothing if "error" is FCERR_NONE.
3646 */
3647 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003648user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649{
3650 switch (error)
3651 {
3652 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003653 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003654 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003655 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003656 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 break;
3658 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003659 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 break;
3661 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003662 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 break;
3664 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003665 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 break;
3667 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003668 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 break;
3670 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003671 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 break;
3673 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003674 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3675 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003677 case FCERR_OTHER:
3678 case FCERR_FAILED:
3679 // assume the error message was already given
3680 break;
3681 case FCERR_NONE:
3682 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 }
3684}
3685
3686/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003687 * Check the argument types "argvars[argcount]" for "name" using the
3688 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3689 * is already included in "argvars[]".
3690 * Will do nothing if "funcexe->fe_check_type" is NULL or
3691 * "funcexe->fe_evaluate" is FALSE;
3692 * Returns an FCERR_ value.
3693 */
3694 static funcerror_T
3695may_check_argument_types(
3696 funcexe_T *funcexe,
3697 typval_T *argvars,
3698 int argcount,
3699 int base_included,
3700 char_u *name)
3701{
3702 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3703 {
3704 // Check that the argument types are OK for the types of the funcref.
3705 if (check_argument_types(funcexe->fe_check_type,
3706 argvars, argcount,
3707 base_included ? NULL : funcexe->fe_basetv,
3708 name) == FAIL)
3709 return FCERR_OTHER;
3710 }
3711 return FCERR_NONE;
3712}
3713
3714/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003715 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003716 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003717 * Return FAIL when the function can't be called, OK otherwise.
3718 * Also returns OK when an error was encountered while executing the function.
3719 */
3720 int
3721call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003722 char_u *funcname, // name of the function
3723 int len, // length of "name" or -1 to use strlen()
3724 typval_T *rettv, // return value goes here
3725 int argcount_in, // number of "argvars"
3726 typval_T *argvars_in, // vars for arguments, must have "argcount"
3727 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003728 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003729{
3730 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003731 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003732 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003733 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003734 char_u fname_buf[FLEN_FIXED + 1];
3735 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003736 char_u *fname = NULL;
3737 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003738 int argcount = argcount_in;
3739 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003740 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003741 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003742 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003743 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003744 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003745 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003746 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003747 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003748
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003749 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3750 // even when call_func() returns FAIL.
3751 rettv->v_type = VAR_UNKNOWN;
3752
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003753 if (partial != NULL)
3754 fp = partial->pt_func;
3755 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003756 fp = funcexe->fe_ufunc;
3757
3758 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003759 {
3760 // Make a copy of the name, if it comes from a funcref variable it
3761 // could be changed or deleted in the called function.
3762 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3763 if (name == NULL)
3764 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003765
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003766 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3767 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003768
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003769 if (funcexe->fe_doesrange != NULL)
3770 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003771
3772 if (partial != NULL)
3773 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003774 // When the function has a partial with a dict and there is a dict
3775 // argument, use the dict argument. That is backwards compatible.
3776 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003777 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003778 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003779 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003780 {
3781 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003782 {
3783 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3784 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003785 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003786 goto theend;
3787 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003788 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003789 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003790 for (i = 0; i < argcount_in; ++i)
3791 argv[i + argv_clear] = argvars_in[i];
3792 argvars = argv;
3793 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003794
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003795 if (funcexe->fe_check_type != NULL
3796 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003797 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003798 // Now funcexe->fe_check_type is missing the added arguments,
3799 // make a copy of the type with the correction.
3800 check_type = *funcexe->fe_check_type;
3801 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003802 check_type.tt_args = check_type_args;
3803 CLEAR_FIELD(check_type_args);
3804 for (i = 0; i < check_type.tt_argcount; ++i)
3805 check_type_args[i + partial->pt_argc] =
3806 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003807 check_type.tt_argcount += partial->pt_argc;
3808 check_type.tt_min_argcount += partial->pt_argc;
3809 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003810 }
3811 }
3812
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003813 if (error == FCERR_NONE)
3814 // check the argument types if possible
3815 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3816 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003817
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003818 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003819 {
3820 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003821 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003822
Bram Moolenaar333894b2020-08-01 18:53:07 +02003823 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003824 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003825 {
3826 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003827 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003828 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003829
Bram Moolenaare38eab22019-12-05 21:50:01 +01003830 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003831 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003832 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003833
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003834 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003835 {
3836 /*
3837 * User defined function.
3838 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003839 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003840 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003841 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003842 if (fp != NULL && !is_global && in_vim9script()
3843 && func_requires_g_prefix(fp))
3844 // In Vim9 script g: is required to find a global
3845 // non-autoload function.
3846 fp = NULL;
3847 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003848
Bram Moolenaare38eab22019-12-05 21:50:01 +01003849 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003850 if (fp == NULL
3851 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003852 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003853 && !aborting())
3854 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003855 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003856 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003857 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003858 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003859 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3860 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003861 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003862 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003863 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003864 if (fp == NULL)
3865 {
3866 char_u *p = untrans_function_name(rfname);
3867
3868 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003869 // Don't do this if the name starts with "s:".
3870 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003871 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003872 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003873
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003874 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003875 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003876 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003877 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003878 int need_arg_check = FALSE;
3879 if (funcexe->fe_check_type == NULL)
3880 {
3881 funcexe->fe_check_type = fp->uf_func_type;
3882 need_arg_check = TRUE;
3883 }
3884
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003885 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003886 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003887 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003888 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003889 argv_clear, fp);
3890 need_arg_check = TRUE;
3891 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003892
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003893 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003894 {
3895 // Method call: base->Method()
3896 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003897 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003898 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003899 argvars = argv;
3900 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003901 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003902 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003903
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003904 // Check the argument types now that the function type and all
3905 // argument values are known, if not done above.
3906 if (need_arg_check)
3907 error = may_check_argument_types(funcexe, argvars, argcount,
3908 TRUE, (name != NULL) ? name : funcname);
3909 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3910 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003912 }
3913 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003914 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003915 {
3916 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003917 * expr->method(): Find the method name in the table, call its
3918 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003919 */
3920 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003921 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003922 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003923 else
3924 {
3925 /*
3926 * Find the function name in the table, call its implementation.
3927 */
3928 error = call_internal_func(fname, argcount, argvars, rettv);
3929 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003930
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003931 /*
3932 * The function call (or "FuncUndefined" autocommand sequence) might
3933 * have been aborted by an error, an interrupt, or an explicitly thrown
3934 * exception that has not been caught so far. This situation can be
3935 * tested for by calling aborting(). For an error in an internal
3936 * function or for the "E132" error in call_user_func(), however, the
3937 * throw point at which the "force_abort" flag (temporarily reset by
3938 * emsg()) is normally updated has not been reached yet. We need to
3939 * update that flag first to make aborting() reliable.
3940 */
3941 update_force_abort();
3942 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003943 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003944 ret = OK;
3945
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003946theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003947 /*
3948 * Report an error unless the argument evaluation or function call has been
3949 * cancelled due to an aborting error, an interrupt, or an exception.
3950 */
3951 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003952 user_func_error(error, (name != NULL) ? name : funcname,
3953 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003954
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003955 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003956 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003957 clear_tv(&argv[--argv_clear + argv_base]);
3958
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003959 vim_free(tofree);
3960 vim_free(name);
3961
3962 return ret;
3963}
3964
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003965/*
3966 * Call a function without arguments, partial or dict.
3967 * This is like call_func() when the call is only "FuncName()".
3968 * To be used by "expr" options.
3969 * Returns NOTDONE when the function could not be found.
3970 */
3971 int
3972call_simple_func(
3973 char_u *funcname, // name of the function
3974 int len, // length of "name" or -1 to use strlen()
3975 typval_T *rettv) // return value goes here
3976{
3977 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003978 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003979 char_u fname_buf[FLEN_FIXED + 1];
3980 char_u *tofree = NULL;
3981 char_u *name;
3982 char_u *fname;
3983 char_u *rfname;
3984 int is_global = FALSE;
3985 ufunc_T *fp;
3986
3987 rettv->v_type = VAR_NUMBER; // default rettv is number zero
3988 rettv->vval.v_number = 0;
3989
3990 // Make a copy of the name, an option can be changed in the function.
3991 name = vim_strnsave(funcname, len);
3992 if (name == NULL)
3993 return ret;
3994
3995 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3996
3997 // Skip "g:" before a function name.
3998 if (fname[0] == 'g' && fname[1] == ':')
3999 {
4000 is_global = TRUE;
4001 rfname = fname + 2;
4002 }
4003 else
4004 rfname = fname;
4005 fp = find_func(rfname, is_global);
4006 if (fp != NULL && !is_global && in_vim9script()
4007 && func_requires_g_prefix(fp))
4008 // In Vim9 script g: is required to find a global non-autoload
4009 // function.
4010 fp = NULL;
4011 if (fp == NULL)
4012 ret = NOTDONE;
4013 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4014 error = FCERR_DELETED;
4015 else if (fp != NULL)
4016 {
4017 typval_T argvars[1];
4018 funcexe_T funcexe;
4019
4020 argvars[0].v_type = VAR_UNKNOWN;
4021 CLEAR_FIELD(funcexe);
4022 funcexe.fe_evaluate = TRUE;
4023
4024 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4025 if (error == FCERR_NONE)
4026 ret = OK;
4027 }
4028
4029 user_func_error(error, name, FALSE);
4030 vim_free(tofree);
4031 vim_free(name);
4032
4033 return ret;
4034}
4035
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004036 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037printable_func_name(ufunc_T *fp)
4038{
4039 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4040}
4041
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004042/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004043 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4044 * TRUE. Otherwise return FALSE.
4045 */
4046 static int
4047function_list_modified(int prev_ht_changed)
4048{
4049 if (prev_ht_changed != func_hashtab.ht_changed)
4050 {
4051 emsg(_(e_function_list_was_modified));
4052 return TRUE;
4053 }
4054 return FALSE;
4055}
4056
4057/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004058 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004059 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004060 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004061list_func_head(ufunc_T *fp, int indent)
4062{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004063 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004064 int j;
4065
4066 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004067
4068 // a timer at the more prompt may have deleted the function
4069 if (function_list_modified(prev_ht_changed))
4070 return FAIL;
4071
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004072 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004073 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004074 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004075 msg_puts("def ");
4076 else
4077 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004079 msg_putchar('(');
4080 for (j = 0; j < fp->uf_args.ga_len; ++j)
4081 {
4082 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004083 msg_puts(", ");
4084 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 if (fp->uf_arg_types != NULL)
4086 {
4087 char *tofree;
4088
4089 msg_puts(": ");
4090 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4091 vim_free(tofree);
4092 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004093 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4094 {
4095 msg_puts(" = ");
4096 msg_puts(((char **)(fp->uf_def_args.ga_data))
4097 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4098 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004099 }
4100 if (fp->uf_varargs)
4101 {
4102 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004103 msg_puts(", ");
4104 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 if (fp->uf_va_name != NULL)
4107 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004108 if (!fp->uf_varargs)
4109 {
4110 if (j)
4111 msg_puts(", ");
4112 msg_puts("...");
4113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004115 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 {
4117 char *tofree;
4118
4119 msg_puts(": ");
4120 msg_puts(type_name(fp->uf_va_type, &tofree));
4121 vim_free(tofree);
4122 }
4123 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004124 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004125
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004126 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004127 {
4128 if (fp->uf_ret_type != &t_void)
4129 {
4130 char *tofree;
4131
4132 msg_puts(": ");
4133 msg_puts(type_name(fp->uf_ret_type, &tofree));
4134 vim_free(tofree);
4135 }
4136 }
4137 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004138 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004139 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004140 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004141 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004142 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004143 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004144 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004145 msg_clr_eos();
4146 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004147 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004148
4149 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004150}
4151
4152/*
4153 * Get a function name, translating "<SID>" and "<SNR>".
4154 * Also handles a Funcref in a List or Dictionary.
4155 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004156 * Set "*is_global" to TRUE when the function must be global, unless
4157 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004158 * flags:
4159 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004160 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004161 * TFN_QUIET: be quiet
4162 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004163 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004164 * Advances "pp" to just after the function name (if no error).
4165 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004166 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004167trans_function_name(
4168 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004169 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004170 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004171 int flags)
4172{
4173 return trans_function_name_ext(pp, is_global, skip, flags,
4174 NULL, NULL, NULL, NULL);
4175}
4176
4177/*
4178 * trans_function_name() with extra arguments.
4179 * "fdp", "partial", "type" and "ufunc" can be NULL.
4180 */
4181 char_u *
4182trans_function_name_ext(
4183 char_u **pp,
4184 int *is_global,
4185 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004186 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004187 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004188 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004189 type_T **type, // return: type of funcref
4190 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004191{
4192 char_u *name = NULL;
4193 char_u *start;
4194 char_u *end;
4195 int lead;
4196 char_u sid_buf[20];
4197 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004199 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004200 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004201 int vim9script = in_vim9script();
4202 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004203
4204 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004205 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004206 start = *pp;
4207
Bram Moolenaare38eab22019-12-05 21:50:01 +01004208 // Check for hard coded <SNR>: already translated function ID (from a user
4209 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004210 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4211 && (*pp)[2] == (int)KE_SNR)
4212 {
4213 *pp += 3;
4214 len = get_id_len(pp) + 3;
4215 return vim_strnsave(start, len);
4216 }
4217
Bram Moolenaare38eab22019-12-05 21:50:01 +01004218 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4219 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004220 lead = eval_fname_script(start);
4221 if (lead > 2)
4222 start += lead;
4223
Bram Moolenaare38eab22019-12-05 21:50:01 +01004224 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004225 end = get_lval(start, NULL, &lv, FALSE, skip,
4226 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004227 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004228 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004229 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004230 {
4231 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004232 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004233 goto theend;
4234 }
4235 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4236 {
4237 /*
4238 * Report an invalid expression in braces, unless the expression
4239 * evaluation has been cancelled due to an aborting error, an
4240 * interrupt, or an exception.
4241 */
4242 if (!aborting())
4243 {
4244 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004245 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004246 }
4247 else
4248 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4249 goto theend;
4250 }
4251
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004252 if (lv.ll_ufunc != NULL)
4253 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004254 if (ufunc != NULL)
4255 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004256 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004257 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004258 goto theend;
4259 }
4260
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004261 if (lv.ll_tv != NULL)
4262 {
4263 if (fdp != NULL)
4264 {
4265 fdp->fd_dict = lv.ll_dict;
4266 fdp->fd_newkey = lv.ll_newkey;
4267 lv.ll_newkey = NULL;
4268 fdp->fd_di = lv.ll_di;
4269 }
4270 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4271 {
4272 name = vim_strsave(lv.ll_tv->vval.v_string);
4273 *pp = end;
4274 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004275 else if (lv.ll_tv->v_type == VAR_CLASS
4276 && lv.ll_tv->vval.v_class != NULL)
4277 {
4278 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4279 *pp = end;
4280 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004281 else if (lv.ll_tv->v_type == VAR_PARTIAL
4282 && lv.ll_tv->vval.v_partial != NULL)
4283 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004284 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004285 *pp = end;
4286 if (partial != NULL)
4287 *partial = lv.ll_tv->vval.v_partial;
4288 }
4289 else
4290 {
4291 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4292 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004293 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004294 else
4295 *pp = end;
4296 name = NULL;
4297 }
4298 goto theend;
4299 }
4300
4301 if (lv.ll_name == NULL)
4302 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004303 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004304 *pp = end;
4305 goto theend;
4306 }
4307
Bram Moolenaare38eab22019-12-05 21:50:01 +01004308 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004309 if (lv.ll_exp_name != NULL)
4310 {
4311 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004312 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004313 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004314 if (name == lv.ll_exp_name)
4315 name = NULL;
4316 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004317 else if (lv.ll_sid > 0)
4318 {
4319 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4320 int cc = *lv.ll_name_end;
4321
4322 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4323 *lv.ll_name_end = NUL;
4324 if (si->sn_autoload_prefix != NULL)
4325 {
4326 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4327 }
4328 else
4329 {
4330 sid_buf[0] = K_SPECIAL;
4331 sid_buf[1] = KS_EXTRA;
4332 sid_buf[2] = (int)KE_SNR;
4333 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004334 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004335 name = concat_str(sid_buf, lv.ll_name);
4336 }
4337 *lv.ll_name_end = cc;
4338 *pp = end;
4339 goto theend;
4340 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004341 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004342 {
4343 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004344 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004345 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004346 if (name == *pp)
4347 name = NULL;
4348 }
4349 if (name != NULL)
4350 {
4351 name = vim_strsave(name);
4352 *pp = end;
4353 if (STRNCMP(name, "<SNR>", 5) == 0)
4354 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004355 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004356 name[0] = K_SPECIAL;
4357 name[1] = KS_EXTRA;
4358 name[2] = (int)KE_SNR;
4359 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4360 }
4361 goto theend;
4362 }
4363
4364 if (lv.ll_exp_name != NULL)
4365 {
4366 len = (int)STRLEN(lv.ll_exp_name);
4367 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4368 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4369 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004370 // When there was "s:" already or the name expanded to get a
4371 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004372 lv.ll_name += 2;
4373 len -= 2;
4374 lead = 2;
4375 }
4376 }
4377 else
4378 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004379 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004380 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004381 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004382 if (lv.ll_name[0] == 'g')
4383 {
4384 if (is_global != NULL)
4385 {
4386 *is_global = TRUE;
4387 }
4388 else
4389 {
4390 // dropping "g:" without setting "is_global" won't work in
4391 // Vim9script, put it back later
4392 prefix_g = TRUE;
4393 extra = 2;
4394 }
4395 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004396 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004397 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004398 len = (int)(end - lv.ll_name);
4399 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004400 if (len <= 0)
4401 {
4402 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004403 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004404 goto theend;
4405 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004406
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004407 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004408 // starts with a lower case character: dict.func(). Or when in a class.
4409 vim9_local = ASCII_ISUPPER(*start) && vim9script
4410 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004412 /*
4413 * Copy the function name to allocated memory.
4414 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4415 * Accept <SNR>123_name() outside a script.
4416 */
4417 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004418 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004419 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004420 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004421 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004422 {
Kota Kato948a3892022-08-16 16:09:59 +01004423 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4424 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004425 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004426 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004427 goto theend;
4428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004430 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004431 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004433 || eval_fname_sid(*pp))
4434 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004436 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004437 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004438 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004439 goto theend;
4440 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004441 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004442 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 extra = 3 + (int)STRLEN(sid_buf);
4444 else
4445 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004446 }
4447 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004448 // The function name must start with an upper case letter (unless it is a
4449 // Vim9 class new() function or a Vim9 class private method)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004450 else if (!(flags & TFN_INT)
4451 && (builtin_function(lv.ll_name, len)
4452 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004453 && !((flags & TFN_IN_CLASS)
4454 && (STRNCMP(lv.ll_name, "new", 3) == 0
4455 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004456 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004457 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004458 : e_function_name_must_start_with_capital_or_s_str),
4459 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004460 goto theend;
4461 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004462 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004463 {
4464 char_u *cp = vim_strchr(lv.ll_name, ':');
4465
4466 if (cp != NULL && cp < end)
4467 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004468 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004469 goto theend;
4470 }
4471 }
4472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004474 if (name != NULL)
4475 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004476 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004477 {
4478 name[0] = K_SPECIAL;
4479 name[1] = KS_EXTRA;
4480 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004481 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004482 STRCPY(name + 3, sid_buf);
4483 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004484 else if (prefix_g)
4485 {
4486 name[0] = 'g';
4487 name[1] = ':';
4488 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4490 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004491 }
4492 *pp = end;
4493
4494theend:
4495 clear_lval(&lv);
4496 return name;
4497}
4498
4499/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004500 * Assuming "name" is the result of trans_function_name() and it was prefixed
4501 * to use the script-local name, return the unmodified name (points into
4502 * "name"). Otherwise return NULL.
4503 * This can be used to first search for a script-local function and fall back
4504 * to the global function if not found.
4505 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004506 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004507untrans_function_name(char_u *name)
4508{
4509 char_u *p;
4510
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004511 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004512 {
4513 p = vim_strchr(name, '_');
4514 if (p != NULL)
4515 return p + 1;
4516 }
4517 return NULL;
4518}
4519
4520/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004521 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4522 * current script ID and returns the expanded function name. The caller should
4523 * free the returned name. If not called from a script context or the function
4524 * name doesn't start with these prefixes, then returns NULL.
4525 * This doesn't check whether the script-local function exists or not.
4526 */
4527 char_u *
4528get_scriptlocal_funcname(char_u *funcname)
4529{
4530 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004531 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004532 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004533 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004534
4535 if (funcname == NULL)
4536 return NULL;
4537
4538 if (STRNCMP(funcname, "s:", 2) != 0
4539 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004540 {
4541 ufunc_T *ufunc;
4542
4543 // The function name does not have a script-local prefix. Try finding
4544 // it when in a Vim9 script and there is no "g:" prefix.
4545 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4546 return NULL;
4547 ufunc = find_func(funcname, FALSE);
4548 if (ufunc == NULL || func_is_global(ufunc)
4549 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4550 return NULL;
4551 ++p;
4552 off = 0;
4553 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004554 else
4555 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004556
4557 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4558 {
4559 emsg(_(e_using_sid_not_in_script_context));
4560 return NULL;
4561 }
4562 // Expand s: prefix into <SNR>nr_<name>
4563 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4564 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004565 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004566 if (newname == NULL)
4567 return NULL;
4568 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004569 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004570
4571 return newname;
4572}
4573
4574/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004575 * Return script-local "fname" with the 3-byte sequence replaced by
4576 * printable <SNR> in allocated memory.
4577 */
4578 char_u *
4579alloc_printable_func_name(char_u *fname)
4580{
4581 char_u *n = alloc(STRLEN(fname + 3) + 6);
4582
4583 if (n != NULL)
4584 {
4585 STRCPY(n, "<SNR>");
4586 STRCPY(n + 5, fname + 3);
4587 }
4588 return n;
4589}
4590
4591/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004592 * Call trans_function_name(), except that a lambda is returned as-is.
4593 * Returns the name in allocated memory.
4594 */
4595 char_u *
4596save_function_name(
4597 char_u **name,
4598 int *is_global,
4599 int skip,
4600 int flags,
4601 funcdict_T *fudi)
4602{
4603 char_u *p = *name;
4604 char_u *saved;
4605
4606 if (STRNCMP(p, "<lambda>", 8) == 0)
4607 {
4608 p += 8;
4609 (void)getdigits(&p);
4610 saved = vim_strnsave(*name, p - *name);
4611 if (fudi != NULL)
4612 CLEAR_POINTER(fudi);
4613 }
4614 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004615 saved = trans_function_name_ext(&p, is_global, skip,
4616 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004617 *name = p;
4618 return saved;
4619}
4620
4621/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004622 * List functions. When "regmatch" is NULL all of then.
4623 * Otherwise functions matching "regmatch".
4624 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004625 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004626list_functions(regmatch_T *regmatch)
4627{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004628 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004629 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004630 hashitem_T *hi;
4631
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004632 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004633 {
4634 if (!HASHITEM_EMPTY(hi))
4635 {
4636 ufunc_T *fp = HI2UF(hi);
4637
4638 --todo;
4639 if ((fp->uf_flags & FC_DEAD) == 0
4640 && (regmatch == NULL
4641 ? !message_filtered(fp->uf_name)
4642 && !func_name_refcount(fp->uf_name)
4643 : !isdigit(*fp->uf_name)
4644 && vim_regexec(regmatch, fp->uf_name, 0)))
4645 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004646 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004647 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004648 if (function_list_modified(prev_ht_changed))
4649 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004650 }
4651 }
4652 }
4653}
4654
4655/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004656 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004657 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4658 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004659 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004660 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4661 * With CF_INTERFACE the function is define inside an interface, only the
4662 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004663 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004664 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004665 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004666define_function(
4667 exarg_T *eap,
4668 char_u *name_arg,
4669 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004670 int class_flags,
4671 ocmember_T *obj_members,
4672 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004673{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004674 int j;
4675 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004676 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004677 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004678 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004679 char_u *p;
4680 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004681 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004682 char_u *line_arg = NULL;
4683 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004685 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004686 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004687 garray_T newlines;
4688 int varargs = FALSE;
4689 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004691 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004692 int fp_allocated = FALSE;
4693 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004694 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004695 dictitem_T *v;
4696 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004697 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004698 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004699 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004700 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004701 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004702 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004703
4704 /*
4705 * ":function" without argument: list functions.
4706 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004707 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004708 {
4709 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004710 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004711 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004712 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004713 }
4714
4715 /*
4716 * ":function /pat": list functions matching pattern.
4717 */
4718 if (*eap->arg == '/')
4719 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004720 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004721 if (!eap->skip)
4722 {
4723 regmatch_T regmatch;
4724
4725 c = *p;
4726 *p = NUL;
4727 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4728 *p = c;
4729 if (regmatch.regprog != NULL)
4730 {
4731 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004732 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004733 vim_regfree(regmatch.regprog);
4734 }
4735 }
4736 if (*p == '/')
4737 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004738 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004739 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004740 }
4741
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 ga_init(&newargs);
4743 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004744 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745 ga_init(&default_args);
4746
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004747 /*
4748 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004749 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004750 * "name" == func, "fudi.fd_dict" == NULL
4751 * dict.func new dictionary entry
4752 * "name" == NULL, "fudi.fd_dict" set,
4753 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4754 * dict.func existing dict entry with a Funcref
4755 * "name" == func, "fudi.fd_dict" set,
4756 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4757 * dict.func existing dict entry that's not a Funcref
4758 * "name" == NULL, "fudi.fd_dict" set,
4759 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4760 * s:func script-local function name
4761 * g:func global function name, same as "func"
4762 */
4763 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004764 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004765 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004766 // nested function, argument is (args).
4767 paren = TRUE;
4768 CLEAR_FIELD(fudi);
4769 }
4770 else
4771 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004772 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004773 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004774 if (p[0] == 's' && p[1] == ':')
4775 {
4776 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4777 return NULL;
4778 }
4779 p = to_name_end(p, TRUE);
4780 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4781 {
4782 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4783 eap->arg);
4784 return NULL;
4785 }
4786 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004787 }
4788
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004789 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004790 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004791 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004792 paren = (vim_strchr(p, '(') != NULL);
4793 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004794 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004795 /*
4796 * Return on an invalid expression in braces, unless the expression
4797 * evaluation has been cancelled due to an aborting error, an
4798 * interrupt, or an exception.
4799 */
4800 if (!aborting())
4801 {
4802 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004803 semsg(_(e_key_not_present_in_dictionary_str),
4804 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004805 vim_free(fudi.fd_newkey);
4806 return NULL;
4807 }
4808 else
4809 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004810 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004811
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004812 // For "export def FuncName()" in an autoload script the function name
4813 // is stored with the legacy autoload name "dir#script#FuncName" so
4814 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004815 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004816 {
4817 char_u *prefixed = may_prefix_autoload(name);
4818
4819 if (prefixed != NULL && prefixed != name)
4820 {
4821 vim_free(name);
4822 name = prefixed;
4823 }
4824 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004825 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004826 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004827 {
4828 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4829 goto ret_free;
4830 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004831 }
4832
Bram Moolenaare38eab22019-12-05 21:50:01 +01004833 // An error in a function call during evaluation of an expression in magic
4834 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004835 saved_did_emsg = did_emsg;
4836 did_emsg = FALSE;
4837
4838 /*
4839 * ":function func" with only function name: list function.
4840 */
4841 if (!paren)
4842 {
4843 if (!ends_excmd(*skipwhite(p)))
4844 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004845 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004846 goto ret_free;
4847 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004848 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004849 if (eap->nextcmd != NULL)
4850 *p = NUL;
4851 if (!eap->skip && !got_int)
4852 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004853 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004854 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4855 {
4856 char_u *up = untrans_function_name(name);
4857
4858 // With Vim9 script the name was made script-local, if not
4859 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004860 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004861 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004862 }
4863
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004864 if (fp != NULL)
4865 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004866 // Check no function was added or removed from a timer, e.g. at
4867 // the more prompt. "fp" may then be invalid.
4868 int prev_ht_changed = func_hashtab.ht_changed;
4869
4870 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004871 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004872 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4873 {
4874 if (FUNCLINE(fp, j) == NULL)
4875 continue;
4876 msg_putchar('\n');
4877 msg_outnum((long)(j + 1));
4878 if (j < 9)
4879 msg_putchar(' ');
4880 if (j < 99)
4881 msg_putchar(' ');
4882 if (function_list_modified(prev_ht_changed))
4883 break;
4884 msg_prt_line(FUNCLINE(fp, j), FALSE);
4885 out_flush(); // show a line at a time
4886 ui_breakcheck();
4887 }
4888 if (!got_int)
4889 {
4890 msg_putchar('\n');
4891 if (!function_list_modified(prev_ht_changed))
4892 {
4893 if (fp->uf_def_status != UF_NOT_COMPILED)
4894 msg_puts(" enddef");
4895 else
4896 msg_puts(" endfunction");
4897 }
4898 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004899 }
4900 }
4901 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004902 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004903 }
4904 goto ret_free;
4905 }
4906
4907 /*
4908 * ":function name(arg1, arg2)" Define function.
4909 */
4910 p = skipwhite(p);
4911 if (*p != '(')
4912 {
4913 if (!eap->skip)
4914 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004915 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004916 goto ret_free;
4917 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004918 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004919 if (vim_strchr(p, '(') != NULL)
4920 p = vim_strchr(p, '(');
4921 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004922
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004923 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4924 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004925 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004926 goto ret_free;
4927 }
4928
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004929 // In Vim9 script only global functions can be redefined.
4930 if (vim9script && eap->forceit && !is_global)
4931 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004932 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004933 goto ret_free;
4934 }
4935
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004936 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004937
Bram Moolenaar04b12692020-05-04 23:24:44 +02004938 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004939 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004940 // Check the name of the function. Unless it's a dictionary function
4941 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004942 if (name != NULL)
4943 arg = name;
4944 else
4945 arg = fudi.fd_newkey;
4946 if (arg != NULL && (fudi.fd_di == NULL
4947 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4948 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4949 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004950 char_u *name_base = arg;
4951 int i;
4952
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004953 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004954 {
4955 name_base = vim_strchr(arg, '_');
4956 if (name_base == NULL)
4957 name_base = arg + 3;
4958 else
4959 ++name_base;
4960 }
4961 for (i = 0; name_base[i] != NUL && (i == 0
4962 ? eval_isnamec1(name_base[i])
4963 : eval_isnamec(name_base[i])); ++i)
4964 ;
4965 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004966 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004967
4968 // In Vim9 script a function cannot have the same name as a
4969 // variable.
4970 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004971 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4972 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004973 + EVAL_VAR_NO_FUNC) == OK)
4974 {
4975 semsg(_(e_redefining_script_item_str), name_base);
4976 goto ret_free;
4977 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004978 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004979 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004980 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004981 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004982 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004983 goto ret_free;
4984 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004985 }
4986
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004987 // This may get more lines and make the pointers into the first line
4988 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004989 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00004991 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02004992 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004993 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00004994 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004995 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004996 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004999 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005001 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005002 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005003 if (*p != ':')
5004 {
5005 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5006 p = skipwhite(p);
5007 }
5008 else if (!IS_WHITE_OR_NUL(p[1]))
5009 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005011 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005013 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005014 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005015 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005017 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005019 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005020 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005021 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005022 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005023 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005024 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005026 else
5027 // find extra arguments "range", "dict", "abort" and "closure"
5028 for (;;)
5029 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005030 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 p = skipwhite(p);
5032 if (STRNCMP(p, "range", 5) == 0)
5033 {
5034 flags |= FC_RANGE;
5035 p += 5;
5036 }
5037 else if (STRNCMP(p, "dict", 4) == 0)
5038 {
5039 flags |= FC_DICT;
5040 p += 4;
5041 }
5042 else if (STRNCMP(p, "abort", 5) == 0)
5043 {
5044 flags |= FC_ABORT;
5045 p += 5;
5046 }
5047 else if (STRNCMP(p, "closure", 7) == 0)
5048 {
5049 flags |= FC_CLOSURE;
5050 p += 7;
5051 if (current_funccal == NULL)
5052 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005053 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054 name == NULL ? (char_u *)"" : name);
5055 goto erret;
5056 }
5057 }
5058 else
5059 break;
5060 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005061
Bram Moolenaare38eab22019-12-05 21:50:01 +01005062 // When there is a line break use what follows for the function body.
5063 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005064 if (*p == '\n')
5065 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005066 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005067 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5068 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005069 && !(VIM_ISWHITE(*whitep) && *p == '#'
5070 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005071 && !eap->skip
5072 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005073 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005074
5075 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5077 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005078 */
5079 if (KeyTyped)
5080 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005081 // Check if the function already exists, don't let the user type the
5082 // whole function before telling him it doesn't work! For a script we
5083 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005084 if (!eap->skip && !eap->forceit)
5085 {
5086 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005087 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005088 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005089 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005090 }
5091
5092 if (!eap->skip && did_emsg)
5093 goto erret;
5094
Bram Moolenaare38eab22019-12-05 21:50:01 +01005095 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005096 cmdline_row = msg_row;
5097 }
5098
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005099 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005100 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005101
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005102 // Do not define the function when getting the body fails and when
5103 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005104 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005105 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005106 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5107 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005108 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005109 goto erret;
5110
5111 /*
5112 * If there are no errors, add the function
5113 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005114 if (fudi.fd_dict != NULL)
5115 {
5116 char numbuf[20];
5117
5118 fp = NULL;
5119 if (fudi.fd_newkey == NULL && !eap->forceit)
5120 {
5121 emsg(_(e_dictionary_entry_already_exists));
5122 goto erret;
5123 }
5124 if (fudi.fd_di == NULL)
5125 {
5126 // Can't add a function to a locked dictionary
5127 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5128 goto erret;
5129 }
5130 // Can't change an existing function if it is locked
5131 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5132 goto erret;
5133
5134 // Give the function a sequential number. Can only be used with a
5135 // Funcref!
5136 vim_free(name);
5137 sprintf(numbuf, "%d", ++func_nr);
5138 name = vim_strsave((char_u *)numbuf);
5139 if (name == NULL)
5140 goto erret;
5141 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005142 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005143 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005145 char_u *find_name = name;
5146 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005147 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005149 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005150 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005151 var_conflict = TRUE;
5152
5153 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5154 {
5155 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5156
5157 if (si->sn_autoload_prefix != NULL)
5158 {
5159 if (is_export)
5160 {
5161 find_name = name + STRLEN(si->sn_autoload_prefix);
5162 v = find_var(find_name, &ht, TRUE);
5163 if (v != NULL)
5164 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005165 // Only check if the function already exists in the script,
5166 // global functions can be shadowed.
5167 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005168 }
5169 else
5170 {
5171 char_u *prefixed = may_prefix_autoload(name);
5172
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005173 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005174 {
5175 v = find_var(prefixed, &ht, TRUE);
5176 if (v != NULL)
5177 var_conflict = TRUE;
5178 vim_free(prefixed);
5179 }
5180 }
5181 }
5182 }
5183 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005184 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005185 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005186 goto erret;
5187 }
5188
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005189 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005190 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005191 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005192 char_u *uname = untrans_function_name(name);
5193
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005194 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005195 }
5196
5197 if (fp != NULL || import != NULL)
5198 {
5199 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005201 // Function can be replaced with "function!" and when sourcing the
5202 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005203 // A name that is used by an import can not be overruled.
5204 if (import != NULL
5205 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005206 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005207 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005208 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005209 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005210 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005211 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005212 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005213 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005214 goto erret;
5215 }
5216 if (fp->uf_calls > 0)
5217 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005218 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005219 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005220 goto erret;
5221 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005222 if (fp->uf_refcount > 1)
5223 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005224 // This function is referenced somewhere, don't redefine it but
5225 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005226 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005227 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005228 fp = NULL;
5229 overwrite = TRUE;
5230 }
5231 else
5232 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005233 char_u *exp_name = fp->uf_name_exp;
5234
5235 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005236 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005237 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005238 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005239 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005240 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005241#ifdef FEAT_PROFILE
5242 fp->uf_profiling = FALSE;
5243 fp->uf_prof_initialized = FALSE;
5244#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005245 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005246 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005247 }
5248 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005249
5250 if (fp == NULL)
5251 {
5252 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5253 {
5254 int slen, plen;
5255 char_u *scriptname;
5256
Bram Moolenaare38eab22019-12-05 21:50:01 +01005257 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005258 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005259 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005260 {
5261 scriptname = autoload_name(name);
5262 if (scriptname != NULL)
5263 {
5264 p = vim_strchr(scriptname, '/');
5265 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005266 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005267 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005268 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005269 j = OK;
5270 vim_free(scriptname);
5271 }
5272 }
5273 if (j == FAIL)
5274 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005275 linenr_T save_lnum = SOURCING_LNUM;
5276
5277 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005278 semsg(_(e_function_name_does_not_match_script_file_name_str),
5279 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005280 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005281 goto erret;
5282 }
5283 }
5284
Bram Moolenaare01e5212023-01-08 20:31:18 +00005285 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005286 if (fp == NULL)
5287 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005288 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005289
5290 if (fudi.fd_dict != NULL)
5291 {
5292 if (fudi.fd_di == NULL)
5293 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005294 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005295 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5296 if (fudi.fd_di == NULL)
5297 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005298 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005299 goto erret;
5300 }
5301 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5302 {
5303 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005304 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005305 goto erret;
5306 }
5307 }
5308 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005309 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005310 clear_tv(&fudi.fd_di->di_tv);
5311 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005312 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005313
Bram Moolenaare38eab22019-12-05 21:50:01 +01005314 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005315 flags |= FC_DICT;
5316 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005317 }
5318 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005319 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005320 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005321 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005322
5323 if (eap->cmdidx == CMD_def)
5324 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005325 int lnum_save = SOURCING_LNUM;
5326 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005327
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005328 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005329
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005330 // error messages are for the first function line
5331 SOURCING_LNUM = sourcing_lnum_top;
5332
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005333 // The function may use script variables from the context.
5334 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005335
h-eastb895b0f2023-09-24 15:46:31 +02005336 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5337 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005339 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005340 free_fp = fp_allocated;
5341 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005343 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005344
5345 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005346 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005347 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005348 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005349 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005350 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005351 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005352 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005353 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005354 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005355 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005356
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005357 if (fp_allocated)
5358 {
5359 // insert the new function in the function list
5360 set_ufunc_name(fp, name);
5361 if (overwrite)
5362 {
5363 hi = hash_find(&func_hashtab, name);
5364 hi->hi_key = UF2HIKEY(fp);
5365 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005366 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005367 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005368 {
5369 free_fp = TRUE;
5370 goto erret;
5371 }
5372 fp->uf_refcount = 1;
5373 }
5374
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005375 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005376 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005377 if ((flags & FC_CLOSURE) != 0)
5378 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005379 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005380 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005381 }
5382 else
5383 fp->uf_scoped = NULL;
5384
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005385#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005386 if (prof_def_func())
5387 func_do_profile(fp);
5388#endif
5389 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005390 if (sandbox)
5391 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005392 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005393 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005394 fp->uf_flags = flags;
5395 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005396 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005397 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005398 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005399 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005400 if (is_export)
5401 {
5402 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005403 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005404 is_export = FALSE;
5405 }
5406
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005407 if (eap->cmdidx == CMD_def)
5408 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005409 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5410 // :func does not use Vim9 script syntax, even in a Vim9 script file
5411 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005412
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005413 goto ret_free;
5414
5415erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005416 if (fp != NULL)
5417 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005418 // these were set to "newargs" and "default_args", which are cleared
5419 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005420 ga_init(&fp->uf_args);
5421 ga_init(&fp->uf_def_args);
5422 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005423errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005424 ga_clear_strings(&newargs);
5425 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005426 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005427 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005428 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005429 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005430 VIM_CLEAR(fp->uf_va_name);
5431 clear_type_list(&fp->uf_type_list);
5432 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005433 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005434 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005435ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005436 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005437 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005438 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005439 if (name != name_arg)
5440 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005441 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005442 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005443
5444 return fp;
5445}
5446
5447/*
5448 * ":function"
5449 */
5450 void
5451ex_function(exarg_T *eap)
5452{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005453 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005454
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005455 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005456 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005457 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005458}
5459
5460/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005461 * Find a function by name, including "<lambda>123".
5462 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005463 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005464 * Return NULL if not found.
5465 */
5466 ufunc_T *
5467find_func_by_name(char_u *name, compiletype_T *compile_type)
5468{
5469 char_u *arg = name;
5470 char_u *fname;
5471 ufunc_T *ufunc;
5472 int is_global = FALSE;
5473
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005474 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5475 {
5476 *compile_type = CT_PROFILE;
5477 arg = skipwhite(arg + 7);
5478 }
5479 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5480 {
5481 *compile_type = CT_DEBUG;
5482 arg = skipwhite(arg + 5);
5483 }
5484
5485 if (STRNCMP(arg, "<lambda>", 8) == 0)
5486 {
5487 arg += 8;
5488 (void)getdigits(&arg);
5489 fname = vim_strnsave(name, arg - name);
5490 }
5491 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005492 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005493 // First try finding a method in a class, trans_function_name() will
5494 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005495 ufunc = find_class_func(&arg);
5496 if (ufunc != NULL)
5497 return ufunc;
5498
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005499 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005500 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005501 NULL, NULL, NULL, &ufunc);
5502 if (ufunc != NULL)
5503 {
5504 vim_free(fname);
5505 return ufunc;
5506 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005507 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005508 if (fname == NULL)
5509 {
5510 semsg(_(e_invalid_argument_str), name);
5511 return NULL;
5512 }
5513 if (!ends_excmd2(name, arg))
5514 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005515 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005516 emsg(ex_errmsg(e_trailing_characters_str, arg));
5517 return NULL;
5518 }
5519
5520 ufunc = find_func(fname, is_global);
5521 if (ufunc == NULL)
5522 {
5523 char_u *p = untrans_function_name(fname);
5524
5525 if (p != NULL)
5526 // Try again without making it script-local.
5527 ufunc = find_func(p, FALSE);
5528 }
5529 vim_free(fname);
5530 if (ufunc == NULL)
5531 semsg(_(e_cannot_find_function_str), name);
5532 return ufunc;
5533}
5534
5535/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005536 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005537 * be compiled or the one specified by the argument.
5538 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005539 */
5540 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005541ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005542{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005543 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005544 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005545 compiletype_T compile_type = CT_NONE;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005546 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005547 if (ufunc != NULL)
5548 {
5549 if (func_needs_compiling(ufunc, compile_type))
5550 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5551 else
5552 smsg(_("Function %s does not need compiling"), eap->arg);
5553 }
5554 }
5555 else
5556 {
5557 long todo = (long)func_hashtab.ht_used;
5558 int changed = func_hashtab.ht_changed;
5559 hashitem_T *hi;
5560
5561 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5562 {
5563 if (!HASHITEM_EMPTY(hi))
5564 {
5565 --todo;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005566 ufunc_T *ufunc = HI2UF(hi);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005567 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5568 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5569 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005570 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005571 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5572
5573 if (func_hashtab.ht_changed != changed)
5574 {
5575 // a function has been added or removed, need to start
5576 // over
5577 todo = (long)func_hashtab.ht_used;
5578 changed = func_hashtab.ht_changed;
5579 hi = func_hashtab.ht_array;
5580 --hi;
5581 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005582 }
5583 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005584 }
5585 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005586}
5587
5588/*
5589 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5590 * Return 2 if "p" starts with "s:".
5591 * Return 0 otherwise.
5592 */
5593 int
5594eval_fname_script(char_u *p)
5595{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005596 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5597 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005598 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5599 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5600 return 5;
5601 if (p[0] == 's' && p[1] == ':')
5602 return 2;
5603 return 0;
5604}
5605
5606 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005607translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005608{
5609 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005610 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005611 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005612}
5613
5614/*
5615 * Return TRUE when "ufunc" has old-style "..." varargs
5616 * or named varargs "...name: type".
5617 */
5618 int
5619has_varargs(ufunc_T *ufunc)
5620{
5621 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005622}
5623
5624/*
5625 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005626 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005627 */
5628 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005629function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005630{
5631 char_u *nm = name;
5632 char_u *p;
5633 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005634 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005635 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005636
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005637 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5638 if (no_deref)
5639 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005640 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005641 nm = skipwhite(nm);
5642
Bram Moolenaare38eab22019-12-05 21:50:01 +01005643 // Only accept "funcname", "funcname ", "funcname (..." and
5644 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005645 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005646 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005647 vim_free(p);
5648 return n;
5649}
5650
Bram Moolenaar113e1072019-01-20 15:30:40 +01005651#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005652 char_u *
5653get_expanded_name(char_u *name, int check)
5654{
5655 char_u *nm = name;
5656 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005657 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005658
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005659 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005660
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005661 if (p != NULL && *nm == NUL
5662 && (!check || translated_function_exists(p, is_global)))
5663 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005664
5665 vim_free(p);
5666 return NULL;
5667}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005668#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005669
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005670/*
5671 * Function given to ExpandGeneric() to obtain the list of user defined
5672 * function names.
5673 */
5674 char_u *
5675get_user_func_name(expand_T *xp, int idx)
5676{
5677 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005678 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005679 static hashitem_T *hi;
5680 ufunc_T *fp;
5681
5682 if (idx == 0)
5683 {
5684 done = 0;
5685 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005686 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005687 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005688 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005689 {
5690 if (done++ > 0)
5691 ++hi;
5692 while (HASHITEM_EMPTY(hi))
5693 ++hi;
5694 fp = HI2UF(hi);
5695
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005696 // don't show dead, dict and lambda functions
5697 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005698 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005699 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005700
5701 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005702 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005703
5704 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005705 if (xp->xp_context != EXPAND_USER_FUNC
5706 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005707 {
5708 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005709 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005710 STRCAT(IObuff, ")");
5711 }
5712 return IObuff;
5713 }
5714 return NULL;
5715}
5716
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005717/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005718 * Make a copy of a function.
5719 * Intended to be used for a function defined on a base class that has a copy
5720 * on the child class.
5721 * The copy has uf_refcount set to one.
5722 * Returns NULL when out of memory.
5723 */
5724 ufunc_T *
5725copy_function(ufunc_T *fp)
5726{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005727 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005728 if (ufunc == NULL)
5729 return NULL;
5730
5731 // Most things can just be copied.
5732 *ufunc = *fp;
5733
5734 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5735 ufunc->uf_dfunc_idx = 0;
5736 ufunc->uf_class = NULL;
5737
5738 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5739 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5740
5741 if (ufunc->uf_arg_types != NULL)
5742 {
5743 // "uf_arg_types" is an allocated array, make a copy.
5744 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5745 if (at != NULL)
5746 {
5747 mch_memmove(at, ufunc->uf_arg_types,
5748 sizeof(type_T *) * ufunc->uf_args.ga_len);
5749 ufunc->uf_arg_types = at;
5750 }
5751 }
5752
5753 // TODO: how about the types themselves? they can be freed when the
5754 // original function is freed:
5755 // type_T **uf_arg_types;
5756 // type_T *uf_ret_type;
5757
Ernie Rael114ec812023-06-04 18:11:35 +01005758 // make uf_type_list empty
5759 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005760
5761 // TODO: partial_T *uf_partial;
5762
5763 if (ufunc->uf_va_name != NULL)
5764 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5765
5766 // TODO:
5767 // type_T *uf_va_type;
5768 // type_T *uf_func_type;
5769
5770 ufunc->uf_block_depth = 0;
5771 ufunc->uf_block_ids = NULL;
5772
5773 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5774
5775 ufunc->uf_refcount = 1;
5776 ufunc->uf_name_exp = NULL;
5777 STRCPY(ufunc->uf_name, fp->uf_name);
5778
5779 return ufunc;
5780}
5781
5782/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005783 * ":delfunction {name}"
5784 */
5785 void
5786ex_delfunction(exarg_T *eap)
5787{
5788 ufunc_T *fp = NULL;
5789 char_u *p;
5790 char_u *name;
5791 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005792 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005793
5794 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005795 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5796 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005797 vim_free(fudi.fd_newkey);
5798 if (name == NULL)
5799 {
5800 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005801 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005802 return;
5803 }
5804 if (!ends_excmd(*skipwhite(p)))
5805 {
5806 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005807 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005808 return;
5809 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005810 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005811 if (eap->nextcmd != NULL)
5812 *p = NUL;
5813
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005814 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005815 {
5816 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005817 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005818 vim_free(name);
5819 return;
5820 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005821 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005822 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005823 vim_free(name);
5824
5825 if (!eap->skip)
5826 {
5827 if (fp == NULL)
5828 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005829 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005830 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005831 return;
5832 }
5833 if (fp->uf_calls > 0)
5834 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005835 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005836 return;
5837 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005838 if (fp->uf_flags & FC_VIM9)
5839 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005840 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005841 return;
5842 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005843
5844 if (fudi.fd_dict != NULL)
5845 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005846 // Delete the dict item that refers to the function, it will
5847 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005848 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005849 }
5850 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005851 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005852 // A normal function (not a numbered function or lambda) has a
5853 // refcount of 1 for the entry in the hashtable. When deleting
5854 // it and the refcount is more than one, it should be kept.
5855 // A numbered function and lambda should be kept if the refcount is
5856 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005857 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005858 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005859 // Function is still referenced somewhere. Don't free it but
5860 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005861 if (func_remove(fp))
5862 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005863 }
5864 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005865 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005866 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005867 }
5868}
5869
5870/*
5871 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005872 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005873 */
5874 void
5875func_unref(char_u *name)
5876{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005877 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005878
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005879 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005880 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005881 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005882 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005883 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005884#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005885 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005886#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005887 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005888 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005889 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005890}
5891
5892/*
5893 * Unreference a Function: decrement the reference count and free it when it
5894 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005895 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005896 */
5897 void
5898func_ptr_unref(ufunc_T *fp)
5899{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005900 if (fp != NULL && (--fp->uf_refcount <= 0
5901 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5902 && fp->uf_partial->pt_refcount <= 1
5903 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005904 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005905 // Only delete it when it's not being used. Otherwise it's done
5906 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005907 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005908 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005909 }
5910}
5911
5912/*
5913 * Count a reference to a Function.
5914 */
5915 void
5916func_ref(char_u *name)
5917{
5918 ufunc_T *fp;
5919
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005920 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005921 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005922 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005923 if (fp != NULL)
5924 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005925 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005926 // Only give an error for a numbered function.
5927 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005928 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005929}
5930
5931/*
5932 * Count a reference to a Function.
5933 */
5934 void
5935func_ptr_ref(ufunc_T *fp)
5936{
5937 if (fp != NULL)
5938 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005939}
5940
5941/*
5942 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5943 * referenced from anywhere that is in use.
5944 */
5945 static int
5946can_free_funccal(funccall_T *fc, int copyID)
5947{
Bram Moolenaarca16c602022-09-06 18:57:08 +01005948 return (fc->fc_l_varlist.lv_copyID != copyID
5949 && fc->fc_l_vars.dv_copyID != copyID
5950 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005951 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005952}
5953
5954/*
5955 * ":return [expr]"
5956 */
5957 void
5958ex_return(exarg_T *eap)
5959{
5960 char_u *arg = eap->arg;
5961 typval_T rettv;
5962 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005963 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005964
5965 if (current_funccal == NULL)
5966 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005967 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005968 return;
5969 }
5970
Bram Moolenaar844fb642021-10-23 13:32:30 +01005971 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005972 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5973
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005974 if (eap->skip)
5975 ++emsg_skip;
5976
5977 eap->nextcmd = NULL;
5978 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005979 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005980 {
5981 if (!eap->skip)
5982 returning = do_return(eap, FALSE, TRUE, &rettv);
5983 else
5984 clear_tv(&rettv);
5985 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005986 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005987 else if (!eap->skip)
5988 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005989 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005990 update_force_abort();
5991
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005992 /*
5993 * Return unless the expression evaluation has been cancelled due to an
5994 * aborting error, an interrupt, or an exception.
5995 */
5996 if (!aborting())
5997 returning = do_return(eap, FALSE, TRUE, NULL);
5998 }
5999
Bram Moolenaare38eab22019-12-05 21:50:01 +01006000 // When skipping or the return gets pending, advance to the next command
6001 // in this line (!returning). Otherwise, ignore the rest of the line.
6002 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006003 if (returning)
6004 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006005 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006006 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006007
6008 if (eap->skip)
6009 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006010 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006011}
6012
zeertzjq5299c092023-04-12 20:48:16 +01006013/*
6014 * Lower level implementation of "call". Only called when not skipping.
6015 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006016 static int
6017ex_call_inner(
6018 exarg_T *eap,
6019 char_u *name,
6020 char_u **arg,
6021 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006022 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006023 evalarg_T *evalarg)
6024{
6025 linenr_T lnum;
6026 int doesrange;
6027 typval_T rettv;
6028 int failed = FALSE;
6029
zeertzjq5299c092023-04-12 20:48:16 +01006030 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006031 for ( ; lnum <= eap->line2; ++lnum)
6032 {
6033 funcexe_T funcexe;
6034
zeertzjq5299c092023-04-12 20:48:16 +01006035 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006036 {
6037 if (lnum > curbuf->b_ml.ml_line_count)
6038 {
6039 // If the function deleted lines or switched to another buffer
6040 // the line number may become invalid.
6041 emsg(_(e_invalid_range));
6042 break;
6043 }
6044 curwin->w_cursor.lnum = lnum;
6045 curwin->w_cursor.col = 0;
6046 curwin->w_cursor.coladd = 0;
6047 }
6048 *arg = startarg;
6049
6050 funcexe = *funcexe_init;
6051 funcexe.fe_doesrange = &doesrange;
6052 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6053 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6054 {
6055 failed = TRUE;
6056 break;
6057 }
6058 if (has_watchexpr())
6059 dbg_check_breakpoint(eap);
6060
6061 // Handle a function returning a Funcref, Dictionary or List.
6062 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006063 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006064 {
6065 failed = TRUE;
6066 break;
6067 }
6068
6069 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006070 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006071 break;
6072
6073 // Stop when immediately aborting on error, or when an interrupt
6074 // occurred or an exception was thrown but not caught.
6075 // get_func_tv() returned OK, so that the check for trailing
6076 // characters below is executed.
6077 if (aborting())
6078 break;
6079 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006080 return failed;
6081}
6082
6083/*
6084 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6085 * Returns FAIL or OK.
6086 */
6087 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006088ex_defer_inner(
6089 char_u *name,
6090 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006091 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006092 partial_T *partial,
6093 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006094{
6095 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006096 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006097 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006098 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006099
6100 if (current_funccal == NULL)
6101 {
6102 semsg(_(e_str_not_inside_function), "defer");
6103 return FAIL;
6104 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006105 if (partial != NULL)
6106 {
6107 if (partial->pt_dict != NULL)
6108 {
6109 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6110 return FAIL;
6111 }
6112 if (partial->pt_argc > 0)
6113 {
6114 int i;
6115
6116 partial_argc = partial->pt_argc;
6117 for (i = 0; i < partial_argc; ++i)
6118 copy_tv(&partial->pt_argv[i], &argvars[i]);
6119 }
6120 }
6121 r = get_func_arguments(arg, evalarg, FALSE,
6122 argvars + partial_argc, &argcount);
6123 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006124
6125 if (r == OK)
6126 {
6127 if (type != NULL)
6128 {
6129 // Check that the arguments are OK for the types of the funcref.
6130 r = check_argument_types(type, argvars, argcount, NULL, name);
6131 }
6132 else if (builtin_function(name, -1))
6133 {
6134 int idx = find_internal_func(name);
6135
6136 if (idx < 0)
6137 {
6138 emsg_funcname(e_unknown_function_str, name);
6139 r = FAIL;
6140 }
6141 else if (check_internal_func(idx, argcount) == -1)
6142 r = FAIL;
6143 }
6144 else
6145 {
6146 ufunc_T *ufunc = find_func(name, FALSE);
6147
6148 // we tolerate an unknown function here, it might be defined later
6149 if (ufunc != NULL)
6150 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006151 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006152 if (error != FCERR_UNKNOWN)
6153 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006154 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006155 r = FAIL;
6156 }
6157 }
6158 }
6159 }
6160
Bram Moolenaar86d87252022-09-05 21:21:25 +01006161 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006162 {
6163 while (--argcount >= 0)
6164 clear_tv(&argvars[argcount]);
6165 return FAIL;
6166 }
6167 return add_defer(name, argcount, argvars);
6168}
6169
6170/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006171 * Return TRUE if currently inside a function call.
6172 * Give an error message and return FALSE when not.
6173 */
6174 int
6175can_add_defer(void)
6176{
6177 if (!in_def_function() && get_current_funccal() == NULL)
6178 {
6179 semsg(_(e_str_not_inside_function), "defer");
6180 return FALSE;
6181 }
6182 return TRUE;
6183}
6184
6185/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006186 * Add a deferred call for "name" with arguments "argvars[argcount]".
6187 * Consumes "argvars[]".
6188 * Caller must check that in_def_function() returns TRUE or current_funccal is
6189 * not NULL.
6190 * Returns OK or FAIL.
6191 */
6192 int
6193add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6194{
6195 char_u *saved_name = vim_strsave(name);
6196 int argcount = argcount_arg;
6197 defer_T *dr;
6198 int ret = FAIL;
6199
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006200 if (saved_name == NULL)
6201 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006202 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006203 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006204 if (add_defer_function(saved_name, argcount, argvars) == OK)
6205 argcount = 0;
6206 }
6207 else
6208 {
6209 if (current_funccal->fc_defer.ga_itemsize == 0)
6210 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6211 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6212 goto theend;
6213 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6214 + current_funccal->fc_defer.ga_len++;
6215 dr->dr_name = saved_name;
6216 dr->dr_argcount = argcount;
6217 while (argcount > 0)
6218 {
6219 --argcount;
6220 dr->dr_argvars[argcount] = argvars[argcount];
6221 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006222 }
6223 ret = OK;
6224
6225theend:
6226 while (--argcount >= 0)
6227 clear_tv(&argvars[argcount]);
6228 return ret;
6229}
6230
6231/*
6232 * Invoked after a functions has finished: invoke ":defer" functions.
6233 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006234 static void
6235handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006236{
6237 int idx;
6238
Bram Moolenaar58779852022-09-06 18:31:14 +01006239 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006240 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006241 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006242
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006243 if (dr->dr_name == NULL)
6244 // already being called, can happen if function does ":qa"
6245 continue;
6246
6247 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006248 CLEAR_FIELD(funcexe);
6249 funcexe.fe_evaluate = TRUE;
6250
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006251 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006252 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006253
6254 char_u *name = dr->dr_name;
6255 dr->dr_name = NULL;
6256
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006257 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006258 // first statement in the function will be executed (because of the
6259 // exception). So save and restore the try/catch/throw exception
6260 // state.
6261 exception_state_T estate;
6262 exception_state_save(&estate);
6263 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006264
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006265 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6266
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006267 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006268
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006269 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006270 vim_free(name);
6271 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006272 clear_tv(&dr->dr_argvars[i]);
6273 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006274 ga_clear(&funccal->fc_defer);
6275}
6276
zeertzjq960cf912023-04-18 21:52:54 +01006277 static void
6278invoke_funccall_defer(funccall_T *fc)
6279{
6280 if (fc->fc_ectx != NULL)
6281 {
6282 // :def function
6283 unwind_def_callstack(fc->fc_ectx);
6284 may_invoke_defer_funcs(fc->fc_ectx);
6285 }
6286 else
6287 {
6288 // legacy function
6289 handle_defer_one(fc);
6290 }
6291}
6292
Bram Moolenaar58779852022-09-06 18:31:14 +01006293/*
6294 * Called when exiting: call all defer functions.
6295 */
6296 void
6297invoke_all_defer(void)
6298{
zeertzjq1be4b812023-04-19 14:21:24 +01006299 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6300 invoke_funccall_defer(fc);
6301
zeertzjq960cf912023-04-18 21:52:54 +01006302 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6303 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6304 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006305}
6306
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006307/*
6308 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006309 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006310 */
6311 void
6312ex_call(exarg_T *eap)
6313{
6314 char_u *arg = eap->arg;
6315 char_u *startarg;
6316 char_u *name;
6317 char_u *tofree;
6318 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006319 int failed = FALSE;
6320 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006321 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006322 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006323 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006324 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006325 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006326 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006327
Bram Moolenaare6b53242020-07-01 17:28:33 +02006328 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006329 if (eap->skip)
6330 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006331 typval_T rettv;
6332
Bram Moolenaare38eab22019-12-05 21:50:01 +01006333 // trans_function_name() doesn't work well when skipping, use eval0()
6334 // instead to skip to any following command, e.g. for:
6335 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006336 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006337 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006338 clear_tv(&rettv);
6339 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006340 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006341 return;
6342 }
6343
zeertzjq5299c092023-04-12 20:48:16 +01006344 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006345 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006346 if (fudi.fd_newkey != NULL)
6347 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006348 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006349 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006350 vim_free(fudi.fd_newkey);
6351 }
6352 if (tofree == NULL)
6353 return;
6354
Bram Moolenaare38eab22019-12-05 21:50:01 +01006355 // Increase refcount on dictionary, it could get deleted when evaluating
6356 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006357 if (fudi.fd_dict != NULL)
6358 ++fudi.fd_dict->dv_refcount;
6359
Bram Moolenaare38eab22019-12-05 21:50:01 +01006360 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6361 // contents. For VAR_PARTIAL get its partial, unless we already have one
6362 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006363 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006364 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006365 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006366 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006367
Bram Moolenaare38eab22019-12-05 21:50:01 +01006368 // Skip white space to allow ":call func ()". Not good, but required for
6369 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006370 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006371 if (*startarg != '(')
6372 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006373 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006374 goto end;
6375 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006376 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006377 {
6378 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6379 goto end;
6380 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006381
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006382 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006383 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006384 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006385 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006386 }
6387 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006388 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006389 funcexe_T funcexe;
6390
Bram Moolenaara80faa82020-04-12 19:37:17 +02006391 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006392 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006393 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006394 funcexe.fe_partial = partial;
6395 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006396 funcexe.fe_firstline = eap->line1;
6397 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006398 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006399 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006400 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006401 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006402
Bram Moolenaar1d341892021-09-18 15:25:52 +02006403 // When inside :try we need to check for following "| catch" or "| endtry".
6404 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006405 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006406 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006407 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006408 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006409 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006410 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006411 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006412 {
6413 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006414 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006415 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006416 }
6417 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006418 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006419 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006420 // Must be after using "arg", it may point into memory cleared here.
6421 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006422
6423end:
6424 dict_unref(fudi.fd_dict);
6425 vim_free(tofree);
6426}
6427
6428/*
6429 * Return from a function. Possibly makes the return pending. Also called
6430 * for a pending return at the ":endtry" or after returning from an extra
6431 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6432 * when called due to a ":return" command. "rettv" may point to a typval_T
6433 * with the return rettv. Returns TRUE when the return can be carried out,
6434 * FALSE when the return gets pending.
6435 */
6436 int
6437do_return(
6438 exarg_T *eap,
6439 int reanimate,
6440 int is_cmd,
6441 void *rettv)
6442{
6443 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006444 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006445
6446 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006447 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006448 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006449
6450 /*
6451 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6452 * not in its finally clause (which then is to be executed next) is found.
6453 * In this case, make the ":return" pending for execution at the ":endtry".
6454 * Otherwise, return normally.
6455 */
6456 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6457 if (idx >= 0)
6458 {
6459 cstack->cs_pending[idx] = CSTP_RETURN;
6460
6461 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006462 // A pending return again gets pending. "rettv" points to an
6463 // allocated variable with the rettv of the original ":return"'s
6464 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006465 cstack->cs_rettv[idx] = rettv;
6466 else
6467 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006468 // When undoing a return in order to make it pending, get the stored
6469 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006470 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006471 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006472
6473 if (rettv != NULL)
6474 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006475 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006476 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6477 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6478 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006479 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006480 }
6481 else
6482 cstack->cs_rettv[idx] = NULL;
6483
6484 if (reanimate)
6485 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006486 // The pending return value could be overwritten by a ":return"
6487 // without argument in a finally clause; reset the default
6488 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006489 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6490 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006491 }
6492 }
6493 report_make_pending(CSTP_RETURN, rettv);
6494 }
6495 else
6496 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006497 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006498
Bram Moolenaare38eab22019-12-05 21:50:01 +01006499 // If the return is carried out now, store the return value. For
6500 // a return immediately after reanimation, the value is already
6501 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006502 if (!reanimate && rettv != NULL)
6503 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006504 clear_tv(current_funccal->fc_rettv);
6505 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006506 if (!is_cmd)
6507 vim_free(rettv);
6508 }
6509 }
6510
6511 return idx < 0;
6512}
6513
6514/*
6515 * Free the variable with a pending return value.
6516 */
6517 void
6518discard_pending_return(void *rettv)
6519{
6520 free_tv((typval_T *)rettv);
6521}
6522
6523/*
6524 * Generate a return command for producing the value of "rettv". The result
6525 * is an allocated string. Used by report_pending() for verbose messages.
6526 */
6527 char_u *
6528get_return_cmd(void *rettv)
6529{
6530 char_u *s = NULL;
6531 char_u *tofree = NULL;
6532 char_u numbuf[NUMBUFLEN];
6533
6534 if (rettv != NULL)
6535 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6536 if (s == NULL)
6537 s = (char_u *)"";
6538
6539 STRCPY(IObuff, ":return ");
6540 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6541 if (STRLEN(s) + 8 >= IOSIZE)
6542 STRCPY(IObuff + IOSIZE - 4, "...");
6543 vim_free(tofree);
6544 return vim_strsave(IObuff);
6545}
6546
6547/*
6548 * Get next function line.
6549 * Called by do_cmdline() to get the next line.
6550 * Returns allocated string, or NULL for end of function.
6551 */
6552 char_u *
6553get_func_line(
6554 int c UNUSED,
6555 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006556 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006557 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006558{
6559 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006560 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006561 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006562 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006563
Bram Moolenaare38eab22019-12-05 21:50:01 +01006564 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006565 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006566 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006567 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006568 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006569 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006570 }
6571#ifdef FEAT_PROFILE
6572 if (do_profiling == PROF_YES)
6573 func_line_end(cookie);
6574#endif
6575
6576 gap = &fp->uf_lines;
6577 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006578 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006579 retval = NULL;
6580 else
6581 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006582 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006583 while (fcp->fc_linenr < gap->ga_len
6584 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6585 ++fcp->fc_linenr;
6586 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006587 retval = NULL;
6588 else
6589 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006590 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6591 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006592#ifdef FEAT_PROFILE
6593 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006594 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006595#endif
6596 }
6597 }
6598
Bram Moolenaare38eab22019-12-05 21:50:01 +01006599 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006600 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006601 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006602 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006603 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006604 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006605 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006606 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006607 }
6608
6609 return retval;
6610}
6611
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006612/*
6613 * Return TRUE if the currently active function should be ended, because a
6614 * return was encountered or an error occurred. Used inside a ":while".
6615 */
6616 int
6617func_has_ended(void *cookie)
6618{
6619 funccall_T *fcp = (funccall_T *)cookie;
6620
Bram Moolenaare38eab22019-12-05 21:50:01 +01006621 // Ignore the "abort" flag if the abortion behavior has been changed due to
6622 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006623 return (((fcp->fc_func->uf_flags & FC_ABORT)
6624 && did_emsg && !aborted_in_try())
6625 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006626}
6627
6628/*
6629 * return TRUE if cookie indicates a function which "abort"s on errors.
6630 */
6631 int
6632func_has_abort(
6633 void *cookie)
6634{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006635 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006636}
6637
6638
6639/*
6640 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6641 * Don't do this when "Func" is already a partial that was bound
6642 * explicitly (pt_auto is FALSE).
6643 * Changes "rettv" in-place.
6644 * Returns the updated "selfdict_in".
6645 */
6646 dict_T *
6647make_partial(dict_T *selfdict_in, typval_T *rettv)
6648{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006649 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006650 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006651 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006652 dict_T *selfdict = selfdict_in;
6653
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006654 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6655 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006656 fp = rettv->vval.v_partial->pt_func;
6657 else
6658 {
6659 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006660 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006661 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006662 if (fname == NULL)
6663 {
6664 // There is no point binding a dict to a NULL function, just create
6665 // a function reference.
6666 rettv->v_type = VAR_FUNC;
6667 rettv->vval.v_string = NULL;
6668 }
6669 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006670 {
6671 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006672 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006673
6674 // Translate "s:func" to the stored function name.
6675 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6676 fp = find_func(fname, FALSE);
6677 vim_free(tofree);
6678 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006679 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006680
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006681 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006682 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006683 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006684
6685 if (pt != NULL)
6686 {
6687 pt->pt_refcount = 1;
6688 pt->pt_dict = selfdict;
6689 pt->pt_auto = TRUE;
6690 selfdict = NULL;
6691 if (rettv->v_type == VAR_FUNC)
6692 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006693 // Just a function: Take over the function name and use
6694 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006695 pt->pt_name = rettv->vval.v_string;
6696 }
6697 else
6698 {
6699 partial_T *ret_pt = rettv->vval.v_partial;
6700 int i;
6701
Bram Moolenaare38eab22019-12-05 21:50:01 +01006702 // Partial: copy the function name, use selfdict and copy
6703 // args. Can't take over name or args, the partial might
6704 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006705 if (ret_pt->pt_name != NULL)
6706 {
6707 pt->pt_name = vim_strsave(ret_pt->pt_name);
6708 func_ref(pt->pt_name);
6709 }
6710 else
6711 {
6712 pt->pt_func = ret_pt->pt_func;
6713 func_ptr_ref(pt->pt_func);
6714 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006715 if (ret_pt->pt_argc > 0)
6716 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006717 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006718 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006719 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006720 pt->pt_argc = 0;
6721 else
6722 {
6723 pt->pt_argc = ret_pt->pt_argc;
6724 for (i = 0; i < pt->pt_argc; i++)
6725 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6726 }
6727 }
6728 partial_unref(ret_pt);
6729 }
6730 rettv->v_type = VAR_PARTIAL;
6731 rettv->vval.v_partial = pt;
6732 }
6733 }
6734 return selfdict;
6735}
6736
6737/*
6738 * Return the name of the executed function.
6739 */
6740 char_u *
6741func_name(void *cookie)
6742{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006743 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006744}
6745
6746/*
6747 * Return the address holding the next breakpoint line for a funccall cookie.
6748 */
6749 linenr_T *
6750func_breakpoint(void *cookie)
6751{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006752 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006753}
6754
6755/*
6756 * Return the address holding the debug tick for a funccall cookie.
6757 */
6758 int *
6759func_dbg_tick(void *cookie)
6760{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006761 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006762}
6763
6764/*
6765 * Return the nesting level for a funccall cookie.
6766 */
6767 int
6768func_level(void *cookie)
6769{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006770 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006771}
6772
6773/*
6774 * Return TRUE when a function was ended by a ":return" command.
6775 */
6776 int
6777current_func_returned(void)
6778{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006779 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006780}
6781
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006782 int
6783free_unref_funccal(int copyID, int testing)
6784{
6785 int did_free = FALSE;
6786 int did_free_funccal = FALSE;
6787 funccall_T *fc, **pfc;
6788
6789 for (pfc = &previous_funccal; *pfc != NULL; )
6790 {
6791 if (can_free_funccal(*pfc, copyID))
6792 {
6793 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006794 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006795 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006796 did_free = TRUE;
6797 did_free_funccal = TRUE;
6798 }
6799 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006800 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006801 }
6802 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006803 // When a funccal was freed some more items might be garbage
6804 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006805 (void)garbage_collect(testing);
6806
6807 return did_free;
6808}
6809
6810/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006811 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006812 */
6813 static funccall_T *
6814get_funccal(void)
6815{
6816 int i;
6817 funccall_T *funccal;
6818 funccall_T *temp_funccal;
6819
6820 funccal = current_funccal;
6821 if (debug_backtrace_level > 0)
6822 {
6823 for (i = 0; i < debug_backtrace_level; i++)
6824 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006825 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006826 if (temp_funccal)
6827 funccal = temp_funccal;
6828 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006829 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006830 debug_backtrace_level = i;
6831 }
6832 }
6833 return funccal;
6834}
6835
6836/*
6837 * Return the hashtable used for local variables in the current funccal.
6838 * Return NULL if there is no current funccal.
6839 */
6840 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006841get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006842{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006843 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006844 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006845 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006846}
6847
6848/*
6849 * Return the l: scope variable.
6850 * Return NULL if there is no current funccal.
6851 */
6852 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006853get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006854{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006855 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006856 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006857 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006858}
6859
6860/*
6861 * Return the hashtable used for argument in the current funccal.
6862 * Return NULL if there is no current funccal.
6863 */
6864 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006865get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006866{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006867 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006868 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006869 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006870}
6871
6872/*
6873 * Return the a: scope variable.
6874 * Return NULL if there is no current funccal.
6875 */
6876 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006877get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006878{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006879 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006880 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006881 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006882}
6883
6884/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006885 * List function variables, if there is a function.
6886 */
6887 void
6888list_func_vars(int *first)
6889{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006890 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6891 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006892 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006893}
6894
6895/*
6896 * If "ht" is the hashtable for local variables in the current funccal, return
6897 * the dict that contains it.
6898 * Otherwise return NULL.
6899 */
6900 dict_T *
6901get_current_funccal_dict(hashtab_T *ht)
6902{
6903 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006904 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6905 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006906 return NULL;
6907}
6908
6909/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006910 * Search hashitem in parent scope.
6911 */
6912 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006913find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006914{
6915 funccall_T *old_current_funccal = current_funccal;
6916 hashtab_T *ht;
6917 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006918 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006919
Bram Moolenaarca16c602022-09-06 18:57:08 +01006920 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006921 return NULL;
6922
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006923 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006924 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006925 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006926 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006927 ht = find_var_ht(name, &varname);
6928 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006929 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006930 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006931 if (!HASHITEM_EMPTY(hi))
6932 {
6933 *pht = ht;
6934 break;
6935 }
6936 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006937 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006938 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006939 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006940 }
6941 current_funccal = old_current_funccal;
6942
6943 return hi;
6944}
6945
6946/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006947 * Search variable in parent scope.
6948 */
6949 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006950find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006951{
6952 dictitem_T *v = NULL;
6953 funccall_T *old_current_funccal = current_funccal;
6954 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006955 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006956
Bram Moolenaarca16c602022-09-06 18:57:08 +01006957 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006958 return NULL;
6959
Bram Moolenaare38eab22019-12-05 21:50:01 +01006960 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01006961 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006962 while (current_funccal)
6963 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006964 ht = find_var_ht(name, &varname);
6965 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006966 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006967 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006968 if (v != NULL)
6969 break;
6970 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006971 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006972 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006973 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006974 }
6975 current_funccal = old_current_funccal;
6976
6977 return v;
6978}
6979
6980/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006981 * Set "copyID + 1" in previous_funccal and callers.
6982 */
6983 int
6984set_ref_in_previous_funccal(int copyID)
6985{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006986 funccall_T *fc;
6987
Bram Moolenaarca16c602022-09-06 18:57:08 +01006988 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006989 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006990 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006991 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
6992 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
6993 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006994 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006995 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006996 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006997}
6998
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006999 static int
7000set_ref_in_funccal(funccall_T *fc, int copyID)
7001{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007002 if (fc->fc_copyID != copyID)
7003 {
7004 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007005 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7006 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7007 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7008 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007009 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007010 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007011 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007012}
7013
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007014/*
7015 * Set "copyID" in all local vars and arguments in the call stack.
7016 */
7017 int
7018set_ref_in_call_stack(int copyID)
7019{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007020 funccall_T *fc;
7021 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007022
Bram Moolenaarca16c602022-09-06 18:57:08 +01007023 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007024 if (set_ref_in_funccal(fc, copyID))
7025 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007026
7027 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007028 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007029 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007030 if (set_ref_in_funccal(fc, copyID))
7031 return TRUE;
7032 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007033}
7034
7035/*
7036 * Set "copyID" in all functions available by name.
7037 */
7038 int
7039set_ref_in_functions(int copyID)
7040{
7041 int todo;
7042 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007043 ufunc_T *fp;
7044
7045 todo = (int)func_hashtab.ht_used;
7046 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007047 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007048 if (!HASHITEM_EMPTY(hi))
7049 {
7050 --todo;
7051 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007052 if (!func_name_refcount(fp->uf_name)
7053 && set_ref_in_func(NULL, fp, copyID))
7054 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007055 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007056 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007057 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007058}
7059
7060/*
7061 * Set "copyID" in all function arguments.
7062 */
7063 int
7064set_ref_in_func_args(int copyID)
7065{
7066 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007067
7068 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007069 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7070 copyID, NULL, NULL))
7071 return TRUE;
7072 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007073}
7074
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007075/*
7076 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007077 * Returns TRUE if setting references failed somehow.
7078 */
7079 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007080set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007081{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007082 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007083 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007084 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007085 char_u fname_buf[FLEN_FIXED + 1];
7086 char_u *tofree = NULL;
7087 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007088 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007089
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007090 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007091 return FALSE;
7092
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007093 if (fp_in == NULL)
7094 {
7095 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007096 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007097 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007098 if (fp != NULL)
7099 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007100 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007101 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007102 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007103
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007104 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007105 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007106}
7107
Bram Moolenaare38eab22019-12-05 21:50:01 +01007108#endif // FEAT_EVAL