blob: 9a82394013a15e767a0383fe19f857e80bc8b8f4 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020032static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010033static void func_clear(ufunc_T *fp, int force);
34static int func_free(ufunc_T *fp, int force);
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +010035static char_u *untrans_function_name(char_u *name);
Bram Moolenaar58779852022-09-06 18:31:14 +010036static void handle_defer_one(funccall_T *funccal);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020037
38 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +000039func_init(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020040{
41 hash_init(&func_hashtab);
42}
43
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020044/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020045 * Return the function hash table
46 */
47 hashtab_T *
48func_tbl_get(void)
49{
50 return &func_hashtab;
51}
52
53/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020054 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020055 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010056 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010057 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000058 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010059 * Return a pointer to after the type.
60 * When something is wrong return "arg".
61 */
62 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010063one_function_arg(
64 char_u *arg,
65 garray_T *newargs,
66 garray_T *argtypes,
67 int types_optional,
h-eastb895b0f2023-09-24 15:46:31 +020068 garray_T *arg_objm,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010069 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000070 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020071 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010072 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073{
Bram Moolenaar6e949782020-04-13 17:21:00 +020074 char_u *p = arg;
75 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020076 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077
78 while (ASCII_ISALNUM(*p) || *p == '_')
79 ++p;
Keith Thompson184f71c2024-01-04 21:19:04 +010080 if (arg == p || SAFE_isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020081 || (argtypes == NULL
82 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
83 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 {
85 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000086 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087 return arg;
88 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010089
Bram Moolenaarce723f32023-06-10 19:00:12 +010090 // Extra checks in Vim9 script.
91 if (!skip && argtypes != NULL)
92 {
93 int c = *p;
94 *p = NUL;
95 int r = check_reserved_name(arg, FALSE);
96 *p = c;
97 if (r == FAIL)
98 return arg;
99
100 // Cannot use script var name for argument. In function: also check
101 // local vars and arguments.
102 if (check_defined(arg, p - arg,
103 evalarg == NULL ? NULL : evalarg->eval_cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +0000104 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarce723f32023-06-10 19:00:12 +0100105 return arg;
106 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100108 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
109 return arg;
110 if (newargs != NULL)
111 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100112 int c;
113 int i;
114
115 c = *p;
116 *p = NUL;
117 arg_copy = vim_strsave(arg);
118 if (arg_copy == NULL)
119 {
120 *p = c;
121 return arg;
122 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200123 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200124 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200125 // Check for duplicate argument name.
126 for (i = 0; i < newargs->ga_len; ++i)
127 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
128 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000129 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200130 vim_free(arg_copy);
131 return arg;
132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
134 newargs->ga_len++;
135
136 *p = c;
137 }
138
139 // get any type from "arg: type"
h-eastb895b0f2023-09-24 15:46:31 +0200140 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK)
141 && arg_objm != NULL && (skip || ga_grow(arg_objm, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 {
143 char_u *type = NULL;
144
Bram Moolenaar6e949782020-04-13 17:21:00 +0200145 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
146 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200147 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200148 arg_copy == NULL ? arg : arg_copy);
149 p = skipwhite(p);
150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 if (*p == ':')
152 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200153 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100154 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200155 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100156 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200157 return arg;
158 }
159 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200160 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100161 if (!skip)
162 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200164 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200165 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200166 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200167 arg_copy == NULL ? arg : arg_copy);
168 return arg;
169 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100170 if (!skip)
171 {
172 if (type == NULL && types_optional)
173 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200174 type = vim_strsave((char_u *)
175 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100176 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
h-eastb895b0f2023-09-24 15:46:31 +0200177 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = FALSE;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 }
180
181 return p;
182}
183
184/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000185 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000186 * Get a next line, store it in "eap" if appropriate and put the line in
187 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000188 */
189 static char_u *
190get_function_line(
191 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000192 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000193 int indent,
194 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000195{
196 char_u *theline;
197
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100198 if (eap->ea_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000199 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000200 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100201 theline = eap->ea_getline(':', eap->cookie, indent, getline_options);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000202 if (theline != NULL)
203 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000204 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000205 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000206 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
207 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000208 *eap->cmdlinep = theline;
Bram Moolenaarb5820102023-01-25 12:27:13 +0000209 (void)ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000210 }
211
212 return theline;
213}
214
215/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200216 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100217 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100218 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200219 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100220 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200221get_function_args(
222 char_u **argp,
223 char_u endchar,
224 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100226 int types_optional, // types optional if "argtypes" is not NULL
h-eastb895b0f2023-09-24 15:46:31 +0200227 garray_T *arg_objm, // NULL unless using :def
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100228 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200229 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200230 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200231 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000232 exarg_T *eap, // can be NULL
Bram Moolenaar554d0312023-01-05 19:59:18 +0000233 int in_class, // non-zero when inside a class or interface
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000234 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000235 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200236{
237 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100238 char_u *arg;
239 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200240 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200241 int any_default = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100242 char_u *whitep = *argp;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100243 int need_expr = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200244
245 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000246 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000248 ga_init2(argtypes, sizeof(char_u *), 3);
h-eastb895b0f2023-09-24 15:46:31 +0200249 if (arg_objm != NULL)
250 ga_init2(arg_objm, sizeof(int8_T), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200251 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000252 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200253
254 if (varargs != NULL)
255 *varargs = FALSE;
256
257 /*
258 * Isolate the arguments: "arg1, arg2, ...)"
259 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100260 arg = skipwhite(*argp);
261 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200262 while (*p != endchar)
263 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100264 while (eap != NULL && eap->ea_getline != NULL
Bram Moolenaar2c330432020-04-13 14:41:35 +0200265 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200266 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200267 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000268 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000269 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000270
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200271 if (theline == NULL)
272 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200273 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200274 p = skipwhite(theline);
275 }
276
277 if (mustend && *p != endchar)
278 {
279 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000280 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100281 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200282 }
Christian Brabandte4a450a2023-12-08 20:57:38 +0100283 if (*p == endchar && !need_expr)
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200284 break;
285
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200286 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
287 {
288 if (varargs != NULL)
289 *varargs = TRUE;
290 p += 3;
291 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100292
293 if (argtypes != NULL)
294 {
295 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200296 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100298 if (!skip)
299 emsg(_(e_missing_name_after_dots));
300 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100301 }
302
303 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100304 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200305 arg_objm, evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306 if (p == arg)
307 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100308 if (*skipwhite(p) == '=')
309 {
310 emsg(_(e_cannot_use_default_for_variable_arguments));
311 break;
312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200314 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000315 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000316 {
317 // this.memberName
318 p += 5;
319 arg = p;
320 while (ASCII_ISALNUM(*p) || *p == '_')
321 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000322 char_u *argend = p;
323
h-eastdb385522023-09-28 22:18:19 +0200324 // object variable this. can be used only in a constructor
325 if (STRNCMP(eap->arg, "new", 3) != 0)
326 {
327 c = *argend;
328 *argend = NUL;
329 semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
330 *argend = c;
331 break;
332 }
333
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000334 if (*skipwhite(p) == '=')
335 {
336 char_u *defval = skipwhite(skipwhite(p) + 1);
337 if (STRNCMP(defval, "v:none", 6) != 0)
338 {
339 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
340 goto err_ret;
341 }
342 any_default = TRUE;
343 p = defval + 6;
344
345 if (ga_grow(default_args, 1) == FAIL)
346 goto err_ret;
347
348 char_u *expr = vim_strsave((char_u *)"v:none");
349 if (expr == NULL)
350 goto err_ret;
351 ((char_u **)(default_args->ga_data))
352 [default_args->ga_len] = expr;
353 default_args->ga_len++;
354 }
355 else if (any_default)
356 {
357 emsg(_(e_non_default_argument_follows_default_argument));
358 goto err_ret;
359 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000360
361 // TODO: check the argument is indeed a member
362 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
363 return FAIL;
364 if (newargs != NULL)
365 {
366 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000367 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000368 newargs->ga_len++;
369
h-eastb895b0f2023-09-24 15:46:31 +0200370 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
371 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000372 {
373 // TODO: use the actual type
374 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
375 vim_strsave((char_u *)"any");
h-eastb895b0f2023-09-24 15:46:31 +0200376 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000377
378 // Add a line to the function body for the assignment.
379 if (ga_grow(newlines, 1) == OK)
380 {
381 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000382 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
383 if (any_default)
384 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000385 char_u *assignment = alloc(len);
386 if (assignment != NULL)
387 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000388 c = *argend;
389 *argend = NUL;
390 if (any_default)
391 vim_snprintf((char *)assignment, len,
392 "ifargisset %d this.%s = %s",
393 default_args->ga_len - 1, arg, arg);
394 else
395 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000396 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000397 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000398 ((char_u **)(newlines->ga_data))[
399 newlines->ga_len++] = assignment;
400 }
401 }
402 }
403 }
404 if (*p == ',')
405 ++p;
406 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200407 else
408 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200409 char_u *np;
410
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200411 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100412 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200413 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200415 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200416
Bram Moolenaar015cf102021-06-26 21:52:02 +0200417 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200418 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200419 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200420 if (*np == '=' && np[1] != '=' && np[1] != '~'
421 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200422 {
423 typval_T rettv;
424
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100425 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200426 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100427 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000428 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200429 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200430 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200431 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200432 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200433 if (ga_grow(default_args, 1) == FAIL)
434 goto err_ret;
435
Christian Brabandte4a450a2023-12-08 20:57:38 +0100436 if (need_expr)
437 need_expr = FALSE;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200438 // trim trailing whitespace
439 while (p > expr && VIM_ISWHITE(p[-1]))
440 p--;
441 c = *p;
442 *p = NUL;
443 expr = vim_strsave(expr);
444 if (expr == NULL)
445 {
446 *p = c;
447 goto err_ret;
448 }
449 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200450 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200451 default_args->ga_len++;
452 *p = c;
453 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200454 }
455 else
Christian Brabandte4a450a2023-12-08 20:57:38 +0100456 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200457 mustend = TRUE;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100458 if (*skipwhite(p) == NUL)
459 need_expr = TRUE;
460 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200461 }
462 else if (any_default)
463 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000464 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200465 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200466 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200467
468 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
469 {
470 // Be tolerant when skipping
471 if (!skip)
472 {
473 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
474 goto err_ret;
475 }
476 p = skipwhite(p);
477 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200478 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200479 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200480 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200481 // Don't give this error when skipping, it makes the "->" not
482 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100483 // Allow missing space after comma in legacy functions.
484 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200485 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
486 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100487 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200488 goto err_ret;
489 }
490 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200491 else
492 mustend = TRUE;
493 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200494 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200495 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200496 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200497
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200498 if (*p != endchar)
499 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100500 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200501
502 *argp = p;
503 return OK;
504
505err_ret:
506 if (newargs != NULL)
507 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200508 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200509 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200510 return FAIL;
511}
512
513/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100514 * Parse the argument types, filling "fp->uf_arg_types".
515 * Return OK or FAIL.
516 */
517 static int
h-eastb895b0f2023-09-24 15:46:31 +0200518parse_argument_types(
519 ufunc_T *fp,
520 garray_T *argtypes,
521 int varargs,
522 garray_T *arg_objm,
523 ocmember_T *obj_members,
524 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100525{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200526 int len = 0;
527
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100528 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
529 if (argtypes->ga_len > 0)
530 {
531 // When "varargs" is set the last name/type goes into uf_va_name
532 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200533 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100534
535 if (len > 0)
536 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
537 if (fp->uf_arg_types != NULL)
538 {
539 int i;
540 type_T *type;
541
542 for (i = 0; i < len; ++ i)
543 {
544 char_u *p = ((char_u **)argtypes->ga_data)[i];
545
546 if (p == NULL)
547 // will get the type from the default value
548 type = &t_unknown;
549 else
h-eastb895b0f2023-09-24 15:46:31 +0200550 {
551 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
552 {
553 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
554
555 type = &t_any;
556 for (int om = 0; om < obj_member_count; ++om)
557 {
Christian Brabandt915f3bf2024-04-05 20:12:19 +0200558 if (obj_members != NULL
559 && STRCMP(aname,
560 obj_members[om].ocm_name) == 0)
h-eastb895b0f2023-09-24 15:46:31 +0200561 {
562 type = obj_members[om].ocm_type;
563 break;
564 }
565 }
566 }
567 else
568 type = parse_type(&p, &fp->uf_type_list, TRUE);
569 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100570 if (type == NULL)
571 return FAIL;
572 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000573 if (i < fp->uf_args.ga_len
574 && (type->tt_type == VAR_FUNC
Yegappan Lakshmanan3e336502024-04-04 19:35:59 +0200575 || type->tt_type == VAR_PARTIAL))
576 {
577 char_u *name = ((char_u **)fp->uf_args.ga_data)[i];
578 if (obj_members != NULL && *name == '_')
579 // protected object method
580 name++;
581
582 if (var_wrong_func_name(name, TRUE))
583 return FAIL;
584 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100585 }
586 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100587 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200588
589 if (varargs)
590 {
591 char_u *p;
592
593 // Move the last argument "...name: type" to uf_va_name and
594 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200595 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000596 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
597 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200598 p = ((char_u **)argtypes->ga_data)[len];
599 if (p == NULL)
600 // TODO: get type from default value
601 fp->uf_va_type = &t_list_any;
602 else
603 {
604 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
605 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
606 {
607 semsg(_(e_variable_arguments_type_must_be_list_str),
608 ((char_u **)argtypes->ga_data)[len]);
609 return FAIL;
610 }
611 }
612 if (fp->uf_va_type == NULL)
613 return FAIL;
614 }
615
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100616 return OK;
617}
618
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100619 static int
620parse_return_type(ufunc_T *fp, char_u *ret_type)
621{
622 if (ret_type == NULL)
623 fp->uf_ret_type = &t_void;
624 else
625 {
626 char_u *p = ret_type;
627
628 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
629 if (fp->uf_ret_type == NULL)
630 {
631 fp->uf_ret_type = &t_void;
632 return FAIL;
633 }
634 }
635 return OK;
636}
637
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100638/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200639 * Register function "fp" as using "current_funccal" as its scope.
640 */
641 static int
642register_closure(ufunc_T *fp)
643{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200644 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100645 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200646 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200647 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200648 fp->uf_scoped = current_funccal;
649 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200650
Bram Moolenaarca16c602022-09-06 18:57:08 +0100651 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200652 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100653 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
654 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200655 return OK;
656}
657
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100658 static void
659set_ufunc_name(ufunc_T *fp, char_u *name)
660{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100661 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
662 // actually extends beyond the struct.
663 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100664
665 if (name[0] == K_SPECIAL)
666 {
667 fp->uf_name_exp = alloc(STRLEN(name) + 3);
668 if (fp->uf_name_exp != NULL)
669 {
670 STRCPY(fp->uf_name_exp, "<SNR>");
671 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
672 }
673 }
674}
675
Bram Moolenaar58016442016-07-31 18:30:22 +0200676/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100677 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
678 * return "buf" filled with a readable function name.
679 * Otherwise just return "name", thus the return value can always be used.
680 * "name" and "buf" may be equal.
681 */
682 char_u *
683make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
684{
685 size_t len;
686
687 if (name[0] != K_SPECIAL)
688 return name;
689 len = STRLEN(name);
690 if (len + 3 > bufsize)
691 return name;
692
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100693 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100694 mch_memmove(buf, "<SNR>", 5);
695 return buf;
696}
697
698/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200699 * Get a name for a lambda. Returned in static memory.
700 */
701 char_u *
702get_lambda_name(void)
703{
704 static char_u name[30];
705 static int lambda_no = 0;
706
707 sprintf((char*)name, "<lambda>%d", ++lambda_no);
708 return name;
709}
710
Bram Moolenaare01e5212023-01-08 20:31:18 +0000711/*
712 * Allocate a "ufunc_T" for a function called "name".
713 * Makes sure the size is right.
714 */
715 static ufunc_T *
716alloc_ufunc(char_u *name)
717{
718 // When the name is short we need to make sure we allocate enough bytes for
719 // the whole struct, including any padding.
720 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
721 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
722}
723
Bram Moolenaar801ab062020-06-25 19:27:56 +0200724#if defined(FEAT_LUA) || defined(PROTO)
725/*
726 * Registers a native C callback which can be called from Vim script.
727 * Returns the name of the Vim script function.
728 */
729 char_u *
730register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
731{
732 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200733 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200734
Bram Moolenaare01e5212023-01-08 20:31:18 +0000735 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200736 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200737 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200738
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200739 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200740 fp->uf_refcount = 1;
741 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000742 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200743 fp->uf_calls = 0;
744 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200745 fp->uf_cb = cb;
746 fp->uf_cb_free = cb_free;
747 fp->uf_cb_state = state;
748
749 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000750 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200751
752 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200753}
754#endif
755
Bram Moolenaar04b12692020-05-04 23:24:44 +0200756/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100757 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100758 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100759 * If "white_error" is not NULL check for correct use of white space and set
760 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100761 * Return NULL if no valid arrow found.
762 */
763 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100764skip_arrow(
765 char_u *start,
766 int equal_arrow,
767 char_u **ret_type,
768 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100769{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100770 char_u *s = start;
771 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100772
773 if (equal_arrow)
774 {
775 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100776 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100777 if (white_error != NULL && !VIM_ISWHITE(s[1]))
778 {
779 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100780 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100781 return NULL;
782 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100783 s = skipwhite(s + 1);
784 *ret_type = s;
785 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100786 if (s == *ret_type)
787 {
788 emsg(_(e_missing_return_type));
789 return NULL;
790 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100791 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100792 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100793 s = skipwhite(s);
794 if (*s != '=')
795 return NULL;
796 ++s;
797 }
798 if (*s != '>')
799 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100800 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
801 || !IS_WHITE_OR_NUL(s[1])))
802 {
803 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100804 semsg(_(e_white_space_required_before_and_after_str_at_str),
805 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100806 return NULL;
807 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100808 return skipwhite(s + 1);
809}
810
811/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100812 * Check if "*cmd" points to a function command and if so advance "*cmd" and
813 * return TRUE.
814 * Otherwise return FALSE;
815 * Do not consider "function(" to be a command.
816 */
817 static int
818is_function_cmd(char_u **cmd)
819{
820 char_u *p = *cmd;
821
822 if (checkforcmd(&p, "function", 2))
823 {
824 if (*p == '(')
825 return FALSE;
826 *cmd = p;
827 return TRUE;
828 }
829 return FALSE;
830}
831
832/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200833 * Called when defining a function: The context may be needed for script
834 * variables declared in a block that is visible now but not when the function
835 * is compiled or called later.
836 */
837 static void
838function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
839{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000840 if (cstack == NULL || cstack->cs_idx < 0)
841 return;
842
843 int count = cstack->cs_idx + 1;
844 int i;
845
846 fp->uf_block_ids = ALLOC_MULT(int, count);
847 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200848 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000849 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
850 sizeof(int) * count);
851 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200852 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000853
854 // Set flag in each block to indicate a function was defined. This
855 // is used to keep the variable when leaving the block, see
856 // hide_script_var().
857 for (i = 0; i <= cstack->cs_idx; ++i)
858 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200859}
860
861/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100862 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200863 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100864 * "newlines" must already have been initialized.
865 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
866 */
867 static int
868get_function_body(
869 exarg_T *eap,
870 garray_T *newlines,
871 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000872 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100873{
874 linenr_T sourcing_lnum_top = SOURCING_LNUM;
875 linenr_T sourcing_lnum_off;
876 int saved_wait_return = need_wait_return;
877 char_u *line_arg = line_arg_in;
878 int vim9_function = eap->cmdidx == CMD_def
879 || eap->cmdidx == CMD_block;
880#define MAX_FUNC_NESTING 50
881 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200882 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100883 int nesting = 0;
884 getline_opt_T getline_options;
885 int indent = 2;
886 char_u *skip_until = NULL;
887 int ret = FAIL;
888 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200889 int heredoc_concat_len = 0;
890 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100891 char_u *heredoc_trimmed = NULL;
892
Bram Moolenaar20677332021-06-06 17:02:53 +0200893 ga_init2(&heredoc_ga, 1, 500);
894
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100895 // Detect having skipped over comment lines to find the return
896 // type. Add NULL lines to keep the line count correct.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100897 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100898 if (SOURCING_LNUM < sourcing_lnum_off)
899 {
900 sourcing_lnum_off -= SOURCING_LNUM;
901 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
902 goto theend;
903 while (sourcing_lnum_off-- > 0)
904 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
905 }
906
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200907 nesting_def[0] = vim9_function;
908 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100909 getline_options = vim9_function
910 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
911 for (;;)
912 {
913 char_u *theline;
914 char_u *p;
915 char_u *arg;
916
917 if (KeyTyped)
918 {
919 msg_scroll = TRUE;
920 saved_wait_return = FALSE;
921 }
922 need_wait_return = FALSE;
923
924 if (line_arg != NULL)
925 {
926 // Use eap->arg, split up in parts by line breaks.
927 theline = line_arg;
928 p = vim_strchr(theline, '\n');
929 if (p == NULL)
930 line_arg += STRLEN(line_arg);
931 else
932 {
933 *p = NUL;
934 line_arg = p + 1;
935 }
936 }
937 else
938 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000939 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100940 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100941 }
942 if (KeyTyped)
943 lines_left = Rows - 1;
944 if (theline == NULL)
945 {
946 // Use the start of the function for the line number.
947 SOURCING_LNUM = sourcing_lnum_top;
948 if (skip_until != NULL)
949 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200950 else if (nesting_inline[nesting])
951 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100952 else if (eap->cmdidx == CMD_def)
953 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100954 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000955 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100956 goto theend;
957 }
958
959 // Detect line continuation: SOURCING_LNUM increased more than one.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100960 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100961 if (SOURCING_LNUM < sourcing_lnum_off)
962 sourcing_lnum_off -= SOURCING_LNUM;
963 else
964 sourcing_lnum_off = 0;
965
966 if (skip_until != NULL)
967 {
968 // Don't check for ":endfunc"/":enddef" between
969 // * ":append" and "."
970 // * ":python <<EOF" and "EOF"
971 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
972 if (heredoc_trimmed == NULL
973 || (is_heredoc && skipwhite(theline) == theline)
974 || STRNCMP(theline, heredoc_trimmed,
975 STRLEN(heredoc_trimmed)) == 0)
976 {
977 if (heredoc_trimmed == NULL)
978 p = theline;
979 else if (is_heredoc)
980 p = skipwhite(theline) == theline
981 ? theline : theline + STRLEN(heredoc_trimmed);
982 else
983 p = theline + STRLEN(heredoc_trimmed);
984 if (STRCMP(p, skip_until) == 0)
985 {
986 VIM_CLEAR(skip_until);
987 VIM_CLEAR(heredoc_trimmed);
988 getline_options = vim9_function
989 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
990 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200991
992 if (heredoc_concat_len > 0)
993 {
994 // Replace the starting line with all the concatenated
995 // lines.
996 ga_concat(&heredoc_ga, theline);
997 vim_free(((char_u **)(newlines->ga_data))[
998 heredoc_concat_len - 1]);
999 ((char_u **)(newlines->ga_data))[
1000 heredoc_concat_len - 1] = heredoc_ga.ga_data;
1001 ga_init(&heredoc_ga);
1002 heredoc_concat_len = 0;
1003 theline += STRLEN(theline); // skip the "EOF"
1004 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001005 }
1006 }
1007 }
1008 else
1009 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001010 int c;
1011 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001012 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001013
1014 // skip ':' and blanks
1015 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1016 ;
1017
1018 // Check for "endfunction", "enddef" or "}".
1019 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001020 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001021 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001022 ? *p == '}'
1023 : (checkforcmd(&p, nesting_def[nesting]
1024 ? "enddef" : "endfunction", 4)
1025 && *p != ':'))
1026 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001027 if (!nesting_inline[nesting] && nesting_def[nesting]
1028 && p < cmd + 6)
1029 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001030 if (nesting-- == 0)
1031 {
1032 char_u *nextcmd = NULL;
1033
1034 if (*p == '|' || *p == '}')
1035 nextcmd = p + 1;
1036 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1037 nextcmd = line_arg;
1038 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001039 && (vim9_function || p_verbose > 0))
1040 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001041 SOURCING_LNUM = sourcing_lnum_top
1042 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001043 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001044 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001045 else
1046 give_warning2((char_u *)
1047 _("W22: Text found after :endfunction: %s"),
1048 p, TRUE);
1049 }
1050 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001051 {
1052 // Another command follows. If the line came from "eap"
1053 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001054 // change "eap->cmdlinep" to point to the last fetched
1055 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001056 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001057 if (lines_to_free->ga_len > 0
1058 && *eap->cmdlinep !=
1059 ((char_u **)lines_to_free->ga_data)
1060 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001061 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001062 // *cmdlinep will be freed later, thus remove the
1063 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001064 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001065 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1066 [lines_to_free->ga_len - 1];
1067 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001068 }
1069 }
1070 break;
1071 }
1072 }
1073
1074 // Check for mismatched "endfunc" or "enddef".
1075 // We don't check for "def" inside "func" thus we also can't check
1076 // for "enddef".
1077 // We continue to find the end of the function, although we might
1078 // not find it.
1079 else if (nesting_def[nesting])
1080 {
1081 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1082 emsg(_(e_mismatched_endfunction));
1083 }
1084 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1085 emsg(_(e_mismatched_enddef));
1086
1087 // Increase indent inside "if", "while", "for" and "try", decrease
1088 // at "end".
1089 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1090 indent -= 2;
1091 else if (STRNCMP(p, "if", 2) == 0
1092 || STRNCMP(p, "wh", 2) == 0
1093 || STRNCMP(p, "for", 3) == 0
1094 || STRNCMP(p, "try", 3) == 0)
1095 indent += 2;
1096
1097 // Check for defining a function inside this function.
1098 // Only recognize "def" inside "def", not inside "function",
1099 // For backwards compatibility, see Test_function_python().
1100 c = *p;
1101 if (is_function_cmd(&p)
1102 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1103 {
1104 if (*p == '!')
1105 p = skipwhite(p + 1);
1106 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001107 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001108 if (*skipwhite(p) == '(')
1109 {
1110 if (nesting == MAX_FUNC_NESTING - 1)
1111 emsg(_(e_function_nesting_too_deep));
1112 else
1113 {
1114 ++nesting;
1115 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001116 nesting_inline[nesting] = FALSE;
1117 indent += 2;
1118 }
1119 }
1120 }
1121
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001122 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001123 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001124 // Not a comment line: check for nested inline function.
1125 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001126 while (end > p && VIM_ISWHITE(*end))
1127 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001128 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001129 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001130 int is_block;
1131
1132 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001133 --end;
1134 while (end > p && VIM_ISWHITE(*end))
1135 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001136 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1137 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001138 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001139 char_u *s = p;
1140
1141 // check for line starting with "au" for :autocmd or
1142 // "com" for :command, these can use a {} block
1143 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1144 || checkforcmd_noparen(&s, "command", 3);
1145 }
1146
1147 if (is_block)
1148 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001149 if (nesting == MAX_FUNC_NESTING - 1)
1150 emsg(_(e_function_nesting_too_deep));
1151 else
1152 {
1153 ++nesting;
1154 nesting_def[nesting] = TRUE;
1155 nesting_inline[nesting] = TRUE;
1156 indent += 2;
1157 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001158 }
1159 }
1160 }
1161
1162 // Check for ":append", ":change", ":insert". Not for :def.
1163 p = skip_range(p, FALSE, NULL);
1164 if (!vim9_function
1165 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1166 || (p[0] == 'c'
1167 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1168 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1169 && (STRNCMP(&p[3], "nge", 3) != 0
1170 || !ASCII_ISALPHA(p[6])))))))
1171 || (p[0] == 'i'
1172 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1173 && (!ASCII_ISALPHA(p[2])
1174 || (p[2] == 's'
1175 && (!ASCII_ISALPHA(p[3])
1176 || p[3] == 'e'))))))))
1177 skip_until = vim_strsave((char_u *)".");
1178
1179 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1180 arg = skipwhite(skiptowhite(p));
1181 if (arg[0] == '<' && arg[1] =='<'
1182 && ((p[0] == 'p' && p[1] == 'y'
1183 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1184 || ((p[2] == '3' || p[2] == 'x')
1185 && !ASCII_ISALPHA(p[3]))))
1186 || (p[0] == 'p' && p[1] == 'e'
1187 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1188 || (p[0] == 't' && p[1] == 'c'
1189 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1190 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1191 && !ASCII_ISALPHA(p[3]))
1192 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1193 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1194 || (p[0] == 'm' && p[1] == 'z'
1195 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1196 ))
1197 {
1198 // ":python <<" continues until a dot, like ":append"
1199 p = skipwhite(arg + 2);
1200 if (STRNCMP(p, "trim", 4) == 0)
1201 {
1202 // Ignore leading white space.
1203 p = skipwhite(p + 4);
1204 heredoc_trimmed = vim_strnsave(theline,
1205 skipwhite(theline) - theline);
1206 }
1207 if (*p == NUL)
1208 skip_until = vim_strsave((char_u *)".");
1209 else
1210 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1211 getline_options = GETLINE_NONE;
1212 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001213 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001214 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001215 }
1216
Bram Moolenaard881d152022-05-13 13:50:36 +01001217 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001218 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001219 // Check for ":cmd v =<< [trim] EOF"
1220 // and ":cmd [a, b] =<< [trim] EOF"
1221 // and "lines =<< [trim] EOF" for Vim9
1222 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001223 arg = p;
1224 if (checkforcmd(&arg, "let", 2)
1225 || checkforcmd(&arg, "var", 3)
1226 || checkforcmd(&arg, "final", 5)
1227 || checkforcmd(&arg, "const", 5)
1228 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001229 {
zeertzjq9a91d2b2024-04-09 21:47:10 +02001230 int save_sc_version = current_sctx.sc_version;
1231 int var_count = 0;
1232 int semicolon = 0;
zeertzjq9a91d2b2024-04-09 21:47:10 +02001233
1234 current_sctx.sc_version
1235 = vim9_function ? SCRIPT_VERSION_VIM9 : 1;
zeertzjq1817ccd2024-04-10 17:37:47 +02001236 arg = skip_var_list(arg, TRUE, &var_count, &semicolon,
zeertzjq9a91d2b2024-04-09 21:47:10 +02001237 TRUE);
zeertzjq1817ccd2024-04-10 17:37:47 +02001238 if (arg != NULL)
1239 arg = skipwhite(arg);
zeertzjq9a91d2b2024-04-09 21:47:10 +02001240 current_sctx.sc_version = save_sc_version;
zeertzjq1817ccd2024-04-10 17:37:47 +02001241 if (arg != NULL && STRNCMP(arg, "=<<", 3) == 0)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001242 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001243 p = skipwhite(arg + 3);
1244 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001245 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001246 if (STRNCMP(p, "trim", 4) == 0)
1247 {
1248 // Ignore leading white space.
1249 p = skipwhite(p + 4);
1250 heredoc_trimmed = vim_strnsave(theline,
1251 skipwhite(theline) - theline);
1252 continue;
1253 }
1254 if (STRNCMP(p, "eval", 4) == 0)
1255 {
1256 // Ignore leading white space.
1257 p = skipwhite(p + 4);
1258 continue;
1259 }
1260 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001261 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001262 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1263 getline_options = GETLINE_NONE;
1264 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001265 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001266 }
1267 }
1268 }
1269
1270 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001271 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001272 goto theend;
1273
Bram Moolenaar20677332021-06-06 17:02:53 +02001274 if (heredoc_concat_len > 0)
1275 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001276 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001277 // to be used for the instruction later.
1278 ga_concat(&heredoc_ga, theline);
1279 ga_concat(&heredoc_ga, (char_u *)"\n");
1280 p = vim_strsave((char_u *)"");
1281 }
1282 else
1283 {
1284 // Copy the line to newly allocated memory. get_one_sourceline()
1285 // allocates 250 bytes per line, this saves 80% on average. The
1286 // cost is an extra alloc/free.
1287 p = vim_strsave(theline);
1288 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001289 if (p == NULL)
1290 goto theend;
1291 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1292
1293 // Add NULL lines for continuation lines, so that the line count is
1294 // equal to the index in the growarray.
1295 while (sourcing_lnum_off-- > 0)
1296 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1297
1298 // Check for end of eap->arg.
1299 if (line_arg != NULL && *line_arg == NUL)
1300 line_arg = NULL;
1301 }
1302
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001303 // Return OK when no error was detected.
1304 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001305 ret = OK;
1306
1307theend:
1308 vim_free(skip_until);
1309 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001310 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001311 need_wait_return |= saved_wait_return;
1312 return ret;
1313}
1314
1315/*
1316 * Handle the body of a lambda. *arg points to the "{", process statements
1317 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001318 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001319 * When successful "rettv" is set to a funcref.
1320 */
1321 static int
1322lambda_function_body(
1323 char_u **arg,
1324 typval_T *rettv,
1325 evalarg_T *evalarg,
1326 garray_T *newargs,
1327 garray_T *argtypes,
1328 int varargs,
1329 garray_T *default_args,
1330 char_u *ret_type)
1331{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001332 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001333 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001334 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001335 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001336 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001337 exarg_T eap;
1338 garray_T newlines;
1339 char_u *cmdline = NULL;
1340 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001341 partial_T *pt;
1342 char_u *name;
1343 int lnum_save = -1;
1344 linenr_T sourcing_lnum_top = SOURCING_LNUM;
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001345 char_u *line_arg = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001346
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001347 *arg = skipwhite(*arg + 1);
1348 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001349 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001350 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001351 return FAIL;
1352 }
1353
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001354 // When there is a line break use what follows for the lambda body.
1355 // Makes lambda body initializers work for object and enum member
1356 // variables.
1357 if (**arg == '\n')
1358 line_arg = *arg + 1;
1359
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001360 CLEAR_FIELD(eap);
1361 eap.cmdidx = CMD_block;
1362 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001363 eap.cmdlinep = &cmdline;
1364 eap.skip = !evaluate;
1365 if (evalarg->eval_cctx != NULL)
1366 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1367 else
1368 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001369 eap.ea_getline = evalarg->eval_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001370 eap.cookie = evalarg->eval_cookie;
1371 }
1372
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001373 ga_init2(&newlines, sizeof(char_u *), 10);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001374 if (get_function_body(&eap, &newlines, line_arg,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001375 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001376 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001377
1378 // When inside a lambda must add the function lines to evalarg.eval_ga.
1379 evalarg->eval_break_count += newlines.ga_len;
1380 if (gap->ga_itemsize > 0)
1381 {
1382 int idx;
1383 char_u *last;
1384 size_t plen;
1385 char_u *pnl;
1386
1387 for (idx = 0; idx < newlines.ga_len; ++idx)
1388 {
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001389 char_u *p = ((char_u **)newlines.ga_data)[idx];
1390 if (p == NULL)
1391 // comment line in the lambda body
1392 continue;
1393
1394 p = skipwhite(p);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001395
Bram Moolenaarecb66452021-05-18 15:09:18 +02001396 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001397 goto erret;
1398
1399 // Going to concatenate the lines after parsing. For an empty or
1400 // comment line use an empty string.
1401 // Insert NL characters at the start of each line, the string will
1402 // be split again later in .get_lambda_tv().
1403 if (*p == NUL || vim9_comment_start(p))
1404 p = (char_u *)"";
1405 plen = STRLEN(p);
1406 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1407 if (pnl != NULL)
1408 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001409 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1410 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001411 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001412 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001413 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001414 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001415 // more is following after the "}", which was skipped
1416 last = cmdline;
1417 else
1418 // nothing is following the "}"
1419 last = (char_u *)"}";
1420 plen = STRLEN(last);
1421 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1422 if (pnl != NULL)
1423 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001424 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1425 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001426 }
1427
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001428 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001429 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001430 garray_T *tfgap = &evalarg->eval_tofree_ga;
1431
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001432 // Something comes after the "}".
1433 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001434
1435 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001436 if (ga_grow(tfgap, 1) == OK)
1437 {
1438 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1439 evalarg->eval_using_cmdline = TRUE;
1440 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001441 }
1442 else
1443 *arg = (char_u *)"";
1444
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001445 if (!evaluate)
1446 {
1447 ret = OK;
1448 goto erret;
1449 }
1450
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001451 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001452 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001453 if (ufunc == NULL)
1454 goto erret;
1455 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001456 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001457 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001458 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001459 ufunc->uf_refcount = 1;
1460 ufunc->uf_args = *newargs;
1461 newargs->ga_data = NULL;
1462 ufunc->uf_def_args = *default_args;
1463 default_args->ga_data = NULL;
1464 ufunc->uf_func_type = &t_func_any;
1465
1466 // error messages are for the first function line
1467 lnum_save = SOURCING_LNUM;
1468 SOURCING_LNUM = sourcing_lnum_top;
1469
1470 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001471 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001472 {
1473 SOURCING_LNUM = lnum_save;
1474 goto erret;
1475 }
1476
1477 // parse the return type, if any
1478 if (parse_return_type(ufunc, ret_type) == FAIL)
1479 goto erret;
1480
1481 pt = ALLOC_CLEAR_ONE(partial_T);
1482 if (pt == NULL)
1483 goto erret;
1484 pt->pt_func = ufunc;
1485 pt->pt_refcount = 1;
1486
1487 ufunc->uf_lines = newlines;
1488 newlines.ga_data = NULL;
1489 if (sandbox)
1490 ufunc->uf_flags |= FC_SANDBOX;
1491 if (!ASCII_ISUPPER(*ufunc->uf_name))
1492 ufunc->uf_flags |= FC_VIM9;
1493 ufunc->uf_script_ctx = current_sctx;
1494 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1495 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1496 set_function_type(ufunc);
1497
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001498 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1499
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001500 rettv->vval.v_partial = pt;
1501 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001502 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001503 ret = OK;
1504
1505erret:
1506 if (lnum_save >= 0)
1507 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001508 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001509 if (newargs != NULL)
1510 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001511 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001512 if (ufunc != NULL)
1513 {
1514 func_clear(ufunc, TRUE);
1515 func_free(ufunc, TRUE);
1516 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001517 return ret;
1518}
1519
1520/*
1521 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001522 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001523 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001524 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1525 */
1526 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001527get_lambda_tv(
1528 char_u **arg,
1529 typval_T *rettv,
1530 int types_optional,
1531 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001532{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001533 int evaluate = evalarg != NULL
1534 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001535 garray_T newargs;
1536 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001537 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001538 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001539 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001540 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001541 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001542 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001543 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001544 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001545 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001546 char_u *s;
1547 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001548 int *old_eval_lavars = eval_lavars_used;
1549 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001550 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001551 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001552 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001553 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001554 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001555 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001556
Bram Moolenaar4525a572022-02-13 11:57:33 +00001557 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001558 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001559
1560 ga_init(&newargs);
1561 ga_init(&newlines);
1562
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001563 // First, check if this is really a lambda expression. "->" or "=>" must
1564 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001565 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001566 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001567 types_optional ? &argtypes : NULL, types_optional,
1568 types_optional ? &arg_objm : NULL, evalarg,
1569 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001570 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001571 {
1572 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001573 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001574 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001575 ga_clear(&arg_objm);
1576 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001577 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001578 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001579
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001580 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001581 if (evaluate)
1582 pnewargs = &newargs;
1583 else
1584 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001585 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001586 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001587 types_optional ? &argtypes : NULL, types_optional,
1588 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001589 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001590 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001591 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001592 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001593 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001594 {
1595 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001596 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001597 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001598 ga_clear(&arg_objm);
1599 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001600 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001601 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001602 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001603 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001604
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001605 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1606 if (ret_type != NULL)
1607 {
1608 ret_type = vim_strsave(ret_type);
1609 tofree2 = ret_type;
1610 }
1611
Bram Moolenaare38eab22019-12-05 21:50:01 +01001612 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001613 if (evaluate)
1614 eval_lavars_used = &eval_lavars;
1615
Bram Moolenaar65c44152020-12-24 15:14:01 +01001616 *arg = skipwhite_and_linebreak(*arg, evalarg);
1617
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001618 // Recognize "{" as the start of a function body.
1619 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001620 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001621 if (evalarg == NULL)
1622 // cannot happen?
1623 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001624 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001625 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1626 types_optional ? &argtypes : NULL, varargs,
1627 &default_args, ret_type) == FAIL)
1628 goto errret;
1629 goto theend;
1630 }
1631 if (default_args.ga_len > 0)
1632 {
1633 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001634 goto errret;
1635 }
1636
Bram Moolenaare38eab22019-12-05 21:50:01 +01001637 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001638 start = *arg;
1639 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640 if (ret == FAIL)
1641 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001642
Bram Moolenaar65c44152020-12-24 15:14:01 +01001643 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001644 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001645 *arg = skipwhite_and_linebreak(*arg, evalarg);
1646 if (**arg != '}')
1647 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001648 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001649 goto errret;
1650 }
1651 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001652 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001653
1654 if (evaluate)
1655 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001656 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001657 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001658 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001659 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001660 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001661
Bram Moolenaare01e5212023-01-08 20:31:18 +00001662 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001663 if (fp == NULL)
1664 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001665 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001666 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001667 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001668 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001669
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001670 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001671 if (ga_grow(&newlines, 1) == FAIL)
1672 goto errret;
1673
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001674 // If there are line breaks, we need to split up the string.
1675 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001676 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001677 line_end = end;
1678
1679 // Add "return " before the expression (or the first line).
1680 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001681 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001682 if (p == NULL)
1683 goto errret;
1684 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1685 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001686 vim_strncpy(p + 7, start, line_end - start);
1687
1688 if (line_end != end)
1689 {
1690 // Add more lines, split by line breaks. Thus is used when a
1691 // lambda with { cmds } is encountered.
1692 while (*line_end == '\n')
1693 {
1694 if (ga_grow(&newlines, 1) == FAIL)
1695 goto errret;
1696 start = line_end + 1;
1697 line_end = vim_strchr(start, '\n');
1698 if (line_end == NULL)
1699 line_end = end;
1700 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1701 vim_strnsave(start, line_end - start);
1702 }
1703 }
1704
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001705 if (strstr((char *)p + 7, "a:") == NULL)
1706 // No a: variables are used for sure.
1707 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001708
1709 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001710 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001711 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001712 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001713 if (types_optional)
1714 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001715 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001716 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001717 goto errret;
1718 if (ret_type != NULL)
1719 {
1720 fp->uf_ret_type = parse_type(&ret_type,
1721 &fp->uf_type_list, TRUE);
1722 if (fp->uf_ret_type == NULL)
1723 goto errret;
1724 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001725 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001726 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001727 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001728
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001729 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001730 if (current_funccal != NULL && eval_lavars)
1731 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001732 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001733 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001734 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001735 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001736
1737#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001738 if (prof_def_func())
1739 func_do_profile(fp);
1740#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001741 if (sandbox)
1742 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001743 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001744 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001745 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001746 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001747 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001748 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001749 // Use the line number of the arguments.
1750 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001751
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001752 function_using_block_scopes(fp, evalarg->eval_cstack);
1753
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001754 pt->pt_func = fp;
1755 pt->pt_refcount = 1;
1756 rettv->vval.v_partial = pt;
1757 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001758
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001759 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001760 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001761
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001762theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001763 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001764 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001765 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001766 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001767 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001768 ga_clear(&arg_objm);
1769 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001770
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001771 return OK;
1772
1773errret:
1774 ga_clear_strings(&newargs);
1775 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001776 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001777 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001778 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001779 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001780 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001781 if (fp != NULL)
1782 vim_free(fp->uf_arg_types);
1783 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001784 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001785 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001786 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001787 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788 return FAIL;
1789}
1790
1791/*
1792 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1793 * name it contains, otherwise return "name".
1794 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1795 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001796 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1797 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001798 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001799 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001800 */
1801 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001802deref_func_name(
1803 char_u *name,
1804 int *lenp,
1805 partial_T **partialp,
1806 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001807 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001808 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001809 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001810{
1811 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001812 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001813 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001814 char_u *s = NULL;
1815 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001816 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001817
1818 if (partialp != NULL)
1819 *partialp = NULL;
1820
1821 cc = name[*lenp];
1822 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001823
Bram Moolenaar71f21932022-01-07 18:20:55 +00001824 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001825 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001826 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001827 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001828 tv = &v->di_tv;
1829 }
1830 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1831 {
1832 imported_T *import;
1833 char_u *p = name;
1834 int len = *lenp;
1835
1836 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001837 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001838 p = name + 2;
1839 len -= 2;
1840 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001841 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001842
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001843 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001844 if (import != NULL)
1845 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001846 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001847 if (new_function)
1848 semsg(_(e_redefining_imported_item_str), name);
1849 else
1850 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001851 name[len] = cc;
1852 *lenp = 0;
1853 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001854 }
1855 }
1856
1857 if (tv != NULL)
1858 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001859 if (found_var != NULL)
1860 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001861 if (tv->v_type == VAR_FUNC)
1862 {
1863 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001864 {
1865 *lenp = 0;
1866 return (char_u *)""; // just in case
1867 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001868 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001869 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001870 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001871
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001872 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001873 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001874 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001875
1876 if (pt == NULL)
1877 {
1878 *lenp = 0;
1879 return (char_u *)""; // just in case
1880 }
1881 if (partialp != NULL)
1882 *partialp = pt;
1883 s = partial_name(pt);
1884 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001885 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001886
1887 if (s != NULL)
1888 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001889 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001890 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001891 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001892
1893 if (sv != NULL)
1894 *type = sv->sv_type;
1895 }
1896 return s;
1897 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001898 }
1899
1900 return name;
1901}
1902
1903/*
1904 * Give an error message with a function name. Handle <SNR> things.
1905 * "ermsg" is to be passed without translation, use N_() instead of _().
1906 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001907 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001908emsg_funcname(char *ermsg, char_u *name)
1909{
Bram Moolenaar54598062022-01-13 12:05:09 +00001910 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001911
Bram Moolenaar54598062022-01-13 12:05:09 +00001912 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001913 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001914 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001915 if (p != name)
1916 vim_free(p);
1917}
1918
1919/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001920 * Get function arguments at "*arg" and advance it.
1921 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001922 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001923 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001924 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001925get_func_arguments(
1926 char_u **arg,
1927 evalarg_T *evalarg,
1928 int partial_argc,
1929 typval_T *argvars,
Ernie Raelb077b582023-12-14 20:11:44 +01001930 int *argcount,
1931 int is_builtin)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001932{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001933 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001934 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001935 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001936 int evaluate = evalarg == NULL
1937 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001938
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001939 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001940 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001941 // skip the '(' or ',' and possibly line breaks
1942 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1943
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001944 if (*argp == ')' || *argp == ',' || *argp == NUL)
1945 break;
Ernie Raelb077b582023-12-14 20:11:44 +01001946
1947 int arg_idx = *argcount;
1948 if (eval1(&argp, &argvars[arg_idx], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001949 {
1950 ret = FAIL;
1951 break;
1952 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001953 ++*argcount;
Ernie Raelb077b582023-12-14 20:11:44 +01001954 if (!is_builtin && check_typval_is_value(&argvars[arg_idx]) == FAIL)
1955 {
1956 ret = FAIL;
1957 break;
1958 }
1959
Bram Moolenaar9d489562020-07-30 20:08:50 +02001960 // The comma should come right after the argument, but this wasn't
1961 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001962 if (vim9script)
1963 {
1964 if (*argp != ',' && *skipwhite(argp) == ',')
1965 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001966 if (evaluate)
1967 semsg(_(e_no_white_space_allowed_before_str_str),
1968 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001969 ret = FAIL;
1970 break;
1971 }
1972 }
1973 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001974 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001975 if (*argp != ',')
1976 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001977 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001978 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001979 if (evaluate)
1980 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001981 ret = FAIL;
1982 break;
1983 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001984 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001985
Bram Moolenaare6b53242020-07-01 17:28:33 +02001986 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001987 if (*argp == ')')
1988 ++argp;
1989 else
1990 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001991 *arg = argp;
1992 return ret;
1993}
1994
1995/*
1996 * Call a function and put the result in "rettv".
1997 * Return OK or FAIL.
1998 */
1999 int
2000get_func_tv(
2001 char_u *name, // name of the function
2002 int len, // length of "name" or -1 to use strlen()
2003 typval_T *rettv,
2004 char_u **arg, // argument, pointing to the '('
2005 evalarg_T *evalarg, // for line continuation
2006 funcexe_T *funcexe) // various values
2007{
2008 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002009 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002010 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
2011 int argcount = 0; // number of arguments found
2012 int vim9script = in_vim9script();
2013 int evaluate = evalarg == NULL
2014 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
2015
2016 argp = *arg;
2017 ret = get_func_arguments(&argp, evalarg,
2018 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
Ernie Raelb077b582023-12-14 20:11:44 +01002019 argvars, &argcount, builtin_function(name, -1));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002020
2021 if (ret == OK)
2022 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002023 int i = 0;
2024 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002025
2026 if (get_vim_var_nr(VV_TESTING))
2027 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002028 // Prepare for calling test_garbagecollect_now(), need to know
2029 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002030 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002031 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002032 for (i = 0; i < argcount; ++i)
2033 if (ga_grow(&funcargs, 1) == OK)
2034 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2035 &argvars[i];
2036 }
2037
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002038 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002039 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002040 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002041 // An error in a builtin function does not return FAIL, but we do
2042 // want to abort further processing if an error was given.
2043 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002044 clear_tv(rettv);
2045 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002046
2047 funcargs.ga_len -= i;
2048 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002049 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002050 {
2051 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002052 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002053 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002054 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002055 }
2056
2057 while (--argcount >= 0)
2058 clear_tv(&argvars[argcount]);
2059
Bram Moolenaar4525a572022-02-13 11:57:33 +00002060 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002061 *arg = argp;
2062 else
2063 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002064 return ret;
2065}
2066
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002067/*
2068 * Return TRUE if "p" starts with "<SID>" or "s:".
2069 * Only works if eval_fname_script() returned non-zero for "p"!
2070 */
2071 static int
2072eval_fname_sid(char_u *p)
2073{
2074 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2075}
2076
2077/*
2078 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2079 * Change <SNR>123_name() to K_SNR 123_name().
2080 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002081 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002082 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002083 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002084fname_trans_sid(
2085 char_u *name,
2086 char_u *fname_buf,
2087 char_u **tofree,
2088 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002089{
2090 int llen;
2091 char_u *fname;
2092 int i;
2093
2094 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002095 if (llen == 0)
2096 return name; // no prefix
2097
2098 fname_buf[0] = K_SPECIAL;
2099 fname_buf[1] = KS_EXTRA;
2100 fname_buf[2] = (int)KE_SNR;
2101 i = 3;
2102 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002103 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002104 if (current_sctx.sc_sid <= 0)
2105 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002106 else
2107 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002108 sprintf((char *)fname_buf + 3, "%ld_",
2109 (long)current_sctx.sc_sid);
2110 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002111 }
2112 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002113 if (i + STRLEN(name + llen) < FLEN_FIXED)
2114 {
2115 STRCPY(fname_buf + i, name + llen);
2116 fname = fname_buf;
2117 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002118 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002119 {
2120 fname = alloc(i + STRLEN(name + llen) + 1);
2121 if (fname == NULL)
2122 *error = FCERR_OTHER;
2123 else
2124 {
2125 *tofree = fname;
2126 mch_memmove(fname, fname_buf, (size_t)i);
2127 STRCPY(fname + i, name + llen);
2128 }
2129 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002130 return fname;
2131}
2132
2133/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002134 * Concatenate the script ID and function name into "<SNR>99_name".
2135 * "buffer" must have size MAX_FUNC_NAME_LEN.
2136 */
2137 void
2138func_name_with_sid(char_u *name, int sid, char_u *buffer)
2139{
2140 // A script-local function is stored as "<SNR>99_name".
2141 buffer[0] = K_SPECIAL;
2142 buffer[1] = KS_EXTRA;
2143 buffer[2] = (int)KE_SNR;
2144 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2145 (long)sid, name);
2146}
2147
2148/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149 * Find a function "name" in script "sid".
2150 */
2151 static ufunc_T *
2152find_func_with_sid(char_u *name, int sid)
2153{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002154 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002155 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156
Bram Moolenaarb8822442022-01-11 15:24:05 +00002157 if (!SCRIPT_ID_VALID(sid))
2158 return NULL; // not in a script
2159
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002160 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 hi = hash_find(&func_hashtab, buffer);
2162 if (!HASHITEM_EMPTY(hi))
2163 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002164 return NULL;
2165}
2166
2167/*
2168 * Find a function "name" in script "sid" prefixing the autoload prefix.
2169 */
2170 static ufunc_T *
2171find_func_with_prefix(char_u *name, int sid)
2172{
2173 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002174 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002175 scriptitem_T *si;
2176
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002177 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002178 return NULL; // already has the prefix
2179 if (!SCRIPT_ID_VALID(sid))
2180 return NULL; // not in a script
2181 si = SCRIPT_ITEM(sid);
2182 if (si->sn_autoload_prefix != NULL)
2183 {
2184 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2185 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002186 char_u *namep;
2187
2188 // skip a "<SNR>99_" prefix
2189 namep = untrans_function_name(name);
2190 if (namep == NULL)
2191 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002192
2193 // An exported function in an autoload script is stored as
2194 // "dir#path#name".
2195 if (len < sizeof(buffer))
2196 auto_name = buffer;
2197 else
2198 auto_name = alloc(len);
2199 if (auto_name != NULL)
2200 {
2201 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002202 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002203 hi = hash_find(&func_hashtab, auto_name);
2204 if (auto_name != buffer)
2205 vim_free(auto_name);
2206 if (!HASHITEM_EMPTY(hi))
2207 return HI2UF(hi);
2208 }
2209 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210
2211 return NULL;
2212}
2213
2214/*
Ernie Rael16409692024-07-31 22:18:11 +02002215 * Find a function by name, return pointer to it.
2216 * The name may be a local script variable, VAR_FUNC. or it may be a fully
2217 * qualified import name such as 'i_imp.FuncName'.
2218 *
2219 * When VAR_FUNC, the import might either direct or autoload.
2220 * When 'i_imp.FuncName' it is direct, autoload is rewritten as i_imp#FuncName
2221 * in f_call and subsequently found.
2222 */
2223 static ufunc_T *
2224find_func_imported(char_u *name, int flags)
2225{
2226 ufunc_T *func = NULL;
2227 char_u *dot = name; // Find a dot, '.', in the name
2228
2229 // Either run into '.' or the end of the string
2230 while (eval_isnamec(*dot))
2231 ++dot;
2232
2233 if (*dot == '.')
2234 {
2235 imported_T *import = find_imported(name, dot - name, FALSE);
2236 if (import != NULL)
2237 func = find_func_with_sid(dot + 1, import->imp_sid);
2238 }
2239 else if (*dot == NUL) // looking at the entire string
2240 {
2241 hashtab_T *ht = get_script_local_ht();
2242 if (ht != NULL)
2243 {
2244 hashitem_T *hi = hash_find(ht, name);
2245 if (!HASHITEM_EMPTY(hi))
2246 {
2247 dictitem_T *di = HI2DI(hi);
2248 if (di->di_tv.v_type == VAR_FUNC)
2249 func = find_func_even_dead(di->di_tv.vval.v_string, flags);
2250 }
2251 }
2252 }
2253 return func;
2254}
2255
2256/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002257 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002258 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2259 * functions.
2260 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002261 * Return NULL for unknown function.
2262 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002263 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002264find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002265{
2266 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002269 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002271 // Find script-local function before global one.
2272 if (in_vim9script() && eval_isnamec1(*name)
2273 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002275 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2276 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002277 if (func != NULL)
2278 return func;
2279 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002280 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2281 {
2282 char_u *p = name + 5;
2283 long sid;
2284
2285 // printable "<SNR>123_Name" form
2286 sid = getdigits(&p);
2287 if (*p == '_')
2288 {
2289 func = find_func_with_sid(p + 1, (int)sid);
2290 if (func != NULL)
2291 return func;
2292 }
2293 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002295
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002296 if ((flags & FFED_NO_GLOBAL) == 0)
2297 {
2298 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002299 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002300 if (!HASHITEM_EMPTY(hi))
2301 return HI2UF(hi);
2302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303
Bram Moolenaarb8822442022-01-11 15:24:05 +00002304 // Find autoload function if this is an autoload script.
Ernie Rael16409692024-07-31 22:18:11 +02002305 func = find_func_with_prefix(name[0] == 's' && name[1] == ':'
Bram Moolenaarb8822442022-01-11 15:24:05 +00002306 ? name + 2 : name, current_sctx.sc_sid);
Ernie Rael16409692024-07-31 22:18:11 +02002307 if (func != NULL)
2308 return func;
2309
2310 // Find a script-local "VAR_FUNC" or i_"imp.Func", so vim9script).
2311 if (in_vim9script())
2312 func = find_func_imported(name, flags);
2313 return func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314}
2315
2316/*
2317 * Find a function by name, return pointer to it in ufuncs.
2318 * "cctx" is passed in a :def function to find imported functions.
2319 * Return NULL for unknown or dead function.
2320 */
2321 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002322find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002324 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325
2326 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2327 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002328 return NULL;
2329}
2330
2331/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002332 * Return TRUE if "ufunc" is a global function.
2333 */
2334 int
2335func_is_global(ufunc_T *ufunc)
2336{
2337 return ufunc->uf_name[0] != K_SPECIAL;
2338}
2339
2340/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002341 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2342 */
2343 int
2344func_requires_g_prefix(ufunc_T *ufunc)
2345{
2346 return ufunc->uf_name[0] != K_SPECIAL
2347 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002348 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01002349 && !SAFE_isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002350}
2351
2352/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002353 * Copy the function name of "fp" to buffer "buf".
2354 * "buf" must be able to hold the function name plus three bytes.
2355 * Takes care of script-local function names.
2356 */
2357 static void
2358cat_func_name(char_u *buf, ufunc_T *fp)
2359{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002360 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002361 {
2362 STRCPY(buf, "<SNR>");
2363 STRCAT(buf, fp->uf_name + 3);
2364 }
2365 else
2366 STRCPY(buf, fp->uf_name);
2367}
2368
2369/*
2370 * Add a number variable "name" to dict "dp" with value "nr".
2371 */
2372 static void
2373add_nr_var(
2374 dict_T *dp,
2375 dictitem_T *v,
2376 char *name,
2377 varnumber_T nr)
2378{
2379 STRCPY(v->di_key, name);
2380 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002381 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002382 v->di_tv.v_type = VAR_NUMBER;
2383 v->di_tv.v_lock = VAR_FIXED;
2384 v->di_tv.vval.v_number = nr;
2385}
2386
2387/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002388 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002389 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002390 static void
2391free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002392{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002393 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002394
Bram Moolenaarca16c602022-09-06 18:57:08 +01002395 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002396 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002397 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002398
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002399 // When garbage collecting a funccall_T may be freed before the
2400 // function that references it, clear its uf_scoped field.
2401 // The function may have been redefined and point to another
2402 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002403 if (fp != NULL && fp->uf_scoped == fc)
2404 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002405 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002406 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002407
Bram Moolenaarca16c602022-09-06 18:57:08 +01002408 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002409 vim_free(fc);
2410}
2411
2412/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002413 * Free "fc" and what it contains.
2414 * Can be called only when "fc" is kept beyond the period of it called,
2415 * i.e. after cleanup_function_call(fc).
2416 */
2417 static void
2418free_funccal_contents(funccall_T *fc)
2419{
2420 listitem_T *li;
2421
2422 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002423 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002424
2425 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002426 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002427
2428 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002429 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002430 clear_tv(&li->li_tv);
2431
2432 free_funccal(fc);
2433}
2434
2435/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002436 * Handle the last part of returning from a function: free the local hashtable.
2437 * Unless it is still in use by a closure.
2438 */
2439 static void
2440cleanup_function_call(funccall_T *fc)
2441{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002442 int may_free_fc = fc->fc_refcount <= 0;
2443 int free_fc = TRUE;
2444
Bram Moolenaarca16c602022-09-06 18:57:08 +01002445 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002446
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002447 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002448 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2449 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002450 else
2451 free_fc = FALSE;
2452
2453 // If the a:000 list and the l: and a: dicts are not referenced and
2454 // there is no closure using it, we can free the funccall_T and what's
2455 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002456 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2457 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002458 else
2459 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002460 int todo;
2461 hashitem_T *hi;
2462 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002463
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002464 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002465
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002466 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002467 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002468 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002469 {
2470 if (!HASHITEM_EMPTY(hi))
2471 {
2472 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002473 di = HI2DI(hi);
2474 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002475 }
2476 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002477 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002478
Bram Moolenaarca16c602022-09-06 18:57:08 +01002479 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2480 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002481 else
2482 {
2483 listitem_T *li;
2484
2485 free_fc = FALSE;
2486
2487 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002488 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002489 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002490 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002491
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002492 if (free_fc)
2493 free_funccal(fc);
2494 else
2495 {
2496 static int made_copy = 0;
2497
2498 // "fc" is still in use. This can happen when returning "a:000",
2499 // assigning "l:" to a global variable or defining a closure.
2500 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002501 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002502 previous_funccal = fc;
2503
2504 if (want_garbage_collect)
2505 // If garbage collector is ready, clear count.
2506 made_copy = 0;
2507 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002508 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002509 // We have made a lot of copies, worth 4 Mbyte. This can happen
2510 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002511 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002512 // too much memory.
2513 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002514 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002515 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002516 }
2517}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002518
2519/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002520 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2521 */
2522 static int
2523numbered_function(char_u *name)
2524{
Keith Thompson184f71c2024-01-04 21:19:04 +01002525 return SAFE_isdigit(*name)
2526 || (name[0] == 'g' && name[1] == ':' && SAFE_isdigit(name[2]));
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002527}
2528
2529/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002530 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002531 * 1. ordinary names, function defined with :function or :def;
2532 * can start with "<SNR>123_" literally or with K_SPECIAL.
2533 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002534 * For the first we only count the name stored in func_hashtab as a reference,
2535 * using function() does not count as a reference, because the function is
2536 * looked up by name.
2537 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002538 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002539func_name_refcount(char_u *name)
2540{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002541 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002542}
2543
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544/*
2545 * Unreference "fc": decrement the reference count and free it when it
2546 * becomes zero. "fp" is detached from "fc".
2547 * When "force" is TRUE we are exiting.
2548 */
2549 static void
2550funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2551{
2552 funccall_T **pfc;
2553 int i;
2554
2555 if (fc == NULL)
2556 return;
2557
2558 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002559 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2560 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2561 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2562 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 {
2564 if (fc == *pfc)
2565 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002566 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 free_funccal_contents(fc);
2568 return;
2569 }
2570 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002571 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2572 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2573 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574}
2575
2576/*
2577 * Remove the function from the function hashtable. If the function was
2578 * deleted while it still has references this was already done.
2579 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2580 */
2581 static int
2582func_remove(ufunc_T *fp)
2583{
2584 hashitem_T *hi;
2585
2586 // Return if it was already virtually deleted.
2587 if (fp->uf_flags & FC_DEAD)
2588 return FALSE;
2589
2590 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002591 if (HASHITEM_EMPTY(hi))
2592 return FALSE;
2593
2594 // When there is a def-function index do not actually remove the
2595 // function, so we can find the index when defining the function again.
2596 // Do remove it when it's a copy.
2597 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002599 fp->uf_flags |= FC_DEAD;
2600 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002602 hash_remove(&func_hashtab, hi, "remove function");
2603 fp->uf_flags |= FC_DELETED;
2604 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605}
2606
2607 static void
2608func_clear_items(ufunc_T *fp)
2609{
2610 ga_clear_strings(&(fp->uf_args));
2611 ga_clear_strings(&(fp->uf_def_args));
2612 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002614 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002615 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002616 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002617
2618 // Increment the refcount of this function to avoid it being freed
2619 // recursively when the partial is freed.
2620 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002621 partial_unref(fp->uf_partial);
2622 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002623 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002624
2625#ifdef FEAT_LUA
2626 if (fp->uf_cb_free != NULL)
2627 {
2628 fp->uf_cb_free(fp->uf_cb_state);
2629 fp->uf_cb_free = NULL;
2630 }
2631
2632 fp->uf_cb_state = NULL;
2633 fp->uf_cb = NULL;
2634#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635#ifdef FEAT_PROFILE
2636 VIM_CLEAR(fp->uf_tml_count);
2637 VIM_CLEAR(fp->uf_tml_total);
2638 VIM_CLEAR(fp->uf_tml_self);
2639#endif
2640}
2641
2642/*
2643 * Free all things that a function contains. Does not free the function
2644 * itself, use func_free() for that.
2645 * When "force" is TRUE we are exiting.
2646 */
2647 static void
2648func_clear(ufunc_T *fp, int force)
2649{
2650 if (fp->uf_cleared)
2651 return;
2652 fp->uf_cleared = TRUE;
2653
2654 // clear this function
2655 func_clear_items(fp);
2656 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002657 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658}
2659
2660/*
2661 * Free a function and remove it from the list of functions. Does not free
2662 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002663 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002664 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002666 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002667func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668{
2669 // Only remove it when not done already, otherwise we would remove a newer
2670 // version of the function with the same name.
2671 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2672 func_remove(fp);
2673
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002674 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002675 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002676 if (fp->uf_dfunc_idx > 0)
2677 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002678 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002680 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002681 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002682 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683}
2684
2685/*
2686 * Free all things that a function contains and free the function itself.
2687 * When "force" is TRUE we are exiting.
2688 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002689 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690func_clear_free(ufunc_T *fp, int force)
2691{
2692 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002693 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2694 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002695 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002696 else
2697 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698}
2699
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002700/*
2701 * Copy already defined function "lambda" to a new function with name "global".
2702 * This is for when a compiled function defines a global function.
2703 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002704 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002705copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002706 char_u *lambda,
2707 char_u *global,
2708 loopvarinfo_T *loopvarinfo,
2709 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002710{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002711 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002712 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002713
2714 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002715 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002716 semsg(_(e_lambda_function_not_found_str), lambda);
2717 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002718 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002719
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002720 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002721 if (fp != NULL)
2722 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002723 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002724 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002725 return FAIL;
2726 }
2727
Bram Moolenaare01e5212023-01-08 20:31:18 +00002728 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002729 if (fp == NULL)
2730 return FAIL;
2731
2732 fp->uf_varargs = ufunc->uf_varargs;
2733 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2734 fp->uf_def_status = ufunc->uf_def_status;
2735 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2736 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2737 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2738 == FAIL
2739 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2740 goto failed;
2741
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002742 if (ufunc->uf_arg_types != NULL)
2743 {
2744 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2745 if (fp->uf_arg_types == NULL)
2746 goto failed;
2747 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2748 sizeof(type_T *) * fp->uf_args.ga_len);
2749 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002750 if (ufunc->uf_va_name != NULL)
2751 {
2752 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2753 if (fp->uf_va_name == NULL)
2754 goto failed;
2755 }
2756 fp->uf_ret_type = ufunc->uf_ret_type;
2757
2758 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002759
2760 fp->uf_name_exp = NULL;
2761 set_ufunc_name(fp, global);
2762
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002763 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002764
2765 // the referenced dfunc_T is now used one more time
2766 link_def_function(fp);
2767
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002768 // Create a partial to store the context of the function where it was
2769 // instantiated. Only needs to be done once. Do this on the original
2770 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002771 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2772 {
2773 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2774
2775 if (pt == NULL)
2776 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002777 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002778 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002779 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002780 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002781 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002782 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002783 }
2784
2785 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002786
2787failed:
2788 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002789 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002790}
2791
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002792static int funcdepth = 0;
2793
2794/*
2795 * Increment the function call depth count.
2796 * Return FAIL when going over 'maxfuncdepth'.
2797 * Otherwise return OK, must call funcdepth_decrement() later!
2798 */
2799 int
2800funcdepth_increment(void)
2801{
2802 if (funcdepth >= p_mfd)
2803 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002804 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002805 return FAIL;
2806 }
2807 ++funcdepth;
2808 return OK;
2809}
2810
2811 void
2812funcdepth_decrement(void)
2813{
2814 --funcdepth;
2815}
2816
2817/*
2818 * Get the current function call depth.
2819 */
2820 int
2821funcdepth_get(void)
2822{
2823 return funcdepth;
2824}
2825
2826/*
2827 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002828 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002829 * times as funcdepth_increment().
2830 */
2831 void
2832funcdepth_restore(int depth)
2833{
2834 funcdepth = depth;
2835}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002836
2837/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002838 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2839 * "rettv".
2840 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2841 * Returns NULL when allocation fails.
2842 */
2843 funccall_T *
2844create_funccal(ufunc_T *fp, typval_T *rettv)
2845{
2846 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2847
2848 if (fc == NULL)
2849 return NULL;
2850 fc->fc_caller = current_funccal;
2851 current_funccal = fc;
2852 fc->fc_func = fp;
2853 func_ptr_ref(fp);
2854 fc->fc_rettv = rettv;
2855 return fc;
2856}
2857
2858/*
2859 * To be called when returning from a compiled function; restores
2860 * current_funccal.
2861 */
2862 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002863remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002864{
2865 funccall_T *fc = current_funccal;
2866
2867 current_funccal = fc->fc_caller;
2868 free_funccal(fc);
2869}
2870
2871/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002872 * Call a user function.
2873 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002874 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002875call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002876 ufunc_T *fp, // pointer to function
2877 int argcount, // nr of args
2878 typval_T *argvars, // arguments
2879 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002880 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002881 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002882{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002883 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002884 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002885 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002886 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002887 funccall_T *fc;
2888 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002889 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002890 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002891 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002892 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002893 int i;
2894 int ai;
2895 int islambda = FALSE;
2896 char_u numbuf[NUMBUFLEN];
2897 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002898 typval_T *tv_to_free[MAX_FUNC_ARGS];
2899 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002900#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002901 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002902#endif
ichizok7e5fe382023-04-15 13:17:50 +01002903 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002904
Bram Moolenaarb2049902021-01-24 12:53:53 +01002905#ifdef FEAT_PROFILE
2906 CLEAR_FIELD(profile_info);
2907#endif
2908
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002909 // If depth of calling is getting too high, don't execute the function.
2910 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002911 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002912 rettv->v_type = VAR_NUMBER;
2913 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002914 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002915 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002916
Bram Moolenaare38eab22019-12-05 21:50:01 +01002917 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002918
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002919 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002920 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002921 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002922 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002923 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002924 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2925 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002926 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002927 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002928
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002929 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002931#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002932 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002933#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002934 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002935#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002936 if (do_profiling == PROF_YES)
2937 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002938#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002939 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002940 if (call_def_function(fp, argcount, argvars, 0,
2941 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2942 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002943 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002944#ifdef FEAT_PROFILE
2945 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002946 || (caller != NULL && caller->uf_profiling)))
2947 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002948#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002949 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002950 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002951 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 }
2953
Bram Moolenaar38453522021-11-28 22:00:12 +00002954 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002955
2956 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002957 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2958 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2959 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002960 */
2961 /*
2962 * Init l: variables.
2963 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002964 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002965 if (selfdict != NULL)
2966 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002967 // Set l:self to "selfdict". Use "name" to avoid a warning from
2968 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002969 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002970 name = v->di_key;
2971 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002972 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002973 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002974 v->di_tv.v_type = VAR_DICT;
2975 v->di_tv.v_lock = 0;
2976 v->di_tv.vval.v_dict = selfdict;
2977 ++selfdict->dv_refcount;
2978 }
2979
2980 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002981 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002982 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983 * Set a:000 to a list with room for the "..." arguments.
2984 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002985 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002986 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002987 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002988 (varnumber_T)(argcount >= fp->uf_args.ga_len
2989 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002990 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002991 if ((fp->uf_flags & FC_NOARGS) == 0)
2992 {
2993 // Use "name" to avoid a warning from some compiler that checks the
2994 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002995 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002996 name = v->di_key;
2997 STRCPY(name, "000");
2998 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002999 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003000 v->di_tv.v_type = VAR_LIST;
3001 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003002 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003003 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01003004 CLEAR_FIELD(fc->fc_l_varlist);
3005 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
3006 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003007
3008 /*
3009 * Set a:firstline to "firstline" and a:lastline to "lastline".
3010 * Set a:name to named arguments.
3011 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003012 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003013 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003014 if ((fp->uf_flags & FC_NOARGS) == 0)
3015 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003016 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3017 "firstline", (varnumber_T)funcexe->fe_firstline);
3018 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3019 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003020 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003021 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003022 {
3023 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003024 typval_T def_rettv;
3025 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003026
3027 ai = i - fp->uf_args.ga_len;
3028 if (ai < 0)
3029 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003030 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003031 name = FUNCARG(fp, i);
3032 if (islambda)
3033 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003034
3035 // evaluate named argument default expression
3036 isdefault = ai + fp->uf_def_args.ga_len >= 0
3037 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
3038 && argvars[i].vval.v_number == VVAL_NONE));
3039 if (isdefault)
3040 {
3041 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003042
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003043 def_rettv.v_type = VAR_NUMBER;
3044 def_rettv.vval.v_number = -1;
3045
3046 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
3047 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003048 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003049 {
3050 default_arg_err = 1;
3051 break;
3052 }
3053 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003054 }
3055 else
3056 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003057 if ((fp->uf_flags & FC_NOARGS) != 0)
3058 // Bail out if no a: arguments used (in lambda).
3059 break;
3060
Bram Moolenaare38eab22019-12-05 21:50:01 +01003061 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003062 sprintf((char *)numbuf, "%d", ai + 1);
3063 name = numbuf;
3064 }
3065 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
3066 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003067 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003068 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003069 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003070 }
3071 else
3072 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003073 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003074 if (v == NULL)
3075 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003076 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003077 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003078
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003079 // Note: the values are copied directly to avoid alloc/free.
3080 // "argvars" must have VAR_FIXED for v_lock.
3081 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003082 v->di_tv.v_lock = VAR_FIXED;
3083
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003084 if (isdefault)
3085 // Need to free this later, no matter where it's stored.
3086 tv_to_free[tv_to_free_len++] = &v->di_tv;
3087
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003088 if (addlocal)
3089 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003090 // Named arguments should be accessed without the "a:" prefix in
3091 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003092 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003093 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003094 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003095 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003096 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003097
3098 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3099 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003100 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003101
3102 li->li_tv = argvars[i];
3103 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003104 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003105 }
3106 }
3107
Bram Moolenaare38eab22019-12-05 21:50:01 +01003108 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003109 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003110
3111 if (fp->uf_flags & FC_SANDBOX)
3112 {
3113 using_sandbox = TRUE;
3114 ++sandbox;
3115 }
3116
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003117 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003118 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003119 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003120 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003121 ++no_wait_return;
3122 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003123
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003124 smsg(_("calling %s"), SOURCING_NAME);
3125 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003126 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003127 char_u buf[MSG_BUF_LEN];
3128 char_u numbuf2[NUMBUFLEN];
3129 char_u *tofree;
3130 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003131
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003132 msg_puts("(");
3133 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003134 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003135 if (i > 0)
3136 msg_puts(", ");
3137 if (argvars[i].v_type == VAR_NUMBER)
3138 msg_outnum((long)argvars[i].vval.v_number);
3139 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003140 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003141 // Do not want errors such as E724 here.
3142 ++emsg_off;
3143 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3144 --emsg_off;
3145 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003146 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003147 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003148 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003149 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3150 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003151 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003152 msg_puts((char *)s);
3153 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003154 }
3155 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003157 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003158 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003159 msg_puts("\n"); // don't overwrite this either
3160
3161 verbose_leave_scroll();
3162 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003163 }
3164#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003165 if (do_profiling == PROF_YES)
3166 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003167 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003168#endif
3169
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003170 // "legacy" does not apply to commands in the function
3171 sticky_cmdmod_flags = 0;
3172
Bram Moolenaar98aff652022-09-06 21:02:35 +01003173 // If called from a compiled :def function the execution context must be
3174 // hidden, any deferred functions need to be added to the function being
3175 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003176 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003177
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003178 save_current_sctx = current_sctx;
3179 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003180 save_did_emsg = did_emsg;
3181 did_emsg = FALSE;
3182
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003183 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003184 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003185 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003186 retval = FCERR_FAILED;
3187 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003188 else if (islambda)
3189 {
3190 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3191
3192 // A Lambda always has the command "return {expr}". It is much faster
3193 // to evaluate {expr} directly.
3194 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003195 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003196 --ex_nesting_level;
3197 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003198 else
3199 // call do_cmdline() to execute the lines
3200 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003201 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3202
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003203 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003204 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003205
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003206 if (RedrawingDisabled > 0)
3207 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003208
Bram Moolenaare38eab22019-12-05 21:50:01 +01003209 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003210 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3211 {
3212 clear_tv(rettv);
3213 rettv->v_type = VAR_NUMBER;
3214 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003215
3216 // In corner cases returning a "failed" value is not backwards
3217 // compatible. Only do this for Vim9 script.
3218 if (in_vim9script())
3219 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003220 }
3221
3222#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003223 if (do_profiling == PROF_YES)
3224 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003225 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003226
3227 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3228 profile_may_end_func(&profile_info, fp, caller);
3229 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003230#endif
3231
Bram Moolenaare38eab22019-12-05 21:50:01 +01003232 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003233 if (p_verbose >= 12)
3234 {
3235 ++no_wait_return;
3236 verbose_enter_scroll();
3237
3238 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003239 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003240 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003241 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003242 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003243 else
3244 {
3245 char_u buf[MSG_BUF_LEN];
3246 char_u numbuf2[NUMBUFLEN];
3247 char_u *tofree;
3248 char_u *s;
3249
Bram Moolenaare38eab22019-12-05 21:50:01 +01003250 // The value may be very long. Skip the middle part, so that we
3251 // have some idea how it starts and ends. smsg() would always
3252 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003253 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003254 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003255 --emsg_off;
3256 if (s != NULL)
3257 {
3258 if (vim_strsize(s) > MSG_BUF_CLEN)
3259 {
3260 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3261 s = buf;
3262 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003263 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003264 vim_free(tofree);
3265 }
3266 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003267 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003268
3269 verbose_leave_scroll();
3270 --no_wait_return;
3271 }
3272
ichizok7e5fe382023-04-15 13:17:50 +01003273 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003274 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003275 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003276 restore_current_ectx(save_current_ectx);
3277
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003278#ifdef FEAT_PROFILE
3279 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003280 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003281#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003282 if (using_sandbox)
3283 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003284 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003285
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003286 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003287 {
3288 ++no_wait_return;
3289 verbose_enter_scroll();
3290
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003291 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003292 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003293
3294 verbose_leave_scroll();
3295 --no_wait_return;
3296 }
3297
3298 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003299 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003300 for (i = 0; i < tv_to_free_len; ++i)
3301 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003302 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003303
3304 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003305}
3306
3307/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003308 * Check the argument count for user function "fp".
3309 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3310 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003311 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003312check_user_func_argcount(ufunc_T *fp, int argcount)
3313{
3314 int regular_args = fp->uf_args.ga_len;
3315
3316 if (argcount < regular_args - fp->uf_def_args.ga_len)
3317 return FCERR_TOOFEW;
3318 else if (!has_varargs(fp) && argcount > regular_args)
3319 return FCERR_TOOMANY;
3320 return FCERR_UNKNOWN;
3321}
3322
3323/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003325 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003326 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327call_user_func_check(
3328 ufunc_T *fp,
3329 int argcount,
3330 typval_T *argvars,
3331 typval_T *rettv,
3332 funcexe_T *funcexe,
3333 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003334{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003335 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003336
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003337#ifdef FEAT_LUA
3338 if (fp->uf_flags & FC_CFUNC)
3339 {
3340 cfunc_T cb = fp->uf_cb;
3341
3342 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3343 }
3344#endif
3345
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003346 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3347 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003348 error = check_user_func_argcount(fp, argcount);
3349 if (error != FCERR_UNKNOWN)
3350 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003351
Bram Moolenaar50824712020-12-20 21:10:17 +01003352 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003353 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003355 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003357 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 int did_save_redo = FALSE;
3359 save_redo_T save_redo;
3360
3361 /*
3362 * Call the user function.
3363 * Save and restore search patterns, script variables and
3364 * redo buffer.
3365 */
3366 save_search_patterns();
3367 if (!ins_compl_active())
3368 {
3369 saveRedobuff(&save_redo);
3370 did_save_redo = TRUE;
3371 }
3372 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003373 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003374 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3376 // Function was unreferenced while being used, free it now.
3377 func_clear_free(fp, FALSE);
3378 if (did_save_redo)
3379 restoreRedobuff(&save_redo);
3380 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003381 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003384}
3385
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003386static funccal_entry_T *funccal_stack = NULL;
3387
3388/*
3389 * Save the current function call pointer, and set it to NULL.
3390 * Used when executing autocommands and for ":source".
3391 */
3392 void
3393save_funccal(funccal_entry_T *entry)
3394{
3395 entry->top_funccal = current_funccal;
3396 entry->next = funccal_stack;
3397 funccal_stack = entry;
3398 current_funccal = NULL;
3399}
3400
3401 void
3402restore_funccal(void)
3403{
3404 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003405 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003406 else
3407 {
3408 current_funccal = funccal_stack->top_funccal;
3409 funccal_stack = funccal_stack->next;
3410 }
3411}
3412
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003413 funccall_T *
3414get_current_funccal(void)
3415{
3416 return current_funccal;
3417}
3418
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003419/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003420 * Return TRUE when currently at the script level:
3421 * - not in a function
3422 * - not executing an autocommand
3423 * Note that when an autocommand sources a script the result is FALSE;
3424 */
3425 int
3426at_script_level(void)
3427{
3428 return current_funccal == NULL && autocmd_match == NULL;
3429}
3430
3431/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003432 * Mark all functions of script "sid" as deleted.
3433 */
3434 void
3435delete_script_functions(int sid)
3436{
3437 hashitem_T *hi;
3438 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003439 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003440 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003441 size_t len;
3442
3443 buf[0] = K_SPECIAL;
3444 buf[1] = KS_EXTRA;
3445 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003446 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003447 len = STRLEN(buf);
3448
Bram Moolenaarce658352020-07-31 23:47:12 +02003449 while (todo > 0)
3450 {
3451 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003452 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003453 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003454 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003455 fp = HI2UF(hi);
3456 if (STRNCMP(fp->uf_name, buf, len) == 0)
3457 {
3458 int changed = func_hashtab.ht_changed;
3459
3460 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003461
3462 if (fp->uf_calls > 0)
3463 {
3464 // Function is executing, don't free it but do remove
3465 // it from the hashtable.
3466 if (func_remove(fp))
3467 fp->uf_refcount--;
3468 }
3469 else
3470 {
3471 func_clear(fp, TRUE);
3472 // When clearing a function another function can be
3473 // cleared as a side effect. When that happens start
3474 // over.
3475 if (changed != func_hashtab.ht_changed)
3476 break;
3477 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003478 }
3479 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003480 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003481 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003482}
3483
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003484#if defined(EXITFREE) || defined(PROTO)
3485 void
3486free_all_functions(void)
3487{
3488 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003489 ufunc_T *fp;
3490 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003491 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003492 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003493
Bram Moolenaare38eab22019-12-05 21:50:01 +01003494 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003495 while (current_funccal != NULL)
3496 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003497 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003498 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003499 if (current_funccal == NULL && funccal_stack != NULL)
3500 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003501 }
3502
Bram Moolenaare38eab22019-12-05 21:50:01 +01003503 // First clear what the functions contain. Since this may lower the
3504 // reference count of a function, it may also free a function and change
3505 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003506 while (todo > 0)
3507 {
3508 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003509 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003510 if (!HASHITEM_EMPTY(hi))
3511 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 // clear the def function index now
3513 fp = HI2UF(hi);
3514 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003515 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516
Bram Moolenaare38eab22019-12-05 21:50:01 +01003517 // Only free functions that are not refcounted, those are
3518 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003519 if (func_name_refcount(fp->uf_name))
3520 ++skipped;
3521 else
3522 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003523 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003524 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003525 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003526 {
3527 skipped = 0;
3528 break;
3529 }
3530 }
3531 --todo;
3532 }
3533 }
3534
Bram Moolenaare38eab22019-12-05 21:50:01 +01003535 // Now actually free the functions. Need to start all over every time,
3536 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003537 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003538 while (func_hashtab.ht_used > skipped)
3539 {
3540 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003541 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003542 if (!HASHITEM_EMPTY(hi))
3543 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003544 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003545 // Only free functions that are not refcounted, those are
3546 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003547 fp = HI2UF(hi);
3548 if (func_name_refcount(fp->uf_name))
3549 ++skipped;
3550 else
3551 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003552 if (func_free(fp, FALSE) == OK)
3553 {
3554 skipped = 0;
3555 break;
3556 }
3557 // did not actually free it
3558 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003559 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003560 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003561 }
3562 if (skipped == 0)
3563 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564
3565 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003566}
3567#endif
3568
3569/*
3570 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003571 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3572 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003573 * "len" is the length of "name", or -1 for NUL terminated.
3574 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003576builtin_function(char_u *name, int len)
3577{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003578 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003579
Bram Moolenaara26b9702020-04-18 19:53:28 +02003580 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003581 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003582 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3583 {
3584 if (name[i] == AUTOLOAD_CHAR)
3585 return FALSE;
3586 if (!eval_isnamec(name[i]))
3587 {
3588 // "name.something" is not a builtin function
3589 if (name[i] == '.')
3590 return FALSE;
3591 break;
3592 }
3593 }
3594 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003595}
3596
3597 int
3598func_call(
3599 char_u *name,
3600 typval_T *args,
3601 partial_T *partial,
3602 dict_T *selfdict,
3603 typval_T *rettv)
3604{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003605 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003606 listitem_T *item;
3607 typval_T argv[MAX_FUNC_ARGS + 1];
3608 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003609 int r = 0;
3610
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003611 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003612 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003613 {
3614 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3615 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003616 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003617 break;
3618 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003619 // Make a copy of each argument. This is needed to be able to set
3620 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003621 copy_tv(&item->li_tv, &argv[argc++]);
3622 }
3623
3624 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003625 {
3626 funcexe_T funcexe;
3627
Bram Moolenaara80faa82020-04-12 19:37:17 +02003628 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003629 funcexe.fe_firstline = curwin->w_cursor.lnum;
3630 funcexe.fe_lastline = curwin->w_cursor.lnum;
3631 funcexe.fe_evaluate = TRUE;
3632 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003633 if (partial != NULL)
3634 {
3635 funcexe.fe_object = partial->pt_obj;
3636 if (funcexe.fe_object != NULL)
3637 ++funcexe.fe_object->obj_refcount;
3638 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003639 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003640 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3641 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003642
Bram Moolenaare38eab22019-12-05 21:50:01 +01003643 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003644 while (argc > 0)
3645 clear_tv(&argv[--argc]);
3646
3647 return r;
3648}
3649
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003650static int callback_depth = 0;
3651
3652 int
3653get_callback_depth(void)
3654{
3655 return callback_depth;
3656}
3657
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003658/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003659 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003660 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003661 */
3662 int
3663call_callback(
3664 callback_T *callback,
3665 int len, // length of "name" or -1 to use strlen()
3666 typval_T *rettv, // return value goes here
3667 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003668 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003669 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003670{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003671 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003672 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003673
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003674 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3675 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003676
zeertzjqfe583b12023-12-21 16:59:26 +01003677 if (callback_depth > p_mfd)
Christian Brabandt47510f32023-10-15 09:56:16 +02003678 {
3679 emsg(_(e_command_too_recursive));
3680 return FAIL;
3681 }
3682
Bram Moolenaara80faa82020-04-12 19:37:17 +02003683 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003684 funcexe.fe_evaluate = TRUE;
3685 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003686 if (callback->cb_partial != NULL)
3687 {
3688 funcexe.fe_object = callback->cb_partial->pt_obj;
3689 if (funcexe.fe_object != NULL)
3690 ++funcexe.fe_object->obj_refcount;
3691 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003692 ++callback_depth;
3693 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3694 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003695
3696 // When a :def function was called that uses :try an error would be turned
3697 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003698 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003699 {
3700 need_rethrow = FALSE;
3701 handle_did_throw();
3702 }
3703
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003704 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003705}
3706
3707/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003708 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003709 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003710 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3711 */
3712 varnumber_T
3713call_callback_retnr(
3714 callback_T *callback,
3715 int argcount, // number of "argvars"
3716 typval_T *argvars) // vars for arguments, must have "argcount"
3717 // PLUS ONE elements!
3718{
3719 typval_T rettv;
3720 varnumber_T retval;
3721
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003722 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003723 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003724
3725 retval = tv_get_number_chk(&rettv, NULL);
3726 clear_tv(&rettv);
3727 return retval;
3728}
3729
3730/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 * Give an error message for the result of a function.
3732 * Nothing if "error" is FCERR_NONE.
3733 */
3734 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003735user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736{
3737 switch (error)
3738 {
3739 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003740 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003741 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003742 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003743 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 break;
3745 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003746 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 break;
3748 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003749 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750 break;
3751 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003752 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 break;
3754 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003755 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 break;
3757 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003758 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 break;
3760 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003761 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3762 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003764 case FCERR_OTHER:
3765 case FCERR_FAILED:
3766 // assume the error message was already given
3767 break;
3768 case FCERR_NONE:
3769 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 }
3771}
3772
3773/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003774 * Check the argument types "argvars[argcount]" for "name" using the
3775 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3776 * is already included in "argvars[]".
3777 * Will do nothing if "funcexe->fe_check_type" is NULL or
3778 * "funcexe->fe_evaluate" is FALSE;
3779 * Returns an FCERR_ value.
3780 */
3781 static funcerror_T
3782may_check_argument_types(
3783 funcexe_T *funcexe,
3784 typval_T *argvars,
3785 int argcount,
3786 int base_included,
3787 char_u *name)
3788{
3789 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3790 {
3791 // Check that the argument types are OK for the types of the funcref.
3792 if (check_argument_types(funcexe->fe_check_type,
3793 argvars, argcount,
3794 base_included ? NULL : funcexe->fe_basetv,
3795 name) == FAIL)
3796 return FCERR_OTHER;
3797 }
3798 return FCERR_NONE;
3799}
3800
3801/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003802 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003803 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003804 * Return FAIL when the function can't be called, OK otherwise.
3805 * Also returns OK when an error was encountered while executing the function.
3806 */
3807 int
3808call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003809 char_u *funcname, // name of the function
3810 int len, // length of "name" or -1 to use strlen()
3811 typval_T *rettv, // return value goes here
3812 int argcount_in, // number of "argvars"
3813 typval_T *argvars_in, // vars for arguments, must have "argcount"
3814 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003815 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003816{
3817 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003818 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003819 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003820 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003821 char_u fname_buf[FLEN_FIXED + 1];
3822 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003823 char_u *fname = NULL;
3824 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003825 int argcount = argcount_in;
3826 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003827 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003828 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003829 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003830 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003831 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003832 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003833 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003834 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003835
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003836 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3837 // even when call_func() returns FAIL.
3838 rettv->v_type = VAR_UNKNOWN;
3839
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003840 if (partial != NULL)
3841 fp = partial->pt_func;
3842 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003843 fp = funcexe->fe_ufunc;
3844
3845 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003846 {
3847 // Make a copy of the name, if it comes from a funcref variable it
3848 // could be changed or deleted in the called function.
3849 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3850 if (name == NULL)
3851 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003852
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003853 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3854 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003855
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003856 if (funcexe->fe_doesrange != NULL)
3857 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003858
3859 if (partial != NULL)
3860 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003861 // When the function has a partial with a dict and there is a dict
3862 // argument, use the dict argument. That is backwards compatible.
3863 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003864 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003865 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003866 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003867 {
3868 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003869 {
3870 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3871 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003872 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003873 goto theend;
3874 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003875 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003876 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003877 for (i = 0; i < argcount_in; ++i)
3878 argv[i + argv_clear] = argvars_in[i];
3879 argvars = argv;
3880 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003881
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003882 if (funcexe->fe_check_type != NULL
3883 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003884 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003885 // Now funcexe->fe_check_type is missing the added arguments,
3886 // make a copy of the type with the correction.
3887 check_type = *funcexe->fe_check_type;
3888 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003889 check_type.tt_args = check_type_args;
3890 CLEAR_FIELD(check_type_args);
3891 for (i = 0; i < check_type.tt_argcount; ++i)
3892 check_type_args[i + partial->pt_argc] =
3893 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003894 check_type.tt_argcount += partial->pt_argc;
3895 check_type.tt_min_argcount += partial->pt_argc;
3896 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003897 }
3898 }
3899
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003900 if (error == FCERR_NONE)
3901 // check the argument types if possible
3902 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3903 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003904
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003905 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003906 {
3907 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003908 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003909
Bram Moolenaar333894b2020-08-01 18:53:07 +02003910 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003911 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003912 {
3913 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003914 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003915 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003916
Bram Moolenaare38eab22019-12-05 21:50:01 +01003917 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003918 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003919 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003920
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003921 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003922 {
3923 /*
3924 * User defined function.
3925 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003926 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003927 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003928 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003929 if (fp != NULL && !is_global && in_vim9script()
3930 && func_requires_g_prefix(fp))
3931 // In Vim9 script g: is required to find a global
3932 // non-autoload function.
3933 fp = NULL;
3934 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003935
Bram Moolenaare38eab22019-12-05 21:50:01 +01003936 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003937 if (fp == NULL
3938 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003939 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003940 && !aborting())
3941 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003942 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003943 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003944 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003945 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003946 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3947 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003948 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003949 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003950 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003951 if (fp == NULL)
3952 {
3953 char_u *p = untrans_function_name(rfname);
3954
3955 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003956 // Don't do this if the name starts with "s:".
3957 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003958 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003959 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003960
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003961 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003962 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003963 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003964 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003965 int need_arg_check = FALSE;
3966 if (funcexe->fe_check_type == NULL)
3967 {
3968 funcexe->fe_check_type = fp->uf_func_type;
3969 need_arg_check = TRUE;
3970 }
3971
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003972 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003973 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003974 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003975 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003976 argv_clear, fp);
3977 need_arg_check = TRUE;
3978 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003979
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003980 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003981 {
3982 // Method call: base->Method()
3983 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003984 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003985 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003986 argvars = argv;
3987 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003988 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003989 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003990
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003991 // Check the argument types now that the function type and all
3992 // argument values are known, if not done above.
3993 if (need_arg_check)
3994 error = may_check_argument_types(funcexe, argvars, argcount,
3995 TRUE, (name != NULL) ? name : funcname);
3996 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3997 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003999 }
4000 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004001 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004002 {
4003 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004004 * expr->method(): Find the method name in the table, call its
4005 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02004006 */
4007 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004008 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004009 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004010 else
4011 {
4012 /*
4013 * Find the function name in the table, call its implementation.
4014 */
4015 error = call_internal_func(fname, argcount, argvars, rettv);
4016 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02004017
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004018 /*
4019 * The function call (or "FuncUndefined" autocommand sequence) might
4020 * have been aborted by an error, an interrupt, or an explicitly thrown
4021 * exception that has not been caught so far. This situation can be
4022 * tested for by calling aborting(). For an error in an internal
4023 * function or for the "E132" error in call_user_func(), however, the
4024 * throw point at which the "force_abort" flag (temporarily reset by
4025 * emsg()) is normally updated has not been reached yet. We need to
4026 * update that flag first to make aborting() reliable.
4027 */
4028 update_force_abort();
4029 }
Bram Moolenaaref140542019-12-31 21:27:13 +01004030 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004031 ret = OK;
4032
Bram Moolenaar4c054e92019-11-10 00:13:50 +01004033theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004034 /*
4035 * Report an error unless the argument evaluation or function call has been
4036 * cancelled due to an aborting error, an interrupt, or an exception.
4037 */
4038 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004039 user_func_error(error, (name != NULL) ? name : funcname,
4040 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004041
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004042 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004043 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004044 clear_tv(&argv[--argv_clear + argv_base]);
4045
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004046 vim_free(tofree);
4047 vim_free(name);
4048
4049 return ret;
4050}
4051
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004052/*
4053 * Call a function without arguments, partial or dict.
4054 * This is like call_func() when the call is only "FuncName()".
4055 * To be used by "expr" options.
4056 * Returns NOTDONE when the function could not be found.
4057 */
4058 int
4059call_simple_func(
4060 char_u *funcname, // name of the function
zeertzjqc1ed7882024-08-01 22:46:54 +02004061 size_t len, // length of "name"
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004062 typval_T *rettv) // return value goes here
4063{
4064 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00004065 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004066 char_u fname_buf[FLEN_FIXED + 1];
4067 char_u *tofree = NULL;
4068 char_u *name;
4069 char_u *fname;
4070 char_u *rfname;
4071 int is_global = FALSE;
4072 ufunc_T *fp;
4073
4074 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4075 rettv->vval.v_number = 0;
4076
4077 // Make a copy of the name, an option can be changed in the function.
4078 name = vim_strnsave(funcname, len);
4079 if (name == NULL)
4080 return ret;
4081
4082 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4083
4084 // Skip "g:" before a function name.
4085 if (fname[0] == 'g' && fname[1] == ':')
4086 {
4087 is_global = TRUE;
4088 rfname = fname + 2;
4089 }
4090 else
4091 rfname = fname;
4092 fp = find_func(rfname, is_global);
4093 if (fp != NULL && !is_global && in_vim9script()
4094 && func_requires_g_prefix(fp))
4095 // In Vim9 script g: is required to find a global non-autoload
4096 // function.
4097 fp = NULL;
4098 if (fp == NULL)
4099 ret = NOTDONE;
4100 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4101 error = FCERR_DELETED;
4102 else if (fp != NULL)
4103 {
4104 typval_T argvars[1];
4105 funcexe_T funcexe;
4106
4107 argvars[0].v_type = VAR_UNKNOWN;
4108 CLEAR_FIELD(funcexe);
4109 funcexe.fe_evaluate = TRUE;
4110
4111 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4112 if (error == FCERR_NONE)
4113 ret = OK;
4114 }
4115
4116 user_func_error(error, name, FALSE);
4117 vim_free(tofree);
4118 vim_free(name);
4119
4120 return ret;
4121}
4122
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004123 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124printable_func_name(ufunc_T *fp)
4125{
4126 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4127}
4128
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004129/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004130 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4131 * TRUE. Otherwise return FALSE.
4132 */
4133 static int
4134function_list_modified(int prev_ht_changed)
4135{
4136 if (prev_ht_changed != func_hashtab.ht_changed)
4137 {
4138 emsg(_(e_function_list_was_modified));
4139 return TRUE;
4140 }
4141 return FALSE;
4142}
4143
4144/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004145 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004146 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004147 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004148list_func_head(ufunc_T *fp, int indent)
4149{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004150 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004151 int j;
4152
4153 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004154
4155 // a timer at the more prompt may have deleted the function
4156 if (function_list_modified(prev_ht_changed))
4157 return FAIL;
4158
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004159 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004160 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004161 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004162 msg_puts("def ");
4163 else
4164 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004166 msg_putchar('(');
4167 for (j = 0; j < fp->uf_args.ga_len; ++j)
4168 {
4169 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004170 msg_puts(", ");
4171 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 if (fp->uf_arg_types != NULL)
4173 {
4174 char *tofree;
4175
4176 msg_puts(": ");
4177 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4178 vim_free(tofree);
4179 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004180 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4181 {
4182 msg_puts(" = ");
4183 msg_puts(((char **)(fp->uf_def_args.ga_data))
4184 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4185 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004186 }
4187 if (fp->uf_varargs)
4188 {
4189 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004190 msg_puts(", ");
4191 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 if (fp->uf_va_name != NULL)
4194 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004195 if (!fp->uf_varargs)
4196 {
4197 if (j)
4198 msg_puts(", ");
4199 msg_puts("...");
4200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004202 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203 {
4204 char *tofree;
4205
4206 msg_puts(": ");
4207 msg_puts(type_name(fp->uf_va_type, &tofree));
4208 vim_free(tofree);
4209 }
4210 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004211 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004212
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004213 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004214 {
4215 if (fp->uf_ret_type != &t_void)
4216 {
4217 char *tofree;
4218
4219 msg_puts(": ");
4220 msg_puts(type_name(fp->uf_ret_type, &tofree));
4221 vim_free(tofree);
4222 }
4223 }
4224 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004225 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004226 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004227 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004228 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004229 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004230 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004231 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004232 msg_clr_eos();
4233 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004234 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004235
4236 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004237}
4238
4239/*
4240 * Get a function name, translating "<SID>" and "<SNR>".
4241 * Also handles a Funcref in a List or Dictionary.
4242 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004243 * Set "*is_global" to TRUE when the function must be global, unless
4244 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004245 * flags:
4246 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004247 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004248 * TFN_QUIET: be quiet
4249 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004250 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004251 * Advances "pp" to just after the function name (if no error).
4252 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004253 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004254trans_function_name(
4255 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004256 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004257 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004258 int flags)
4259{
4260 return trans_function_name_ext(pp, is_global, skip, flags,
4261 NULL, NULL, NULL, NULL);
4262}
4263
4264/*
4265 * trans_function_name() with extra arguments.
4266 * "fdp", "partial", "type" and "ufunc" can be NULL.
4267 */
4268 char_u *
4269trans_function_name_ext(
4270 char_u **pp,
4271 int *is_global,
4272 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004273 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004274 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004275 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004276 type_T **type, // return: type of funcref
4277 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004278{
4279 char_u *name = NULL;
4280 char_u *start;
4281 char_u *end;
4282 int lead;
4283 char_u sid_buf[20];
4284 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004286 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004287 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004288 int vim9script = in_vim9script();
4289 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004290
4291 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004292 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004293 start = *pp;
4294
Bram Moolenaare38eab22019-12-05 21:50:01 +01004295 // Check for hard coded <SNR>: already translated function ID (from a user
4296 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004297 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4298 && (*pp)[2] == (int)KE_SNR)
4299 {
4300 *pp += 3;
4301 len = get_id_len(pp) + 3;
4302 return vim_strnsave(start, len);
4303 }
4304
Bram Moolenaare38eab22019-12-05 21:50:01 +01004305 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4306 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004307 lead = eval_fname_script(start);
4308 if (lead > 2)
4309 start += lead;
4310
Bram Moolenaare38eab22019-12-05 21:50:01 +01004311 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004312 end = get_lval(start, NULL, &lv, FALSE, skip,
4313 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004314 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004315 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004316 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004317 {
4318 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004319 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004320 goto theend;
4321 }
4322 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4323 {
4324 /*
4325 * Report an invalid expression in braces, unless the expression
4326 * evaluation has been cancelled due to an aborting error, an
4327 * interrupt, or an exception.
4328 */
4329 if (!aborting())
4330 {
4331 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004332 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004333 }
4334 else
4335 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4336 goto theend;
4337 }
4338
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004339 if (lv.ll_ufunc != NULL)
4340 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004341 if (ufunc != NULL)
4342 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004343 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004344 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004345 goto theend;
4346 }
4347
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004348 if (lv.ll_tv != NULL)
4349 {
4350 if (fdp != NULL)
4351 {
4352 fdp->fd_dict = lv.ll_dict;
4353 fdp->fd_newkey = lv.ll_newkey;
4354 lv.ll_newkey = NULL;
4355 fdp->fd_di = lv.ll_di;
4356 }
4357 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4358 {
4359 name = vim_strsave(lv.ll_tv->vval.v_string);
4360 *pp = end;
4361 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004362 else if (lv.ll_tv->v_type == VAR_CLASS
4363 && lv.ll_tv->vval.v_class != NULL)
4364 {
4365 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4366 *pp = end;
4367 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004368 else if (lv.ll_tv->v_type == VAR_PARTIAL
4369 && lv.ll_tv->vval.v_partial != NULL)
4370 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004371 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004372 *pp = end;
4373 if (partial != NULL)
4374 *partial = lv.ll_tv->vval.v_partial;
4375 }
4376 else
4377 {
4378 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4379 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004380 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004381 else
4382 *pp = end;
4383 name = NULL;
4384 }
4385 goto theend;
4386 }
4387
4388 if (lv.ll_name == NULL)
4389 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004390 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004391 *pp = end;
4392 goto theend;
4393 }
4394
Bram Moolenaare38eab22019-12-05 21:50:01 +01004395 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004396 if (lv.ll_exp_name != NULL)
4397 {
4398 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004399 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004400 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004401 if (name == lv.ll_exp_name)
4402 name = NULL;
4403 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004404 else if (lv.ll_sid > 0)
4405 {
4406 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4407 int cc = *lv.ll_name_end;
4408
4409 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4410 *lv.ll_name_end = NUL;
4411 if (si->sn_autoload_prefix != NULL)
4412 {
4413 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4414 }
4415 else
4416 {
4417 sid_buf[0] = K_SPECIAL;
4418 sid_buf[1] = KS_EXTRA;
4419 sid_buf[2] = (int)KE_SNR;
4420 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004421 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004422 name = concat_str(sid_buf, lv.ll_name);
4423 }
4424 *lv.ll_name_end = cc;
4425 *pp = end;
4426 goto theend;
4427 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004428 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004429 {
4430 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004431 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004432 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004433 if (name == *pp)
4434 name = NULL;
4435 }
4436 if (name != NULL)
4437 {
4438 name = vim_strsave(name);
4439 *pp = end;
4440 if (STRNCMP(name, "<SNR>", 5) == 0)
4441 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004442 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004443 name[0] = K_SPECIAL;
4444 name[1] = KS_EXTRA;
4445 name[2] = (int)KE_SNR;
4446 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4447 }
4448 goto theend;
4449 }
4450
4451 if (lv.ll_exp_name != NULL)
4452 {
4453 len = (int)STRLEN(lv.ll_exp_name);
4454 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4455 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4456 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004457 // When there was "s:" already or the name expanded to get a
4458 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004459 lv.ll_name += 2;
4460 len -= 2;
4461 lead = 2;
4462 }
4463 }
4464 else
4465 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004466 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004467 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004468 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004469 if (lv.ll_name[0] == 'g')
4470 {
4471 if (is_global != NULL)
4472 {
4473 *is_global = TRUE;
4474 }
4475 else
4476 {
4477 // dropping "g:" without setting "is_global" won't work in
4478 // Vim9script, put it back later
4479 prefix_g = TRUE;
4480 extra = 2;
4481 }
4482 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004483 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004484 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004485 len = (int)(end - lv.ll_name);
4486 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004487 if (len <= 0)
4488 {
4489 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004490 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004491 goto theend;
4492 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004493
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004494 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004495 // starts with a lower case character: dict.func(). Or when in a class.
4496 vim9_local = ASCII_ISUPPER(*start) && vim9script
4497 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004499 /*
4500 * Copy the function name to allocated memory.
4501 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4502 * Accept <SNR>123_name() outside a script.
4503 */
4504 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004505 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004506 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004507 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004508 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004509 {
Kota Kato948a3892022-08-16 16:09:59 +01004510 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4511 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004512 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004513 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004514 goto theend;
4515 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004517 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004518 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004520 || eval_fname_sid(*pp))
4521 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004523 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004524 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004525 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004526 goto theend;
4527 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004528 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004529 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 extra = 3 + (int)STRLEN(sid_buf);
4531 else
4532 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004533 }
4534 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004535 // The function name must start with an upper case letter (unless it is a
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004536 // Vim9 class new() function or a Vim9 class private method or one of the
4537 // supported Vim9 object builtin functions)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004538 else if (!(flags & TFN_INT)
4539 && (builtin_function(lv.ll_name, len)
4540 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004541 && !((flags & TFN_IN_CLASS)
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004542 && (is_valid_builtin_obj_methodname(lv.ll_name)
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004543 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004544 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004545 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004546 : e_function_name_must_start_with_capital_or_s_str),
4547 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004548 goto theend;
4549 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004550 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004551 {
4552 char_u *cp = vim_strchr(lv.ll_name, ':');
4553
4554 if (cp != NULL && cp < end)
4555 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004556 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004557 goto theend;
4558 }
4559 }
4560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004562 if (name != NULL)
4563 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004564 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004565 {
4566 name[0] = K_SPECIAL;
4567 name[1] = KS_EXTRA;
4568 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004569 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004570 STRCPY(name + 3, sid_buf);
4571 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004572 else if (prefix_g)
4573 {
4574 name[0] = 'g';
4575 name[1] = ':';
4576 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4578 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004579 }
4580 *pp = end;
4581
4582theend:
4583 clear_lval(&lv);
4584 return name;
4585}
4586
4587/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004588 * Assuming "name" is the result of trans_function_name() and it was prefixed
4589 * to use the script-local name, return the unmodified name (points into
4590 * "name"). Otherwise return NULL.
4591 * This can be used to first search for a script-local function and fall back
4592 * to the global function if not found.
4593 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004594 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004595untrans_function_name(char_u *name)
4596{
4597 char_u *p;
4598
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004599 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004600 {
4601 p = vim_strchr(name, '_');
4602 if (p != NULL)
4603 return p + 1;
4604 }
4605 return NULL;
4606}
4607
4608/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004609 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4610 * current script ID and returns the expanded function name. The caller should
4611 * free the returned name. If not called from a script context or the function
4612 * name doesn't start with these prefixes, then returns NULL.
4613 * This doesn't check whether the script-local function exists or not.
4614 */
4615 char_u *
4616get_scriptlocal_funcname(char_u *funcname)
4617{
4618 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004619 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004620 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004621 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004622
4623 if (funcname == NULL)
4624 return NULL;
4625
4626 if (STRNCMP(funcname, "s:", 2) != 0
4627 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004628 {
4629 ufunc_T *ufunc;
4630
4631 // The function name does not have a script-local prefix. Try finding
4632 // it when in a Vim9 script and there is no "g:" prefix.
4633 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4634 return NULL;
4635 ufunc = find_func(funcname, FALSE);
4636 if (ufunc == NULL || func_is_global(ufunc)
4637 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4638 return NULL;
4639 ++p;
4640 off = 0;
4641 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004642 else
4643 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004644
4645 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4646 {
4647 emsg(_(e_using_sid_not_in_script_context));
4648 return NULL;
4649 }
4650 // Expand s: prefix into <SNR>nr_<name>
4651 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4652 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004653 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004654 if (newname == NULL)
4655 return NULL;
4656 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004657 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004658
4659 return newname;
4660}
4661
4662/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004663 * Return script-local "fname" with the 3-byte sequence replaced by
4664 * printable <SNR> in allocated memory.
4665 */
4666 char_u *
4667alloc_printable_func_name(char_u *fname)
4668{
4669 char_u *n = alloc(STRLEN(fname + 3) + 6);
4670
4671 if (n != NULL)
4672 {
4673 STRCPY(n, "<SNR>");
4674 STRCPY(n + 5, fname + 3);
4675 }
4676 return n;
4677}
4678
4679/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004680 * Call trans_function_name(), except that a lambda is returned as-is.
4681 * Returns the name in allocated memory.
4682 */
4683 char_u *
4684save_function_name(
4685 char_u **name,
4686 int *is_global,
4687 int skip,
4688 int flags,
4689 funcdict_T *fudi)
4690{
4691 char_u *p = *name;
4692 char_u *saved;
4693
4694 if (STRNCMP(p, "<lambda>", 8) == 0)
4695 {
4696 p += 8;
4697 (void)getdigits(&p);
4698 saved = vim_strnsave(*name, p - *name);
4699 if (fudi != NULL)
4700 CLEAR_POINTER(fudi);
4701 }
4702 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004703 saved = trans_function_name_ext(&p, is_global, skip,
4704 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004705 *name = p;
4706 return saved;
4707}
4708
4709/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004710 * List functions. When "regmatch" is NULL all of then.
4711 * Otherwise functions matching "regmatch".
4712 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004713 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004714list_functions(regmatch_T *regmatch)
4715{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004716 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004717 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004718 hashitem_T *hi;
4719
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004720 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004721 {
4722 if (!HASHITEM_EMPTY(hi))
4723 {
4724 ufunc_T *fp = HI2UF(hi);
4725
4726 --todo;
4727 if ((fp->uf_flags & FC_DEAD) == 0
4728 && (regmatch == NULL
4729 ? !message_filtered(fp->uf_name)
4730 && !func_name_refcount(fp->uf_name)
Keith Thompson184f71c2024-01-04 21:19:04 +01004731 : !SAFE_isdigit(*fp->uf_name)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004732 && vim_regexec(regmatch, fp->uf_name, 0)))
4733 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004734 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004735 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004736 if (function_list_modified(prev_ht_changed))
4737 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004738 }
4739 }
4740 }
4741}
4742
4743/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004744 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004745 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4746 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004747 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004748 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4749 * With CF_INTERFACE the function is define inside an interface, only the
4750 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004751 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004752 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004753 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004754define_function(
4755 exarg_T *eap,
4756 char_u *name_arg,
4757 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004758 int class_flags,
4759 ocmember_T *obj_members,
4760 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004761{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004762 int j;
4763 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004764 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004765 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004766 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004767 char_u *p;
4768 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004769 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004770 char_u *line_arg = NULL;
4771 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004773 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004774 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004775 garray_T newlines;
4776 int varargs = FALSE;
4777 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004779 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004780 int fp_allocated = FALSE;
4781 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004782 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004783 dictitem_T *v;
4784 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004785 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004786 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004787 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004788 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004789 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004790 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004791
4792 /*
4793 * ":function" without argument: list functions.
4794 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004795 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004796 {
4797 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004798 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004799 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004800 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004801 }
4802
4803 /*
4804 * ":function /pat": list functions matching pattern.
4805 */
4806 if (*eap->arg == '/')
4807 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004808 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004809 if (!eap->skip)
4810 {
4811 regmatch_T regmatch;
4812
4813 c = *p;
4814 *p = NUL;
4815 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4816 *p = c;
4817 if (regmatch.regprog != NULL)
4818 {
4819 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004820 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004821 vim_regfree(regmatch.regprog);
4822 }
4823 }
4824 if (*p == '/')
4825 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004826 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004827 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004828 }
4829
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830 ga_init(&newargs);
4831 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004832 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833 ga_init(&default_args);
4834
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004835 /*
4836 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004837 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004838 * "name" == func, "fudi.fd_dict" == NULL
4839 * dict.func new dictionary entry
4840 * "name" == NULL, "fudi.fd_dict" set,
4841 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4842 * dict.func existing dict entry with a Funcref
4843 * "name" == func, "fudi.fd_dict" set,
4844 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4845 * dict.func existing dict entry that's not a Funcref
4846 * "name" == NULL, "fudi.fd_dict" set,
4847 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4848 * s:func script-local function name
4849 * g:func global function name, same as "func"
4850 */
4851 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004852 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004853 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004854 // nested function, argument is (args).
4855 paren = TRUE;
4856 CLEAR_FIELD(fudi);
4857 }
4858 else
4859 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004860 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004861 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004862 if (p[0] == 's' && p[1] == ':')
4863 {
4864 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4865 return NULL;
4866 }
4867 p = to_name_end(p, TRUE);
4868 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4869 {
4870 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4871 eap->arg);
4872 return NULL;
4873 }
4874 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004875 }
4876
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004877 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004878 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004879 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004880 paren = (vim_strchr(p, '(') != NULL);
4881 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004882 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004883 /*
4884 * Return on an invalid expression in braces, unless the expression
4885 * evaluation has been cancelled due to an aborting error, an
4886 * interrupt, or an exception.
4887 */
4888 if (!aborting())
4889 {
4890 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004891 semsg(_(e_key_not_present_in_dictionary_str),
4892 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004893 vim_free(fudi.fd_newkey);
4894 return NULL;
4895 }
4896 else
4897 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004898 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004899
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004900 // For "export def FuncName()" in an autoload script the function name
4901 // is stored with the legacy autoload name "dir#script#FuncName" so
4902 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004903 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004904 {
4905 char_u *prefixed = may_prefix_autoload(name);
4906
4907 if (prefixed != NULL && prefixed != name)
4908 {
4909 vim_free(name);
4910 name = prefixed;
4911 }
4912 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004913 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004914 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004915 {
4916 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4917 goto ret_free;
4918 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004919 }
4920
Bram Moolenaare38eab22019-12-05 21:50:01 +01004921 // An error in a function call during evaluation of an expression in magic
4922 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004923 saved_did_emsg = did_emsg;
4924 did_emsg = FALSE;
4925
4926 /*
4927 * ":function func" with only function name: list function.
4928 */
4929 if (!paren)
4930 {
4931 if (!ends_excmd(*skipwhite(p)))
4932 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004933 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004934 goto ret_free;
4935 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004936 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004937 if (eap->nextcmd != NULL)
4938 *p = NUL;
4939 if (!eap->skip && !got_int)
4940 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004941 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004942 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4943 {
4944 char_u *up = untrans_function_name(name);
4945
4946 // With Vim9 script the name was made script-local, if not
4947 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004948 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004949 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004950 }
4951
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004952 if (fp != NULL)
4953 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004954 // Check no function was added or removed from a timer, e.g. at
4955 // the more prompt. "fp" may then be invalid.
4956 int prev_ht_changed = func_hashtab.ht_changed;
4957
4958 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004959 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004960 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4961 {
4962 if (FUNCLINE(fp, j) == NULL)
4963 continue;
4964 msg_putchar('\n');
4965 msg_outnum((long)(j + 1));
4966 if (j < 9)
4967 msg_putchar(' ');
4968 if (j < 99)
4969 msg_putchar(' ');
4970 if (function_list_modified(prev_ht_changed))
4971 break;
4972 msg_prt_line(FUNCLINE(fp, j), FALSE);
4973 out_flush(); // show a line at a time
4974 ui_breakcheck();
4975 }
4976 if (!got_int)
4977 {
4978 msg_putchar('\n');
4979 if (!function_list_modified(prev_ht_changed))
4980 {
4981 if (fp->uf_def_status != UF_NOT_COMPILED)
4982 msg_puts(" enddef");
4983 else
4984 msg_puts(" endfunction");
4985 }
4986 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004987 }
4988 }
4989 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004990 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004991 }
4992 goto ret_free;
4993 }
4994
4995 /*
4996 * ":function name(arg1, arg2)" Define function.
4997 */
4998 p = skipwhite(p);
4999 if (*p != '(')
5000 {
5001 if (!eap->skip)
5002 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005003 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005004 goto ret_free;
5005 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005006 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005007 if (vim_strchr(p, '(') != NULL)
5008 p = vim_strchr(p, '(');
5009 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005010
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005011 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
5012 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01005013 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005014 goto ret_free;
5015 }
5016
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005017 // In Vim9 script only global functions can be redefined.
5018 if (vim9script && eap->forceit && !is_global)
5019 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005020 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005021 goto ret_free;
5022 }
5023
Bram Moolenaar04935fb2022-01-08 16:19:22 +00005024 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005025
Bram Moolenaar04b12692020-05-04 23:24:44 +02005026 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005027 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005028 // Check the name of the function. Unless it's a dictionary function
5029 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005030 if (name != NULL)
5031 arg = name;
5032 else
5033 arg = fudi.fd_newkey;
5034 if (arg != NULL && (fudi.fd_di == NULL
5035 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
5036 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
5037 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00005038 char_u *name_base = arg;
5039 int i;
5040
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005041 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005042 {
5043 name_base = vim_strchr(arg, '_');
5044 if (name_base == NULL)
5045 name_base = arg + 3;
5046 else
5047 ++name_base;
5048 }
5049 for (i = 0; name_base[i] != NUL && (i == 0
5050 ? eval_isnamec1(name_base[i])
5051 : eval_isnamec(name_base[i])); ++i)
5052 ;
5053 if (name_base[i] != NUL)
zeertzjq6a04bf52024-03-16 09:39:06 +01005054 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005055 emsg_funcname(e_invalid_argument_str, arg);
zeertzjq6a04bf52024-03-16 09:39:06 +01005056 goto ret_free;
5057 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00005058
5059 // In Vim9 script a function cannot have the same name as a
5060 // variable.
5061 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005062 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
5063 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00005064 + EVAL_VAR_NO_FUNC) == OK)
5065 {
5066 semsg(_(e_redefining_script_item_str), name_base);
5067 goto ret_free;
5068 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005069 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005070 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005071 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005072 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005073 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00005074 goto ret_free;
5075 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005076 }
5077
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005078 // This may get more lines and make the pointers into the first line
5079 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005080 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005081 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005082 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005083 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005084 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005085 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005086 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005087 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005088
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005090 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005092 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005093 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005094 if (*p != ':')
5095 {
5096 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5097 p = skipwhite(p);
5098 }
5099 else if (!IS_WHITE_OR_NUL(p[1]))
5100 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005102 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005104 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005105 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005106 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005107 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005108 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005110 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005111 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005112 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005113 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005114 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005115 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005116 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005117 else
5118 // find extra arguments "range", "dict", "abort" and "closure"
5119 for (;;)
5120 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005121 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005122 p = skipwhite(p);
5123 if (STRNCMP(p, "range", 5) == 0)
5124 {
5125 flags |= FC_RANGE;
5126 p += 5;
5127 }
5128 else if (STRNCMP(p, "dict", 4) == 0)
5129 {
5130 flags |= FC_DICT;
5131 p += 4;
5132 }
5133 else if (STRNCMP(p, "abort", 5) == 0)
5134 {
5135 flags |= FC_ABORT;
5136 p += 5;
5137 }
5138 else if (STRNCMP(p, "closure", 7) == 0)
5139 {
5140 flags |= FC_CLOSURE;
5141 p += 7;
5142 if (current_funccal == NULL)
5143 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005144 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005145 name == NULL ? (char_u *)"" : name);
5146 goto erret;
5147 }
5148 }
5149 else
5150 break;
5151 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005152
Bram Moolenaare38eab22019-12-05 21:50:01 +01005153 // When there is a line break use what follows for the function body.
5154 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005155 if (*p == '\n')
5156 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005157 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005158 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5159 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005160 && !(VIM_ISWHITE(*whitep) && *p == '#'
5161 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005162 && !eap->skip
5163 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005164 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005165
5166 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5168 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005169 */
5170 if (KeyTyped)
5171 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005172 // Check if the function already exists, don't let the user type the
5173 // whole function before telling him it doesn't work! For a script we
5174 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005175 if (!eap->skip && !eap->forceit)
5176 {
5177 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005178 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005179 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005180 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005181 }
5182
5183 if (!eap->skip && did_emsg)
5184 goto erret;
5185
Bram Moolenaare38eab22019-12-05 21:50:01 +01005186 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005187 cmdline_row = msg_row;
5188 }
5189
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005190 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005191 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005192
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005193 // Do not define the function when getting the body fails and when
5194 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005195 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005196 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005197 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5198 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005199 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005200 goto erret;
5201
5202 /*
5203 * If there are no errors, add the function
5204 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005205 if (fudi.fd_dict != NULL)
5206 {
5207 char numbuf[20];
5208
5209 fp = NULL;
5210 if (fudi.fd_newkey == NULL && !eap->forceit)
5211 {
5212 emsg(_(e_dictionary_entry_already_exists));
5213 goto erret;
5214 }
5215 if (fudi.fd_di == NULL)
5216 {
5217 // Can't add a function to a locked dictionary
5218 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5219 goto erret;
5220 }
5221 // Can't change an existing function if it is locked
5222 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5223 goto erret;
5224
5225 // Give the function a sequential number. Can only be used with a
5226 // Funcref!
5227 vim_free(name);
5228 sprintf(numbuf, "%d", ++func_nr);
5229 name = vim_strsave((char_u *)numbuf);
5230 if (name == NULL)
5231 goto erret;
5232 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005233 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005234 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005236 char_u *find_name = name;
5237 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005238 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005240 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005241 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005242 var_conflict = TRUE;
5243
5244 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5245 {
5246 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5247
5248 if (si->sn_autoload_prefix != NULL)
5249 {
5250 if (is_export)
5251 {
5252 find_name = name + STRLEN(si->sn_autoload_prefix);
5253 v = find_var(find_name, &ht, TRUE);
5254 if (v != NULL)
5255 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005256 // Only check if the function already exists in the script,
5257 // global functions can be shadowed.
5258 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005259 }
5260 else
5261 {
5262 char_u *prefixed = may_prefix_autoload(name);
5263
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005264 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005265 {
5266 v = find_var(prefixed, &ht, TRUE);
5267 if (v != NULL)
5268 var_conflict = TRUE;
5269 vim_free(prefixed);
5270 }
5271 }
5272 }
5273 }
5274 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005275 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005276 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005277 goto erret;
5278 }
5279
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005280 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005281 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005282 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005283 char_u *uname = untrans_function_name(name);
5284
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005285 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005286 }
5287
5288 if (fp != NULL || import != NULL)
5289 {
5290 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005292 // Function can be replaced with "function!" and when sourcing the
5293 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005294 // A name that is used by an import can not be overruled.
5295 if (import != NULL
5296 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005297 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005298 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005299 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005300 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005301 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005302 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005303 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005304 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005305 goto erret;
5306 }
5307 if (fp->uf_calls > 0)
5308 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005309 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005310 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005311 goto erret;
5312 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005313 if (fp->uf_refcount > 1)
5314 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005315 // This function is referenced somewhere, don't redefine it but
5316 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005317 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005318 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005319 fp = NULL;
5320 overwrite = TRUE;
5321 }
5322 else
5323 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005324 char_u *exp_name = fp->uf_name_exp;
5325
5326 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005327 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005328 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005329 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005330 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005331 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005332#ifdef FEAT_PROFILE
5333 fp->uf_profiling = FALSE;
5334 fp->uf_prof_initialized = FALSE;
5335#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005336 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005337 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005338 }
5339 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005340
5341 if (fp == NULL)
5342 {
5343 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5344 {
5345 int slen, plen;
5346 char_u *scriptname;
5347
Bram Moolenaare38eab22019-12-05 21:50:01 +01005348 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005349 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005350 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005351 {
5352 scriptname = autoload_name(name);
5353 if (scriptname != NULL)
5354 {
5355 p = vim_strchr(scriptname, '/');
5356 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005357 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005358 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005359 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005360 j = OK;
5361 vim_free(scriptname);
5362 }
5363 }
5364 if (j == FAIL)
5365 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005366 linenr_T save_lnum = SOURCING_LNUM;
5367
5368 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005369 semsg(_(e_function_name_does_not_match_script_file_name_str),
5370 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005371 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005372 goto erret;
5373 }
5374 }
5375
Bram Moolenaare01e5212023-01-08 20:31:18 +00005376 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005377 if (fp == NULL)
5378 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005379 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005380
5381 if (fudi.fd_dict != NULL)
5382 {
5383 if (fudi.fd_di == NULL)
5384 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005385 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005386 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5387 if (fudi.fd_di == NULL)
5388 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005389 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005390 goto erret;
5391 }
5392 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5393 {
5394 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005395 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005396 goto erret;
5397 }
5398 }
5399 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005400 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005401 clear_tv(&fudi.fd_di->di_tv);
5402 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005403 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005404
Bram Moolenaare38eab22019-12-05 21:50:01 +01005405 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005406 flags |= FC_DICT;
5407 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005408 }
5409 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005410 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005411 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005412 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005413
5414 if (eap->cmdidx == CMD_def)
5415 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005416 int lnum_save = SOURCING_LNUM;
5417 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005418
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005419 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005420
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005421 // error messages are for the first function line
5422 SOURCING_LNUM = sourcing_lnum_top;
5423
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005424 // The function may use script variables from the context.
5425 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005426
h-eastb895b0f2023-09-24 15:46:31 +02005427 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5428 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005429 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005430 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005431 free_fp = fp_allocated;
5432 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005433 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005434 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005435
5436 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005437 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005438 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005439 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005440 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005441 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005443 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005444 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005445 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005446 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005447
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005448 if (fp_allocated)
5449 {
5450 // insert the new function in the function list
5451 set_ufunc_name(fp, name);
5452 if (overwrite)
5453 {
5454 hi = hash_find(&func_hashtab, name);
5455 hi->hi_key = UF2HIKEY(fp);
5456 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005457 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005458 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005459 {
5460 free_fp = TRUE;
5461 goto erret;
5462 }
5463 fp->uf_refcount = 1;
5464 }
5465
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005466 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005467 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005468 if ((flags & FC_CLOSURE) != 0)
5469 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005470 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005471 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005472 }
5473 else
5474 fp->uf_scoped = NULL;
5475
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005476#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005477 if (prof_def_func())
5478 func_do_profile(fp);
5479#endif
5480 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005481 if (sandbox)
5482 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005483 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005484 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005485 fp->uf_flags = flags;
5486 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005487 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005488 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005489 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005490 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005491 if (is_export)
5492 {
5493 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005494 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005495 is_export = FALSE;
5496 }
5497
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005498 if (eap->cmdidx == CMD_def)
5499 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005500 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5501 // :func does not use Vim9 script syntax, even in a Vim9 script file
5502 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005503
Yegappan Lakshmanan5715a722024-05-03 18:24:07 +02005504 // If test_override('defcompile' 1) is used, then compile the function now
5505 if (eap->cmdidx == CMD_def && override_defcompile)
5506 defcompile_function(fp, NULL);
5507
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005508 goto ret_free;
5509
5510erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005511 if (fp != NULL)
5512 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005513 // these were set to "newargs" and "default_args", which are cleared
5514 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005515 ga_init(&fp->uf_args);
5516 ga_init(&fp->uf_def_args);
5517 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005518errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005519 ga_clear_strings(&newargs);
5520 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005521 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005522 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005523 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005524 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005525 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01005526 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005527 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005528 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005529 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005530ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005531 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005532 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005533 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005534 if (name != name_arg)
5535 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005536 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005537 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005538
5539 return fp;
5540}
5541
5542/*
5543 * ":function"
5544 */
5545 void
5546ex_function(exarg_T *eap)
5547{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005548 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005549
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005550 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005551 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005552 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005553}
5554
LemonBoy48b7d052024-07-09 18:24:59 +02005555 int
5556get_func_arity(char_u *name, int *required, int *optional, int *varargs)
5557{
5558 ufunc_T *ufunc = NULL;
5559 int argcount = 0;
5560 int min_argcount = 0;
5561 int idx;
5562
5563 idx = find_internal_func(name);
5564 if (idx >= 0)
5565 {
5566 internal_func_get_argcount(idx, &argcount, &min_argcount);
5567 *varargs = FALSE;
5568 }
5569 else
5570 {
5571 char_u fname_buf[FLEN_FIXED + 1];
5572 char_u *tofree = NULL;
5573 funcerror_T error = FCERR_NONE;
5574 char_u *fname;
5575
5576 // May need to translate <SNR>123_ to K_SNR.
5577 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
5578 if (error == FCERR_NONE)
5579 ufunc = find_func(fname, FALSE);
5580 vim_free(tofree);
5581
5582 if (ufunc == NULL)
5583 return FAIL;
5584
5585 argcount = ufunc->uf_args.ga_len;
5586 min_argcount = ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len;
5587 *varargs = has_varargs(ufunc);
5588 }
5589
5590 *required = min_argcount;
5591 *optional = argcount - min_argcount;
5592
5593 return OK;
5594}
5595
Bram Moolenaar822ba242020-05-24 23:00:18 +02005596/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005597 * Find a function by name, including "<lambda>123".
5598 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005599 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005600 * Return NULL if not found.
5601 */
5602 ufunc_T *
5603find_func_by_name(char_u *name, compiletype_T *compile_type)
5604{
5605 char_u *arg = name;
5606 char_u *fname;
5607 ufunc_T *ufunc;
5608 int is_global = FALSE;
5609
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005610 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5611 {
5612 *compile_type = CT_PROFILE;
5613 arg = skipwhite(arg + 7);
5614 }
5615 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5616 {
5617 *compile_type = CT_DEBUG;
5618 arg = skipwhite(arg + 5);
5619 }
5620
5621 if (STRNCMP(arg, "<lambda>", 8) == 0)
5622 {
5623 arg += 8;
5624 (void)getdigits(&arg);
5625 fname = vim_strnsave(name, arg - name);
5626 }
5627 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005628 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005629 // First try finding a method in a class, trans_function_name() will
5630 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005631 ufunc = find_class_func(&arg);
5632 if (ufunc != NULL)
5633 return ufunc;
5634
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005635 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005636 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005637 NULL, NULL, NULL, &ufunc);
5638 if (ufunc != NULL)
5639 {
5640 vim_free(fname);
5641 return ufunc;
5642 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005643 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005644 if (fname == NULL)
5645 {
5646 semsg(_(e_invalid_argument_str), name);
5647 return NULL;
5648 }
5649 if (!ends_excmd2(name, arg))
5650 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005651 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005652 emsg(ex_errmsg(e_trailing_characters_str, arg));
5653 return NULL;
5654 }
5655
5656 ufunc = find_func(fname, is_global);
5657 if (ufunc == NULL)
5658 {
5659 char_u *p = untrans_function_name(fname);
5660
5661 if (p != NULL)
5662 // Try again without making it script-local.
5663 ufunc = find_func(p, FALSE);
5664 }
5665 vim_free(fname);
5666 if (ufunc == NULL)
5667 semsg(_(e_cannot_find_function_str), name);
5668 return ufunc;
5669}
5670
5671/*
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005672 * Compile the :def function "ufunc". If "cl" is not NULL, then compile the
5673 * class or object method "ufunc" in "cl".
5674 */
5675 void
5676defcompile_function(ufunc_T *ufunc, class_T *cl)
5677{
5678 compiletype_T compile_type = CT_NONE;
5679
5680 if (func_needs_compiling(ufunc, compile_type))
5681 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5682 else
5683 smsg(_("Function %s%s%s does not need compiling"),
5684 cl != NULL ? cl->class_name : (char_u *)"",
5685 cl != NULL ? (char_u *)"." : (char_u *)"",
5686 ufunc->uf_name);
5687}
5688
5689/*
5690 * Compile all the :def functions defined in the current script
5691 */
5692 static void
5693defcompile_funcs_in_script(void)
5694{
5695 long todo = (long)func_hashtab.ht_used;
5696 int changed = func_hashtab.ht_changed;
5697 hashitem_T *hi;
5698
5699 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5700 {
5701 if (!HASHITEM_EMPTY(hi))
5702 {
5703 --todo;
5704 ufunc_T *ufunc = HI2UF(hi);
5705 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5706 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5707 && (ufunc->uf_flags & FC_DEAD) == 0)
5708 {
5709 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5710
5711 if (func_hashtab.ht_changed != changed)
5712 {
5713 // a function has been added or removed, need to start
5714 // over
5715 todo = (long)func_hashtab.ht_used;
5716 changed = func_hashtab.ht_changed;
5717 hi = func_hashtab.ht_array;
5718 --hi;
5719 }
5720 }
5721 }
5722 }
5723}
5724
5725/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005726 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005727 * be compiled or the one specified by the argument.
5728 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005729 */
5730 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005731ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005732{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005733 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005734 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005735 typval_T tv;
5736
5737 if (is_class_name(eap->arg, &tv))
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005738 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005739 class_T *cl = tv.vval.v_class;
5740
5741 if (cl != NULL)
5742 defcompile_class(cl);
5743 }
5744 else
5745 {
5746 compiletype_T compile_type = CT_NONE;
5747 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
5748 if (ufunc != NULL)
5749 defcompile_function(ufunc, NULL);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005750 }
5751 }
5752 else
5753 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005754 defcompile_funcs_in_script();
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005755
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005756 // compile all the class defined in the current script
5757 defcompile_classes_in_script();
Bram Moolenaar822ba242020-05-24 23:00:18 +02005758 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005759}
5760
5761/*
5762 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5763 * Return 2 if "p" starts with "s:".
5764 * Return 0 otherwise.
5765 */
5766 int
5767eval_fname_script(char_u *p)
5768{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005769 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5770 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005771 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5772 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5773 return 5;
5774 if (p[0] == 's' && p[1] == ':')
5775 return 2;
5776 return 0;
5777}
5778
5779 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005780translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005781{
5782 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005783 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005784 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005785}
5786
5787/*
5788 * Return TRUE when "ufunc" has old-style "..." varargs
5789 * or named varargs "...name: type".
5790 */
5791 int
5792has_varargs(ufunc_T *ufunc)
5793{
5794 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005795}
5796
5797/*
5798 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005799 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005800 */
5801 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005802function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005803{
5804 char_u *nm = name;
5805 char_u *p;
5806 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005807 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005808 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005809
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005810 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5811 if (no_deref)
5812 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005813 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005814 nm = skipwhite(nm);
5815
Bram Moolenaare38eab22019-12-05 21:50:01 +01005816 // Only accept "funcname", "funcname ", "funcname (..." and
5817 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005818 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005819 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005820 vim_free(p);
5821 return n;
5822}
5823
Bram Moolenaar113e1072019-01-20 15:30:40 +01005824#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005825 char_u *
5826get_expanded_name(char_u *name, int check)
5827{
5828 char_u *nm = name;
5829 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005830 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005831
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005832 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005833
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005834 if (p != NULL && *nm == NUL
5835 && (!check || translated_function_exists(p, is_global)))
5836 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005837
5838 vim_free(p);
5839 return NULL;
5840}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005841#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005842
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005843/*
5844 * Function given to ExpandGeneric() to obtain the list of user defined
5845 * function names.
5846 */
5847 char_u *
5848get_user_func_name(expand_T *xp, int idx)
5849{
5850 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005851 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005852 static hashitem_T *hi;
5853 ufunc_T *fp;
5854
5855 if (idx == 0)
5856 {
5857 done = 0;
5858 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005859 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005860 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005861 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005862 {
5863 if (done++ > 0)
5864 ++hi;
5865 while (HASHITEM_EMPTY(hi))
5866 ++hi;
5867 fp = HI2UF(hi);
5868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005869 // don't show dead, dict and lambda functions
5870 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005871 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005872 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005873
5874 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005875 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005876
5877 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005878 if (xp->xp_context != EXPAND_USER_FUNC
5879 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005880 {
5881 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005882 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005883 STRCAT(IObuff, ")");
5884 }
5885 return IObuff;
5886 }
5887 return NULL;
5888}
5889
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005890/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005891 * Make a copy of a function.
5892 * Intended to be used for a function defined on a base class that has a copy
5893 * on the child class.
5894 * The copy has uf_refcount set to one.
5895 * Returns NULL when out of memory.
5896 */
5897 ufunc_T *
5898copy_function(ufunc_T *fp)
5899{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005900 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005901 if (ufunc == NULL)
5902 return NULL;
5903
5904 // Most things can just be copied.
5905 *ufunc = *fp;
5906
5907 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5908 ufunc->uf_dfunc_idx = 0;
5909 ufunc->uf_class = NULL;
5910
5911 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5912 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5913
5914 if (ufunc->uf_arg_types != NULL)
5915 {
5916 // "uf_arg_types" is an allocated array, make a copy.
5917 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5918 if (at != NULL)
5919 {
5920 mch_memmove(at, ufunc->uf_arg_types,
5921 sizeof(type_T *) * ufunc->uf_args.ga_len);
5922 ufunc->uf_arg_types = at;
5923 }
5924 }
5925
5926 // TODO: how about the types themselves? they can be freed when the
5927 // original function is freed:
5928 // type_T **uf_arg_types;
5929 // type_T *uf_ret_type;
5930
Ernie Rael114ec812023-06-04 18:11:35 +01005931 // make uf_type_list empty
5932 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005933
5934 // TODO: partial_T *uf_partial;
5935
5936 if (ufunc->uf_va_name != NULL)
5937 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5938
5939 // TODO:
5940 // type_T *uf_va_type;
5941 // type_T *uf_func_type;
5942
5943 ufunc->uf_block_depth = 0;
5944 ufunc->uf_block_ids = NULL;
5945
5946 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5947
5948 ufunc->uf_refcount = 1;
5949 ufunc->uf_name_exp = NULL;
5950 STRCPY(ufunc->uf_name, fp->uf_name);
5951
5952 return ufunc;
5953}
5954
5955/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005956 * ":delfunction {name}"
5957 */
5958 void
5959ex_delfunction(exarg_T *eap)
5960{
5961 ufunc_T *fp = NULL;
5962 char_u *p;
5963 char_u *name;
5964 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005965 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005966
5967 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005968 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5969 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005970 vim_free(fudi.fd_newkey);
5971 if (name == NULL)
5972 {
5973 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005974 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005975 return;
5976 }
5977 if (!ends_excmd(*skipwhite(p)))
5978 {
5979 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005980 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005981 return;
5982 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005983 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005984 if (eap->nextcmd != NULL)
5985 *p = NUL;
5986
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005987 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005988 {
5989 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005990 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005991 vim_free(name);
5992 return;
5993 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005994 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005995 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005996 vim_free(name);
5997
5998 if (!eap->skip)
5999 {
6000 if (fp == NULL)
6001 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02006002 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00006003 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006004 return;
6005 }
6006 if (fp->uf_calls > 0)
6007 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006008 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006009 return;
6010 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006011 if (fp->uf_flags & FC_VIM9)
6012 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006013 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006014 return;
6015 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006016
6017 if (fudi.fd_dict != NULL)
6018 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006019 // Delete the dict item that refers to the function, it will
6020 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00006021 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006022 }
6023 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006024 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006025 // A normal function (not a numbered function or lambda) has a
6026 // refcount of 1 for the entry in the hashtable. When deleting
6027 // it and the refcount is more than one, it should be kept.
6028 // A numbered function and lambda should be kept if the refcount is
6029 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006030 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006031 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006032 // Function is still referenced somewhere. Don't free it but
6033 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006034 if (func_remove(fp))
6035 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006036 }
6037 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006038 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006039 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006040 }
6041}
6042
6043/*
6044 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006045 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006046 */
6047 void
6048func_unref(char_u *name)
6049{
Bram Moolenaar97baee82016-07-26 20:46:08 +02006050 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006051
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006052 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006053 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006054 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006055 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006056 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006057#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006058 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006059#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01006060 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006061 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006062 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006063}
6064
6065/*
6066 * Unreference a Function: decrement the reference count and free it when it
6067 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006068 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006069 */
6070 void
6071func_ptr_unref(ufunc_T *fp)
6072{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006073 if (fp != NULL && (--fp->uf_refcount <= 0
6074 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
6075 && fp->uf_partial->pt_refcount <= 1
6076 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02006077 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006078 // Only delete it when it's not being used. Otherwise it's done
6079 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02006080 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006081 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006082 }
6083}
6084
6085/*
6086 * Count a reference to a Function.
6087 */
6088 void
6089func_ref(char_u *name)
6090{
6091 ufunc_T *fp;
6092
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006093 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006094 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006095 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006096 if (fp != NULL)
6097 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006098 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01006099 // Only give an error for a numbered function.
6100 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01006101 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006102}
6103
6104/*
6105 * Count a reference to a Function.
6106 */
6107 void
6108func_ptr_ref(ufunc_T *fp)
6109{
6110 if (fp != NULL)
6111 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006112}
6113
6114/*
6115 * Return TRUE if items in "fc" do not have "copyID". That means they are not
6116 * referenced from anywhere that is in use.
6117 */
6118 static int
6119can_free_funccal(funccall_T *fc, int copyID)
6120{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006121 return (fc->fc_l_varlist.lv_copyID != copyID
6122 && fc->fc_l_vars.dv_copyID != copyID
6123 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006124 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006125}
6126
6127/*
6128 * ":return [expr]"
6129 */
6130 void
6131ex_return(exarg_T *eap)
6132{
6133 char_u *arg = eap->arg;
6134 typval_T rettv;
6135 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006136 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006137
6138 if (current_funccal == NULL)
6139 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006140 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006141 return;
6142 }
6143
Bram Moolenaar844fb642021-10-23 13:32:30 +01006144 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006145 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
6146
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006147 if (eap->skip)
6148 ++emsg_skip;
6149
6150 eap->nextcmd = NULL;
6151 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006152 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006153 {
6154 if (!eap->skip)
6155 returning = do_return(eap, FALSE, TRUE, &rettv);
6156 else
6157 clear_tv(&rettv);
6158 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01006159 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006160 else if (!eap->skip)
6161 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006162 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006163 update_force_abort();
6164
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006165 /*
6166 * Return unless the expression evaluation has been cancelled due to an
6167 * aborting error, an interrupt, or an exception.
6168 */
6169 if (!aborting())
6170 returning = do_return(eap, FALSE, TRUE, NULL);
6171 }
6172
Bram Moolenaare38eab22019-12-05 21:50:01 +01006173 // When skipping or the return gets pending, advance to the next command
6174 // in this line (!returning). Otherwise, ignore the rest of the line.
6175 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006176 if (returning)
6177 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006178 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006179 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006180
6181 if (eap->skip)
6182 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006183 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006184}
6185
zeertzjq5299c092023-04-12 20:48:16 +01006186/*
6187 * Lower level implementation of "call". Only called when not skipping.
6188 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006189 static int
6190ex_call_inner(
6191 exarg_T *eap,
6192 char_u *name,
6193 char_u **arg,
6194 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006195 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006196 evalarg_T *evalarg)
6197{
6198 linenr_T lnum;
6199 int doesrange;
6200 typval_T rettv;
6201 int failed = FALSE;
6202
zeertzjq5299c092023-04-12 20:48:16 +01006203 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006204 for ( ; lnum <= eap->line2; ++lnum)
6205 {
6206 funcexe_T funcexe;
6207
zeertzjq5299c092023-04-12 20:48:16 +01006208 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006209 {
6210 if (lnum > curbuf->b_ml.ml_line_count)
6211 {
6212 // If the function deleted lines or switched to another buffer
6213 // the line number may become invalid.
6214 emsg(_(e_invalid_range));
6215 break;
6216 }
6217 curwin->w_cursor.lnum = lnum;
6218 curwin->w_cursor.col = 0;
6219 curwin->w_cursor.coladd = 0;
6220 }
6221 *arg = startarg;
6222
6223 funcexe = *funcexe_init;
6224 funcexe.fe_doesrange = &doesrange;
6225 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6226 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6227 {
6228 failed = TRUE;
6229 break;
6230 }
6231 if (has_watchexpr())
6232 dbg_check_breakpoint(eap);
6233
6234 // Handle a function returning a Funcref, Dictionary or List.
6235 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006236 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006237 {
6238 failed = TRUE;
6239 break;
6240 }
6241
6242 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006243 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006244 break;
6245
6246 // Stop when immediately aborting on error, or when an interrupt
6247 // occurred or an exception was thrown but not caught.
6248 // get_func_tv() returned OK, so that the check for trailing
6249 // characters below is executed.
6250 if (aborting())
6251 break;
6252 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006253 return failed;
6254}
6255
6256/*
6257 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6258 * Returns FAIL or OK.
6259 */
6260 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006261ex_defer_inner(
6262 char_u *name,
6263 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006264 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006265 partial_T *partial,
6266 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006267{
6268 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006269 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006270 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006271 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006272
6273 if (current_funccal == NULL)
6274 {
6275 semsg(_(e_str_not_inside_function), "defer");
6276 return FAIL;
6277 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006278 if (partial != NULL)
6279 {
6280 if (partial->pt_dict != NULL)
6281 {
6282 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6283 return FAIL;
6284 }
6285 if (partial->pt_argc > 0)
6286 {
6287 int i;
6288
6289 partial_argc = partial->pt_argc;
6290 for (i = 0; i < partial_argc; ++i)
6291 copy_tv(&partial->pt_argv[i], &argvars[i]);
6292 }
6293 }
Ernie Raelb077b582023-12-14 20:11:44 +01006294 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006295 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006296 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006297 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006298
6299 if (r == OK)
6300 {
6301 if (type != NULL)
6302 {
6303 // Check that the arguments are OK for the types of the funcref.
6304 r = check_argument_types(type, argvars, argcount, NULL, name);
6305 }
Ernie Raelb077b582023-12-14 20:11:44 +01006306 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006307 {
6308 int idx = find_internal_func(name);
6309
6310 if (idx < 0)
6311 {
6312 emsg_funcname(e_unknown_function_str, name);
6313 r = FAIL;
6314 }
6315 else if (check_internal_func(idx, argcount) == -1)
6316 r = FAIL;
6317 }
6318 else
6319 {
6320 ufunc_T *ufunc = find_func(name, FALSE);
6321
6322 // we tolerate an unknown function here, it might be defined later
6323 if (ufunc != NULL)
6324 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006325 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006326 if (error != FCERR_UNKNOWN)
6327 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006328 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006329 r = FAIL;
6330 }
6331 }
6332 }
6333 }
6334
Bram Moolenaar86d87252022-09-05 21:21:25 +01006335 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006336 {
6337 while (--argcount >= 0)
6338 clear_tv(&argvars[argcount]);
6339 return FAIL;
6340 }
6341 return add_defer(name, argcount, argvars);
6342}
6343
6344/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006345 * Return TRUE if currently inside a function call.
6346 * Give an error message and return FALSE when not.
6347 */
6348 int
6349can_add_defer(void)
6350{
6351 if (!in_def_function() && get_current_funccal() == NULL)
6352 {
6353 semsg(_(e_str_not_inside_function), "defer");
6354 return FALSE;
6355 }
6356 return TRUE;
6357}
6358
6359/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006360 * Add a deferred call for "name" with arguments "argvars[argcount]".
6361 * Consumes "argvars[]".
6362 * Caller must check that in_def_function() returns TRUE or current_funccal is
6363 * not NULL.
6364 * Returns OK or FAIL.
6365 */
6366 int
6367add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6368{
6369 char_u *saved_name = vim_strsave(name);
6370 int argcount = argcount_arg;
6371 defer_T *dr;
6372 int ret = FAIL;
6373
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006374 if (saved_name == NULL)
6375 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006376 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006377 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006378 if (add_defer_function(saved_name, argcount, argvars) == OK)
6379 argcount = 0;
6380 }
6381 else
6382 {
6383 if (current_funccal->fc_defer.ga_itemsize == 0)
6384 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6385 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6386 goto theend;
6387 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6388 + current_funccal->fc_defer.ga_len++;
6389 dr->dr_name = saved_name;
6390 dr->dr_argcount = argcount;
6391 while (argcount > 0)
6392 {
6393 --argcount;
6394 dr->dr_argvars[argcount] = argvars[argcount];
6395 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006396 }
6397 ret = OK;
6398
6399theend:
6400 while (--argcount >= 0)
6401 clear_tv(&argvars[argcount]);
6402 return ret;
6403}
6404
6405/*
6406 * Invoked after a functions has finished: invoke ":defer" functions.
6407 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006408 static void
6409handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006410{
6411 int idx;
6412
Bram Moolenaar58779852022-09-06 18:31:14 +01006413 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006414 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006415 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006416
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006417 if (dr->dr_name == NULL)
6418 // already being called, can happen if function does ":qa"
6419 continue;
6420
6421 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006422 CLEAR_FIELD(funcexe);
6423 funcexe.fe_evaluate = TRUE;
6424
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006425 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006426 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006427
6428 char_u *name = dr->dr_name;
6429 dr->dr_name = NULL;
6430
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006431 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006432 // first statement in the function will be executed (because of the
6433 // exception). So save and restore the try/catch/throw exception
6434 // state.
6435 exception_state_T estate;
6436 exception_state_save(&estate);
6437 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006438
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006439 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6440
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006441 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006442
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006443 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006444 vim_free(name);
6445 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006446 clear_tv(&dr->dr_argvars[i]);
6447 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006448 ga_clear(&funccal->fc_defer);
6449}
6450
zeertzjq960cf912023-04-18 21:52:54 +01006451 static void
6452invoke_funccall_defer(funccall_T *fc)
6453{
6454 if (fc->fc_ectx != NULL)
6455 {
6456 // :def function
6457 unwind_def_callstack(fc->fc_ectx);
6458 may_invoke_defer_funcs(fc->fc_ectx);
6459 }
6460 else
6461 {
6462 // legacy function
6463 handle_defer_one(fc);
6464 }
6465}
6466
Bram Moolenaar58779852022-09-06 18:31:14 +01006467/*
6468 * Called when exiting: call all defer functions.
6469 */
6470 void
6471invoke_all_defer(void)
6472{
zeertzjq1be4b812023-04-19 14:21:24 +01006473 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6474 invoke_funccall_defer(fc);
6475
zeertzjq960cf912023-04-18 21:52:54 +01006476 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6477 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6478 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006479}
6480
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006481/*
6482 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006483 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006484 */
6485 void
6486ex_call(exarg_T *eap)
6487{
6488 char_u *arg = eap->arg;
6489 char_u *startarg;
6490 char_u *name;
6491 char_u *tofree;
6492 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006493 int failed = FALSE;
6494 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006495 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006496 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006497 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006498 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006499 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006500 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006501
Bram Moolenaare6b53242020-07-01 17:28:33 +02006502 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006503 if (eap->skip)
6504 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006505 typval_T rettv;
6506
Bram Moolenaare38eab22019-12-05 21:50:01 +01006507 // trans_function_name() doesn't work well when skipping, use eval0()
6508 // instead to skip to any following command, e.g. for:
6509 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006510 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006511 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006512 clear_tv(&rettv);
6513 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006514 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006515 return;
6516 }
6517
zeertzjq5299c092023-04-12 20:48:16 +01006518 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006519 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006520 if (fudi.fd_newkey != NULL)
6521 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006522 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006523 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006524 vim_free(fudi.fd_newkey);
6525 }
6526 if (tofree == NULL)
6527 return;
6528
Bram Moolenaare38eab22019-12-05 21:50:01 +01006529 // Increase refcount on dictionary, it could get deleted when evaluating
6530 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006531 if (fudi.fd_dict != NULL)
6532 ++fudi.fd_dict->dv_refcount;
6533
Bram Moolenaare38eab22019-12-05 21:50:01 +01006534 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6535 // contents. For VAR_PARTIAL get its partial, unless we already have one
6536 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006537 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006538 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006539 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006540 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006541
Bram Moolenaare38eab22019-12-05 21:50:01 +01006542 // Skip white space to allow ":call func ()". Not good, but required for
6543 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006544 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006545 if (*startarg != '(')
6546 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006547 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006548 goto end;
6549 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006550 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006551 {
6552 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6553 goto end;
6554 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006555
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006556 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006557 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006558 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006559 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006560 }
6561 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006562 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006563 funcexe_T funcexe;
6564
Bram Moolenaara80faa82020-04-12 19:37:17 +02006565 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006566 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006567 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006568 funcexe.fe_partial = partial;
6569 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006570 funcexe.fe_firstline = eap->line1;
6571 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006572 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006573 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006574 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006575 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006576
Bram Moolenaar1d341892021-09-18 15:25:52 +02006577 // When inside :try we need to check for following "| catch" or "| endtry".
6578 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006579 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006580 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006581 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006582 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006583 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006584 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006585 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006586 {
6587 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006588 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006589 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006590 }
6591 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006592 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006593 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006594 // Must be after using "arg", it may point into memory cleared here.
6595 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006596
6597end:
6598 dict_unref(fudi.fd_dict);
6599 vim_free(tofree);
6600}
6601
6602/*
6603 * Return from a function. Possibly makes the return pending. Also called
6604 * for a pending return at the ":endtry" or after returning from an extra
6605 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6606 * when called due to a ":return" command. "rettv" may point to a typval_T
6607 * with the return rettv. Returns TRUE when the return can be carried out,
6608 * FALSE when the return gets pending.
6609 */
6610 int
6611do_return(
6612 exarg_T *eap,
6613 int reanimate,
6614 int is_cmd,
6615 void *rettv)
6616{
6617 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006618 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006619
6620 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006621 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006622 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006623
6624 /*
6625 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6626 * not in its finally clause (which then is to be executed next) is found.
6627 * In this case, make the ":return" pending for execution at the ":endtry".
6628 * Otherwise, return normally.
6629 */
6630 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6631 if (idx >= 0)
6632 {
6633 cstack->cs_pending[idx] = CSTP_RETURN;
6634
6635 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006636 // A pending return again gets pending. "rettv" points to an
6637 // allocated variable with the rettv of the original ":return"'s
6638 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006639 cstack->cs_rettv[idx] = rettv;
6640 else
6641 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006642 // When undoing a return in order to make it pending, get the stored
6643 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006644 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006645 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006646
6647 if (rettv != NULL)
6648 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006649 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006650 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6651 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6652 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006653 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006654 }
6655 else
6656 cstack->cs_rettv[idx] = NULL;
6657
6658 if (reanimate)
6659 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006660 // The pending return value could be overwritten by a ":return"
6661 // without argument in a finally clause; reset the default
6662 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006663 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6664 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006665 }
6666 }
6667 report_make_pending(CSTP_RETURN, rettv);
6668 }
6669 else
6670 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006671 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006672
Bram Moolenaare38eab22019-12-05 21:50:01 +01006673 // If the return is carried out now, store the return value. For
6674 // a return immediately after reanimation, the value is already
6675 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006676 if (!reanimate && rettv != NULL)
6677 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006678 clear_tv(current_funccal->fc_rettv);
6679 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006680 if (!is_cmd)
6681 vim_free(rettv);
6682 }
6683 }
6684
6685 return idx < 0;
6686}
6687
6688/*
6689 * Free the variable with a pending return value.
6690 */
6691 void
6692discard_pending_return(void *rettv)
6693{
6694 free_tv((typval_T *)rettv);
6695}
6696
6697/*
6698 * Generate a return command for producing the value of "rettv". The result
6699 * is an allocated string. Used by report_pending() for verbose messages.
6700 */
6701 char_u *
6702get_return_cmd(void *rettv)
6703{
6704 char_u *s = NULL;
6705 char_u *tofree = NULL;
6706 char_u numbuf[NUMBUFLEN];
6707
6708 if (rettv != NULL)
6709 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6710 if (s == NULL)
6711 s = (char_u *)"";
6712
6713 STRCPY(IObuff, ":return ");
6714 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6715 if (STRLEN(s) + 8 >= IOSIZE)
6716 STRCPY(IObuff + IOSIZE - 4, "...");
6717 vim_free(tofree);
6718 return vim_strsave(IObuff);
6719}
6720
6721/*
6722 * Get next function line.
6723 * Called by do_cmdline() to get the next line.
6724 * Returns allocated string, or NULL for end of function.
6725 */
6726 char_u *
6727get_func_line(
6728 int c UNUSED,
6729 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006730 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006731 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006732{
6733 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006734 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006735 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006736 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006737
Bram Moolenaare38eab22019-12-05 21:50:01 +01006738 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006739 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006740 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006741 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006742 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006743 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006744 }
6745#ifdef FEAT_PROFILE
6746 if (do_profiling == PROF_YES)
6747 func_line_end(cookie);
6748#endif
6749
6750 gap = &fp->uf_lines;
6751 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006752 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006753 retval = NULL;
6754 else
6755 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006756 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006757 while (fcp->fc_linenr < gap->ga_len
6758 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6759 ++fcp->fc_linenr;
6760 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006761 retval = NULL;
6762 else
6763 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006764 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6765 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006766#ifdef FEAT_PROFILE
6767 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006768 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006769#endif
6770 }
6771 }
6772
Bram Moolenaare38eab22019-12-05 21:50:01 +01006773 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006774 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006775 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006776 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006777 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006778 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006779 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006780 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006781 }
6782
6783 return retval;
6784}
6785
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006786/*
6787 * Return TRUE if the currently active function should be ended, because a
6788 * return was encountered or an error occurred. Used inside a ":while".
6789 */
6790 int
6791func_has_ended(void *cookie)
6792{
6793 funccall_T *fcp = (funccall_T *)cookie;
6794
Bram Moolenaare38eab22019-12-05 21:50:01 +01006795 // Ignore the "abort" flag if the abortion behavior has been changed due to
6796 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006797 return (((fcp->fc_func->uf_flags & FC_ABORT)
6798 && did_emsg && !aborted_in_try())
6799 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006800}
6801
6802/*
6803 * return TRUE if cookie indicates a function which "abort"s on errors.
6804 */
6805 int
6806func_has_abort(
6807 void *cookie)
6808{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006809 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006810}
6811
6812
6813/*
6814 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6815 * Don't do this when "Func" is already a partial that was bound
6816 * explicitly (pt_auto is FALSE).
6817 * Changes "rettv" in-place.
6818 * Returns the updated "selfdict_in".
6819 */
6820 dict_T *
6821make_partial(dict_T *selfdict_in, typval_T *rettv)
6822{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006823 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006824 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006825 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006826 dict_T *selfdict = selfdict_in;
6827
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006828 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6829 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006830 fp = rettv->vval.v_partial->pt_func;
6831 else
6832 {
6833 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006834 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006835 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006836 if (fname == NULL)
6837 {
6838 // There is no point binding a dict to a NULL function, just create
6839 // a function reference.
6840 rettv->v_type = VAR_FUNC;
6841 rettv->vval.v_string = NULL;
6842 }
6843 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006844 {
6845 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006846 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006847
6848 // Translate "s:func" to the stored function name.
6849 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6850 fp = find_func(fname, FALSE);
6851 vim_free(tofree);
6852 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006853 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006854
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006855 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006856 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006857 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006858
6859 if (pt != NULL)
6860 {
6861 pt->pt_refcount = 1;
6862 pt->pt_dict = selfdict;
6863 pt->pt_auto = TRUE;
6864 selfdict = NULL;
6865 if (rettv->v_type == VAR_FUNC)
6866 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006867 // Just a function: Take over the function name and use
6868 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006869 pt->pt_name = rettv->vval.v_string;
6870 }
6871 else
6872 {
6873 partial_T *ret_pt = rettv->vval.v_partial;
6874 int i;
6875
Bram Moolenaare38eab22019-12-05 21:50:01 +01006876 // Partial: copy the function name, use selfdict and copy
6877 // args. Can't take over name or args, the partial might
6878 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006879 if (ret_pt->pt_name != NULL)
6880 {
6881 pt->pt_name = vim_strsave(ret_pt->pt_name);
6882 func_ref(pt->pt_name);
6883 }
6884 else
6885 {
6886 pt->pt_func = ret_pt->pt_func;
6887 func_ptr_ref(pt->pt_func);
6888 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006889 if (ret_pt->pt_argc > 0)
6890 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006891 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006892 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006893 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006894 pt->pt_argc = 0;
6895 else
6896 {
6897 pt->pt_argc = ret_pt->pt_argc;
6898 for (i = 0; i < pt->pt_argc; i++)
6899 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6900 }
6901 }
6902 partial_unref(ret_pt);
6903 }
6904 rettv->v_type = VAR_PARTIAL;
6905 rettv->vval.v_partial = pt;
6906 }
6907 }
6908 return selfdict;
6909}
6910
6911/*
6912 * Return the name of the executed function.
6913 */
6914 char_u *
6915func_name(void *cookie)
6916{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006917 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006918}
6919
6920/*
6921 * Return the address holding the next breakpoint line for a funccall cookie.
6922 */
6923 linenr_T *
6924func_breakpoint(void *cookie)
6925{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006926 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006927}
6928
6929/*
6930 * Return the address holding the debug tick for a funccall cookie.
6931 */
6932 int *
6933func_dbg_tick(void *cookie)
6934{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006935 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006936}
6937
6938/*
6939 * Return the nesting level for a funccall cookie.
6940 */
6941 int
6942func_level(void *cookie)
6943{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006944 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006945}
6946
6947/*
6948 * Return TRUE when a function was ended by a ":return" command.
6949 */
6950 int
6951current_func_returned(void)
6952{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006953 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006954}
6955
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006956 int
6957free_unref_funccal(int copyID, int testing)
6958{
6959 int did_free = FALSE;
6960 int did_free_funccal = FALSE;
6961 funccall_T *fc, **pfc;
6962
6963 for (pfc = &previous_funccal; *pfc != NULL; )
6964 {
6965 if (can_free_funccal(*pfc, copyID))
6966 {
6967 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006968 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006969 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006970 did_free = TRUE;
6971 did_free_funccal = TRUE;
6972 }
6973 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006974 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006975 }
6976 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006977 // When a funccal was freed some more items might be garbage
6978 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006979 (void)garbage_collect(testing);
6980
6981 return did_free;
6982}
6983
6984/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006985 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006986 */
6987 static funccall_T *
6988get_funccal(void)
6989{
6990 int i;
6991 funccall_T *funccal;
6992 funccall_T *temp_funccal;
6993
6994 funccal = current_funccal;
6995 if (debug_backtrace_level > 0)
6996 {
6997 for (i = 0; i < debug_backtrace_level; i++)
6998 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006999 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007000 if (temp_funccal)
7001 funccal = temp_funccal;
7002 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01007003 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007004 debug_backtrace_level = i;
7005 }
7006 }
7007 return funccal;
7008}
7009
7010/*
7011 * Return the hashtable used for local variables in the current funccal.
7012 * Return NULL if there is no current funccal.
7013 */
7014 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007015get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007016{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007017 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007018 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007019 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007020}
7021
7022/*
7023 * Return the l: scope variable.
7024 * Return NULL if there is no current funccal.
7025 */
7026 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007027get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007028{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007029 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007030 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007031 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007032}
7033
7034/*
7035 * Return the hashtable used for argument in the current funccal.
7036 * Return NULL if there is no current funccal.
7037 */
7038 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007039get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007040{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007041 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007042 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007043 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007044}
7045
7046/*
7047 * Return the a: scope variable.
7048 * Return NULL if there is no current funccal.
7049 */
7050 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007051get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007052{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007053 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007054 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007055 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007056}
7057
7058/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007059 * List function variables, if there is a function.
7060 */
7061 void
7062list_func_vars(int *first)
7063{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007064 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
7065 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01007066 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007067}
7068
7069/*
7070 * If "ht" is the hashtable for local variables in the current funccal, return
7071 * the dict that contains it.
7072 * Otherwise return NULL.
7073 */
7074 dict_T *
7075get_current_funccal_dict(hashtab_T *ht)
7076{
7077 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01007078 && ht == &current_funccal->fc_l_vars.dv_hashtab)
7079 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007080 return NULL;
7081}
7082
7083/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007084 * Search hashitem in parent scope.
7085 */
7086 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007087find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007088{
7089 funccall_T *old_current_funccal = current_funccal;
7090 hashtab_T *ht;
7091 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007092 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007093
Bram Moolenaarca16c602022-09-06 18:57:08 +01007094 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007095 return NULL;
7096
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02007097 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01007098 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02007099 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007100 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007101 ht = find_var_ht(name, &varname);
7102 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02007103 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007104 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02007105 if (!HASHITEM_EMPTY(hi))
7106 {
7107 *pht = ht;
7108 break;
7109 }
7110 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007111 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02007112 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007113 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007114 }
7115 current_funccal = old_current_funccal;
7116
7117 return hi;
7118}
7119
7120/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007121 * Search variable in parent scope.
7122 */
7123 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007124find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007125{
7126 dictitem_T *v = NULL;
7127 funccall_T *old_current_funccal = current_funccal;
7128 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007129 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007130
Bram Moolenaarca16c602022-09-06 18:57:08 +01007131 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007132 return NULL;
7133
Bram Moolenaare38eab22019-12-05 21:50:01 +01007134 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01007135 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007136 while (current_funccal)
7137 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007138 ht = find_var_ht(name, &varname);
7139 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007140 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007141 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007142 if (v != NULL)
7143 break;
7144 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007145 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007146 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007147 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007148 }
7149 current_funccal = old_current_funccal;
7150
7151 return v;
7152}
7153
7154/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007155 * Set "copyID + 1" in previous_funccal and callers.
7156 */
7157 int
7158set_ref_in_previous_funccal(int copyID)
7159{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007160 funccall_T *fc;
7161
Bram Moolenaarca16c602022-09-06 18:57:08 +01007162 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007163 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007164 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007165 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
7166 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
7167 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007168 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007169 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007170 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007171}
7172
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007173 static int
7174set_ref_in_funccal(funccall_T *fc, int copyID)
7175{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007176 if (fc->fc_copyID != copyID)
7177 {
7178 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007179 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7180 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7181 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7182 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007183 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007184 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007185 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007186}
7187
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007188/*
7189 * Set "copyID" in all local vars and arguments in the call stack.
7190 */
7191 int
7192set_ref_in_call_stack(int copyID)
7193{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007194 funccall_T *fc;
7195 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007196
Bram Moolenaarca16c602022-09-06 18:57:08 +01007197 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007198 if (set_ref_in_funccal(fc, copyID))
7199 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007200
7201 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007202 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007203 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007204 if (set_ref_in_funccal(fc, copyID))
7205 return TRUE;
7206 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007207}
7208
7209/*
7210 * Set "copyID" in all functions available by name.
7211 */
7212 int
7213set_ref_in_functions(int copyID)
7214{
7215 int todo;
7216 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007217 ufunc_T *fp;
7218
7219 todo = (int)func_hashtab.ht_used;
7220 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007221 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007222 if (!HASHITEM_EMPTY(hi))
7223 {
7224 --todo;
7225 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007226 if (!func_name_refcount(fp->uf_name)
7227 && set_ref_in_func(NULL, fp, copyID))
7228 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007229 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007230 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007231 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007232}
7233
7234/*
7235 * Set "copyID" in all function arguments.
7236 */
7237 int
7238set_ref_in_func_args(int copyID)
7239{
7240 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007241
7242 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007243 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7244 copyID, NULL, NULL))
7245 return TRUE;
7246 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007247}
7248
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007249/*
7250 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007251 * Returns TRUE if setting references failed somehow.
7252 */
7253 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007254set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007255{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007256 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007257 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007258 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007259 char_u fname_buf[FLEN_FIXED + 1];
7260 char_u *tofree = NULL;
7261 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007262 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007263
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007264 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007265 return FALSE;
7266
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007267 if (fp_in == NULL)
7268 {
7269 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007270 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007271 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007272 if (fp != NULL)
7273 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007274 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007275 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007276 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007277
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007278 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007279 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007280}
7281
Bram Moolenaare38eab22019-12-05 21:50:01 +01007282#endif // FEAT_EVAL