blob: 00e499fd0f123c3f8150726f29a33c3f21063c8f [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 {
558 if (STRCMP(aname, obj_members[om].ocm_name) == 0)
559 {
560 type = obj_members[om].ocm_type;
561 break;
562 }
563 }
564 }
565 else
566 type = parse_type(&p, &fp->uf_type_list, TRUE);
567 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100568 if (type == NULL)
569 return FAIL;
570 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000571 if (i < fp->uf_args.ga_len
572 && (type->tt_type == VAR_FUNC
573 || type->tt_type == VAR_PARTIAL)
574 && var_wrong_func_name(
575 ((char_u **)fp->uf_args.ga_data)[i], TRUE))
576 return FAIL;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100577 }
578 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100579 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200580
581 if (varargs)
582 {
583 char_u *p;
584
585 // Move the last argument "...name: type" to uf_va_name and
586 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200587 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000588 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
589 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200590 p = ((char_u **)argtypes->ga_data)[len];
591 if (p == NULL)
592 // TODO: get type from default value
593 fp->uf_va_type = &t_list_any;
594 else
595 {
596 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
597 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
598 {
599 semsg(_(e_variable_arguments_type_must_be_list_str),
600 ((char_u **)argtypes->ga_data)[len]);
601 return FAIL;
602 }
603 }
604 if (fp->uf_va_type == NULL)
605 return FAIL;
606 }
607
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100608 return OK;
609}
610
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100611 static int
612parse_return_type(ufunc_T *fp, char_u *ret_type)
613{
614 if (ret_type == NULL)
615 fp->uf_ret_type = &t_void;
616 else
617 {
618 char_u *p = ret_type;
619
620 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
621 if (fp->uf_ret_type == NULL)
622 {
623 fp->uf_ret_type = &t_void;
624 return FAIL;
625 }
626 }
627 return OK;
628}
629
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100630/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200631 * Register function "fp" as using "current_funccal" as its scope.
632 */
633 static int
634register_closure(ufunc_T *fp)
635{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200636 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100637 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200638 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200639 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200640 fp->uf_scoped = current_funccal;
641 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200642
Bram Moolenaarca16c602022-09-06 18:57:08 +0100643 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200644 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100645 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
646 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200647 return OK;
648}
649
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100650 static void
651set_ufunc_name(ufunc_T *fp, char_u *name)
652{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100653 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
654 // actually extends beyond the struct.
655 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100656
657 if (name[0] == K_SPECIAL)
658 {
659 fp->uf_name_exp = alloc(STRLEN(name) + 3);
660 if (fp->uf_name_exp != NULL)
661 {
662 STRCPY(fp->uf_name_exp, "<SNR>");
663 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
664 }
665 }
666}
667
Bram Moolenaar58016442016-07-31 18:30:22 +0200668/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100669 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
670 * return "buf" filled with a readable function name.
671 * Otherwise just return "name", thus the return value can always be used.
672 * "name" and "buf" may be equal.
673 */
674 char_u *
675make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
676{
677 size_t len;
678
679 if (name[0] != K_SPECIAL)
680 return name;
681 len = STRLEN(name);
682 if (len + 3 > bufsize)
683 return name;
684
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100685 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100686 mch_memmove(buf, "<SNR>", 5);
687 return buf;
688}
689
690/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200691 * Get a name for a lambda. Returned in static memory.
692 */
693 char_u *
694get_lambda_name(void)
695{
696 static char_u name[30];
697 static int lambda_no = 0;
698
699 sprintf((char*)name, "<lambda>%d", ++lambda_no);
700 return name;
701}
702
Bram Moolenaare01e5212023-01-08 20:31:18 +0000703/*
704 * Allocate a "ufunc_T" for a function called "name".
705 * Makes sure the size is right.
706 */
707 static ufunc_T *
708alloc_ufunc(char_u *name)
709{
710 // When the name is short we need to make sure we allocate enough bytes for
711 // the whole struct, including any padding.
712 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
713 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
714}
715
Bram Moolenaar801ab062020-06-25 19:27:56 +0200716#if defined(FEAT_LUA) || defined(PROTO)
717/*
718 * Registers a native C callback which can be called from Vim script.
719 * Returns the name of the Vim script function.
720 */
721 char_u *
722register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
723{
724 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200725 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200726
Bram Moolenaare01e5212023-01-08 20:31:18 +0000727 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200728 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200729 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200730
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200731 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200732 fp->uf_refcount = 1;
733 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000734 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200735 fp->uf_calls = 0;
736 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200737 fp->uf_cb = cb;
738 fp->uf_cb_free = cb_free;
739 fp->uf_cb_state = state;
740
741 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000742 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200743
744 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200745}
746#endif
747
Bram Moolenaar04b12692020-05-04 23:24:44 +0200748/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100749 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100750 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100751 * If "white_error" is not NULL check for correct use of white space and set
752 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100753 * Return NULL if no valid arrow found.
754 */
755 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100756skip_arrow(
757 char_u *start,
758 int equal_arrow,
759 char_u **ret_type,
760 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100761{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100762 char_u *s = start;
763 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100764
765 if (equal_arrow)
766 {
767 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100768 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100769 if (white_error != NULL && !VIM_ISWHITE(s[1]))
770 {
771 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100772 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100773 return NULL;
774 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100775 s = skipwhite(s + 1);
776 *ret_type = s;
777 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100778 if (s == *ret_type)
779 {
780 emsg(_(e_missing_return_type));
781 return NULL;
782 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100783 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100784 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100785 s = skipwhite(s);
786 if (*s != '=')
787 return NULL;
788 ++s;
789 }
790 if (*s != '>')
791 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100792 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
793 || !IS_WHITE_OR_NUL(s[1])))
794 {
795 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100796 semsg(_(e_white_space_required_before_and_after_str_at_str),
797 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100798 return NULL;
799 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100800 return skipwhite(s + 1);
801}
802
803/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100804 * Check if "*cmd" points to a function command and if so advance "*cmd" and
805 * return TRUE.
806 * Otherwise return FALSE;
807 * Do not consider "function(" to be a command.
808 */
809 static int
810is_function_cmd(char_u **cmd)
811{
812 char_u *p = *cmd;
813
814 if (checkforcmd(&p, "function", 2))
815 {
816 if (*p == '(')
817 return FALSE;
818 *cmd = p;
819 return TRUE;
820 }
821 return FALSE;
822}
823
824/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200825 * Called when defining a function: The context may be needed for script
826 * variables declared in a block that is visible now but not when the function
827 * is compiled or called later.
828 */
829 static void
830function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
831{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000832 if (cstack == NULL || cstack->cs_idx < 0)
833 return;
834
835 int count = cstack->cs_idx + 1;
836 int i;
837
838 fp->uf_block_ids = ALLOC_MULT(int, count);
839 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200840 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000841 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
842 sizeof(int) * count);
843 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200844 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000845
846 // Set flag in each block to indicate a function was defined. This
847 // is used to keep the variable when leaving the block, see
848 // hide_script_var().
849 for (i = 0; i <= cstack->cs_idx; ++i)
850 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200851}
852
853/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100854 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200855 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100856 * "newlines" must already have been initialized.
857 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
858 */
859 static int
860get_function_body(
861 exarg_T *eap,
862 garray_T *newlines,
863 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000864 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100865{
866 linenr_T sourcing_lnum_top = SOURCING_LNUM;
867 linenr_T sourcing_lnum_off;
868 int saved_wait_return = need_wait_return;
869 char_u *line_arg = line_arg_in;
870 int vim9_function = eap->cmdidx == CMD_def
871 || eap->cmdidx == CMD_block;
872#define MAX_FUNC_NESTING 50
873 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200874 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100875 int nesting = 0;
876 getline_opt_T getline_options;
877 int indent = 2;
878 char_u *skip_until = NULL;
879 int ret = FAIL;
880 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200881 int heredoc_concat_len = 0;
882 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100883 char_u *heredoc_trimmed = NULL;
884
Bram Moolenaar20677332021-06-06 17:02:53 +0200885 ga_init2(&heredoc_ga, 1, 500);
886
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100887 // Detect having skipped over comment lines to find the return
888 // type. Add NULL lines to keep the line count correct.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100889 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100890 if (SOURCING_LNUM < sourcing_lnum_off)
891 {
892 sourcing_lnum_off -= SOURCING_LNUM;
893 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
894 goto theend;
895 while (sourcing_lnum_off-- > 0)
896 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
897 }
898
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200899 nesting_def[0] = vim9_function;
900 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100901 getline_options = vim9_function
902 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
903 for (;;)
904 {
905 char_u *theline;
906 char_u *p;
907 char_u *arg;
908
909 if (KeyTyped)
910 {
911 msg_scroll = TRUE;
912 saved_wait_return = FALSE;
913 }
914 need_wait_return = FALSE;
915
916 if (line_arg != NULL)
917 {
918 // Use eap->arg, split up in parts by line breaks.
919 theline = line_arg;
920 p = vim_strchr(theline, '\n');
921 if (p == NULL)
922 line_arg += STRLEN(line_arg);
923 else
924 {
925 *p = NUL;
926 line_arg = p + 1;
927 }
928 }
929 else
930 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000931 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100932 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100933 }
934 if (KeyTyped)
935 lines_left = Rows - 1;
936 if (theline == NULL)
937 {
938 // Use the start of the function for the line number.
939 SOURCING_LNUM = sourcing_lnum_top;
940 if (skip_until != NULL)
941 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200942 else if (nesting_inline[nesting])
943 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100944 else if (eap->cmdidx == CMD_def)
945 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100946 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000947 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100948 goto theend;
949 }
950
951 // Detect line continuation: SOURCING_LNUM increased more than one.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100952 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100953 if (SOURCING_LNUM < sourcing_lnum_off)
954 sourcing_lnum_off -= SOURCING_LNUM;
955 else
956 sourcing_lnum_off = 0;
957
958 if (skip_until != NULL)
959 {
960 // Don't check for ":endfunc"/":enddef" between
961 // * ":append" and "."
962 // * ":python <<EOF" and "EOF"
963 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
964 if (heredoc_trimmed == NULL
965 || (is_heredoc && skipwhite(theline) == theline)
966 || STRNCMP(theline, heredoc_trimmed,
967 STRLEN(heredoc_trimmed)) == 0)
968 {
969 if (heredoc_trimmed == NULL)
970 p = theline;
971 else if (is_heredoc)
972 p = skipwhite(theline) == theline
973 ? theline : theline + STRLEN(heredoc_trimmed);
974 else
975 p = theline + STRLEN(heredoc_trimmed);
976 if (STRCMP(p, skip_until) == 0)
977 {
978 VIM_CLEAR(skip_until);
979 VIM_CLEAR(heredoc_trimmed);
980 getline_options = vim9_function
981 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
982 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200983
984 if (heredoc_concat_len > 0)
985 {
986 // Replace the starting line with all the concatenated
987 // lines.
988 ga_concat(&heredoc_ga, theline);
989 vim_free(((char_u **)(newlines->ga_data))[
990 heredoc_concat_len - 1]);
991 ((char_u **)(newlines->ga_data))[
992 heredoc_concat_len - 1] = heredoc_ga.ga_data;
993 ga_init(&heredoc_ga);
994 heredoc_concat_len = 0;
995 theline += STRLEN(theline); // skip the "EOF"
996 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100997 }
998 }
999 }
1000 else
1001 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001002 int c;
1003 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001004 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001005
1006 // skip ':' and blanks
1007 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1008 ;
1009
1010 // Check for "endfunction", "enddef" or "}".
1011 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001012 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001013 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001014 ? *p == '}'
1015 : (checkforcmd(&p, nesting_def[nesting]
1016 ? "enddef" : "endfunction", 4)
1017 && *p != ':'))
1018 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001019 if (!nesting_inline[nesting] && nesting_def[nesting]
1020 && p < cmd + 6)
1021 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001022 if (nesting-- == 0)
1023 {
1024 char_u *nextcmd = NULL;
1025
1026 if (*p == '|' || *p == '}')
1027 nextcmd = p + 1;
1028 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1029 nextcmd = line_arg;
1030 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001031 && (vim9_function || p_verbose > 0))
1032 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001033 SOURCING_LNUM = sourcing_lnum_top
1034 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001035 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001036 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001037 else
1038 give_warning2((char_u *)
1039 _("W22: Text found after :endfunction: %s"),
1040 p, TRUE);
1041 }
1042 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001043 {
1044 // Another command follows. If the line came from "eap"
1045 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001046 // change "eap->cmdlinep" to point to the last fetched
1047 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001048 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001049 if (lines_to_free->ga_len > 0
1050 && *eap->cmdlinep !=
1051 ((char_u **)lines_to_free->ga_data)
1052 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001053 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001054 // *cmdlinep will be freed later, thus remove the
1055 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001056 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001057 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1058 [lines_to_free->ga_len - 1];
1059 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001060 }
1061 }
1062 break;
1063 }
1064 }
1065
1066 // Check for mismatched "endfunc" or "enddef".
1067 // We don't check for "def" inside "func" thus we also can't check
1068 // for "enddef".
1069 // We continue to find the end of the function, although we might
1070 // not find it.
1071 else if (nesting_def[nesting])
1072 {
1073 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1074 emsg(_(e_mismatched_endfunction));
1075 }
1076 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1077 emsg(_(e_mismatched_enddef));
1078
1079 // Increase indent inside "if", "while", "for" and "try", decrease
1080 // at "end".
1081 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1082 indent -= 2;
1083 else if (STRNCMP(p, "if", 2) == 0
1084 || STRNCMP(p, "wh", 2) == 0
1085 || STRNCMP(p, "for", 3) == 0
1086 || STRNCMP(p, "try", 3) == 0)
1087 indent += 2;
1088
1089 // Check for defining a function inside this function.
1090 // Only recognize "def" inside "def", not inside "function",
1091 // For backwards compatibility, see Test_function_python().
1092 c = *p;
1093 if (is_function_cmd(&p)
1094 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1095 {
1096 if (*p == '!')
1097 p = skipwhite(p + 1);
1098 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001099 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001100 if (*skipwhite(p) == '(')
1101 {
1102 if (nesting == MAX_FUNC_NESTING - 1)
1103 emsg(_(e_function_nesting_too_deep));
1104 else
1105 {
1106 ++nesting;
1107 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001108 nesting_inline[nesting] = FALSE;
1109 indent += 2;
1110 }
1111 }
1112 }
1113
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001114 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001115 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001116 // Not a comment line: check for nested inline function.
1117 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001118 while (end > p && VIM_ISWHITE(*end))
1119 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001120 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001121 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001122 int is_block;
1123
1124 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001125 --end;
1126 while (end > p && VIM_ISWHITE(*end))
1127 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001128 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1129 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001130 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001131 char_u *s = p;
1132
1133 // check for line starting with "au" for :autocmd or
1134 // "com" for :command, these can use a {} block
1135 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1136 || checkforcmd_noparen(&s, "command", 3);
1137 }
1138
1139 if (is_block)
1140 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001141 if (nesting == MAX_FUNC_NESTING - 1)
1142 emsg(_(e_function_nesting_too_deep));
1143 else
1144 {
1145 ++nesting;
1146 nesting_def[nesting] = TRUE;
1147 nesting_inline[nesting] = TRUE;
1148 indent += 2;
1149 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001150 }
1151 }
1152 }
1153
1154 // Check for ":append", ":change", ":insert". Not for :def.
1155 p = skip_range(p, FALSE, NULL);
1156 if (!vim9_function
1157 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1158 || (p[0] == 'c'
1159 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1160 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1161 && (STRNCMP(&p[3], "nge", 3) != 0
1162 || !ASCII_ISALPHA(p[6])))))))
1163 || (p[0] == 'i'
1164 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1165 && (!ASCII_ISALPHA(p[2])
1166 || (p[2] == 's'
1167 && (!ASCII_ISALPHA(p[3])
1168 || p[3] == 'e'))))))))
1169 skip_until = vim_strsave((char_u *)".");
1170
1171 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1172 arg = skipwhite(skiptowhite(p));
1173 if (arg[0] == '<' && arg[1] =='<'
1174 && ((p[0] == 'p' && p[1] == 'y'
1175 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1176 || ((p[2] == '3' || p[2] == 'x')
1177 && !ASCII_ISALPHA(p[3]))))
1178 || (p[0] == 'p' && p[1] == 'e'
1179 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1180 || (p[0] == 't' && p[1] == 'c'
1181 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1182 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1183 && !ASCII_ISALPHA(p[3]))
1184 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1185 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1186 || (p[0] == 'm' && p[1] == 'z'
1187 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1188 ))
1189 {
1190 // ":python <<" continues until a dot, like ":append"
1191 p = skipwhite(arg + 2);
1192 if (STRNCMP(p, "trim", 4) == 0)
1193 {
1194 // Ignore leading white space.
1195 p = skipwhite(p + 4);
1196 heredoc_trimmed = vim_strnsave(theline,
1197 skipwhite(theline) - theline);
1198 }
1199 if (*p == NUL)
1200 skip_until = vim_strsave((char_u *)".");
1201 else
1202 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1203 getline_options = GETLINE_NONE;
1204 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001205 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001206 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001207 }
1208
Bram Moolenaard881d152022-05-13 13:50:36 +01001209 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001210 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001211 // Check for ":cmd v =<< [trim] EOF"
1212 // and ":cmd [a, b] =<< [trim] EOF"
1213 // and "lines =<< [trim] EOF" for Vim9
1214 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001215 arg = p;
1216 if (checkforcmd(&arg, "let", 2)
1217 || checkforcmd(&arg, "var", 3)
1218 || checkforcmd(&arg, "final", 5)
1219 || checkforcmd(&arg, "const", 5)
1220 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001221 {
zeertzjqa93d9cd2023-05-02 16:25:47 +01001222 while (vim_strchr((char_u *)"$@&", *arg) != NULL)
1223 ++arg;
1224 arg = skipwhite(find_name_end(arg, NULL, NULL,
1225 FNE_INCL_BR | FNE_ALLOW_CURLY));
1226 if (vim9_function && *arg == ':')
1227 arg = skipwhite(skip_type(skipwhite(arg + 1), FALSE));
1228 if (arg[0] == '=' && arg[1] == '<' && arg[2] =='<')
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001229 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001230 p = skipwhite(arg + 3);
1231 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001232 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001233 if (STRNCMP(p, "trim", 4) == 0)
1234 {
1235 // Ignore leading white space.
1236 p = skipwhite(p + 4);
1237 heredoc_trimmed = vim_strnsave(theline,
1238 skipwhite(theline) - theline);
1239 continue;
1240 }
1241 if (STRNCMP(p, "eval", 4) == 0)
1242 {
1243 // Ignore leading white space.
1244 p = skipwhite(p + 4);
1245 continue;
1246 }
1247 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001248 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001249 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1250 getline_options = GETLINE_NONE;
1251 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001252 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001253 }
1254 }
1255 }
1256
1257 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001258 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001259 goto theend;
1260
Bram Moolenaar20677332021-06-06 17:02:53 +02001261 if (heredoc_concat_len > 0)
1262 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001263 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001264 // to be used for the instruction later.
1265 ga_concat(&heredoc_ga, theline);
1266 ga_concat(&heredoc_ga, (char_u *)"\n");
1267 p = vim_strsave((char_u *)"");
1268 }
1269 else
1270 {
1271 // Copy the line to newly allocated memory. get_one_sourceline()
1272 // allocates 250 bytes per line, this saves 80% on average. The
1273 // cost is an extra alloc/free.
1274 p = vim_strsave(theline);
1275 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001276 if (p == NULL)
1277 goto theend;
1278 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1279
1280 // Add NULL lines for continuation lines, so that the line count is
1281 // equal to the index in the growarray.
1282 while (sourcing_lnum_off-- > 0)
1283 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1284
1285 // Check for end of eap->arg.
1286 if (line_arg != NULL && *line_arg == NUL)
1287 line_arg = NULL;
1288 }
1289
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001290 // Return OK when no error was detected.
1291 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001292 ret = OK;
1293
1294theend:
1295 vim_free(skip_until);
1296 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001297 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001298 need_wait_return |= saved_wait_return;
1299 return ret;
1300}
1301
1302/*
1303 * Handle the body of a lambda. *arg points to the "{", process statements
1304 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001305 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001306 * When successful "rettv" is set to a funcref.
1307 */
1308 static int
1309lambda_function_body(
1310 char_u **arg,
1311 typval_T *rettv,
1312 evalarg_T *evalarg,
1313 garray_T *newargs,
1314 garray_T *argtypes,
1315 int varargs,
1316 garray_T *default_args,
1317 char_u *ret_type)
1318{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001319 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001320 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001321 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001322 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001323 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001324 exarg_T eap;
1325 garray_T newlines;
1326 char_u *cmdline = NULL;
1327 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001328 partial_T *pt;
1329 char_u *name;
1330 int lnum_save = -1;
1331 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1332
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001333 *arg = skipwhite(*arg + 1);
1334 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001335 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001336 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001337 return FAIL;
1338 }
1339
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001340 CLEAR_FIELD(eap);
1341 eap.cmdidx = CMD_block;
1342 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001343 eap.cmdlinep = &cmdline;
1344 eap.skip = !evaluate;
1345 if (evalarg->eval_cctx != NULL)
1346 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1347 else
1348 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001349 eap.ea_getline = evalarg->eval_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001350 eap.cookie = evalarg->eval_cookie;
1351 }
1352
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001353 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001354 if (get_function_body(&eap, &newlines, NULL,
1355 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001356 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001357
1358 // When inside a lambda must add the function lines to evalarg.eval_ga.
1359 evalarg->eval_break_count += newlines.ga_len;
1360 if (gap->ga_itemsize > 0)
1361 {
1362 int idx;
1363 char_u *last;
1364 size_t plen;
1365 char_u *pnl;
1366
1367 for (idx = 0; idx < newlines.ga_len; ++idx)
1368 {
1369 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1370
Bram Moolenaarecb66452021-05-18 15:09:18 +02001371 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001372 goto erret;
1373
1374 // Going to concatenate the lines after parsing. For an empty or
1375 // comment line use an empty string.
1376 // Insert NL characters at the start of each line, the string will
1377 // be split again later in .get_lambda_tv().
1378 if (*p == NUL || vim9_comment_start(p))
1379 p = (char_u *)"";
1380 plen = STRLEN(p);
1381 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1382 if (pnl != NULL)
1383 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001384 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1385 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001386 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001387 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001388 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001389 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001390 // more is following after the "}", which was skipped
1391 last = cmdline;
1392 else
1393 // nothing is following the "}"
1394 last = (char_u *)"}";
1395 plen = STRLEN(last);
1396 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1397 if (pnl != NULL)
1398 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001399 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1400 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001401 }
1402
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001403 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001404 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001405 garray_T *tfgap = &evalarg->eval_tofree_ga;
1406
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001407 // Something comes after the "}".
1408 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001409
1410 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001411 if (ga_grow(tfgap, 1) == OK)
1412 {
1413 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1414 evalarg->eval_using_cmdline = TRUE;
1415 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001416 }
1417 else
1418 *arg = (char_u *)"";
1419
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001420 if (!evaluate)
1421 {
1422 ret = OK;
1423 goto erret;
1424 }
1425
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001426 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001427 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001428 if (ufunc == NULL)
1429 goto erret;
1430 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001431 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001432 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001433 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001434 ufunc->uf_refcount = 1;
1435 ufunc->uf_args = *newargs;
1436 newargs->ga_data = NULL;
1437 ufunc->uf_def_args = *default_args;
1438 default_args->ga_data = NULL;
1439 ufunc->uf_func_type = &t_func_any;
1440
1441 // error messages are for the first function line
1442 lnum_save = SOURCING_LNUM;
1443 SOURCING_LNUM = sourcing_lnum_top;
1444
1445 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001446 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001447 {
1448 SOURCING_LNUM = lnum_save;
1449 goto erret;
1450 }
1451
1452 // parse the return type, if any
1453 if (parse_return_type(ufunc, ret_type) == FAIL)
1454 goto erret;
1455
1456 pt = ALLOC_CLEAR_ONE(partial_T);
1457 if (pt == NULL)
1458 goto erret;
1459 pt->pt_func = ufunc;
1460 pt->pt_refcount = 1;
1461
1462 ufunc->uf_lines = newlines;
1463 newlines.ga_data = NULL;
1464 if (sandbox)
1465 ufunc->uf_flags |= FC_SANDBOX;
1466 if (!ASCII_ISUPPER(*ufunc->uf_name))
1467 ufunc->uf_flags |= FC_VIM9;
1468 ufunc->uf_script_ctx = current_sctx;
1469 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1470 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1471 set_function_type(ufunc);
1472
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001473 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1474
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001475 rettv->vval.v_partial = pt;
1476 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001477 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001478 ret = OK;
1479
1480erret:
1481 if (lnum_save >= 0)
1482 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001483 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001484 if (newargs != NULL)
1485 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001486 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001487 if (ufunc != NULL)
1488 {
1489 func_clear(ufunc, TRUE);
1490 func_free(ufunc, TRUE);
1491 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001492 return ret;
1493}
1494
1495/*
1496 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001497 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001498 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001499 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1500 */
1501 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001502get_lambda_tv(
1503 char_u **arg,
1504 typval_T *rettv,
1505 int types_optional,
1506 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001507{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001508 int evaluate = evalarg != NULL
1509 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001510 garray_T newargs;
1511 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001512 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001513 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001514 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001515 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001516 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001517 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001518 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001519 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001520 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001521 char_u *s;
1522 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001523 int *old_eval_lavars = eval_lavars_used;
1524 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001525 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001526 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001527 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001528 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001529 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001530 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001531
Bram Moolenaar4525a572022-02-13 11:57:33 +00001532 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001533 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001534
1535 ga_init(&newargs);
1536 ga_init(&newlines);
1537
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001538 // First, check if this is really a lambda expression. "->" or "=>" must
1539 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001540 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001541 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001542 types_optional ? &argtypes : NULL, types_optional,
1543 types_optional ? &arg_objm : NULL, evalarg,
1544 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001545 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001546 {
1547 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001548 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001549 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001550 ga_clear(&arg_objm);
1551 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001552 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001553 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001554
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001555 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001556 if (evaluate)
1557 pnewargs = &newargs;
1558 else
1559 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001560 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001561 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001562 types_optional ? &argtypes : NULL, types_optional,
1563 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001564 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001565 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001566 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001567 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001568 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001569 {
1570 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001571 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001572 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001573 ga_clear(&arg_objm);
1574 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001575 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001576 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001577 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001578 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001579
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001580 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1581 if (ret_type != NULL)
1582 {
1583 ret_type = vim_strsave(ret_type);
1584 tofree2 = ret_type;
1585 }
1586
Bram Moolenaare38eab22019-12-05 21:50:01 +01001587 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001588 if (evaluate)
1589 eval_lavars_used = &eval_lavars;
1590
Bram Moolenaar65c44152020-12-24 15:14:01 +01001591 *arg = skipwhite_and_linebreak(*arg, evalarg);
1592
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001593 // Recognize "{" as the start of a function body.
1594 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001595 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001596 if (evalarg == NULL)
1597 // cannot happen?
1598 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001599 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001600 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1601 types_optional ? &argtypes : NULL, varargs,
1602 &default_args, ret_type) == FAIL)
1603 goto errret;
1604 goto theend;
1605 }
1606 if (default_args.ga_len > 0)
1607 {
1608 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001609 goto errret;
1610 }
1611
Bram Moolenaare38eab22019-12-05 21:50:01 +01001612 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001613 start = *arg;
1614 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001615 if (ret == FAIL)
1616 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001617
Bram Moolenaar65c44152020-12-24 15:14:01 +01001618 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001619 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001620 *arg = skipwhite_and_linebreak(*arg, evalarg);
1621 if (**arg != '}')
1622 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001623 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001624 goto errret;
1625 }
1626 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001627 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001628
1629 if (evaluate)
1630 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001631 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001632 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001633 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001634 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001635 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001636
Bram Moolenaare01e5212023-01-08 20:31:18 +00001637 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001638 if (fp == NULL)
1639 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001640 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001641 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001642 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001643 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001644
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001645 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001646 if (ga_grow(&newlines, 1) == FAIL)
1647 goto errret;
1648
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001649 // If there are line breaks, we need to split up the string.
1650 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001651 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001652 line_end = end;
1653
1654 // Add "return " before the expression (or the first line).
1655 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001656 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001657 if (p == NULL)
1658 goto errret;
1659 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1660 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001661 vim_strncpy(p + 7, start, line_end - start);
1662
1663 if (line_end != end)
1664 {
1665 // Add more lines, split by line breaks. Thus is used when a
1666 // lambda with { cmds } is encountered.
1667 while (*line_end == '\n')
1668 {
1669 if (ga_grow(&newlines, 1) == FAIL)
1670 goto errret;
1671 start = line_end + 1;
1672 line_end = vim_strchr(start, '\n');
1673 if (line_end == NULL)
1674 line_end = end;
1675 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1676 vim_strnsave(start, line_end - start);
1677 }
1678 }
1679
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001680 if (strstr((char *)p + 7, "a:") == NULL)
1681 // No a: variables are used for sure.
1682 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001683
1684 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001685 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001686 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001687 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001688 if (types_optional)
1689 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001690 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001691 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001692 goto errret;
1693 if (ret_type != NULL)
1694 {
1695 fp->uf_ret_type = parse_type(&ret_type,
1696 &fp->uf_type_list, TRUE);
1697 if (fp->uf_ret_type == NULL)
1698 goto errret;
1699 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001700 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001701 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001702 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001703
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001704 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001705 if (current_funccal != NULL && eval_lavars)
1706 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001707 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001708 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001709 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001710 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001711
1712#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001713 if (prof_def_func())
1714 func_do_profile(fp);
1715#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001716 if (sandbox)
1717 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001718 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001719 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001720 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001721 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001722 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001723 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001724 // Use the line number of the arguments.
1725 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001726
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001727 function_using_block_scopes(fp, evalarg->eval_cstack);
1728
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001729 pt->pt_func = fp;
1730 pt->pt_refcount = 1;
1731 rettv->vval.v_partial = pt;
1732 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001733
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001734 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001735 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001736
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001737theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001738 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001739 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001740 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001741 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001742 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001743 ga_clear(&arg_objm);
1744 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001745
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001746 return OK;
1747
1748errret:
1749 ga_clear_strings(&newargs);
1750 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001751 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001752 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001753 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001754 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001755 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001756 if (fp != NULL)
1757 vim_free(fp->uf_arg_types);
1758 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001759 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001760 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001761 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001762 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001763 return FAIL;
1764}
1765
1766/*
1767 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1768 * name it contains, otherwise return "name".
1769 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1770 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001771 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1772 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001773 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001774 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001775 */
1776 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001777deref_func_name(
1778 char_u *name,
1779 int *lenp,
1780 partial_T **partialp,
1781 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001782 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001783 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001784 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001785{
1786 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001787 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001789 char_u *s = NULL;
1790 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001791 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001792
1793 if (partialp != NULL)
1794 *partialp = NULL;
1795
1796 cc = name[*lenp];
1797 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001798
Bram Moolenaar71f21932022-01-07 18:20:55 +00001799 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001800 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001801 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001802 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001803 tv = &v->di_tv;
1804 }
1805 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1806 {
1807 imported_T *import;
1808 char_u *p = name;
1809 int len = *lenp;
1810
1811 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001812 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001813 p = name + 2;
1814 len -= 2;
1815 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001816 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001817
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001818 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001819 if (import != NULL)
1820 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001821 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001822 if (new_function)
1823 semsg(_(e_redefining_imported_item_str), name);
1824 else
1825 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001826 name[len] = cc;
1827 *lenp = 0;
1828 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001829 }
1830 }
1831
1832 if (tv != NULL)
1833 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001834 if (found_var != NULL)
1835 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001836 if (tv->v_type == VAR_FUNC)
1837 {
1838 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001839 {
1840 *lenp = 0;
1841 return (char_u *)""; // just in case
1842 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001843 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001844 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001845 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001846
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001847 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001848 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001849 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001850
1851 if (pt == NULL)
1852 {
1853 *lenp = 0;
1854 return (char_u *)""; // just in case
1855 }
1856 if (partialp != NULL)
1857 *partialp = pt;
1858 s = partial_name(pt);
1859 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001860 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001861
1862 if (s != NULL)
1863 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001864 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001865 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001866 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001867
1868 if (sv != NULL)
1869 *type = sv->sv_type;
1870 }
1871 return s;
1872 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001873 }
1874
1875 return name;
1876}
1877
1878/*
1879 * Give an error message with a function name. Handle <SNR> things.
1880 * "ermsg" is to be passed without translation, use N_() instead of _().
1881 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001882 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001883emsg_funcname(char *ermsg, char_u *name)
1884{
Bram Moolenaar54598062022-01-13 12:05:09 +00001885 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001886
Bram Moolenaar54598062022-01-13 12:05:09 +00001887 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001888 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001889 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001890 if (p != name)
1891 vim_free(p);
1892}
1893
1894/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001895 * Get function arguments at "*arg" and advance it.
1896 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001897 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001898 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001899 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001900get_func_arguments(
1901 char_u **arg,
1902 evalarg_T *evalarg,
1903 int partial_argc,
1904 typval_T *argvars,
Ernie Raelb077b582023-12-14 20:11:44 +01001905 int *argcount,
1906 int is_builtin)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001907{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001908 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001909 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001910 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001911 int evaluate = evalarg == NULL
1912 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001913
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001914 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001915 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001916 // skip the '(' or ',' and possibly line breaks
1917 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1918
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001919 if (*argp == ')' || *argp == ',' || *argp == NUL)
1920 break;
Ernie Raelb077b582023-12-14 20:11:44 +01001921
1922 int arg_idx = *argcount;
1923 if (eval1(&argp, &argvars[arg_idx], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001924 {
1925 ret = FAIL;
1926 break;
1927 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001928 ++*argcount;
Ernie Raelb077b582023-12-14 20:11:44 +01001929 if (!is_builtin && check_typval_is_value(&argvars[arg_idx]) == FAIL)
1930 {
1931 ret = FAIL;
1932 break;
1933 }
1934
Bram Moolenaar9d489562020-07-30 20:08:50 +02001935 // The comma should come right after the argument, but this wasn't
1936 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001937 if (vim9script)
1938 {
1939 if (*argp != ',' && *skipwhite(argp) == ',')
1940 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001941 if (evaluate)
1942 semsg(_(e_no_white_space_allowed_before_str_str),
1943 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001944 ret = FAIL;
1945 break;
1946 }
1947 }
1948 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001949 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001950 if (*argp != ',')
1951 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001952 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001953 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001954 if (evaluate)
1955 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001956 ret = FAIL;
1957 break;
1958 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001959 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001960
Bram Moolenaare6b53242020-07-01 17:28:33 +02001961 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001962 if (*argp == ')')
1963 ++argp;
1964 else
1965 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001966 *arg = argp;
1967 return ret;
1968}
1969
1970/*
1971 * Call a function and put the result in "rettv".
1972 * Return OK or FAIL.
1973 */
1974 int
1975get_func_tv(
1976 char_u *name, // name of the function
1977 int len, // length of "name" or -1 to use strlen()
1978 typval_T *rettv,
1979 char_u **arg, // argument, pointing to the '('
1980 evalarg_T *evalarg, // for line continuation
1981 funcexe_T *funcexe) // various values
1982{
1983 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001984 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001985 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1986 int argcount = 0; // number of arguments found
1987 int vim9script = in_vim9script();
1988 int evaluate = evalarg == NULL
1989 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1990
1991 argp = *arg;
1992 ret = get_func_arguments(&argp, evalarg,
1993 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
Ernie Raelb077b582023-12-14 20:11:44 +01001994 argvars, &argcount, builtin_function(name, -1));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001995
1996 if (ret == OK)
1997 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001998 int i = 0;
1999 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002000
2001 if (get_vim_var_nr(VV_TESTING))
2002 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002003 // Prepare for calling test_garbagecollect_now(), need to know
2004 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002005 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002006 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002007 for (i = 0; i < argcount; ++i)
2008 if (ga_grow(&funcargs, 1) == OK)
2009 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2010 &argvars[i];
2011 }
2012
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002013 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002014 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002015 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002016 // An error in a builtin function does not return FAIL, but we do
2017 // want to abort further processing if an error was given.
2018 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002019 clear_tv(rettv);
2020 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002021
2022 funcargs.ga_len -= i;
2023 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002024 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002025 {
2026 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002027 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002028 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002029 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002030 }
2031
2032 while (--argcount >= 0)
2033 clear_tv(&argvars[argcount]);
2034
Bram Moolenaar4525a572022-02-13 11:57:33 +00002035 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002036 *arg = argp;
2037 else
2038 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002039 return ret;
2040}
2041
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002042/*
2043 * Return TRUE if "p" starts with "<SID>" or "s:".
2044 * Only works if eval_fname_script() returned non-zero for "p"!
2045 */
2046 static int
2047eval_fname_sid(char_u *p)
2048{
2049 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2050}
2051
2052/*
2053 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2054 * Change <SNR>123_name() to K_SNR 123_name().
2055 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002056 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002057 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002058 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002059fname_trans_sid(
2060 char_u *name,
2061 char_u *fname_buf,
2062 char_u **tofree,
2063 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002064{
2065 int llen;
2066 char_u *fname;
2067 int i;
2068
2069 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002070 if (llen == 0)
2071 return name; // no prefix
2072
2073 fname_buf[0] = K_SPECIAL;
2074 fname_buf[1] = KS_EXTRA;
2075 fname_buf[2] = (int)KE_SNR;
2076 i = 3;
2077 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002078 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002079 if (current_sctx.sc_sid <= 0)
2080 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002081 else
2082 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002083 sprintf((char *)fname_buf + 3, "%ld_",
2084 (long)current_sctx.sc_sid);
2085 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002086 }
2087 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002088 if (i + STRLEN(name + llen) < FLEN_FIXED)
2089 {
2090 STRCPY(fname_buf + i, name + llen);
2091 fname = fname_buf;
2092 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002093 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002094 {
2095 fname = alloc(i + STRLEN(name + llen) + 1);
2096 if (fname == NULL)
2097 *error = FCERR_OTHER;
2098 else
2099 {
2100 *tofree = fname;
2101 mch_memmove(fname, fname_buf, (size_t)i);
2102 STRCPY(fname + i, name + llen);
2103 }
2104 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002105 return fname;
2106}
2107
2108/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002109 * Concatenate the script ID and function name into "<SNR>99_name".
2110 * "buffer" must have size MAX_FUNC_NAME_LEN.
2111 */
2112 void
2113func_name_with_sid(char_u *name, int sid, char_u *buffer)
2114{
2115 // A script-local function is stored as "<SNR>99_name".
2116 buffer[0] = K_SPECIAL;
2117 buffer[1] = KS_EXTRA;
2118 buffer[2] = (int)KE_SNR;
2119 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2120 (long)sid, name);
2121}
2122
2123/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 * Find a function "name" in script "sid".
2125 */
2126 static ufunc_T *
2127find_func_with_sid(char_u *name, int sid)
2128{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002129 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002130 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131
Bram Moolenaarb8822442022-01-11 15:24:05 +00002132 if (!SCRIPT_ID_VALID(sid))
2133 return NULL; // not in a script
2134
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002135 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 hi = hash_find(&func_hashtab, buffer);
2137 if (!HASHITEM_EMPTY(hi))
2138 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002139 return NULL;
2140}
2141
2142/*
2143 * Find a function "name" in script "sid" prefixing the autoload prefix.
2144 */
2145 static ufunc_T *
2146find_func_with_prefix(char_u *name, int sid)
2147{
2148 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002149 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002150 scriptitem_T *si;
2151
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002152 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002153 return NULL; // already has the prefix
2154 if (!SCRIPT_ID_VALID(sid))
2155 return NULL; // not in a script
2156 si = SCRIPT_ITEM(sid);
2157 if (si->sn_autoload_prefix != NULL)
2158 {
2159 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2160 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002161 char_u *namep;
2162
2163 // skip a "<SNR>99_" prefix
2164 namep = untrans_function_name(name);
2165 if (namep == NULL)
2166 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002167
2168 // An exported function in an autoload script is stored as
2169 // "dir#path#name".
2170 if (len < sizeof(buffer))
2171 auto_name = buffer;
2172 else
2173 auto_name = alloc(len);
2174 if (auto_name != NULL)
2175 {
2176 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002177 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002178 hi = hash_find(&func_hashtab, auto_name);
2179 if (auto_name != buffer)
2180 vim_free(auto_name);
2181 if (!HASHITEM_EMPTY(hi))
2182 return HI2UF(hi);
2183 }
2184 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185
2186 return NULL;
2187}
2188
2189/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002190 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002191 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2192 * functions.
2193 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002194 * Return NULL for unknown function.
2195 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002196 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002197find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002198{
2199 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002202 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002203 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002204 // Find script-local function before global one.
2205 if (in_vim9script() && eval_isnamec1(*name)
2206 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002208 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2209 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002210 if (func != NULL)
2211 return func;
2212 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002213 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2214 {
2215 char_u *p = name + 5;
2216 long sid;
2217
2218 // printable "<SNR>123_Name" form
2219 sid = getdigits(&p);
2220 if (*p == '_')
2221 {
2222 func = find_func_with_sid(p + 1, (int)sid);
2223 if (func != NULL)
2224 return func;
2225 }
2226 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002228
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002229 if ((flags & FFED_NO_GLOBAL) == 0)
2230 {
2231 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002232 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002233 if (!HASHITEM_EMPTY(hi))
2234 return HI2UF(hi);
2235 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236
Bram Moolenaarb8822442022-01-11 15:24:05 +00002237 // Find autoload function if this is an autoload script.
2238 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2239 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240}
2241
2242/*
2243 * Find a function by name, return pointer to it in ufuncs.
2244 * "cctx" is passed in a :def function to find imported functions.
2245 * Return NULL for unknown or dead function.
2246 */
2247 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002248find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002250 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251
2252 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2253 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002254 return NULL;
2255}
2256
2257/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002258 * Return TRUE if "ufunc" is a global function.
2259 */
2260 int
2261func_is_global(ufunc_T *ufunc)
2262{
2263 return ufunc->uf_name[0] != K_SPECIAL;
2264}
2265
2266/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002267 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2268 */
2269 int
2270func_requires_g_prefix(ufunc_T *ufunc)
2271{
2272 return ufunc->uf_name[0] != K_SPECIAL
2273 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002274 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01002275 && !SAFE_isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002276}
2277
2278/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002279 * Copy the function name of "fp" to buffer "buf".
2280 * "buf" must be able to hold the function name plus three bytes.
2281 * Takes care of script-local function names.
2282 */
2283 static void
2284cat_func_name(char_u *buf, ufunc_T *fp)
2285{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002286 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002287 {
2288 STRCPY(buf, "<SNR>");
2289 STRCAT(buf, fp->uf_name + 3);
2290 }
2291 else
2292 STRCPY(buf, fp->uf_name);
2293}
2294
2295/*
2296 * Add a number variable "name" to dict "dp" with value "nr".
2297 */
2298 static void
2299add_nr_var(
2300 dict_T *dp,
2301 dictitem_T *v,
2302 char *name,
2303 varnumber_T nr)
2304{
2305 STRCPY(v->di_key, name);
2306 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002307 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002308 v->di_tv.v_type = VAR_NUMBER;
2309 v->di_tv.v_lock = VAR_FIXED;
2310 v->di_tv.vval.v_number = nr;
2311}
2312
2313/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002314 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002315 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002316 static void
2317free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002318{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002319 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002320
Bram Moolenaarca16c602022-09-06 18:57:08 +01002321 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002322 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002323 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002324
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002325 // When garbage collecting a funccall_T may be freed before the
2326 // function that references it, clear its uf_scoped field.
2327 // The function may have been redefined and point to another
2328 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002329 if (fp != NULL && fp->uf_scoped == fc)
2330 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002331 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002332 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002333
Bram Moolenaarca16c602022-09-06 18:57:08 +01002334 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002335 vim_free(fc);
2336}
2337
2338/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002339 * Free "fc" and what it contains.
2340 * Can be called only when "fc" is kept beyond the period of it called,
2341 * i.e. after cleanup_function_call(fc).
2342 */
2343 static void
2344free_funccal_contents(funccall_T *fc)
2345{
2346 listitem_T *li;
2347
2348 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002349 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002350
2351 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002352 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002353
2354 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002355 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002356 clear_tv(&li->li_tv);
2357
2358 free_funccal(fc);
2359}
2360
2361/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002362 * Handle the last part of returning from a function: free the local hashtable.
2363 * Unless it is still in use by a closure.
2364 */
2365 static void
2366cleanup_function_call(funccall_T *fc)
2367{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002368 int may_free_fc = fc->fc_refcount <= 0;
2369 int free_fc = TRUE;
2370
Bram Moolenaarca16c602022-09-06 18:57:08 +01002371 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002372
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002373 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002374 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2375 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002376 else
2377 free_fc = FALSE;
2378
2379 // If the a:000 list and the l: and a: dicts are not referenced and
2380 // there is no closure using it, we can free the funccall_T and what's
2381 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002382 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2383 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002384 else
2385 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002386 int todo;
2387 hashitem_T *hi;
2388 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002389
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002390 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002391
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002392 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002393 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002394 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002395 {
2396 if (!HASHITEM_EMPTY(hi))
2397 {
2398 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002399 di = HI2DI(hi);
2400 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002401 }
2402 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002403 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002404
Bram Moolenaarca16c602022-09-06 18:57:08 +01002405 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2406 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002407 else
2408 {
2409 listitem_T *li;
2410
2411 free_fc = FALSE;
2412
2413 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002414 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002415 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002416 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002417
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002418 if (free_fc)
2419 free_funccal(fc);
2420 else
2421 {
2422 static int made_copy = 0;
2423
2424 // "fc" is still in use. This can happen when returning "a:000",
2425 // assigning "l:" to a global variable or defining a closure.
2426 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002427 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002428 previous_funccal = fc;
2429
2430 if (want_garbage_collect)
2431 // If garbage collector is ready, clear count.
2432 made_copy = 0;
2433 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002434 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002435 // We have made a lot of copies, worth 4 Mbyte. This can happen
2436 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002437 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002438 // too much memory.
2439 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002440 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002441 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002442 }
2443}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002444
2445/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002446 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2447 */
2448 static int
2449numbered_function(char_u *name)
2450{
Keith Thompson184f71c2024-01-04 21:19:04 +01002451 return SAFE_isdigit(*name)
2452 || (name[0] == 'g' && name[1] == ':' && SAFE_isdigit(name[2]));
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002453}
2454
2455/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002456 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002457 * 1. ordinary names, function defined with :function or :def;
2458 * can start with "<SNR>123_" literally or with K_SPECIAL.
2459 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002460 * For the first we only count the name stored in func_hashtab as a reference,
2461 * using function() does not count as a reference, because the function is
2462 * looked up by name.
2463 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002464 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002465func_name_refcount(char_u *name)
2466{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002467 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002468}
2469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470/*
2471 * Unreference "fc": decrement the reference count and free it when it
2472 * becomes zero. "fp" is detached from "fc".
2473 * When "force" is TRUE we are exiting.
2474 */
2475 static void
2476funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2477{
2478 funccall_T **pfc;
2479 int i;
2480
2481 if (fc == NULL)
2482 return;
2483
2484 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002485 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2486 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2487 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2488 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 {
2490 if (fc == *pfc)
2491 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002492 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 free_funccal_contents(fc);
2494 return;
2495 }
2496 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002497 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2498 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2499 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500}
2501
2502/*
2503 * Remove the function from the function hashtable. If the function was
2504 * deleted while it still has references this was already done.
2505 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2506 */
2507 static int
2508func_remove(ufunc_T *fp)
2509{
2510 hashitem_T *hi;
2511
2512 // Return if it was already virtually deleted.
2513 if (fp->uf_flags & FC_DEAD)
2514 return FALSE;
2515
2516 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002517 if (HASHITEM_EMPTY(hi))
2518 return FALSE;
2519
2520 // When there is a def-function index do not actually remove the
2521 // function, so we can find the index when defining the function again.
2522 // Do remove it when it's a copy.
2523 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002525 fp->uf_flags |= FC_DEAD;
2526 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002528 hash_remove(&func_hashtab, hi, "remove function");
2529 fp->uf_flags |= FC_DELETED;
2530 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531}
2532
2533 static void
2534func_clear_items(ufunc_T *fp)
2535{
2536 ga_clear_strings(&(fp->uf_args));
2537 ga_clear_strings(&(fp->uf_def_args));
2538 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002540 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002541 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002542 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002543
2544 // Increment the refcount of this function to avoid it being freed
2545 // recursively when the partial is freed.
2546 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002547 partial_unref(fp->uf_partial);
2548 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002549 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002550
2551#ifdef FEAT_LUA
2552 if (fp->uf_cb_free != NULL)
2553 {
2554 fp->uf_cb_free(fp->uf_cb_state);
2555 fp->uf_cb_free = NULL;
2556 }
2557
2558 fp->uf_cb_state = NULL;
2559 fp->uf_cb = NULL;
2560#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561#ifdef FEAT_PROFILE
2562 VIM_CLEAR(fp->uf_tml_count);
2563 VIM_CLEAR(fp->uf_tml_total);
2564 VIM_CLEAR(fp->uf_tml_self);
2565#endif
2566}
2567
2568/*
2569 * Free all things that a function contains. Does not free the function
2570 * itself, use func_free() for that.
2571 * When "force" is TRUE we are exiting.
2572 */
2573 static void
2574func_clear(ufunc_T *fp, int force)
2575{
2576 if (fp->uf_cleared)
2577 return;
2578 fp->uf_cleared = TRUE;
2579
2580 // clear this function
2581 func_clear_items(fp);
2582 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002583 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584}
2585
2586/*
2587 * Free a function and remove it from the list of functions. Does not free
2588 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002589 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002590 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002592 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002593func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594{
2595 // Only remove it when not done already, otherwise we would remove a newer
2596 // version of the function with the same name.
2597 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2598 func_remove(fp);
2599
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002600 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002601 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002602 if (fp->uf_dfunc_idx > 0)
2603 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002604 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002606 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002607 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002608 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609}
2610
2611/*
2612 * Free all things that a function contains and free the function itself.
2613 * When "force" is TRUE we are exiting.
2614 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002615 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616func_clear_free(ufunc_T *fp, int force)
2617{
2618 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002619 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2620 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002621 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002622 else
2623 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624}
2625
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002626/*
2627 * Copy already defined function "lambda" to a new function with name "global".
2628 * This is for when a compiled function defines a global function.
2629 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002630 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002631copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002632 char_u *lambda,
2633 char_u *global,
2634 loopvarinfo_T *loopvarinfo,
2635 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002636{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002637 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002638 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002639
2640 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002641 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002642 semsg(_(e_lambda_function_not_found_str), lambda);
2643 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002644 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002645
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002646 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002647 if (fp != NULL)
2648 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002649 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002650 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002651 return FAIL;
2652 }
2653
Bram Moolenaare01e5212023-01-08 20:31:18 +00002654 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002655 if (fp == NULL)
2656 return FAIL;
2657
2658 fp->uf_varargs = ufunc->uf_varargs;
2659 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2660 fp->uf_def_status = ufunc->uf_def_status;
2661 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2662 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2663 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2664 == FAIL
2665 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2666 goto failed;
2667
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002668 if (ufunc->uf_arg_types != NULL)
2669 {
2670 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2671 if (fp->uf_arg_types == NULL)
2672 goto failed;
2673 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2674 sizeof(type_T *) * fp->uf_args.ga_len);
2675 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002676 if (ufunc->uf_va_name != NULL)
2677 {
2678 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2679 if (fp->uf_va_name == NULL)
2680 goto failed;
2681 }
2682 fp->uf_ret_type = ufunc->uf_ret_type;
2683
2684 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002685
2686 fp->uf_name_exp = NULL;
2687 set_ufunc_name(fp, global);
2688
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002689 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002690
2691 // the referenced dfunc_T is now used one more time
2692 link_def_function(fp);
2693
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002694 // Create a partial to store the context of the function where it was
2695 // instantiated. Only needs to be done once. Do this on the original
2696 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002697 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2698 {
2699 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2700
2701 if (pt == NULL)
2702 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002703 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002704 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002705 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002706 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002707 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002708 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002709 }
2710
2711 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002712
2713failed:
2714 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002715 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002716}
2717
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002718static int funcdepth = 0;
2719
2720/*
2721 * Increment the function call depth count.
2722 * Return FAIL when going over 'maxfuncdepth'.
2723 * Otherwise return OK, must call funcdepth_decrement() later!
2724 */
2725 int
2726funcdepth_increment(void)
2727{
2728 if (funcdepth >= p_mfd)
2729 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002730 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002731 return FAIL;
2732 }
2733 ++funcdepth;
2734 return OK;
2735}
2736
2737 void
2738funcdepth_decrement(void)
2739{
2740 --funcdepth;
2741}
2742
2743/*
2744 * Get the current function call depth.
2745 */
2746 int
2747funcdepth_get(void)
2748{
2749 return funcdepth;
2750}
2751
2752/*
2753 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002754 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002755 * times as funcdepth_increment().
2756 */
2757 void
2758funcdepth_restore(int depth)
2759{
2760 funcdepth = depth;
2761}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002762
2763/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002764 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2765 * "rettv".
2766 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2767 * Returns NULL when allocation fails.
2768 */
2769 funccall_T *
2770create_funccal(ufunc_T *fp, typval_T *rettv)
2771{
2772 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2773
2774 if (fc == NULL)
2775 return NULL;
2776 fc->fc_caller = current_funccal;
2777 current_funccal = fc;
2778 fc->fc_func = fp;
2779 func_ptr_ref(fp);
2780 fc->fc_rettv = rettv;
2781 return fc;
2782}
2783
2784/*
2785 * To be called when returning from a compiled function; restores
2786 * current_funccal.
2787 */
2788 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002789remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002790{
2791 funccall_T *fc = current_funccal;
2792
2793 current_funccal = fc->fc_caller;
2794 free_funccal(fc);
2795}
2796
2797/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002798 * Call a user function.
2799 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002800 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002801call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002802 ufunc_T *fp, // pointer to function
2803 int argcount, // nr of args
2804 typval_T *argvars, // arguments
2805 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002806 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002807 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002808{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002809 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002810 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002811 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002812 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002813 funccall_T *fc;
2814 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002815 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002816 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002817 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002818 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002819 int i;
2820 int ai;
2821 int islambda = FALSE;
2822 char_u numbuf[NUMBUFLEN];
2823 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002824 typval_T *tv_to_free[MAX_FUNC_ARGS];
2825 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002826#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002827 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002828#endif
ichizok7e5fe382023-04-15 13:17:50 +01002829 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002830
Bram Moolenaarb2049902021-01-24 12:53:53 +01002831#ifdef FEAT_PROFILE
2832 CLEAR_FIELD(profile_info);
2833#endif
2834
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002835 // If depth of calling is getting too high, don't execute the function.
2836 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002837 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002838 rettv->v_type = VAR_NUMBER;
2839 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002840 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002841 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002842
Bram Moolenaare38eab22019-12-05 21:50:01 +01002843 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002844
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002845 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002846 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002847 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002848 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002849 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002850 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2851 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002852 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002853 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002854
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002855 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002857#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002858 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002859#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002860 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002861#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002862 if (do_profiling == PROF_YES)
2863 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002864#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002865 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002866 if (call_def_function(fp, argcount, argvars, 0,
2867 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2868 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002869 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002870#ifdef FEAT_PROFILE
2871 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002872 || (caller != NULL && caller->uf_profiling)))
2873 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002874#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002875 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002876 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002877 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 }
2879
Bram Moolenaar38453522021-11-28 22:00:12 +00002880 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002881
2882 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002883 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2884 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2885 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002886 */
2887 /*
2888 * Init l: variables.
2889 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002890 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002891 if (selfdict != NULL)
2892 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002893 // Set l:self to "selfdict". Use "name" to avoid a warning from
2894 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002895 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002896 name = v->di_key;
2897 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002898 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002899 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002900 v->di_tv.v_type = VAR_DICT;
2901 v->di_tv.v_lock = 0;
2902 v->di_tv.vval.v_dict = selfdict;
2903 ++selfdict->dv_refcount;
2904 }
2905
2906 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002907 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002908 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002909 * Set a:000 to a list with room for the "..." arguments.
2910 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002911 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002912 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002913 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002914 (varnumber_T)(argcount >= fp->uf_args.ga_len
2915 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002916 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002917 if ((fp->uf_flags & FC_NOARGS) == 0)
2918 {
2919 // Use "name" to avoid a warning from some compiler that checks the
2920 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002921 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002922 name = v->di_key;
2923 STRCPY(name, "000");
2924 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002925 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002926 v->di_tv.v_type = VAR_LIST;
2927 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002928 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002929 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002930 CLEAR_FIELD(fc->fc_l_varlist);
2931 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2932 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002933
2934 /*
2935 * Set a:firstline to "firstline" and a:lastline to "lastline".
2936 * Set a:name to named arguments.
2937 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002938 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002939 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002940 if ((fp->uf_flags & FC_NOARGS) == 0)
2941 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002942 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2943 "firstline", (varnumber_T)funcexe->fe_firstline);
2944 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2945 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002946 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002947 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002948 {
2949 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002950 typval_T def_rettv;
2951 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002952
2953 ai = i - fp->uf_args.ga_len;
2954 if (ai < 0)
2955 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002956 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002957 name = FUNCARG(fp, i);
2958 if (islambda)
2959 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002960
2961 // evaluate named argument default expression
2962 isdefault = ai + fp->uf_def_args.ga_len >= 0
2963 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2964 && argvars[i].vval.v_number == VVAL_NONE));
2965 if (isdefault)
2966 {
2967 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002968
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002969 def_rettv.v_type = VAR_NUMBER;
2970 def_rettv.vval.v_number = -1;
2971
2972 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2973 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002974 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002975 {
2976 default_arg_err = 1;
2977 break;
2978 }
2979 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002980 }
2981 else
2982 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002983 if ((fp->uf_flags & FC_NOARGS) != 0)
2984 // Bail out if no a: arguments used (in lambda).
2985 break;
2986
Bram Moolenaare38eab22019-12-05 21:50:01 +01002987 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002988 sprintf((char *)numbuf, "%d", ai + 1);
2989 name = numbuf;
2990 }
2991 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2992 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002993 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002995 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002996 }
2997 else
2998 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002999 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003000 if (v == NULL)
3001 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003002 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003003 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003004
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003005 // Note: the values are copied directly to avoid alloc/free.
3006 // "argvars" must have VAR_FIXED for v_lock.
3007 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003008 v->di_tv.v_lock = VAR_FIXED;
3009
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003010 if (isdefault)
3011 // Need to free this later, no matter where it's stored.
3012 tv_to_free[tv_to_free_len++] = &v->di_tv;
3013
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003014 if (addlocal)
3015 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003016 // Named arguments should be accessed without the "a:" prefix in
3017 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003018 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003019 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003020 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003021 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003022 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003023
3024 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3025 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003026 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003027
3028 li->li_tv = argvars[i];
3029 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003030 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003031 }
3032 }
3033
Bram Moolenaare38eab22019-12-05 21:50:01 +01003034 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003035 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003036
3037 if (fp->uf_flags & FC_SANDBOX)
3038 {
3039 using_sandbox = TRUE;
3040 ++sandbox;
3041 }
3042
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003043 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003044 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003045 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003046 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003047 ++no_wait_return;
3048 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003049
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003050 smsg(_("calling %s"), SOURCING_NAME);
3051 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003052 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003053 char_u buf[MSG_BUF_LEN];
3054 char_u numbuf2[NUMBUFLEN];
3055 char_u *tofree;
3056 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003057
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003058 msg_puts("(");
3059 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003060 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003061 if (i > 0)
3062 msg_puts(", ");
3063 if (argvars[i].v_type == VAR_NUMBER)
3064 msg_outnum((long)argvars[i].vval.v_number);
3065 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003066 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003067 // Do not want errors such as E724 here.
3068 ++emsg_off;
3069 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3070 --emsg_off;
3071 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003072 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003073 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003074 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003075 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3076 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003077 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003078 msg_puts((char *)s);
3079 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003080 }
3081 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003082 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003083 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003084 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003085 msg_puts("\n"); // don't overwrite this either
3086
3087 verbose_leave_scroll();
3088 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003089 }
3090#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003091 if (do_profiling == PROF_YES)
3092 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003093 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003094#endif
3095
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003096 // "legacy" does not apply to commands in the function
3097 sticky_cmdmod_flags = 0;
3098
Bram Moolenaar98aff652022-09-06 21:02:35 +01003099 // If called from a compiled :def function the execution context must be
3100 // hidden, any deferred functions need to be added to the function being
3101 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003102 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003103
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003104 save_current_sctx = current_sctx;
3105 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003106 save_did_emsg = did_emsg;
3107 did_emsg = FALSE;
3108
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003109 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003110 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003111 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003112 retval = FCERR_FAILED;
3113 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003114 else if (islambda)
3115 {
3116 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3117
3118 // A Lambda always has the command "return {expr}". It is much faster
3119 // to evaluate {expr} directly.
3120 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003121 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003122 --ex_nesting_level;
3123 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003124 else
3125 // call do_cmdline() to execute the lines
3126 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003127 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3128
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003129 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003130 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003131
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003132 if (RedrawingDisabled > 0)
3133 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003134
Bram Moolenaare38eab22019-12-05 21:50:01 +01003135 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003136 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3137 {
3138 clear_tv(rettv);
3139 rettv->v_type = VAR_NUMBER;
3140 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003141
3142 // In corner cases returning a "failed" value is not backwards
3143 // compatible. Only do this for Vim9 script.
3144 if (in_vim9script())
3145 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003146 }
3147
3148#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003149 if (do_profiling == PROF_YES)
3150 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003151 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003152
3153 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3154 profile_may_end_func(&profile_info, fp, caller);
3155 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156#endif
3157
Bram Moolenaare38eab22019-12-05 21:50:01 +01003158 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003159 if (p_verbose >= 12)
3160 {
3161 ++no_wait_return;
3162 verbose_enter_scroll();
3163
3164 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003165 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003166 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003167 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003168 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003169 else
3170 {
3171 char_u buf[MSG_BUF_LEN];
3172 char_u numbuf2[NUMBUFLEN];
3173 char_u *tofree;
3174 char_u *s;
3175
Bram Moolenaare38eab22019-12-05 21:50:01 +01003176 // The value may be very long. Skip the middle part, so that we
3177 // have some idea how it starts and ends. smsg() would always
3178 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003179 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003180 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003181 --emsg_off;
3182 if (s != NULL)
3183 {
3184 if (vim_strsize(s) > MSG_BUF_CLEN)
3185 {
3186 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3187 s = buf;
3188 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003189 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003190 vim_free(tofree);
3191 }
3192 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003193 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003194
3195 verbose_leave_scroll();
3196 --no_wait_return;
3197 }
3198
ichizok7e5fe382023-04-15 13:17:50 +01003199 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003200 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003201 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003202 restore_current_ectx(save_current_ectx);
3203
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003204#ifdef FEAT_PROFILE
3205 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003206 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003207#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003208 if (using_sandbox)
3209 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003210 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003211
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003212 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003213 {
3214 ++no_wait_return;
3215 verbose_enter_scroll();
3216
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003217 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003218 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003219
3220 verbose_leave_scroll();
3221 --no_wait_return;
3222 }
3223
3224 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003225 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003226 for (i = 0; i < tv_to_free_len; ++i)
3227 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003228 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003229
3230 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003231}
3232
3233/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003234 * Check the argument count for user function "fp".
3235 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3236 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003237 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003238check_user_func_argcount(ufunc_T *fp, int argcount)
3239{
3240 int regular_args = fp->uf_args.ga_len;
3241
3242 if (argcount < regular_args - fp->uf_def_args.ga_len)
3243 return FCERR_TOOFEW;
3244 else if (!has_varargs(fp) && argcount > regular_args)
3245 return FCERR_TOOMANY;
3246 return FCERR_UNKNOWN;
3247}
3248
3249/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003251 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003252 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253call_user_func_check(
3254 ufunc_T *fp,
3255 int argcount,
3256 typval_T *argvars,
3257 typval_T *rettv,
3258 funcexe_T *funcexe,
3259 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003260{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003261 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003262
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003263#ifdef FEAT_LUA
3264 if (fp->uf_flags & FC_CFUNC)
3265 {
3266 cfunc_T cb = fp->uf_cb;
3267
3268 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3269 }
3270#endif
3271
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003272 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3273 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003274 error = check_user_func_argcount(fp, argcount);
3275 if (error != FCERR_UNKNOWN)
3276 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003277
Bram Moolenaar50824712020-12-20 21:10:17 +01003278 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003279 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003281 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003283 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 int did_save_redo = FALSE;
3285 save_redo_T save_redo;
3286
3287 /*
3288 * Call the user function.
3289 * Save and restore search patterns, script variables and
3290 * redo buffer.
3291 */
3292 save_search_patterns();
3293 if (!ins_compl_active())
3294 {
3295 saveRedobuff(&save_redo);
3296 did_save_redo = TRUE;
3297 }
3298 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003299 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003300 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3302 // Function was unreferenced while being used, free it now.
3303 func_clear_free(fp, FALSE);
3304 if (did_save_redo)
3305 restoreRedobuff(&save_redo);
3306 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003307 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003308
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003310}
3311
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003312static funccal_entry_T *funccal_stack = NULL;
3313
3314/*
3315 * Save the current function call pointer, and set it to NULL.
3316 * Used when executing autocommands and for ":source".
3317 */
3318 void
3319save_funccal(funccal_entry_T *entry)
3320{
3321 entry->top_funccal = current_funccal;
3322 entry->next = funccal_stack;
3323 funccal_stack = entry;
3324 current_funccal = NULL;
3325}
3326
3327 void
3328restore_funccal(void)
3329{
3330 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003331 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003332 else
3333 {
3334 current_funccal = funccal_stack->top_funccal;
3335 funccal_stack = funccal_stack->next;
3336 }
3337}
3338
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003339 funccall_T *
3340get_current_funccal(void)
3341{
3342 return current_funccal;
3343}
3344
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003345/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003346 * Return TRUE when currently at the script level:
3347 * - not in a function
3348 * - not executing an autocommand
3349 * Note that when an autocommand sources a script the result is FALSE;
3350 */
3351 int
3352at_script_level(void)
3353{
3354 return current_funccal == NULL && autocmd_match == NULL;
3355}
3356
3357/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003358 * Mark all functions of script "sid" as deleted.
3359 */
3360 void
3361delete_script_functions(int sid)
3362{
3363 hashitem_T *hi;
3364 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003365 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003366 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003367 size_t len;
3368
3369 buf[0] = K_SPECIAL;
3370 buf[1] = KS_EXTRA;
3371 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003372 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003373 len = STRLEN(buf);
3374
Bram Moolenaarce658352020-07-31 23:47:12 +02003375 while (todo > 0)
3376 {
3377 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003378 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003379 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003380 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003381 fp = HI2UF(hi);
3382 if (STRNCMP(fp->uf_name, buf, len) == 0)
3383 {
3384 int changed = func_hashtab.ht_changed;
3385
3386 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003387
3388 if (fp->uf_calls > 0)
3389 {
3390 // Function is executing, don't free it but do remove
3391 // it from the hashtable.
3392 if (func_remove(fp))
3393 fp->uf_refcount--;
3394 }
3395 else
3396 {
3397 func_clear(fp, TRUE);
3398 // When clearing a function another function can be
3399 // cleared as a side effect. When that happens start
3400 // over.
3401 if (changed != func_hashtab.ht_changed)
3402 break;
3403 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003404 }
3405 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003406 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003407 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003408}
3409
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003410#if defined(EXITFREE) || defined(PROTO)
3411 void
3412free_all_functions(void)
3413{
3414 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003415 ufunc_T *fp;
3416 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003417 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003418 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003419
Bram Moolenaare38eab22019-12-05 21:50:01 +01003420 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003421 while (current_funccal != NULL)
3422 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003423 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003424 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003425 if (current_funccal == NULL && funccal_stack != NULL)
3426 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003427 }
3428
Bram Moolenaare38eab22019-12-05 21:50:01 +01003429 // First clear what the functions contain. Since this may lower the
3430 // reference count of a function, it may also free a function and change
3431 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003432 while (todo > 0)
3433 {
3434 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003435 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003436 if (!HASHITEM_EMPTY(hi))
3437 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 // clear the def function index now
3439 fp = HI2UF(hi);
3440 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003441 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442
Bram Moolenaare38eab22019-12-05 21:50:01 +01003443 // Only free functions that are not refcounted, those are
3444 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003445 if (func_name_refcount(fp->uf_name))
3446 ++skipped;
3447 else
3448 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003449 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003450 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003451 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003452 {
3453 skipped = 0;
3454 break;
3455 }
3456 }
3457 --todo;
3458 }
3459 }
3460
Bram Moolenaare38eab22019-12-05 21:50:01 +01003461 // Now actually free the functions. Need to start all over every time,
3462 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003463 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003464 while (func_hashtab.ht_used > skipped)
3465 {
3466 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003467 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003468 if (!HASHITEM_EMPTY(hi))
3469 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003470 --todo;
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 Moolenaarc2574872016-08-11 22:51:05 +02003473 fp = HI2UF(hi);
3474 if (func_name_refcount(fp->uf_name))
3475 ++skipped;
3476 else
3477 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003478 if (func_free(fp, FALSE) == OK)
3479 {
3480 skipped = 0;
3481 break;
3482 }
3483 // did not actually free it
3484 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003485 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003486 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003487 }
3488 if (skipped == 0)
3489 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490
3491 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003492}
3493#endif
3494
3495/*
3496 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003497 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3498 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003499 * "len" is the length of "name", or -1 for NUL terminated.
3500 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003502builtin_function(char_u *name, int len)
3503{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003504 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003505
Bram Moolenaara26b9702020-04-18 19:53:28 +02003506 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003507 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003508 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3509 {
3510 if (name[i] == AUTOLOAD_CHAR)
3511 return FALSE;
3512 if (!eval_isnamec(name[i]))
3513 {
3514 // "name.something" is not a builtin function
3515 if (name[i] == '.')
3516 return FALSE;
3517 break;
3518 }
3519 }
3520 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003521}
3522
3523 int
3524func_call(
3525 char_u *name,
3526 typval_T *args,
3527 partial_T *partial,
3528 dict_T *selfdict,
3529 typval_T *rettv)
3530{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003531 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003532 listitem_T *item;
3533 typval_T argv[MAX_FUNC_ARGS + 1];
3534 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003535 int r = 0;
3536
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003537 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003538 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003539 {
3540 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3541 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003542 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003543 break;
3544 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003545 // Make a copy of each argument. This is needed to be able to set
3546 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003547 copy_tv(&item->li_tv, &argv[argc++]);
3548 }
3549
3550 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003551 {
3552 funcexe_T funcexe;
3553
Bram Moolenaara80faa82020-04-12 19:37:17 +02003554 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003555 funcexe.fe_firstline = curwin->w_cursor.lnum;
3556 funcexe.fe_lastline = curwin->w_cursor.lnum;
3557 funcexe.fe_evaluate = TRUE;
3558 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003559 if (partial != NULL)
3560 {
3561 funcexe.fe_object = partial->pt_obj;
3562 if (funcexe.fe_object != NULL)
3563 ++funcexe.fe_object->obj_refcount;
3564 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003565 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003566 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3567 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003568
Bram Moolenaare38eab22019-12-05 21:50:01 +01003569 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003570 while (argc > 0)
3571 clear_tv(&argv[--argc]);
3572
3573 return r;
3574}
3575
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003576static int callback_depth = 0;
3577
3578 int
3579get_callback_depth(void)
3580{
3581 return callback_depth;
3582}
3583
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003584/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003585 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003586 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003587 */
3588 int
3589call_callback(
3590 callback_T *callback,
3591 int len, // length of "name" or -1 to use strlen()
3592 typval_T *rettv, // return value goes here
3593 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003594 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003595 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003596{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003597 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003598 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003599
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003600 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3601 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003602
zeertzjqfe583b12023-12-21 16:59:26 +01003603 if (callback_depth > p_mfd)
Christian Brabandt47510f32023-10-15 09:56:16 +02003604 {
3605 emsg(_(e_command_too_recursive));
3606 return FAIL;
3607 }
3608
Bram Moolenaara80faa82020-04-12 19:37:17 +02003609 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003610 funcexe.fe_evaluate = TRUE;
3611 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003612 if (callback->cb_partial != NULL)
3613 {
3614 funcexe.fe_object = callback->cb_partial->pt_obj;
3615 if (funcexe.fe_object != NULL)
3616 ++funcexe.fe_object->obj_refcount;
3617 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003618 ++callback_depth;
3619 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3620 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003621
3622 // When a :def function was called that uses :try an error would be turned
3623 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003624 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003625 {
3626 need_rethrow = FALSE;
3627 handle_did_throw();
3628 }
3629
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003630 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003631}
3632
3633/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003634 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003635 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003636 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3637 */
3638 varnumber_T
3639call_callback_retnr(
3640 callback_T *callback,
3641 int argcount, // number of "argvars"
3642 typval_T *argvars) // vars for arguments, must have "argcount"
3643 // PLUS ONE elements!
3644{
3645 typval_T rettv;
3646 varnumber_T retval;
3647
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003648 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003649 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003650
3651 retval = tv_get_number_chk(&rettv, NULL);
3652 clear_tv(&rettv);
3653 return retval;
3654}
3655
3656/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 * Give an error message for the result of a function.
3658 * Nothing if "error" is FCERR_NONE.
3659 */
3660 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003661user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662{
3663 switch (error)
3664 {
3665 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003666 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003667 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003668 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003669 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 break;
3671 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003672 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 break;
3674 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003675 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 break;
3677 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003678 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 break;
3680 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003681 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 break;
3683 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003684 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 break;
3686 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003687 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3688 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003690 case FCERR_OTHER:
3691 case FCERR_FAILED:
3692 // assume the error message was already given
3693 break;
3694 case FCERR_NONE:
3695 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 }
3697}
3698
3699/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003700 * Check the argument types "argvars[argcount]" for "name" using the
3701 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3702 * is already included in "argvars[]".
3703 * Will do nothing if "funcexe->fe_check_type" is NULL or
3704 * "funcexe->fe_evaluate" is FALSE;
3705 * Returns an FCERR_ value.
3706 */
3707 static funcerror_T
3708may_check_argument_types(
3709 funcexe_T *funcexe,
3710 typval_T *argvars,
3711 int argcount,
3712 int base_included,
3713 char_u *name)
3714{
3715 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3716 {
3717 // Check that the argument types are OK for the types of the funcref.
3718 if (check_argument_types(funcexe->fe_check_type,
3719 argvars, argcount,
3720 base_included ? NULL : funcexe->fe_basetv,
3721 name) == FAIL)
3722 return FCERR_OTHER;
3723 }
3724 return FCERR_NONE;
3725}
3726
3727/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003728 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003729 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003730 * Return FAIL when the function can't be called, OK otherwise.
3731 * Also returns OK when an error was encountered while executing the function.
3732 */
3733 int
3734call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003735 char_u *funcname, // name of the function
3736 int len, // length of "name" or -1 to use strlen()
3737 typval_T *rettv, // return value goes here
3738 int argcount_in, // number of "argvars"
3739 typval_T *argvars_in, // vars for arguments, must have "argcount"
3740 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003741 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003742{
3743 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003744 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003745 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003746 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003747 char_u fname_buf[FLEN_FIXED + 1];
3748 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003749 char_u *fname = NULL;
3750 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003751 int argcount = argcount_in;
3752 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003753 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003754 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003755 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003756 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003757 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003758 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003759 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003760 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003761
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003762 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3763 // even when call_func() returns FAIL.
3764 rettv->v_type = VAR_UNKNOWN;
3765
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003766 if (partial != NULL)
3767 fp = partial->pt_func;
3768 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003769 fp = funcexe->fe_ufunc;
3770
3771 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003772 {
3773 // Make a copy of the name, if it comes from a funcref variable it
3774 // could be changed or deleted in the called function.
3775 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3776 if (name == NULL)
3777 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003778
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003779 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3780 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003781
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003782 if (funcexe->fe_doesrange != NULL)
3783 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003784
3785 if (partial != NULL)
3786 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003787 // When the function has a partial with a dict and there is a dict
3788 // argument, use the dict argument. That is backwards compatible.
3789 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003790 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003791 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003792 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003793 {
3794 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003795 {
3796 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3797 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003798 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003799 goto theend;
3800 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003801 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003802 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003803 for (i = 0; i < argcount_in; ++i)
3804 argv[i + argv_clear] = argvars_in[i];
3805 argvars = argv;
3806 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003807
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003808 if (funcexe->fe_check_type != NULL
3809 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003810 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003811 // Now funcexe->fe_check_type is missing the added arguments,
3812 // make a copy of the type with the correction.
3813 check_type = *funcexe->fe_check_type;
3814 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003815 check_type.tt_args = check_type_args;
3816 CLEAR_FIELD(check_type_args);
3817 for (i = 0; i < check_type.tt_argcount; ++i)
3818 check_type_args[i + partial->pt_argc] =
3819 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003820 check_type.tt_argcount += partial->pt_argc;
3821 check_type.tt_min_argcount += partial->pt_argc;
3822 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003823 }
3824 }
3825
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003826 if (error == FCERR_NONE)
3827 // check the argument types if possible
3828 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3829 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003830
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003831 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003832 {
3833 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003834 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003835
Bram Moolenaar333894b2020-08-01 18:53:07 +02003836 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003837 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003838 {
3839 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003840 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003841 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003842
Bram Moolenaare38eab22019-12-05 21:50:01 +01003843 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003844 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003845 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003846
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003847 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003848 {
3849 /*
3850 * User defined function.
3851 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003852 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003853 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003854 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003855 if (fp != NULL && !is_global && in_vim9script()
3856 && func_requires_g_prefix(fp))
3857 // In Vim9 script g: is required to find a global
3858 // non-autoload function.
3859 fp = NULL;
3860 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003861
Bram Moolenaare38eab22019-12-05 21:50:01 +01003862 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003863 if (fp == NULL
3864 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003865 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003866 && !aborting())
3867 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003868 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003869 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003870 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003871 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003872 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3873 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003874 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003875 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003876 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003877 if (fp == NULL)
3878 {
3879 char_u *p = untrans_function_name(rfname);
3880
3881 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003882 // Don't do this if the name starts with "s:".
3883 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003884 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003885 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003886
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003887 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003888 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003889 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003890 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003891 int need_arg_check = FALSE;
3892 if (funcexe->fe_check_type == NULL)
3893 {
3894 funcexe->fe_check_type = fp->uf_func_type;
3895 need_arg_check = TRUE;
3896 }
3897
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003898 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003899 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003900 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003901 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003902 argv_clear, fp);
3903 need_arg_check = TRUE;
3904 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003905
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003906 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003907 {
3908 // Method call: base->Method()
3909 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003910 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003911 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003912 argvars = argv;
3913 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003914 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003915 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003916
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003917 // Check the argument types now that the function type and all
3918 // argument values are known, if not done above.
3919 if (need_arg_check)
3920 error = may_check_argument_types(funcexe, argvars, argcount,
3921 TRUE, (name != NULL) ? name : funcname);
3922 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3923 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003925 }
3926 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003927 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003928 {
3929 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003930 * expr->method(): Find the method name in the table, call its
3931 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003932 */
3933 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003934 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003935 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003936 else
3937 {
3938 /*
3939 * Find the function name in the table, call its implementation.
3940 */
3941 error = call_internal_func(fname, argcount, argvars, rettv);
3942 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003943
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003944 /*
3945 * The function call (or "FuncUndefined" autocommand sequence) might
3946 * have been aborted by an error, an interrupt, or an explicitly thrown
3947 * exception that has not been caught so far. This situation can be
3948 * tested for by calling aborting(). For an error in an internal
3949 * function or for the "E132" error in call_user_func(), however, the
3950 * throw point at which the "force_abort" flag (temporarily reset by
3951 * emsg()) is normally updated has not been reached yet. We need to
3952 * update that flag first to make aborting() reliable.
3953 */
3954 update_force_abort();
3955 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003956 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003957 ret = OK;
3958
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003959theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003960 /*
3961 * Report an error unless the argument evaluation or function call has been
3962 * cancelled due to an aborting error, an interrupt, or an exception.
3963 */
3964 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003965 user_func_error(error, (name != NULL) ? name : funcname,
3966 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003967
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003968 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003969 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003970 clear_tv(&argv[--argv_clear + argv_base]);
3971
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003972 vim_free(tofree);
3973 vim_free(name);
3974
3975 return ret;
3976}
3977
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003978/*
3979 * Call a function without arguments, partial or dict.
3980 * This is like call_func() when the call is only "FuncName()".
3981 * To be used by "expr" options.
3982 * Returns NOTDONE when the function could not be found.
3983 */
3984 int
3985call_simple_func(
3986 char_u *funcname, // name of the function
3987 int len, // length of "name" or -1 to use strlen()
3988 typval_T *rettv) // return value goes here
3989{
3990 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003991 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003992 char_u fname_buf[FLEN_FIXED + 1];
3993 char_u *tofree = NULL;
3994 char_u *name;
3995 char_u *fname;
3996 char_u *rfname;
3997 int is_global = FALSE;
3998 ufunc_T *fp;
3999
4000 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4001 rettv->vval.v_number = 0;
4002
4003 // Make a copy of the name, an option can be changed in the function.
4004 name = vim_strnsave(funcname, len);
4005 if (name == NULL)
4006 return ret;
4007
4008 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4009
4010 // Skip "g:" before a function name.
4011 if (fname[0] == 'g' && fname[1] == ':')
4012 {
4013 is_global = TRUE;
4014 rfname = fname + 2;
4015 }
4016 else
4017 rfname = fname;
4018 fp = find_func(rfname, is_global);
4019 if (fp != NULL && !is_global && in_vim9script()
4020 && func_requires_g_prefix(fp))
4021 // In Vim9 script g: is required to find a global non-autoload
4022 // function.
4023 fp = NULL;
4024 if (fp == NULL)
4025 ret = NOTDONE;
4026 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4027 error = FCERR_DELETED;
4028 else if (fp != NULL)
4029 {
4030 typval_T argvars[1];
4031 funcexe_T funcexe;
4032
4033 argvars[0].v_type = VAR_UNKNOWN;
4034 CLEAR_FIELD(funcexe);
4035 funcexe.fe_evaluate = TRUE;
4036
4037 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4038 if (error == FCERR_NONE)
4039 ret = OK;
4040 }
4041
4042 user_func_error(error, name, FALSE);
4043 vim_free(tofree);
4044 vim_free(name);
4045
4046 return ret;
4047}
4048
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004049 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050printable_func_name(ufunc_T *fp)
4051{
4052 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4053}
4054
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004055/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004056 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4057 * TRUE. Otherwise return FALSE.
4058 */
4059 static int
4060function_list_modified(int prev_ht_changed)
4061{
4062 if (prev_ht_changed != func_hashtab.ht_changed)
4063 {
4064 emsg(_(e_function_list_was_modified));
4065 return TRUE;
4066 }
4067 return FALSE;
4068}
4069
4070/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004071 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004072 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004073 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004074list_func_head(ufunc_T *fp, int indent)
4075{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004076 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004077 int j;
4078
4079 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004080
4081 // a timer at the more prompt may have deleted the function
4082 if (function_list_modified(prev_ht_changed))
4083 return FAIL;
4084
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004085 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004086 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004087 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004088 msg_puts("def ");
4089 else
4090 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004092 msg_putchar('(');
4093 for (j = 0; j < fp->uf_args.ga_len; ++j)
4094 {
4095 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004096 msg_puts(", ");
4097 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 if (fp->uf_arg_types != NULL)
4099 {
4100 char *tofree;
4101
4102 msg_puts(": ");
4103 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4104 vim_free(tofree);
4105 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004106 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4107 {
4108 msg_puts(" = ");
4109 msg_puts(((char **)(fp->uf_def_args.ga_data))
4110 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4111 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004112 }
4113 if (fp->uf_varargs)
4114 {
4115 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004116 msg_puts(", ");
4117 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004118 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 if (fp->uf_va_name != NULL)
4120 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004121 if (!fp->uf_varargs)
4122 {
4123 if (j)
4124 msg_puts(", ");
4125 msg_puts("...");
4126 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004128 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 {
4130 char *tofree;
4131
4132 msg_puts(": ");
4133 msg_puts(type_name(fp->uf_va_type, &tofree));
4134 vim_free(tofree);
4135 }
4136 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004137 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004138
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004139 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004140 {
4141 if (fp->uf_ret_type != &t_void)
4142 {
4143 char *tofree;
4144
4145 msg_puts(": ");
4146 msg_puts(type_name(fp->uf_ret_type, &tofree));
4147 vim_free(tofree);
4148 }
4149 }
4150 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004151 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004152 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004153 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004154 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004155 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004156 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004157 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004158 msg_clr_eos();
4159 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004160 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004161
4162 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004163}
4164
4165/*
4166 * Get a function name, translating "<SID>" and "<SNR>".
4167 * Also handles a Funcref in a List or Dictionary.
4168 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004169 * Set "*is_global" to TRUE when the function must be global, unless
4170 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004171 * flags:
4172 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004173 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004174 * TFN_QUIET: be quiet
4175 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004176 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004177 * Advances "pp" to just after the function name (if no error).
4178 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004179 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004180trans_function_name(
4181 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004182 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004183 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004184 int flags)
4185{
4186 return trans_function_name_ext(pp, is_global, skip, flags,
4187 NULL, NULL, NULL, NULL);
4188}
4189
4190/*
4191 * trans_function_name() with extra arguments.
4192 * "fdp", "partial", "type" and "ufunc" can be NULL.
4193 */
4194 char_u *
4195trans_function_name_ext(
4196 char_u **pp,
4197 int *is_global,
4198 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004199 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004200 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004201 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004202 type_T **type, // return: type of funcref
4203 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004204{
4205 char_u *name = NULL;
4206 char_u *start;
4207 char_u *end;
4208 int lead;
4209 char_u sid_buf[20];
4210 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004212 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004213 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004214 int vim9script = in_vim9script();
4215 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004216
4217 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004218 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004219 start = *pp;
4220
Bram Moolenaare38eab22019-12-05 21:50:01 +01004221 // Check for hard coded <SNR>: already translated function ID (from a user
4222 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004223 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4224 && (*pp)[2] == (int)KE_SNR)
4225 {
4226 *pp += 3;
4227 len = get_id_len(pp) + 3;
4228 return vim_strnsave(start, len);
4229 }
4230
Bram Moolenaare38eab22019-12-05 21:50:01 +01004231 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4232 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004233 lead = eval_fname_script(start);
4234 if (lead > 2)
4235 start += lead;
4236
Bram Moolenaare38eab22019-12-05 21:50:01 +01004237 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004238 end = get_lval(start, NULL, &lv, FALSE, skip,
4239 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004240 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004241 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004242 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004243 {
4244 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004245 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004246 goto theend;
4247 }
4248 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4249 {
4250 /*
4251 * Report an invalid expression in braces, unless the expression
4252 * evaluation has been cancelled due to an aborting error, an
4253 * interrupt, or an exception.
4254 */
4255 if (!aborting())
4256 {
4257 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004258 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004259 }
4260 else
4261 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4262 goto theend;
4263 }
4264
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004265 if (lv.ll_ufunc != NULL)
4266 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004267 if (ufunc != NULL)
4268 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004269 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004270 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004271 goto theend;
4272 }
4273
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004274 if (lv.ll_tv != NULL)
4275 {
4276 if (fdp != NULL)
4277 {
4278 fdp->fd_dict = lv.ll_dict;
4279 fdp->fd_newkey = lv.ll_newkey;
4280 lv.ll_newkey = NULL;
4281 fdp->fd_di = lv.ll_di;
4282 }
4283 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4284 {
4285 name = vim_strsave(lv.ll_tv->vval.v_string);
4286 *pp = end;
4287 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004288 else if (lv.ll_tv->v_type == VAR_CLASS
4289 && lv.ll_tv->vval.v_class != NULL)
4290 {
4291 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4292 *pp = end;
4293 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004294 else if (lv.ll_tv->v_type == VAR_PARTIAL
4295 && lv.ll_tv->vval.v_partial != NULL)
4296 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004297 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004298 *pp = end;
4299 if (partial != NULL)
4300 *partial = lv.ll_tv->vval.v_partial;
4301 }
4302 else
4303 {
4304 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4305 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004306 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004307 else
4308 *pp = end;
4309 name = NULL;
4310 }
4311 goto theend;
4312 }
4313
4314 if (lv.ll_name == NULL)
4315 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004316 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004317 *pp = end;
4318 goto theend;
4319 }
4320
Bram Moolenaare38eab22019-12-05 21:50:01 +01004321 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004322 if (lv.ll_exp_name != NULL)
4323 {
4324 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004325 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004326 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004327 if (name == lv.ll_exp_name)
4328 name = NULL;
4329 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004330 else if (lv.ll_sid > 0)
4331 {
4332 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4333 int cc = *lv.ll_name_end;
4334
4335 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4336 *lv.ll_name_end = NUL;
4337 if (si->sn_autoload_prefix != NULL)
4338 {
4339 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4340 }
4341 else
4342 {
4343 sid_buf[0] = K_SPECIAL;
4344 sid_buf[1] = KS_EXTRA;
4345 sid_buf[2] = (int)KE_SNR;
4346 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004347 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004348 name = concat_str(sid_buf, lv.ll_name);
4349 }
4350 *lv.ll_name_end = cc;
4351 *pp = end;
4352 goto theend;
4353 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004354 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004355 {
4356 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004357 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004358 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004359 if (name == *pp)
4360 name = NULL;
4361 }
4362 if (name != NULL)
4363 {
4364 name = vim_strsave(name);
4365 *pp = end;
4366 if (STRNCMP(name, "<SNR>", 5) == 0)
4367 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004368 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004369 name[0] = K_SPECIAL;
4370 name[1] = KS_EXTRA;
4371 name[2] = (int)KE_SNR;
4372 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4373 }
4374 goto theend;
4375 }
4376
4377 if (lv.ll_exp_name != NULL)
4378 {
4379 len = (int)STRLEN(lv.ll_exp_name);
4380 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4381 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4382 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004383 // When there was "s:" already or the name expanded to get a
4384 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004385 lv.ll_name += 2;
4386 len -= 2;
4387 lead = 2;
4388 }
4389 }
4390 else
4391 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004392 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004393 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004394 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004395 if (lv.ll_name[0] == 'g')
4396 {
4397 if (is_global != NULL)
4398 {
4399 *is_global = TRUE;
4400 }
4401 else
4402 {
4403 // dropping "g:" without setting "is_global" won't work in
4404 // Vim9script, put it back later
4405 prefix_g = TRUE;
4406 extra = 2;
4407 }
4408 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004409 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004410 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004411 len = (int)(end - lv.ll_name);
4412 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004413 if (len <= 0)
4414 {
4415 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004416 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004417 goto theend;
4418 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004419
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004420 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004421 // starts with a lower case character: dict.func(). Or when in a class.
4422 vim9_local = ASCII_ISUPPER(*start) && vim9script
4423 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004425 /*
4426 * Copy the function name to allocated memory.
4427 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4428 * Accept <SNR>123_name() outside a script.
4429 */
4430 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004431 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004432 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004433 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004434 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004435 {
Kota Kato948a3892022-08-16 16:09:59 +01004436 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4437 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004438 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004439 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004440 goto theend;
4441 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004443 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004444 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004446 || eval_fname_sid(*pp))
4447 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004449 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004450 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004451 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004452 goto theend;
4453 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004454 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004455 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 extra = 3 + (int)STRLEN(sid_buf);
4457 else
4458 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004459 }
4460 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004461 // The function name must start with an upper case letter (unless it is a
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004462 // Vim9 class new() function or a Vim9 class private method or one of the
4463 // supported Vim9 object builtin functions)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004464 else if (!(flags & TFN_INT)
4465 && (builtin_function(lv.ll_name, len)
4466 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004467 && !((flags & TFN_IN_CLASS)
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004468 && (is_valid_builtin_obj_methodname(lv.ll_name)
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004469 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004470 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004471 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004472 : e_function_name_must_start_with_capital_or_s_str),
4473 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004474 goto theend;
4475 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004476 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004477 {
4478 char_u *cp = vim_strchr(lv.ll_name, ':');
4479
4480 if (cp != NULL && cp < end)
4481 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004482 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004483 goto theend;
4484 }
4485 }
4486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004488 if (name != NULL)
4489 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004490 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004491 {
4492 name[0] = K_SPECIAL;
4493 name[1] = KS_EXTRA;
4494 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004495 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004496 STRCPY(name + 3, sid_buf);
4497 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004498 else if (prefix_g)
4499 {
4500 name[0] = 'g';
4501 name[1] = ':';
4502 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4504 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004505 }
4506 *pp = end;
4507
4508theend:
4509 clear_lval(&lv);
4510 return name;
4511}
4512
4513/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004514 * Assuming "name" is the result of trans_function_name() and it was prefixed
4515 * to use the script-local name, return the unmodified name (points into
4516 * "name"). Otherwise return NULL.
4517 * This can be used to first search for a script-local function and fall back
4518 * to the global function if not found.
4519 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004520 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004521untrans_function_name(char_u *name)
4522{
4523 char_u *p;
4524
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004525 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004526 {
4527 p = vim_strchr(name, '_');
4528 if (p != NULL)
4529 return p + 1;
4530 }
4531 return NULL;
4532}
4533
4534/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004535 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4536 * current script ID and returns the expanded function name. The caller should
4537 * free the returned name. If not called from a script context or the function
4538 * name doesn't start with these prefixes, then returns NULL.
4539 * This doesn't check whether the script-local function exists or not.
4540 */
4541 char_u *
4542get_scriptlocal_funcname(char_u *funcname)
4543{
4544 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004545 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004546 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004547 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004548
4549 if (funcname == NULL)
4550 return NULL;
4551
4552 if (STRNCMP(funcname, "s:", 2) != 0
4553 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004554 {
4555 ufunc_T *ufunc;
4556
4557 // The function name does not have a script-local prefix. Try finding
4558 // it when in a Vim9 script and there is no "g:" prefix.
4559 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4560 return NULL;
4561 ufunc = find_func(funcname, FALSE);
4562 if (ufunc == NULL || func_is_global(ufunc)
4563 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4564 return NULL;
4565 ++p;
4566 off = 0;
4567 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004568 else
4569 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004570
4571 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4572 {
4573 emsg(_(e_using_sid_not_in_script_context));
4574 return NULL;
4575 }
4576 // Expand s: prefix into <SNR>nr_<name>
4577 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4578 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004579 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004580 if (newname == NULL)
4581 return NULL;
4582 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004583 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004584
4585 return newname;
4586}
4587
4588/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004589 * Return script-local "fname" with the 3-byte sequence replaced by
4590 * printable <SNR> in allocated memory.
4591 */
4592 char_u *
4593alloc_printable_func_name(char_u *fname)
4594{
4595 char_u *n = alloc(STRLEN(fname + 3) + 6);
4596
4597 if (n != NULL)
4598 {
4599 STRCPY(n, "<SNR>");
4600 STRCPY(n + 5, fname + 3);
4601 }
4602 return n;
4603}
4604
4605/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004606 * Call trans_function_name(), except that a lambda is returned as-is.
4607 * Returns the name in allocated memory.
4608 */
4609 char_u *
4610save_function_name(
4611 char_u **name,
4612 int *is_global,
4613 int skip,
4614 int flags,
4615 funcdict_T *fudi)
4616{
4617 char_u *p = *name;
4618 char_u *saved;
4619
4620 if (STRNCMP(p, "<lambda>", 8) == 0)
4621 {
4622 p += 8;
4623 (void)getdigits(&p);
4624 saved = vim_strnsave(*name, p - *name);
4625 if (fudi != NULL)
4626 CLEAR_POINTER(fudi);
4627 }
4628 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004629 saved = trans_function_name_ext(&p, is_global, skip,
4630 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004631 *name = p;
4632 return saved;
4633}
4634
4635/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004636 * List functions. When "regmatch" is NULL all of then.
4637 * Otherwise functions matching "regmatch".
4638 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004639 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004640list_functions(regmatch_T *regmatch)
4641{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004642 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004643 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004644 hashitem_T *hi;
4645
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004646 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004647 {
4648 if (!HASHITEM_EMPTY(hi))
4649 {
4650 ufunc_T *fp = HI2UF(hi);
4651
4652 --todo;
4653 if ((fp->uf_flags & FC_DEAD) == 0
4654 && (regmatch == NULL
4655 ? !message_filtered(fp->uf_name)
4656 && !func_name_refcount(fp->uf_name)
Keith Thompson184f71c2024-01-04 21:19:04 +01004657 : !SAFE_isdigit(*fp->uf_name)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004658 && vim_regexec(regmatch, fp->uf_name, 0)))
4659 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004660 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004661 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004662 if (function_list_modified(prev_ht_changed))
4663 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004664 }
4665 }
4666 }
4667}
4668
4669/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004670 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004671 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4672 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004673 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004674 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4675 * With CF_INTERFACE the function is define inside an interface, only the
4676 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004677 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004678 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004679 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004680define_function(
4681 exarg_T *eap,
4682 char_u *name_arg,
4683 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004684 int class_flags,
4685 ocmember_T *obj_members,
4686 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004687{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004688 int j;
4689 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004690 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004691 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004692 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004693 char_u *p;
4694 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004695 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004696 char_u *line_arg = NULL;
4697 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004699 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004700 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004701 garray_T newlines;
4702 int varargs = FALSE;
4703 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004705 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004706 int fp_allocated = FALSE;
4707 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004708 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004709 dictitem_T *v;
4710 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004711 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004712 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004713 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004714 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004715 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004716 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004717
4718 /*
4719 * ":function" without argument: list functions.
4720 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004721 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004722 {
4723 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004724 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004725 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004726 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004727 }
4728
4729 /*
4730 * ":function /pat": list functions matching pattern.
4731 */
4732 if (*eap->arg == '/')
4733 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004734 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004735 if (!eap->skip)
4736 {
4737 regmatch_T regmatch;
4738
4739 c = *p;
4740 *p = NUL;
4741 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4742 *p = c;
4743 if (regmatch.regprog != NULL)
4744 {
4745 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004746 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004747 vim_regfree(regmatch.regprog);
4748 }
4749 }
4750 if (*p == '/')
4751 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004752 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004753 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004754 }
4755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004756 ga_init(&newargs);
4757 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004758 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759 ga_init(&default_args);
4760
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004761 /*
4762 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004763 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004764 * "name" == func, "fudi.fd_dict" == NULL
4765 * dict.func new dictionary entry
4766 * "name" == NULL, "fudi.fd_dict" set,
4767 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4768 * dict.func existing dict entry with a Funcref
4769 * "name" == func, "fudi.fd_dict" set,
4770 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4771 * dict.func existing dict entry that's not a Funcref
4772 * "name" == NULL, "fudi.fd_dict" set,
4773 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4774 * s:func script-local function name
4775 * g:func global function name, same as "func"
4776 */
4777 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004778 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004779 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004780 // nested function, argument is (args).
4781 paren = TRUE;
4782 CLEAR_FIELD(fudi);
4783 }
4784 else
4785 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004786 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004787 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004788 if (p[0] == 's' && p[1] == ':')
4789 {
4790 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4791 return NULL;
4792 }
4793 p = to_name_end(p, TRUE);
4794 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4795 {
4796 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4797 eap->arg);
4798 return NULL;
4799 }
4800 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004801 }
4802
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004803 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004804 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004805 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004806 paren = (vim_strchr(p, '(') != NULL);
4807 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004808 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004809 /*
4810 * Return on an invalid expression in braces, unless the expression
4811 * evaluation has been cancelled due to an aborting error, an
4812 * interrupt, or an exception.
4813 */
4814 if (!aborting())
4815 {
4816 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004817 semsg(_(e_key_not_present_in_dictionary_str),
4818 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004819 vim_free(fudi.fd_newkey);
4820 return NULL;
4821 }
4822 else
4823 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004824 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004825
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004826 // For "export def FuncName()" in an autoload script the function name
4827 // is stored with the legacy autoload name "dir#script#FuncName" so
4828 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004829 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004830 {
4831 char_u *prefixed = may_prefix_autoload(name);
4832
4833 if (prefixed != NULL && prefixed != name)
4834 {
4835 vim_free(name);
4836 name = prefixed;
4837 }
4838 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004839 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004840 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004841 {
4842 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4843 goto ret_free;
4844 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004845 }
4846
Bram Moolenaare38eab22019-12-05 21:50:01 +01004847 // An error in a function call during evaluation of an expression in magic
4848 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004849 saved_did_emsg = did_emsg;
4850 did_emsg = FALSE;
4851
4852 /*
4853 * ":function func" with only function name: list function.
4854 */
4855 if (!paren)
4856 {
4857 if (!ends_excmd(*skipwhite(p)))
4858 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004859 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004860 goto ret_free;
4861 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004862 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004863 if (eap->nextcmd != NULL)
4864 *p = NUL;
4865 if (!eap->skip && !got_int)
4866 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004867 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004868 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4869 {
4870 char_u *up = untrans_function_name(name);
4871
4872 // With Vim9 script the name was made script-local, if not
4873 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004874 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004875 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004876 }
4877
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004878 if (fp != NULL)
4879 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004880 // Check no function was added or removed from a timer, e.g. at
4881 // the more prompt. "fp" may then be invalid.
4882 int prev_ht_changed = func_hashtab.ht_changed;
4883
4884 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004885 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004886 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4887 {
4888 if (FUNCLINE(fp, j) == NULL)
4889 continue;
4890 msg_putchar('\n');
4891 msg_outnum((long)(j + 1));
4892 if (j < 9)
4893 msg_putchar(' ');
4894 if (j < 99)
4895 msg_putchar(' ');
4896 if (function_list_modified(prev_ht_changed))
4897 break;
4898 msg_prt_line(FUNCLINE(fp, j), FALSE);
4899 out_flush(); // show a line at a time
4900 ui_breakcheck();
4901 }
4902 if (!got_int)
4903 {
4904 msg_putchar('\n');
4905 if (!function_list_modified(prev_ht_changed))
4906 {
4907 if (fp->uf_def_status != UF_NOT_COMPILED)
4908 msg_puts(" enddef");
4909 else
4910 msg_puts(" endfunction");
4911 }
4912 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004913 }
4914 }
4915 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004916 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004917 }
4918 goto ret_free;
4919 }
4920
4921 /*
4922 * ":function name(arg1, arg2)" Define function.
4923 */
4924 p = skipwhite(p);
4925 if (*p != '(')
4926 {
4927 if (!eap->skip)
4928 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004929 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004930 goto ret_free;
4931 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004932 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004933 if (vim_strchr(p, '(') != NULL)
4934 p = vim_strchr(p, '(');
4935 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004936
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004937 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4938 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004939 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004940 goto ret_free;
4941 }
4942
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004943 // In Vim9 script only global functions can be redefined.
4944 if (vim9script && eap->forceit && !is_global)
4945 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004946 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004947 goto ret_free;
4948 }
4949
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004950 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004951
Bram Moolenaar04b12692020-05-04 23:24:44 +02004952 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004953 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004954 // Check the name of the function. Unless it's a dictionary function
4955 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004956 if (name != NULL)
4957 arg = name;
4958 else
4959 arg = fudi.fd_newkey;
4960 if (arg != NULL && (fudi.fd_di == NULL
4961 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4962 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4963 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004964 char_u *name_base = arg;
4965 int i;
4966
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004967 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004968 {
4969 name_base = vim_strchr(arg, '_');
4970 if (name_base == NULL)
4971 name_base = arg + 3;
4972 else
4973 ++name_base;
4974 }
4975 for (i = 0; name_base[i] != NUL && (i == 0
4976 ? eval_isnamec1(name_base[i])
4977 : eval_isnamec(name_base[i])); ++i)
4978 ;
4979 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004980 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004981
4982 // In Vim9 script a function cannot have the same name as a
4983 // variable.
4984 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004985 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4986 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004987 + EVAL_VAR_NO_FUNC) == OK)
4988 {
4989 semsg(_(e_redefining_script_item_str), name_base);
4990 goto ret_free;
4991 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004992 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004993 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004994 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004995 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004996 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004997 goto ret_free;
4998 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004999 }
5000
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005001 // This may get more lines and make the pointers into the first line
5002 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005003 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005005 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005006 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005007 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005008 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005009 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005010 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005013 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005015 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005016 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005017 if (*p != ':')
5018 {
5019 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5020 p = skipwhite(p);
5021 }
5022 else if (!IS_WHITE_OR_NUL(p[1]))
5023 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005025 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005026 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005027 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005028 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005029 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005033 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005034 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005035 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005036 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005037 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005038 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005039 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005040 else
5041 // find extra arguments "range", "dict", "abort" and "closure"
5042 for (;;)
5043 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005044 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 p = skipwhite(p);
5046 if (STRNCMP(p, "range", 5) == 0)
5047 {
5048 flags |= FC_RANGE;
5049 p += 5;
5050 }
5051 else if (STRNCMP(p, "dict", 4) == 0)
5052 {
5053 flags |= FC_DICT;
5054 p += 4;
5055 }
5056 else if (STRNCMP(p, "abort", 5) == 0)
5057 {
5058 flags |= FC_ABORT;
5059 p += 5;
5060 }
5061 else if (STRNCMP(p, "closure", 7) == 0)
5062 {
5063 flags |= FC_CLOSURE;
5064 p += 7;
5065 if (current_funccal == NULL)
5066 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005067 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 name == NULL ? (char_u *)"" : name);
5069 goto erret;
5070 }
5071 }
5072 else
5073 break;
5074 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005075
Bram Moolenaare38eab22019-12-05 21:50:01 +01005076 // When there is a line break use what follows for the function body.
5077 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005078 if (*p == '\n')
5079 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005080 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005081 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5082 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005083 && !(VIM_ISWHITE(*whitep) && *p == '#'
5084 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005085 && !eap->skip
5086 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005087 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005088
5089 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5091 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005092 */
5093 if (KeyTyped)
5094 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005095 // Check if the function already exists, don't let the user type the
5096 // whole function before telling him it doesn't work! For a script we
5097 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005098 if (!eap->skip && !eap->forceit)
5099 {
5100 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005101 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005102 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005103 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005104 }
5105
5106 if (!eap->skip && did_emsg)
5107 goto erret;
5108
Bram Moolenaare38eab22019-12-05 21:50:01 +01005109 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005110 cmdline_row = msg_row;
5111 }
5112
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005113 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005114 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005115
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005116 // Do not define the function when getting the body fails and when
5117 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005118 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005119 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005120 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5121 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005122 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005123 goto erret;
5124
5125 /*
5126 * If there are no errors, add the function
5127 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005128 if (fudi.fd_dict != NULL)
5129 {
5130 char numbuf[20];
5131
5132 fp = NULL;
5133 if (fudi.fd_newkey == NULL && !eap->forceit)
5134 {
5135 emsg(_(e_dictionary_entry_already_exists));
5136 goto erret;
5137 }
5138 if (fudi.fd_di == NULL)
5139 {
5140 // Can't add a function to a locked dictionary
5141 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5142 goto erret;
5143 }
5144 // Can't change an existing function if it is locked
5145 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5146 goto erret;
5147
5148 // Give the function a sequential number. Can only be used with a
5149 // Funcref!
5150 vim_free(name);
5151 sprintf(numbuf, "%d", ++func_nr);
5152 name = vim_strsave((char_u *)numbuf);
5153 if (name == NULL)
5154 goto erret;
5155 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005156 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005157 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005158 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005159 char_u *find_name = name;
5160 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005161 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005162
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005163 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005164 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005165 var_conflict = TRUE;
5166
5167 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5168 {
5169 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5170
5171 if (si->sn_autoload_prefix != NULL)
5172 {
5173 if (is_export)
5174 {
5175 find_name = name + STRLEN(si->sn_autoload_prefix);
5176 v = find_var(find_name, &ht, TRUE);
5177 if (v != NULL)
5178 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005179 // Only check if the function already exists in the script,
5180 // global functions can be shadowed.
5181 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005182 }
5183 else
5184 {
5185 char_u *prefixed = may_prefix_autoload(name);
5186
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005187 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005188 {
5189 v = find_var(prefixed, &ht, TRUE);
5190 if (v != NULL)
5191 var_conflict = TRUE;
5192 vim_free(prefixed);
5193 }
5194 }
5195 }
5196 }
5197 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005198 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005199 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005200 goto erret;
5201 }
5202
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005203 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005204 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005205 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005206 char_u *uname = untrans_function_name(name);
5207
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005208 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005209 }
5210
5211 if (fp != NULL || import != NULL)
5212 {
5213 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005215 // Function can be replaced with "function!" and when sourcing the
5216 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005217 // A name that is used by an import can not be overruled.
5218 if (import != NULL
5219 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005220 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005221 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005222 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005223 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005224 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005225 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005226 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005227 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005228 goto erret;
5229 }
5230 if (fp->uf_calls > 0)
5231 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005232 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005233 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005234 goto erret;
5235 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005236 if (fp->uf_refcount > 1)
5237 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005238 // This function is referenced somewhere, don't redefine it but
5239 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005240 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005241 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005242 fp = NULL;
5243 overwrite = TRUE;
5244 }
5245 else
5246 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005247 char_u *exp_name = fp->uf_name_exp;
5248
5249 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005250 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005251 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005252 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005253 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005254 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005255#ifdef FEAT_PROFILE
5256 fp->uf_profiling = FALSE;
5257 fp->uf_prof_initialized = FALSE;
5258#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005259 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005260 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005261 }
5262 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005263
5264 if (fp == NULL)
5265 {
5266 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5267 {
5268 int slen, plen;
5269 char_u *scriptname;
5270
Bram Moolenaare38eab22019-12-05 21:50:01 +01005271 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005272 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005273 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005274 {
5275 scriptname = autoload_name(name);
5276 if (scriptname != NULL)
5277 {
5278 p = vim_strchr(scriptname, '/');
5279 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005280 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005281 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005282 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005283 j = OK;
5284 vim_free(scriptname);
5285 }
5286 }
5287 if (j == FAIL)
5288 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005289 linenr_T save_lnum = SOURCING_LNUM;
5290
5291 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005292 semsg(_(e_function_name_does_not_match_script_file_name_str),
5293 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005294 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005295 goto erret;
5296 }
5297 }
5298
Bram Moolenaare01e5212023-01-08 20:31:18 +00005299 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005300 if (fp == NULL)
5301 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005302 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005303
5304 if (fudi.fd_dict != NULL)
5305 {
5306 if (fudi.fd_di == NULL)
5307 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005308 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005309 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5310 if (fudi.fd_di == NULL)
5311 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005312 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005313 goto erret;
5314 }
5315 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5316 {
5317 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005318 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005319 goto erret;
5320 }
5321 }
5322 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005323 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005324 clear_tv(&fudi.fd_di->di_tv);
5325 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005326 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005327
Bram Moolenaare38eab22019-12-05 21:50:01 +01005328 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005329 flags |= FC_DICT;
5330 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005331 }
5332 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005333 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005334 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005335 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005336
5337 if (eap->cmdidx == CMD_def)
5338 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005339 int lnum_save = SOURCING_LNUM;
5340 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005341
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005342 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005343
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005344 // error messages are for the first function line
5345 SOURCING_LNUM = sourcing_lnum_top;
5346
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005347 // The function may use script variables from the context.
5348 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005349
h-eastb895b0f2023-09-24 15:46:31 +02005350 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5351 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005352 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005353 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005354 free_fp = fp_allocated;
5355 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005356 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005357 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005358
5359 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005360 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005362 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005363 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005364 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005365 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005366 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005367 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005368 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005369 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005371 if (fp_allocated)
5372 {
5373 // insert the new function in the function list
5374 set_ufunc_name(fp, name);
5375 if (overwrite)
5376 {
5377 hi = hash_find(&func_hashtab, name);
5378 hi->hi_key = UF2HIKEY(fp);
5379 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005380 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005381 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005382 {
5383 free_fp = TRUE;
5384 goto erret;
5385 }
5386 fp->uf_refcount = 1;
5387 }
5388
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005389 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005390 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005391 if ((flags & FC_CLOSURE) != 0)
5392 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005393 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005394 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005395 }
5396 else
5397 fp->uf_scoped = NULL;
5398
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005399#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005400 if (prof_def_func())
5401 func_do_profile(fp);
5402#endif
5403 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005404 if (sandbox)
5405 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005406 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005407 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005408 fp->uf_flags = flags;
5409 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005410 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005411 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005412 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005413 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005414 if (is_export)
5415 {
5416 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005417 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005418 is_export = FALSE;
5419 }
5420
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005421 if (eap->cmdidx == CMD_def)
5422 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005423 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5424 // :func does not use Vim9 script syntax, even in a Vim9 script file
5425 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005426
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005427 goto ret_free;
5428
5429erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005430 if (fp != NULL)
5431 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005432 // these were set to "newargs" and "default_args", which are cleared
5433 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005434 ga_init(&fp->uf_args);
5435 ga_init(&fp->uf_def_args);
5436 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005437errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005438 ga_clear_strings(&newargs);
5439 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005440 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005441 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005442 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005443 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005444 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01005445 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005446 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005447 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005448 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005449ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005450 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005451 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005452 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005453 if (name != name_arg)
5454 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005455 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005456 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005457
5458 return fp;
5459}
5460
5461/*
5462 * ":function"
5463 */
5464 void
5465ex_function(exarg_T *eap)
5466{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005467 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005468
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005469 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005470 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005471 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005472}
5473
5474/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005475 * Find a function by name, including "<lambda>123".
5476 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005477 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005478 * Return NULL if not found.
5479 */
5480 ufunc_T *
5481find_func_by_name(char_u *name, compiletype_T *compile_type)
5482{
5483 char_u *arg = name;
5484 char_u *fname;
5485 ufunc_T *ufunc;
5486 int is_global = FALSE;
5487
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005488 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5489 {
5490 *compile_type = CT_PROFILE;
5491 arg = skipwhite(arg + 7);
5492 }
5493 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5494 {
5495 *compile_type = CT_DEBUG;
5496 arg = skipwhite(arg + 5);
5497 }
5498
5499 if (STRNCMP(arg, "<lambda>", 8) == 0)
5500 {
5501 arg += 8;
5502 (void)getdigits(&arg);
5503 fname = vim_strnsave(name, arg - name);
5504 }
5505 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005506 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005507 // First try finding a method in a class, trans_function_name() will
5508 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005509 ufunc = find_class_func(&arg);
5510 if (ufunc != NULL)
5511 return ufunc;
5512
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005513 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005514 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005515 NULL, NULL, NULL, &ufunc);
5516 if (ufunc != NULL)
5517 {
5518 vim_free(fname);
5519 return ufunc;
5520 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005521 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005522 if (fname == NULL)
5523 {
5524 semsg(_(e_invalid_argument_str), name);
5525 return NULL;
5526 }
5527 if (!ends_excmd2(name, arg))
5528 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005529 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005530 emsg(ex_errmsg(e_trailing_characters_str, arg));
5531 return NULL;
5532 }
5533
5534 ufunc = find_func(fname, is_global);
5535 if (ufunc == NULL)
5536 {
5537 char_u *p = untrans_function_name(fname);
5538
5539 if (p != NULL)
5540 // Try again without making it script-local.
5541 ufunc = find_func(p, FALSE);
5542 }
5543 vim_free(fname);
5544 if (ufunc == NULL)
5545 semsg(_(e_cannot_find_function_str), name);
5546 return ufunc;
5547}
5548
5549/*
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005550 * Compile the :def function "ufunc". If "cl" is not NULL, then compile the
5551 * class or object method "ufunc" in "cl".
5552 */
5553 void
5554defcompile_function(ufunc_T *ufunc, class_T *cl)
5555{
5556 compiletype_T compile_type = CT_NONE;
5557
5558 if (func_needs_compiling(ufunc, compile_type))
5559 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5560 else
5561 smsg(_("Function %s%s%s does not need compiling"),
5562 cl != NULL ? cl->class_name : (char_u *)"",
5563 cl != NULL ? (char_u *)"." : (char_u *)"",
5564 ufunc->uf_name);
5565}
5566
5567/*
5568 * Compile all the :def functions defined in the current script
5569 */
5570 static void
5571defcompile_funcs_in_script(void)
5572{
5573 long todo = (long)func_hashtab.ht_used;
5574 int changed = func_hashtab.ht_changed;
5575 hashitem_T *hi;
5576
5577 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5578 {
5579 if (!HASHITEM_EMPTY(hi))
5580 {
5581 --todo;
5582 ufunc_T *ufunc = HI2UF(hi);
5583 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5584 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5585 && (ufunc->uf_flags & FC_DEAD) == 0)
5586 {
5587 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5588
5589 if (func_hashtab.ht_changed != changed)
5590 {
5591 // a function has been added or removed, need to start
5592 // over
5593 todo = (long)func_hashtab.ht_used;
5594 changed = func_hashtab.ht_changed;
5595 hi = func_hashtab.ht_array;
5596 --hi;
5597 }
5598 }
5599 }
5600 }
5601}
5602
5603/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005604 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005605 * be compiled or the one specified by the argument.
5606 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005607 */
5608 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005609ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005610{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005611 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005612 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005613 typval_T tv;
5614
5615 if (is_class_name(eap->arg, &tv))
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005616 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005617 class_T *cl = tv.vval.v_class;
5618
5619 if (cl != NULL)
5620 defcompile_class(cl);
5621 }
5622 else
5623 {
5624 compiletype_T compile_type = CT_NONE;
5625 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
5626 if (ufunc != NULL)
5627 defcompile_function(ufunc, NULL);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005628 }
5629 }
5630 else
5631 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005632 defcompile_funcs_in_script();
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005633
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005634 // compile all the class defined in the current script
5635 defcompile_classes_in_script();
Bram Moolenaar822ba242020-05-24 23:00:18 +02005636 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005637}
5638
5639/*
5640 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5641 * Return 2 if "p" starts with "s:".
5642 * Return 0 otherwise.
5643 */
5644 int
5645eval_fname_script(char_u *p)
5646{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005647 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5648 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005649 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5650 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5651 return 5;
5652 if (p[0] == 's' && p[1] == ':')
5653 return 2;
5654 return 0;
5655}
5656
5657 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005658translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005659{
5660 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005661 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005662 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005663}
5664
5665/*
5666 * Return TRUE when "ufunc" has old-style "..." varargs
5667 * or named varargs "...name: type".
5668 */
5669 int
5670has_varargs(ufunc_T *ufunc)
5671{
5672 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005673}
5674
5675/*
5676 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005677 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005678 */
5679 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005680function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005681{
5682 char_u *nm = name;
5683 char_u *p;
5684 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005685 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005686 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005687
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005688 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5689 if (no_deref)
5690 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005691 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005692 nm = skipwhite(nm);
5693
Bram Moolenaare38eab22019-12-05 21:50:01 +01005694 // Only accept "funcname", "funcname ", "funcname (..." and
5695 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005696 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005697 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005698 vim_free(p);
5699 return n;
5700}
5701
Bram Moolenaar113e1072019-01-20 15:30:40 +01005702#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005703 char_u *
5704get_expanded_name(char_u *name, int check)
5705{
5706 char_u *nm = name;
5707 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005708 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005709
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005710 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005711
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005712 if (p != NULL && *nm == NUL
5713 && (!check || translated_function_exists(p, is_global)))
5714 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005715
5716 vim_free(p);
5717 return NULL;
5718}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005719#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005720
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005721/*
5722 * Function given to ExpandGeneric() to obtain the list of user defined
5723 * function names.
5724 */
5725 char_u *
5726get_user_func_name(expand_T *xp, int idx)
5727{
5728 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005729 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005730 static hashitem_T *hi;
5731 ufunc_T *fp;
5732
5733 if (idx == 0)
5734 {
5735 done = 0;
5736 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005737 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005738 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005739 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005740 {
5741 if (done++ > 0)
5742 ++hi;
5743 while (HASHITEM_EMPTY(hi))
5744 ++hi;
5745 fp = HI2UF(hi);
5746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005747 // don't show dead, dict and lambda functions
5748 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005749 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005750 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005751
5752 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005753 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005754
5755 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005756 if (xp->xp_context != EXPAND_USER_FUNC
5757 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005758 {
5759 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005760 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005761 STRCAT(IObuff, ")");
5762 }
5763 return IObuff;
5764 }
5765 return NULL;
5766}
5767
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005768/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005769 * Make a copy of a function.
5770 * Intended to be used for a function defined on a base class that has a copy
5771 * on the child class.
5772 * The copy has uf_refcount set to one.
5773 * Returns NULL when out of memory.
5774 */
5775 ufunc_T *
5776copy_function(ufunc_T *fp)
5777{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005778 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005779 if (ufunc == NULL)
5780 return NULL;
5781
5782 // Most things can just be copied.
5783 *ufunc = *fp;
5784
5785 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5786 ufunc->uf_dfunc_idx = 0;
5787 ufunc->uf_class = NULL;
5788
5789 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5790 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5791
5792 if (ufunc->uf_arg_types != NULL)
5793 {
5794 // "uf_arg_types" is an allocated array, make a copy.
5795 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5796 if (at != NULL)
5797 {
5798 mch_memmove(at, ufunc->uf_arg_types,
5799 sizeof(type_T *) * ufunc->uf_args.ga_len);
5800 ufunc->uf_arg_types = at;
5801 }
5802 }
5803
5804 // TODO: how about the types themselves? they can be freed when the
5805 // original function is freed:
5806 // type_T **uf_arg_types;
5807 // type_T *uf_ret_type;
5808
Ernie Rael114ec812023-06-04 18:11:35 +01005809 // make uf_type_list empty
5810 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005811
5812 // TODO: partial_T *uf_partial;
5813
5814 if (ufunc->uf_va_name != NULL)
5815 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5816
5817 // TODO:
5818 // type_T *uf_va_type;
5819 // type_T *uf_func_type;
5820
5821 ufunc->uf_block_depth = 0;
5822 ufunc->uf_block_ids = NULL;
5823
5824 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5825
5826 ufunc->uf_refcount = 1;
5827 ufunc->uf_name_exp = NULL;
5828 STRCPY(ufunc->uf_name, fp->uf_name);
5829
5830 return ufunc;
5831}
5832
5833/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005834 * ":delfunction {name}"
5835 */
5836 void
5837ex_delfunction(exarg_T *eap)
5838{
5839 ufunc_T *fp = NULL;
5840 char_u *p;
5841 char_u *name;
5842 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005843 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005844
5845 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005846 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5847 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005848 vim_free(fudi.fd_newkey);
5849 if (name == NULL)
5850 {
5851 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005852 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005853 return;
5854 }
5855 if (!ends_excmd(*skipwhite(p)))
5856 {
5857 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005858 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005859 return;
5860 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005861 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005862 if (eap->nextcmd != NULL)
5863 *p = NUL;
5864
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005865 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005866 {
5867 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005868 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005869 vim_free(name);
5870 return;
5871 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005872 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005873 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005874 vim_free(name);
5875
5876 if (!eap->skip)
5877 {
5878 if (fp == NULL)
5879 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005880 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005881 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005882 return;
5883 }
5884 if (fp->uf_calls > 0)
5885 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005886 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005887 return;
5888 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005889 if (fp->uf_flags & FC_VIM9)
5890 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005891 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005892 return;
5893 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005894
5895 if (fudi.fd_dict != NULL)
5896 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005897 // Delete the dict item that refers to the function, it will
5898 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005899 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005900 }
5901 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005902 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005903 // A normal function (not a numbered function or lambda) has a
5904 // refcount of 1 for the entry in the hashtable. When deleting
5905 // it and the refcount is more than one, it should be kept.
5906 // A numbered function and lambda should be kept if the refcount is
5907 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005908 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005909 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005910 // Function is still referenced somewhere. Don't free it but
5911 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005912 if (func_remove(fp))
5913 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005914 }
5915 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005916 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005917 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005918 }
5919}
5920
5921/*
5922 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005923 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005924 */
5925 void
5926func_unref(char_u *name)
5927{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005928 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005929
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005930 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005931 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005932 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005933 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005934 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005935#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005936 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005937#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005938 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005939 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005940 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005941}
5942
5943/*
5944 * Unreference a Function: decrement the reference count and free it when it
5945 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005946 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005947 */
5948 void
5949func_ptr_unref(ufunc_T *fp)
5950{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005951 if (fp != NULL && (--fp->uf_refcount <= 0
5952 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5953 && fp->uf_partial->pt_refcount <= 1
5954 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005955 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005956 // Only delete it when it's not being used. Otherwise it's done
5957 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005958 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005959 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005960 }
5961}
5962
5963/*
5964 * Count a reference to a Function.
5965 */
5966 void
5967func_ref(char_u *name)
5968{
5969 ufunc_T *fp;
5970
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005971 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005972 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005973 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005974 if (fp != NULL)
5975 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005976 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005977 // Only give an error for a numbered function.
5978 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005979 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005980}
5981
5982/*
5983 * Count a reference to a Function.
5984 */
5985 void
5986func_ptr_ref(ufunc_T *fp)
5987{
5988 if (fp != NULL)
5989 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005990}
5991
5992/*
5993 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5994 * referenced from anywhere that is in use.
5995 */
5996 static int
5997can_free_funccal(funccall_T *fc, int copyID)
5998{
Bram Moolenaarca16c602022-09-06 18:57:08 +01005999 return (fc->fc_l_varlist.lv_copyID != copyID
6000 && fc->fc_l_vars.dv_copyID != copyID
6001 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006002 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006003}
6004
6005/*
6006 * ":return [expr]"
6007 */
6008 void
6009ex_return(exarg_T *eap)
6010{
6011 char_u *arg = eap->arg;
6012 typval_T rettv;
6013 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006014 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006015
6016 if (current_funccal == NULL)
6017 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006018 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006019 return;
6020 }
6021
Bram Moolenaar844fb642021-10-23 13:32:30 +01006022 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006023 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
6024
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006025 if (eap->skip)
6026 ++emsg_skip;
6027
6028 eap->nextcmd = NULL;
6029 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006030 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006031 {
6032 if (!eap->skip)
6033 returning = do_return(eap, FALSE, TRUE, &rettv);
6034 else
6035 clear_tv(&rettv);
6036 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01006037 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006038 else if (!eap->skip)
6039 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006040 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006041 update_force_abort();
6042
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006043 /*
6044 * Return unless the expression evaluation has been cancelled due to an
6045 * aborting error, an interrupt, or an exception.
6046 */
6047 if (!aborting())
6048 returning = do_return(eap, FALSE, TRUE, NULL);
6049 }
6050
Bram Moolenaare38eab22019-12-05 21:50:01 +01006051 // When skipping or the return gets pending, advance to the next command
6052 // in this line (!returning). Otherwise, ignore the rest of the line.
6053 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006054 if (returning)
6055 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006056 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006057 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006058
6059 if (eap->skip)
6060 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006061 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006062}
6063
zeertzjq5299c092023-04-12 20:48:16 +01006064/*
6065 * Lower level implementation of "call". Only called when not skipping.
6066 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006067 static int
6068ex_call_inner(
6069 exarg_T *eap,
6070 char_u *name,
6071 char_u **arg,
6072 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006073 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006074 evalarg_T *evalarg)
6075{
6076 linenr_T lnum;
6077 int doesrange;
6078 typval_T rettv;
6079 int failed = FALSE;
6080
zeertzjq5299c092023-04-12 20:48:16 +01006081 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006082 for ( ; lnum <= eap->line2; ++lnum)
6083 {
6084 funcexe_T funcexe;
6085
zeertzjq5299c092023-04-12 20:48:16 +01006086 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006087 {
6088 if (lnum > curbuf->b_ml.ml_line_count)
6089 {
6090 // If the function deleted lines or switched to another buffer
6091 // the line number may become invalid.
6092 emsg(_(e_invalid_range));
6093 break;
6094 }
6095 curwin->w_cursor.lnum = lnum;
6096 curwin->w_cursor.col = 0;
6097 curwin->w_cursor.coladd = 0;
6098 }
6099 *arg = startarg;
6100
6101 funcexe = *funcexe_init;
6102 funcexe.fe_doesrange = &doesrange;
6103 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6104 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6105 {
6106 failed = TRUE;
6107 break;
6108 }
6109 if (has_watchexpr())
6110 dbg_check_breakpoint(eap);
6111
6112 // Handle a function returning a Funcref, Dictionary or List.
6113 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006114 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006115 {
6116 failed = TRUE;
6117 break;
6118 }
6119
6120 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006121 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006122 break;
6123
6124 // Stop when immediately aborting on error, or when an interrupt
6125 // occurred or an exception was thrown but not caught.
6126 // get_func_tv() returned OK, so that the check for trailing
6127 // characters below is executed.
6128 if (aborting())
6129 break;
6130 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006131 return failed;
6132}
6133
6134/*
6135 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6136 * Returns FAIL or OK.
6137 */
6138 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006139ex_defer_inner(
6140 char_u *name,
6141 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006142 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006143 partial_T *partial,
6144 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006145{
6146 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006147 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006148 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006149 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006150
6151 if (current_funccal == NULL)
6152 {
6153 semsg(_(e_str_not_inside_function), "defer");
6154 return FAIL;
6155 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006156 if (partial != NULL)
6157 {
6158 if (partial->pt_dict != NULL)
6159 {
6160 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6161 return FAIL;
6162 }
6163 if (partial->pt_argc > 0)
6164 {
6165 int i;
6166
6167 partial_argc = partial->pt_argc;
6168 for (i = 0; i < partial_argc; ++i)
6169 copy_tv(&partial->pt_argv[i], &argvars[i]);
6170 }
6171 }
Ernie Raelb077b582023-12-14 20:11:44 +01006172 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006173 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006174 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006175 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006176
6177 if (r == OK)
6178 {
6179 if (type != NULL)
6180 {
6181 // Check that the arguments are OK for the types of the funcref.
6182 r = check_argument_types(type, argvars, argcount, NULL, name);
6183 }
Ernie Raelb077b582023-12-14 20:11:44 +01006184 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006185 {
6186 int idx = find_internal_func(name);
6187
6188 if (idx < 0)
6189 {
6190 emsg_funcname(e_unknown_function_str, name);
6191 r = FAIL;
6192 }
6193 else if (check_internal_func(idx, argcount) == -1)
6194 r = FAIL;
6195 }
6196 else
6197 {
6198 ufunc_T *ufunc = find_func(name, FALSE);
6199
6200 // we tolerate an unknown function here, it might be defined later
6201 if (ufunc != NULL)
6202 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006203 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006204 if (error != FCERR_UNKNOWN)
6205 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006206 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006207 r = FAIL;
6208 }
6209 }
6210 }
6211 }
6212
Bram Moolenaar86d87252022-09-05 21:21:25 +01006213 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006214 {
6215 while (--argcount >= 0)
6216 clear_tv(&argvars[argcount]);
6217 return FAIL;
6218 }
6219 return add_defer(name, argcount, argvars);
6220}
6221
6222/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006223 * Return TRUE if currently inside a function call.
6224 * Give an error message and return FALSE when not.
6225 */
6226 int
6227can_add_defer(void)
6228{
6229 if (!in_def_function() && get_current_funccal() == NULL)
6230 {
6231 semsg(_(e_str_not_inside_function), "defer");
6232 return FALSE;
6233 }
6234 return TRUE;
6235}
6236
6237/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006238 * Add a deferred call for "name" with arguments "argvars[argcount]".
6239 * Consumes "argvars[]".
6240 * Caller must check that in_def_function() returns TRUE or current_funccal is
6241 * not NULL.
6242 * Returns OK or FAIL.
6243 */
6244 int
6245add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6246{
6247 char_u *saved_name = vim_strsave(name);
6248 int argcount = argcount_arg;
6249 defer_T *dr;
6250 int ret = FAIL;
6251
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006252 if (saved_name == NULL)
6253 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006254 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006255 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006256 if (add_defer_function(saved_name, argcount, argvars) == OK)
6257 argcount = 0;
6258 }
6259 else
6260 {
6261 if (current_funccal->fc_defer.ga_itemsize == 0)
6262 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6263 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6264 goto theend;
6265 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6266 + current_funccal->fc_defer.ga_len++;
6267 dr->dr_name = saved_name;
6268 dr->dr_argcount = argcount;
6269 while (argcount > 0)
6270 {
6271 --argcount;
6272 dr->dr_argvars[argcount] = argvars[argcount];
6273 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006274 }
6275 ret = OK;
6276
6277theend:
6278 while (--argcount >= 0)
6279 clear_tv(&argvars[argcount]);
6280 return ret;
6281}
6282
6283/*
6284 * Invoked after a functions has finished: invoke ":defer" functions.
6285 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006286 static void
6287handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006288{
6289 int idx;
6290
Bram Moolenaar58779852022-09-06 18:31:14 +01006291 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006292 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006293 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006294
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006295 if (dr->dr_name == NULL)
6296 // already being called, can happen if function does ":qa"
6297 continue;
6298
6299 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006300 CLEAR_FIELD(funcexe);
6301 funcexe.fe_evaluate = TRUE;
6302
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006303 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006304 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006305
6306 char_u *name = dr->dr_name;
6307 dr->dr_name = NULL;
6308
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006309 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006310 // first statement in the function will be executed (because of the
6311 // exception). So save and restore the try/catch/throw exception
6312 // state.
6313 exception_state_T estate;
6314 exception_state_save(&estate);
6315 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006316
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006317 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6318
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006319 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006320
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006321 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006322 vim_free(name);
6323 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006324 clear_tv(&dr->dr_argvars[i]);
6325 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006326 ga_clear(&funccal->fc_defer);
6327}
6328
zeertzjq960cf912023-04-18 21:52:54 +01006329 static void
6330invoke_funccall_defer(funccall_T *fc)
6331{
6332 if (fc->fc_ectx != NULL)
6333 {
6334 // :def function
6335 unwind_def_callstack(fc->fc_ectx);
6336 may_invoke_defer_funcs(fc->fc_ectx);
6337 }
6338 else
6339 {
6340 // legacy function
6341 handle_defer_one(fc);
6342 }
6343}
6344
Bram Moolenaar58779852022-09-06 18:31:14 +01006345/*
6346 * Called when exiting: call all defer functions.
6347 */
6348 void
6349invoke_all_defer(void)
6350{
zeertzjq1be4b812023-04-19 14:21:24 +01006351 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6352 invoke_funccall_defer(fc);
6353
zeertzjq960cf912023-04-18 21:52:54 +01006354 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6355 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6356 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006357}
6358
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006359/*
6360 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006361 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006362 */
6363 void
6364ex_call(exarg_T *eap)
6365{
6366 char_u *arg = eap->arg;
6367 char_u *startarg;
6368 char_u *name;
6369 char_u *tofree;
6370 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006371 int failed = FALSE;
6372 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006373 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006374 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006375 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006376 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006377 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006378 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006379
Bram Moolenaare6b53242020-07-01 17:28:33 +02006380 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006381 if (eap->skip)
6382 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006383 typval_T rettv;
6384
Bram Moolenaare38eab22019-12-05 21:50:01 +01006385 // trans_function_name() doesn't work well when skipping, use eval0()
6386 // instead to skip to any following command, e.g. for:
6387 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006388 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006389 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006390 clear_tv(&rettv);
6391 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006392 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006393 return;
6394 }
6395
zeertzjq5299c092023-04-12 20:48:16 +01006396 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006397 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006398 if (fudi.fd_newkey != NULL)
6399 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006400 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006401 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006402 vim_free(fudi.fd_newkey);
6403 }
6404 if (tofree == NULL)
6405 return;
6406
Bram Moolenaare38eab22019-12-05 21:50:01 +01006407 // Increase refcount on dictionary, it could get deleted when evaluating
6408 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006409 if (fudi.fd_dict != NULL)
6410 ++fudi.fd_dict->dv_refcount;
6411
Bram Moolenaare38eab22019-12-05 21:50:01 +01006412 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6413 // contents. For VAR_PARTIAL get its partial, unless we already have one
6414 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006415 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006416 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006417 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006418 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006419
Bram Moolenaare38eab22019-12-05 21:50:01 +01006420 // Skip white space to allow ":call func ()". Not good, but required for
6421 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006422 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006423 if (*startarg != '(')
6424 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006425 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006426 goto end;
6427 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006428 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006429 {
6430 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6431 goto end;
6432 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006433
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006434 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006435 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006436 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006437 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006438 }
6439 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006440 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006441 funcexe_T funcexe;
6442
Bram Moolenaara80faa82020-04-12 19:37:17 +02006443 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006444 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006445 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006446 funcexe.fe_partial = partial;
6447 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006448 funcexe.fe_firstline = eap->line1;
6449 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006450 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006451 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006452 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006453 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006454
Bram Moolenaar1d341892021-09-18 15:25:52 +02006455 // When inside :try we need to check for following "| catch" or "| endtry".
6456 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006457 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006458 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006459 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006460 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006461 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006462 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006463 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006464 {
6465 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006466 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006467 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006468 }
6469 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006470 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006471 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006472 // Must be after using "arg", it may point into memory cleared here.
6473 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006474
6475end:
6476 dict_unref(fudi.fd_dict);
6477 vim_free(tofree);
6478}
6479
6480/*
6481 * Return from a function. Possibly makes the return pending. Also called
6482 * for a pending return at the ":endtry" or after returning from an extra
6483 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6484 * when called due to a ":return" command. "rettv" may point to a typval_T
6485 * with the return rettv. Returns TRUE when the return can be carried out,
6486 * FALSE when the return gets pending.
6487 */
6488 int
6489do_return(
6490 exarg_T *eap,
6491 int reanimate,
6492 int is_cmd,
6493 void *rettv)
6494{
6495 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006496 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006497
6498 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006499 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006500 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006501
6502 /*
6503 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6504 * not in its finally clause (which then is to be executed next) is found.
6505 * In this case, make the ":return" pending for execution at the ":endtry".
6506 * Otherwise, return normally.
6507 */
6508 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6509 if (idx >= 0)
6510 {
6511 cstack->cs_pending[idx] = CSTP_RETURN;
6512
6513 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006514 // A pending return again gets pending. "rettv" points to an
6515 // allocated variable with the rettv of the original ":return"'s
6516 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006517 cstack->cs_rettv[idx] = rettv;
6518 else
6519 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006520 // When undoing a return in order to make it pending, get the stored
6521 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006522 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006523 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006524
6525 if (rettv != NULL)
6526 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006527 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006528 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6529 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6530 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006531 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006532 }
6533 else
6534 cstack->cs_rettv[idx] = NULL;
6535
6536 if (reanimate)
6537 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006538 // The pending return value could be overwritten by a ":return"
6539 // without argument in a finally clause; reset the default
6540 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006541 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6542 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006543 }
6544 }
6545 report_make_pending(CSTP_RETURN, rettv);
6546 }
6547 else
6548 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006549 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006550
Bram Moolenaare38eab22019-12-05 21:50:01 +01006551 // If the return is carried out now, store the return value. For
6552 // a return immediately after reanimation, the value is already
6553 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006554 if (!reanimate && rettv != NULL)
6555 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006556 clear_tv(current_funccal->fc_rettv);
6557 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006558 if (!is_cmd)
6559 vim_free(rettv);
6560 }
6561 }
6562
6563 return idx < 0;
6564}
6565
6566/*
6567 * Free the variable with a pending return value.
6568 */
6569 void
6570discard_pending_return(void *rettv)
6571{
6572 free_tv((typval_T *)rettv);
6573}
6574
6575/*
6576 * Generate a return command for producing the value of "rettv". The result
6577 * is an allocated string. Used by report_pending() for verbose messages.
6578 */
6579 char_u *
6580get_return_cmd(void *rettv)
6581{
6582 char_u *s = NULL;
6583 char_u *tofree = NULL;
6584 char_u numbuf[NUMBUFLEN];
6585
6586 if (rettv != NULL)
6587 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6588 if (s == NULL)
6589 s = (char_u *)"";
6590
6591 STRCPY(IObuff, ":return ");
6592 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6593 if (STRLEN(s) + 8 >= IOSIZE)
6594 STRCPY(IObuff + IOSIZE - 4, "...");
6595 vim_free(tofree);
6596 return vim_strsave(IObuff);
6597}
6598
6599/*
6600 * Get next function line.
6601 * Called by do_cmdline() to get the next line.
6602 * Returns allocated string, or NULL for end of function.
6603 */
6604 char_u *
6605get_func_line(
6606 int c UNUSED,
6607 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006608 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006609 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006610{
6611 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006612 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006613 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006614 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006615
Bram Moolenaare38eab22019-12-05 21:50:01 +01006616 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006617 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006618 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006619 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006620 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006621 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006622 }
6623#ifdef FEAT_PROFILE
6624 if (do_profiling == PROF_YES)
6625 func_line_end(cookie);
6626#endif
6627
6628 gap = &fp->uf_lines;
6629 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006630 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006631 retval = NULL;
6632 else
6633 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006634 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006635 while (fcp->fc_linenr < gap->ga_len
6636 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6637 ++fcp->fc_linenr;
6638 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006639 retval = NULL;
6640 else
6641 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006642 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6643 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006644#ifdef FEAT_PROFILE
6645 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006646 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006647#endif
6648 }
6649 }
6650
Bram Moolenaare38eab22019-12-05 21:50:01 +01006651 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006652 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006653 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006654 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006655 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006656 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006657 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006658 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006659 }
6660
6661 return retval;
6662}
6663
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006664/*
6665 * Return TRUE if the currently active function should be ended, because a
6666 * return was encountered or an error occurred. Used inside a ":while".
6667 */
6668 int
6669func_has_ended(void *cookie)
6670{
6671 funccall_T *fcp = (funccall_T *)cookie;
6672
Bram Moolenaare38eab22019-12-05 21:50:01 +01006673 // Ignore the "abort" flag if the abortion behavior has been changed due to
6674 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006675 return (((fcp->fc_func->uf_flags & FC_ABORT)
6676 && did_emsg && !aborted_in_try())
6677 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006678}
6679
6680/*
6681 * return TRUE if cookie indicates a function which "abort"s on errors.
6682 */
6683 int
6684func_has_abort(
6685 void *cookie)
6686{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006687 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006688}
6689
6690
6691/*
6692 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6693 * Don't do this when "Func" is already a partial that was bound
6694 * explicitly (pt_auto is FALSE).
6695 * Changes "rettv" in-place.
6696 * Returns the updated "selfdict_in".
6697 */
6698 dict_T *
6699make_partial(dict_T *selfdict_in, typval_T *rettv)
6700{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006701 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006702 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006703 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006704 dict_T *selfdict = selfdict_in;
6705
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006706 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6707 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006708 fp = rettv->vval.v_partial->pt_func;
6709 else
6710 {
6711 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006712 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006713 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006714 if (fname == NULL)
6715 {
6716 // There is no point binding a dict to a NULL function, just create
6717 // a function reference.
6718 rettv->v_type = VAR_FUNC;
6719 rettv->vval.v_string = NULL;
6720 }
6721 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006722 {
6723 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006724 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006725
6726 // Translate "s:func" to the stored function name.
6727 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6728 fp = find_func(fname, FALSE);
6729 vim_free(tofree);
6730 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006731 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006732
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006733 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006734 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006735 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006736
6737 if (pt != NULL)
6738 {
6739 pt->pt_refcount = 1;
6740 pt->pt_dict = selfdict;
6741 pt->pt_auto = TRUE;
6742 selfdict = NULL;
6743 if (rettv->v_type == VAR_FUNC)
6744 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006745 // Just a function: Take over the function name and use
6746 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006747 pt->pt_name = rettv->vval.v_string;
6748 }
6749 else
6750 {
6751 partial_T *ret_pt = rettv->vval.v_partial;
6752 int i;
6753
Bram Moolenaare38eab22019-12-05 21:50:01 +01006754 // Partial: copy the function name, use selfdict and copy
6755 // args. Can't take over name or args, the partial might
6756 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006757 if (ret_pt->pt_name != NULL)
6758 {
6759 pt->pt_name = vim_strsave(ret_pt->pt_name);
6760 func_ref(pt->pt_name);
6761 }
6762 else
6763 {
6764 pt->pt_func = ret_pt->pt_func;
6765 func_ptr_ref(pt->pt_func);
6766 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006767 if (ret_pt->pt_argc > 0)
6768 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006769 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006770 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006771 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006772 pt->pt_argc = 0;
6773 else
6774 {
6775 pt->pt_argc = ret_pt->pt_argc;
6776 for (i = 0; i < pt->pt_argc; i++)
6777 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6778 }
6779 }
6780 partial_unref(ret_pt);
6781 }
6782 rettv->v_type = VAR_PARTIAL;
6783 rettv->vval.v_partial = pt;
6784 }
6785 }
6786 return selfdict;
6787}
6788
6789/*
6790 * Return the name of the executed function.
6791 */
6792 char_u *
6793func_name(void *cookie)
6794{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006795 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006796}
6797
6798/*
6799 * Return the address holding the next breakpoint line for a funccall cookie.
6800 */
6801 linenr_T *
6802func_breakpoint(void *cookie)
6803{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006804 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006805}
6806
6807/*
6808 * Return the address holding the debug tick for a funccall cookie.
6809 */
6810 int *
6811func_dbg_tick(void *cookie)
6812{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006813 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006814}
6815
6816/*
6817 * Return the nesting level for a funccall cookie.
6818 */
6819 int
6820func_level(void *cookie)
6821{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006822 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006823}
6824
6825/*
6826 * Return TRUE when a function was ended by a ":return" command.
6827 */
6828 int
6829current_func_returned(void)
6830{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006831 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006832}
6833
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006834 int
6835free_unref_funccal(int copyID, int testing)
6836{
6837 int did_free = FALSE;
6838 int did_free_funccal = FALSE;
6839 funccall_T *fc, **pfc;
6840
6841 for (pfc = &previous_funccal; *pfc != NULL; )
6842 {
6843 if (can_free_funccal(*pfc, copyID))
6844 {
6845 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006846 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006847 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006848 did_free = TRUE;
6849 did_free_funccal = TRUE;
6850 }
6851 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006852 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006853 }
6854 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006855 // When a funccal was freed some more items might be garbage
6856 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006857 (void)garbage_collect(testing);
6858
6859 return did_free;
6860}
6861
6862/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006863 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006864 */
6865 static funccall_T *
6866get_funccal(void)
6867{
6868 int i;
6869 funccall_T *funccal;
6870 funccall_T *temp_funccal;
6871
6872 funccal = current_funccal;
6873 if (debug_backtrace_level > 0)
6874 {
6875 for (i = 0; i < debug_backtrace_level; i++)
6876 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006877 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006878 if (temp_funccal)
6879 funccal = temp_funccal;
6880 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006881 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006882 debug_backtrace_level = i;
6883 }
6884 }
6885 return funccal;
6886}
6887
6888/*
6889 * Return the hashtable used for local variables in the current funccal.
6890 * Return NULL if there is no current funccal.
6891 */
6892 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006893get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006894{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006895 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006896 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006897 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006898}
6899
6900/*
6901 * Return the l: scope variable.
6902 * Return NULL if there is no current funccal.
6903 */
6904 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006905get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006906{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006907 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006908 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006909 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006910}
6911
6912/*
6913 * Return the hashtable used for argument in the current funccal.
6914 * Return NULL if there is no current funccal.
6915 */
6916 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006917get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006918{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006919 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006920 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006921 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006922}
6923
6924/*
6925 * Return the a: scope variable.
6926 * Return NULL if there is no current funccal.
6927 */
6928 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006929get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006930{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006931 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006932 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006933 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006934}
6935
6936/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006937 * List function variables, if there is a function.
6938 */
6939 void
6940list_func_vars(int *first)
6941{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006942 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6943 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006944 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006945}
6946
6947/*
6948 * If "ht" is the hashtable for local variables in the current funccal, return
6949 * the dict that contains it.
6950 * Otherwise return NULL.
6951 */
6952 dict_T *
6953get_current_funccal_dict(hashtab_T *ht)
6954{
6955 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006956 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6957 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006958 return NULL;
6959}
6960
6961/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006962 * Search hashitem in parent scope.
6963 */
6964 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006965find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006966{
6967 funccall_T *old_current_funccal = current_funccal;
6968 hashtab_T *ht;
6969 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006970 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006971
Bram Moolenaarca16c602022-09-06 18:57:08 +01006972 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006973 return NULL;
6974
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006975 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006976 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006977 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006978 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006979 ht = find_var_ht(name, &varname);
6980 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006981 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006982 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006983 if (!HASHITEM_EMPTY(hi))
6984 {
6985 *pht = ht;
6986 break;
6987 }
6988 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006989 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006990 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006991 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006992 }
6993 current_funccal = old_current_funccal;
6994
6995 return hi;
6996}
6997
6998/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006999 * Search variable in parent scope.
7000 */
7001 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007002find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007003{
7004 dictitem_T *v = NULL;
7005 funccall_T *old_current_funccal = current_funccal;
7006 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007007 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007008
Bram Moolenaarca16c602022-09-06 18:57:08 +01007009 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007010 return NULL;
7011
Bram Moolenaare38eab22019-12-05 21:50:01 +01007012 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01007013 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007014 while (current_funccal)
7015 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007016 ht = find_var_ht(name, &varname);
7017 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007018 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007019 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007020 if (v != NULL)
7021 break;
7022 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007023 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007024 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007025 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007026 }
7027 current_funccal = old_current_funccal;
7028
7029 return v;
7030}
7031
7032/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007033 * Set "copyID + 1" in previous_funccal and callers.
7034 */
7035 int
7036set_ref_in_previous_funccal(int copyID)
7037{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007038 funccall_T *fc;
7039
Bram Moolenaarca16c602022-09-06 18:57:08 +01007040 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007041 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007042 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007043 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
7044 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
7045 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007046 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007047 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007048 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007049}
7050
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007051 static int
7052set_ref_in_funccal(funccall_T *fc, int copyID)
7053{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007054 if (fc->fc_copyID != copyID)
7055 {
7056 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007057 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7058 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7059 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7060 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007061 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007062 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007063 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007064}
7065
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007066/*
7067 * Set "copyID" in all local vars and arguments in the call stack.
7068 */
7069 int
7070set_ref_in_call_stack(int copyID)
7071{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007072 funccall_T *fc;
7073 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007074
Bram Moolenaarca16c602022-09-06 18:57:08 +01007075 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007076 if (set_ref_in_funccal(fc, copyID))
7077 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007078
7079 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007080 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007081 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007082 if (set_ref_in_funccal(fc, copyID))
7083 return TRUE;
7084 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007085}
7086
7087/*
7088 * Set "copyID" in all functions available by name.
7089 */
7090 int
7091set_ref_in_functions(int copyID)
7092{
7093 int todo;
7094 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007095 ufunc_T *fp;
7096
7097 todo = (int)func_hashtab.ht_used;
7098 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007099 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007100 if (!HASHITEM_EMPTY(hi))
7101 {
7102 --todo;
7103 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007104 if (!func_name_refcount(fp->uf_name)
7105 && set_ref_in_func(NULL, fp, copyID))
7106 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007107 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007108 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007109 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007110}
7111
7112/*
7113 * Set "copyID" in all function arguments.
7114 */
7115 int
7116set_ref_in_func_args(int copyID)
7117{
7118 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007119
7120 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007121 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7122 copyID, NULL, NULL))
7123 return TRUE;
7124 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007125}
7126
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007127/*
7128 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007129 * Returns TRUE if setting references failed somehow.
7130 */
7131 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007132set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007133{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007134 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007135 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007136 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007137 char_u fname_buf[FLEN_FIXED + 1];
7138 char_u *tofree = NULL;
7139 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007140 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007141
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007142 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007143 return FALSE;
7144
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007145 if (fp_in == NULL)
7146 {
7147 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007148 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007149 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007150 if (fp != NULL)
7151 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007152 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007153 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007154 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007155
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007156 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007157 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007158}
7159
Bram Moolenaare38eab22019-12-05 21:50:01 +01007160#endif // FEAT_EVAL