blob: ea03e786950eac2614ed96eb25d40c454d52d1eb [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
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100201 if (eap->ea_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000202 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000203 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100204 theline = eap->ea_getline(':', eap->cookie, indent, getline_options);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000205 if (theline != NULL)
206 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000207 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000208 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000209 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
210 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000211 *eap->cmdlinep = theline;
Bram Moolenaarb5820102023-01-25 12:27:13 +0000212 (void)ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000213 }
214
215 return theline;
216}
217
218/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200219 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100220 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100221 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200222 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100223 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200224get_function_args(
225 char_u **argp,
226 char_u endchar,
227 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100229 int types_optional, // types optional if "argtypes" is not NULL
h-eastb895b0f2023-09-24 15:46:31 +0200230 garray_T *arg_objm, // NULL unless using :def
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100231 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200232 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200233 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200234 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000235 exarg_T *eap, // can be NULL
Bram Moolenaar554d0312023-01-05 19:59:18 +0000236 int in_class, // non-zero when inside a class or interface
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000237 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000238 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200239{
240 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100241 char_u *arg;
242 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200243 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200244 int any_default = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100245 char_u *whitep = *argp;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100246 int need_expr = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200247
248 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000249 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100250 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000251 ga_init2(argtypes, sizeof(char_u *), 3);
h-eastb895b0f2023-09-24 15:46:31 +0200252 if (arg_objm != NULL)
253 ga_init2(arg_objm, sizeof(int8_T), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200254 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000255 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200256
257 if (varargs != NULL)
258 *varargs = FALSE;
259
260 /*
261 * Isolate the arguments: "arg1, arg2, ...)"
262 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100263 arg = skipwhite(*argp);
264 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200265 while (*p != endchar)
266 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100267 while (eap != NULL && eap->ea_getline != NULL
Bram Moolenaar2c330432020-04-13 14:41:35 +0200268 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200269 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200270 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000271 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000272 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000273
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200274 if (theline == NULL)
275 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200276 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200277 p = skipwhite(theline);
278 }
279
280 if (mustend && *p != endchar)
281 {
282 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000283 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100284 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200285 }
Christian Brabandte4a450a2023-12-08 20:57:38 +0100286 if (*p == endchar && !need_expr)
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200287 break;
288
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200289 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
290 {
291 if (varargs != NULL)
292 *varargs = TRUE;
293 p += 3;
294 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295
296 if (argtypes != NULL)
297 {
298 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200299 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100300 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100301 if (!skip)
302 emsg(_(e_missing_name_after_dots));
303 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100304 }
305
306 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100307 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200308 arg_objm, evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100309 if (p == arg)
310 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100311 if (*skipwhite(p) == '=')
312 {
313 emsg(_(e_cannot_use_default_for_variable_arguments));
314 break;
315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100316 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200317 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000318 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000319 {
320 // this.memberName
321 p += 5;
322 arg = p;
323 while (ASCII_ISALNUM(*p) || *p == '_')
324 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000325 char_u *argend = p;
326
h-eastdb385522023-09-28 22:18:19 +0200327 // object variable this. can be used only in a constructor
328 if (STRNCMP(eap->arg, "new", 3) != 0)
329 {
330 c = *argend;
331 *argend = NUL;
332 semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
333 *argend = c;
334 break;
335 }
336
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000337 if (*skipwhite(p) == '=')
338 {
339 char_u *defval = skipwhite(skipwhite(p) + 1);
340 if (STRNCMP(defval, "v:none", 6) != 0)
341 {
342 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
343 goto err_ret;
344 }
345 any_default = TRUE;
346 p = defval + 6;
347
348 if (ga_grow(default_args, 1) == FAIL)
349 goto err_ret;
350
351 char_u *expr = vim_strsave((char_u *)"v:none");
352 if (expr == NULL)
353 goto err_ret;
354 ((char_u **)(default_args->ga_data))
355 [default_args->ga_len] = expr;
356 default_args->ga_len++;
357 }
358 else if (any_default)
359 {
360 emsg(_(e_non_default_argument_follows_default_argument));
361 goto err_ret;
362 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000363
364 // TODO: check the argument is indeed a member
365 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
366 return FAIL;
367 if (newargs != NULL)
368 {
369 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000370 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000371 newargs->ga_len++;
372
h-eastb895b0f2023-09-24 15:46:31 +0200373 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
374 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000375 {
376 // TODO: use the actual type
377 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
378 vim_strsave((char_u *)"any");
h-eastb895b0f2023-09-24 15:46:31 +0200379 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000380
381 // Add a line to the function body for the assignment.
382 if (ga_grow(newlines, 1) == OK)
383 {
384 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000385 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
386 if (any_default)
387 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000388 char_u *assignment = alloc(len);
389 if (assignment != NULL)
390 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000391 c = *argend;
392 *argend = NUL;
393 if (any_default)
394 vim_snprintf((char *)assignment, len,
395 "ifargisset %d this.%s = %s",
396 default_args->ga_len - 1, arg, arg);
397 else
398 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000399 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000400 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000401 ((char_u **)(newlines->ga_data))[
402 newlines->ga_len++] = assignment;
403 }
404 }
405 }
406 }
407 if (*p == ',')
408 ++p;
409 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200410 else
411 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200412 char_u *np;
413
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200414 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100415 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200416 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100417 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200418 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200419
Bram Moolenaar015cf102021-06-26 21:52:02 +0200420 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200421 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200422 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200423 if (*np == '=' && np[1] != '=' && np[1] != '~'
424 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200425 {
426 typval_T rettv;
427
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100428 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200429 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100430 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000431 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200432 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200433 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200434 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200435 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200436 if (ga_grow(default_args, 1) == FAIL)
437 goto err_ret;
438
Christian Brabandte4a450a2023-12-08 20:57:38 +0100439 if (need_expr)
440 need_expr = FALSE;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200441 // trim trailing whitespace
442 while (p > expr && VIM_ISWHITE(p[-1]))
443 p--;
444 c = *p;
445 *p = NUL;
446 expr = vim_strsave(expr);
447 if (expr == NULL)
448 {
449 *p = c;
450 goto err_ret;
451 }
452 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200453 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200454 default_args->ga_len++;
455 *p = c;
456 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200457 }
458 else
Christian Brabandte4a450a2023-12-08 20:57:38 +0100459 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200460 mustend = TRUE;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100461 if (*skipwhite(p) == NUL)
462 need_expr = TRUE;
463 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200464 }
465 else if (any_default)
466 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000467 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200468 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200469 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200470
471 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
472 {
473 // Be tolerant when skipping
474 if (!skip)
475 {
476 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
477 goto err_ret;
478 }
479 p = skipwhite(p);
480 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200481 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200482 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200483 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200484 // Don't give this error when skipping, it makes the "->" not
485 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100486 // Allow missing space after comma in legacy functions.
487 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200488 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
489 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100490 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200491 goto err_ret;
492 }
493 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200494 else
495 mustend = TRUE;
496 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200497 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200498 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200499 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200500
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200501 if (*p != endchar)
502 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100503 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200504
505 *argp = p;
506 return OK;
507
508err_ret:
509 if (newargs != NULL)
510 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200511 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200512 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200513 return FAIL;
514}
515
516/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100517 * Parse the argument types, filling "fp->uf_arg_types".
518 * Return OK or FAIL.
519 */
520 static int
h-eastb895b0f2023-09-24 15:46:31 +0200521parse_argument_types(
522 ufunc_T *fp,
523 garray_T *argtypes,
524 int varargs,
525 garray_T *arg_objm,
526 ocmember_T *obj_members,
527 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100528{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200529 int len = 0;
530
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100531 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
532 if (argtypes->ga_len > 0)
533 {
534 // When "varargs" is set the last name/type goes into uf_va_name
535 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200536 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100537
538 if (len > 0)
539 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
540 if (fp->uf_arg_types != NULL)
541 {
542 int i;
543 type_T *type;
544
545 for (i = 0; i < len; ++ i)
546 {
547 char_u *p = ((char_u **)argtypes->ga_data)[i];
548
549 if (p == NULL)
550 // will get the type from the default value
551 type = &t_unknown;
552 else
h-eastb895b0f2023-09-24 15:46:31 +0200553 {
554 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
555 {
556 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
557
558 type = &t_any;
559 for (int om = 0; om < obj_member_count; ++om)
560 {
561 if (STRCMP(aname, obj_members[om].ocm_name) == 0)
562 {
563 type = obj_members[om].ocm_type;
564 break;
565 }
566 }
567 }
568 else
569 type = parse_type(&p, &fp->uf_type_list, TRUE);
570 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100571 if (type == NULL)
572 return FAIL;
573 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000574 if (i < fp->uf_args.ga_len
575 && (type->tt_type == VAR_FUNC
576 || type->tt_type == VAR_PARTIAL)
577 && var_wrong_func_name(
578 ((char_u **)fp->uf_args.ga_data)[i], TRUE))
579 return FAIL;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100580 }
581 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100582 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200583
584 if (varargs)
585 {
586 char_u *p;
587
588 // Move the last argument "...name: type" to uf_va_name and
589 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200590 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000591 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
592 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200593 p = ((char_u **)argtypes->ga_data)[len];
594 if (p == NULL)
595 // TODO: get type from default value
596 fp->uf_va_type = &t_list_any;
597 else
598 {
599 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
600 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
601 {
602 semsg(_(e_variable_arguments_type_must_be_list_str),
603 ((char_u **)argtypes->ga_data)[len]);
604 return FAIL;
605 }
606 }
607 if (fp->uf_va_type == NULL)
608 return FAIL;
609 }
610
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100611 return OK;
612}
613
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100614 static int
615parse_return_type(ufunc_T *fp, char_u *ret_type)
616{
617 if (ret_type == NULL)
618 fp->uf_ret_type = &t_void;
619 else
620 {
621 char_u *p = ret_type;
622
623 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
624 if (fp->uf_ret_type == NULL)
625 {
626 fp->uf_ret_type = &t_void;
627 return FAIL;
628 }
629 }
630 return OK;
631}
632
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100633/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200634 * Register function "fp" as using "current_funccal" as its scope.
635 */
636 static int
637register_closure(ufunc_T *fp)
638{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200639 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100640 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200641 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200642 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200643 fp->uf_scoped = current_funccal;
644 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200645
Bram Moolenaarca16c602022-09-06 18:57:08 +0100646 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200647 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100648 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
649 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200650 return OK;
651}
652
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100653 static void
654set_ufunc_name(ufunc_T *fp, char_u *name)
655{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100656 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
657 // actually extends beyond the struct.
658 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100659
660 if (name[0] == K_SPECIAL)
661 {
662 fp->uf_name_exp = alloc(STRLEN(name) + 3);
663 if (fp->uf_name_exp != NULL)
664 {
665 STRCPY(fp->uf_name_exp, "<SNR>");
666 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
667 }
668 }
669}
670
Bram Moolenaar58016442016-07-31 18:30:22 +0200671/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100672 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
673 * return "buf" filled with a readable function name.
674 * Otherwise just return "name", thus the return value can always be used.
675 * "name" and "buf" may be equal.
676 */
677 char_u *
678make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
679{
680 size_t len;
681
682 if (name[0] != K_SPECIAL)
683 return name;
684 len = STRLEN(name);
685 if (len + 3 > bufsize)
686 return name;
687
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100688 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100689 mch_memmove(buf, "<SNR>", 5);
690 return buf;
691}
692
693/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200694 * Get a name for a lambda. Returned in static memory.
695 */
696 char_u *
697get_lambda_name(void)
698{
699 static char_u name[30];
700 static int lambda_no = 0;
701
702 sprintf((char*)name, "<lambda>%d", ++lambda_no);
703 return name;
704}
705
Bram Moolenaare01e5212023-01-08 20:31:18 +0000706/*
707 * Allocate a "ufunc_T" for a function called "name".
708 * Makes sure the size is right.
709 */
710 static ufunc_T *
711alloc_ufunc(char_u *name)
712{
713 // When the name is short we need to make sure we allocate enough bytes for
714 // the whole struct, including any padding.
715 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
716 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
717}
718
Bram Moolenaar801ab062020-06-25 19:27:56 +0200719#if defined(FEAT_LUA) || defined(PROTO)
720/*
721 * Registers a native C callback which can be called from Vim script.
722 * Returns the name of the Vim script function.
723 */
724 char_u *
725register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
726{
727 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200728 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200729
Bram Moolenaare01e5212023-01-08 20:31:18 +0000730 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200731 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200732 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200733
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200734 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200735 fp->uf_refcount = 1;
736 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000737 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200738 fp->uf_calls = 0;
739 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200740 fp->uf_cb = cb;
741 fp->uf_cb_free = cb_free;
742 fp->uf_cb_state = state;
743
744 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000745 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200746
747 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200748}
749#endif
750
Bram Moolenaar04b12692020-05-04 23:24:44 +0200751/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100752 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100753 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100754 * If "white_error" is not NULL check for correct use of white space and set
755 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100756 * Return NULL if no valid arrow found.
757 */
758 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100759skip_arrow(
760 char_u *start,
761 int equal_arrow,
762 char_u **ret_type,
763 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100764{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100765 char_u *s = start;
766 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100767
768 if (equal_arrow)
769 {
770 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100771 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100772 if (white_error != NULL && !VIM_ISWHITE(s[1]))
773 {
774 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100775 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100776 return NULL;
777 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100778 s = skipwhite(s + 1);
779 *ret_type = s;
780 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100781 if (s == *ret_type)
782 {
783 emsg(_(e_missing_return_type));
784 return NULL;
785 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100786 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100787 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100788 s = skipwhite(s);
789 if (*s != '=')
790 return NULL;
791 ++s;
792 }
793 if (*s != '>')
794 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100795 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
796 || !IS_WHITE_OR_NUL(s[1])))
797 {
798 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100799 semsg(_(e_white_space_required_before_and_after_str_at_str),
800 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100801 return NULL;
802 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100803 return skipwhite(s + 1);
804}
805
806/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100807 * Check if "*cmd" points to a function command and if so advance "*cmd" and
808 * return TRUE.
809 * Otherwise return FALSE;
810 * Do not consider "function(" to be a command.
811 */
812 static int
813is_function_cmd(char_u **cmd)
814{
815 char_u *p = *cmd;
816
817 if (checkforcmd(&p, "function", 2))
818 {
819 if (*p == '(')
820 return FALSE;
821 *cmd = p;
822 return TRUE;
823 }
824 return FALSE;
825}
826
827/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200828 * Called when defining a function: The context may be needed for script
829 * variables declared in a block that is visible now but not when the function
830 * is compiled or called later.
831 */
832 static void
833function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
834{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000835 if (cstack == NULL || cstack->cs_idx < 0)
836 return;
837
838 int count = cstack->cs_idx + 1;
839 int i;
840
841 fp->uf_block_ids = ALLOC_MULT(int, count);
842 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200843 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000844 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
845 sizeof(int) * count);
846 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200847 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000848
849 // Set flag in each block to indicate a function was defined. This
850 // is used to keep the variable when leaving the block, see
851 // hide_script_var().
852 for (i = 0; i <= cstack->cs_idx; ++i)
853 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200854}
855
856/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100857 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200858 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100859 * "newlines" must already have been initialized.
860 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
861 */
862 static int
863get_function_body(
864 exarg_T *eap,
865 garray_T *newlines,
866 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000867 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100868{
869 linenr_T sourcing_lnum_top = SOURCING_LNUM;
870 linenr_T sourcing_lnum_off;
871 int saved_wait_return = need_wait_return;
872 char_u *line_arg = line_arg_in;
873 int vim9_function = eap->cmdidx == CMD_def
874 || eap->cmdidx == CMD_block;
875#define MAX_FUNC_NESTING 50
876 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200877 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100878 int nesting = 0;
879 getline_opt_T getline_options;
880 int indent = 2;
881 char_u *skip_until = NULL;
882 int ret = FAIL;
883 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200884 int heredoc_concat_len = 0;
885 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100886 char_u *heredoc_trimmed = NULL;
887
Bram Moolenaar20677332021-06-06 17:02:53 +0200888 ga_init2(&heredoc_ga, 1, 500);
889
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100890 // Detect having skipped over comment lines to find the return
891 // type. Add NULL lines to keep the line count correct.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100892 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100893 if (SOURCING_LNUM < sourcing_lnum_off)
894 {
895 sourcing_lnum_off -= SOURCING_LNUM;
896 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
897 goto theend;
898 while (sourcing_lnum_off-- > 0)
899 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
900 }
901
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200902 nesting_def[0] = vim9_function;
903 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100904 getline_options = vim9_function
905 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
906 for (;;)
907 {
908 char_u *theline;
909 char_u *p;
910 char_u *arg;
911
912 if (KeyTyped)
913 {
914 msg_scroll = TRUE;
915 saved_wait_return = FALSE;
916 }
917 need_wait_return = FALSE;
918
919 if (line_arg != NULL)
920 {
921 // Use eap->arg, split up in parts by line breaks.
922 theline = line_arg;
923 p = vim_strchr(theline, '\n');
924 if (p == NULL)
925 line_arg += STRLEN(line_arg);
926 else
927 {
928 *p = NUL;
929 line_arg = p + 1;
930 }
931 }
932 else
933 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000934 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100935 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100936 }
937 if (KeyTyped)
938 lines_left = Rows - 1;
939 if (theline == NULL)
940 {
941 // Use the start of the function for the line number.
942 SOURCING_LNUM = sourcing_lnum_top;
943 if (skip_until != NULL)
944 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200945 else if (nesting_inline[nesting])
946 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100947 else if (eap->cmdidx == CMD_def)
948 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100949 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000950 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100951 goto theend;
952 }
953
954 // Detect line continuation: SOURCING_LNUM increased more than one.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100955 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100956 if (SOURCING_LNUM < sourcing_lnum_off)
957 sourcing_lnum_off -= SOURCING_LNUM;
958 else
959 sourcing_lnum_off = 0;
960
961 if (skip_until != NULL)
962 {
963 // Don't check for ":endfunc"/":enddef" between
964 // * ":append" and "."
965 // * ":python <<EOF" and "EOF"
966 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
967 if (heredoc_trimmed == NULL
968 || (is_heredoc && skipwhite(theline) == theline)
969 || STRNCMP(theline, heredoc_trimmed,
970 STRLEN(heredoc_trimmed)) == 0)
971 {
972 if (heredoc_trimmed == NULL)
973 p = theline;
974 else if (is_heredoc)
975 p = skipwhite(theline) == theline
976 ? theline : theline + STRLEN(heredoc_trimmed);
977 else
978 p = theline + STRLEN(heredoc_trimmed);
979 if (STRCMP(p, skip_until) == 0)
980 {
981 VIM_CLEAR(skip_until);
982 VIM_CLEAR(heredoc_trimmed);
983 getline_options = vim9_function
984 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
985 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200986
987 if (heredoc_concat_len > 0)
988 {
989 // Replace the starting line with all the concatenated
990 // lines.
991 ga_concat(&heredoc_ga, theline);
992 vim_free(((char_u **)(newlines->ga_data))[
993 heredoc_concat_len - 1]);
994 ((char_u **)(newlines->ga_data))[
995 heredoc_concat_len - 1] = heredoc_ga.ga_data;
996 ga_init(&heredoc_ga);
997 heredoc_concat_len = 0;
998 theline += STRLEN(theline); // skip the "EOF"
999 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001000 }
1001 }
1002 }
1003 else
1004 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001005 int c;
1006 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001007 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001008
1009 // skip ':' and blanks
1010 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1011 ;
1012
1013 // Check for "endfunction", "enddef" or "}".
1014 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001015 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001016 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001017 ? *p == '}'
1018 : (checkforcmd(&p, nesting_def[nesting]
1019 ? "enddef" : "endfunction", 4)
1020 && *p != ':'))
1021 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001022 if (!nesting_inline[nesting] && nesting_def[nesting]
1023 && p < cmd + 6)
1024 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001025 if (nesting-- == 0)
1026 {
1027 char_u *nextcmd = NULL;
1028
1029 if (*p == '|' || *p == '}')
1030 nextcmd = p + 1;
1031 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1032 nextcmd = line_arg;
1033 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001034 && (vim9_function || p_verbose > 0))
1035 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001036 SOURCING_LNUM = sourcing_lnum_top
1037 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001038 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001039 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001040 else
1041 give_warning2((char_u *)
1042 _("W22: Text found after :endfunction: %s"),
1043 p, TRUE);
1044 }
1045 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001046 {
1047 // Another command follows. If the line came from "eap"
1048 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001049 // change "eap->cmdlinep" to point to the last fetched
1050 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001051 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001052 if (lines_to_free->ga_len > 0
1053 && *eap->cmdlinep !=
1054 ((char_u **)lines_to_free->ga_data)
1055 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001056 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001057 // *cmdlinep will be freed later, thus remove the
1058 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001059 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001060 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1061 [lines_to_free->ga_len - 1];
1062 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001063 }
1064 }
1065 break;
1066 }
1067 }
1068
1069 // Check for mismatched "endfunc" or "enddef".
1070 // We don't check for "def" inside "func" thus we also can't check
1071 // for "enddef".
1072 // We continue to find the end of the function, although we might
1073 // not find it.
1074 else if (nesting_def[nesting])
1075 {
1076 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1077 emsg(_(e_mismatched_endfunction));
1078 }
1079 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1080 emsg(_(e_mismatched_enddef));
1081
1082 // Increase indent inside "if", "while", "for" and "try", decrease
1083 // at "end".
1084 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1085 indent -= 2;
1086 else if (STRNCMP(p, "if", 2) == 0
1087 || STRNCMP(p, "wh", 2) == 0
1088 || STRNCMP(p, "for", 3) == 0
1089 || STRNCMP(p, "try", 3) == 0)
1090 indent += 2;
1091
1092 // Check for defining a function inside this function.
1093 // Only recognize "def" inside "def", not inside "function",
1094 // For backwards compatibility, see Test_function_python().
1095 c = *p;
1096 if (is_function_cmd(&p)
1097 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1098 {
1099 if (*p == '!')
1100 p = skipwhite(p + 1);
1101 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001102 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001103 if (*skipwhite(p) == '(')
1104 {
1105 if (nesting == MAX_FUNC_NESTING - 1)
1106 emsg(_(e_function_nesting_too_deep));
1107 else
1108 {
1109 ++nesting;
1110 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001111 nesting_inline[nesting] = FALSE;
1112 indent += 2;
1113 }
1114 }
1115 }
1116
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001117 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001118 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001119 // Not a comment line: check for nested inline function.
1120 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001121 while (end > p && VIM_ISWHITE(*end))
1122 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001123 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001124 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001125 int is_block;
1126
1127 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001128 --end;
1129 while (end > p && VIM_ISWHITE(*end))
1130 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001131 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1132 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001133 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001134 char_u *s = p;
1135
1136 // check for line starting with "au" for :autocmd or
1137 // "com" for :command, these can use a {} block
1138 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1139 || checkforcmd_noparen(&s, "command", 3);
1140 }
1141
1142 if (is_block)
1143 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001144 if (nesting == MAX_FUNC_NESTING - 1)
1145 emsg(_(e_function_nesting_too_deep));
1146 else
1147 {
1148 ++nesting;
1149 nesting_def[nesting] = TRUE;
1150 nesting_inline[nesting] = TRUE;
1151 indent += 2;
1152 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001153 }
1154 }
1155 }
1156
1157 // Check for ":append", ":change", ":insert". Not for :def.
1158 p = skip_range(p, FALSE, NULL);
1159 if (!vim9_function
1160 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1161 || (p[0] == 'c'
1162 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1163 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1164 && (STRNCMP(&p[3], "nge", 3) != 0
1165 || !ASCII_ISALPHA(p[6])))))))
1166 || (p[0] == 'i'
1167 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1168 && (!ASCII_ISALPHA(p[2])
1169 || (p[2] == 's'
1170 && (!ASCII_ISALPHA(p[3])
1171 || p[3] == 'e'))))))))
1172 skip_until = vim_strsave((char_u *)".");
1173
1174 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1175 arg = skipwhite(skiptowhite(p));
1176 if (arg[0] == '<' && arg[1] =='<'
1177 && ((p[0] == 'p' && p[1] == 'y'
1178 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1179 || ((p[2] == '3' || p[2] == 'x')
1180 && !ASCII_ISALPHA(p[3]))))
1181 || (p[0] == 'p' && p[1] == 'e'
1182 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1183 || (p[0] == 't' && p[1] == 'c'
1184 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1185 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1186 && !ASCII_ISALPHA(p[3]))
1187 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1188 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1189 || (p[0] == 'm' && p[1] == 'z'
1190 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1191 ))
1192 {
1193 // ":python <<" continues until a dot, like ":append"
1194 p = skipwhite(arg + 2);
1195 if (STRNCMP(p, "trim", 4) == 0)
1196 {
1197 // Ignore leading white space.
1198 p = skipwhite(p + 4);
1199 heredoc_trimmed = vim_strnsave(theline,
1200 skipwhite(theline) - theline);
1201 }
1202 if (*p == NUL)
1203 skip_until = vim_strsave((char_u *)".");
1204 else
1205 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1206 getline_options = GETLINE_NONE;
1207 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001208 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001209 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001210 }
1211
Bram Moolenaard881d152022-05-13 13:50:36 +01001212 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001213 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001214 // Check for ":cmd v =<< [trim] EOF"
1215 // and ":cmd [a, b] =<< [trim] EOF"
1216 // and "lines =<< [trim] EOF" for Vim9
1217 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001218 arg = p;
1219 if (checkforcmd(&arg, "let", 2)
1220 || checkforcmd(&arg, "var", 3)
1221 || checkforcmd(&arg, "final", 5)
1222 || checkforcmd(&arg, "const", 5)
1223 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001224 {
zeertzjqa93d9cd2023-05-02 16:25:47 +01001225 while (vim_strchr((char_u *)"$@&", *arg) != NULL)
1226 ++arg;
1227 arg = skipwhite(find_name_end(arg, NULL, NULL,
1228 FNE_INCL_BR | FNE_ALLOW_CURLY));
1229 if (vim9_function && *arg == ':')
1230 arg = skipwhite(skip_type(skipwhite(arg + 1), FALSE));
1231 if (arg[0] == '=' && arg[1] == '<' && arg[2] =='<')
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001232 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001233 p = skipwhite(arg + 3);
1234 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001235 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001236 if (STRNCMP(p, "trim", 4) == 0)
1237 {
1238 // Ignore leading white space.
1239 p = skipwhite(p + 4);
1240 heredoc_trimmed = vim_strnsave(theline,
1241 skipwhite(theline) - theline);
1242 continue;
1243 }
1244 if (STRNCMP(p, "eval", 4) == 0)
1245 {
1246 // Ignore leading white space.
1247 p = skipwhite(p + 4);
1248 continue;
1249 }
1250 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001251 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001252 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1253 getline_options = GETLINE_NONE;
1254 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001255 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001256 }
1257 }
1258 }
1259
1260 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001261 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001262 goto theend;
1263
Bram Moolenaar20677332021-06-06 17:02:53 +02001264 if (heredoc_concat_len > 0)
1265 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001266 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001267 // to be used for the instruction later.
1268 ga_concat(&heredoc_ga, theline);
1269 ga_concat(&heredoc_ga, (char_u *)"\n");
1270 p = vim_strsave((char_u *)"");
1271 }
1272 else
1273 {
1274 // Copy the line to newly allocated memory. get_one_sourceline()
1275 // allocates 250 bytes per line, this saves 80% on average. The
1276 // cost is an extra alloc/free.
1277 p = vim_strsave(theline);
1278 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001279 if (p == NULL)
1280 goto theend;
1281 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1282
1283 // Add NULL lines for continuation lines, so that the line count is
1284 // equal to the index in the growarray.
1285 while (sourcing_lnum_off-- > 0)
1286 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1287
1288 // Check for end of eap->arg.
1289 if (line_arg != NULL && *line_arg == NUL)
1290 line_arg = NULL;
1291 }
1292
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001293 // Return OK when no error was detected.
1294 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001295 ret = OK;
1296
1297theend:
1298 vim_free(skip_until);
1299 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001300 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001301 need_wait_return |= saved_wait_return;
1302 return ret;
1303}
1304
1305/*
1306 * Handle the body of a lambda. *arg points to the "{", process statements
1307 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001308 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001309 * When successful "rettv" is set to a funcref.
1310 */
1311 static int
1312lambda_function_body(
1313 char_u **arg,
1314 typval_T *rettv,
1315 evalarg_T *evalarg,
1316 garray_T *newargs,
1317 garray_T *argtypes,
1318 int varargs,
1319 garray_T *default_args,
1320 char_u *ret_type)
1321{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001322 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001323 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001324 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001325 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001326 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001327 exarg_T eap;
1328 garray_T newlines;
1329 char_u *cmdline = NULL;
1330 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001331 partial_T *pt;
1332 char_u *name;
1333 int lnum_save = -1;
1334 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1335
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001336 *arg = skipwhite(*arg + 1);
1337 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001338 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001339 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001340 return FAIL;
1341 }
1342
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001343 CLEAR_FIELD(eap);
1344 eap.cmdidx = CMD_block;
1345 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001346 eap.cmdlinep = &cmdline;
1347 eap.skip = !evaluate;
1348 if (evalarg->eval_cctx != NULL)
1349 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1350 else
1351 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001352 eap.ea_getline = evalarg->eval_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001353 eap.cookie = evalarg->eval_cookie;
1354 }
1355
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001356 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001357 if (get_function_body(&eap, &newlines, NULL,
1358 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001359 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001360
1361 // When inside a lambda must add the function lines to evalarg.eval_ga.
1362 evalarg->eval_break_count += newlines.ga_len;
1363 if (gap->ga_itemsize > 0)
1364 {
1365 int idx;
1366 char_u *last;
1367 size_t plen;
1368 char_u *pnl;
1369
1370 for (idx = 0; idx < newlines.ga_len; ++idx)
1371 {
1372 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1373
Bram Moolenaarecb66452021-05-18 15:09:18 +02001374 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001375 goto erret;
1376
1377 // Going to concatenate the lines after parsing. For an empty or
1378 // comment line use an empty string.
1379 // Insert NL characters at the start of each line, the string will
1380 // be split again later in .get_lambda_tv().
1381 if (*p == NUL || vim9_comment_start(p))
1382 p = (char_u *)"";
1383 plen = STRLEN(p);
1384 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1385 if (pnl != NULL)
1386 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001387 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1388 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001389 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001390 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001391 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001392 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001393 // more is following after the "}", which was skipped
1394 last = cmdline;
1395 else
1396 // nothing is following the "}"
1397 last = (char_u *)"}";
1398 plen = STRLEN(last);
1399 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1400 if (pnl != NULL)
1401 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001402 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1403 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001404 }
1405
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001406 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001407 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001408 garray_T *tfgap = &evalarg->eval_tofree_ga;
1409
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001410 // Something comes after the "}".
1411 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001412
1413 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001414 if (ga_grow(tfgap, 1) == OK)
1415 {
1416 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1417 evalarg->eval_using_cmdline = TRUE;
1418 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001419 }
1420 else
1421 *arg = (char_u *)"";
1422
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001423 if (!evaluate)
1424 {
1425 ret = OK;
1426 goto erret;
1427 }
1428
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001429 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001430 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001431 if (ufunc == NULL)
1432 goto erret;
1433 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001434 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001435 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001436 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001437 ufunc->uf_refcount = 1;
1438 ufunc->uf_args = *newargs;
1439 newargs->ga_data = NULL;
1440 ufunc->uf_def_args = *default_args;
1441 default_args->ga_data = NULL;
1442 ufunc->uf_func_type = &t_func_any;
1443
1444 // error messages are for the first function line
1445 lnum_save = SOURCING_LNUM;
1446 SOURCING_LNUM = sourcing_lnum_top;
1447
1448 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001449 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001450 {
1451 SOURCING_LNUM = lnum_save;
1452 goto erret;
1453 }
1454
1455 // parse the return type, if any
1456 if (parse_return_type(ufunc, ret_type) == FAIL)
1457 goto erret;
1458
1459 pt = ALLOC_CLEAR_ONE(partial_T);
1460 if (pt == NULL)
1461 goto erret;
1462 pt->pt_func = ufunc;
1463 pt->pt_refcount = 1;
1464
1465 ufunc->uf_lines = newlines;
1466 newlines.ga_data = NULL;
1467 if (sandbox)
1468 ufunc->uf_flags |= FC_SANDBOX;
1469 if (!ASCII_ISUPPER(*ufunc->uf_name))
1470 ufunc->uf_flags |= FC_VIM9;
1471 ufunc->uf_script_ctx = current_sctx;
1472 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1473 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1474 set_function_type(ufunc);
1475
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001476 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1477
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001478 rettv->vval.v_partial = pt;
1479 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001480 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001481 ret = OK;
1482
1483erret:
1484 if (lnum_save >= 0)
1485 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001486 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001487 if (newargs != NULL)
1488 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001489 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001490 if (ufunc != NULL)
1491 {
1492 func_clear(ufunc, TRUE);
1493 func_free(ufunc, TRUE);
1494 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001495 return ret;
1496}
1497
1498/*
1499 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001500 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001501 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001502 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1503 */
1504 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001505get_lambda_tv(
1506 char_u **arg,
1507 typval_T *rettv,
1508 int types_optional,
1509 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001510{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001511 int evaluate = evalarg != NULL
1512 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001513 garray_T newargs;
1514 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001515 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001516 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001517 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001518 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001519 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001520 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001521 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001522 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001523 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001524 char_u *s;
1525 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001526 int *old_eval_lavars = eval_lavars_used;
1527 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001528 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001529 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001530 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001531 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001532 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001533 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001534
Bram Moolenaar4525a572022-02-13 11:57:33 +00001535 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001536 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001537
1538 ga_init(&newargs);
1539 ga_init(&newlines);
1540
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001541 // First, check if this is really a lambda expression. "->" or "=>" must
1542 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001543 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001544 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001545 types_optional ? &argtypes : NULL, types_optional,
1546 types_optional ? &arg_objm : NULL, evalarg,
1547 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001548 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001549 {
1550 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001551 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001552 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001553 ga_clear(&arg_objm);
1554 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001555 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001556 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001557
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001558 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001559 if (evaluate)
1560 pnewargs = &newargs;
1561 else
1562 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001563 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001564 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001565 types_optional ? &argtypes : NULL, types_optional,
1566 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001567 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001568 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001569 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001570 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001571 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001572 {
1573 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001574 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001575 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001576 ga_clear(&arg_objm);
1577 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001578 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001579 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001580 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001581 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001582
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001583 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1584 if (ret_type != NULL)
1585 {
1586 ret_type = vim_strsave(ret_type);
1587 tofree2 = ret_type;
1588 }
1589
Bram Moolenaare38eab22019-12-05 21:50:01 +01001590 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001591 if (evaluate)
1592 eval_lavars_used = &eval_lavars;
1593
Bram Moolenaar65c44152020-12-24 15:14:01 +01001594 *arg = skipwhite_and_linebreak(*arg, evalarg);
1595
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001596 // Recognize "{" as the start of a function body.
1597 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001598 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001599 if (evalarg == NULL)
1600 // cannot happen?
1601 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001602 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001603 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1604 types_optional ? &argtypes : NULL, varargs,
1605 &default_args, ret_type) == FAIL)
1606 goto errret;
1607 goto theend;
1608 }
1609 if (default_args.ga_len > 0)
1610 {
1611 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001612 goto errret;
1613 }
1614
Bram Moolenaare38eab22019-12-05 21:50:01 +01001615 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001616 start = *arg;
1617 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001618 if (ret == FAIL)
1619 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001620
Bram Moolenaar65c44152020-12-24 15:14:01 +01001621 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001622 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001623 *arg = skipwhite_and_linebreak(*arg, evalarg);
1624 if (**arg != '}')
1625 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001626 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001627 goto errret;
1628 }
1629 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001630 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001631
1632 if (evaluate)
1633 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001634 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001635 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001636 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001637 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001638 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001639
Bram Moolenaare01e5212023-01-08 20:31:18 +00001640 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001641 if (fp == NULL)
1642 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001643 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001644 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001645 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001646 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001647
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001648 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001649 if (ga_grow(&newlines, 1) == FAIL)
1650 goto errret;
1651
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001652 // If there are line breaks, we need to split up the string.
1653 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001654 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001655 line_end = end;
1656
1657 // Add "return " before the expression (or the first line).
1658 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001659 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001660 if (p == NULL)
1661 goto errret;
1662 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1663 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001664 vim_strncpy(p + 7, start, line_end - start);
1665
1666 if (line_end != end)
1667 {
1668 // Add more lines, split by line breaks. Thus is used when a
1669 // lambda with { cmds } is encountered.
1670 while (*line_end == '\n')
1671 {
1672 if (ga_grow(&newlines, 1) == FAIL)
1673 goto errret;
1674 start = line_end + 1;
1675 line_end = vim_strchr(start, '\n');
1676 if (line_end == NULL)
1677 line_end = end;
1678 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1679 vim_strnsave(start, line_end - start);
1680 }
1681 }
1682
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001683 if (strstr((char *)p + 7, "a:") == NULL)
1684 // No a: variables are used for sure.
1685 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001686
1687 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001688 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001689 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001690 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001691 if (types_optional)
1692 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001693 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001694 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001695 goto errret;
1696 if (ret_type != NULL)
1697 {
1698 fp->uf_ret_type = parse_type(&ret_type,
1699 &fp->uf_type_list, TRUE);
1700 if (fp->uf_ret_type == NULL)
1701 goto errret;
1702 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001703 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001704 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001705 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001706
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001707 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001708 if (current_funccal != NULL && eval_lavars)
1709 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001710 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001711 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001712 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001713 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001714
1715#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001716 if (prof_def_func())
1717 func_do_profile(fp);
1718#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001719 if (sandbox)
1720 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001721 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001722 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001723 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001724 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001725 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001726 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001727 // Use the line number of the arguments.
1728 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001729
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001730 function_using_block_scopes(fp, evalarg->eval_cstack);
1731
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001732 pt->pt_func = fp;
1733 pt->pt_refcount = 1;
1734 rettv->vval.v_partial = pt;
1735 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001736
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001737 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001738 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001739
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001740theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001741 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001742 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001743 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001744 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001745 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001746 ga_clear(&arg_objm);
1747 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001748
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001749 return OK;
1750
1751errret:
1752 ga_clear_strings(&newargs);
1753 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001754 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001755 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001756 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001757 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001758 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001759 if (fp != NULL)
1760 vim_free(fp->uf_arg_types);
1761 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001762 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001763 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001764 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001765 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001766 return FAIL;
1767}
1768
1769/*
1770 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1771 * name it contains, otherwise return "name".
1772 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1773 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001774 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1775 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001776 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001777 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001778 */
1779 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001780deref_func_name(
1781 char_u *name,
1782 int *lenp,
1783 partial_T **partialp,
1784 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001785 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001786 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001787 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788{
1789 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001790 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001791 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001792 char_u *s = NULL;
1793 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001794 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001795
1796 if (partialp != NULL)
1797 *partialp = NULL;
1798
1799 cc = name[*lenp];
1800 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001801
Bram Moolenaar71f21932022-01-07 18:20:55 +00001802 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001803 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001804 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001806 tv = &v->di_tv;
1807 }
1808 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1809 {
1810 imported_T *import;
1811 char_u *p = name;
1812 int len = *lenp;
1813
1814 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001815 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001816 p = name + 2;
1817 len -= 2;
1818 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001819 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001820
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001821 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001822 if (import != NULL)
1823 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001824 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001825 if (new_function)
1826 semsg(_(e_redefining_imported_item_str), name);
1827 else
1828 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001829 name[len] = cc;
1830 *lenp = 0;
1831 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001832 }
1833 }
1834
1835 if (tv != NULL)
1836 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001837 if (found_var != NULL)
1838 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001839 if (tv->v_type == VAR_FUNC)
1840 {
1841 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001842 {
1843 *lenp = 0;
1844 return (char_u *)""; // just in case
1845 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001846 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001847 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001848 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001849
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001850 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001851 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001852 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001853
1854 if (pt == NULL)
1855 {
1856 *lenp = 0;
1857 return (char_u *)""; // just in case
1858 }
1859 if (partialp != NULL)
1860 *partialp = pt;
1861 s = partial_name(pt);
1862 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001863 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001864
1865 if (s != NULL)
1866 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001867 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001868 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001869 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001870
1871 if (sv != NULL)
1872 *type = sv->sv_type;
1873 }
1874 return s;
1875 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001876 }
1877
1878 return name;
1879}
1880
1881/*
1882 * Give an error message with a function name. Handle <SNR> things.
1883 * "ermsg" is to be passed without translation, use N_() instead of _().
1884 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001885 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001886emsg_funcname(char *ermsg, char_u *name)
1887{
Bram Moolenaar54598062022-01-13 12:05:09 +00001888 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001889
Bram Moolenaar54598062022-01-13 12:05:09 +00001890 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001891 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001892 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001893 if (p != name)
1894 vim_free(p);
1895}
1896
1897/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001898 * Get function arguments at "*arg" and advance it.
1899 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001900 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001901 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001902 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001903get_func_arguments(
1904 char_u **arg,
1905 evalarg_T *evalarg,
1906 int partial_argc,
1907 typval_T *argvars,
Ernie Raelb077b582023-12-14 20:11:44 +01001908 int *argcount,
1909 int is_builtin)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001910{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001911 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001912 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001913 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001914 int evaluate = evalarg == NULL
1915 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001916
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001917 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001918 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001919 // skip the '(' or ',' and possibly line breaks
1920 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1921
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001922 if (*argp == ')' || *argp == ',' || *argp == NUL)
1923 break;
Ernie Raelb077b582023-12-14 20:11:44 +01001924
1925 int arg_idx = *argcount;
1926 if (eval1(&argp, &argvars[arg_idx], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001927 {
1928 ret = FAIL;
1929 break;
1930 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001931 ++*argcount;
Ernie Raelb077b582023-12-14 20:11:44 +01001932 if (!is_builtin && check_typval_is_value(&argvars[arg_idx]) == FAIL)
1933 {
1934 ret = FAIL;
1935 break;
1936 }
1937
Bram Moolenaar9d489562020-07-30 20:08:50 +02001938 // The comma should come right after the argument, but this wasn't
1939 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001940 if (vim9script)
1941 {
1942 if (*argp != ',' && *skipwhite(argp) == ',')
1943 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001944 if (evaluate)
1945 semsg(_(e_no_white_space_allowed_before_str_str),
1946 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001947 ret = FAIL;
1948 break;
1949 }
1950 }
1951 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001952 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001953 if (*argp != ',')
1954 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001955 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001956 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001957 if (evaluate)
1958 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001959 ret = FAIL;
1960 break;
1961 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001962 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001963
Bram Moolenaare6b53242020-07-01 17:28:33 +02001964 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001965 if (*argp == ')')
1966 ++argp;
1967 else
1968 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001969 *arg = argp;
1970 return ret;
1971}
1972
1973/*
1974 * Call a function and put the result in "rettv".
1975 * Return OK or FAIL.
1976 */
1977 int
1978get_func_tv(
1979 char_u *name, // name of the function
1980 int len, // length of "name" or -1 to use strlen()
1981 typval_T *rettv,
1982 char_u **arg, // argument, pointing to the '('
1983 evalarg_T *evalarg, // for line continuation
1984 funcexe_T *funcexe) // various values
1985{
1986 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001987 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001988 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1989 int argcount = 0; // number of arguments found
1990 int vim9script = in_vim9script();
1991 int evaluate = evalarg == NULL
1992 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1993
1994 argp = *arg;
1995 ret = get_func_arguments(&argp, evalarg,
1996 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
Ernie Raelb077b582023-12-14 20:11:44 +01001997 argvars, &argcount, builtin_function(name, -1));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001998
1999 if (ret == OK)
2000 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002001 int i = 0;
2002 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002003
2004 if (get_vim_var_nr(VV_TESTING))
2005 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002006 // Prepare for calling test_garbagecollect_now(), need to know
2007 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002008 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002009 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002010 for (i = 0; i < argcount; ++i)
2011 if (ga_grow(&funcargs, 1) == OK)
2012 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2013 &argvars[i];
2014 }
2015
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002016 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002017 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002018 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002019 // An error in a builtin function does not return FAIL, but we do
2020 // want to abort further processing if an error was given.
2021 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002022 clear_tv(rettv);
2023 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002024
2025 funcargs.ga_len -= i;
2026 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002027 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002028 {
2029 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002030 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002031 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002032 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002033 }
2034
2035 while (--argcount >= 0)
2036 clear_tv(&argvars[argcount]);
2037
Bram Moolenaar4525a572022-02-13 11:57:33 +00002038 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002039 *arg = argp;
2040 else
2041 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002042 return ret;
2043}
2044
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002045/*
2046 * Return TRUE if "p" starts with "<SID>" or "s:".
2047 * Only works if eval_fname_script() returned non-zero for "p"!
2048 */
2049 static int
2050eval_fname_sid(char_u *p)
2051{
2052 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2053}
2054
2055/*
2056 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2057 * Change <SNR>123_name() to K_SNR 123_name().
2058 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002059 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002060 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002061 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002062fname_trans_sid(
2063 char_u *name,
2064 char_u *fname_buf,
2065 char_u **tofree,
2066 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002067{
2068 int llen;
2069 char_u *fname;
2070 int i;
2071
2072 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002073 if (llen == 0)
2074 return name; // no prefix
2075
2076 fname_buf[0] = K_SPECIAL;
2077 fname_buf[1] = KS_EXTRA;
2078 fname_buf[2] = (int)KE_SNR;
2079 i = 3;
2080 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002081 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002082 if (current_sctx.sc_sid <= 0)
2083 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002084 else
2085 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002086 sprintf((char *)fname_buf + 3, "%ld_",
2087 (long)current_sctx.sc_sid);
2088 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002089 }
2090 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002091 if (i + STRLEN(name + llen) < FLEN_FIXED)
2092 {
2093 STRCPY(fname_buf + i, name + llen);
2094 fname = fname_buf;
2095 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002096 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002097 {
2098 fname = alloc(i + STRLEN(name + llen) + 1);
2099 if (fname == NULL)
2100 *error = FCERR_OTHER;
2101 else
2102 {
2103 *tofree = fname;
2104 mch_memmove(fname, fname_buf, (size_t)i);
2105 STRCPY(fname + i, name + llen);
2106 }
2107 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002108 return fname;
2109}
2110
2111/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002112 * Concatenate the script ID and function name into "<SNR>99_name".
2113 * "buffer" must have size MAX_FUNC_NAME_LEN.
2114 */
2115 void
2116func_name_with_sid(char_u *name, int sid, char_u *buffer)
2117{
2118 // A script-local function is stored as "<SNR>99_name".
2119 buffer[0] = K_SPECIAL;
2120 buffer[1] = KS_EXTRA;
2121 buffer[2] = (int)KE_SNR;
2122 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2123 (long)sid, name);
2124}
2125
2126/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127 * Find a function "name" in script "sid".
2128 */
2129 static ufunc_T *
2130find_func_with_sid(char_u *name, int sid)
2131{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002132 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002133 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134
Bram Moolenaarb8822442022-01-11 15:24:05 +00002135 if (!SCRIPT_ID_VALID(sid))
2136 return NULL; // not in a script
2137
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002138 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002139 hi = hash_find(&func_hashtab, buffer);
2140 if (!HASHITEM_EMPTY(hi))
2141 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002142 return NULL;
2143}
2144
2145/*
2146 * Find a function "name" in script "sid" prefixing the autoload prefix.
2147 */
2148 static ufunc_T *
2149find_func_with_prefix(char_u *name, int sid)
2150{
2151 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002152 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002153 scriptitem_T *si;
2154
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002155 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002156 return NULL; // already has the prefix
2157 if (!SCRIPT_ID_VALID(sid))
2158 return NULL; // not in a script
2159 si = SCRIPT_ITEM(sid);
2160 if (si->sn_autoload_prefix != NULL)
2161 {
2162 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2163 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002164 char_u *namep;
2165
2166 // skip a "<SNR>99_" prefix
2167 namep = untrans_function_name(name);
2168 if (namep == NULL)
2169 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002170
2171 // An exported function in an autoload script is stored as
2172 // "dir#path#name".
2173 if (len < sizeof(buffer))
2174 auto_name = buffer;
2175 else
2176 auto_name = alloc(len);
2177 if (auto_name != NULL)
2178 {
2179 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002180 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002181 hi = hash_find(&func_hashtab, auto_name);
2182 if (auto_name != buffer)
2183 vim_free(auto_name);
2184 if (!HASHITEM_EMPTY(hi))
2185 return HI2UF(hi);
2186 }
2187 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002188
2189 return NULL;
2190}
2191
2192/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002193 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002194 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2195 * functions.
2196 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002197 * Return NULL for unknown function.
2198 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002199 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002200find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002201{
2202 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002203 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002205 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002207 // Find script-local function before global one.
2208 if (in_vim9script() && eval_isnamec1(*name)
2209 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002211 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2212 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002213 if (func != NULL)
2214 return func;
2215 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002216 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2217 {
2218 char_u *p = name + 5;
2219 long sid;
2220
2221 // printable "<SNR>123_Name" form
2222 sid = getdigits(&p);
2223 if (*p == '_')
2224 {
2225 func = find_func_with_sid(p + 1, (int)sid);
2226 if (func != NULL)
2227 return func;
2228 }
2229 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002231
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002232 if ((flags & FFED_NO_GLOBAL) == 0)
2233 {
2234 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002235 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002236 if (!HASHITEM_EMPTY(hi))
2237 return HI2UF(hi);
2238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239
Bram Moolenaarb8822442022-01-11 15:24:05 +00002240 // Find autoload function if this is an autoload script.
2241 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2242 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243}
2244
2245/*
2246 * Find a function by name, return pointer to it in ufuncs.
2247 * "cctx" is passed in a :def function to find imported functions.
2248 * Return NULL for unknown or dead function.
2249 */
2250 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002251find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002253 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254
2255 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2256 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002257 return NULL;
2258}
2259
2260/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002261 * Return TRUE if "ufunc" is a global function.
2262 */
2263 int
2264func_is_global(ufunc_T *ufunc)
2265{
2266 return ufunc->uf_name[0] != K_SPECIAL;
2267}
2268
2269/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002270 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2271 */
2272 int
2273func_requires_g_prefix(ufunc_T *ufunc)
2274{
2275 return ufunc->uf_name[0] != K_SPECIAL
2276 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002277 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
2278 && !isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002279}
2280
2281/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002282 * Copy the function name of "fp" to buffer "buf".
2283 * "buf" must be able to hold the function name plus three bytes.
2284 * Takes care of script-local function names.
2285 */
2286 static void
2287cat_func_name(char_u *buf, ufunc_T *fp)
2288{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002289 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002290 {
2291 STRCPY(buf, "<SNR>");
2292 STRCAT(buf, fp->uf_name + 3);
2293 }
2294 else
2295 STRCPY(buf, fp->uf_name);
2296}
2297
2298/*
2299 * Add a number variable "name" to dict "dp" with value "nr".
2300 */
2301 static void
2302add_nr_var(
2303 dict_T *dp,
2304 dictitem_T *v,
2305 char *name,
2306 varnumber_T nr)
2307{
2308 STRCPY(v->di_key, name);
2309 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002310 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002311 v->di_tv.v_type = VAR_NUMBER;
2312 v->di_tv.v_lock = VAR_FIXED;
2313 v->di_tv.vval.v_number = nr;
2314}
2315
2316/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002317 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002318 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002319 static void
2320free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002321{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002322 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002323
Bram Moolenaarca16c602022-09-06 18:57:08 +01002324 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002325 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002326 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002327
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002328 // When garbage collecting a funccall_T may be freed before the
2329 // function that references it, clear its uf_scoped field.
2330 // The function may have been redefined and point to another
2331 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002332 if (fp != NULL && fp->uf_scoped == fc)
2333 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002334 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002335 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002336
Bram Moolenaarca16c602022-09-06 18:57:08 +01002337 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002338 vim_free(fc);
2339}
2340
2341/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002342 * Free "fc" and what it contains.
2343 * Can be called only when "fc" is kept beyond the period of it called,
2344 * i.e. after cleanup_function_call(fc).
2345 */
2346 static void
2347free_funccal_contents(funccall_T *fc)
2348{
2349 listitem_T *li;
2350
2351 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002352 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002353
2354 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002355 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002356
2357 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002358 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002359 clear_tv(&li->li_tv);
2360
2361 free_funccal(fc);
2362}
2363
2364/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002365 * Handle the last part of returning from a function: free the local hashtable.
2366 * Unless it is still in use by a closure.
2367 */
2368 static void
2369cleanup_function_call(funccall_T *fc)
2370{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002371 int may_free_fc = fc->fc_refcount <= 0;
2372 int free_fc = TRUE;
2373
Bram Moolenaarca16c602022-09-06 18:57:08 +01002374 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002375
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002376 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002377 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2378 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002379 else
2380 free_fc = FALSE;
2381
2382 // If the a:000 list and the l: and a: dicts are not referenced and
2383 // there is no closure using it, we can free the funccall_T and what's
2384 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002385 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2386 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002387 else
2388 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002389 int todo;
2390 hashitem_T *hi;
2391 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002392
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002393 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002394
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002395 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002396 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002397 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002398 {
2399 if (!HASHITEM_EMPTY(hi))
2400 {
2401 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002402 di = HI2DI(hi);
2403 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002404 }
2405 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002406 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002407
Bram Moolenaarca16c602022-09-06 18:57:08 +01002408 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2409 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002410 else
2411 {
2412 listitem_T *li;
2413
2414 free_fc = FALSE;
2415
2416 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002417 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002418 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002419 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002420
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002421 if (free_fc)
2422 free_funccal(fc);
2423 else
2424 {
2425 static int made_copy = 0;
2426
2427 // "fc" is still in use. This can happen when returning "a:000",
2428 // assigning "l:" to a global variable or defining a closure.
2429 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002430 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002431 previous_funccal = fc;
2432
2433 if (want_garbage_collect)
2434 // If garbage collector is ready, clear count.
2435 made_copy = 0;
2436 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002437 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002438 // We have made a lot of copies, worth 4 Mbyte. This can happen
2439 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002440 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002441 // too much memory.
2442 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002443 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002444 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002445 }
2446}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002447
2448/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002449 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2450 */
2451 static int
2452numbered_function(char_u *name)
2453{
2454 return isdigit(*name)
2455 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2456}
2457
2458/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002459 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002460 * 1. ordinary names, function defined with :function or :def;
2461 * can start with "<SNR>123_" literally or with K_SPECIAL.
2462 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002463 * For the first we only count the name stored in func_hashtab as a reference,
2464 * using function() does not count as a reference, because the function is
2465 * looked up by name.
2466 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002467 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002468func_name_refcount(char_u *name)
2469{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002470 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002471}
2472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473/*
2474 * Unreference "fc": decrement the reference count and free it when it
2475 * becomes zero. "fp" is detached from "fc".
2476 * When "force" is TRUE we are exiting.
2477 */
2478 static void
2479funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2480{
2481 funccall_T **pfc;
2482 int i;
2483
2484 if (fc == NULL)
2485 return;
2486
2487 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002488 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2489 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2490 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2491 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 {
2493 if (fc == *pfc)
2494 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002495 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 free_funccal_contents(fc);
2497 return;
2498 }
2499 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002500 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2501 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2502 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503}
2504
2505/*
2506 * Remove the function from the function hashtable. If the function was
2507 * deleted while it still has references this was already done.
2508 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2509 */
2510 static int
2511func_remove(ufunc_T *fp)
2512{
2513 hashitem_T *hi;
2514
2515 // Return if it was already virtually deleted.
2516 if (fp->uf_flags & FC_DEAD)
2517 return FALSE;
2518
2519 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002520 if (HASHITEM_EMPTY(hi))
2521 return FALSE;
2522
2523 // When there is a def-function index do not actually remove the
2524 // function, so we can find the index when defining the function again.
2525 // Do remove it when it's a copy.
2526 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002528 fp->uf_flags |= FC_DEAD;
2529 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002531 hash_remove(&func_hashtab, hi, "remove function");
2532 fp->uf_flags |= FC_DELETED;
2533 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534}
2535
2536 static void
2537func_clear_items(ufunc_T *fp)
2538{
2539 ga_clear_strings(&(fp->uf_args));
2540 ga_clear_strings(&(fp->uf_def_args));
2541 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002543 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002544 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002545 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002546
2547 // Increment the refcount of this function to avoid it being freed
2548 // recursively when the partial is freed.
2549 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002550 partial_unref(fp->uf_partial);
2551 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002552 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002553
2554#ifdef FEAT_LUA
2555 if (fp->uf_cb_free != NULL)
2556 {
2557 fp->uf_cb_free(fp->uf_cb_state);
2558 fp->uf_cb_free = NULL;
2559 }
2560
2561 fp->uf_cb_state = NULL;
2562 fp->uf_cb = NULL;
2563#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564#ifdef FEAT_PROFILE
2565 VIM_CLEAR(fp->uf_tml_count);
2566 VIM_CLEAR(fp->uf_tml_total);
2567 VIM_CLEAR(fp->uf_tml_self);
2568#endif
2569}
2570
2571/*
2572 * Free all things that a function contains. Does not free the function
2573 * itself, use func_free() for that.
2574 * When "force" is TRUE we are exiting.
2575 */
2576 static void
2577func_clear(ufunc_T *fp, int force)
2578{
2579 if (fp->uf_cleared)
2580 return;
2581 fp->uf_cleared = TRUE;
2582
2583 // clear this function
2584 func_clear_items(fp);
2585 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002586 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587}
2588
2589/*
2590 * Free a function and remove it from the list of functions. Does not free
2591 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002592 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002593 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002595 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002596func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597{
2598 // Only remove it when not done already, otherwise we would remove a newer
2599 // version of the function with the same name.
2600 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2601 func_remove(fp);
2602
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002603 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002604 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002605 if (fp->uf_dfunc_idx > 0)
2606 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002607 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002609 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002610 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002611 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612}
2613
2614/*
2615 * Free all things that a function contains and free the function itself.
2616 * When "force" is TRUE we are exiting.
2617 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002618 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619func_clear_free(ufunc_T *fp, int force)
2620{
2621 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002622 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2623 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002624 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002625 else
2626 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627}
2628
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002629/*
2630 * Copy already defined function "lambda" to a new function with name "global".
2631 * This is for when a compiled function defines a global function.
2632 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002633 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002634copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002635 char_u *lambda,
2636 char_u *global,
2637 loopvarinfo_T *loopvarinfo,
2638 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002639{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002640 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002641 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002642
2643 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002644 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002645 semsg(_(e_lambda_function_not_found_str), lambda);
2646 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002647 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002648
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002649 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002650 if (fp != NULL)
2651 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002652 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002653 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002654 return FAIL;
2655 }
2656
Bram Moolenaare01e5212023-01-08 20:31:18 +00002657 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002658 if (fp == NULL)
2659 return FAIL;
2660
2661 fp->uf_varargs = ufunc->uf_varargs;
2662 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2663 fp->uf_def_status = ufunc->uf_def_status;
2664 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2665 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2666 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2667 == FAIL
2668 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2669 goto failed;
2670
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002671 if (ufunc->uf_arg_types != NULL)
2672 {
2673 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2674 if (fp->uf_arg_types == NULL)
2675 goto failed;
2676 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2677 sizeof(type_T *) * fp->uf_args.ga_len);
2678 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002679 if (ufunc->uf_va_name != NULL)
2680 {
2681 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2682 if (fp->uf_va_name == NULL)
2683 goto failed;
2684 }
2685 fp->uf_ret_type = ufunc->uf_ret_type;
2686
2687 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002688
2689 fp->uf_name_exp = NULL;
2690 set_ufunc_name(fp, global);
2691
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002692 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002693
2694 // the referenced dfunc_T is now used one more time
2695 link_def_function(fp);
2696
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002697 // Create a partial to store the context of the function where it was
2698 // instantiated. Only needs to be done once. Do this on the original
2699 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002700 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2701 {
2702 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2703
2704 if (pt == NULL)
2705 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002706 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002707 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002708 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002709 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002710 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002711 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002712 }
2713
2714 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002715
2716failed:
2717 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002718 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002719}
2720
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002721static int funcdepth = 0;
2722
2723/*
2724 * Increment the function call depth count.
2725 * Return FAIL when going over 'maxfuncdepth'.
2726 * Otherwise return OK, must call funcdepth_decrement() later!
2727 */
2728 int
2729funcdepth_increment(void)
2730{
2731 if (funcdepth >= p_mfd)
2732 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002733 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002734 return FAIL;
2735 }
2736 ++funcdepth;
2737 return OK;
2738}
2739
2740 void
2741funcdepth_decrement(void)
2742{
2743 --funcdepth;
2744}
2745
2746/*
2747 * Get the current function call depth.
2748 */
2749 int
2750funcdepth_get(void)
2751{
2752 return funcdepth;
2753}
2754
2755/*
2756 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002757 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002758 * times as funcdepth_increment().
2759 */
2760 void
2761funcdepth_restore(int depth)
2762{
2763 funcdepth = depth;
2764}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002765
2766/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002767 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2768 * "rettv".
2769 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2770 * Returns NULL when allocation fails.
2771 */
2772 funccall_T *
2773create_funccal(ufunc_T *fp, typval_T *rettv)
2774{
2775 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2776
2777 if (fc == NULL)
2778 return NULL;
2779 fc->fc_caller = current_funccal;
2780 current_funccal = fc;
2781 fc->fc_func = fp;
2782 func_ptr_ref(fp);
2783 fc->fc_rettv = rettv;
2784 return fc;
2785}
2786
2787/*
2788 * To be called when returning from a compiled function; restores
2789 * current_funccal.
2790 */
2791 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002792remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002793{
2794 funccall_T *fc = current_funccal;
2795
2796 current_funccal = fc->fc_caller;
2797 free_funccal(fc);
2798}
2799
2800/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002801 * Call a user function.
2802 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002803 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002804call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002805 ufunc_T *fp, // pointer to function
2806 int argcount, // nr of args
2807 typval_T *argvars, // arguments
2808 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002809 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002810 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002811{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002812 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002813 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002814 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002815 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002816 funccall_T *fc;
2817 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002818 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002819 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002820 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002821 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002822 int i;
2823 int ai;
2824 int islambda = FALSE;
2825 char_u numbuf[NUMBUFLEN];
2826 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002827 typval_T *tv_to_free[MAX_FUNC_ARGS];
2828 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002829#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002830 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002831#endif
ichizok7e5fe382023-04-15 13:17:50 +01002832 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002833
Bram Moolenaarb2049902021-01-24 12:53:53 +01002834#ifdef FEAT_PROFILE
2835 CLEAR_FIELD(profile_info);
2836#endif
2837
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002838 // If depth of calling is getting too high, don't execute the function.
2839 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002840 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002841 rettv->v_type = VAR_NUMBER;
2842 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002843 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002844 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002845
Bram Moolenaare38eab22019-12-05 21:50:01 +01002846 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002847
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002848 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002849 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002850 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002851 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002852 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002853 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2854 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002855 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002856 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002857
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002858 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002860#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002861 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002862#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002863 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002864#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002865 if (do_profiling == PROF_YES)
2866 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002867#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002868 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002869 if (call_def_function(fp, argcount, argvars, 0,
2870 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2871 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002872 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002873#ifdef FEAT_PROFILE
2874 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002875 || (caller != NULL && caller->uf_profiling)))
2876 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002877#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002878 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002879 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002880 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 }
2882
Bram Moolenaar38453522021-11-28 22:00:12 +00002883 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002884
2885 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002886 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2887 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2888 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002889 */
2890 /*
2891 * Init l: variables.
2892 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002893 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002894 if (selfdict != NULL)
2895 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002896 // Set l:self to "selfdict". Use "name" to avoid a warning from
2897 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002898 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002899 name = v->di_key;
2900 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002901 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002902 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002903 v->di_tv.v_type = VAR_DICT;
2904 v->di_tv.v_lock = 0;
2905 v->di_tv.vval.v_dict = selfdict;
2906 ++selfdict->dv_refcount;
2907 }
2908
2909 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002910 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002911 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002912 * Set a:000 to a list with room for the "..." arguments.
2913 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002914 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002915 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002916 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002917 (varnumber_T)(argcount >= fp->uf_args.ga_len
2918 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002919 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002920 if ((fp->uf_flags & FC_NOARGS) == 0)
2921 {
2922 // Use "name" to avoid a warning from some compiler that checks the
2923 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002924 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002925 name = v->di_key;
2926 STRCPY(name, "000");
2927 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002928 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002929 v->di_tv.v_type = VAR_LIST;
2930 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002931 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002932 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002933 CLEAR_FIELD(fc->fc_l_varlist);
2934 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2935 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002936
2937 /*
2938 * Set a:firstline to "firstline" and a:lastline to "lastline".
2939 * Set a:name to named arguments.
2940 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002941 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002942 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002943 if ((fp->uf_flags & FC_NOARGS) == 0)
2944 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002945 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2946 "firstline", (varnumber_T)funcexe->fe_firstline);
2947 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2948 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002949 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002950 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002951 {
2952 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002953 typval_T def_rettv;
2954 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002955
2956 ai = i - fp->uf_args.ga_len;
2957 if (ai < 0)
2958 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002959 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002960 name = FUNCARG(fp, i);
2961 if (islambda)
2962 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002963
2964 // evaluate named argument default expression
2965 isdefault = ai + fp->uf_def_args.ga_len >= 0
2966 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2967 && argvars[i].vval.v_number == VVAL_NONE));
2968 if (isdefault)
2969 {
2970 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002971
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002972 def_rettv.v_type = VAR_NUMBER;
2973 def_rettv.vval.v_number = -1;
2974
2975 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2976 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002977 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002978 {
2979 default_arg_err = 1;
2980 break;
2981 }
2982 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983 }
2984 else
2985 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002986 if ((fp->uf_flags & FC_NOARGS) != 0)
2987 // Bail out if no a: arguments used (in lambda).
2988 break;
2989
Bram Moolenaare38eab22019-12-05 21:50:01 +01002990 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002991 sprintf((char *)numbuf, "%d", ai + 1);
2992 name = numbuf;
2993 }
2994 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2995 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002996 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002997 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002998 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002999 }
3000 else
3001 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003002 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003003 if (v == NULL)
3004 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003005 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003006 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003007
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003008 // Note: the values are copied directly to avoid alloc/free.
3009 // "argvars" must have VAR_FIXED for v_lock.
3010 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003011 v->di_tv.v_lock = VAR_FIXED;
3012
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003013 if (isdefault)
3014 // Need to free this later, no matter where it's stored.
3015 tv_to_free[tv_to_free_len++] = &v->di_tv;
3016
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003017 if (addlocal)
3018 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003019 // Named arguments should be accessed without the "a:" prefix in
3020 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003021 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003022 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003023 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003024 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003025 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003026
3027 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3028 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003029 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003030
3031 li->li_tv = argvars[i];
3032 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003033 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003034 }
3035 }
3036
Bram Moolenaare38eab22019-12-05 21:50:01 +01003037 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003038 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003039
3040 if (fp->uf_flags & FC_SANDBOX)
3041 {
3042 using_sandbox = TRUE;
3043 ++sandbox;
3044 }
3045
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003046 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003047 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003048 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003049 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003050 ++no_wait_return;
3051 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003052
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003053 smsg(_("calling %s"), SOURCING_NAME);
3054 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003055 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003056 char_u buf[MSG_BUF_LEN];
3057 char_u numbuf2[NUMBUFLEN];
3058 char_u *tofree;
3059 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003060
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003061 msg_puts("(");
3062 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003063 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003064 if (i > 0)
3065 msg_puts(", ");
3066 if (argvars[i].v_type == VAR_NUMBER)
3067 msg_outnum((long)argvars[i].vval.v_number);
3068 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003069 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003070 // Do not want errors such as E724 here.
3071 ++emsg_off;
3072 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3073 --emsg_off;
3074 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003075 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003076 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003077 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003078 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3079 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003080 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003081 msg_puts((char *)s);
3082 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003083 }
3084 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003085 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003086 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003087 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003088 msg_puts("\n"); // don't overwrite this either
3089
3090 verbose_leave_scroll();
3091 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003092 }
3093#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003094 if (do_profiling == PROF_YES)
3095 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003096 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003097#endif
3098
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003099 // "legacy" does not apply to commands in the function
3100 sticky_cmdmod_flags = 0;
3101
Bram Moolenaar98aff652022-09-06 21:02:35 +01003102 // If called from a compiled :def function the execution context must be
3103 // hidden, any deferred functions need to be added to the function being
3104 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003105 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003106
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003107 save_current_sctx = current_sctx;
3108 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003109 save_did_emsg = did_emsg;
3110 did_emsg = FALSE;
3111
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003112 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003113 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003114 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003115 retval = FCERR_FAILED;
3116 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003117 else if (islambda)
3118 {
3119 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3120
3121 // A Lambda always has the command "return {expr}". It is much faster
3122 // to evaluate {expr} directly.
3123 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003124 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003125 --ex_nesting_level;
3126 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003127 else
3128 // call do_cmdline() to execute the lines
3129 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003130 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3131
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003132 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003133 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003134
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003135 if (RedrawingDisabled > 0)
3136 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003137
Bram Moolenaare38eab22019-12-05 21:50:01 +01003138 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003139 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3140 {
3141 clear_tv(rettv);
3142 rettv->v_type = VAR_NUMBER;
3143 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003144
3145 // In corner cases returning a "failed" value is not backwards
3146 // compatible. Only do this for Vim9 script.
3147 if (in_vim9script())
3148 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003149 }
3150
3151#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003152 if (do_profiling == PROF_YES)
3153 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003154 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003155
3156 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3157 profile_may_end_func(&profile_info, fp, caller);
3158 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003159#endif
3160
Bram Moolenaare38eab22019-12-05 21:50:01 +01003161 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003162 if (p_verbose >= 12)
3163 {
3164 ++no_wait_return;
3165 verbose_enter_scroll();
3166
3167 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003168 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003169 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003170 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003171 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003172 else
3173 {
3174 char_u buf[MSG_BUF_LEN];
3175 char_u numbuf2[NUMBUFLEN];
3176 char_u *tofree;
3177 char_u *s;
3178
Bram Moolenaare38eab22019-12-05 21:50:01 +01003179 // The value may be very long. Skip the middle part, so that we
3180 // have some idea how it starts and ends. smsg() would always
3181 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003182 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003183 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003184 --emsg_off;
3185 if (s != NULL)
3186 {
3187 if (vim_strsize(s) > MSG_BUF_CLEN)
3188 {
3189 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3190 s = buf;
3191 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003192 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003193 vim_free(tofree);
3194 }
3195 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003196 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003197
3198 verbose_leave_scroll();
3199 --no_wait_return;
3200 }
3201
ichizok7e5fe382023-04-15 13:17:50 +01003202 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003203 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003204 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003205 restore_current_ectx(save_current_ectx);
3206
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003207#ifdef FEAT_PROFILE
3208 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003209 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003210#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003211 if (using_sandbox)
3212 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003213 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003214
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003215 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003216 {
3217 ++no_wait_return;
3218 verbose_enter_scroll();
3219
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003220 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003221 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003222
3223 verbose_leave_scroll();
3224 --no_wait_return;
3225 }
3226
3227 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003228 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003229 for (i = 0; i < tv_to_free_len; ++i)
3230 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003231 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003232
3233 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003234}
3235
3236/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003237 * Check the argument count for user function "fp".
3238 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3239 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003240 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003241check_user_func_argcount(ufunc_T *fp, int argcount)
3242{
3243 int regular_args = fp->uf_args.ga_len;
3244
3245 if (argcount < regular_args - fp->uf_def_args.ga_len)
3246 return FCERR_TOOFEW;
3247 else if (!has_varargs(fp) && argcount > regular_args)
3248 return FCERR_TOOMANY;
3249 return FCERR_UNKNOWN;
3250}
3251
3252/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003254 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003255 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256call_user_func_check(
3257 ufunc_T *fp,
3258 int argcount,
3259 typval_T *argvars,
3260 typval_T *rettv,
3261 funcexe_T *funcexe,
3262 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003263{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003264 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003265
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003266#ifdef FEAT_LUA
3267 if (fp->uf_flags & FC_CFUNC)
3268 {
3269 cfunc_T cb = fp->uf_cb;
3270
3271 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3272 }
3273#endif
3274
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003275 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3276 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003277 error = check_user_func_argcount(fp, argcount);
3278 if (error != FCERR_UNKNOWN)
3279 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003280
Bram Moolenaar50824712020-12-20 21:10:17 +01003281 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003282 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003284 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003286 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 int did_save_redo = FALSE;
3288 save_redo_T save_redo;
3289
3290 /*
3291 * Call the user function.
3292 * Save and restore search patterns, script variables and
3293 * redo buffer.
3294 */
3295 save_search_patterns();
3296 if (!ins_compl_active())
3297 {
3298 saveRedobuff(&save_redo);
3299 did_save_redo = TRUE;
3300 }
3301 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003302 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003303 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3305 // Function was unreferenced while being used, free it now.
3306 func_clear_free(fp, FALSE);
3307 if (did_save_redo)
3308 restoreRedobuff(&save_redo);
3309 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003310 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003311
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003313}
3314
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003315static funccal_entry_T *funccal_stack = NULL;
3316
3317/*
3318 * Save the current function call pointer, and set it to NULL.
3319 * Used when executing autocommands and for ":source".
3320 */
3321 void
3322save_funccal(funccal_entry_T *entry)
3323{
3324 entry->top_funccal = current_funccal;
3325 entry->next = funccal_stack;
3326 funccal_stack = entry;
3327 current_funccal = NULL;
3328}
3329
3330 void
3331restore_funccal(void)
3332{
3333 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003334 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003335 else
3336 {
3337 current_funccal = funccal_stack->top_funccal;
3338 funccal_stack = funccal_stack->next;
3339 }
3340}
3341
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003342 funccall_T *
3343get_current_funccal(void)
3344{
3345 return current_funccal;
3346}
3347
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003348/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003349 * Return TRUE when currently at the script level:
3350 * - not in a function
3351 * - not executing an autocommand
3352 * Note that when an autocommand sources a script the result is FALSE;
3353 */
3354 int
3355at_script_level(void)
3356{
3357 return current_funccal == NULL && autocmd_match == NULL;
3358}
3359
3360/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003361 * Mark all functions of script "sid" as deleted.
3362 */
3363 void
3364delete_script_functions(int sid)
3365{
3366 hashitem_T *hi;
3367 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003368 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003369 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003370 size_t len;
3371
3372 buf[0] = K_SPECIAL;
3373 buf[1] = KS_EXTRA;
3374 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003375 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003376 len = STRLEN(buf);
3377
Bram Moolenaarce658352020-07-31 23:47:12 +02003378 while (todo > 0)
3379 {
3380 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003381 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003382 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003383 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003384 fp = HI2UF(hi);
3385 if (STRNCMP(fp->uf_name, buf, len) == 0)
3386 {
3387 int changed = func_hashtab.ht_changed;
3388
3389 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003390
3391 if (fp->uf_calls > 0)
3392 {
3393 // Function is executing, don't free it but do remove
3394 // it from the hashtable.
3395 if (func_remove(fp))
3396 fp->uf_refcount--;
3397 }
3398 else
3399 {
3400 func_clear(fp, TRUE);
3401 // When clearing a function another function can be
3402 // cleared as a side effect. When that happens start
3403 // over.
3404 if (changed != func_hashtab.ht_changed)
3405 break;
3406 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003407 }
3408 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003409 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003410 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003411}
3412
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003413#if defined(EXITFREE) || defined(PROTO)
3414 void
3415free_all_functions(void)
3416{
3417 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003418 ufunc_T *fp;
3419 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003420 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003421 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003422
Bram Moolenaare38eab22019-12-05 21:50:01 +01003423 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003424 while (current_funccal != NULL)
3425 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003426 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003427 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003428 if (current_funccal == NULL && funccal_stack != NULL)
3429 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003430 }
3431
Bram Moolenaare38eab22019-12-05 21:50:01 +01003432 // First clear what the functions contain. Since this may lower the
3433 // reference count of a function, it may also free a function and change
3434 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003435 while (todo > 0)
3436 {
3437 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003438 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003439 if (!HASHITEM_EMPTY(hi))
3440 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 // clear the def function index now
3442 fp = HI2UF(hi);
3443 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003444 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445
Bram Moolenaare38eab22019-12-05 21:50:01 +01003446 // Only free functions that are not refcounted, those are
3447 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003448 if (func_name_refcount(fp->uf_name))
3449 ++skipped;
3450 else
3451 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003452 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003453 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003454 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003455 {
3456 skipped = 0;
3457 break;
3458 }
3459 }
3460 --todo;
3461 }
3462 }
3463
Bram Moolenaare38eab22019-12-05 21:50:01 +01003464 // Now actually free the functions. Need to start all over every time,
3465 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003466 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003467 while (func_hashtab.ht_used > skipped)
3468 {
3469 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003470 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003471 if (!HASHITEM_EMPTY(hi))
3472 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003473 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003474 // Only free functions that are not refcounted, those are
3475 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003476 fp = HI2UF(hi);
3477 if (func_name_refcount(fp->uf_name))
3478 ++skipped;
3479 else
3480 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003481 if (func_free(fp, FALSE) == OK)
3482 {
3483 skipped = 0;
3484 break;
3485 }
3486 // did not actually free it
3487 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003488 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003489 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003490 }
3491 if (skipped == 0)
3492 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493
3494 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003495}
3496#endif
3497
3498/*
3499 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003500 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3501 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003502 * "len" is the length of "name", or -1 for NUL terminated.
3503 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003505builtin_function(char_u *name, int len)
3506{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003507 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003508
Bram Moolenaara26b9702020-04-18 19:53:28 +02003509 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003510 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003511 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3512 {
3513 if (name[i] == AUTOLOAD_CHAR)
3514 return FALSE;
3515 if (!eval_isnamec(name[i]))
3516 {
3517 // "name.something" is not a builtin function
3518 if (name[i] == '.')
3519 return FALSE;
3520 break;
3521 }
3522 }
3523 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003524}
3525
3526 int
3527func_call(
3528 char_u *name,
3529 typval_T *args,
3530 partial_T *partial,
3531 dict_T *selfdict,
3532 typval_T *rettv)
3533{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003534 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003535 listitem_T *item;
3536 typval_T argv[MAX_FUNC_ARGS + 1];
3537 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003538 int r = 0;
3539
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003540 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003541 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003542 {
3543 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3544 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003545 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003546 break;
3547 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003548 // Make a copy of each argument. This is needed to be able to set
3549 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003550 copy_tv(&item->li_tv, &argv[argc++]);
3551 }
3552
3553 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003554 {
3555 funcexe_T funcexe;
3556
Bram Moolenaara80faa82020-04-12 19:37:17 +02003557 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003558 funcexe.fe_firstline = curwin->w_cursor.lnum;
3559 funcexe.fe_lastline = curwin->w_cursor.lnum;
3560 funcexe.fe_evaluate = TRUE;
3561 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003562 if (partial != NULL)
3563 {
3564 funcexe.fe_object = partial->pt_obj;
3565 if (funcexe.fe_object != NULL)
3566 ++funcexe.fe_object->obj_refcount;
3567 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003568 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003569 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3570 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003571
Bram Moolenaare38eab22019-12-05 21:50:01 +01003572 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003573 while (argc > 0)
3574 clear_tv(&argv[--argc]);
3575
3576 return r;
3577}
3578
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003579static int callback_depth = 0;
3580
3581 int
3582get_callback_depth(void)
3583{
3584 return callback_depth;
3585}
3586
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003587/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003588 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003589 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003590 */
3591 int
3592call_callback(
3593 callback_T *callback,
3594 int len, // length of "name" or -1 to use strlen()
3595 typval_T *rettv, // return value goes here
3596 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003597 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003598 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003599{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003600 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003601 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003602
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003603 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3604 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003605
Christian Brabandt20764632023-11-12 16:59:29 +01003606 if (callback_depth > MAX_CALLBACK_DEPTH)
Christian Brabandt47510f32023-10-15 09:56:16 +02003607 {
3608 emsg(_(e_command_too_recursive));
3609 return FAIL;
3610 }
3611
Bram Moolenaara80faa82020-04-12 19:37:17 +02003612 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003613 funcexe.fe_evaluate = TRUE;
3614 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003615 if (callback->cb_partial != NULL)
3616 {
3617 funcexe.fe_object = callback->cb_partial->pt_obj;
3618 if (funcexe.fe_object != NULL)
3619 ++funcexe.fe_object->obj_refcount;
3620 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003621 ++callback_depth;
3622 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3623 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003624
3625 // When a :def function was called that uses :try an error would be turned
3626 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003627 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003628 {
3629 need_rethrow = FALSE;
3630 handle_did_throw();
3631 }
3632
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003633 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003634}
3635
3636/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003637 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003638 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003639 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3640 */
3641 varnumber_T
3642call_callback_retnr(
3643 callback_T *callback,
3644 int argcount, // number of "argvars"
3645 typval_T *argvars) // vars for arguments, must have "argcount"
3646 // PLUS ONE elements!
3647{
3648 typval_T rettv;
3649 varnumber_T retval;
3650
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003651 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003652 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003653
3654 retval = tv_get_number_chk(&rettv, NULL);
3655 clear_tv(&rettv);
3656 return retval;
3657}
3658
3659/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 * Give an error message for the result of a function.
3661 * Nothing if "error" is FCERR_NONE.
3662 */
3663 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003664user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665{
3666 switch (error)
3667 {
3668 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003669 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003670 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003671 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003672 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 break;
3674 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003675 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 break;
3677 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003678 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 break;
3680 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003681 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 break;
3683 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003684 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 break;
3686 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003687 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 break;
3689 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003690 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3691 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003693 case FCERR_OTHER:
3694 case FCERR_FAILED:
3695 // assume the error message was already given
3696 break;
3697 case FCERR_NONE:
3698 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 }
3700}
3701
3702/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003703 * Check the argument types "argvars[argcount]" for "name" using the
3704 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3705 * is already included in "argvars[]".
3706 * Will do nothing if "funcexe->fe_check_type" is NULL or
3707 * "funcexe->fe_evaluate" is FALSE;
3708 * Returns an FCERR_ value.
3709 */
3710 static funcerror_T
3711may_check_argument_types(
3712 funcexe_T *funcexe,
3713 typval_T *argvars,
3714 int argcount,
3715 int base_included,
3716 char_u *name)
3717{
3718 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3719 {
3720 // Check that the argument types are OK for the types of the funcref.
3721 if (check_argument_types(funcexe->fe_check_type,
3722 argvars, argcount,
3723 base_included ? NULL : funcexe->fe_basetv,
3724 name) == FAIL)
3725 return FCERR_OTHER;
3726 }
3727 return FCERR_NONE;
3728}
3729
3730/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003731 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003732 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003733 * Return FAIL when the function can't be called, OK otherwise.
3734 * Also returns OK when an error was encountered while executing the function.
3735 */
3736 int
3737call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003738 char_u *funcname, // name of the function
3739 int len, // length of "name" or -1 to use strlen()
3740 typval_T *rettv, // return value goes here
3741 int argcount_in, // number of "argvars"
3742 typval_T *argvars_in, // vars for arguments, must have "argcount"
3743 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003744 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003745{
3746 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003747 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003748 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003749 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003750 char_u fname_buf[FLEN_FIXED + 1];
3751 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003752 char_u *fname = NULL;
3753 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003754 int argcount = argcount_in;
3755 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003756 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003757 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003758 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003759 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003760 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003761 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003762 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003763 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003764
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003765 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3766 // even when call_func() returns FAIL.
3767 rettv->v_type = VAR_UNKNOWN;
3768
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003769 if (partial != NULL)
3770 fp = partial->pt_func;
3771 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003772 fp = funcexe->fe_ufunc;
3773
3774 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003775 {
3776 // Make a copy of the name, if it comes from a funcref variable it
3777 // could be changed or deleted in the called function.
3778 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3779 if (name == NULL)
3780 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003781
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003782 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3783 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003784
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003785 if (funcexe->fe_doesrange != NULL)
3786 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003787
3788 if (partial != NULL)
3789 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003790 // When the function has a partial with a dict and there is a dict
3791 // argument, use the dict argument. That is backwards compatible.
3792 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003793 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003794 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003795 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003796 {
3797 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003798 {
3799 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3800 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003801 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003802 goto theend;
3803 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003804 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003805 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003806 for (i = 0; i < argcount_in; ++i)
3807 argv[i + argv_clear] = argvars_in[i];
3808 argvars = argv;
3809 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003810
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003811 if (funcexe->fe_check_type != NULL
3812 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003813 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003814 // Now funcexe->fe_check_type is missing the added arguments,
3815 // make a copy of the type with the correction.
3816 check_type = *funcexe->fe_check_type;
3817 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003818 check_type.tt_args = check_type_args;
3819 CLEAR_FIELD(check_type_args);
3820 for (i = 0; i < check_type.tt_argcount; ++i)
3821 check_type_args[i + partial->pt_argc] =
3822 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003823 check_type.tt_argcount += partial->pt_argc;
3824 check_type.tt_min_argcount += partial->pt_argc;
3825 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003826 }
3827 }
3828
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003829 if (error == FCERR_NONE)
3830 // check the argument types if possible
3831 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3832 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003833
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003834 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003835 {
3836 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003837 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003838
Bram Moolenaar333894b2020-08-01 18:53:07 +02003839 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003840 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003841 {
3842 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003843 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003844 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003845
Bram Moolenaare38eab22019-12-05 21:50:01 +01003846 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003847 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003848 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003849
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003850 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003851 {
3852 /*
3853 * User defined function.
3854 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003855 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003856 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003857 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003858 if (fp != NULL && !is_global && in_vim9script()
3859 && func_requires_g_prefix(fp))
3860 // In Vim9 script g: is required to find a global
3861 // non-autoload function.
3862 fp = NULL;
3863 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003864
Bram Moolenaare38eab22019-12-05 21:50:01 +01003865 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003866 if (fp == NULL
3867 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003868 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003869 && !aborting())
3870 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003871 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003872 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003873 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003874 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003875 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3876 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003877 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003878 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003879 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003880 if (fp == NULL)
3881 {
3882 char_u *p = untrans_function_name(rfname);
3883
3884 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003885 // Don't do this if the name starts with "s:".
3886 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003887 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003888 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003889
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003890 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003891 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003892 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003893 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003894 int need_arg_check = FALSE;
3895 if (funcexe->fe_check_type == NULL)
3896 {
3897 funcexe->fe_check_type = fp->uf_func_type;
3898 need_arg_check = TRUE;
3899 }
3900
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003901 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003902 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003903 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003904 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003905 argv_clear, fp);
3906 need_arg_check = TRUE;
3907 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003908
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003909 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003910 {
3911 // Method call: base->Method()
3912 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003913 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003914 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003915 argvars = argv;
3916 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003917 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003918 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003919
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003920 // Check the argument types now that the function type and all
3921 // argument values are known, if not done above.
3922 if (need_arg_check)
3923 error = may_check_argument_types(funcexe, argvars, argcount,
3924 TRUE, (name != NULL) ? name : funcname);
3925 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3926 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003928 }
3929 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003930 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003931 {
3932 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003933 * expr->method(): Find the method name in the table, call its
3934 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003935 */
3936 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003937 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003938 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003939 else
3940 {
3941 /*
3942 * Find the function name in the table, call its implementation.
3943 */
3944 error = call_internal_func(fname, argcount, argvars, rettv);
3945 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003946
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003947 /*
3948 * The function call (or "FuncUndefined" autocommand sequence) might
3949 * have been aborted by an error, an interrupt, or an explicitly thrown
3950 * exception that has not been caught so far. This situation can be
3951 * tested for by calling aborting(). For an error in an internal
3952 * function or for the "E132" error in call_user_func(), however, the
3953 * throw point at which the "force_abort" flag (temporarily reset by
3954 * emsg()) is normally updated has not been reached yet. We need to
3955 * update that flag first to make aborting() reliable.
3956 */
3957 update_force_abort();
3958 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003959 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003960 ret = OK;
3961
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003962theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003963 /*
3964 * Report an error unless the argument evaluation or function call has been
3965 * cancelled due to an aborting error, an interrupt, or an exception.
3966 */
3967 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003968 user_func_error(error, (name != NULL) ? name : funcname,
3969 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003970
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003971 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003972 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003973 clear_tv(&argv[--argv_clear + argv_base]);
3974
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003975 vim_free(tofree);
3976 vim_free(name);
3977
3978 return ret;
3979}
3980
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003981/*
3982 * Call a function without arguments, partial or dict.
3983 * This is like call_func() when the call is only "FuncName()".
3984 * To be used by "expr" options.
3985 * Returns NOTDONE when the function could not be found.
3986 */
3987 int
3988call_simple_func(
3989 char_u *funcname, // name of the function
3990 int len, // length of "name" or -1 to use strlen()
3991 typval_T *rettv) // return value goes here
3992{
3993 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003994 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003995 char_u fname_buf[FLEN_FIXED + 1];
3996 char_u *tofree = NULL;
3997 char_u *name;
3998 char_u *fname;
3999 char_u *rfname;
4000 int is_global = FALSE;
4001 ufunc_T *fp;
4002
4003 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4004 rettv->vval.v_number = 0;
4005
4006 // Make a copy of the name, an option can be changed in the function.
4007 name = vim_strnsave(funcname, len);
4008 if (name == NULL)
4009 return ret;
4010
4011 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4012
4013 // Skip "g:" before a function name.
4014 if (fname[0] == 'g' && fname[1] == ':')
4015 {
4016 is_global = TRUE;
4017 rfname = fname + 2;
4018 }
4019 else
4020 rfname = fname;
4021 fp = find_func(rfname, is_global);
4022 if (fp != NULL && !is_global && in_vim9script()
4023 && func_requires_g_prefix(fp))
4024 // In Vim9 script g: is required to find a global non-autoload
4025 // function.
4026 fp = NULL;
4027 if (fp == NULL)
4028 ret = NOTDONE;
4029 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4030 error = FCERR_DELETED;
4031 else if (fp != NULL)
4032 {
4033 typval_T argvars[1];
4034 funcexe_T funcexe;
4035
4036 argvars[0].v_type = VAR_UNKNOWN;
4037 CLEAR_FIELD(funcexe);
4038 funcexe.fe_evaluate = TRUE;
4039
4040 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4041 if (error == FCERR_NONE)
4042 ret = OK;
4043 }
4044
4045 user_func_error(error, name, FALSE);
4046 vim_free(tofree);
4047 vim_free(name);
4048
4049 return ret;
4050}
4051
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004052 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053printable_func_name(ufunc_T *fp)
4054{
4055 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4056}
4057
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004058/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004059 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4060 * TRUE. Otherwise return FALSE.
4061 */
4062 static int
4063function_list_modified(int prev_ht_changed)
4064{
4065 if (prev_ht_changed != func_hashtab.ht_changed)
4066 {
4067 emsg(_(e_function_list_was_modified));
4068 return TRUE;
4069 }
4070 return FALSE;
4071}
4072
4073/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004074 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004075 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004076 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004077list_func_head(ufunc_T *fp, int indent)
4078{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004079 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004080 int j;
4081
4082 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004083
4084 // a timer at the more prompt may have deleted the function
4085 if (function_list_modified(prev_ht_changed))
4086 return FAIL;
4087
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004088 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004089 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004090 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004091 msg_puts("def ");
4092 else
4093 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004095 msg_putchar('(');
4096 for (j = 0; j < fp->uf_args.ga_len; ++j)
4097 {
4098 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004099 msg_puts(", ");
4100 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 if (fp->uf_arg_types != NULL)
4102 {
4103 char *tofree;
4104
4105 msg_puts(": ");
4106 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4107 vim_free(tofree);
4108 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004109 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4110 {
4111 msg_puts(" = ");
4112 msg_puts(((char **)(fp->uf_def_args.ga_data))
4113 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4114 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004115 }
4116 if (fp->uf_varargs)
4117 {
4118 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004119 msg_puts(", ");
4120 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004121 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 if (fp->uf_va_name != NULL)
4123 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004124 if (!fp->uf_varargs)
4125 {
4126 if (j)
4127 msg_puts(", ");
4128 msg_puts("...");
4129 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004131 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 {
4133 char *tofree;
4134
4135 msg_puts(": ");
4136 msg_puts(type_name(fp->uf_va_type, &tofree));
4137 vim_free(tofree);
4138 }
4139 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004140 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004141
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004142 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004143 {
4144 if (fp->uf_ret_type != &t_void)
4145 {
4146 char *tofree;
4147
4148 msg_puts(": ");
4149 msg_puts(type_name(fp->uf_ret_type, &tofree));
4150 vim_free(tofree);
4151 }
4152 }
4153 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004154 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004155 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004156 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004157 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004158 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004159 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004160 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004161 msg_clr_eos();
4162 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004163 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004164
4165 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004166}
4167
4168/*
4169 * Get a function name, translating "<SID>" and "<SNR>".
4170 * Also handles a Funcref in a List or Dictionary.
4171 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004172 * Set "*is_global" to TRUE when the function must be global, unless
4173 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004174 * flags:
4175 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004176 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004177 * TFN_QUIET: be quiet
4178 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004179 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004180 * Advances "pp" to just after the function name (if no error).
4181 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004182 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004183trans_function_name(
4184 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004185 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004186 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004187 int flags)
4188{
4189 return trans_function_name_ext(pp, is_global, skip, flags,
4190 NULL, NULL, NULL, NULL);
4191}
4192
4193/*
4194 * trans_function_name() with extra arguments.
4195 * "fdp", "partial", "type" and "ufunc" can be NULL.
4196 */
4197 char_u *
4198trans_function_name_ext(
4199 char_u **pp,
4200 int *is_global,
4201 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004202 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004203 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004204 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004205 type_T **type, // return: type of funcref
4206 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004207{
4208 char_u *name = NULL;
4209 char_u *start;
4210 char_u *end;
4211 int lead;
4212 char_u sid_buf[20];
4213 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004215 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004216 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004217 int vim9script = in_vim9script();
4218 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004219
4220 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004221 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004222 start = *pp;
4223
Bram Moolenaare38eab22019-12-05 21:50:01 +01004224 // Check for hard coded <SNR>: already translated function ID (from a user
4225 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004226 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4227 && (*pp)[2] == (int)KE_SNR)
4228 {
4229 *pp += 3;
4230 len = get_id_len(pp) + 3;
4231 return vim_strnsave(start, len);
4232 }
4233
Bram Moolenaare38eab22019-12-05 21:50:01 +01004234 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4235 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004236 lead = eval_fname_script(start);
4237 if (lead > 2)
4238 start += lead;
4239
Bram Moolenaare38eab22019-12-05 21:50:01 +01004240 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004241 end = get_lval(start, NULL, &lv, FALSE, skip,
4242 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004243 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004244 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004245 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004246 {
4247 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004248 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004249 goto theend;
4250 }
4251 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4252 {
4253 /*
4254 * Report an invalid expression in braces, unless the expression
4255 * evaluation has been cancelled due to an aborting error, an
4256 * interrupt, or an exception.
4257 */
4258 if (!aborting())
4259 {
4260 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004261 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004262 }
4263 else
4264 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4265 goto theend;
4266 }
4267
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004268 if (lv.ll_ufunc != NULL)
4269 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004270 if (ufunc != NULL)
4271 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004272 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004273 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004274 goto theend;
4275 }
4276
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004277 if (lv.ll_tv != NULL)
4278 {
4279 if (fdp != NULL)
4280 {
4281 fdp->fd_dict = lv.ll_dict;
4282 fdp->fd_newkey = lv.ll_newkey;
4283 lv.ll_newkey = NULL;
4284 fdp->fd_di = lv.ll_di;
4285 }
4286 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4287 {
4288 name = vim_strsave(lv.ll_tv->vval.v_string);
4289 *pp = end;
4290 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004291 else if (lv.ll_tv->v_type == VAR_CLASS
4292 && lv.ll_tv->vval.v_class != NULL)
4293 {
4294 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4295 *pp = end;
4296 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004297 else if (lv.ll_tv->v_type == VAR_PARTIAL
4298 && lv.ll_tv->vval.v_partial != NULL)
4299 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004300 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004301 *pp = end;
4302 if (partial != NULL)
4303 *partial = lv.ll_tv->vval.v_partial;
4304 }
4305 else
4306 {
4307 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4308 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004309 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004310 else
4311 *pp = end;
4312 name = NULL;
4313 }
4314 goto theend;
4315 }
4316
4317 if (lv.ll_name == NULL)
4318 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004319 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004320 *pp = end;
4321 goto theend;
4322 }
4323
Bram Moolenaare38eab22019-12-05 21:50:01 +01004324 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004325 if (lv.ll_exp_name != NULL)
4326 {
4327 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004328 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004329 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004330 if (name == lv.ll_exp_name)
4331 name = NULL;
4332 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004333 else if (lv.ll_sid > 0)
4334 {
4335 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4336 int cc = *lv.ll_name_end;
4337
4338 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4339 *lv.ll_name_end = NUL;
4340 if (si->sn_autoload_prefix != NULL)
4341 {
4342 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4343 }
4344 else
4345 {
4346 sid_buf[0] = K_SPECIAL;
4347 sid_buf[1] = KS_EXTRA;
4348 sid_buf[2] = (int)KE_SNR;
4349 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004350 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004351 name = concat_str(sid_buf, lv.ll_name);
4352 }
4353 *lv.ll_name_end = cc;
4354 *pp = end;
4355 goto theend;
4356 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004357 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004358 {
4359 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004360 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004361 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004362 if (name == *pp)
4363 name = NULL;
4364 }
4365 if (name != NULL)
4366 {
4367 name = vim_strsave(name);
4368 *pp = end;
4369 if (STRNCMP(name, "<SNR>", 5) == 0)
4370 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004371 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004372 name[0] = K_SPECIAL;
4373 name[1] = KS_EXTRA;
4374 name[2] = (int)KE_SNR;
4375 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4376 }
4377 goto theend;
4378 }
4379
4380 if (lv.ll_exp_name != NULL)
4381 {
4382 len = (int)STRLEN(lv.ll_exp_name);
4383 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4384 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4385 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004386 // When there was "s:" already or the name expanded to get a
4387 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004388 lv.ll_name += 2;
4389 len -= 2;
4390 lead = 2;
4391 }
4392 }
4393 else
4394 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004395 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004396 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004397 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004398 if (lv.ll_name[0] == 'g')
4399 {
4400 if (is_global != NULL)
4401 {
4402 *is_global = TRUE;
4403 }
4404 else
4405 {
4406 // dropping "g:" without setting "is_global" won't work in
4407 // Vim9script, put it back later
4408 prefix_g = TRUE;
4409 extra = 2;
4410 }
4411 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004412 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004413 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004414 len = (int)(end - lv.ll_name);
4415 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004416 if (len <= 0)
4417 {
4418 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004419 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004420 goto theend;
4421 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004422
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004423 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004424 // starts with a lower case character: dict.func(). Or when in a class.
4425 vim9_local = ASCII_ISUPPER(*start) && vim9script
4426 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004428 /*
4429 * Copy the function name to allocated memory.
4430 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4431 * Accept <SNR>123_name() outside a script.
4432 */
4433 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004434 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004435 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004436 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004437 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004438 {
Kota Kato948a3892022-08-16 16:09:59 +01004439 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4440 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004441 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004442 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004443 goto theend;
4444 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004446 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004447 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004449 || eval_fname_sid(*pp))
4450 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004452 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004453 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004454 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004455 goto theend;
4456 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004457 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004458 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 extra = 3 + (int)STRLEN(sid_buf);
4460 else
4461 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004462 }
4463 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004464 // The function name must start with an upper case letter (unless it is a
4465 // Vim9 class new() function or a Vim9 class private method)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004466 else if (!(flags & TFN_INT)
4467 && (builtin_function(lv.ll_name, len)
4468 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004469 && !((flags & TFN_IN_CLASS)
4470 && (STRNCMP(lv.ll_name, "new", 3) == 0
4471 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004472 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004473 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004474 : e_function_name_must_start_with_capital_or_s_str),
4475 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004476 goto theend;
4477 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004478 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004479 {
4480 char_u *cp = vim_strchr(lv.ll_name, ':');
4481
4482 if (cp != NULL && cp < end)
4483 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004484 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004485 goto theend;
4486 }
4487 }
4488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004490 if (name != NULL)
4491 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004492 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004493 {
4494 name[0] = K_SPECIAL;
4495 name[1] = KS_EXTRA;
4496 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004497 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004498 STRCPY(name + 3, sid_buf);
4499 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004500 else if (prefix_g)
4501 {
4502 name[0] = 'g';
4503 name[1] = ':';
4504 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4506 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004507 }
4508 *pp = end;
4509
4510theend:
4511 clear_lval(&lv);
4512 return name;
4513}
4514
4515/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004516 * Assuming "name" is the result of trans_function_name() and it was prefixed
4517 * to use the script-local name, return the unmodified name (points into
4518 * "name"). Otherwise return NULL.
4519 * This can be used to first search for a script-local function and fall back
4520 * to the global function if not found.
4521 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004522 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004523untrans_function_name(char_u *name)
4524{
4525 char_u *p;
4526
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004527 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004528 {
4529 p = vim_strchr(name, '_');
4530 if (p != NULL)
4531 return p + 1;
4532 }
4533 return NULL;
4534}
4535
4536/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004537 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4538 * current script ID and returns the expanded function name. The caller should
4539 * free the returned name. If not called from a script context or the function
4540 * name doesn't start with these prefixes, then returns NULL.
4541 * This doesn't check whether the script-local function exists or not.
4542 */
4543 char_u *
4544get_scriptlocal_funcname(char_u *funcname)
4545{
4546 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004547 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004548 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004549 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004550
4551 if (funcname == NULL)
4552 return NULL;
4553
4554 if (STRNCMP(funcname, "s:", 2) != 0
4555 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004556 {
4557 ufunc_T *ufunc;
4558
4559 // The function name does not have a script-local prefix. Try finding
4560 // it when in a Vim9 script and there is no "g:" prefix.
4561 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4562 return NULL;
4563 ufunc = find_func(funcname, FALSE);
4564 if (ufunc == NULL || func_is_global(ufunc)
4565 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4566 return NULL;
4567 ++p;
4568 off = 0;
4569 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004570 else
4571 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004572
4573 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4574 {
4575 emsg(_(e_using_sid_not_in_script_context));
4576 return NULL;
4577 }
4578 // Expand s: prefix into <SNR>nr_<name>
4579 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4580 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004581 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004582 if (newname == NULL)
4583 return NULL;
4584 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004585 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004586
4587 return newname;
4588}
4589
4590/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004591 * Return script-local "fname" with the 3-byte sequence replaced by
4592 * printable <SNR> in allocated memory.
4593 */
4594 char_u *
4595alloc_printable_func_name(char_u *fname)
4596{
4597 char_u *n = alloc(STRLEN(fname + 3) + 6);
4598
4599 if (n != NULL)
4600 {
4601 STRCPY(n, "<SNR>");
4602 STRCPY(n + 5, fname + 3);
4603 }
4604 return n;
4605}
4606
4607/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004608 * Call trans_function_name(), except that a lambda is returned as-is.
4609 * Returns the name in allocated memory.
4610 */
4611 char_u *
4612save_function_name(
4613 char_u **name,
4614 int *is_global,
4615 int skip,
4616 int flags,
4617 funcdict_T *fudi)
4618{
4619 char_u *p = *name;
4620 char_u *saved;
4621
4622 if (STRNCMP(p, "<lambda>", 8) == 0)
4623 {
4624 p += 8;
4625 (void)getdigits(&p);
4626 saved = vim_strnsave(*name, p - *name);
4627 if (fudi != NULL)
4628 CLEAR_POINTER(fudi);
4629 }
4630 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004631 saved = trans_function_name_ext(&p, is_global, skip,
4632 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004633 *name = p;
4634 return saved;
4635}
4636
4637/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004638 * List functions. When "regmatch" is NULL all of then.
4639 * Otherwise functions matching "regmatch".
4640 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004641 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004642list_functions(regmatch_T *regmatch)
4643{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004644 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004645 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004646 hashitem_T *hi;
4647
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004648 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004649 {
4650 if (!HASHITEM_EMPTY(hi))
4651 {
4652 ufunc_T *fp = HI2UF(hi);
4653
4654 --todo;
4655 if ((fp->uf_flags & FC_DEAD) == 0
4656 && (regmatch == NULL
4657 ? !message_filtered(fp->uf_name)
4658 && !func_name_refcount(fp->uf_name)
4659 : !isdigit(*fp->uf_name)
4660 && vim_regexec(regmatch, fp->uf_name, 0)))
4661 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004662 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004663 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004664 if (function_list_modified(prev_ht_changed))
4665 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004666 }
4667 }
4668 }
4669}
4670
4671/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004672 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004673 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4674 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004675 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004676 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4677 * With CF_INTERFACE the function is define inside an interface, only the
4678 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004679 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004680 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004681 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004682define_function(
4683 exarg_T *eap,
4684 char_u *name_arg,
4685 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004686 int class_flags,
4687 ocmember_T *obj_members,
4688 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004689{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004690 int j;
4691 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004692 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004693 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004694 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004695 char_u *p;
4696 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004697 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004698 char_u *line_arg = NULL;
4699 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004701 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004702 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004703 garray_T newlines;
4704 int varargs = FALSE;
4705 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004707 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004708 int fp_allocated = FALSE;
4709 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004710 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004711 dictitem_T *v;
4712 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004713 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004714 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004715 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004716 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004717 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004718 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004719
4720 /*
4721 * ":function" without argument: list functions.
4722 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004723 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004724 {
4725 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004726 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004727 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004728 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004729 }
4730
4731 /*
4732 * ":function /pat": list functions matching pattern.
4733 */
4734 if (*eap->arg == '/')
4735 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004736 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004737 if (!eap->skip)
4738 {
4739 regmatch_T regmatch;
4740
4741 c = *p;
4742 *p = NUL;
4743 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4744 *p = c;
4745 if (regmatch.regprog != NULL)
4746 {
4747 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004748 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004749 vim_regfree(regmatch.regprog);
4750 }
4751 }
4752 if (*p == '/')
4753 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004754 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004755 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004756 }
4757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004758 ga_init(&newargs);
4759 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004760 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 ga_init(&default_args);
4762
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004763 /*
4764 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004765 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004766 * "name" == func, "fudi.fd_dict" == NULL
4767 * dict.func new dictionary entry
4768 * "name" == NULL, "fudi.fd_dict" set,
4769 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4770 * dict.func existing dict entry with a Funcref
4771 * "name" == func, "fudi.fd_dict" set,
4772 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4773 * dict.func existing dict entry that's not a Funcref
4774 * "name" == NULL, "fudi.fd_dict" set,
4775 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4776 * s:func script-local function name
4777 * g:func global function name, same as "func"
4778 */
4779 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004780 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004781 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004782 // nested function, argument is (args).
4783 paren = TRUE;
4784 CLEAR_FIELD(fudi);
4785 }
4786 else
4787 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004788 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004789 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004790 if (p[0] == 's' && p[1] == ':')
4791 {
4792 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4793 return NULL;
4794 }
4795 p = to_name_end(p, TRUE);
4796 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4797 {
4798 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4799 eap->arg);
4800 return NULL;
4801 }
4802 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004803 }
4804
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004805 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004806 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004807 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004808 paren = (vim_strchr(p, '(') != NULL);
4809 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004810 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004811 /*
4812 * Return on an invalid expression in braces, unless the expression
4813 * evaluation has been cancelled due to an aborting error, an
4814 * interrupt, or an exception.
4815 */
4816 if (!aborting())
4817 {
4818 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004819 semsg(_(e_key_not_present_in_dictionary_str),
4820 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004821 vim_free(fudi.fd_newkey);
4822 return NULL;
4823 }
4824 else
4825 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004826 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004827
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004828 // For "export def FuncName()" in an autoload script the function name
4829 // is stored with the legacy autoload name "dir#script#FuncName" so
4830 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004831 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004832 {
4833 char_u *prefixed = may_prefix_autoload(name);
4834
4835 if (prefixed != NULL && prefixed != name)
4836 {
4837 vim_free(name);
4838 name = prefixed;
4839 }
4840 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004841 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004842 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004843 {
4844 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4845 goto ret_free;
4846 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004847 }
4848
Bram Moolenaare38eab22019-12-05 21:50:01 +01004849 // An error in a function call during evaluation of an expression in magic
4850 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004851 saved_did_emsg = did_emsg;
4852 did_emsg = FALSE;
4853
4854 /*
4855 * ":function func" with only function name: list function.
4856 */
4857 if (!paren)
4858 {
4859 if (!ends_excmd(*skipwhite(p)))
4860 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004861 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004862 goto ret_free;
4863 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004864 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004865 if (eap->nextcmd != NULL)
4866 *p = NUL;
4867 if (!eap->skip && !got_int)
4868 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004869 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004870 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4871 {
4872 char_u *up = untrans_function_name(name);
4873
4874 // With Vim9 script the name was made script-local, if not
4875 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004876 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004877 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004878 }
4879
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004880 if (fp != NULL)
4881 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004882 // Check no function was added or removed from a timer, e.g. at
4883 // the more prompt. "fp" may then be invalid.
4884 int prev_ht_changed = func_hashtab.ht_changed;
4885
4886 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004887 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004888 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4889 {
4890 if (FUNCLINE(fp, j) == NULL)
4891 continue;
4892 msg_putchar('\n');
4893 msg_outnum((long)(j + 1));
4894 if (j < 9)
4895 msg_putchar(' ');
4896 if (j < 99)
4897 msg_putchar(' ');
4898 if (function_list_modified(prev_ht_changed))
4899 break;
4900 msg_prt_line(FUNCLINE(fp, j), FALSE);
4901 out_flush(); // show a line at a time
4902 ui_breakcheck();
4903 }
4904 if (!got_int)
4905 {
4906 msg_putchar('\n');
4907 if (!function_list_modified(prev_ht_changed))
4908 {
4909 if (fp->uf_def_status != UF_NOT_COMPILED)
4910 msg_puts(" enddef");
4911 else
4912 msg_puts(" endfunction");
4913 }
4914 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004915 }
4916 }
4917 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004918 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004919 }
4920 goto ret_free;
4921 }
4922
4923 /*
4924 * ":function name(arg1, arg2)" Define function.
4925 */
4926 p = skipwhite(p);
4927 if (*p != '(')
4928 {
4929 if (!eap->skip)
4930 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004931 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004932 goto ret_free;
4933 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004934 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004935 if (vim_strchr(p, '(') != NULL)
4936 p = vim_strchr(p, '(');
4937 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004938
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004939 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4940 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004941 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004942 goto ret_free;
4943 }
4944
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004945 // In Vim9 script only global functions can be redefined.
4946 if (vim9script && eap->forceit && !is_global)
4947 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004948 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004949 goto ret_free;
4950 }
4951
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004952 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004953
Bram Moolenaar04b12692020-05-04 23:24:44 +02004954 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004955 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004956 // Check the name of the function. Unless it's a dictionary function
4957 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004958 if (name != NULL)
4959 arg = name;
4960 else
4961 arg = fudi.fd_newkey;
4962 if (arg != NULL && (fudi.fd_di == NULL
4963 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4964 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4965 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004966 char_u *name_base = arg;
4967 int i;
4968
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004969 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004970 {
4971 name_base = vim_strchr(arg, '_');
4972 if (name_base == NULL)
4973 name_base = arg + 3;
4974 else
4975 ++name_base;
4976 }
4977 for (i = 0; name_base[i] != NUL && (i == 0
4978 ? eval_isnamec1(name_base[i])
4979 : eval_isnamec(name_base[i])); ++i)
4980 ;
4981 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004982 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004983
4984 // In Vim9 script a function cannot have the same name as a
4985 // variable.
4986 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004987 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4988 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004989 + EVAL_VAR_NO_FUNC) == OK)
4990 {
4991 semsg(_(e_redefining_script_item_str), name_base);
4992 goto ret_free;
4993 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004994 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004995 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004996 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004997 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004998 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004999 goto ret_free;
5000 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005001 }
5002
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005003 // This may get more lines and make the pointers into the first line
5004 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005005 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005006 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005007 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005008 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005009 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005010 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005011 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005012 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005015 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005017 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005018 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005019 if (*p != ':')
5020 {
5021 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5022 p = skipwhite(p);
5023 }
5024 else if (!IS_WHITE_OR_NUL(p[1]))
5025 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005026 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005027 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005029 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005030 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005031 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005033 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005034 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005035 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005036 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005037 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005038 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005039 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005040 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005041 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042 else
5043 // find extra arguments "range", "dict", "abort" and "closure"
5044 for (;;)
5045 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005046 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047 p = skipwhite(p);
5048 if (STRNCMP(p, "range", 5) == 0)
5049 {
5050 flags |= FC_RANGE;
5051 p += 5;
5052 }
5053 else if (STRNCMP(p, "dict", 4) == 0)
5054 {
5055 flags |= FC_DICT;
5056 p += 4;
5057 }
5058 else if (STRNCMP(p, "abort", 5) == 0)
5059 {
5060 flags |= FC_ABORT;
5061 p += 5;
5062 }
5063 else if (STRNCMP(p, "closure", 7) == 0)
5064 {
5065 flags |= FC_CLOSURE;
5066 p += 7;
5067 if (current_funccal == NULL)
5068 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005069 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 name == NULL ? (char_u *)"" : name);
5071 goto erret;
5072 }
5073 }
5074 else
5075 break;
5076 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005077
Bram Moolenaare38eab22019-12-05 21:50:01 +01005078 // When there is a line break use what follows for the function body.
5079 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005080 if (*p == '\n')
5081 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005082 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005083 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5084 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005085 && !(VIM_ISWHITE(*whitep) && *p == '#'
5086 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005087 && !eap->skip
5088 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005089 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005090
5091 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005092 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5093 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005094 */
5095 if (KeyTyped)
5096 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005097 // Check if the function already exists, don't let the user type the
5098 // whole function before telling him it doesn't work! For a script we
5099 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005100 if (!eap->skip && !eap->forceit)
5101 {
5102 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005103 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005104 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005105 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005106 }
5107
5108 if (!eap->skip && did_emsg)
5109 goto erret;
5110
Bram Moolenaare38eab22019-12-05 21:50:01 +01005111 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005112 cmdline_row = msg_row;
5113 }
5114
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005115 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005116 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005117
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005118 // Do not define the function when getting the body fails and when
5119 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005120 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005121 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005122 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5123 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005124 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005125 goto erret;
5126
5127 /*
5128 * If there are no errors, add the function
5129 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005130 if (fudi.fd_dict != NULL)
5131 {
5132 char numbuf[20];
5133
5134 fp = NULL;
5135 if (fudi.fd_newkey == NULL && !eap->forceit)
5136 {
5137 emsg(_(e_dictionary_entry_already_exists));
5138 goto erret;
5139 }
5140 if (fudi.fd_di == NULL)
5141 {
5142 // Can't add a function to a locked dictionary
5143 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5144 goto erret;
5145 }
5146 // Can't change an existing function if it is locked
5147 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5148 goto erret;
5149
5150 // Give the function a sequential number. Can only be used with a
5151 // Funcref!
5152 vim_free(name);
5153 sprintf(numbuf, "%d", ++func_nr);
5154 name = vim_strsave((char_u *)numbuf);
5155 if (name == NULL)
5156 goto erret;
5157 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005158 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005159 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005160 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005161 char_u *find_name = name;
5162 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005163 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005165 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005166 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005167 var_conflict = TRUE;
5168
5169 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5170 {
5171 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5172
5173 if (si->sn_autoload_prefix != NULL)
5174 {
5175 if (is_export)
5176 {
5177 find_name = name + STRLEN(si->sn_autoload_prefix);
5178 v = find_var(find_name, &ht, TRUE);
5179 if (v != NULL)
5180 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005181 // Only check if the function already exists in the script,
5182 // global functions can be shadowed.
5183 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005184 }
5185 else
5186 {
5187 char_u *prefixed = may_prefix_autoload(name);
5188
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005189 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005190 {
5191 v = find_var(prefixed, &ht, TRUE);
5192 if (v != NULL)
5193 var_conflict = TRUE;
5194 vim_free(prefixed);
5195 }
5196 }
5197 }
5198 }
5199 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005200 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005201 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005202 goto erret;
5203 }
5204
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005205 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005206 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005207 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005208 char_u *uname = untrans_function_name(name);
5209
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005210 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005211 }
5212
5213 if (fp != NULL || import != NULL)
5214 {
5215 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005217 // Function can be replaced with "function!" and when sourcing the
5218 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005219 // A name that is used by an import can not be overruled.
5220 if (import != NULL
5221 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005222 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005223 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005224 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005225 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005226 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005227 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005228 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005229 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005230 goto erret;
5231 }
5232 if (fp->uf_calls > 0)
5233 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005234 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005235 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005236 goto erret;
5237 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005238 if (fp->uf_refcount > 1)
5239 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005240 // This function is referenced somewhere, don't redefine it but
5241 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005242 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005243 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005244 fp = NULL;
5245 overwrite = TRUE;
5246 }
5247 else
5248 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005249 char_u *exp_name = fp->uf_name_exp;
5250
5251 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005252 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005253 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005254 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005255 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005257#ifdef FEAT_PROFILE
5258 fp->uf_profiling = FALSE;
5259 fp->uf_prof_initialized = FALSE;
5260#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005261 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005262 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005263 }
5264 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005265
5266 if (fp == NULL)
5267 {
5268 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5269 {
5270 int slen, plen;
5271 char_u *scriptname;
5272
Bram Moolenaare38eab22019-12-05 21:50:01 +01005273 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005274 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005275 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005276 {
5277 scriptname = autoload_name(name);
5278 if (scriptname != NULL)
5279 {
5280 p = vim_strchr(scriptname, '/');
5281 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005282 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005283 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005284 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005285 j = OK;
5286 vim_free(scriptname);
5287 }
5288 }
5289 if (j == FAIL)
5290 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005291 linenr_T save_lnum = SOURCING_LNUM;
5292
5293 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005294 semsg(_(e_function_name_does_not_match_script_file_name_str),
5295 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005296 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005297 goto erret;
5298 }
5299 }
5300
Bram Moolenaare01e5212023-01-08 20:31:18 +00005301 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005302 if (fp == NULL)
5303 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005304 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005305
5306 if (fudi.fd_dict != NULL)
5307 {
5308 if (fudi.fd_di == NULL)
5309 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005310 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005311 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5312 if (fudi.fd_di == NULL)
5313 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005314 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005315 goto erret;
5316 }
5317 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5318 {
5319 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005320 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005321 goto erret;
5322 }
5323 }
5324 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005325 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005326 clear_tv(&fudi.fd_di->di_tv);
5327 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005328 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005329
Bram Moolenaare38eab22019-12-05 21:50:01 +01005330 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005331 flags |= FC_DICT;
5332 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005333 }
5334 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005335 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005336 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005337 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338
5339 if (eap->cmdidx == CMD_def)
5340 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005341 int lnum_save = SOURCING_LNUM;
5342 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005343
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005344 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005345
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005346 // error messages are for the first function line
5347 SOURCING_LNUM = sourcing_lnum_top;
5348
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005349 // The function may use script variables from the context.
5350 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005351
h-eastb895b0f2023-09-24 15:46:31 +02005352 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5353 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005354 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005355 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005356 free_fp = fp_allocated;
5357 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005358 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005359 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360
5361 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005362 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005363 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005364 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005365 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005366 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005367 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005368 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005369 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005370 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005371 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005372
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005373 if (fp_allocated)
5374 {
5375 // insert the new function in the function list
5376 set_ufunc_name(fp, name);
5377 if (overwrite)
5378 {
5379 hi = hash_find(&func_hashtab, name);
5380 hi->hi_key = UF2HIKEY(fp);
5381 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005382 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005383 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005384 {
5385 free_fp = TRUE;
5386 goto erret;
5387 }
5388 fp->uf_refcount = 1;
5389 }
5390
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005391 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005392 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005393 if ((flags & FC_CLOSURE) != 0)
5394 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005395 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005396 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005397 }
5398 else
5399 fp->uf_scoped = NULL;
5400
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005401#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005402 if (prof_def_func())
5403 func_do_profile(fp);
5404#endif
5405 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005406 if (sandbox)
5407 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005408 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005409 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005410 fp->uf_flags = flags;
5411 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005412 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005413 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005414 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005415 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005416 if (is_export)
5417 {
5418 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005419 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005420 is_export = FALSE;
5421 }
5422
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005423 if (eap->cmdidx == CMD_def)
5424 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005425 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5426 // :func does not use Vim9 script syntax, even in a Vim9 script file
5427 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005428
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005429 goto ret_free;
5430
5431erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005432 if (fp != NULL)
5433 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005434 // these were set to "newargs" and "default_args", which are cleared
5435 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005436 ga_init(&fp->uf_args);
5437 ga_init(&fp->uf_def_args);
5438 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005439errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005440 ga_clear_strings(&newargs);
5441 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005442 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005443 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005444 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005445 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005446 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01005447 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005448 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005449 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005450 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005451ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005452 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005453 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005454 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005455 if (name != name_arg)
5456 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005457 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005458 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005459
5460 return fp;
5461}
5462
5463/*
5464 * ":function"
5465 */
5466 void
5467ex_function(exarg_T *eap)
5468{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005469 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005470
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005471 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005472 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005473 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005474}
5475
5476/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005477 * Find a function by name, including "<lambda>123".
5478 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005479 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005480 * Return NULL if not found.
5481 */
5482 ufunc_T *
5483find_func_by_name(char_u *name, compiletype_T *compile_type)
5484{
5485 char_u *arg = name;
5486 char_u *fname;
5487 ufunc_T *ufunc;
5488 int is_global = FALSE;
5489
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005490 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5491 {
5492 *compile_type = CT_PROFILE;
5493 arg = skipwhite(arg + 7);
5494 }
5495 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5496 {
5497 *compile_type = CT_DEBUG;
5498 arg = skipwhite(arg + 5);
5499 }
5500
5501 if (STRNCMP(arg, "<lambda>", 8) == 0)
5502 {
5503 arg += 8;
5504 (void)getdigits(&arg);
5505 fname = vim_strnsave(name, arg - name);
5506 }
5507 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005508 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005509 // First try finding a method in a class, trans_function_name() will
5510 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005511 ufunc = find_class_func(&arg);
5512 if (ufunc != NULL)
5513 return ufunc;
5514
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005515 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005516 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005517 NULL, NULL, NULL, &ufunc);
5518 if (ufunc != NULL)
5519 {
5520 vim_free(fname);
5521 return ufunc;
5522 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005523 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005524 if (fname == NULL)
5525 {
5526 semsg(_(e_invalid_argument_str), name);
5527 return NULL;
5528 }
5529 if (!ends_excmd2(name, arg))
5530 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005531 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005532 emsg(ex_errmsg(e_trailing_characters_str, arg));
5533 return NULL;
5534 }
5535
5536 ufunc = find_func(fname, is_global);
5537 if (ufunc == NULL)
5538 {
5539 char_u *p = untrans_function_name(fname);
5540
5541 if (p != NULL)
5542 // Try again without making it script-local.
5543 ufunc = find_func(p, FALSE);
5544 }
5545 vim_free(fname);
5546 if (ufunc == NULL)
5547 semsg(_(e_cannot_find_function_str), name);
5548 return ufunc;
5549}
5550
5551/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005552 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005553 * be compiled or the one specified by the argument.
5554 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005555 */
5556 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005557ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005558{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005559 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005560 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005561 compiletype_T compile_type = CT_NONE;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005562 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005563 if (ufunc != NULL)
5564 {
5565 if (func_needs_compiling(ufunc, compile_type))
5566 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5567 else
5568 smsg(_("Function %s does not need compiling"), eap->arg);
5569 }
5570 }
5571 else
5572 {
5573 long todo = (long)func_hashtab.ht_used;
5574 int changed = func_hashtab.ht_changed;
5575 hashitem_T *hi;
5576
5577 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5578 {
5579 if (!HASHITEM_EMPTY(hi))
5580 {
5581 --todo;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005582 ufunc_T *ufunc = HI2UF(hi);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005583 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5584 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5585 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005586 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005587 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5588
5589 if (func_hashtab.ht_changed != changed)
5590 {
5591 // a function has been added or removed, need to start
5592 // over
5593 todo = (long)func_hashtab.ht_used;
5594 changed = func_hashtab.ht_changed;
5595 hi = func_hashtab.ht_array;
5596 --hi;
5597 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005598 }
5599 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005600 }
5601 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005602}
5603
5604/*
5605 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5606 * Return 2 if "p" starts with "s:".
5607 * Return 0 otherwise.
5608 */
5609 int
5610eval_fname_script(char_u *p)
5611{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005612 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5613 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005614 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5615 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5616 return 5;
5617 if (p[0] == 's' && p[1] == ':')
5618 return 2;
5619 return 0;
5620}
5621
5622 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005623translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005624{
5625 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005626 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005627 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005628}
5629
5630/*
5631 * Return TRUE when "ufunc" has old-style "..." varargs
5632 * or named varargs "...name: type".
5633 */
5634 int
5635has_varargs(ufunc_T *ufunc)
5636{
5637 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005638}
5639
5640/*
5641 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005642 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005643 */
5644 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005645function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005646{
5647 char_u *nm = name;
5648 char_u *p;
5649 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005650 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005651 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005652
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005653 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5654 if (no_deref)
5655 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005656 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005657 nm = skipwhite(nm);
5658
Bram Moolenaare38eab22019-12-05 21:50:01 +01005659 // Only accept "funcname", "funcname ", "funcname (..." and
5660 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005661 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005662 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005663 vim_free(p);
5664 return n;
5665}
5666
Bram Moolenaar113e1072019-01-20 15:30:40 +01005667#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005668 char_u *
5669get_expanded_name(char_u *name, int check)
5670{
5671 char_u *nm = name;
5672 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005673 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005674
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005675 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005676
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005677 if (p != NULL && *nm == NUL
5678 && (!check || translated_function_exists(p, is_global)))
5679 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005680
5681 vim_free(p);
5682 return NULL;
5683}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005684#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005685
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005686/*
5687 * Function given to ExpandGeneric() to obtain the list of user defined
5688 * function names.
5689 */
5690 char_u *
5691get_user_func_name(expand_T *xp, int idx)
5692{
5693 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005694 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005695 static hashitem_T *hi;
5696 ufunc_T *fp;
5697
5698 if (idx == 0)
5699 {
5700 done = 0;
5701 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005702 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005703 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005704 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005705 {
5706 if (done++ > 0)
5707 ++hi;
5708 while (HASHITEM_EMPTY(hi))
5709 ++hi;
5710 fp = HI2UF(hi);
5711
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005712 // don't show dead, dict and lambda functions
5713 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005714 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005715 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005716
5717 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005718 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005719
5720 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005721 if (xp->xp_context != EXPAND_USER_FUNC
5722 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005723 {
5724 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005725 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005726 STRCAT(IObuff, ")");
5727 }
5728 return IObuff;
5729 }
5730 return NULL;
5731}
5732
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005733/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005734 * Make a copy of a function.
5735 * Intended to be used for a function defined on a base class that has a copy
5736 * on the child class.
5737 * The copy has uf_refcount set to one.
5738 * Returns NULL when out of memory.
5739 */
5740 ufunc_T *
5741copy_function(ufunc_T *fp)
5742{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005743 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005744 if (ufunc == NULL)
5745 return NULL;
5746
5747 // Most things can just be copied.
5748 *ufunc = *fp;
5749
5750 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5751 ufunc->uf_dfunc_idx = 0;
5752 ufunc->uf_class = NULL;
5753
5754 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5755 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5756
5757 if (ufunc->uf_arg_types != NULL)
5758 {
5759 // "uf_arg_types" is an allocated array, make a copy.
5760 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5761 if (at != NULL)
5762 {
5763 mch_memmove(at, ufunc->uf_arg_types,
5764 sizeof(type_T *) * ufunc->uf_args.ga_len);
5765 ufunc->uf_arg_types = at;
5766 }
5767 }
5768
5769 // TODO: how about the types themselves? they can be freed when the
5770 // original function is freed:
5771 // type_T **uf_arg_types;
5772 // type_T *uf_ret_type;
5773
Ernie Rael114ec812023-06-04 18:11:35 +01005774 // make uf_type_list empty
5775 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005776
5777 // TODO: partial_T *uf_partial;
5778
5779 if (ufunc->uf_va_name != NULL)
5780 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5781
5782 // TODO:
5783 // type_T *uf_va_type;
5784 // type_T *uf_func_type;
5785
5786 ufunc->uf_block_depth = 0;
5787 ufunc->uf_block_ids = NULL;
5788
5789 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5790
5791 ufunc->uf_refcount = 1;
5792 ufunc->uf_name_exp = NULL;
5793 STRCPY(ufunc->uf_name, fp->uf_name);
5794
5795 return ufunc;
5796}
5797
5798/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005799 * ":delfunction {name}"
5800 */
5801 void
5802ex_delfunction(exarg_T *eap)
5803{
5804 ufunc_T *fp = NULL;
5805 char_u *p;
5806 char_u *name;
5807 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005808 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005809
5810 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005811 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5812 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005813 vim_free(fudi.fd_newkey);
5814 if (name == NULL)
5815 {
5816 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005817 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005818 return;
5819 }
5820 if (!ends_excmd(*skipwhite(p)))
5821 {
5822 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005823 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005824 return;
5825 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005826 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005827 if (eap->nextcmd != NULL)
5828 *p = NUL;
5829
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005830 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005831 {
5832 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005833 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005834 vim_free(name);
5835 return;
5836 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005837 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005838 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005839 vim_free(name);
5840
5841 if (!eap->skip)
5842 {
5843 if (fp == NULL)
5844 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005845 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005846 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005847 return;
5848 }
5849 if (fp->uf_calls > 0)
5850 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005851 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005852 return;
5853 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005854 if (fp->uf_flags & FC_VIM9)
5855 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005856 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005857 return;
5858 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005859
5860 if (fudi.fd_dict != NULL)
5861 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005862 // Delete the dict item that refers to the function, it will
5863 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005864 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005865 }
5866 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005867 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005868 // A normal function (not a numbered function or lambda) has a
5869 // refcount of 1 for the entry in the hashtable. When deleting
5870 // it and the refcount is more than one, it should be kept.
5871 // A numbered function and lambda should be kept if the refcount is
5872 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005873 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005874 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005875 // Function is still referenced somewhere. Don't free it but
5876 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005877 if (func_remove(fp))
5878 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005879 }
5880 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005881 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005882 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005883 }
5884}
5885
5886/*
5887 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005888 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005889 */
5890 void
5891func_unref(char_u *name)
5892{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005893 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005894
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005895 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005896 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005897 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005898 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005899 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005900#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005901 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005902#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005903 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005904 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005905 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005906}
5907
5908/*
5909 * Unreference a Function: decrement the reference count and free it when it
5910 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005911 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005912 */
5913 void
5914func_ptr_unref(ufunc_T *fp)
5915{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005916 if (fp != NULL && (--fp->uf_refcount <= 0
5917 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5918 && fp->uf_partial->pt_refcount <= 1
5919 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005920 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005921 // Only delete it when it's not being used. Otherwise it's done
5922 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005923 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005924 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005925 }
5926}
5927
5928/*
5929 * Count a reference to a Function.
5930 */
5931 void
5932func_ref(char_u *name)
5933{
5934 ufunc_T *fp;
5935
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005936 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005937 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005938 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005939 if (fp != NULL)
5940 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005941 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005942 // Only give an error for a numbered function.
5943 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005944 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005945}
5946
5947/*
5948 * Count a reference to a Function.
5949 */
5950 void
5951func_ptr_ref(ufunc_T *fp)
5952{
5953 if (fp != NULL)
5954 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005955}
5956
5957/*
5958 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5959 * referenced from anywhere that is in use.
5960 */
5961 static int
5962can_free_funccal(funccall_T *fc, int copyID)
5963{
Bram Moolenaarca16c602022-09-06 18:57:08 +01005964 return (fc->fc_l_varlist.lv_copyID != copyID
5965 && fc->fc_l_vars.dv_copyID != copyID
5966 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005967 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005968}
5969
5970/*
5971 * ":return [expr]"
5972 */
5973 void
5974ex_return(exarg_T *eap)
5975{
5976 char_u *arg = eap->arg;
5977 typval_T rettv;
5978 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005979 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005980
5981 if (current_funccal == NULL)
5982 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005983 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005984 return;
5985 }
5986
Bram Moolenaar844fb642021-10-23 13:32:30 +01005987 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005988 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5989
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005990 if (eap->skip)
5991 ++emsg_skip;
5992
5993 eap->nextcmd = NULL;
5994 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005995 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005996 {
5997 if (!eap->skip)
5998 returning = do_return(eap, FALSE, TRUE, &rettv);
5999 else
6000 clear_tv(&rettv);
6001 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01006002 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006003 else if (!eap->skip)
6004 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006005 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006006 update_force_abort();
6007
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006008 /*
6009 * Return unless the expression evaluation has been cancelled due to an
6010 * aborting error, an interrupt, or an exception.
6011 */
6012 if (!aborting())
6013 returning = do_return(eap, FALSE, TRUE, NULL);
6014 }
6015
Bram Moolenaare38eab22019-12-05 21:50:01 +01006016 // When skipping or the return gets pending, advance to the next command
6017 // in this line (!returning). Otherwise, ignore the rest of the line.
6018 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006019 if (returning)
6020 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006021 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006022 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006023
6024 if (eap->skip)
6025 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006026 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006027}
6028
zeertzjq5299c092023-04-12 20:48:16 +01006029/*
6030 * Lower level implementation of "call". Only called when not skipping.
6031 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006032 static int
6033ex_call_inner(
6034 exarg_T *eap,
6035 char_u *name,
6036 char_u **arg,
6037 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006038 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006039 evalarg_T *evalarg)
6040{
6041 linenr_T lnum;
6042 int doesrange;
6043 typval_T rettv;
6044 int failed = FALSE;
6045
zeertzjq5299c092023-04-12 20:48:16 +01006046 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006047 for ( ; lnum <= eap->line2; ++lnum)
6048 {
6049 funcexe_T funcexe;
6050
zeertzjq5299c092023-04-12 20:48:16 +01006051 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006052 {
6053 if (lnum > curbuf->b_ml.ml_line_count)
6054 {
6055 // If the function deleted lines or switched to another buffer
6056 // the line number may become invalid.
6057 emsg(_(e_invalid_range));
6058 break;
6059 }
6060 curwin->w_cursor.lnum = lnum;
6061 curwin->w_cursor.col = 0;
6062 curwin->w_cursor.coladd = 0;
6063 }
6064 *arg = startarg;
6065
6066 funcexe = *funcexe_init;
6067 funcexe.fe_doesrange = &doesrange;
6068 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6069 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6070 {
6071 failed = TRUE;
6072 break;
6073 }
6074 if (has_watchexpr())
6075 dbg_check_breakpoint(eap);
6076
6077 // Handle a function returning a Funcref, Dictionary or List.
6078 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006079 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006080 {
6081 failed = TRUE;
6082 break;
6083 }
6084
6085 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006086 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006087 break;
6088
6089 // Stop when immediately aborting on error, or when an interrupt
6090 // occurred or an exception was thrown but not caught.
6091 // get_func_tv() returned OK, so that the check for trailing
6092 // characters below is executed.
6093 if (aborting())
6094 break;
6095 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006096 return failed;
6097}
6098
6099/*
6100 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6101 * Returns FAIL or OK.
6102 */
6103 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006104ex_defer_inner(
6105 char_u *name,
6106 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006107 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006108 partial_T *partial,
6109 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006110{
6111 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006112 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006113 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006114 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006115
6116 if (current_funccal == NULL)
6117 {
6118 semsg(_(e_str_not_inside_function), "defer");
6119 return FAIL;
6120 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006121 if (partial != NULL)
6122 {
6123 if (partial->pt_dict != NULL)
6124 {
6125 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6126 return FAIL;
6127 }
6128 if (partial->pt_argc > 0)
6129 {
6130 int i;
6131
6132 partial_argc = partial->pt_argc;
6133 for (i = 0; i < partial_argc; ++i)
6134 copy_tv(&partial->pt_argv[i], &argvars[i]);
6135 }
6136 }
Ernie Raelb077b582023-12-14 20:11:44 +01006137 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006138 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006139 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006140 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006141
6142 if (r == OK)
6143 {
6144 if (type != NULL)
6145 {
6146 // Check that the arguments are OK for the types of the funcref.
6147 r = check_argument_types(type, argvars, argcount, NULL, name);
6148 }
Ernie Raelb077b582023-12-14 20:11:44 +01006149 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006150 {
6151 int idx = find_internal_func(name);
6152
6153 if (idx < 0)
6154 {
6155 emsg_funcname(e_unknown_function_str, name);
6156 r = FAIL;
6157 }
6158 else if (check_internal_func(idx, argcount) == -1)
6159 r = FAIL;
6160 }
6161 else
6162 {
6163 ufunc_T *ufunc = find_func(name, FALSE);
6164
6165 // we tolerate an unknown function here, it might be defined later
6166 if (ufunc != NULL)
6167 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006168 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006169 if (error != FCERR_UNKNOWN)
6170 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006171 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006172 r = FAIL;
6173 }
6174 }
6175 }
6176 }
6177
Bram Moolenaar86d87252022-09-05 21:21:25 +01006178 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006179 {
6180 while (--argcount >= 0)
6181 clear_tv(&argvars[argcount]);
6182 return FAIL;
6183 }
6184 return add_defer(name, argcount, argvars);
6185}
6186
6187/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006188 * Return TRUE if currently inside a function call.
6189 * Give an error message and return FALSE when not.
6190 */
6191 int
6192can_add_defer(void)
6193{
6194 if (!in_def_function() && get_current_funccal() == NULL)
6195 {
6196 semsg(_(e_str_not_inside_function), "defer");
6197 return FALSE;
6198 }
6199 return TRUE;
6200}
6201
6202/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006203 * Add a deferred call for "name" with arguments "argvars[argcount]".
6204 * Consumes "argvars[]".
6205 * Caller must check that in_def_function() returns TRUE or current_funccal is
6206 * not NULL.
6207 * Returns OK or FAIL.
6208 */
6209 int
6210add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6211{
6212 char_u *saved_name = vim_strsave(name);
6213 int argcount = argcount_arg;
6214 defer_T *dr;
6215 int ret = FAIL;
6216
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006217 if (saved_name == NULL)
6218 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006219 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006220 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006221 if (add_defer_function(saved_name, argcount, argvars) == OK)
6222 argcount = 0;
6223 }
6224 else
6225 {
6226 if (current_funccal->fc_defer.ga_itemsize == 0)
6227 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6228 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6229 goto theend;
6230 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6231 + current_funccal->fc_defer.ga_len++;
6232 dr->dr_name = saved_name;
6233 dr->dr_argcount = argcount;
6234 while (argcount > 0)
6235 {
6236 --argcount;
6237 dr->dr_argvars[argcount] = argvars[argcount];
6238 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006239 }
6240 ret = OK;
6241
6242theend:
6243 while (--argcount >= 0)
6244 clear_tv(&argvars[argcount]);
6245 return ret;
6246}
6247
6248/*
6249 * Invoked after a functions has finished: invoke ":defer" functions.
6250 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006251 static void
6252handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006253{
6254 int idx;
6255
Bram Moolenaar58779852022-09-06 18:31:14 +01006256 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006257 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006258 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006259
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006260 if (dr->dr_name == NULL)
6261 // already being called, can happen if function does ":qa"
6262 continue;
6263
6264 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006265 CLEAR_FIELD(funcexe);
6266 funcexe.fe_evaluate = TRUE;
6267
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006268 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006269 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006270
6271 char_u *name = dr->dr_name;
6272 dr->dr_name = NULL;
6273
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006274 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006275 // first statement in the function will be executed (because of the
6276 // exception). So save and restore the try/catch/throw exception
6277 // state.
6278 exception_state_T estate;
6279 exception_state_save(&estate);
6280 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006281
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006282 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6283
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006284 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006285
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006286 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006287 vim_free(name);
6288 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006289 clear_tv(&dr->dr_argvars[i]);
6290 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006291 ga_clear(&funccal->fc_defer);
6292}
6293
zeertzjq960cf912023-04-18 21:52:54 +01006294 static void
6295invoke_funccall_defer(funccall_T *fc)
6296{
6297 if (fc->fc_ectx != NULL)
6298 {
6299 // :def function
6300 unwind_def_callstack(fc->fc_ectx);
6301 may_invoke_defer_funcs(fc->fc_ectx);
6302 }
6303 else
6304 {
6305 // legacy function
6306 handle_defer_one(fc);
6307 }
6308}
6309
Bram Moolenaar58779852022-09-06 18:31:14 +01006310/*
6311 * Called when exiting: call all defer functions.
6312 */
6313 void
6314invoke_all_defer(void)
6315{
zeertzjq1be4b812023-04-19 14:21:24 +01006316 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6317 invoke_funccall_defer(fc);
6318
zeertzjq960cf912023-04-18 21:52:54 +01006319 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6320 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6321 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006322}
6323
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006324/*
6325 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006326 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006327 */
6328 void
6329ex_call(exarg_T *eap)
6330{
6331 char_u *arg = eap->arg;
6332 char_u *startarg;
6333 char_u *name;
6334 char_u *tofree;
6335 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006336 int failed = FALSE;
6337 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006338 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006339 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006340 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006341 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006342 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006343 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006344
Bram Moolenaare6b53242020-07-01 17:28:33 +02006345 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006346 if (eap->skip)
6347 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006348 typval_T rettv;
6349
Bram Moolenaare38eab22019-12-05 21:50:01 +01006350 // trans_function_name() doesn't work well when skipping, use eval0()
6351 // instead to skip to any following command, e.g. for:
6352 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006353 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006354 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006355 clear_tv(&rettv);
6356 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006357 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006358 return;
6359 }
6360
zeertzjq5299c092023-04-12 20:48:16 +01006361 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006362 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006363 if (fudi.fd_newkey != NULL)
6364 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006365 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006366 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006367 vim_free(fudi.fd_newkey);
6368 }
6369 if (tofree == NULL)
6370 return;
6371
Bram Moolenaare38eab22019-12-05 21:50:01 +01006372 // Increase refcount on dictionary, it could get deleted when evaluating
6373 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006374 if (fudi.fd_dict != NULL)
6375 ++fudi.fd_dict->dv_refcount;
6376
Bram Moolenaare38eab22019-12-05 21:50:01 +01006377 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6378 // contents. For VAR_PARTIAL get its partial, unless we already have one
6379 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006380 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006381 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006382 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006383 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006384
Bram Moolenaare38eab22019-12-05 21:50:01 +01006385 // Skip white space to allow ":call func ()". Not good, but required for
6386 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006387 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006388 if (*startarg != '(')
6389 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006390 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006391 goto end;
6392 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006393 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006394 {
6395 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6396 goto end;
6397 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006398
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006399 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006400 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006401 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006402 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006403 }
6404 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006405 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006406 funcexe_T funcexe;
6407
Bram Moolenaara80faa82020-04-12 19:37:17 +02006408 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006409 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006410 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006411 funcexe.fe_partial = partial;
6412 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006413 funcexe.fe_firstline = eap->line1;
6414 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006415 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006416 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006417 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006418 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006419
Bram Moolenaar1d341892021-09-18 15:25:52 +02006420 // When inside :try we need to check for following "| catch" or "| endtry".
6421 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006422 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006423 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006424 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006425 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006426 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006427 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006428 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006429 {
6430 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006431 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006432 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006433 }
6434 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006435 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006436 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006437 // Must be after using "arg", it may point into memory cleared here.
6438 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006439
6440end:
6441 dict_unref(fudi.fd_dict);
6442 vim_free(tofree);
6443}
6444
6445/*
6446 * Return from a function. Possibly makes the return pending. Also called
6447 * for a pending return at the ":endtry" or after returning from an extra
6448 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6449 * when called due to a ":return" command. "rettv" may point to a typval_T
6450 * with the return rettv. Returns TRUE when the return can be carried out,
6451 * FALSE when the return gets pending.
6452 */
6453 int
6454do_return(
6455 exarg_T *eap,
6456 int reanimate,
6457 int is_cmd,
6458 void *rettv)
6459{
6460 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006461 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006462
6463 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006464 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006465 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006466
6467 /*
6468 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6469 * not in its finally clause (which then is to be executed next) is found.
6470 * In this case, make the ":return" pending for execution at the ":endtry".
6471 * Otherwise, return normally.
6472 */
6473 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6474 if (idx >= 0)
6475 {
6476 cstack->cs_pending[idx] = CSTP_RETURN;
6477
6478 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006479 // A pending return again gets pending. "rettv" points to an
6480 // allocated variable with the rettv of the original ":return"'s
6481 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006482 cstack->cs_rettv[idx] = rettv;
6483 else
6484 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006485 // When undoing a return in order to make it pending, get the stored
6486 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006487 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006488 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006489
6490 if (rettv != NULL)
6491 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006492 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006493 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6494 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6495 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006496 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006497 }
6498 else
6499 cstack->cs_rettv[idx] = NULL;
6500
6501 if (reanimate)
6502 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006503 // The pending return value could be overwritten by a ":return"
6504 // without argument in a finally clause; reset the default
6505 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006506 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6507 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006508 }
6509 }
6510 report_make_pending(CSTP_RETURN, rettv);
6511 }
6512 else
6513 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006514 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006515
Bram Moolenaare38eab22019-12-05 21:50:01 +01006516 // If the return is carried out now, store the return value. For
6517 // a return immediately after reanimation, the value is already
6518 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006519 if (!reanimate && rettv != NULL)
6520 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006521 clear_tv(current_funccal->fc_rettv);
6522 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006523 if (!is_cmd)
6524 vim_free(rettv);
6525 }
6526 }
6527
6528 return idx < 0;
6529}
6530
6531/*
6532 * Free the variable with a pending return value.
6533 */
6534 void
6535discard_pending_return(void *rettv)
6536{
6537 free_tv((typval_T *)rettv);
6538}
6539
6540/*
6541 * Generate a return command for producing the value of "rettv". The result
6542 * is an allocated string. Used by report_pending() for verbose messages.
6543 */
6544 char_u *
6545get_return_cmd(void *rettv)
6546{
6547 char_u *s = NULL;
6548 char_u *tofree = NULL;
6549 char_u numbuf[NUMBUFLEN];
6550
6551 if (rettv != NULL)
6552 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6553 if (s == NULL)
6554 s = (char_u *)"";
6555
6556 STRCPY(IObuff, ":return ");
6557 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6558 if (STRLEN(s) + 8 >= IOSIZE)
6559 STRCPY(IObuff + IOSIZE - 4, "...");
6560 vim_free(tofree);
6561 return vim_strsave(IObuff);
6562}
6563
6564/*
6565 * Get next function line.
6566 * Called by do_cmdline() to get the next line.
6567 * Returns allocated string, or NULL for end of function.
6568 */
6569 char_u *
6570get_func_line(
6571 int c UNUSED,
6572 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006573 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006574 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006575{
6576 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006577 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006578 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006579 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006580
Bram Moolenaare38eab22019-12-05 21:50:01 +01006581 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006582 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006583 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006584 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006585 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006586 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006587 }
6588#ifdef FEAT_PROFILE
6589 if (do_profiling == PROF_YES)
6590 func_line_end(cookie);
6591#endif
6592
6593 gap = &fp->uf_lines;
6594 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006595 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006596 retval = NULL;
6597 else
6598 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006599 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006600 while (fcp->fc_linenr < gap->ga_len
6601 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6602 ++fcp->fc_linenr;
6603 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006604 retval = NULL;
6605 else
6606 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006607 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6608 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006609#ifdef FEAT_PROFILE
6610 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006611 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006612#endif
6613 }
6614 }
6615
Bram Moolenaare38eab22019-12-05 21:50:01 +01006616 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006617 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006618 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006619 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006620 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006621 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006622 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006623 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006624 }
6625
6626 return retval;
6627}
6628
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006629/*
6630 * Return TRUE if the currently active function should be ended, because a
6631 * return was encountered or an error occurred. Used inside a ":while".
6632 */
6633 int
6634func_has_ended(void *cookie)
6635{
6636 funccall_T *fcp = (funccall_T *)cookie;
6637
Bram Moolenaare38eab22019-12-05 21:50:01 +01006638 // Ignore the "abort" flag if the abortion behavior has been changed due to
6639 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006640 return (((fcp->fc_func->uf_flags & FC_ABORT)
6641 && did_emsg && !aborted_in_try())
6642 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006643}
6644
6645/*
6646 * return TRUE if cookie indicates a function which "abort"s on errors.
6647 */
6648 int
6649func_has_abort(
6650 void *cookie)
6651{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006652 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006653}
6654
6655
6656/*
6657 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6658 * Don't do this when "Func" is already a partial that was bound
6659 * explicitly (pt_auto is FALSE).
6660 * Changes "rettv" in-place.
6661 * Returns the updated "selfdict_in".
6662 */
6663 dict_T *
6664make_partial(dict_T *selfdict_in, typval_T *rettv)
6665{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006666 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006667 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006668 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006669 dict_T *selfdict = selfdict_in;
6670
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006671 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6672 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006673 fp = rettv->vval.v_partial->pt_func;
6674 else
6675 {
6676 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006677 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006678 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006679 if (fname == NULL)
6680 {
6681 // There is no point binding a dict to a NULL function, just create
6682 // a function reference.
6683 rettv->v_type = VAR_FUNC;
6684 rettv->vval.v_string = NULL;
6685 }
6686 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006687 {
6688 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006689 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006690
6691 // Translate "s:func" to the stored function name.
6692 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6693 fp = find_func(fname, FALSE);
6694 vim_free(tofree);
6695 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006696 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006697
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006698 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006699 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006700 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006701
6702 if (pt != NULL)
6703 {
6704 pt->pt_refcount = 1;
6705 pt->pt_dict = selfdict;
6706 pt->pt_auto = TRUE;
6707 selfdict = NULL;
6708 if (rettv->v_type == VAR_FUNC)
6709 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006710 // Just a function: Take over the function name and use
6711 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006712 pt->pt_name = rettv->vval.v_string;
6713 }
6714 else
6715 {
6716 partial_T *ret_pt = rettv->vval.v_partial;
6717 int i;
6718
Bram Moolenaare38eab22019-12-05 21:50:01 +01006719 // Partial: copy the function name, use selfdict and copy
6720 // args. Can't take over name or args, the partial might
6721 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006722 if (ret_pt->pt_name != NULL)
6723 {
6724 pt->pt_name = vim_strsave(ret_pt->pt_name);
6725 func_ref(pt->pt_name);
6726 }
6727 else
6728 {
6729 pt->pt_func = ret_pt->pt_func;
6730 func_ptr_ref(pt->pt_func);
6731 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006732 if (ret_pt->pt_argc > 0)
6733 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006734 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006735 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006736 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006737 pt->pt_argc = 0;
6738 else
6739 {
6740 pt->pt_argc = ret_pt->pt_argc;
6741 for (i = 0; i < pt->pt_argc; i++)
6742 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6743 }
6744 }
6745 partial_unref(ret_pt);
6746 }
6747 rettv->v_type = VAR_PARTIAL;
6748 rettv->vval.v_partial = pt;
6749 }
6750 }
6751 return selfdict;
6752}
6753
6754/*
6755 * Return the name of the executed function.
6756 */
6757 char_u *
6758func_name(void *cookie)
6759{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006760 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006761}
6762
6763/*
6764 * Return the address holding the next breakpoint line for a funccall cookie.
6765 */
6766 linenr_T *
6767func_breakpoint(void *cookie)
6768{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006769 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006770}
6771
6772/*
6773 * Return the address holding the debug tick for a funccall cookie.
6774 */
6775 int *
6776func_dbg_tick(void *cookie)
6777{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006778 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006779}
6780
6781/*
6782 * Return the nesting level for a funccall cookie.
6783 */
6784 int
6785func_level(void *cookie)
6786{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006787 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006788}
6789
6790/*
6791 * Return TRUE when a function was ended by a ":return" command.
6792 */
6793 int
6794current_func_returned(void)
6795{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006796 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006797}
6798
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006799 int
6800free_unref_funccal(int copyID, int testing)
6801{
6802 int did_free = FALSE;
6803 int did_free_funccal = FALSE;
6804 funccall_T *fc, **pfc;
6805
6806 for (pfc = &previous_funccal; *pfc != NULL; )
6807 {
6808 if (can_free_funccal(*pfc, copyID))
6809 {
6810 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006811 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006812 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006813 did_free = TRUE;
6814 did_free_funccal = TRUE;
6815 }
6816 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006817 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006818 }
6819 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006820 // When a funccal was freed some more items might be garbage
6821 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006822 (void)garbage_collect(testing);
6823
6824 return did_free;
6825}
6826
6827/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006828 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006829 */
6830 static funccall_T *
6831get_funccal(void)
6832{
6833 int i;
6834 funccall_T *funccal;
6835 funccall_T *temp_funccal;
6836
6837 funccal = current_funccal;
6838 if (debug_backtrace_level > 0)
6839 {
6840 for (i = 0; i < debug_backtrace_level; i++)
6841 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006842 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006843 if (temp_funccal)
6844 funccal = temp_funccal;
6845 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006846 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006847 debug_backtrace_level = i;
6848 }
6849 }
6850 return funccal;
6851}
6852
6853/*
6854 * Return the hashtable used for local variables in the current funccal.
6855 * Return NULL if there is no current funccal.
6856 */
6857 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006858get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006859{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006860 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006861 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006862 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006863}
6864
6865/*
6866 * Return the l: scope variable.
6867 * Return NULL if there is no current funccal.
6868 */
6869 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006870get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006871{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006872 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006873 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006874 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006875}
6876
6877/*
6878 * Return the hashtable used for argument in the current funccal.
6879 * Return NULL if there is no current funccal.
6880 */
6881 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006882get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006883{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006884 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006885 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006886 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006887}
6888
6889/*
6890 * Return the a: scope variable.
6891 * Return NULL if there is no current funccal.
6892 */
6893 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006894get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006895{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006896 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006897 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006898 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006899}
6900
6901/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006902 * List function variables, if there is a function.
6903 */
6904 void
6905list_func_vars(int *first)
6906{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006907 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6908 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006909 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006910}
6911
6912/*
6913 * If "ht" is the hashtable for local variables in the current funccal, return
6914 * the dict that contains it.
6915 * Otherwise return NULL.
6916 */
6917 dict_T *
6918get_current_funccal_dict(hashtab_T *ht)
6919{
6920 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006921 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6922 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006923 return NULL;
6924}
6925
6926/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006927 * Search hashitem in parent scope.
6928 */
6929 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006930find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006931{
6932 funccall_T *old_current_funccal = current_funccal;
6933 hashtab_T *ht;
6934 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006935 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006936
Bram Moolenaarca16c602022-09-06 18:57:08 +01006937 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006938 return NULL;
6939
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006940 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006941 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006942 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006943 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006944 ht = find_var_ht(name, &varname);
6945 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006946 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006947 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006948 if (!HASHITEM_EMPTY(hi))
6949 {
6950 *pht = ht;
6951 break;
6952 }
6953 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006954 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006955 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006956 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006957 }
6958 current_funccal = old_current_funccal;
6959
6960 return hi;
6961}
6962
6963/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006964 * Search variable in parent scope.
6965 */
6966 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006967find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006968{
6969 dictitem_T *v = NULL;
6970 funccall_T *old_current_funccal = current_funccal;
6971 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006972 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006973
Bram Moolenaarca16c602022-09-06 18:57:08 +01006974 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006975 return NULL;
6976
Bram Moolenaare38eab22019-12-05 21:50:01 +01006977 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01006978 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006979 while (current_funccal)
6980 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006981 ht = find_var_ht(name, &varname);
6982 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006983 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006984 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006985 if (v != NULL)
6986 break;
6987 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006988 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006989 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006990 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006991 }
6992 current_funccal = old_current_funccal;
6993
6994 return v;
6995}
6996
6997/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006998 * Set "copyID + 1" in previous_funccal and callers.
6999 */
7000 int
7001set_ref_in_previous_funccal(int copyID)
7002{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007003 funccall_T *fc;
7004
Bram Moolenaarca16c602022-09-06 18:57:08 +01007005 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007006 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007007 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007008 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
7009 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
7010 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007011 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007012 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007013 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007014}
7015
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007016 static int
7017set_ref_in_funccal(funccall_T *fc, int copyID)
7018{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007019 if (fc->fc_copyID != copyID)
7020 {
7021 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007022 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7023 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7024 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7025 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007026 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007027 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007028 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007029}
7030
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007031/*
7032 * Set "copyID" in all local vars and arguments in the call stack.
7033 */
7034 int
7035set_ref_in_call_stack(int copyID)
7036{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007037 funccall_T *fc;
7038 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007039
Bram Moolenaarca16c602022-09-06 18:57:08 +01007040 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007041 if (set_ref_in_funccal(fc, copyID))
7042 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007043
7044 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007045 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007046 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007047 if (set_ref_in_funccal(fc, copyID))
7048 return TRUE;
7049 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007050}
7051
7052/*
7053 * Set "copyID" in all functions available by name.
7054 */
7055 int
7056set_ref_in_functions(int copyID)
7057{
7058 int todo;
7059 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007060 ufunc_T *fp;
7061
7062 todo = (int)func_hashtab.ht_used;
7063 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007064 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007065 if (!HASHITEM_EMPTY(hi))
7066 {
7067 --todo;
7068 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007069 if (!func_name_refcount(fp->uf_name)
7070 && set_ref_in_func(NULL, fp, copyID))
7071 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007072 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007073 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007074 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007075}
7076
7077/*
7078 * Set "copyID" in all function arguments.
7079 */
7080 int
7081set_ref_in_func_args(int copyID)
7082{
7083 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007084
7085 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007086 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7087 copyID, NULL, NULL))
7088 return TRUE;
7089 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007090}
7091
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007092/*
7093 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007094 * Returns TRUE if setting references failed somehow.
7095 */
7096 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007097set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007098{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007099 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007100 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007101 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007102 char_u fname_buf[FLEN_FIXED + 1];
7103 char_u *tofree = NULL;
7104 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007105 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007106
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007107 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007108 return FALSE;
7109
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007110 if (fp_in == NULL)
7111 {
7112 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007113 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007114 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007115 if (fp != NULL)
7116 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007117 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007118 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007119 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007120
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007121 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007122 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007123}
7124
Bram Moolenaare38eab22019-12-05 21:50:01 +01007125#endif // FEAT_EVAL