blob: 015733cdb1a7415872829d2e2ccb471f08da4e67 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020032static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010033static void func_clear(ufunc_T *fp, int force);
34static int func_free(ufunc_T *fp, int force);
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +010035static char_u *untrans_function_name(char_u *name);
Bram Moolenaar58779852022-09-06 18:31:14 +010036static void handle_defer_one(funccall_T *funccal);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020037
38 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +000039func_init(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020040{
41 hash_init(&func_hashtab);
42}
43
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020044/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020045 * Return the function hash table
46 */
47 hashtab_T *
48func_tbl_get(void)
49{
50 return &func_hashtab;
51}
52
53/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020054 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020055 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010056 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010057 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000058 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010059 * Return a pointer to after the type.
60 * When something is wrong return "arg".
61 */
62 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010063one_function_arg(
64 char_u *arg,
65 garray_T *newargs,
66 garray_T *argtypes,
67 int types_optional,
h-eastb895b0f2023-09-24 15:46:31 +020068 garray_T *arg_objm,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010069 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000070 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020071 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010072 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073{
Bram Moolenaar6e949782020-04-13 17:21:00 +020074 char_u *p = arg;
75 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020076 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077
78 while (ASCII_ISALNUM(*p) || *p == '_')
79 ++p;
Keith Thompson184f71c2024-01-04 21:19:04 +010080 if (arg == p || SAFE_isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020081 || (argtypes == NULL
82 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
83 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 {
85 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000086 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087 return arg;
88 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010089
Bram Moolenaarce723f32023-06-10 19:00:12 +010090 // Extra checks in Vim9 script.
91 if (!skip && argtypes != NULL)
92 {
93 int c = *p;
94 *p = NUL;
95 int r = check_reserved_name(arg, FALSE);
96 *p = c;
97 if (r == FAIL)
98 return arg;
99
100 // Cannot use script var name for argument. In function: also check
101 // local vars and arguments.
102 if (check_defined(arg, p - arg,
103 evalarg == NULL ? NULL : evalarg->eval_cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +0000104 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarce723f32023-06-10 19:00:12 +0100105 return arg;
106 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100108 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
109 return arg;
110 if (newargs != NULL)
111 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100112 int c;
113 int i;
114
115 c = *p;
116 *p = NUL;
117 arg_copy = vim_strsave(arg);
118 if (arg_copy == NULL)
119 {
120 *p = c;
121 return arg;
122 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200123 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200124 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200125 // Check for duplicate argument name.
126 for (i = 0; i < newargs->ga_len; ++i)
127 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
128 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000129 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200130 vim_free(arg_copy);
131 return arg;
132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
134 newargs->ga_len++;
135
136 *p = c;
137 }
138
139 // get any type from "arg: type"
h-eastb895b0f2023-09-24 15:46:31 +0200140 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK)
141 && arg_objm != NULL && (skip || ga_grow(arg_objm, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 {
143 char_u *type = NULL;
144
Bram Moolenaar6e949782020-04-13 17:21:00 +0200145 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
146 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200147 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200148 arg_copy == NULL ? arg : arg_copy);
149 p = skipwhite(p);
150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 if (*p == ':')
152 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200153 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100154 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200155 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100156 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200157 return arg;
158 }
159 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200160 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100161 if (!skip)
162 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200164 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200165 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200166 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200167 arg_copy == NULL ? arg : arg_copy);
168 return arg;
169 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100170 if (!skip)
171 {
172 if (type == NULL && types_optional)
173 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200174 type = vim_strsave((char_u *)
175 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100176 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
h-eastb895b0f2023-09-24 15:46:31 +0200177 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = FALSE;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 }
180
181 return p;
182}
183
184/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000185 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000186 * Get a next line, store it in "eap" if appropriate and put the line in
187 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000188 */
189 static char_u *
190get_function_line(
191 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000192 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000193 int indent,
194 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000195{
196 char_u *theline;
197
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100198 if (eap->ea_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000199 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000200 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100201 theline = eap->ea_getline(':', eap->cookie, indent, getline_options);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000202 if (theline != NULL)
203 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000204 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000205 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000206 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
207 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000208 *eap->cmdlinep = theline;
Bram Moolenaarb5820102023-01-25 12:27:13 +0000209 (void)ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000210 }
211
212 return theline;
213}
214
215/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200216 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100217 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100218 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200219 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100220 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200221get_function_args(
222 char_u **argp,
223 char_u endchar,
224 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100226 int types_optional, // types optional if "argtypes" is not NULL
h-eastb895b0f2023-09-24 15:46:31 +0200227 garray_T *arg_objm, // NULL unless using :def
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100228 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200229 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200230 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200231 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000232 exarg_T *eap, // can be NULL
Bram Moolenaar554d0312023-01-05 19:59:18 +0000233 int in_class, // non-zero when inside a class or interface
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000234 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000235 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200236{
237 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100238 char_u *arg;
239 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200240 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200241 int any_default = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100242 char_u *whitep = *argp;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100243 int need_expr = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200244
245 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000246 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000248 ga_init2(argtypes, sizeof(char_u *), 3);
h-eastb895b0f2023-09-24 15:46:31 +0200249 if (arg_objm != NULL)
250 ga_init2(arg_objm, sizeof(int8_T), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200251 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000252 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200253
254 if (varargs != NULL)
255 *varargs = FALSE;
256
257 /*
258 * Isolate the arguments: "arg1, arg2, ...)"
259 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100260 arg = skipwhite(*argp);
261 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200262 while (*p != endchar)
263 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100264 while (eap != NULL && eap->ea_getline != NULL
Bram Moolenaar2c330432020-04-13 14:41:35 +0200265 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200266 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200267 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000268 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000269 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000270
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200271 if (theline == NULL)
272 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200273 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200274 p = skipwhite(theline);
275 }
276
277 if (mustend && *p != endchar)
278 {
279 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000280 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100281 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200282 }
Christian Brabandte4a450a2023-12-08 20:57:38 +0100283 if (*p == endchar && !need_expr)
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200284 break;
285
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200286 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
287 {
288 if (varargs != NULL)
289 *varargs = TRUE;
290 p += 3;
291 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100292
293 if (argtypes != NULL)
294 {
295 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200296 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100298 if (!skip)
299 emsg(_(e_missing_name_after_dots));
300 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100301 }
302
303 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100304 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200305 arg_objm, evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306 if (p == arg)
307 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100308 if (*skipwhite(p) == '=')
309 {
310 emsg(_(e_cannot_use_default_for_variable_arguments));
311 break;
312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200314 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000315 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000316 {
317 // this.memberName
318 p += 5;
319 arg = p;
320 while (ASCII_ISALNUM(*p) || *p == '_')
321 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000322 char_u *argend = p;
323
h-eastdb385522023-09-28 22:18:19 +0200324 // object variable this. can be used only in a constructor
325 if (STRNCMP(eap->arg, "new", 3) != 0)
326 {
327 c = *argend;
328 *argend = NUL;
329 semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
330 *argend = c;
331 break;
332 }
333
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000334 if (*skipwhite(p) == '=')
335 {
336 char_u *defval = skipwhite(skipwhite(p) + 1);
337 if (STRNCMP(defval, "v:none", 6) != 0)
338 {
339 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
340 goto err_ret;
341 }
342 any_default = TRUE;
343 p = defval + 6;
344
345 if (ga_grow(default_args, 1) == FAIL)
346 goto err_ret;
347
348 char_u *expr = vim_strsave((char_u *)"v:none");
349 if (expr == NULL)
350 goto err_ret;
351 ((char_u **)(default_args->ga_data))
352 [default_args->ga_len] = expr;
353 default_args->ga_len++;
354 }
355 else if (any_default)
356 {
357 emsg(_(e_non_default_argument_follows_default_argument));
358 goto err_ret;
359 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000360
361 // TODO: check the argument is indeed a member
362 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
363 return FAIL;
364 if (newargs != NULL)
365 {
366 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000367 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000368 newargs->ga_len++;
369
h-eastb895b0f2023-09-24 15:46:31 +0200370 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
371 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000372 {
373 // TODO: use the actual type
374 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
375 vim_strsave((char_u *)"any");
h-eastb895b0f2023-09-24 15:46:31 +0200376 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000377
378 // Add a line to the function body for the assignment.
379 if (ga_grow(newlines, 1) == OK)
380 {
381 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000382 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
383 if (any_default)
384 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000385 char_u *assignment = alloc(len);
386 if (assignment != NULL)
387 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000388 c = *argend;
389 *argend = NUL;
390 if (any_default)
391 vim_snprintf((char *)assignment, len,
392 "ifargisset %d this.%s = %s",
393 default_args->ga_len - 1, arg, arg);
394 else
395 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000396 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000397 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000398 ((char_u **)(newlines->ga_data))[
399 newlines->ga_len++] = assignment;
400 }
401 }
402 }
403 }
404 if (*p == ',')
405 ++p;
406 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200407 else
408 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200409 char_u *np;
410
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200411 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100412 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200413 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200415 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200416
Bram Moolenaar015cf102021-06-26 21:52:02 +0200417 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200418 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200419 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200420 if (*np == '=' && np[1] != '=' && np[1] != '~'
421 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200422 {
423 typval_T rettv;
424
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100425 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200426 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100427 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000428 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200429 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200430 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200431 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200432 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200433 if (ga_grow(default_args, 1) == FAIL)
434 goto err_ret;
435
Christian Brabandte4a450a2023-12-08 20:57:38 +0100436 if (need_expr)
437 need_expr = FALSE;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200438 // trim trailing whitespace
439 while (p > expr && VIM_ISWHITE(p[-1]))
440 p--;
441 c = *p;
442 *p = NUL;
443 expr = vim_strsave(expr);
444 if (expr == NULL)
445 {
446 *p = c;
447 goto err_ret;
448 }
449 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200450 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200451 default_args->ga_len++;
452 *p = c;
453 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200454 }
455 else
Christian Brabandte4a450a2023-12-08 20:57:38 +0100456 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200457 mustend = TRUE;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100458 if (*skipwhite(p) == NUL)
459 need_expr = TRUE;
460 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200461 }
462 else if (any_default)
463 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000464 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200465 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200466 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200467
468 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
469 {
470 // Be tolerant when skipping
471 if (!skip)
472 {
473 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
474 goto err_ret;
475 }
476 p = skipwhite(p);
477 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200478 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200479 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200480 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200481 // Don't give this error when skipping, it makes the "->" not
482 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100483 // Allow missing space after comma in legacy functions.
484 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200485 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
486 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100487 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200488 goto err_ret;
489 }
490 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200491 else
492 mustend = TRUE;
493 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200494 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200495 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200496 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200497
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200498 if (*p != endchar)
499 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100500 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200501
502 *argp = p;
503 return OK;
504
505err_ret:
506 if (newargs != NULL)
507 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200508 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200509 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200510 return FAIL;
511}
512
513/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100514 * Parse the argument types, filling "fp->uf_arg_types".
515 * Return OK or FAIL.
516 */
517 static int
h-eastb895b0f2023-09-24 15:46:31 +0200518parse_argument_types(
519 ufunc_T *fp,
520 garray_T *argtypes,
521 int varargs,
522 garray_T *arg_objm,
523 ocmember_T *obj_members,
524 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100525{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200526 int len = 0;
527
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100528 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
529 if (argtypes->ga_len > 0)
530 {
531 // When "varargs" is set the last name/type goes into uf_va_name
532 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200533 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100534
535 if (len > 0)
536 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
537 if (fp->uf_arg_types != NULL)
538 {
539 int i;
540 type_T *type;
541
542 for (i = 0; i < len; ++ i)
543 {
544 char_u *p = ((char_u **)argtypes->ga_data)[i];
545
546 if (p == NULL)
547 // will get the type from the default value
548 type = &t_unknown;
549 else
h-eastb895b0f2023-09-24 15:46:31 +0200550 {
551 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
552 {
553 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
554
555 type = &t_any;
556 for (int om = 0; om < obj_member_count; ++om)
557 {
Christian Brabandt915f3bf2024-04-05 20:12:19 +0200558 if (obj_members != NULL
559 && STRCMP(aname,
560 obj_members[om].ocm_name) == 0)
h-eastb895b0f2023-09-24 15:46:31 +0200561 {
562 type = obj_members[om].ocm_type;
563 break;
564 }
565 }
566 }
567 else
568 type = parse_type(&p, &fp->uf_type_list, TRUE);
569 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100570 if (type == NULL)
571 return FAIL;
572 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000573 if (i < fp->uf_args.ga_len
574 && (type->tt_type == VAR_FUNC
Yegappan Lakshmanan3e336502024-04-04 19:35:59 +0200575 || type->tt_type == VAR_PARTIAL))
576 {
577 char_u *name = ((char_u **)fp->uf_args.ga_data)[i];
578 if (obj_members != NULL && *name == '_')
579 // protected object method
580 name++;
581
582 if (var_wrong_func_name(name, TRUE))
583 return FAIL;
584 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100585 }
586 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100587 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200588
589 if (varargs)
590 {
591 char_u *p;
592
593 // Move the last argument "...name: type" to uf_va_name and
594 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200595 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000596 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
597 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200598 p = ((char_u **)argtypes->ga_data)[len];
599 if (p == NULL)
600 // TODO: get type from default value
601 fp->uf_va_type = &t_list_any;
602 else
603 {
604 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
605 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
606 {
607 semsg(_(e_variable_arguments_type_must_be_list_str),
608 ((char_u **)argtypes->ga_data)[len]);
609 return FAIL;
610 }
611 }
612 if (fp->uf_va_type == NULL)
613 return FAIL;
614 }
615
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100616 return OK;
617}
618
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100619 static int
620parse_return_type(ufunc_T *fp, char_u *ret_type)
621{
622 if (ret_type == NULL)
623 fp->uf_ret_type = &t_void;
624 else
625 {
626 char_u *p = ret_type;
627
628 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
629 if (fp->uf_ret_type == NULL)
630 {
631 fp->uf_ret_type = &t_void;
632 return FAIL;
633 }
634 }
635 return OK;
636}
637
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100638/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200639 * Register function "fp" as using "current_funccal" as its scope.
640 */
641 static int
642register_closure(ufunc_T *fp)
643{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200644 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100645 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200646 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200647 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200648 fp->uf_scoped = current_funccal;
649 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200650
Bram Moolenaarca16c602022-09-06 18:57:08 +0100651 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200652 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100653 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
654 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200655 return OK;
656}
657
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100658 static void
659set_ufunc_name(ufunc_T *fp, char_u *name)
660{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100661 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
662 // actually extends beyond the struct.
663 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100664
665 if (name[0] == K_SPECIAL)
666 {
667 fp->uf_name_exp = alloc(STRLEN(name) + 3);
668 if (fp->uf_name_exp != NULL)
669 {
670 STRCPY(fp->uf_name_exp, "<SNR>");
671 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
672 }
673 }
674}
675
Bram Moolenaar58016442016-07-31 18:30:22 +0200676/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100677 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
678 * return "buf" filled with a readable function name.
679 * Otherwise just return "name", thus the return value can always be used.
680 * "name" and "buf" may be equal.
681 */
682 char_u *
683make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
684{
685 size_t len;
686
687 if (name[0] != K_SPECIAL)
688 return name;
689 len = STRLEN(name);
690 if (len + 3 > bufsize)
691 return name;
692
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100693 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100694 mch_memmove(buf, "<SNR>", 5);
695 return buf;
696}
697
698/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200699 * Get a name for a lambda. Returned in static memory.
700 */
701 char_u *
702get_lambda_name(void)
703{
704 static char_u name[30];
705 static int lambda_no = 0;
706
707 sprintf((char*)name, "<lambda>%d", ++lambda_no);
708 return name;
709}
710
Bram Moolenaare01e5212023-01-08 20:31:18 +0000711/*
712 * Allocate a "ufunc_T" for a function called "name".
713 * Makes sure the size is right.
714 */
715 static ufunc_T *
716alloc_ufunc(char_u *name)
717{
718 // When the name is short we need to make sure we allocate enough bytes for
719 // the whole struct, including any padding.
720 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
721 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
722}
723
Bram Moolenaar801ab062020-06-25 19:27:56 +0200724#if defined(FEAT_LUA) || defined(PROTO)
725/*
726 * Registers a native C callback which can be called from Vim script.
727 * Returns the name of the Vim script function.
728 */
729 char_u *
730register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
731{
732 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200733 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200734
Bram Moolenaare01e5212023-01-08 20:31:18 +0000735 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200736 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200737 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200738
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200739 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200740 fp->uf_refcount = 1;
741 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000742 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200743 fp->uf_calls = 0;
744 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200745 fp->uf_cb = cb;
746 fp->uf_cb_free = cb_free;
747 fp->uf_cb_state = state;
748
749 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000750 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200751
752 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200753}
754#endif
755
Bram Moolenaar04b12692020-05-04 23:24:44 +0200756/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100757 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100758 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100759 * If "white_error" is not NULL check for correct use of white space and set
760 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100761 * Return NULL if no valid arrow found.
762 */
763 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100764skip_arrow(
765 char_u *start,
766 int equal_arrow,
767 char_u **ret_type,
768 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100769{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100770 char_u *s = start;
771 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100772
773 if (equal_arrow)
774 {
775 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100776 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100777 if (white_error != NULL && !VIM_ISWHITE(s[1]))
778 {
779 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100780 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100781 return NULL;
782 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100783 s = skipwhite(s + 1);
784 *ret_type = s;
785 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100786 if (s == *ret_type)
787 {
788 emsg(_(e_missing_return_type));
789 return NULL;
790 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100791 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100792 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100793 s = skipwhite(s);
794 if (*s != '=')
795 return NULL;
796 ++s;
797 }
798 if (*s != '>')
799 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100800 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
801 || !IS_WHITE_OR_NUL(s[1])))
802 {
803 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100804 semsg(_(e_white_space_required_before_and_after_str_at_str),
805 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100806 return NULL;
807 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100808 return skipwhite(s + 1);
809}
810
811/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100812 * Check if "*cmd" points to a function command and if so advance "*cmd" and
813 * return TRUE.
814 * Otherwise return FALSE;
815 * Do not consider "function(" to be a command.
816 */
817 static int
818is_function_cmd(char_u **cmd)
819{
820 char_u *p = *cmd;
821
822 if (checkforcmd(&p, "function", 2))
823 {
824 if (*p == '(')
825 return FALSE;
826 *cmd = p;
827 return TRUE;
828 }
829 return FALSE;
830}
831
832/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200833 * Called when defining a function: The context may be needed for script
834 * variables declared in a block that is visible now but not when the function
835 * is compiled or called later.
836 */
837 static void
838function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
839{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000840 if (cstack == NULL || cstack->cs_idx < 0)
841 return;
842
843 int count = cstack->cs_idx + 1;
844 int i;
845
846 fp->uf_block_ids = ALLOC_MULT(int, count);
847 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200848 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000849 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
850 sizeof(int) * count);
851 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200852 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000853
854 // Set flag in each block to indicate a function was defined. This
855 // is used to keep the variable when leaving the block, see
856 // hide_script_var().
857 for (i = 0; i <= cstack->cs_idx; ++i)
858 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200859}
860
861/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100862 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200863 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100864 * "newlines" must already have been initialized.
865 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
866 */
867 static int
868get_function_body(
869 exarg_T *eap,
870 garray_T *newlines,
871 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000872 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100873{
874 linenr_T sourcing_lnum_top = SOURCING_LNUM;
875 linenr_T sourcing_lnum_off;
876 int saved_wait_return = need_wait_return;
877 char_u *line_arg = line_arg_in;
878 int vim9_function = eap->cmdidx == CMD_def
879 || eap->cmdidx == CMD_block;
880#define MAX_FUNC_NESTING 50
881 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200882 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100883 int nesting = 0;
884 getline_opt_T getline_options;
885 int indent = 2;
886 char_u *skip_until = NULL;
887 int ret = FAIL;
888 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200889 int heredoc_concat_len = 0;
890 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100891 char_u *heredoc_trimmed = NULL;
892
Bram Moolenaar20677332021-06-06 17:02:53 +0200893 ga_init2(&heredoc_ga, 1, 500);
894
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100895 // Detect having skipped over comment lines to find the return
896 // type. Add NULL lines to keep the line count correct.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100897 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100898 if (SOURCING_LNUM < sourcing_lnum_off)
899 {
900 sourcing_lnum_off -= SOURCING_LNUM;
901 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
902 goto theend;
903 while (sourcing_lnum_off-- > 0)
904 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
905 }
906
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200907 nesting_def[0] = vim9_function;
908 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100909 getline_options = vim9_function
910 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
911 for (;;)
912 {
913 char_u *theline;
914 char_u *p;
915 char_u *arg;
916
917 if (KeyTyped)
918 {
919 msg_scroll = TRUE;
920 saved_wait_return = FALSE;
921 }
922 need_wait_return = FALSE;
923
924 if (line_arg != NULL)
925 {
926 // Use eap->arg, split up in parts by line breaks.
927 theline = line_arg;
928 p = vim_strchr(theline, '\n');
929 if (p == NULL)
930 line_arg += STRLEN(line_arg);
931 else
932 {
933 *p = NUL;
934 line_arg = p + 1;
935 }
936 }
937 else
938 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000939 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100940 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100941 }
942 if (KeyTyped)
943 lines_left = Rows - 1;
944 if (theline == NULL)
945 {
946 // Use the start of the function for the line number.
947 SOURCING_LNUM = sourcing_lnum_top;
948 if (skip_until != NULL)
949 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200950 else if (nesting_inline[nesting])
951 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100952 else if (eap->cmdidx == CMD_def)
953 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100954 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000955 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100956 goto theend;
957 }
958
959 // Detect line continuation: SOURCING_LNUM increased more than one.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100960 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100961 if (SOURCING_LNUM < sourcing_lnum_off)
962 sourcing_lnum_off -= SOURCING_LNUM;
963 else
964 sourcing_lnum_off = 0;
965
966 if (skip_until != NULL)
967 {
968 // Don't check for ":endfunc"/":enddef" between
969 // * ":append" and "."
970 // * ":python <<EOF" and "EOF"
971 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
972 if (heredoc_trimmed == NULL
973 || (is_heredoc && skipwhite(theline) == theline)
974 || STRNCMP(theline, heredoc_trimmed,
975 STRLEN(heredoc_trimmed)) == 0)
976 {
977 if (heredoc_trimmed == NULL)
978 p = theline;
979 else if (is_heredoc)
980 p = skipwhite(theline) == theline
981 ? theline : theline + STRLEN(heredoc_trimmed);
982 else
983 p = theline + STRLEN(heredoc_trimmed);
984 if (STRCMP(p, skip_until) == 0)
985 {
986 VIM_CLEAR(skip_until);
987 VIM_CLEAR(heredoc_trimmed);
988 getline_options = vim9_function
989 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
990 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200991
992 if (heredoc_concat_len > 0)
993 {
994 // Replace the starting line with all the concatenated
995 // lines.
996 ga_concat(&heredoc_ga, theline);
997 vim_free(((char_u **)(newlines->ga_data))[
998 heredoc_concat_len - 1]);
999 ((char_u **)(newlines->ga_data))[
1000 heredoc_concat_len - 1] = heredoc_ga.ga_data;
1001 ga_init(&heredoc_ga);
1002 heredoc_concat_len = 0;
1003 theline += STRLEN(theline); // skip the "EOF"
1004 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001005 }
1006 }
1007 }
1008 else
1009 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001010 int c;
1011 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001012 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001013
1014 // skip ':' and blanks
1015 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1016 ;
1017
1018 // Check for "endfunction", "enddef" or "}".
1019 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001020 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001021 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001022 ? *p == '}'
1023 : (checkforcmd(&p, nesting_def[nesting]
1024 ? "enddef" : "endfunction", 4)
1025 && *p != ':'))
1026 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001027 if (!nesting_inline[nesting] && nesting_def[nesting]
1028 && p < cmd + 6)
1029 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001030 if (nesting-- == 0)
1031 {
1032 char_u *nextcmd = NULL;
1033
1034 if (*p == '|' || *p == '}')
1035 nextcmd = p + 1;
1036 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1037 nextcmd = line_arg;
1038 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001039 && (vim9_function || p_verbose > 0))
1040 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001041 SOURCING_LNUM = sourcing_lnum_top
1042 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001043 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001044 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001045 else
1046 give_warning2((char_u *)
1047 _("W22: Text found after :endfunction: %s"),
1048 p, TRUE);
1049 }
1050 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001051 {
1052 // Another command follows. If the line came from "eap"
1053 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001054 // change "eap->cmdlinep" to point to the last fetched
1055 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001056 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001057 if (lines_to_free->ga_len > 0
1058 && *eap->cmdlinep !=
1059 ((char_u **)lines_to_free->ga_data)
1060 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001061 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001062 // *cmdlinep will be freed later, thus remove the
1063 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001064 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001065 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1066 [lines_to_free->ga_len - 1];
1067 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001068 }
1069 }
1070 break;
1071 }
1072 }
1073
1074 // Check for mismatched "endfunc" or "enddef".
1075 // We don't check for "def" inside "func" thus we also can't check
1076 // for "enddef".
1077 // We continue to find the end of the function, although we might
1078 // not find it.
1079 else if (nesting_def[nesting])
1080 {
1081 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1082 emsg(_(e_mismatched_endfunction));
1083 }
1084 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1085 emsg(_(e_mismatched_enddef));
1086
1087 // Increase indent inside "if", "while", "for" and "try", decrease
1088 // at "end".
1089 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1090 indent -= 2;
1091 else if (STRNCMP(p, "if", 2) == 0
1092 || STRNCMP(p, "wh", 2) == 0
1093 || STRNCMP(p, "for", 3) == 0
1094 || STRNCMP(p, "try", 3) == 0)
1095 indent += 2;
1096
1097 // Check for defining a function inside this function.
1098 // Only recognize "def" inside "def", not inside "function",
1099 // For backwards compatibility, see Test_function_python().
1100 c = *p;
1101 if (is_function_cmd(&p)
1102 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1103 {
1104 if (*p == '!')
1105 p = skipwhite(p + 1);
1106 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001107 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001108 if (*skipwhite(p) == '(')
1109 {
1110 if (nesting == MAX_FUNC_NESTING - 1)
1111 emsg(_(e_function_nesting_too_deep));
1112 else
1113 {
1114 ++nesting;
1115 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001116 nesting_inline[nesting] = FALSE;
1117 indent += 2;
1118 }
1119 }
1120 }
1121
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001122 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001123 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001124 // Not a comment line: check for nested inline function.
1125 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001126 while (end > p && VIM_ISWHITE(*end))
1127 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001128 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001129 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001130 int is_block;
1131
1132 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001133 --end;
1134 while (end > p && VIM_ISWHITE(*end))
1135 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001136 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1137 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001138 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001139 char_u *s = p;
1140
1141 // check for line starting with "au" for :autocmd or
1142 // "com" for :command, these can use a {} block
1143 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1144 || checkforcmd_noparen(&s, "command", 3);
1145 }
1146
1147 if (is_block)
1148 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001149 if (nesting == MAX_FUNC_NESTING - 1)
1150 emsg(_(e_function_nesting_too_deep));
1151 else
1152 {
1153 ++nesting;
1154 nesting_def[nesting] = TRUE;
1155 nesting_inline[nesting] = TRUE;
1156 indent += 2;
1157 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001158 }
1159 }
1160 }
1161
1162 // Check for ":append", ":change", ":insert". Not for :def.
1163 p = skip_range(p, FALSE, NULL);
1164 if (!vim9_function
1165 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1166 || (p[0] == 'c'
1167 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1168 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1169 && (STRNCMP(&p[3], "nge", 3) != 0
1170 || !ASCII_ISALPHA(p[6])))))))
1171 || (p[0] == 'i'
1172 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1173 && (!ASCII_ISALPHA(p[2])
1174 || (p[2] == 's'
1175 && (!ASCII_ISALPHA(p[3])
1176 || p[3] == 'e'))))))))
1177 skip_until = vim_strsave((char_u *)".");
1178
1179 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1180 arg = skipwhite(skiptowhite(p));
1181 if (arg[0] == '<' && arg[1] =='<'
1182 && ((p[0] == 'p' && p[1] == 'y'
1183 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1184 || ((p[2] == '3' || p[2] == 'x')
1185 && !ASCII_ISALPHA(p[3]))))
1186 || (p[0] == 'p' && p[1] == 'e'
1187 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1188 || (p[0] == 't' && p[1] == 'c'
1189 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1190 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1191 && !ASCII_ISALPHA(p[3]))
1192 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1193 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1194 || (p[0] == 'm' && p[1] == 'z'
1195 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1196 ))
1197 {
1198 // ":python <<" continues until a dot, like ":append"
1199 p = skipwhite(arg + 2);
1200 if (STRNCMP(p, "trim", 4) == 0)
1201 {
1202 // Ignore leading white space.
1203 p = skipwhite(p + 4);
1204 heredoc_trimmed = vim_strnsave(theline,
1205 skipwhite(theline) - theline);
1206 }
1207 if (*p == NUL)
1208 skip_until = vim_strsave((char_u *)".");
1209 else
1210 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1211 getline_options = GETLINE_NONE;
1212 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001213 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001214 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001215 }
1216
Bram Moolenaard881d152022-05-13 13:50:36 +01001217 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001218 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001219 // Check for ":cmd v =<< [trim] EOF"
1220 // and ":cmd [a, b] =<< [trim] EOF"
1221 // and "lines =<< [trim] EOF" for Vim9
1222 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001223 arg = p;
1224 if (checkforcmd(&arg, "let", 2)
1225 || checkforcmd(&arg, "var", 3)
1226 || checkforcmd(&arg, "final", 5)
1227 || checkforcmd(&arg, "const", 5)
1228 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001229 {
zeertzjq9a91d2b2024-04-09 21:47:10 +02001230 int save_sc_version = current_sctx.sc_version;
1231 int var_count = 0;
1232 int semicolon = 0;
1233 char_u *argend;
1234
1235 current_sctx.sc_version
1236 = vim9_function ? SCRIPT_VERSION_VIM9 : 1;
1237 argend = skip_var_list(arg, TRUE, &var_count, &semicolon,
1238 TRUE);
1239 if (argend == NULL)
1240 // Invalid list assignment: skip to closing bracket.
1241 argend = find_name_end(arg, NULL, NULL, FNE_INCL_BR);
1242 arg = skipwhite(argend);
1243 current_sctx.sc_version = save_sc_version;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001244 if (arg[0] == '=' && arg[1] == '<' && arg[2] =='<')
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001245 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001246 p = skipwhite(arg + 3);
1247 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001248 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001249 if (STRNCMP(p, "trim", 4) == 0)
1250 {
1251 // Ignore leading white space.
1252 p = skipwhite(p + 4);
1253 heredoc_trimmed = vim_strnsave(theline,
1254 skipwhite(theline) - theline);
1255 continue;
1256 }
1257 if (STRNCMP(p, "eval", 4) == 0)
1258 {
1259 // Ignore leading white space.
1260 p = skipwhite(p + 4);
1261 continue;
1262 }
1263 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001264 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001265 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1266 getline_options = GETLINE_NONE;
1267 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001268 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001269 }
1270 }
1271 }
1272
1273 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001274 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001275 goto theend;
1276
Bram Moolenaar20677332021-06-06 17:02:53 +02001277 if (heredoc_concat_len > 0)
1278 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001279 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001280 // to be used for the instruction later.
1281 ga_concat(&heredoc_ga, theline);
1282 ga_concat(&heredoc_ga, (char_u *)"\n");
1283 p = vim_strsave((char_u *)"");
1284 }
1285 else
1286 {
1287 // Copy the line to newly allocated memory. get_one_sourceline()
1288 // allocates 250 bytes per line, this saves 80% on average. The
1289 // cost is an extra alloc/free.
1290 p = vim_strsave(theline);
1291 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001292 if (p == NULL)
1293 goto theend;
1294 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1295
1296 // Add NULL lines for continuation lines, so that the line count is
1297 // equal to the index in the growarray.
1298 while (sourcing_lnum_off-- > 0)
1299 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1300
1301 // Check for end of eap->arg.
1302 if (line_arg != NULL && *line_arg == NUL)
1303 line_arg = NULL;
1304 }
1305
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001306 // Return OK when no error was detected.
1307 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001308 ret = OK;
1309
1310theend:
1311 vim_free(skip_until);
1312 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001313 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001314 need_wait_return |= saved_wait_return;
1315 return ret;
1316}
1317
1318/*
1319 * Handle the body of a lambda. *arg points to the "{", process statements
1320 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001321 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001322 * When successful "rettv" is set to a funcref.
1323 */
1324 static int
1325lambda_function_body(
1326 char_u **arg,
1327 typval_T *rettv,
1328 evalarg_T *evalarg,
1329 garray_T *newargs,
1330 garray_T *argtypes,
1331 int varargs,
1332 garray_T *default_args,
1333 char_u *ret_type)
1334{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001335 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001336 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001337 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001338 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001339 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001340 exarg_T eap;
1341 garray_T newlines;
1342 char_u *cmdline = NULL;
1343 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001344 partial_T *pt;
1345 char_u *name;
1346 int lnum_save = -1;
1347 linenr_T sourcing_lnum_top = SOURCING_LNUM;
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001348 char_u *line_arg = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001349
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001350 *arg = skipwhite(*arg + 1);
1351 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001352 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001353 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001354 return FAIL;
1355 }
1356
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001357 // When there is a line break use what follows for the lambda body.
1358 // Makes lambda body initializers work for object and enum member
1359 // variables.
1360 if (**arg == '\n')
1361 line_arg = *arg + 1;
1362
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001363 CLEAR_FIELD(eap);
1364 eap.cmdidx = CMD_block;
1365 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001366 eap.cmdlinep = &cmdline;
1367 eap.skip = !evaluate;
1368 if (evalarg->eval_cctx != NULL)
1369 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1370 else
1371 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001372 eap.ea_getline = evalarg->eval_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001373 eap.cookie = evalarg->eval_cookie;
1374 }
1375
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001376 ga_init2(&newlines, sizeof(char_u *), 10);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001377 if (get_function_body(&eap, &newlines, line_arg,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001378 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001379 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001380
1381 // When inside a lambda must add the function lines to evalarg.eval_ga.
1382 evalarg->eval_break_count += newlines.ga_len;
1383 if (gap->ga_itemsize > 0)
1384 {
1385 int idx;
1386 char_u *last;
1387 size_t plen;
1388 char_u *pnl;
1389
1390 for (idx = 0; idx < newlines.ga_len; ++idx)
1391 {
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001392 char_u *p = ((char_u **)newlines.ga_data)[idx];
1393 if (p == NULL)
1394 // comment line in the lambda body
1395 continue;
1396
1397 p = skipwhite(p);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001398
Bram Moolenaarecb66452021-05-18 15:09:18 +02001399 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001400 goto erret;
1401
1402 // Going to concatenate the lines after parsing. For an empty or
1403 // comment line use an empty string.
1404 // Insert NL characters at the start of each line, the string will
1405 // be split again later in .get_lambda_tv().
1406 if (*p == NUL || vim9_comment_start(p))
1407 p = (char_u *)"";
1408 plen = STRLEN(p);
1409 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1410 if (pnl != NULL)
1411 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001412 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1413 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001414 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001415 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001416 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001417 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001418 // more is following after the "}", which was skipped
1419 last = cmdline;
1420 else
1421 // nothing is following the "}"
1422 last = (char_u *)"}";
1423 plen = STRLEN(last);
1424 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1425 if (pnl != NULL)
1426 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001427 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1428 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001429 }
1430
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001431 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001432 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001433 garray_T *tfgap = &evalarg->eval_tofree_ga;
1434
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001435 // Something comes after the "}".
1436 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001437
1438 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001439 if (ga_grow(tfgap, 1) == OK)
1440 {
1441 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1442 evalarg->eval_using_cmdline = TRUE;
1443 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001444 }
1445 else
1446 *arg = (char_u *)"";
1447
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001448 if (!evaluate)
1449 {
1450 ret = OK;
1451 goto erret;
1452 }
1453
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001454 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001455 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001456 if (ufunc == NULL)
1457 goto erret;
1458 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001459 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001460 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001461 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001462 ufunc->uf_refcount = 1;
1463 ufunc->uf_args = *newargs;
1464 newargs->ga_data = NULL;
1465 ufunc->uf_def_args = *default_args;
1466 default_args->ga_data = NULL;
1467 ufunc->uf_func_type = &t_func_any;
1468
1469 // error messages are for the first function line
1470 lnum_save = SOURCING_LNUM;
1471 SOURCING_LNUM = sourcing_lnum_top;
1472
1473 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001474 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001475 {
1476 SOURCING_LNUM = lnum_save;
1477 goto erret;
1478 }
1479
1480 // parse the return type, if any
1481 if (parse_return_type(ufunc, ret_type) == FAIL)
1482 goto erret;
1483
1484 pt = ALLOC_CLEAR_ONE(partial_T);
1485 if (pt == NULL)
1486 goto erret;
1487 pt->pt_func = ufunc;
1488 pt->pt_refcount = 1;
1489
1490 ufunc->uf_lines = newlines;
1491 newlines.ga_data = NULL;
1492 if (sandbox)
1493 ufunc->uf_flags |= FC_SANDBOX;
1494 if (!ASCII_ISUPPER(*ufunc->uf_name))
1495 ufunc->uf_flags |= FC_VIM9;
1496 ufunc->uf_script_ctx = current_sctx;
1497 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1498 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1499 set_function_type(ufunc);
1500
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001501 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1502
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001503 rettv->vval.v_partial = pt;
1504 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001505 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001506 ret = OK;
1507
1508erret:
1509 if (lnum_save >= 0)
1510 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001511 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001512 if (newargs != NULL)
1513 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001514 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001515 if (ufunc != NULL)
1516 {
1517 func_clear(ufunc, TRUE);
1518 func_free(ufunc, TRUE);
1519 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001520 return ret;
1521}
1522
1523/*
1524 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001525 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001526 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001527 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1528 */
1529 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001530get_lambda_tv(
1531 char_u **arg,
1532 typval_T *rettv,
1533 int types_optional,
1534 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001535{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001536 int evaluate = evalarg != NULL
1537 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001538 garray_T newargs;
1539 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001540 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001541 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001542 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001543 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001544 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001545 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001546 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001547 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001548 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001549 char_u *s;
1550 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001551 int *old_eval_lavars = eval_lavars_used;
1552 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001553 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001554 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001555 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001556 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001557 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001558 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001559
Bram Moolenaar4525a572022-02-13 11:57:33 +00001560 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001561 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001562
1563 ga_init(&newargs);
1564 ga_init(&newlines);
1565
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001566 // First, check if this is really a lambda expression. "->" or "=>" must
1567 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001568 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001569 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001570 types_optional ? &argtypes : NULL, types_optional,
1571 types_optional ? &arg_objm : NULL, evalarg,
1572 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001573 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001574 {
1575 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001576 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001577 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001578 ga_clear(&arg_objm);
1579 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001580 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001581 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001582
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001583 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001584 if (evaluate)
1585 pnewargs = &newargs;
1586 else
1587 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001588 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001589 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001590 types_optional ? &argtypes : NULL, types_optional,
1591 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001592 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001593 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001594 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001595 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001596 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001597 {
1598 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001599 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001600 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001601 ga_clear(&arg_objm);
1602 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001603 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001604 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001605 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001606 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001607
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001608 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1609 if (ret_type != NULL)
1610 {
1611 ret_type = vim_strsave(ret_type);
1612 tofree2 = ret_type;
1613 }
1614
Bram Moolenaare38eab22019-12-05 21:50:01 +01001615 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001616 if (evaluate)
1617 eval_lavars_used = &eval_lavars;
1618
Bram Moolenaar65c44152020-12-24 15:14:01 +01001619 *arg = skipwhite_and_linebreak(*arg, evalarg);
1620
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001621 // Recognize "{" as the start of a function body.
1622 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001623 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001624 if (evalarg == NULL)
1625 // cannot happen?
1626 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001627 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001628 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1629 types_optional ? &argtypes : NULL, varargs,
1630 &default_args, ret_type) == FAIL)
1631 goto errret;
1632 goto theend;
1633 }
1634 if (default_args.ga_len > 0)
1635 {
1636 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001637 goto errret;
1638 }
1639
Bram Moolenaare38eab22019-12-05 21:50:01 +01001640 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001641 start = *arg;
1642 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001643 if (ret == FAIL)
1644 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001645
Bram Moolenaar65c44152020-12-24 15:14:01 +01001646 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001647 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001648 *arg = skipwhite_and_linebreak(*arg, evalarg);
1649 if (**arg != '}')
1650 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001651 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001652 goto errret;
1653 }
1654 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001655 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001656
1657 if (evaluate)
1658 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001659 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001660 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001661 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001662 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001663 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001664
Bram Moolenaare01e5212023-01-08 20:31:18 +00001665 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001666 if (fp == NULL)
1667 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001668 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001669 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001670 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001671 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001672
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001673 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001674 if (ga_grow(&newlines, 1) == FAIL)
1675 goto errret;
1676
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001677 // If there are line breaks, we need to split up the string.
1678 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001679 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001680 line_end = end;
1681
1682 // Add "return " before the expression (or the first line).
1683 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001684 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001685 if (p == NULL)
1686 goto errret;
1687 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1688 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001689 vim_strncpy(p + 7, start, line_end - start);
1690
1691 if (line_end != end)
1692 {
1693 // Add more lines, split by line breaks. Thus is used when a
1694 // lambda with { cmds } is encountered.
1695 while (*line_end == '\n')
1696 {
1697 if (ga_grow(&newlines, 1) == FAIL)
1698 goto errret;
1699 start = line_end + 1;
1700 line_end = vim_strchr(start, '\n');
1701 if (line_end == NULL)
1702 line_end = end;
1703 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1704 vim_strnsave(start, line_end - start);
1705 }
1706 }
1707
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001708 if (strstr((char *)p + 7, "a:") == NULL)
1709 // No a: variables are used for sure.
1710 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001711
1712 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001713 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001714 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001715 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001716 if (types_optional)
1717 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001718 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001719 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001720 goto errret;
1721 if (ret_type != NULL)
1722 {
1723 fp->uf_ret_type = parse_type(&ret_type,
1724 &fp->uf_type_list, TRUE);
1725 if (fp->uf_ret_type == NULL)
1726 goto errret;
1727 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001728 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001729 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001730 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001731
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001732 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001733 if (current_funccal != NULL && eval_lavars)
1734 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001735 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001736 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001737 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001738 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001739
1740#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001741 if (prof_def_func())
1742 func_do_profile(fp);
1743#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001744 if (sandbox)
1745 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001746 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001747 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001748 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001749 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001750 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001751 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001752 // Use the line number of the arguments.
1753 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001754
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001755 function_using_block_scopes(fp, evalarg->eval_cstack);
1756
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001757 pt->pt_func = fp;
1758 pt->pt_refcount = 1;
1759 rettv->vval.v_partial = pt;
1760 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001761
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001762 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001763 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001764
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001765theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001766 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001767 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001768 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001769 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001770 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001771 ga_clear(&arg_objm);
1772 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001773
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001774 return OK;
1775
1776errret:
1777 ga_clear_strings(&newargs);
1778 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001779 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001780 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001781 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001782 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001783 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001784 if (fp != NULL)
1785 vim_free(fp->uf_arg_types);
1786 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001787 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001788 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001789 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001790 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001791 return FAIL;
1792}
1793
1794/*
1795 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1796 * name it contains, otherwise return "name".
1797 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1798 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001799 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1800 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001801 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001802 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001803 */
1804 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001805deref_func_name(
1806 char_u *name,
1807 int *lenp,
1808 partial_T **partialp,
1809 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001810 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001811 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001812 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001813{
1814 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001815 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001816 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001817 char_u *s = NULL;
1818 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001819 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001820
1821 if (partialp != NULL)
1822 *partialp = NULL;
1823
1824 cc = name[*lenp];
1825 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001826
Bram Moolenaar71f21932022-01-07 18:20:55 +00001827 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001828 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001829 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001830 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001831 tv = &v->di_tv;
1832 }
1833 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1834 {
1835 imported_T *import;
1836 char_u *p = name;
1837 int len = *lenp;
1838
1839 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001840 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001841 p = name + 2;
1842 len -= 2;
1843 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001844 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001845
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001846 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001847 if (import != NULL)
1848 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001849 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001850 if (new_function)
1851 semsg(_(e_redefining_imported_item_str), name);
1852 else
1853 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001854 name[len] = cc;
1855 *lenp = 0;
1856 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001857 }
1858 }
1859
1860 if (tv != NULL)
1861 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001862 if (found_var != NULL)
1863 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001864 if (tv->v_type == VAR_FUNC)
1865 {
1866 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001867 {
1868 *lenp = 0;
1869 return (char_u *)""; // just in case
1870 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001871 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001872 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001873 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001874
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001875 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001876 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001877 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001878
1879 if (pt == NULL)
1880 {
1881 *lenp = 0;
1882 return (char_u *)""; // just in case
1883 }
1884 if (partialp != NULL)
1885 *partialp = pt;
1886 s = partial_name(pt);
1887 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001888 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001889
1890 if (s != NULL)
1891 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001892 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001893 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001894 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001895
1896 if (sv != NULL)
1897 *type = sv->sv_type;
1898 }
1899 return s;
1900 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001901 }
1902
1903 return name;
1904}
1905
1906/*
1907 * Give an error message with a function name. Handle <SNR> things.
1908 * "ermsg" is to be passed without translation, use N_() instead of _().
1909 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001910 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001911emsg_funcname(char *ermsg, char_u *name)
1912{
Bram Moolenaar54598062022-01-13 12:05:09 +00001913 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001914
Bram Moolenaar54598062022-01-13 12:05:09 +00001915 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001916 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001917 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001918 if (p != name)
1919 vim_free(p);
1920}
1921
1922/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001923 * Get function arguments at "*arg" and advance it.
1924 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001925 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001926 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001927 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001928get_func_arguments(
1929 char_u **arg,
1930 evalarg_T *evalarg,
1931 int partial_argc,
1932 typval_T *argvars,
Ernie Raelb077b582023-12-14 20:11:44 +01001933 int *argcount,
1934 int is_builtin)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001935{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001936 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001937 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001938 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001939 int evaluate = evalarg == NULL
1940 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001941
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001942 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001943 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001944 // skip the '(' or ',' and possibly line breaks
1945 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1946
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001947 if (*argp == ')' || *argp == ',' || *argp == NUL)
1948 break;
Ernie Raelb077b582023-12-14 20:11:44 +01001949
1950 int arg_idx = *argcount;
1951 if (eval1(&argp, &argvars[arg_idx], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001952 {
1953 ret = FAIL;
1954 break;
1955 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001956 ++*argcount;
Ernie Raelb077b582023-12-14 20:11:44 +01001957 if (!is_builtin && check_typval_is_value(&argvars[arg_idx]) == FAIL)
1958 {
1959 ret = FAIL;
1960 break;
1961 }
1962
Bram Moolenaar9d489562020-07-30 20:08:50 +02001963 // The comma should come right after the argument, but this wasn't
1964 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001965 if (vim9script)
1966 {
1967 if (*argp != ',' && *skipwhite(argp) == ',')
1968 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001969 if (evaluate)
1970 semsg(_(e_no_white_space_allowed_before_str_str),
1971 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001972 ret = FAIL;
1973 break;
1974 }
1975 }
1976 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001977 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001978 if (*argp != ',')
1979 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001980 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001981 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001982 if (evaluate)
1983 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001984 ret = FAIL;
1985 break;
1986 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001987 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001988
Bram Moolenaare6b53242020-07-01 17:28:33 +02001989 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001990 if (*argp == ')')
1991 ++argp;
1992 else
1993 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001994 *arg = argp;
1995 return ret;
1996}
1997
1998/*
1999 * Call a function and put the result in "rettv".
2000 * Return OK or FAIL.
2001 */
2002 int
2003get_func_tv(
2004 char_u *name, // name of the function
2005 int len, // length of "name" or -1 to use strlen()
2006 typval_T *rettv,
2007 char_u **arg, // argument, pointing to the '('
2008 evalarg_T *evalarg, // for line continuation
2009 funcexe_T *funcexe) // various values
2010{
2011 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002012 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002013 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
2014 int argcount = 0; // number of arguments found
2015 int vim9script = in_vim9script();
2016 int evaluate = evalarg == NULL
2017 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
2018
2019 argp = *arg;
2020 ret = get_func_arguments(&argp, evalarg,
2021 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
Ernie Raelb077b582023-12-14 20:11:44 +01002022 argvars, &argcount, builtin_function(name, -1));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002023
2024 if (ret == OK)
2025 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002026 int i = 0;
2027 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002028
2029 if (get_vim_var_nr(VV_TESTING))
2030 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002031 // Prepare for calling test_garbagecollect_now(), need to know
2032 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002033 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002034 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002035 for (i = 0; i < argcount; ++i)
2036 if (ga_grow(&funcargs, 1) == OK)
2037 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2038 &argvars[i];
2039 }
2040
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002041 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002042 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002043 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002044 // An error in a builtin function does not return FAIL, but we do
2045 // want to abort further processing if an error was given.
2046 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002047 clear_tv(rettv);
2048 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002049
2050 funcargs.ga_len -= i;
2051 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002052 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002053 {
2054 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002055 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002056 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002057 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002058 }
2059
2060 while (--argcount >= 0)
2061 clear_tv(&argvars[argcount]);
2062
Bram Moolenaar4525a572022-02-13 11:57:33 +00002063 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002064 *arg = argp;
2065 else
2066 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002067 return ret;
2068}
2069
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002070/*
2071 * Return TRUE if "p" starts with "<SID>" or "s:".
2072 * Only works if eval_fname_script() returned non-zero for "p"!
2073 */
2074 static int
2075eval_fname_sid(char_u *p)
2076{
2077 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2078}
2079
2080/*
2081 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2082 * Change <SNR>123_name() to K_SNR 123_name().
2083 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002084 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002085 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002086 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002087fname_trans_sid(
2088 char_u *name,
2089 char_u *fname_buf,
2090 char_u **tofree,
2091 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002092{
2093 int llen;
2094 char_u *fname;
2095 int i;
2096
2097 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002098 if (llen == 0)
2099 return name; // no prefix
2100
2101 fname_buf[0] = K_SPECIAL;
2102 fname_buf[1] = KS_EXTRA;
2103 fname_buf[2] = (int)KE_SNR;
2104 i = 3;
2105 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002106 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002107 if (current_sctx.sc_sid <= 0)
2108 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002109 else
2110 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002111 sprintf((char *)fname_buf + 3, "%ld_",
2112 (long)current_sctx.sc_sid);
2113 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002114 }
2115 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002116 if (i + STRLEN(name + llen) < FLEN_FIXED)
2117 {
2118 STRCPY(fname_buf + i, name + llen);
2119 fname = fname_buf;
2120 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002121 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002122 {
2123 fname = alloc(i + STRLEN(name + llen) + 1);
2124 if (fname == NULL)
2125 *error = FCERR_OTHER;
2126 else
2127 {
2128 *tofree = fname;
2129 mch_memmove(fname, fname_buf, (size_t)i);
2130 STRCPY(fname + i, name + llen);
2131 }
2132 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002133 return fname;
2134}
2135
2136/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002137 * Concatenate the script ID and function name into "<SNR>99_name".
2138 * "buffer" must have size MAX_FUNC_NAME_LEN.
2139 */
2140 void
2141func_name_with_sid(char_u *name, int sid, char_u *buffer)
2142{
2143 // A script-local function is stored as "<SNR>99_name".
2144 buffer[0] = K_SPECIAL;
2145 buffer[1] = KS_EXTRA;
2146 buffer[2] = (int)KE_SNR;
2147 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2148 (long)sid, name);
2149}
2150
2151/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002152 * Find a function "name" in script "sid".
2153 */
2154 static ufunc_T *
2155find_func_with_sid(char_u *name, int sid)
2156{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002157 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002158 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159
Bram Moolenaarb8822442022-01-11 15:24:05 +00002160 if (!SCRIPT_ID_VALID(sid))
2161 return NULL; // not in a script
2162
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002163 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 hi = hash_find(&func_hashtab, buffer);
2165 if (!HASHITEM_EMPTY(hi))
2166 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002167 return NULL;
2168}
2169
2170/*
2171 * Find a function "name" in script "sid" prefixing the autoload prefix.
2172 */
2173 static ufunc_T *
2174find_func_with_prefix(char_u *name, int sid)
2175{
2176 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002177 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002178 scriptitem_T *si;
2179
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002180 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002181 return NULL; // already has the prefix
2182 if (!SCRIPT_ID_VALID(sid))
2183 return NULL; // not in a script
2184 si = SCRIPT_ITEM(sid);
2185 if (si->sn_autoload_prefix != NULL)
2186 {
2187 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2188 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002189 char_u *namep;
2190
2191 // skip a "<SNR>99_" prefix
2192 namep = untrans_function_name(name);
2193 if (namep == NULL)
2194 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002195
2196 // An exported function in an autoload script is stored as
2197 // "dir#path#name".
2198 if (len < sizeof(buffer))
2199 auto_name = buffer;
2200 else
2201 auto_name = alloc(len);
2202 if (auto_name != NULL)
2203 {
2204 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002205 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002206 hi = hash_find(&func_hashtab, auto_name);
2207 if (auto_name != buffer)
2208 vim_free(auto_name);
2209 if (!HASHITEM_EMPTY(hi))
2210 return HI2UF(hi);
2211 }
2212 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213
2214 return NULL;
2215}
2216
2217/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002218 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002219 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2220 * functions.
2221 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002222 * Return NULL for unknown function.
2223 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002224 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002225find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002226{
2227 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002230 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002232 // Find script-local function before global one.
2233 if (in_vim9script() && eval_isnamec1(*name)
2234 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002236 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2237 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002238 if (func != NULL)
2239 return func;
2240 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002241 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2242 {
2243 char_u *p = name + 5;
2244 long sid;
2245
2246 // printable "<SNR>123_Name" form
2247 sid = getdigits(&p);
2248 if (*p == '_')
2249 {
2250 func = find_func_with_sid(p + 1, (int)sid);
2251 if (func != NULL)
2252 return func;
2253 }
2254 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002256
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002257 if ((flags & FFED_NO_GLOBAL) == 0)
2258 {
2259 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002260 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002261 if (!HASHITEM_EMPTY(hi))
2262 return HI2UF(hi);
2263 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264
Bram Moolenaarb8822442022-01-11 15:24:05 +00002265 // Find autoload function if this is an autoload script.
2266 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2267 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268}
2269
2270/*
2271 * Find a function by name, return pointer to it in ufuncs.
2272 * "cctx" is passed in a :def function to find imported functions.
2273 * Return NULL for unknown or dead function.
2274 */
2275 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002276find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002278 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279
2280 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2281 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002282 return NULL;
2283}
2284
2285/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002286 * Return TRUE if "ufunc" is a global function.
2287 */
2288 int
2289func_is_global(ufunc_T *ufunc)
2290{
2291 return ufunc->uf_name[0] != K_SPECIAL;
2292}
2293
2294/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002295 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2296 */
2297 int
2298func_requires_g_prefix(ufunc_T *ufunc)
2299{
2300 return ufunc->uf_name[0] != K_SPECIAL
2301 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002302 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01002303 && !SAFE_isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002304}
2305
2306/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002307 * Copy the function name of "fp" to buffer "buf".
2308 * "buf" must be able to hold the function name plus three bytes.
2309 * Takes care of script-local function names.
2310 */
2311 static void
2312cat_func_name(char_u *buf, ufunc_T *fp)
2313{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002314 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002315 {
2316 STRCPY(buf, "<SNR>");
2317 STRCAT(buf, fp->uf_name + 3);
2318 }
2319 else
2320 STRCPY(buf, fp->uf_name);
2321}
2322
2323/*
2324 * Add a number variable "name" to dict "dp" with value "nr".
2325 */
2326 static void
2327add_nr_var(
2328 dict_T *dp,
2329 dictitem_T *v,
2330 char *name,
2331 varnumber_T nr)
2332{
2333 STRCPY(v->di_key, name);
2334 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002335 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002336 v->di_tv.v_type = VAR_NUMBER;
2337 v->di_tv.v_lock = VAR_FIXED;
2338 v->di_tv.vval.v_number = nr;
2339}
2340
2341/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002342 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002343 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002344 static void
2345free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002346{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002347 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002348
Bram Moolenaarca16c602022-09-06 18:57:08 +01002349 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002350 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002351 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002352
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002353 // When garbage collecting a funccall_T may be freed before the
2354 // function that references it, clear its uf_scoped field.
2355 // The function may have been redefined and point to another
2356 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002357 if (fp != NULL && fp->uf_scoped == fc)
2358 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002359 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002360 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002361
Bram Moolenaarca16c602022-09-06 18:57:08 +01002362 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002363 vim_free(fc);
2364}
2365
2366/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002367 * Free "fc" and what it contains.
2368 * Can be called only when "fc" is kept beyond the period of it called,
2369 * i.e. after cleanup_function_call(fc).
2370 */
2371 static void
2372free_funccal_contents(funccall_T *fc)
2373{
2374 listitem_T *li;
2375
2376 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002377 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002378
2379 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002380 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002381
2382 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002383 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002384 clear_tv(&li->li_tv);
2385
2386 free_funccal(fc);
2387}
2388
2389/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002390 * Handle the last part of returning from a function: free the local hashtable.
2391 * Unless it is still in use by a closure.
2392 */
2393 static void
2394cleanup_function_call(funccall_T *fc)
2395{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002396 int may_free_fc = fc->fc_refcount <= 0;
2397 int free_fc = TRUE;
2398
Bram Moolenaarca16c602022-09-06 18:57:08 +01002399 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002400
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002401 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002402 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2403 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002404 else
2405 free_fc = FALSE;
2406
2407 // If the a:000 list and the l: and a: dicts are not referenced and
2408 // there is no closure using it, we can free the funccall_T and what's
2409 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002410 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2411 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002412 else
2413 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002414 int todo;
2415 hashitem_T *hi;
2416 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002417
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002418 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002419
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002420 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002421 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002422 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002423 {
2424 if (!HASHITEM_EMPTY(hi))
2425 {
2426 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002427 di = HI2DI(hi);
2428 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002429 }
2430 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002431 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002432
Bram Moolenaarca16c602022-09-06 18:57:08 +01002433 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2434 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002435 else
2436 {
2437 listitem_T *li;
2438
2439 free_fc = FALSE;
2440
2441 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002442 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002443 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002444 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002445
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002446 if (free_fc)
2447 free_funccal(fc);
2448 else
2449 {
2450 static int made_copy = 0;
2451
2452 // "fc" is still in use. This can happen when returning "a:000",
2453 // assigning "l:" to a global variable or defining a closure.
2454 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002455 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002456 previous_funccal = fc;
2457
2458 if (want_garbage_collect)
2459 // If garbage collector is ready, clear count.
2460 made_copy = 0;
2461 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002462 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002463 // We have made a lot of copies, worth 4 Mbyte. This can happen
2464 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002465 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002466 // too much memory.
2467 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002468 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002469 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002470 }
2471}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002472
2473/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002474 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2475 */
2476 static int
2477numbered_function(char_u *name)
2478{
Keith Thompson184f71c2024-01-04 21:19:04 +01002479 return SAFE_isdigit(*name)
2480 || (name[0] == 'g' && name[1] == ':' && SAFE_isdigit(name[2]));
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002481}
2482
2483/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002484 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002485 * 1. ordinary names, function defined with :function or :def;
2486 * can start with "<SNR>123_" literally or with K_SPECIAL.
2487 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002488 * For the first we only count the name stored in func_hashtab as a reference,
2489 * using function() does not count as a reference, because the function is
2490 * looked up by name.
2491 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002492 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002493func_name_refcount(char_u *name)
2494{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002495 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002496}
2497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498/*
2499 * Unreference "fc": decrement the reference count and free it when it
2500 * becomes zero. "fp" is detached from "fc".
2501 * When "force" is TRUE we are exiting.
2502 */
2503 static void
2504funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2505{
2506 funccall_T **pfc;
2507 int i;
2508
2509 if (fc == NULL)
2510 return;
2511
2512 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002513 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2514 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2515 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2516 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 {
2518 if (fc == *pfc)
2519 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002520 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 free_funccal_contents(fc);
2522 return;
2523 }
2524 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002525 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2526 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2527 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528}
2529
2530/*
2531 * Remove the function from the function hashtable. If the function was
2532 * deleted while it still has references this was already done.
2533 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2534 */
2535 static int
2536func_remove(ufunc_T *fp)
2537{
2538 hashitem_T *hi;
2539
2540 // Return if it was already virtually deleted.
2541 if (fp->uf_flags & FC_DEAD)
2542 return FALSE;
2543
2544 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002545 if (HASHITEM_EMPTY(hi))
2546 return FALSE;
2547
2548 // When there is a def-function index do not actually remove the
2549 // function, so we can find the index when defining the function again.
2550 // Do remove it when it's a copy.
2551 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002553 fp->uf_flags |= FC_DEAD;
2554 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002556 hash_remove(&func_hashtab, hi, "remove function");
2557 fp->uf_flags |= FC_DELETED;
2558 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559}
2560
2561 static void
2562func_clear_items(ufunc_T *fp)
2563{
2564 ga_clear_strings(&(fp->uf_args));
2565 ga_clear_strings(&(fp->uf_def_args));
2566 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002568 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002569 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002570 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002571
2572 // Increment the refcount of this function to avoid it being freed
2573 // recursively when the partial is freed.
2574 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002575 partial_unref(fp->uf_partial);
2576 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002577 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002578
2579#ifdef FEAT_LUA
2580 if (fp->uf_cb_free != NULL)
2581 {
2582 fp->uf_cb_free(fp->uf_cb_state);
2583 fp->uf_cb_free = NULL;
2584 }
2585
2586 fp->uf_cb_state = NULL;
2587 fp->uf_cb = NULL;
2588#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589#ifdef FEAT_PROFILE
2590 VIM_CLEAR(fp->uf_tml_count);
2591 VIM_CLEAR(fp->uf_tml_total);
2592 VIM_CLEAR(fp->uf_tml_self);
2593#endif
2594}
2595
2596/*
2597 * Free all things that a function contains. Does not free the function
2598 * itself, use func_free() for that.
2599 * When "force" is TRUE we are exiting.
2600 */
2601 static void
2602func_clear(ufunc_T *fp, int force)
2603{
2604 if (fp->uf_cleared)
2605 return;
2606 fp->uf_cleared = TRUE;
2607
2608 // clear this function
2609 func_clear_items(fp);
2610 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002611 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612}
2613
2614/*
2615 * Free a function and remove it from the list of functions. Does not free
2616 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002617 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002618 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002620 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002621func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622{
2623 // Only remove it when not done already, otherwise we would remove a newer
2624 // version of the function with the same name.
2625 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2626 func_remove(fp);
2627
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002628 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002629 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002630 if (fp->uf_dfunc_idx > 0)
2631 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002632 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002634 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002635 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002636 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637}
2638
2639/*
2640 * Free all things that a function contains and free the function itself.
2641 * When "force" is TRUE we are exiting.
2642 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002643 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644func_clear_free(ufunc_T *fp, int force)
2645{
2646 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002647 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2648 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002649 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002650 else
2651 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652}
2653
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002654/*
2655 * Copy already defined function "lambda" to a new function with name "global".
2656 * This is for when a compiled function defines a global function.
2657 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002658 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002659copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002660 char_u *lambda,
2661 char_u *global,
2662 loopvarinfo_T *loopvarinfo,
2663 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002664{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002665 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002666 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002667
2668 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002669 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002670 semsg(_(e_lambda_function_not_found_str), lambda);
2671 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002672 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002673
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002674 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002675 if (fp != NULL)
2676 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002677 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002678 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002679 return FAIL;
2680 }
2681
Bram Moolenaare01e5212023-01-08 20:31:18 +00002682 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002683 if (fp == NULL)
2684 return FAIL;
2685
2686 fp->uf_varargs = ufunc->uf_varargs;
2687 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2688 fp->uf_def_status = ufunc->uf_def_status;
2689 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2690 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2691 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2692 == FAIL
2693 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2694 goto failed;
2695
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002696 if (ufunc->uf_arg_types != NULL)
2697 {
2698 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2699 if (fp->uf_arg_types == NULL)
2700 goto failed;
2701 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2702 sizeof(type_T *) * fp->uf_args.ga_len);
2703 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002704 if (ufunc->uf_va_name != NULL)
2705 {
2706 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2707 if (fp->uf_va_name == NULL)
2708 goto failed;
2709 }
2710 fp->uf_ret_type = ufunc->uf_ret_type;
2711
2712 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002713
2714 fp->uf_name_exp = NULL;
2715 set_ufunc_name(fp, global);
2716
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002717 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002718
2719 // the referenced dfunc_T is now used one more time
2720 link_def_function(fp);
2721
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002722 // Create a partial to store the context of the function where it was
2723 // instantiated. Only needs to be done once. Do this on the original
2724 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002725 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2726 {
2727 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2728
2729 if (pt == NULL)
2730 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002731 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002732 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002733 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002734 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002735 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002736 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002737 }
2738
2739 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002740
2741failed:
2742 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002743 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002744}
2745
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002746static int funcdepth = 0;
2747
2748/*
2749 * Increment the function call depth count.
2750 * Return FAIL when going over 'maxfuncdepth'.
2751 * Otherwise return OK, must call funcdepth_decrement() later!
2752 */
2753 int
2754funcdepth_increment(void)
2755{
2756 if (funcdepth >= p_mfd)
2757 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002758 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002759 return FAIL;
2760 }
2761 ++funcdepth;
2762 return OK;
2763}
2764
2765 void
2766funcdepth_decrement(void)
2767{
2768 --funcdepth;
2769}
2770
2771/*
2772 * Get the current function call depth.
2773 */
2774 int
2775funcdepth_get(void)
2776{
2777 return funcdepth;
2778}
2779
2780/*
2781 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002782 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002783 * times as funcdepth_increment().
2784 */
2785 void
2786funcdepth_restore(int depth)
2787{
2788 funcdepth = depth;
2789}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002790
2791/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002792 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2793 * "rettv".
2794 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2795 * Returns NULL when allocation fails.
2796 */
2797 funccall_T *
2798create_funccal(ufunc_T *fp, typval_T *rettv)
2799{
2800 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2801
2802 if (fc == NULL)
2803 return NULL;
2804 fc->fc_caller = current_funccal;
2805 current_funccal = fc;
2806 fc->fc_func = fp;
2807 func_ptr_ref(fp);
2808 fc->fc_rettv = rettv;
2809 return fc;
2810}
2811
2812/*
2813 * To be called when returning from a compiled function; restores
2814 * current_funccal.
2815 */
2816 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002817remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002818{
2819 funccall_T *fc = current_funccal;
2820
2821 current_funccal = fc->fc_caller;
2822 free_funccal(fc);
2823}
2824
2825/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002826 * Call a user function.
2827 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002828 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002829call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002830 ufunc_T *fp, // pointer to function
2831 int argcount, // nr of args
2832 typval_T *argvars, // arguments
2833 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002834 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002835 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002836{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002837 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002838 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002839 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002840 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002841 funccall_T *fc;
2842 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002843 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002844 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002845 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002846 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002847 int i;
2848 int ai;
2849 int islambda = FALSE;
2850 char_u numbuf[NUMBUFLEN];
2851 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002852 typval_T *tv_to_free[MAX_FUNC_ARGS];
2853 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002854#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002855 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002856#endif
ichizok7e5fe382023-04-15 13:17:50 +01002857 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002858
Bram Moolenaarb2049902021-01-24 12:53:53 +01002859#ifdef FEAT_PROFILE
2860 CLEAR_FIELD(profile_info);
2861#endif
2862
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002863 // If depth of calling is getting too high, don't execute the function.
2864 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002865 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002866 rettv->v_type = VAR_NUMBER;
2867 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002868 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002869 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002870
Bram Moolenaare38eab22019-12-05 21:50:01 +01002871 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002872
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002873 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002874 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002875 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002876 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002877 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002878 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2879 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002880 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002881 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002882
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002883 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002885#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002886 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002887#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002888 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002889#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002890 if (do_profiling == PROF_YES)
2891 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002892#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002893 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002894 if (call_def_function(fp, argcount, argvars, 0,
2895 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2896 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002897 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002898#ifdef FEAT_PROFILE
2899 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002900 || (caller != NULL && caller->uf_profiling)))
2901 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002902#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002903 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002904 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002905 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 }
2907
Bram Moolenaar38453522021-11-28 22:00:12 +00002908 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002909
2910 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002911 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2912 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2913 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002914 */
2915 /*
2916 * Init l: variables.
2917 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002918 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002919 if (selfdict != NULL)
2920 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002921 // Set l:self to "selfdict". Use "name" to avoid a warning from
2922 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002923 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002924 name = v->di_key;
2925 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002926 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002927 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002928 v->di_tv.v_type = VAR_DICT;
2929 v->di_tv.v_lock = 0;
2930 v->di_tv.vval.v_dict = selfdict;
2931 ++selfdict->dv_refcount;
2932 }
2933
2934 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002935 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002936 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002937 * Set a:000 to a list with room for the "..." arguments.
2938 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002939 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002940 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002941 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002942 (varnumber_T)(argcount >= fp->uf_args.ga_len
2943 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002944 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002945 if ((fp->uf_flags & FC_NOARGS) == 0)
2946 {
2947 // Use "name" to avoid a warning from some compiler that checks the
2948 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002949 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002950 name = v->di_key;
2951 STRCPY(name, "000");
2952 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002953 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002954 v->di_tv.v_type = VAR_LIST;
2955 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002956 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002957 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002958 CLEAR_FIELD(fc->fc_l_varlist);
2959 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2960 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002961
2962 /*
2963 * Set a:firstline to "firstline" and a:lastline to "lastline".
2964 * Set a:name to named arguments.
2965 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002966 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002967 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002968 if ((fp->uf_flags & FC_NOARGS) == 0)
2969 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002970 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2971 "firstline", (varnumber_T)funcexe->fe_firstline);
2972 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2973 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002974 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002975 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002976 {
2977 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002978 typval_T def_rettv;
2979 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002980
2981 ai = i - fp->uf_args.ga_len;
2982 if (ai < 0)
2983 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002984 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002985 name = FUNCARG(fp, i);
2986 if (islambda)
2987 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002988
2989 // evaluate named argument default expression
2990 isdefault = ai + fp->uf_def_args.ga_len >= 0
2991 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2992 && argvars[i].vval.v_number == VVAL_NONE));
2993 if (isdefault)
2994 {
2995 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002996
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002997 def_rettv.v_type = VAR_NUMBER;
2998 def_rettv.vval.v_number = -1;
2999
3000 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
3001 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003002 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003003 {
3004 default_arg_err = 1;
3005 break;
3006 }
3007 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003008 }
3009 else
3010 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003011 if ((fp->uf_flags & FC_NOARGS) != 0)
3012 // Bail out if no a: arguments used (in lambda).
3013 break;
3014
Bram Moolenaare38eab22019-12-05 21:50:01 +01003015 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003016 sprintf((char *)numbuf, "%d", ai + 1);
3017 name = numbuf;
3018 }
3019 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
3020 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003021 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003022 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003023 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003024 }
3025 else
3026 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003027 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003028 if (v == NULL)
3029 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003030 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003031 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003032
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003033 // Note: the values are copied directly to avoid alloc/free.
3034 // "argvars" must have VAR_FIXED for v_lock.
3035 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003036 v->di_tv.v_lock = VAR_FIXED;
3037
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003038 if (isdefault)
3039 // Need to free this later, no matter where it's stored.
3040 tv_to_free[tv_to_free_len++] = &v->di_tv;
3041
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003042 if (addlocal)
3043 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003044 // Named arguments should be accessed without the "a:" prefix in
3045 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003046 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003047 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003048 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003049 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003050 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003051
3052 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3053 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003054 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003055
3056 li->li_tv = argvars[i];
3057 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003058 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003059 }
3060 }
3061
Bram Moolenaare38eab22019-12-05 21:50:01 +01003062 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003063 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003064
3065 if (fp->uf_flags & FC_SANDBOX)
3066 {
3067 using_sandbox = TRUE;
3068 ++sandbox;
3069 }
3070
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003071 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003072 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003073 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003074 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003075 ++no_wait_return;
3076 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003077
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003078 smsg(_("calling %s"), SOURCING_NAME);
3079 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003080 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003081 char_u buf[MSG_BUF_LEN];
3082 char_u numbuf2[NUMBUFLEN];
3083 char_u *tofree;
3084 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003085
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003086 msg_puts("(");
3087 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003088 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003089 if (i > 0)
3090 msg_puts(", ");
3091 if (argvars[i].v_type == VAR_NUMBER)
3092 msg_outnum((long)argvars[i].vval.v_number);
3093 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003094 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003095 // Do not want errors such as E724 here.
3096 ++emsg_off;
3097 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3098 --emsg_off;
3099 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003100 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003101 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003102 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003103 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3104 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003105 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003106 msg_puts((char *)s);
3107 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003108 }
3109 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003110 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003111 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003112 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003113 msg_puts("\n"); // don't overwrite this either
3114
3115 verbose_leave_scroll();
3116 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003117 }
3118#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003119 if (do_profiling == PROF_YES)
3120 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003121 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003122#endif
3123
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003124 // "legacy" does not apply to commands in the function
3125 sticky_cmdmod_flags = 0;
3126
Bram Moolenaar98aff652022-09-06 21:02:35 +01003127 // If called from a compiled :def function the execution context must be
3128 // hidden, any deferred functions need to be added to the function being
3129 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003130 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003131
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003132 save_current_sctx = current_sctx;
3133 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003134 save_did_emsg = did_emsg;
3135 did_emsg = FALSE;
3136
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003137 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003138 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003139 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003140 retval = FCERR_FAILED;
3141 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003142 else if (islambda)
3143 {
3144 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3145
3146 // A Lambda always has the command "return {expr}". It is much faster
3147 // to evaluate {expr} directly.
3148 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003149 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003150 --ex_nesting_level;
3151 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003152 else
3153 // call do_cmdline() to execute the lines
3154 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003155 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3156
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003157 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003158 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003159
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003160 if (RedrawingDisabled > 0)
3161 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003162
Bram Moolenaare38eab22019-12-05 21:50:01 +01003163 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003164 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3165 {
3166 clear_tv(rettv);
3167 rettv->v_type = VAR_NUMBER;
3168 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003169
3170 // In corner cases returning a "failed" value is not backwards
3171 // compatible. Only do this for Vim9 script.
3172 if (in_vim9script())
3173 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003174 }
3175
3176#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003177 if (do_profiling == PROF_YES)
3178 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003179 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003180
3181 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3182 profile_may_end_func(&profile_info, fp, caller);
3183 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003184#endif
3185
Bram Moolenaare38eab22019-12-05 21:50:01 +01003186 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003187 if (p_verbose >= 12)
3188 {
3189 ++no_wait_return;
3190 verbose_enter_scroll();
3191
3192 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003193 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003194 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003195 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003196 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003197 else
3198 {
3199 char_u buf[MSG_BUF_LEN];
3200 char_u numbuf2[NUMBUFLEN];
3201 char_u *tofree;
3202 char_u *s;
3203
Bram Moolenaare38eab22019-12-05 21:50:01 +01003204 // The value may be very long. Skip the middle part, so that we
3205 // have some idea how it starts and ends. smsg() would always
3206 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003207 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003208 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003209 --emsg_off;
3210 if (s != NULL)
3211 {
3212 if (vim_strsize(s) > MSG_BUF_CLEN)
3213 {
3214 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3215 s = buf;
3216 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003217 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003218 vim_free(tofree);
3219 }
3220 }
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
ichizok7e5fe382023-04-15 13:17:50 +01003227 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003228 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003229 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003230 restore_current_ectx(save_current_ectx);
3231
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003232#ifdef FEAT_PROFILE
3233 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003234 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003235#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003236 if (using_sandbox)
3237 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003238 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003239
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003240 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003241 {
3242 ++no_wait_return;
3243 verbose_enter_scroll();
3244
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003245 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003246 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003247
3248 verbose_leave_scroll();
3249 --no_wait_return;
3250 }
3251
3252 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003253 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003254 for (i = 0; i < tv_to_free_len; ++i)
3255 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003256 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003257
3258 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003259}
3260
3261/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003262 * Check the argument count for user function "fp".
3263 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3264 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003265 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003266check_user_func_argcount(ufunc_T *fp, int argcount)
3267{
3268 int regular_args = fp->uf_args.ga_len;
3269
3270 if (argcount < regular_args - fp->uf_def_args.ga_len)
3271 return FCERR_TOOFEW;
3272 else if (!has_varargs(fp) && argcount > regular_args)
3273 return FCERR_TOOMANY;
3274 return FCERR_UNKNOWN;
3275}
3276
3277/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003279 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003280 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281call_user_func_check(
3282 ufunc_T *fp,
3283 int argcount,
3284 typval_T *argvars,
3285 typval_T *rettv,
3286 funcexe_T *funcexe,
3287 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003288{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003289 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003290
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003291#ifdef FEAT_LUA
3292 if (fp->uf_flags & FC_CFUNC)
3293 {
3294 cfunc_T cb = fp->uf_cb;
3295
3296 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3297 }
3298#endif
3299
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003300 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3301 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003302 error = check_user_func_argcount(fp, argcount);
3303 if (error != FCERR_UNKNOWN)
3304 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003305
Bram Moolenaar50824712020-12-20 21:10:17 +01003306 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003307 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003309 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003311 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 int did_save_redo = FALSE;
3313 save_redo_T save_redo;
3314
3315 /*
3316 * Call the user function.
3317 * Save and restore search patterns, script variables and
3318 * redo buffer.
3319 */
3320 save_search_patterns();
3321 if (!ins_compl_active())
3322 {
3323 saveRedobuff(&save_redo);
3324 did_save_redo = TRUE;
3325 }
3326 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003327 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003328 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3330 // Function was unreferenced while being used, free it now.
3331 func_clear_free(fp, FALSE);
3332 if (did_save_redo)
3333 restoreRedobuff(&save_redo);
3334 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003335 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003336
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003338}
3339
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003340static funccal_entry_T *funccal_stack = NULL;
3341
3342/*
3343 * Save the current function call pointer, and set it to NULL.
3344 * Used when executing autocommands and for ":source".
3345 */
3346 void
3347save_funccal(funccal_entry_T *entry)
3348{
3349 entry->top_funccal = current_funccal;
3350 entry->next = funccal_stack;
3351 funccal_stack = entry;
3352 current_funccal = NULL;
3353}
3354
3355 void
3356restore_funccal(void)
3357{
3358 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003359 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003360 else
3361 {
3362 current_funccal = funccal_stack->top_funccal;
3363 funccal_stack = funccal_stack->next;
3364 }
3365}
3366
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003367 funccall_T *
3368get_current_funccal(void)
3369{
3370 return current_funccal;
3371}
3372
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003373/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003374 * Return TRUE when currently at the script level:
3375 * - not in a function
3376 * - not executing an autocommand
3377 * Note that when an autocommand sources a script the result is FALSE;
3378 */
3379 int
3380at_script_level(void)
3381{
3382 return current_funccal == NULL && autocmd_match == NULL;
3383}
3384
3385/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003386 * Mark all functions of script "sid" as deleted.
3387 */
3388 void
3389delete_script_functions(int sid)
3390{
3391 hashitem_T *hi;
3392 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003393 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003394 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003395 size_t len;
3396
3397 buf[0] = K_SPECIAL;
3398 buf[1] = KS_EXTRA;
3399 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003400 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003401 len = STRLEN(buf);
3402
Bram Moolenaarce658352020-07-31 23:47:12 +02003403 while (todo > 0)
3404 {
3405 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003406 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003407 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003408 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003409 fp = HI2UF(hi);
3410 if (STRNCMP(fp->uf_name, buf, len) == 0)
3411 {
3412 int changed = func_hashtab.ht_changed;
3413
3414 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003415
3416 if (fp->uf_calls > 0)
3417 {
3418 // Function is executing, don't free it but do remove
3419 // it from the hashtable.
3420 if (func_remove(fp))
3421 fp->uf_refcount--;
3422 }
3423 else
3424 {
3425 func_clear(fp, TRUE);
3426 // When clearing a function another function can be
3427 // cleared as a side effect. When that happens start
3428 // over.
3429 if (changed != func_hashtab.ht_changed)
3430 break;
3431 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003432 }
3433 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003434 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003435 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003436}
3437
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003438#if defined(EXITFREE) || defined(PROTO)
3439 void
3440free_all_functions(void)
3441{
3442 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003443 ufunc_T *fp;
3444 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003445 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003446 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003447
Bram Moolenaare38eab22019-12-05 21:50:01 +01003448 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003449 while (current_funccal != NULL)
3450 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003451 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003452 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003453 if (current_funccal == NULL && funccal_stack != NULL)
3454 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003455 }
3456
Bram Moolenaare38eab22019-12-05 21:50:01 +01003457 // First clear what the functions contain. Since this may lower the
3458 // reference count of a function, it may also free a function and change
3459 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003460 while (todo > 0)
3461 {
3462 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003463 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003464 if (!HASHITEM_EMPTY(hi))
3465 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 // clear the def function index now
3467 fp = HI2UF(hi);
3468 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003469 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470
Bram Moolenaare38eab22019-12-05 21:50:01 +01003471 // Only free functions that are not refcounted, those are
3472 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003473 if (func_name_refcount(fp->uf_name))
3474 ++skipped;
3475 else
3476 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003477 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003478 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003479 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003480 {
3481 skipped = 0;
3482 break;
3483 }
3484 }
3485 --todo;
3486 }
3487 }
3488
Bram Moolenaare38eab22019-12-05 21:50:01 +01003489 // Now actually free the functions. Need to start all over every time,
3490 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003491 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003492 while (func_hashtab.ht_used > skipped)
3493 {
3494 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003495 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003496 if (!HASHITEM_EMPTY(hi))
3497 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003498 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003499 // Only free functions that are not refcounted, those are
3500 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003501 fp = HI2UF(hi);
3502 if (func_name_refcount(fp->uf_name))
3503 ++skipped;
3504 else
3505 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003506 if (func_free(fp, FALSE) == OK)
3507 {
3508 skipped = 0;
3509 break;
3510 }
3511 // did not actually free it
3512 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003513 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003514 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003515 }
3516 if (skipped == 0)
3517 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518
3519 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003520}
3521#endif
3522
3523/*
3524 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003525 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3526 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003527 * "len" is the length of "name", or -1 for NUL terminated.
3528 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003530builtin_function(char_u *name, int len)
3531{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003532 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003533
Bram Moolenaara26b9702020-04-18 19:53:28 +02003534 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003535 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003536 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3537 {
3538 if (name[i] == AUTOLOAD_CHAR)
3539 return FALSE;
3540 if (!eval_isnamec(name[i]))
3541 {
3542 // "name.something" is not a builtin function
3543 if (name[i] == '.')
3544 return FALSE;
3545 break;
3546 }
3547 }
3548 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003549}
3550
3551 int
3552func_call(
3553 char_u *name,
3554 typval_T *args,
3555 partial_T *partial,
3556 dict_T *selfdict,
3557 typval_T *rettv)
3558{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003559 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003560 listitem_T *item;
3561 typval_T argv[MAX_FUNC_ARGS + 1];
3562 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003563 int r = 0;
3564
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003565 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003566 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003567 {
3568 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3569 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003570 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003571 break;
3572 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003573 // Make a copy of each argument. This is needed to be able to set
3574 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003575 copy_tv(&item->li_tv, &argv[argc++]);
3576 }
3577
3578 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003579 {
3580 funcexe_T funcexe;
3581
Bram Moolenaara80faa82020-04-12 19:37:17 +02003582 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003583 funcexe.fe_firstline = curwin->w_cursor.lnum;
3584 funcexe.fe_lastline = curwin->w_cursor.lnum;
3585 funcexe.fe_evaluate = TRUE;
3586 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003587 if (partial != NULL)
3588 {
3589 funcexe.fe_object = partial->pt_obj;
3590 if (funcexe.fe_object != NULL)
3591 ++funcexe.fe_object->obj_refcount;
3592 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003593 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003594 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3595 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003596
Bram Moolenaare38eab22019-12-05 21:50:01 +01003597 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003598 while (argc > 0)
3599 clear_tv(&argv[--argc]);
3600
3601 return r;
3602}
3603
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003604static int callback_depth = 0;
3605
3606 int
3607get_callback_depth(void)
3608{
3609 return callback_depth;
3610}
3611
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003612/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003613 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003614 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003615 */
3616 int
3617call_callback(
3618 callback_T *callback,
3619 int len, // length of "name" or -1 to use strlen()
3620 typval_T *rettv, // return value goes here
3621 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003622 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003623 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003624{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003625 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003626 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003627
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003628 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3629 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003630
zeertzjqfe583b12023-12-21 16:59:26 +01003631 if (callback_depth > p_mfd)
Christian Brabandt47510f32023-10-15 09:56:16 +02003632 {
3633 emsg(_(e_command_too_recursive));
3634 return FAIL;
3635 }
3636
Bram Moolenaara80faa82020-04-12 19:37:17 +02003637 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003638 funcexe.fe_evaluate = TRUE;
3639 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003640 if (callback->cb_partial != NULL)
3641 {
3642 funcexe.fe_object = callback->cb_partial->pt_obj;
3643 if (funcexe.fe_object != NULL)
3644 ++funcexe.fe_object->obj_refcount;
3645 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003646 ++callback_depth;
3647 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3648 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003649
3650 // When a :def function was called that uses :try an error would be turned
3651 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003652 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003653 {
3654 need_rethrow = FALSE;
3655 handle_did_throw();
3656 }
3657
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003658 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003659}
3660
3661/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003662 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003663 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003664 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3665 */
3666 varnumber_T
3667call_callback_retnr(
3668 callback_T *callback,
3669 int argcount, // number of "argvars"
3670 typval_T *argvars) // vars for arguments, must have "argcount"
3671 // PLUS ONE elements!
3672{
3673 typval_T rettv;
3674 varnumber_T retval;
3675
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003676 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003677 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003678
3679 retval = tv_get_number_chk(&rettv, NULL);
3680 clear_tv(&rettv);
3681 return retval;
3682}
3683
3684/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 * Give an error message for the result of a function.
3686 * Nothing if "error" is FCERR_NONE.
3687 */
3688 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003689user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690{
3691 switch (error)
3692 {
3693 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003694 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003695 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003696 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003697 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 break;
3699 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003700 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 break;
3702 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003703 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 break;
3705 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003706 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 break;
3708 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003709 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 break;
3711 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003712 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 break;
3714 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003715 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3716 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003718 case FCERR_OTHER:
3719 case FCERR_FAILED:
3720 // assume the error message was already given
3721 break;
3722 case FCERR_NONE:
3723 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 }
3725}
3726
3727/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003728 * Check the argument types "argvars[argcount]" for "name" using the
3729 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3730 * is already included in "argvars[]".
3731 * Will do nothing if "funcexe->fe_check_type" is NULL or
3732 * "funcexe->fe_evaluate" is FALSE;
3733 * Returns an FCERR_ value.
3734 */
3735 static funcerror_T
3736may_check_argument_types(
3737 funcexe_T *funcexe,
3738 typval_T *argvars,
3739 int argcount,
3740 int base_included,
3741 char_u *name)
3742{
3743 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3744 {
3745 // Check that the argument types are OK for the types of the funcref.
3746 if (check_argument_types(funcexe->fe_check_type,
3747 argvars, argcount,
3748 base_included ? NULL : funcexe->fe_basetv,
3749 name) == FAIL)
3750 return FCERR_OTHER;
3751 }
3752 return FCERR_NONE;
3753}
3754
3755/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003756 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003757 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003758 * Return FAIL when the function can't be called, OK otherwise.
3759 * Also returns OK when an error was encountered while executing the function.
3760 */
3761 int
3762call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003763 char_u *funcname, // name of the function
3764 int len, // length of "name" or -1 to use strlen()
3765 typval_T *rettv, // return value goes here
3766 int argcount_in, // number of "argvars"
3767 typval_T *argvars_in, // vars for arguments, must have "argcount"
3768 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003769 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003770{
3771 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003772 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003773 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003774 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003775 char_u fname_buf[FLEN_FIXED + 1];
3776 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003777 char_u *fname = NULL;
3778 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003779 int argcount = argcount_in;
3780 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003781 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003782 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003783 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003784 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003785 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003786 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003787 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003788 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003789
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003790 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3791 // even when call_func() returns FAIL.
3792 rettv->v_type = VAR_UNKNOWN;
3793
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003794 if (partial != NULL)
3795 fp = partial->pt_func;
3796 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003797 fp = funcexe->fe_ufunc;
3798
3799 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003800 {
3801 // Make a copy of the name, if it comes from a funcref variable it
3802 // could be changed or deleted in the called function.
3803 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3804 if (name == NULL)
3805 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003806
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003807 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3808 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003809
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003810 if (funcexe->fe_doesrange != NULL)
3811 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003812
3813 if (partial != NULL)
3814 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003815 // When the function has a partial with a dict and there is a dict
3816 // argument, use the dict argument. That is backwards compatible.
3817 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003818 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003819 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003820 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003821 {
3822 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003823 {
3824 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3825 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003826 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003827 goto theend;
3828 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003829 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003830 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003831 for (i = 0; i < argcount_in; ++i)
3832 argv[i + argv_clear] = argvars_in[i];
3833 argvars = argv;
3834 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003835
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003836 if (funcexe->fe_check_type != NULL
3837 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003838 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003839 // Now funcexe->fe_check_type is missing the added arguments,
3840 // make a copy of the type with the correction.
3841 check_type = *funcexe->fe_check_type;
3842 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003843 check_type.tt_args = check_type_args;
3844 CLEAR_FIELD(check_type_args);
3845 for (i = 0; i < check_type.tt_argcount; ++i)
3846 check_type_args[i + partial->pt_argc] =
3847 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003848 check_type.tt_argcount += partial->pt_argc;
3849 check_type.tt_min_argcount += partial->pt_argc;
3850 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003851 }
3852 }
3853
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003854 if (error == FCERR_NONE)
3855 // check the argument types if possible
3856 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3857 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003858
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003859 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003860 {
3861 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003862 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003863
Bram Moolenaar333894b2020-08-01 18:53:07 +02003864 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003865 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003866 {
3867 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003868 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003869 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003870
Bram Moolenaare38eab22019-12-05 21:50:01 +01003871 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003872 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003873 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003874
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003875 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003876 {
3877 /*
3878 * User defined function.
3879 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003880 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003881 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003882 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003883 if (fp != NULL && !is_global && in_vim9script()
3884 && func_requires_g_prefix(fp))
3885 // In Vim9 script g: is required to find a global
3886 // non-autoload function.
3887 fp = NULL;
3888 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003889
Bram Moolenaare38eab22019-12-05 21:50:01 +01003890 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003891 if (fp == NULL
3892 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003893 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003894 && !aborting())
3895 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003896 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003897 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003898 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003899 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003900 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3901 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003902 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003903 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003904 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003905 if (fp == NULL)
3906 {
3907 char_u *p = untrans_function_name(rfname);
3908
3909 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003910 // Don't do this if the name starts with "s:".
3911 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003912 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003913 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003914
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003915 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003916 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003917 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003918 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003919 int need_arg_check = FALSE;
3920 if (funcexe->fe_check_type == NULL)
3921 {
3922 funcexe->fe_check_type = fp->uf_func_type;
3923 need_arg_check = TRUE;
3924 }
3925
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003926 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003927 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003928 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003929 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003930 argv_clear, fp);
3931 need_arg_check = TRUE;
3932 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003933
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003934 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003935 {
3936 // Method call: base->Method()
3937 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003938 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003939 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003940 argvars = argv;
3941 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003942 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003943 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003944
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003945 // Check the argument types now that the function type and all
3946 // argument values are known, if not done above.
3947 if (need_arg_check)
3948 error = may_check_argument_types(funcexe, argvars, argcount,
3949 TRUE, (name != NULL) ? name : funcname);
3950 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3951 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003953 }
3954 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003955 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003956 {
3957 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003958 * expr->method(): Find the method name in the table, call its
3959 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003960 */
3961 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003962 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003963 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003964 else
3965 {
3966 /*
3967 * Find the function name in the table, call its implementation.
3968 */
3969 error = call_internal_func(fname, argcount, argvars, rettv);
3970 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003971
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003972 /*
3973 * The function call (or "FuncUndefined" autocommand sequence) might
3974 * have been aborted by an error, an interrupt, or an explicitly thrown
3975 * exception that has not been caught so far. This situation can be
3976 * tested for by calling aborting(). For an error in an internal
3977 * function or for the "E132" error in call_user_func(), however, the
3978 * throw point at which the "force_abort" flag (temporarily reset by
3979 * emsg()) is normally updated has not been reached yet. We need to
3980 * update that flag first to make aborting() reliable.
3981 */
3982 update_force_abort();
3983 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003984 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003985 ret = OK;
3986
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003987theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003988 /*
3989 * Report an error unless the argument evaluation or function call has been
3990 * cancelled due to an aborting error, an interrupt, or an exception.
3991 */
3992 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003993 user_func_error(error, (name != NULL) ? name : funcname,
3994 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003995
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003996 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003997 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003998 clear_tv(&argv[--argv_clear + argv_base]);
3999
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004000 vim_free(tofree);
4001 vim_free(name);
4002
4003 return ret;
4004}
4005
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004006/*
4007 * Call a function without arguments, partial or dict.
4008 * This is like call_func() when the call is only "FuncName()".
4009 * To be used by "expr" options.
4010 * Returns NOTDONE when the function could not be found.
4011 */
4012 int
4013call_simple_func(
4014 char_u *funcname, // name of the function
4015 int len, // length of "name" or -1 to use strlen()
4016 typval_T *rettv) // return value goes here
4017{
4018 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00004019 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004020 char_u fname_buf[FLEN_FIXED + 1];
4021 char_u *tofree = NULL;
4022 char_u *name;
4023 char_u *fname;
4024 char_u *rfname;
4025 int is_global = FALSE;
4026 ufunc_T *fp;
4027
4028 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4029 rettv->vval.v_number = 0;
4030
4031 // Make a copy of the name, an option can be changed in the function.
4032 name = vim_strnsave(funcname, len);
4033 if (name == NULL)
4034 return ret;
4035
4036 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4037
4038 // Skip "g:" before a function name.
4039 if (fname[0] == 'g' && fname[1] == ':')
4040 {
4041 is_global = TRUE;
4042 rfname = fname + 2;
4043 }
4044 else
4045 rfname = fname;
4046 fp = find_func(rfname, is_global);
4047 if (fp != NULL && !is_global && in_vim9script()
4048 && func_requires_g_prefix(fp))
4049 // In Vim9 script g: is required to find a global non-autoload
4050 // function.
4051 fp = NULL;
4052 if (fp == NULL)
4053 ret = NOTDONE;
4054 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4055 error = FCERR_DELETED;
4056 else if (fp != NULL)
4057 {
4058 typval_T argvars[1];
4059 funcexe_T funcexe;
4060
4061 argvars[0].v_type = VAR_UNKNOWN;
4062 CLEAR_FIELD(funcexe);
4063 funcexe.fe_evaluate = TRUE;
4064
4065 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4066 if (error == FCERR_NONE)
4067 ret = OK;
4068 }
4069
4070 user_func_error(error, name, FALSE);
4071 vim_free(tofree);
4072 vim_free(name);
4073
4074 return ret;
4075}
4076
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004077 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078printable_func_name(ufunc_T *fp)
4079{
4080 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4081}
4082
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004083/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004084 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4085 * TRUE. Otherwise return FALSE.
4086 */
4087 static int
4088function_list_modified(int prev_ht_changed)
4089{
4090 if (prev_ht_changed != func_hashtab.ht_changed)
4091 {
4092 emsg(_(e_function_list_was_modified));
4093 return TRUE;
4094 }
4095 return FALSE;
4096}
4097
4098/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004099 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004100 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004101 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004102list_func_head(ufunc_T *fp, int indent)
4103{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004104 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004105 int j;
4106
4107 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004108
4109 // a timer at the more prompt may have deleted the function
4110 if (function_list_modified(prev_ht_changed))
4111 return FAIL;
4112
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004113 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004114 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004115 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004116 msg_puts("def ");
4117 else
4118 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004120 msg_putchar('(');
4121 for (j = 0; j < fp->uf_args.ga_len; ++j)
4122 {
4123 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004124 msg_puts(", ");
4125 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 if (fp->uf_arg_types != NULL)
4127 {
4128 char *tofree;
4129
4130 msg_puts(": ");
4131 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4132 vim_free(tofree);
4133 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004134 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4135 {
4136 msg_puts(" = ");
4137 msg_puts(((char **)(fp->uf_def_args.ga_data))
4138 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4139 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004140 }
4141 if (fp->uf_varargs)
4142 {
4143 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004144 msg_puts(", ");
4145 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004146 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 if (fp->uf_va_name != NULL)
4148 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004149 if (!fp->uf_varargs)
4150 {
4151 if (j)
4152 msg_puts(", ");
4153 msg_puts("...");
4154 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004156 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 {
4158 char *tofree;
4159
4160 msg_puts(": ");
4161 msg_puts(type_name(fp->uf_va_type, &tofree));
4162 vim_free(tofree);
4163 }
4164 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004165 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004166
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004167 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004168 {
4169 if (fp->uf_ret_type != &t_void)
4170 {
4171 char *tofree;
4172
4173 msg_puts(": ");
4174 msg_puts(type_name(fp->uf_ret_type, &tofree));
4175 vim_free(tofree);
4176 }
4177 }
4178 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004179 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004180 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004181 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004182 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004183 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004184 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004185 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004186 msg_clr_eos();
4187 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004188 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004189
4190 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004191}
4192
4193/*
4194 * Get a function name, translating "<SID>" and "<SNR>".
4195 * Also handles a Funcref in a List or Dictionary.
4196 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004197 * Set "*is_global" to TRUE when the function must be global, unless
4198 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004199 * flags:
4200 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004201 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004202 * TFN_QUIET: be quiet
4203 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004204 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004205 * Advances "pp" to just after the function name (if no error).
4206 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004207 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004208trans_function_name(
4209 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004210 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004211 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004212 int flags)
4213{
4214 return trans_function_name_ext(pp, is_global, skip, flags,
4215 NULL, NULL, NULL, NULL);
4216}
4217
4218/*
4219 * trans_function_name() with extra arguments.
4220 * "fdp", "partial", "type" and "ufunc" can be NULL.
4221 */
4222 char_u *
4223trans_function_name_ext(
4224 char_u **pp,
4225 int *is_global,
4226 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004227 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004228 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004229 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004230 type_T **type, // return: type of funcref
4231 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004232{
4233 char_u *name = NULL;
4234 char_u *start;
4235 char_u *end;
4236 int lead;
4237 char_u sid_buf[20];
4238 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004240 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004241 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004242 int vim9script = in_vim9script();
4243 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004244
4245 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004246 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004247 start = *pp;
4248
Bram Moolenaare38eab22019-12-05 21:50:01 +01004249 // Check for hard coded <SNR>: already translated function ID (from a user
4250 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004251 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4252 && (*pp)[2] == (int)KE_SNR)
4253 {
4254 *pp += 3;
4255 len = get_id_len(pp) + 3;
4256 return vim_strnsave(start, len);
4257 }
4258
Bram Moolenaare38eab22019-12-05 21:50:01 +01004259 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4260 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004261 lead = eval_fname_script(start);
4262 if (lead > 2)
4263 start += lead;
4264
Bram Moolenaare38eab22019-12-05 21:50:01 +01004265 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004266 end = get_lval(start, NULL, &lv, FALSE, skip,
4267 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004268 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004269 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004270 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004271 {
4272 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004273 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004274 goto theend;
4275 }
4276 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4277 {
4278 /*
4279 * Report an invalid expression in braces, unless the expression
4280 * evaluation has been cancelled due to an aborting error, an
4281 * interrupt, or an exception.
4282 */
4283 if (!aborting())
4284 {
4285 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004286 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004287 }
4288 else
4289 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4290 goto theend;
4291 }
4292
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004293 if (lv.ll_ufunc != NULL)
4294 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004295 if (ufunc != NULL)
4296 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004297 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004298 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004299 goto theend;
4300 }
4301
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004302 if (lv.ll_tv != NULL)
4303 {
4304 if (fdp != NULL)
4305 {
4306 fdp->fd_dict = lv.ll_dict;
4307 fdp->fd_newkey = lv.ll_newkey;
4308 lv.ll_newkey = NULL;
4309 fdp->fd_di = lv.ll_di;
4310 }
4311 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4312 {
4313 name = vim_strsave(lv.ll_tv->vval.v_string);
4314 *pp = end;
4315 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004316 else if (lv.ll_tv->v_type == VAR_CLASS
4317 && lv.ll_tv->vval.v_class != NULL)
4318 {
4319 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4320 *pp = end;
4321 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004322 else if (lv.ll_tv->v_type == VAR_PARTIAL
4323 && lv.ll_tv->vval.v_partial != NULL)
4324 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004325 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004326 *pp = end;
4327 if (partial != NULL)
4328 *partial = lv.ll_tv->vval.v_partial;
4329 }
4330 else
4331 {
4332 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4333 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004334 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004335 else
4336 *pp = end;
4337 name = NULL;
4338 }
4339 goto theend;
4340 }
4341
4342 if (lv.ll_name == NULL)
4343 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004344 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004345 *pp = end;
4346 goto theend;
4347 }
4348
Bram Moolenaare38eab22019-12-05 21:50:01 +01004349 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004350 if (lv.ll_exp_name != NULL)
4351 {
4352 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004353 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004354 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004355 if (name == lv.ll_exp_name)
4356 name = NULL;
4357 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004358 else if (lv.ll_sid > 0)
4359 {
4360 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4361 int cc = *lv.ll_name_end;
4362
4363 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4364 *lv.ll_name_end = NUL;
4365 if (si->sn_autoload_prefix != NULL)
4366 {
4367 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4368 }
4369 else
4370 {
4371 sid_buf[0] = K_SPECIAL;
4372 sid_buf[1] = KS_EXTRA;
4373 sid_buf[2] = (int)KE_SNR;
4374 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004375 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004376 name = concat_str(sid_buf, lv.ll_name);
4377 }
4378 *lv.ll_name_end = cc;
4379 *pp = end;
4380 goto theend;
4381 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004382 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004383 {
4384 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004385 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004386 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004387 if (name == *pp)
4388 name = NULL;
4389 }
4390 if (name != NULL)
4391 {
4392 name = vim_strsave(name);
4393 *pp = end;
4394 if (STRNCMP(name, "<SNR>", 5) == 0)
4395 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004396 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004397 name[0] = K_SPECIAL;
4398 name[1] = KS_EXTRA;
4399 name[2] = (int)KE_SNR;
4400 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4401 }
4402 goto theend;
4403 }
4404
4405 if (lv.ll_exp_name != NULL)
4406 {
4407 len = (int)STRLEN(lv.ll_exp_name);
4408 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4409 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4410 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004411 // When there was "s:" already or the name expanded to get a
4412 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004413 lv.ll_name += 2;
4414 len -= 2;
4415 lead = 2;
4416 }
4417 }
4418 else
4419 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004420 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004421 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004422 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004423 if (lv.ll_name[0] == 'g')
4424 {
4425 if (is_global != NULL)
4426 {
4427 *is_global = TRUE;
4428 }
4429 else
4430 {
4431 // dropping "g:" without setting "is_global" won't work in
4432 // Vim9script, put it back later
4433 prefix_g = TRUE;
4434 extra = 2;
4435 }
4436 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004437 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004438 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004439 len = (int)(end - lv.ll_name);
4440 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004441 if (len <= 0)
4442 {
4443 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004444 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004445 goto theend;
4446 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004447
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004448 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004449 // starts with a lower case character: dict.func(). Or when in a class.
4450 vim9_local = ASCII_ISUPPER(*start) && vim9script
4451 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004453 /*
4454 * Copy the function name to allocated memory.
4455 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4456 * Accept <SNR>123_name() outside a script.
4457 */
4458 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004459 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004460 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004461 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004462 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004463 {
Kota Kato948a3892022-08-16 16:09:59 +01004464 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4465 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004466 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004467 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004468 goto theend;
4469 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004471 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004472 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004474 || eval_fname_sid(*pp))
4475 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004477 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004478 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004479 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004480 goto theend;
4481 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004482 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004483 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 extra = 3 + (int)STRLEN(sid_buf);
4485 else
4486 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004487 }
4488 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004489 // The function name must start with an upper case letter (unless it is a
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004490 // Vim9 class new() function or a Vim9 class private method or one of the
4491 // supported Vim9 object builtin functions)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004492 else if (!(flags & TFN_INT)
4493 && (builtin_function(lv.ll_name, len)
4494 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004495 && !((flags & TFN_IN_CLASS)
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004496 && (is_valid_builtin_obj_methodname(lv.ll_name)
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004497 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004498 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004499 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004500 : e_function_name_must_start_with_capital_or_s_str),
4501 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004502 goto theend;
4503 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004504 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004505 {
4506 char_u *cp = vim_strchr(lv.ll_name, ':');
4507
4508 if (cp != NULL && cp < end)
4509 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004510 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004511 goto theend;
4512 }
4513 }
4514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004516 if (name != NULL)
4517 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004518 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004519 {
4520 name[0] = K_SPECIAL;
4521 name[1] = KS_EXTRA;
4522 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004523 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004524 STRCPY(name + 3, sid_buf);
4525 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004526 else if (prefix_g)
4527 {
4528 name[0] = 'g';
4529 name[1] = ':';
4530 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4532 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004533 }
4534 *pp = end;
4535
4536theend:
4537 clear_lval(&lv);
4538 return name;
4539}
4540
4541/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004542 * Assuming "name" is the result of trans_function_name() and it was prefixed
4543 * to use the script-local name, return the unmodified name (points into
4544 * "name"). Otherwise return NULL.
4545 * This can be used to first search for a script-local function and fall back
4546 * to the global function if not found.
4547 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004548 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004549untrans_function_name(char_u *name)
4550{
4551 char_u *p;
4552
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004553 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004554 {
4555 p = vim_strchr(name, '_');
4556 if (p != NULL)
4557 return p + 1;
4558 }
4559 return NULL;
4560}
4561
4562/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004563 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4564 * current script ID and returns the expanded function name. The caller should
4565 * free the returned name. If not called from a script context or the function
4566 * name doesn't start with these prefixes, then returns NULL.
4567 * This doesn't check whether the script-local function exists or not.
4568 */
4569 char_u *
4570get_scriptlocal_funcname(char_u *funcname)
4571{
4572 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004573 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004574 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004575 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004576
4577 if (funcname == NULL)
4578 return NULL;
4579
4580 if (STRNCMP(funcname, "s:", 2) != 0
4581 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004582 {
4583 ufunc_T *ufunc;
4584
4585 // The function name does not have a script-local prefix. Try finding
4586 // it when in a Vim9 script and there is no "g:" prefix.
4587 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4588 return NULL;
4589 ufunc = find_func(funcname, FALSE);
4590 if (ufunc == NULL || func_is_global(ufunc)
4591 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4592 return NULL;
4593 ++p;
4594 off = 0;
4595 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004596 else
4597 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004598
4599 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4600 {
4601 emsg(_(e_using_sid_not_in_script_context));
4602 return NULL;
4603 }
4604 // Expand s: prefix into <SNR>nr_<name>
4605 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4606 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004607 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004608 if (newname == NULL)
4609 return NULL;
4610 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004611 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004612
4613 return newname;
4614}
4615
4616/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004617 * Return script-local "fname" with the 3-byte sequence replaced by
4618 * printable <SNR> in allocated memory.
4619 */
4620 char_u *
4621alloc_printable_func_name(char_u *fname)
4622{
4623 char_u *n = alloc(STRLEN(fname + 3) + 6);
4624
4625 if (n != NULL)
4626 {
4627 STRCPY(n, "<SNR>");
4628 STRCPY(n + 5, fname + 3);
4629 }
4630 return n;
4631}
4632
4633/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004634 * Call trans_function_name(), except that a lambda is returned as-is.
4635 * Returns the name in allocated memory.
4636 */
4637 char_u *
4638save_function_name(
4639 char_u **name,
4640 int *is_global,
4641 int skip,
4642 int flags,
4643 funcdict_T *fudi)
4644{
4645 char_u *p = *name;
4646 char_u *saved;
4647
4648 if (STRNCMP(p, "<lambda>", 8) == 0)
4649 {
4650 p += 8;
4651 (void)getdigits(&p);
4652 saved = vim_strnsave(*name, p - *name);
4653 if (fudi != NULL)
4654 CLEAR_POINTER(fudi);
4655 }
4656 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004657 saved = trans_function_name_ext(&p, is_global, skip,
4658 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004659 *name = p;
4660 return saved;
4661}
4662
4663/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004664 * List functions. When "regmatch" is NULL all of then.
4665 * Otherwise functions matching "regmatch".
4666 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004667 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004668list_functions(regmatch_T *regmatch)
4669{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004670 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004671 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004672 hashitem_T *hi;
4673
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004674 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004675 {
4676 if (!HASHITEM_EMPTY(hi))
4677 {
4678 ufunc_T *fp = HI2UF(hi);
4679
4680 --todo;
4681 if ((fp->uf_flags & FC_DEAD) == 0
4682 && (regmatch == NULL
4683 ? !message_filtered(fp->uf_name)
4684 && !func_name_refcount(fp->uf_name)
Keith Thompson184f71c2024-01-04 21:19:04 +01004685 : !SAFE_isdigit(*fp->uf_name)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004686 && vim_regexec(regmatch, fp->uf_name, 0)))
4687 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004688 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004689 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004690 if (function_list_modified(prev_ht_changed))
4691 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004692 }
4693 }
4694 }
4695}
4696
4697/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004698 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004699 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4700 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004701 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004702 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4703 * With CF_INTERFACE the function is define inside an interface, only the
4704 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004705 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004706 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004707 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004708define_function(
4709 exarg_T *eap,
4710 char_u *name_arg,
4711 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004712 int class_flags,
4713 ocmember_T *obj_members,
4714 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004715{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004716 int j;
4717 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004718 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004719 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004720 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004721 char_u *p;
4722 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004723 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004724 char_u *line_arg = NULL;
4725 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004727 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004728 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004729 garray_T newlines;
4730 int varargs = FALSE;
4731 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004733 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004734 int fp_allocated = FALSE;
4735 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004736 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004737 dictitem_T *v;
4738 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004739 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004740 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004741 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004742 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004743 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004744 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004745
4746 /*
4747 * ":function" without argument: list functions.
4748 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004749 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004750 {
4751 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004752 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004753 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004754 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004755 }
4756
4757 /*
4758 * ":function /pat": list functions matching pattern.
4759 */
4760 if (*eap->arg == '/')
4761 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004762 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004763 if (!eap->skip)
4764 {
4765 regmatch_T regmatch;
4766
4767 c = *p;
4768 *p = NUL;
4769 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4770 *p = c;
4771 if (regmatch.regprog != NULL)
4772 {
4773 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004774 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004775 vim_regfree(regmatch.regprog);
4776 }
4777 }
4778 if (*p == '/')
4779 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004780 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004781 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004782 }
4783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784 ga_init(&newargs);
4785 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004786 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787 ga_init(&default_args);
4788
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004789 /*
4790 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004791 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004792 * "name" == func, "fudi.fd_dict" == NULL
4793 * dict.func new dictionary entry
4794 * "name" == NULL, "fudi.fd_dict" set,
4795 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4796 * dict.func existing dict entry with a Funcref
4797 * "name" == func, "fudi.fd_dict" set,
4798 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4799 * dict.func existing dict entry that's not a Funcref
4800 * "name" == NULL, "fudi.fd_dict" set,
4801 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4802 * s:func script-local function name
4803 * g:func global function name, same as "func"
4804 */
4805 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004806 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004807 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004808 // nested function, argument is (args).
4809 paren = TRUE;
4810 CLEAR_FIELD(fudi);
4811 }
4812 else
4813 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004814 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004815 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004816 if (p[0] == 's' && p[1] == ':')
4817 {
4818 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4819 return NULL;
4820 }
4821 p = to_name_end(p, TRUE);
4822 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4823 {
4824 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4825 eap->arg);
4826 return NULL;
4827 }
4828 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004829 }
4830
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004831 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004832 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004833 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004834 paren = (vim_strchr(p, '(') != NULL);
4835 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004836 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004837 /*
4838 * Return on an invalid expression in braces, unless the expression
4839 * evaluation has been cancelled due to an aborting error, an
4840 * interrupt, or an exception.
4841 */
4842 if (!aborting())
4843 {
4844 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004845 semsg(_(e_key_not_present_in_dictionary_str),
4846 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004847 vim_free(fudi.fd_newkey);
4848 return NULL;
4849 }
4850 else
4851 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004852 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004853
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004854 // For "export def FuncName()" in an autoload script the function name
4855 // is stored with the legacy autoload name "dir#script#FuncName" so
4856 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004857 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004858 {
4859 char_u *prefixed = may_prefix_autoload(name);
4860
4861 if (prefixed != NULL && prefixed != name)
4862 {
4863 vim_free(name);
4864 name = prefixed;
4865 }
4866 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004867 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004868 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004869 {
4870 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4871 goto ret_free;
4872 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004873 }
4874
Bram Moolenaare38eab22019-12-05 21:50:01 +01004875 // An error in a function call during evaluation of an expression in magic
4876 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004877 saved_did_emsg = did_emsg;
4878 did_emsg = FALSE;
4879
4880 /*
4881 * ":function func" with only function name: list function.
4882 */
4883 if (!paren)
4884 {
4885 if (!ends_excmd(*skipwhite(p)))
4886 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004887 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004888 goto ret_free;
4889 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004890 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004891 if (eap->nextcmd != NULL)
4892 *p = NUL;
4893 if (!eap->skip && !got_int)
4894 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004895 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004896 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4897 {
4898 char_u *up = untrans_function_name(name);
4899
4900 // With Vim9 script the name was made script-local, if not
4901 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004902 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004903 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004904 }
4905
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004906 if (fp != NULL)
4907 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004908 // Check no function was added or removed from a timer, e.g. at
4909 // the more prompt. "fp" may then be invalid.
4910 int prev_ht_changed = func_hashtab.ht_changed;
4911
4912 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004913 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004914 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4915 {
4916 if (FUNCLINE(fp, j) == NULL)
4917 continue;
4918 msg_putchar('\n');
4919 msg_outnum((long)(j + 1));
4920 if (j < 9)
4921 msg_putchar(' ');
4922 if (j < 99)
4923 msg_putchar(' ');
4924 if (function_list_modified(prev_ht_changed))
4925 break;
4926 msg_prt_line(FUNCLINE(fp, j), FALSE);
4927 out_flush(); // show a line at a time
4928 ui_breakcheck();
4929 }
4930 if (!got_int)
4931 {
4932 msg_putchar('\n');
4933 if (!function_list_modified(prev_ht_changed))
4934 {
4935 if (fp->uf_def_status != UF_NOT_COMPILED)
4936 msg_puts(" enddef");
4937 else
4938 msg_puts(" endfunction");
4939 }
4940 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004941 }
4942 }
4943 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004944 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004945 }
4946 goto ret_free;
4947 }
4948
4949 /*
4950 * ":function name(arg1, arg2)" Define function.
4951 */
4952 p = skipwhite(p);
4953 if (*p != '(')
4954 {
4955 if (!eap->skip)
4956 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004957 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004958 goto ret_free;
4959 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004960 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004961 if (vim_strchr(p, '(') != NULL)
4962 p = vim_strchr(p, '(');
4963 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004964
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004965 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4966 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004967 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004968 goto ret_free;
4969 }
4970
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004971 // In Vim9 script only global functions can be redefined.
4972 if (vim9script && eap->forceit && !is_global)
4973 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004974 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004975 goto ret_free;
4976 }
4977
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004978 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004979
Bram Moolenaar04b12692020-05-04 23:24:44 +02004980 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004981 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004982 // Check the name of the function. Unless it's a dictionary function
4983 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004984 if (name != NULL)
4985 arg = name;
4986 else
4987 arg = fudi.fd_newkey;
4988 if (arg != NULL && (fudi.fd_di == NULL
4989 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4990 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4991 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004992 char_u *name_base = arg;
4993 int i;
4994
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004995 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004996 {
4997 name_base = vim_strchr(arg, '_');
4998 if (name_base == NULL)
4999 name_base = arg + 3;
5000 else
5001 ++name_base;
5002 }
5003 for (i = 0; name_base[i] != NUL && (i == 0
5004 ? eval_isnamec1(name_base[i])
5005 : eval_isnamec(name_base[i])); ++i)
5006 ;
5007 if (name_base[i] != NUL)
zeertzjq6a04bf52024-03-16 09:39:06 +01005008 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005009 emsg_funcname(e_invalid_argument_str, arg);
zeertzjq6a04bf52024-03-16 09:39:06 +01005010 goto ret_free;
5011 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00005012
5013 // In Vim9 script a function cannot have the same name as a
5014 // variable.
5015 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005016 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
5017 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00005018 + EVAL_VAR_NO_FUNC) == OK)
5019 {
5020 semsg(_(e_redefining_script_item_str), name_base);
5021 goto ret_free;
5022 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005023 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005024 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005025 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005026 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005027 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00005028 goto ret_free;
5029 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005030 }
5031
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005032 // This may get more lines and make the pointers into the first line
5033 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005034 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005036 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005037 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005038 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005039 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005040 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005041 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005043 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005044 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005046 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005047 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005048 if (*p != ':')
5049 {
5050 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5051 p = skipwhite(p);
5052 }
5053 else if (!IS_WHITE_OR_NUL(p[1]))
5054 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005055 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005056 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005057 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005058 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005059 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005060 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005062 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005063 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005064 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005065 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005066 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005067 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005068 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005069 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005071 else
5072 // find extra arguments "range", "dict", "abort" and "closure"
5073 for (;;)
5074 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005075 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 p = skipwhite(p);
5077 if (STRNCMP(p, "range", 5) == 0)
5078 {
5079 flags |= FC_RANGE;
5080 p += 5;
5081 }
5082 else if (STRNCMP(p, "dict", 4) == 0)
5083 {
5084 flags |= FC_DICT;
5085 p += 4;
5086 }
5087 else if (STRNCMP(p, "abort", 5) == 0)
5088 {
5089 flags |= FC_ABORT;
5090 p += 5;
5091 }
5092 else if (STRNCMP(p, "closure", 7) == 0)
5093 {
5094 flags |= FC_CLOSURE;
5095 p += 7;
5096 if (current_funccal == NULL)
5097 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005098 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005099 name == NULL ? (char_u *)"" : name);
5100 goto erret;
5101 }
5102 }
5103 else
5104 break;
5105 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005106
Bram Moolenaare38eab22019-12-05 21:50:01 +01005107 // When there is a line break use what follows for the function body.
5108 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005109 if (*p == '\n')
5110 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005111 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005112 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5113 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005114 && !(VIM_ISWHITE(*whitep) && *p == '#'
5115 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005116 && !eap->skip
5117 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005118 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005119
5120 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5122 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005123 */
5124 if (KeyTyped)
5125 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005126 // Check if the function already exists, don't let the user type the
5127 // whole function before telling him it doesn't work! For a script we
5128 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005129 if (!eap->skip && !eap->forceit)
5130 {
5131 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005132 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005133 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005134 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005135 }
5136
5137 if (!eap->skip && did_emsg)
5138 goto erret;
5139
Bram Moolenaare38eab22019-12-05 21:50:01 +01005140 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005141 cmdline_row = msg_row;
5142 }
5143
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005144 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005145 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005146
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005147 // Do not define the function when getting the body fails and when
5148 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005149 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005150 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005151 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5152 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005153 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005154 goto erret;
5155
5156 /*
5157 * If there are no errors, add the function
5158 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005159 if (fudi.fd_dict != NULL)
5160 {
5161 char numbuf[20];
5162
5163 fp = NULL;
5164 if (fudi.fd_newkey == NULL && !eap->forceit)
5165 {
5166 emsg(_(e_dictionary_entry_already_exists));
5167 goto erret;
5168 }
5169 if (fudi.fd_di == NULL)
5170 {
5171 // Can't add a function to a locked dictionary
5172 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5173 goto erret;
5174 }
5175 // Can't change an existing function if it is locked
5176 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5177 goto erret;
5178
5179 // Give the function a sequential number. Can only be used with a
5180 // Funcref!
5181 vim_free(name);
5182 sprintf(numbuf, "%d", ++func_nr);
5183 name = vim_strsave((char_u *)numbuf);
5184 if (name == NULL)
5185 goto erret;
5186 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005187 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005188 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005189 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005190 char_u *find_name = name;
5191 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005192 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005194 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005195 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005196 var_conflict = TRUE;
5197
5198 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5199 {
5200 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5201
5202 if (si->sn_autoload_prefix != NULL)
5203 {
5204 if (is_export)
5205 {
5206 find_name = name + STRLEN(si->sn_autoload_prefix);
5207 v = find_var(find_name, &ht, TRUE);
5208 if (v != NULL)
5209 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005210 // Only check if the function already exists in the script,
5211 // global functions can be shadowed.
5212 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005213 }
5214 else
5215 {
5216 char_u *prefixed = may_prefix_autoload(name);
5217
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005218 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005219 {
5220 v = find_var(prefixed, &ht, TRUE);
5221 if (v != NULL)
5222 var_conflict = TRUE;
5223 vim_free(prefixed);
5224 }
5225 }
5226 }
5227 }
5228 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005229 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005230 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005231 goto erret;
5232 }
5233
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005234 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005235 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005236 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005237 char_u *uname = untrans_function_name(name);
5238
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005239 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005240 }
5241
5242 if (fp != NULL || import != NULL)
5243 {
5244 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005245
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005246 // Function can be replaced with "function!" and when sourcing the
5247 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005248 // A name that is used by an import can not be overruled.
5249 if (import != NULL
5250 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005251 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005252 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005253 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005254 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005255 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005256 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005257 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005258 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005259 goto erret;
5260 }
5261 if (fp->uf_calls > 0)
5262 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005263 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005264 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005265 goto erret;
5266 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005267 if (fp->uf_refcount > 1)
5268 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005269 // This function is referenced somewhere, don't redefine it but
5270 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005271 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005272 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005273 fp = NULL;
5274 overwrite = TRUE;
5275 }
5276 else
5277 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005278 char_u *exp_name = fp->uf_name_exp;
5279
5280 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005281 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005282 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005283 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005284 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005285 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005286#ifdef FEAT_PROFILE
5287 fp->uf_profiling = FALSE;
5288 fp->uf_prof_initialized = FALSE;
5289#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005290 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005291 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005292 }
5293 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005294
5295 if (fp == NULL)
5296 {
5297 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5298 {
5299 int slen, plen;
5300 char_u *scriptname;
5301
Bram Moolenaare38eab22019-12-05 21:50:01 +01005302 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005303 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005304 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005305 {
5306 scriptname = autoload_name(name);
5307 if (scriptname != NULL)
5308 {
5309 p = vim_strchr(scriptname, '/');
5310 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005311 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005312 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005313 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005314 j = OK;
5315 vim_free(scriptname);
5316 }
5317 }
5318 if (j == FAIL)
5319 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005320 linenr_T save_lnum = SOURCING_LNUM;
5321
5322 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005323 semsg(_(e_function_name_does_not_match_script_file_name_str),
5324 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005325 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005326 goto erret;
5327 }
5328 }
5329
Bram Moolenaare01e5212023-01-08 20:31:18 +00005330 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005331 if (fp == NULL)
5332 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005333 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005334
5335 if (fudi.fd_dict != NULL)
5336 {
5337 if (fudi.fd_di == NULL)
5338 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005339 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005340 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5341 if (fudi.fd_di == NULL)
5342 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005343 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005344 goto erret;
5345 }
5346 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5347 {
5348 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005349 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005350 goto erret;
5351 }
5352 }
5353 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005354 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005355 clear_tv(&fudi.fd_di->di_tv);
5356 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005357 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005358
Bram Moolenaare38eab22019-12-05 21:50:01 +01005359 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005360 flags |= FC_DICT;
5361 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005362 }
5363 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005364 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005365 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005366 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005367
5368 if (eap->cmdidx == CMD_def)
5369 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005370 int lnum_save = SOURCING_LNUM;
5371 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005372
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005373 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005374
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005375 // error messages are for the first function line
5376 SOURCING_LNUM = sourcing_lnum_top;
5377
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005378 // The function may use script variables from the context.
5379 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005380
h-eastb895b0f2023-09-24 15:46:31 +02005381 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5382 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005383 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005384 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005385 free_fp = fp_allocated;
5386 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005387 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005388 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389
5390 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005391 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005392 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005393 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005394 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005395 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005397 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005398 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005399 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005400 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005401
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005402 if (fp_allocated)
5403 {
5404 // insert the new function in the function list
5405 set_ufunc_name(fp, name);
5406 if (overwrite)
5407 {
5408 hi = hash_find(&func_hashtab, name);
5409 hi->hi_key = UF2HIKEY(fp);
5410 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005411 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005412 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005413 {
5414 free_fp = TRUE;
5415 goto erret;
5416 }
5417 fp->uf_refcount = 1;
5418 }
5419
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005420 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005421 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005422 if ((flags & FC_CLOSURE) != 0)
5423 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005424 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005425 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005426 }
5427 else
5428 fp->uf_scoped = NULL;
5429
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005430#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005431 if (prof_def_func())
5432 func_do_profile(fp);
5433#endif
5434 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005435 if (sandbox)
5436 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005437 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005438 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005439 fp->uf_flags = flags;
5440 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005441 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005442 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005443 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005444 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005445 if (is_export)
5446 {
5447 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005448 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449 is_export = FALSE;
5450 }
5451
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005452 if (eap->cmdidx == CMD_def)
5453 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005454 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5455 // :func does not use Vim9 script syntax, even in a Vim9 script file
5456 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005457
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005458 goto ret_free;
5459
5460erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005461 if (fp != NULL)
5462 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005463 // these were set to "newargs" and "default_args", which are cleared
5464 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005465 ga_init(&fp->uf_args);
5466 ga_init(&fp->uf_def_args);
5467 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005468errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005469 ga_clear_strings(&newargs);
5470 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005471 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005472 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005473 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005474 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005475 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01005476 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005477 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005478 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005479 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005480ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005481 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005482 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005483 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005484 if (name != name_arg)
5485 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005486 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005487 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005488
5489 return fp;
5490}
5491
5492/*
5493 * ":function"
5494 */
5495 void
5496ex_function(exarg_T *eap)
5497{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005498 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005499
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005500 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005501 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005502 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005503}
5504
5505/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005506 * Find a function by name, including "<lambda>123".
5507 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005508 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005509 * Return NULL if not found.
5510 */
5511 ufunc_T *
5512find_func_by_name(char_u *name, compiletype_T *compile_type)
5513{
5514 char_u *arg = name;
5515 char_u *fname;
5516 ufunc_T *ufunc;
5517 int is_global = FALSE;
5518
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005519 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5520 {
5521 *compile_type = CT_PROFILE;
5522 arg = skipwhite(arg + 7);
5523 }
5524 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5525 {
5526 *compile_type = CT_DEBUG;
5527 arg = skipwhite(arg + 5);
5528 }
5529
5530 if (STRNCMP(arg, "<lambda>", 8) == 0)
5531 {
5532 arg += 8;
5533 (void)getdigits(&arg);
5534 fname = vim_strnsave(name, arg - name);
5535 }
5536 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005537 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005538 // First try finding a method in a class, trans_function_name() will
5539 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005540 ufunc = find_class_func(&arg);
5541 if (ufunc != NULL)
5542 return ufunc;
5543
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005544 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005545 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005546 NULL, NULL, NULL, &ufunc);
5547 if (ufunc != NULL)
5548 {
5549 vim_free(fname);
5550 return ufunc;
5551 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005552 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005553 if (fname == NULL)
5554 {
5555 semsg(_(e_invalid_argument_str), name);
5556 return NULL;
5557 }
5558 if (!ends_excmd2(name, arg))
5559 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005560 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005561 emsg(ex_errmsg(e_trailing_characters_str, arg));
5562 return NULL;
5563 }
5564
5565 ufunc = find_func(fname, is_global);
5566 if (ufunc == NULL)
5567 {
5568 char_u *p = untrans_function_name(fname);
5569
5570 if (p != NULL)
5571 // Try again without making it script-local.
5572 ufunc = find_func(p, FALSE);
5573 }
5574 vim_free(fname);
5575 if (ufunc == NULL)
5576 semsg(_(e_cannot_find_function_str), name);
5577 return ufunc;
5578}
5579
5580/*
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005581 * Compile the :def function "ufunc". If "cl" is not NULL, then compile the
5582 * class or object method "ufunc" in "cl".
5583 */
5584 void
5585defcompile_function(ufunc_T *ufunc, class_T *cl)
5586{
5587 compiletype_T compile_type = CT_NONE;
5588
5589 if (func_needs_compiling(ufunc, compile_type))
5590 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5591 else
5592 smsg(_("Function %s%s%s does not need compiling"),
5593 cl != NULL ? cl->class_name : (char_u *)"",
5594 cl != NULL ? (char_u *)"." : (char_u *)"",
5595 ufunc->uf_name);
5596}
5597
5598/*
5599 * Compile all the :def functions defined in the current script
5600 */
5601 static void
5602defcompile_funcs_in_script(void)
5603{
5604 long todo = (long)func_hashtab.ht_used;
5605 int changed = func_hashtab.ht_changed;
5606 hashitem_T *hi;
5607
5608 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5609 {
5610 if (!HASHITEM_EMPTY(hi))
5611 {
5612 --todo;
5613 ufunc_T *ufunc = HI2UF(hi);
5614 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5615 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5616 && (ufunc->uf_flags & FC_DEAD) == 0)
5617 {
5618 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5619
5620 if (func_hashtab.ht_changed != changed)
5621 {
5622 // a function has been added or removed, need to start
5623 // over
5624 todo = (long)func_hashtab.ht_used;
5625 changed = func_hashtab.ht_changed;
5626 hi = func_hashtab.ht_array;
5627 --hi;
5628 }
5629 }
5630 }
5631 }
5632}
5633
5634/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005635 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005636 * be compiled or the one specified by the argument.
5637 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005638 */
5639 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005640ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005641{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005642 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005643 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005644 typval_T tv;
5645
5646 if (is_class_name(eap->arg, &tv))
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005647 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005648 class_T *cl = tv.vval.v_class;
5649
5650 if (cl != NULL)
5651 defcompile_class(cl);
5652 }
5653 else
5654 {
5655 compiletype_T compile_type = CT_NONE;
5656 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
5657 if (ufunc != NULL)
5658 defcompile_function(ufunc, NULL);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005659 }
5660 }
5661 else
5662 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005663 defcompile_funcs_in_script();
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005664
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005665 // compile all the class defined in the current script
5666 defcompile_classes_in_script();
Bram Moolenaar822ba242020-05-24 23:00:18 +02005667 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005668}
5669
5670/*
5671 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5672 * Return 2 if "p" starts with "s:".
5673 * Return 0 otherwise.
5674 */
5675 int
5676eval_fname_script(char_u *p)
5677{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005678 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5679 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005680 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5681 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5682 return 5;
5683 if (p[0] == 's' && p[1] == ':')
5684 return 2;
5685 return 0;
5686}
5687
5688 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005689translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005690{
5691 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005692 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005693 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005694}
5695
5696/*
5697 * Return TRUE when "ufunc" has old-style "..." varargs
5698 * or named varargs "...name: type".
5699 */
5700 int
5701has_varargs(ufunc_T *ufunc)
5702{
5703 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005704}
5705
5706/*
5707 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005708 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005709 */
5710 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005711function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005712{
5713 char_u *nm = name;
5714 char_u *p;
5715 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005716 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005717 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005718
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005719 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5720 if (no_deref)
5721 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005722 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005723 nm = skipwhite(nm);
5724
Bram Moolenaare38eab22019-12-05 21:50:01 +01005725 // Only accept "funcname", "funcname ", "funcname (..." and
5726 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005727 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005728 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005729 vim_free(p);
5730 return n;
5731}
5732
Bram Moolenaar113e1072019-01-20 15:30:40 +01005733#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005734 char_u *
5735get_expanded_name(char_u *name, int check)
5736{
5737 char_u *nm = name;
5738 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005739 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005740
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005741 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005742
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005743 if (p != NULL && *nm == NUL
5744 && (!check || translated_function_exists(p, is_global)))
5745 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005746
5747 vim_free(p);
5748 return NULL;
5749}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005750#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005751
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005752/*
5753 * Function given to ExpandGeneric() to obtain the list of user defined
5754 * function names.
5755 */
5756 char_u *
5757get_user_func_name(expand_T *xp, int idx)
5758{
5759 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005760 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005761 static hashitem_T *hi;
5762 ufunc_T *fp;
5763
5764 if (idx == 0)
5765 {
5766 done = 0;
5767 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005768 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005769 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005770 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005771 {
5772 if (done++ > 0)
5773 ++hi;
5774 while (HASHITEM_EMPTY(hi))
5775 ++hi;
5776 fp = HI2UF(hi);
5777
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005778 // don't show dead, dict and lambda functions
5779 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005780 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005781 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005782
5783 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005784 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005785
5786 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005787 if (xp->xp_context != EXPAND_USER_FUNC
5788 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005789 {
5790 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005791 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005792 STRCAT(IObuff, ")");
5793 }
5794 return IObuff;
5795 }
5796 return NULL;
5797}
5798
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005799/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005800 * Make a copy of a function.
5801 * Intended to be used for a function defined on a base class that has a copy
5802 * on the child class.
5803 * The copy has uf_refcount set to one.
5804 * Returns NULL when out of memory.
5805 */
5806 ufunc_T *
5807copy_function(ufunc_T *fp)
5808{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005809 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005810 if (ufunc == NULL)
5811 return NULL;
5812
5813 // Most things can just be copied.
5814 *ufunc = *fp;
5815
5816 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5817 ufunc->uf_dfunc_idx = 0;
5818 ufunc->uf_class = NULL;
5819
5820 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5821 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5822
5823 if (ufunc->uf_arg_types != NULL)
5824 {
5825 // "uf_arg_types" is an allocated array, make a copy.
5826 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5827 if (at != NULL)
5828 {
5829 mch_memmove(at, ufunc->uf_arg_types,
5830 sizeof(type_T *) * ufunc->uf_args.ga_len);
5831 ufunc->uf_arg_types = at;
5832 }
5833 }
5834
5835 // TODO: how about the types themselves? they can be freed when the
5836 // original function is freed:
5837 // type_T **uf_arg_types;
5838 // type_T *uf_ret_type;
5839
Ernie Rael114ec812023-06-04 18:11:35 +01005840 // make uf_type_list empty
5841 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005842
5843 // TODO: partial_T *uf_partial;
5844
5845 if (ufunc->uf_va_name != NULL)
5846 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5847
5848 // TODO:
5849 // type_T *uf_va_type;
5850 // type_T *uf_func_type;
5851
5852 ufunc->uf_block_depth = 0;
5853 ufunc->uf_block_ids = NULL;
5854
5855 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5856
5857 ufunc->uf_refcount = 1;
5858 ufunc->uf_name_exp = NULL;
5859 STRCPY(ufunc->uf_name, fp->uf_name);
5860
5861 return ufunc;
5862}
5863
5864/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005865 * ":delfunction {name}"
5866 */
5867 void
5868ex_delfunction(exarg_T *eap)
5869{
5870 ufunc_T *fp = NULL;
5871 char_u *p;
5872 char_u *name;
5873 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005874 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005875
5876 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005877 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5878 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005879 vim_free(fudi.fd_newkey);
5880 if (name == NULL)
5881 {
5882 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005883 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005884 return;
5885 }
5886 if (!ends_excmd(*skipwhite(p)))
5887 {
5888 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005889 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005890 return;
5891 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005892 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005893 if (eap->nextcmd != NULL)
5894 *p = NUL;
5895
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005896 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005897 {
5898 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005899 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005900 vim_free(name);
5901 return;
5902 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005903 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005904 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005905 vim_free(name);
5906
5907 if (!eap->skip)
5908 {
5909 if (fp == NULL)
5910 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005911 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005912 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005913 return;
5914 }
5915 if (fp->uf_calls > 0)
5916 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005917 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005918 return;
5919 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005920 if (fp->uf_flags & FC_VIM9)
5921 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005922 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005923 return;
5924 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005925
5926 if (fudi.fd_dict != NULL)
5927 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005928 // Delete the dict item that refers to the function, it will
5929 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005930 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005931 }
5932 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005933 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005934 // A normal function (not a numbered function or lambda) has a
5935 // refcount of 1 for the entry in the hashtable. When deleting
5936 // it and the refcount is more than one, it should be kept.
5937 // A numbered function and lambda should be kept if the refcount is
5938 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005939 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005940 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005941 // Function is still referenced somewhere. Don't free it but
5942 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005943 if (func_remove(fp))
5944 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005945 }
5946 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005947 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005948 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005949 }
5950}
5951
5952/*
5953 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005954 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005955 */
5956 void
5957func_unref(char_u *name)
5958{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005959 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005960
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005961 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005962 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005963 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005964 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005965 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005966#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005967 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005968#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005969 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005970 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005971 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005972}
5973
5974/*
5975 * Unreference a Function: decrement the reference count and free it when it
5976 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005977 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005978 */
5979 void
5980func_ptr_unref(ufunc_T *fp)
5981{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005982 if (fp != NULL && (--fp->uf_refcount <= 0
5983 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5984 && fp->uf_partial->pt_refcount <= 1
5985 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005986 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005987 // Only delete it when it's not being used. Otherwise it's done
5988 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005989 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005990 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005991 }
5992}
5993
5994/*
5995 * Count a reference to a Function.
5996 */
5997 void
5998func_ref(char_u *name)
5999{
6000 ufunc_T *fp;
6001
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006002 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006003 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006004 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006005 if (fp != NULL)
6006 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006007 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01006008 // Only give an error for a numbered function.
6009 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01006010 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006011}
6012
6013/*
6014 * Count a reference to a Function.
6015 */
6016 void
6017func_ptr_ref(ufunc_T *fp)
6018{
6019 if (fp != NULL)
6020 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006021}
6022
6023/*
6024 * Return TRUE if items in "fc" do not have "copyID". That means they are not
6025 * referenced from anywhere that is in use.
6026 */
6027 static int
6028can_free_funccal(funccall_T *fc, int copyID)
6029{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006030 return (fc->fc_l_varlist.lv_copyID != copyID
6031 && fc->fc_l_vars.dv_copyID != copyID
6032 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006033 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006034}
6035
6036/*
6037 * ":return [expr]"
6038 */
6039 void
6040ex_return(exarg_T *eap)
6041{
6042 char_u *arg = eap->arg;
6043 typval_T rettv;
6044 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006045 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006046
6047 if (current_funccal == NULL)
6048 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006049 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006050 return;
6051 }
6052
Bram Moolenaar844fb642021-10-23 13:32:30 +01006053 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006054 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
6055
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006056 if (eap->skip)
6057 ++emsg_skip;
6058
6059 eap->nextcmd = NULL;
6060 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006061 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006062 {
6063 if (!eap->skip)
6064 returning = do_return(eap, FALSE, TRUE, &rettv);
6065 else
6066 clear_tv(&rettv);
6067 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01006068 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006069 else if (!eap->skip)
6070 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006071 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006072 update_force_abort();
6073
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006074 /*
6075 * Return unless the expression evaluation has been cancelled due to an
6076 * aborting error, an interrupt, or an exception.
6077 */
6078 if (!aborting())
6079 returning = do_return(eap, FALSE, TRUE, NULL);
6080 }
6081
Bram Moolenaare38eab22019-12-05 21:50:01 +01006082 // When skipping or the return gets pending, advance to the next command
6083 // in this line (!returning). Otherwise, ignore the rest of the line.
6084 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006085 if (returning)
6086 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006087 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006088 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006089
6090 if (eap->skip)
6091 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006092 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006093}
6094
zeertzjq5299c092023-04-12 20:48:16 +01006095/*
6096 * Lower level implementation of "call". Only called when not skipping.
6097 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006098 static int
6099ex_call_inner(
6100 exarg_T *eap,
6101 char_u *name,
6102 char_u **arg,
6103 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006104 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006105 evalarg_T *evalarg)
6106{
6107 linenr_T lnum;
6108 int doesrange;
6109 typval_T rettv;
6110 int failed = FALSE;
6111
zeertzjq5299c092023-04-12 20:48:16 +01006112 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006113 for ( ; lnum <= eap->line2; ++lnum)
6114 {
6115 funcexe_T funcexe;
6116
zeertzjq5299c092023-04-12 20:48:16 +01006117 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006118 {
6119 if (lnum > curbuf->b_ml.ml_line_count)
6120 {
6121 // If the function deleted lines or switched to another buffer
6122 // the line number may become invalid.
6123 emsg(_(e_invalid_range));
6124 break;
6125 }
6126 curwin->w_cursor.lnum = lnum;
6127 curwin->w_cursor.col = 0;
6128 curwin->w_cursor.coladd = 0;
6129 }
6130 *arg = startarg;
6131
6132 funcexe = *funcexe_init;
6133 funcexe.fe_doesrange = &doesrange;
6134 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6135 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6136 {
6137 failed = TRUE;
6138 break;
6139 }
6140 if (has_watchexpr())
6141 dbg_check_breakpoint(eap);
6142
6143 // Handle a function returning a Funcref, Dictionary or List.
6144 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006145 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006146 {
6147 failed = TRUE;
6148 break;
6149 }
6150
6151 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006152 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006153 break;
6154
6155 // Stop when immediately aborting on error, or when an interrupt
6156 // occurred or an exception was thrown but not caught.
6157 // get_func_tv() returned OK, so that the check for trailing
6158 // characters below is executed.
6159 if (aborting())
6160 break;
6161 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006162 return failed;
6163}
6164
6165/*
6166 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6167 * Returns FAIL or OK.
6168 */
6169 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006170ex_defer_inner(
6171 char_u *name,
6172 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006173 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006174 partial_T *partial,
6175 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006176{
6177 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006178 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006179 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006180 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006181
6182 if (current_funccal == NULL)
6183 {
6184 semsg(_(e_str_not_inside_function), "defer");
6185 return FAIL;
6186 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006187 if (partial != NULL)
6188 {
6189 if (partial->pt_dict != NULL)
6190 {
6191 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6192 return FAIL;
6193 }
6194 if (partial->pt_argc > 0)
6195 {
6196 int i;
6197
6198 partial_argc = partial->pt_argc;
6199 for (i = 0; i < partial_argc; ++i)
6200 copy_tv(&partial->pt_argv[i], &argvars[i]);
6201 }
6202 }
Ernie Raelb077b582023-12-14 20:11:44 +01006203 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006204 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006205 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006206 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006207
6208 if (r == OK)
6209 {
6210 if (type != NULL)
6211 {
6212 // Check that the arguments are OK for the types of the funcref.
6213 r = check_argument_types(type, argvars, argcount, NULL, name);
6214 }
Ernie Raelb077b582023-12-14 20:11:44 +01006215 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006216 {
6217 int idx = find_internal_func(name);
6218
6219 if (idx < 0)
6220 {
6221 emsg_funcname(e_unknown_function_str, name);
6222 r = FAIL;
6223 }
6224 else if (check_internal_func(idx, argcount) == -1)
6225 r = FAIL;
6226 }
6227 else
6228 {
6229 ufunc_T *ufunc = find_func(name, FALSE);
6230
6231 // we tolerate an unknown function here, it might be defined later
6232 if (ufunc != NULL)
6233 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006234 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006235 if (error != FCERR_UNKNOWN)
6236 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006237 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006238 r = FAIL;
6239 }
6240 }
6241 }
6242 }
6243
Bram Moolenaar86d87252022-09-05 21:21:25 +01006244 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006245 {
6246 while (--argcount >= 0)
6247 clear_tv(&argvars[argcount]);
6248 return FAIL;
6249 }
6250 return add_defer(name, argcount, argvars);
6251}
6252
6253/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006254 * Return TRUE if currently inside a function call.
6255 * Give an error message and return FALSE when not.
6256 */
6257 int
6258can_add_defer(void)
6259{
6260 if (!in_def_function() && get_current_funccal() == NULL)
6261 {
6262 semsg(_(e_str_not_inside_function), "defer");
6263 return FALSE;
6264 }
6265 return TRUE;
6266}
6267
6268/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006269 * Add a deferred call for "name" with arguments "argvars[argcount]".
6270 * Consumes "argvars[]".
6271 * Caller must check that in_def_function() returns TRUE or current_funccal is
6272 * not NULL.
6273 * Returns OK or FAIL.
6274 */
6275 int
6276add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6277{
6278 char_u *saved_name = vim_strsave(name);
6279 int argcount = argcount_arg;
6280 defer_T *dr;
6281 int ret = FAIL;
6282
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006283 if (saved_name == NULL)
6284 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006285 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006286 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006287 if (add_defer_function(saved_name, argcount, argvars) == OK)
6288 argcount = 0;
6289 }
6290 else
6291 {
6292 if (current_funccal->fc_defer.ga_itemsize == 0)
6293 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6294 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6295 goto theend;
6296 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6297 + current_funccal->fc_defer.ga_len++;
6298 dr->dr_name = saved_name;
6299 dr->dr_argcount = argcount;
6300 while (argcount > 0)
6301 {
6302 --argcount;
6303 dr->dr_argvars[argcount] = argvars[argcount];
6304 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006305 }
6306 ret = OK;
6307
6308theend:
6309 while (--argcount >= 0)
6310 clear_tv(&argvars[argcount]);
6311 return ret;
6312}
6313
6314/*
6315 * Invoked after a functions has finished: invoke ":defer" functions.
6316 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006317 static void
6318handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006319{
6320 int idx;
6321
Bram Moolenaar58779852022-09-06 18:31:14 +01006322 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006323 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006324 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006325
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006326 if (dr->dr_name == NULL)
6327 // already being called, can happen if function does ":qa"
6328 continue;
6329
6330 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006331 CLEAR_FIELD(funcexe);
6332 funcexe.fe_evaluate = TRUE;
6333
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006334 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006335 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006336
6337 char_u *name = dr->dr_name;
6338 dr->dr_name = NULL;
6339
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006340 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006341 // first statement in the function will be executed (because of the
6342 // exception). So save and restore the try/catch/throw exception
6343 // state.
6344 exception_state_T estate;
6345 exception_state_save(&estate);
6346 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006347
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006348 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6349
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006350 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006351
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006352 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006353 vim_free(name);
6354 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006355 clear_tv(&dr->dr_argvars[i]);
6356 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006357 ga_clear(&funccal->fc_defer);
6358}
6359
zeertzjq960cf912023-04-18 21:52:54 +01006360 static void
6361invoke_funccall_defer(funccall_T *fc)
6362{
6363 if (fc->fc_ectx != NULL)
6364 {
6365 // :def function
6366 unwind_def_callstack(fc->fc_ectx);
6367 may_invoke_defer_funcs(fc->fc_ectx);
6368 }
6369 else
6370 {
6371 // legacy function
6372 handle_defer_one(fc);
6373 }
6374}
6375
Bram Moolenaar58779852022-09-06 18:31:14 +01006376/*
6377 * Called when exiting: call all defer functions.
6378 */
6379 void
6380invoke_all_defer(void)
6381{
zeertzjq1be4b812023-04-19 14:21:24 +01006382 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6383 invoke_funccall_defer(fc);
6384
zeertzjq960cf912023-04-18 21:52:54 +01006385 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6386 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6387 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006388}
6389
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006390/*
6391 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006392 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006393 */
6394 void
6395ex_call(exarg_T *eap)
6396{
6397 char_u *arg = eap->arg;
6398 char_u *startarg;
6399 char_u *name;
6400 char_u *tofree;
6401 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006402 int failed = FALSE;
6403 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006404 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006405 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006406 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006407 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006408 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006409 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006410
Bram Moolenaare6b53242020-07-01 17:28:33 +02006411 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006412 if (eap->skip)
6413 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006414 typval_T rettv;
6415
Bram Moolenaare38eab22019-12-05 21:50:01 +01006416 // trans_function_name() doesn't work well when skipping, use eval0()
6417 // instead to skip to any following command, e.g. for:
6418 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006419 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006420 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006421 clear_tv(&rettv);
6422 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006423 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006424 return;
6425 }
6426
zeertzjq5299c092023-04-12 20:48:16 +01006427 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006428 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006429 if (fudi.fd_newkey != NULL)
6430 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006431 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006432 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006433 vim_free(fudi.fd_newkey);
6434 }
6435 if (tofree == NULL)
6436 return;
6437
Bram Moolenaare38eab22019-12-05 21:50:01 +01006438 // Increase refcount on dictionary, it could get deleted when evaluating
6439 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006440 if (fudi.fd_dict != NULL)
6441 ++fudi.fd_dict->dv_refcount;
6442
Bram Moolenaare38eab22019-12-05 21:50:01 +01006443 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6444 // contents. For VAR_PARTIAL get its partial, unless we already have one
6445 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006446 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006447 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006448 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006449 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006450
Bram Moolenaare38eab22019-12-05 21:50:01 +01006451 // Skip white space to allow ":call func ()". Not good, but required for
6452 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006453 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006454 if (*startarg != '(')
6455 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006456 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006457 goto end;
6458 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006459 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006460 {
6461 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6462 goto end;
6463 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006464
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006465 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006466 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006467 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006468 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006469 }
6470 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006471 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006472 funcexe_T funcexe;
6473
Bram Moolenaara80faa82020-04-12 19:37:17 +02006474 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006475 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006476 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006477 funcexe.fe_partial = partial;
6478 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006479 funcexe.fe_firstline = eap->line1;
6480 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006481 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006482 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006483 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006484 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006485
Bram Moolenaar1d341892021-09-18 15:25:52 +02006486 // When inside :try we need to check for following "| catch" or "| endtry".
6487 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006488 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006489 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006490 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006491 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006492 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006493 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006494 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006495 {
6496 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006497 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006498 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006499 }
6500 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006501 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006502 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006503 // Must be after using "arg", it may point into memory cleared here.
6504 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006505
6506end:
6507 dict_unref(fudi.fd_dict);
6508 vim_free(tofree);
6509}
6510
6511/*
6512 * Return from a function. Possibly makes the return pending. Also called
6513 * for a pending return at the ":endtry" or after returning from an extra
6514 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6515 * when called due to a ":return" command. "rettv" may point to a typval_T
6516 * with the return rettv. Returns TRUE when the return can be carried out,
6517 * FALSE when the return gets pending.
6518 */
6519 int
6520do_return(
6521 exarg_T *eap,
6522 int reanimate,
6523 int is_cmd,
6524 void *rettv)
6525{
6526 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006527 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006528
6529 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006530 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006531 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006532
6533 /*
6534 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6535 * not in its finally clause (which then is to be executed next) is found.
6536 * In this case, make the ":return" pending for execution at the ":endtry".
6537 * Otherwise, return normally.
6538 */
6539 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6540 if (idx >= 0)
6541 {
6542 cstack->cs_pending[idx] = CSTP_RETURN;
6543
6544 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006545 // A pending return again gets pending. "rettv" points to an
6546 // allocated variable with the rettv of the original ":return"'s
6547 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006548 cstack->cs_rettv[idx] = rettv;
6549 else
6550 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006551 // When undoing a return in order to make it pending, get the stored
6552 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006553 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006554 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006555
6556 if (rettv != NULL)
6557 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006558 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006559 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6560 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6561 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006562 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006563 }
6564 else
6565 cstack->cs_rettv[idx] = NULL;
6566
6567 if (reanimate)
6568 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006569 // The pending return value could be overwritten by a ":return"
6570 // without argument in a finally clause; reset the default
6571 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006572 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6573 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006574 }
6575 }
6576 report_make_pending(CSTP_RETURN, rettv);
6577 }
6578 else
6579 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006580 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006581
Bram Moolenaare38eab22019-12-05 21:50:01 +01006582 // If the return is carried out now, store the return value. For
6583 // a return immediately after reanimation, the value is already
6584 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006585 if (!reanimate && rettv != NULL)
6586 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006587 clear_tv(current_funccal->fc_rettv);
6588 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006589 if (!is_cmd)
6590 vim_free(rettv);
6591 }
6592 }
6593
6594 return idx < 0;
6595}
6596
6597/*
6598 * Free the variable with a pending return value.
6599 */
6600 void
6601discard_pending_return(void *rettv)
6602{
6603 free_tv((typval_T *)rettv);
6604}
6605
6606/*
6607 * Generate a return command for producing the value of "rettv". The result
6608 * is an allocated string. Used by report_pending() for verbose messages.
6609 */
6610 char_u *
6611get_return_cmd(void *rettv)
6612{
6613 char_u *s = NULL;
6614 char_u *tofree = NULL;
6615 char_u numbuf[NUMBUFLEN];
6616
6617 if (rettv != NULL)
6618 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6619 if (s == NULL)
6620 s = (char_u *)"";
6621
6622 STRCPY(IObuff, ":return ");
6623 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6624 if (STRLEN(s) + 8 >= IOSIZE)
6625 STRCPY(IObuff + IOSIZE - 4, "...");
6626 vim_free(tofree);
6627 return vim_strsave(IObuff);
6628}
6629
6630/*
6631 * Get next function line.
6632 * Called by do_cmdline() to get the next line.
6633 * Returns allocated string, or NULL for end of function.
6634 */
6635 char_u *
6636get_func_line(
6637 int c UNUSED,
6638 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006639 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006640 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006641{
6642 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006643 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006644 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006645 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006646
Bram Moolenaare38eab22019-12-05 21:50:01 +01006647 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006648 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006649 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006650 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006651 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006652 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006653 }
6654#ifdef FEAT_PROFILE
6655 if (do_profiling == PROF_YES)
6656 func_line_end(cookie);
6657#endif
6658
6659 gap = &fp->uf_lines;
6660 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006661 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006662 retval = NULL;
6663 else
6664 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006665 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006666 while (fcp->fc_linenr < gap->ga_len
6667 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6668 ++fcp->fc_linenr;
6669 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006670 retval = NULL;
6671 else
6672 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006673 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6674 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006675#ifdef FEAT_PROFILE
6676 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006677 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006678#endif
6679 }
6680 }
6681
Bram Moolenaare38eab22019-12-05 21:50:01 +01006682 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006683 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006684 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006685 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006686 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006687 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006688 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006689 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006690 }
6691
6692 return retval;
6693}
6694
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006695/*
6696 * Return TRUE if the currently active function should be ended, because a
6697 * return was encountered or an error occurred. Used inside a ":while".
6698 */
6699 int
6700func_has_ended(void *cookie)
6701{
6702 funccall_T *fcp = (funccall_T *)cookie;
6703
Bram Moolenaare38eab22019-12-05 21:50:01 +01006704 // Ignore the "abort" flag if the abortion behavior has been changed due to
6705 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006706 return (((fcp->fc_func->uf_flags & FC_ABORT)
6707 && did_emsg && !aborted_in_try())
6708 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006709}
6710
6711/*
6712 * return TRUE if cookie indicates a function which "abort"s on errors.
6713 */
6714 int
6715func_has_abort(
6716 void *cookie)
6717{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006718 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006719}
6720
6721
6722/*
6723 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6724 * Don't do this when "Func" is already a partial that was bound
6725 * explicitly (pt_auto is FALSE).
6726 * Changes "rettv" in-place.
6727 * Returns the updated "selfdict_in".
6728 */
6729 dict_T *
6730make_partial(dict_T *selfdict_in, typval_T *rettv)
6731{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006732 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006733 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006734 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006735 dict_T *selfdict = selfdict_in;
6736
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006737 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6738 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006739 fp = rettv->vval.v_partial->pt_func;
6740 else
6741 {
6742 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006743 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006744 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006745 if (fname == NULL)
6746 {
6747 // There is no point binding a dict to a NULL function, just create
6748 // a function reference.
6749 rettv->v_type = VAR_FUNC;
6750 rettv->vval.v_string = NULL;
6751 }
6752 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006753 {
6754 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006755 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006756
6757 // Translate "s:func" to the stored function name.
6758 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6759 fp = find_func(fname, FALSE);
6760 vim_free(tofree);
6761 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006762 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006763
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006764 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006765 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006766 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006767
6768 if (pt != NULL)
6769 {
6770 pt->pt_refcount = 1;
6771 pt->pt_dict = selfdict;
6772 pt->pt_auto = TRUE;
6773 selfdict = NULL;
6774 if (rettv->v_type == VAR_FUNC)
6775 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006776 // Just a function: Take over the function name and use
6777 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006778 pt->pt_name = rettv->vval.v_string;
6779 }
6780 else
6781 {
6782 partial_T *ret_pt = rettv->vval.v_partial;
6783 int i;
6784
Bram Moolenaare38eab22019-12-05 21:50:01 +01006785 // Partial: copy the function name, use selfdict and copy
6786 // args. Can't take over name or args, the partial might
6787 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006788 if (ret_pt->pt_name != NULL)
6789 {
6790 pt->pt_name = vim_strsave(ret_pt->pt_name);
6791 func_ref(pt->pt_name);
6792 }
6793 else
6794 {
6795 pt->pt_func = ret_pt->pt_func;
6796 func_ptr_ref(pt->pt_func);
6797 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006798 if (ret_pt->pt_argc > 0)
6799 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006800 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006801 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006802 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006803 pt->pt_argc = 0;
6804 else
6805 {
6806 pt->pt_argc = ret_pt->pt_argc;
6807 for (i = 0; i < pt->pt_argc; i++)
6808 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6809 }
6810 }
6811 partial_unref(ret_pt);
6812 }
6813 rettv->v_type = VAR_PARTIAL;
6814 rettv->vval.v_partial = pt;
6815 }
6816 }
6817 return selfdict;
6818}
6819
6820/*
6821 * Return the name of the executed function.
6822 */
6823 char_u *
6824func_name(void *cookie)
6825{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006826 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006827}
6828
6829/*
6830 * Return the address holding the next breakpoint line for a funccall cookie.
6831 */
6832 linenr_T *
6833func_breakpoint(void *cookie)
6834{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006835 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006836}
6837
6838/*
6839 * Return the address holding the debug tick for a funccall cookie.
6840 */
6841 int *
6842func_dbg_tick(void *cookie)
6843{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006844 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006845}
6846
6847/*
6848 * Return the nesting level for a funccall cookie.
6849 */
6850 int
6851func_level(void *cookie)
6852{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006853 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006854}
6855
6856/*
6857 * Return TRUE when a function was ended by a ":return" command.
6858 */
6859 int
6860current_func_returned(void)
6861{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006862 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006863}
6864
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006865 int
6866free_unref_funccal(int copyID, int testing)
6867{
6868 int did_free = FALSE;
6869 int did_free_funccal = FALSE;
6870 funccall_T *fc, **pfc;
6871
6872 for (pfc = &previous_funccal; *pfc != NULL; )
6873 {
6874 if (can_free_funccal(*pfc, copyID))
6875 {
6876 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006877 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006878 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006879 did_free = TRUE;
6880 did_free_funccal = TRUE;
6881 }
6882 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006883 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006884 }
6885 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006886 // When a funccal was freed some more items might be garbage
6887 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006888 (void)garbage_collect(testing);
6889
6890 return did_free;
6891}
6892
6893/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006894 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006895 */
6896 static funccall_T *
6897get_funccal(void)
6898{
6899 int i;
6900 funccall_T *funccal;
6901 funccall_T *temp_funccal;
6902
6903 funccal = current_funccal;
6904 if (debug_backtrace_level > 0)
6905 {
6906 for (i = 0; i < debug_backtrace_level; i++)
6907 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006908 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006909 if (temp_funccal)
6910 funccal = temp_funccal;
6911 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006912 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006913 debug_backtrace_level = i;
6914 }
6915 }
6916 return funccal;
6917}
6918
6919/*
6920 * Return the hashtable used for local variables in the current funccal.
6921 * Return NULL if there is no current funccal.
6922 */
6923 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006924get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006925{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006926 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006927 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006928 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006929}
6930
6931/*
6932 * Return the l: scope variable.
6933 * Return NULL if there is no current funccal.
6934 */
6935 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006936get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006937{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006938 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006939 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006940 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006941}
6942
6943/*
6944 * Return the hashtable used for argument in the current funccal.
6945 * Return NULL if there is no current funccal.
6946 */
6947 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006948get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006949{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006950 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006951 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006952 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006953}
6954
6955/*
6956 * Return the a: scope variable.
6957 * Return NULL if there is no current funccal.
6958 */
6959 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006960get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006961{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006962 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006963 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006964 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006965}
6966
6967/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006968 * List function variables, if there is a function.
6969 */
6970 void
6971list_func_vars(int *first)
6972{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006973 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6974 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006975 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006976}
6977
6978/*
6979 * If "ht" is the hashtable for local variables in the current funccal, return
6980 * the dict that contains it.
6981 * Otherwise return NULL.
6982 */
6983 dict_T *
6984get_current_funccal_dict(hashtab_T *ht)
6985{
6986 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006987 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6988 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006989 return NULL;
6990}
6991
6992/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006993 * Search hashitem in parent scope.
6994 */
6995 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006996find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006997{
6998 funccall_T *old_current_funccal = current_funccal;
6999 hashtab_T *ht;
7000 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007001 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007002
Bram Moolenaarca16c602022-09-06 18:57:08 +01007003 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007004 return NULL;
7005
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02007006 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01007007 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02007008 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007009 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007010 ht = find_var_ht(name, &varname);
7011 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02007012 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007013 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02007014 if (!HASHITEM_EMPTY(hi))
7015 {
7016 *pht = ht;
7017 break;
7018 }
7019 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007020 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02007021 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007022 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007023 }
7024 current_funccal = old_current_funccal;
7025
7026 return hi;
7027}
7028
7029/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007030 * Search variable in parent scope.
7031 */
7032 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007033find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007034{
7035 dictitem_T *v = NULL;
7036 funccall_T *old_current_funccal = current_funccal;
7037 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007038 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007039
Bram Moolenaarca16c602022-09-06 18:57:08 +01007040 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007041 return NULL;
7042
Bram Moolenaare38eab22019-12-05 21:50:01 +01007043 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01007044 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007045 while (current_funccal)
7046 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007047 ht = find_var_ht(name, &varname);
7048 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007049 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007050 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007051 if (v != NULL)
7052 break;
7053 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007054 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007055 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007056 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007057 }
7058 current_funccal = old_current_funccal;
7059
7060 return v;
7061}
7062
7063/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007064 * Set "copyID + 1" in previous_funccal and callers.
7065 */
7066 int
7067set_ref_in_previous_funccal(int copyID)
7068{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007069 funccall_T *fc;
7070
Bram Moolenaarca16c602022-09-06 18:57:08 +01007071 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007072 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007073 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007074 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
7075 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
7076 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007077 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007078 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007079 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007080}
7081
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007082 static int
7083set_ref_in_funccal(funccall_T *fc, int copyID)
7084{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007085 if (fc->fc_copyID != copyID)
7086 {
7087 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007088 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7089 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7090 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7091 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007092 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007093 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007094 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007095}
7096
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007097/*
7098 * Set "copyID" in all local vars and arguments in the call stack.
7099 */
7100 int
7101set_ref_in_call_stack(int copyID)
7102{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007103 funccall_T *fc;
7104 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007105
Bram Moolenaarca16c602022-09-06 18:57:08 +01007106 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007107 if (set_ref_in_funccal(fc, copyID))
7108 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007109
7110 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007111 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007112 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007113 if (set_ref_in_funccal(fc, copyID))
7114 return TRUE;
7115 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007116}
7117
7118/*
7119 * Set "copyID" in all functions available by name.
7120 */
7121 int
7122set_ref_in_functions(int copyID)
7123{
7124 int todo;
7125 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007126 ufunc_T *fp;
7127
7128 todo = (int)func_hashtab.ht_used;
7129 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007130 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007131 if (!HASHITEM_EMPTY(hi))
7132 {
7133 --todo;
7134 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007135 if (!func_name_refcount(fp->uf_name)
7136 && set_ref_in_func(NULL, fp, copyID))
7137 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007138 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007139 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007140 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007141}
7142
7143/*
7144 * Set "copyID" in all function arguments.
7145 */
7146 int
7147set_ref_in_func_args(int copyID)
7148{
7149 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007150
7151 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007152 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7153 copyID, NULL, NULL))
7154 return TRUE;
7155 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007156}
7157
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007158/*
7159 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007160 * Returns TRUE if setting references failed somehow.
7161 */
7162 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007163set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007164{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007165 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007166 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007167 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007168 char_u fname_buf[FLEN_FIXED + 1];
7169 char_u *tofree = NULL;
7170 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007171 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007172
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007173 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007174 return FALSE;
7175
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007176 if (fp_in == NULL)
7177 {
7178 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007179 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007180 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007181 if (fp != NULL)
7182 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007183 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007184 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007185 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007186
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007187 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007188 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007189}
7190
Bram Moolenaare38eab22019-12-05 21:50:01 +01007191#endif // FEAT_EVAL