blob: 91c971e8fa853665aa846d63d0f80b3985215cdc [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);
Yegappan Lakshmanana04003a2024-10-27 21:54:11 +010037static char_u *trans_function_name_ext(char_u **pp, int *is_global, int skip, int flags, funcdict_T *fdp, partial_T **partial, type_T **type, ufunc_T **ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020038
39 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +000040func_init(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020041{
42 hash_init(&func_hashtab);
43}
44
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020045/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020046 * Return the function hash table
47 */
48 hashtab_T *
49func_tbl_get(void)
50{
51 return &func_hashtab;
52}
53
54/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020055 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020056 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010057 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010058 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000059 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060 * Return a pointer to after the type.
61 * When something is wrong return "arg".
62 */
63 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010064one_function_arg(
65 char_u *arg,
66 garray_T *newargs,
67 garray_T *argtypes,
68 int types_optional,
h-eastb895b0f2023-09-24 15:46:31 +020069 garray_T *arg_objm,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010070 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000071 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020072 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010073 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074{
Bram Moolenaar6e949782020-04-13 17:21:00 +020075 char_u *p = arg;
76 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020077 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010078
79 while (ASCII_ISALNUM(*p) || *p == '_')
80 ++p;
Keith Thompson184f71c2024-01-04 21:19:04 +010081 if (arg == p || SAFE_isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020082 || (argtypes == NULL
83 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
84 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 {
86 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000087 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 return arg;
89 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010090
Bram Moolenaarce723f32023-06-10 19:00:12 +010091 // Extra checks in Vim9 script.
92 if (!skip && argtypes != NULL)
93 {
94 int c = *p;
95 *p = NUL;
96 int r = check_reserved_name(arg, FALSE);
97 *p = c;
98 if (r == FAIL)
99 return arg;
100
101 // Cannot use script var name for argument. In function: also check
102 // local vars and arguments.
103 if (check_defined(arg, p - arg,
104 evalarg == NULL ? NULL : evalarg->eval_cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +0000105 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarce723f32023-06-10 19:00:12 +0100106 return arg;
107 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100109 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
110 return arg;
111 if (newargs != NULL)
112 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113 int c;
114 int i;
115
116 c = *p;
117 *p = NUL;
118 arg_copy = vim_strsave(arg);
119 if (arg_copy == NULL)
120 {
121 *p = c;
122 return arg;
123 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200124 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200125 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200126 // Check for duplicate argument name.
127 for (i = 0; i < newargs->ga_len; ++i)
128 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
129 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000130 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200131 vim_free(arg_copy);
132 return arg;
133 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100134 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
135 newargs->ga_len++;
136
137 *p = c;
138 }
139
140 // get any type from "arg: type"
h-eastb895b0f2023-09-24 15:46:31 +0200141 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK)
142 && arg_objm != NULL && (skip || ga_grow(arg_objm, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 {
144 char_u *type = NULL;
145
Bram Moolenaar6e949782020-04-13 17:21:00 +0200146 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
147 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200148 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200149 arg_copy == NULL ? arg : arg_copy);
150 p = skipwhite(p);
151 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152 if (*p == ':')
153 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200154 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100155 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200156 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100157 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200158 return arg;
159 }
160 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200161 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100162 if (!skip)
163 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200165 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200166 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200167 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200168 arg_copy == NULL ? arg : arg_copy);
169 return arg;
170 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100171 if (!skip)
172 {
173 if (type == NULL && types_optional)
174 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200175 type = vim_strsave((char_u *)
176 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100177 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
h-eastb895b0f2023-09-24 15:46:31 +0200178 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = FALSE;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100179 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100180 }
181
182 return p;
183}
184
185/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000186 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000187 * Get a next line, store it in "eap" if appropriate and put the line in
188 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000189 */
190 static char_u *
191get_function_line(
192 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000193 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000194 int indent,
195 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000196{
197 char_u *theline;
198
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100199 if (eap->ea_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000200 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000201 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100202 theline = eap->ea_getline(':', eap->cookie, indent, getline_options);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000203 if (theline != NULL)
204 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000205 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000206 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000207 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
208 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000209 *eap->cmdlinep = theline;
Bram Moolenaarb5820102023-01-25 12:27:13 +0000210 (void)ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000211 }
212
213 return theline;
214}
215
216/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200217 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100218 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100219 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200220 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100221 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200222get_function_args(
223 char_u **argp,
224 char_u endchar,
225 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100227 int types_optional, // types optional if "argtypes" is not NULL
h-eastb895b0f2023-09-24 15:46:31 +0200228 garray_T *arg_objm, // NULL unless using :def
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100229 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200230 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200231 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200232 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000233 exarg_T *eap, // can be NULL
Bram Moolenaar554d0312023-01-05 19:59:18 +0000234 int in_class, // non-zero when inside a class or interface
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000235 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000236 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200237{
238 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100239 char_u *arg;
240 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200241 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200242 int any_default = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100243 char_u *whitep = *argp;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100244 int need_expr = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200245
246 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000247 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000249 ga_init2(argtypes, sizeof(char_u *), 3);
h-eastb895b0f2023-09-24 15:46:31 +0200250 if (arg_objm != NULL)
251 ga_init2(arg_objm, sizeof(int8_T), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200252 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000253 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200254
255 if (varargs != NULL)
256 *varargs = FALSE;
257
258 /*
259 * Isolate the arguments: "arg1, arg2, ...)"
260 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100261 arg = skipwhite(*argp);
262 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200263 while (*p != endchar)
264 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100265 while (eap != NULL && eap->ea_getline != NULL
Bram Moolenaar2c330432020-04-13 14:41:35 +0200266 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200267 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200268 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000269 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000270 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000271
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200272 if (theline == NULL)
273 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200274 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200275 p = skipwhite(theline);
276 }
277
278 if (mustend && *p != endchar)
279 {
280 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000281 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100282 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200283 }
Christian Brabandte4a450a2023-12-08 20:57:38 +0100284 if (*p == endchar && !need_expr)
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200285 break;
286
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200287 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
288 {
289 if (varargs != NULL)
290 *varargs = TRUE;
291 p += 3;
292 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100293
294 if (argtypes != NULL)
295 {
296 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200297 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100299 if (!skip)
300 emsg(_(e_missing_name_after_dots));
301 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100302 }
303
304 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100305 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200306 arg_objm, evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100307 if (p == arg)
308 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100309 if (*skipwhite(p) == '=')
310 {
311 emsg(_(e_cannot_use_default_for_variable_arguments));
312 break;
313 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100314 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200315 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000316 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000317 {
318 // this.memberName
319 p += 5;
320 arg = p;
321 while (ASCII_ISALNUM(*p) || *p == '_')
322 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000323 char_u *argend = p;
324
h-eastdb385522023-09-28 22:18:19 +0200325 // object variable this. can be used only in a constructor
326 if (STRNCMP(eap->arg, "new", 3) != 0)
327 {
328 c = *argend;
329 *argend = NUL;
330 semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
331 *argend = c;
332 break;
333 }
334
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000335 if (*skipwhite(p) == '=')
336 {
337 char_u *defval = skipwhite(skipwhite(p) + 1);
338 if (STRNCMP(defval, "v:none", 6) != 0)
339 {
340 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
341 goto err_ret;
342 }
343 any_default = TRUE;
344 p = defval + 6;
345
346 if (ga_grow(default_args, 1) == FAIL)
347 goto err_ret;
348
349 char_u *expr = vim_strsave((char_u *)"v:none");
350 if (expr == NULL)
351 goto err_ret;
352 ((char_u **)(default_args->ga_data))
353 [default_args->ga_len] = expr;
354 default_args->ga_len++;
355 }
356 else if (any_default)
357 {
358 emsg(_(e_non_default_argument_follows_default_argument));
359 goto err_ret;
360 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000361
362 // TODO: check the argument is indeed a member
363 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
364 return FAIL;
365 if (newargs != NULL)
366 {
367 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000368 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000369 newargs->ga_len++;
370
h-eastb895b0f2023-09-24 15:46:31 +0200371 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
372 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000373 {
374 // TODO: use the actual type
375 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
376 vim_strsave((char_u *)"any");
h-eastb895b0f2023-09-24 15:46:31 +0200377 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000378
379 // Add a line to the function body for the assignment.
380 if (ga_grow(newlines, 1) == OK)
381 {
382 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000383 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
384 if (any_default)
385 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000386 char_u *assignment = alloc(len);
387 if (assignment != NULL)
388 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000389 c = *argend;
390 *argend = NUL;
391 if (any_default)
392 vim_snprintf((char *)assignment, len,
393 "ifargisset %d this.%s = %s",
394 default_args->ga_len - 1, arg, arg);
395 else
396 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000397 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000398 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000399 ((char_u **)(newlines->ga_data))[
400 newlines->ga_len++] = assignment;
401 }
402 }
403 }
404 }
405 if (*p == ',')
406 ++p;
407 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200408 else
409 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200410 char_u *np;
411
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200412 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100413 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200414 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100415 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200416 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200417
Bram Moolenaar015cf102021-06-26 21:52:02 +0200418 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200419 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200420 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200421 if (*np == '=' && np[1] != '=' && np[1] != '~'
422 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200423 {
424 typval_T rettv;
425
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100426 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200427 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100428 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000429 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200430 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200431 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200432 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200433 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200434 if (ga_grow(default_args, 1) == FAIL)
435 goto err_ret;
436
Christian Brabandte4a450a2023-12-08 20:57:38 +0100437 if (need_expr)
438 need_expr = FALSE;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200439 // trim trailing whitespace
440 while (p > expr && VIM_ISWHITE(p[-1]))
441 p--;
442 c = *p;
443 *p = NUL;
444 expr = vim_strsave(expr);
445 if (expr == NULL)
446 {
447 *p = c;
448 goto err_ret;
449 }
450 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200451 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200452 default_args->ga_len++;
453 *p = c;
454 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200455 }
456 else
Christian Brabandte4a450a2023-12-08 20:57:38 +0100457 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200458 mustend = TRUE;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100459 if (*skipwhite(p) == NUL)
460 need_expr = TRUE;
461 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200462 }
463 else if (any_default)
464 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000465 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200466 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200467 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200468
469 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
470 {
471 // Be tolerant when skipping
472 if (!skip)
473 {
474 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
475 goto err_ret;
476 }
477 p = skipwhite(p);
478 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200479 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200480 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200481 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200482 // Don't give this error when skipping, it makes the "->" not
483 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100484 // Allow missing space after comma in legacy functions.
485 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200486 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
487 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100488 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200489 goto err_ret;
490 }
491 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200492 else
493 mustend = TRUE;
494 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200495 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200496 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200497 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200498
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200499 if (*p != endchar)
500 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100501 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200502
503 *argp = p;
504 return OK;
505
506err_ret:
507 if (newargs != NULL)
508 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200509 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200510 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200511 return FAIL;
512}
513
514/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100515 * Parse the argument types, filling "fp->uf_arg_types".
516 * Return OK or FAIL.
517 */
518 static int
h-eastb895b0f2023-09-24 15:46:31 +0200519parse_argument_types(
520 ufunc_T *fp,
521 garray_T *argtypes,
522 int varargs,
523 garray_T *arg_objm,
524 ocmember_T *obj_members,
525 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100526{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200527 int len = 0;
528
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100529 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
530 if (argtypes->ga_len > 0)
531 {
532 // When "varargs" is set the last name/type goes into uf_va_name
533 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200534 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100535
536 if (len > 0)
537 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
538 if (fp->uf_arg_types != NULL)
539 {
540 int i;
541 type_T *type;
542
543 for (i = 0; i < len; ++ i)
544 {
545 char_u *p = ((char_u **)argtypes->ga_data)[i];
546
547 if (p == NULL)
548 // will get the type from the default value
549 type = &t_unknown;
550 else
h-eastb895b0f2023-09-24 15:46:31 +0200551 {
552 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
553 {
554 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
555
556 type = &t_any;
557 for (int om = 0; om < obj_member_count; ++om)
558 {
Christian Brabandt915f3bf2024-04-05 20:12:19 +0200559 if (obj_members != NULL
560 && STRCMP(aname,
561 obj_members[om].ocm_name) == 0)
h-eastb895b0f2023-09-24 15:46:31 +0200562 {
563 type = obj_members[om].ocm_type;
564 break;
565 }
566 }
567 }
568 else
569 type = parse_type(&p, &fp->uf_type_list, TRUE);
570 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100571 if (type == NULL)
572 return FAIL;
573 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000574 if (i < fp->uf_args.ga_len
575 && (type->tt_type == VAR_FUNC
Yegappan Lakshmanan3e336502024-04-04 19:35:59 +0200576 || type->tt_type == VAR_PARTIAL))
577 {
578 char_u *name = ((char_u **)fp->uf_args.ga_data)[i];
579 if (obj_members != NULL && *name == '_')
580 // protected object method
581 name++;
582
583 if (var_wrong_func_name(name, TRUE))
584 return FAIL;
585 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100586 }
587 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100588 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200589
590 if (varargs)
591 {
592 char_u *p;
593
594 // Move the last argument "...name: type" to uf_va_name and
595 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200596 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000597 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
598 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200599 p = ((char_u **)argtypes->ga_data)[len];
600 if (p == NULL)
601 // TODO: get type from default value
602 fp->uf_va_type = &t_list_any;
603 else
604 {
605 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
606 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
607 {
608 semsg(_(e_variable_arguments_type_must_be_list_str),
609 ((char_u **)argtypes->ga_data)[len]);
610 return FAIL;
611 }
612 }
613 if (fp->uf_va_type == NULL)
614 return FAIL;
615 }
616
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100617 return OK;
618}
619
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100620 static int
621parse_return_type(ufunc_T *fp, char_u *ret_type)
622{
623 if (ret_type == NULL)
624 fp->uf_ret_type = &t_void;
625 else
626 {
627 char_u *p = ret_type;
628
629 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
630 if (fp->uf_ret_type == NULL)
631 {
632 fp->uf_ret_type = &t_void;
633 return FAIL;
634 }
635 }
636 return OK;
637}
638
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100639/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200640 * Register function "fp" as using "current_funccal" as its scope.
641 */
642 static int
643register_closure(ufunc_T *fp)
644{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200645 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100646 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200647 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200648 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200649 fp->uf_scoped = current_funccal;
650 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200651
Bram Moolenaarca16c602022-09-06 18:57:08 +0100652 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200653 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100654 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
655 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200656 return OK;
657}
658
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100659 static void
660set_ufunc_name(ufunc_T *fp, char_u *name)
661{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100662 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
663 // actually extends beyond the struct.
664 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100665
666 if (name[0] == K_SPECIAL)
667 {
668 fp->uf_name_exp = alloc(STRLEN(name) + 3);
669 if (fp->uf_name_exp != NULL)
670 {
671 STRCPY(fp->uf_name_exp, "<SNR>");
672 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
673 }
674 }
675}
676
Bram Moolenaar58016442016-07-31 18:30:22 +0200677/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100678 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
679 * return "buf" filled with a readable function name.
680 * Otherwise just return "name", thus the return value can always be used.
681 * "name" and "buf" may be equal.
682 */
683 char_u *
684make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
685{
686 size_t len;
687
688 if (name[0] != K_SPECIAL)
689 return name;
690 len = STRLEN(name);
691 if (len + 3 > bufsize)
692 return name;
693
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100694 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100695 mch_memmove(buf, "<SNR>", 5);
696 return buf;
697}
698
699/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200700 * Get a name for a lambda. Returned in static memory.
701 */
702 char_u *
703get_lambda_name(void)
704{
705 static char_u name[30];
706 static int lambda_no = 0;
707
708 sprintf((char*)name, "<lambda>%d", ++lambda_no);
709 return name;
710}
711
Bram Moolenaare01e5212023-01-08 20:31:18 +0000712/*
713 * Allocate a "ufunc_T" for a function called "name".
714 * Makes sure the size is right.
715 */
716 static ufunc_T *
717alloc_ufunc(char_u *name)
718{
719 // When the name is short we need to make sure we allocate enough bytes for
720 // the whole struct, including any padding.
721 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
722 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
723}
724
Bram Moolenaar801ab062020-06-25 19:27:56 +0200725#if defined(FEAT_LUA) || defined(PROTO)
726/*
727 * Registers a native C callback which can be called from Vim script.
728 * Returns the name of the Vim script function.
729 */
730 char_u *
731register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
732{
733 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200734 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200735
Bram Moolenaare01e5212023-01-08 20:31:18 +0000736 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200737 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200738 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200739
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200740 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200741 fp->uf_refcount = 1;
742 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000743 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200744 fp->uf_calls = 0;
745 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200746 fp->uf_cb = cb;
747 fp->uf_cb_free = cb_free;
748 fp->uf_cb_state = state;
749
750 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000751 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200752
753 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200754}
755#endif
756
Bram Moolenaar04b12692020-05-04 23:24:44 +0200757/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100758 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100759 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100760 * If "white_error" is not NULL check for correct use of white space and set
761 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100762 * Return NULL if no valid arrow found.
763 */
764 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100765skip_arrow(
766 char_u *start,
767 int equal_arrow,
768 char_u **ret_type,
769 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100770{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100771 char_u *s = start;
772 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100773
774 if (equal_arrow)
775 {
776 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100777 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100778 if (white_error != NULL && !VIM_ISWHITE(s[1]))
779 {
780 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100781 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100782 return NULL;
783 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100784 s = skipwhite(s + 1);
785 *ret_type = s;
786 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100787 if (s == *ret_type)
788 {
789 emsg(_(e_missing_return_type));
790 return NULL;
791 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100792 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100793 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100794 s = skipwhite(s);
795 if (*s != '=')
796 return NULL;
797 ++s;
798 }
799 if (*s != '>')
800 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100801 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
802 || !IS_WHITE_OR_NUL(s[1])))
803 {
804 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100805 semsg(_(e_white_space_required_before_and_after_str_at_str),
806 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100807 return NULL;
808 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100809 return skipwhite(s + 1);
810}
811
812/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100813 * Check if "*cmd" points to a function command and if so advance "*cmd" and
814 * return TRUE.
815 * Otherwise return FALSE;
816 * Do not consider "function(" to be a command.
817 */
818 static int
819is_function_cmd(char_u **cmd)
820{
821 char_u *p = *cmd;
822
823 if (checkforcmd(&p, "function", 2))
824 {
825 if (*p == '(')
826 return FALSE;
827 *cmd = p;
828 return TRUE;
829 }
830 return FALSE;
831}
832
833/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200834 * Called when defining a function: The context may be needed for script
835 * variables declared in a block that is visible now but not when the function
836 * is compiled or called later.
837 */
838 static void
839function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
840{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000841 if (cstack == NULL || cstack->cs_idx < 0)
842 return;
843
844 int count = cstack->cs_idx + 1;
845 int i;
846
847 fp->uf_block_ids = ALLOC_MULT(int, count);
848 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200849 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000850 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
851 sizeof(int) * count);
852 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200853 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000854
855 // Set flag in each block to indicate a function was defined. This
856 // is used to keep the variable when leaving the block, see
857 // hide_script_var().
858 for (i = 0; i <= cstack->cs_idx; ++i)
859 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200860}
861
862/*
Yegappan Lakshmanan0072cee2025-01-07 20:22:32 +0100863 * Skip over all the characters in a single quoted string starting at "p" and
864 * return a pointer to the character following the ending single quote.
865 * If the ending single quote is missing, then return a pointer to the NUL
866 * character.
867 */
868 static char_u *
869skip_single_quote_string(char_u *p)
870{
871 p++; // skip the beginning single quote
872 while (*p != NUL)
873 {
874 // Within the string, a single quote can be escaped by using
875 // two single quotes.
876 if (*p == '\'' && *(p + 1) == '\'')
877 p += 2;
878 else if (*p == '\'')
879 {
880 p++; // skip the ending single quote
881 break;
882 }
883 else
884 MB_PTR_ADV(p);
885 }
886
887 return p;
888}
889
890/*
891 * Skip over all the characters in a double quoted string starting at "p" and
892 * return a pointer to the character following the ending double quote.
893 * If the ending double quote is missing, then return a pointer to the NUL
894 * character.
895 */
896 static char_u *
897skip_double_quote_string(char_u *p)
898{
899 p++; // skip the beginning double quote
900 while (*p != NUL)
901 {
902 // Within the string, a double quote can be escaped by
903 // preceding it with a backslash.
904 if (*p == '\\' && *(p + 1) == '"')
905 p += 2;
906 else if (*p == '"')
907 {
908 p++; // skip the ending double quote
909 break;
910 }
911 else
912 MB_PTR_ADV(p);
913 }
914
915 return p;
916}
917
918/*
919 * Return the start of a Vim9 comment (#) in the line starting at "line".
920 * If a comment is not found, then returns a pointer to the end of the
921 * string (NUL).
922 */
923 static char_u *
924find_start_of_vim9_comment(char_u *line)
925{
926 char_u *p = line;
927
928 while (*p != NUL)
929 {
930 if (*p == '\'')
931 // Skip a single quoted string.
932 p = skip_single_quote_string(p);
933 else if (*p == '"')
934 // Skip a double quoted string.
935 p = skip_double_quote_string(p);
936 else
937 {
938 if (*p == '#')
939 // Found the start of a Vim9 comment
940 break;
941 MB_PTR_ADV(p);
942 }
943 }
944
945 return p;
946}
947
948/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100949 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200950 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100951 * "newlines" must already have been initialized.
952 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
953 */
954 static int
955get_function_body(
956 exarg_T *eap,
957 garray_T *newlines,
958 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000959 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100960{
961 linenr_T sourcing_lnum_top = SOURCING_LNUM;
962 linenr_T sourcing_lnum_off;
963 int saved_wait_return = need_wait_return;
964 char_u *line_arg = line_arg_in;
965 int vim9_function = eap->cmdidx == CMD_def
966 || eap->cmdidx == CMD_block;
967#define MAX_FUNC_NESTING 50
968 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200969 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100970 int nesting = 0;
971 getline_opt_T getline_options;
972 int indent = 2;
973 char_u *skip_until = NULL;
974 int ret = FAIL;
975 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200976 int heredoc_concat_len = 0;
977 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100978 char_u *heredoc_trimmed = NULL;
979
Bram Moolenaar20677332021-06-06 17:02:53 +0200980 ga_init2(&heredoc_ga, 1, 500);
981
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100982 // Detect having skipped over comment lines to find the return
983 // type. Add NULL lines to keep the line count correct.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100984 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100985 if (SOURCING_LNUM < sourcing_lnum_off)
986 {
987 sourcing_lnum_off -= SOURCING_LNUM;
988 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
989 goto theend;
990 while (sourcing_lnum_off-- > 0)
991 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
992 }
993
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200994 nesting_def[0] = vim9_function;
995 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100996 getline_options = vim9_function
997 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
998 for (;;)
999 {
1000 char_u *theline;
1001 char_u *p;
1002 char_u *arg;
1003
1004 if (KeyTyped)
1005 {
1006 msg_scroll = TRUE;
1007 saved_wait_return = FALSE;
1008 }
1009 need_wait_return = FALSE;
1010
1011 if (line_arg != NULL)
1012 {
1013 // Use eap->arg, split up in parts by line breaks.
1014 theline = line_arg;
1015 p = vim_strchr(theline, '\n');
1016 if (p == NULL)
1017 line_arg += STRLEN(line_arg);
1018 else
1019 {
1020 *p = NUL;
1021 line_arg = p + 1;
1022 }
1023 }
1024 else
1025 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001026 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001027 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001028 }
1029 if (KeyTyped)
1030 lines_left = Rows - 1;
1031 if (theline == NULL)
1032 {
1033 // Use the start of the function for the line number.
1034 SOURCING_LNUM = sourcing_lnum_top;
1035 if (skip_until != NULL)
1036 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001037 else if (nesting_inline[nesting])
1038 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001039 else if (eap->cmdidx == CMD_def)
1040 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001041 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001042 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001043 goto theend;
1044 }
1045
1046 // Detect line continuation: SOURCING_LNUM increased more than one.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001047 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001048 if (SOURCING_LNUM < sourcing_lnum_off)
1049 sourcing_lnum_off -= SOURCING_LNUM;
1050 else
1051 sourcing_lnum_off = 0;
1052
1053 if (skip_until != NULL)
1054 {
1055 // Don't check for ":endfunc"/":enddef" between
1056 // * ":append" and "."
1057 // * ":python <<EOF" and "EOF"
1058 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
1059 if (heredoc_trimmed == NULL
1060 || (is_heredoc && skipwhite(theline) == theline)
1061 || STRNCMP(theline, heredoc_trimmed,
1062 STRLEN(heredoc_trimmed)) == 0)
1063 {
1064 if (heredoc_trimmed == NULL)
1065 p = theline;
1066 else if (is_heredoc)
1067 p = skipwhite(theline) == theline
1068 ? theline : theline + STRLEN(heredoc_trimmed);
1069 else
1070 p = theline + STRLEN(heredoc_trimmed);
1071 if (STRCMP(p, skip_until) == 0)
1072 {
1073 VIM_CLEAR(skip_until);
1074 VIM_CLEAR(heredoc_trimmed);
1075 getline_options = vim9_function
1076 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
1077 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +02001078
1079 if (heredoc_concat_len > 0)
1080 {
1081 // Replace the starting line with all the concatenated
1082 // lines.
1083 ga_concat(&heredoc_ga, theline);
1084 vim_free(((char_u **)(newlines->ga_data))[
1085 heredoc_concat_len - 1]);
1086 ((char_u **)(newlines->ga_data))[
1087 heredoc_concat_len - 1] = heredoc_ga.ga_data;
1088 ga_init(&heredoc_ga);
1089 heredoc_concat_len = 0;
1090 theline += STRLEN(theline); // skip the "EOF"
1091 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001092 }
1093 }
1094 }
1095 else
1096 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001097 int c;
1098 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001099 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001100
1101 // skip ':' and blanks
1102 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1103 ;
1104
1105 // Check for "endfunction", "enddef" or "}".
1106 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001107 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001108 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001109 ? *p == '}'
1110 : (checkforcmd(&p, nesting_def[nesting]
1111 ? "enddef" : "endfunction", 4)
1112 && *p != ':'))
1113 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001114 if (!nesting_inline[nesting] && nesting_def[nesting]
1115 && p < cmd + 6)
1116 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001117 if (nesting-- == 0)
1118 {
1119 char_u *nextcmd = NULL;
1120
1121 if (*p == '|' || *p == '}')
1122 nextcmd = p + 1;
1123 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1124 nextcmd = line_arg;
1125 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001126 && (vim9_function || p_verbose > 0))
1127 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001128 SOURCING_LNUM = sourcing_lnum_top
1129 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001130 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001131 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001132 else
1133 give_warning2((char_u *)
1134 _("W22: Text found after :endfunction: %s"),
1135 p, TRUE);
1136 }
1137 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001138 {
1139 // Another command follows. If the line came from "eap"
1140 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001141 // change "eap->cmdlinep" to point to the last fetched
1142 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001143 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001144 if (lines_to_free->ga_len > 0
1145 && *eap->cmdlinep !=
1146 ((char_u **)lines_to_free->ga_data)
1147 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001148 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001149 // *cmdlinep will be freed later, thus remove the
1150 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001151 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001152 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1153 [lines_to_free->ga_len - 1];
1154 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001155 }
1156 }
1157 break;
1158 }
1159 }
1160
1161 // Check for mismatched "endfunc" or "enddef".
1162 // We don't check for "def" inside "func" thus we also can't check
1163 // for "enddef".
1164 // We continue to find the end of the function, although we might
1165 // not find it.
1166 else if (nesting_def[nesting])
1167 {
1168 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1169 emsg(_(e_mismatched_endfunction));
1170 }
1171 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1172 emsg(_(e_mismatched_enddef));
1173
1174 // Increase indent inside "if", "while", "for" and "try", decrease
1175 // at "end".
1176 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1177 indent -= 2;
1178 else if (STRNCMP(p, "if", 2) == 0
1179 || STRNCMP(p, "wh", 2) == 0
1180 || STRNCMP(p, "for", 3) == 0
1181 || STRNCMP(p, "try", 3) == 0)
1182 indent += 2;
1183
1184 // Check for defining a function inside this function.
1185 // Only recognize "def" inside "def", not inside "function",
1186 // For backwards compatibility, see Test_function_python().
1187 c = *p;
1188 if (is_function_cmd(&p)
1189 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1190 {
1191 if (*p == '!')
1192 p = skipwhite(p + 1);
1193 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001194 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001195 if (*skipwhite(p) == '(')
1196 {
1197 if (nesting == MAX_FUNC_NESTING - 1)
1198 emsg(_(e_function_nesting_too_deep));
1199 else
1200 {
1201 ++nesting;
1202 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001203 nesting_inline[nesting] = FALSE;
1204 indent += 2;
1205 }
1206 }
1207 }
1208
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001209 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001210 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001211 // Not a comment line: check for nested inline function.
Yegappan Lakshmanan0072cee2025-01-07 20:22:32 +01001212
1213 if (nesting_inline[nesting])
1214 {
1215 // A comment (#) can follow the opening curly brace of a
1216 // block statement. Need to ignore the comment and look
1217 // for the opening curly brace before the comment.
1218 end = find_start_of_vim9_comment(p) - 1;
1219 }
1220 else
1221 end = p + STRLEN(p) - 1;
1222
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001223 while (end > p && VIM_ISWHITE(*end))
1224 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001225 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001226 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001227 int is_block;
1228
1229 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001230 --end;
1231 while (end > p && VIM_ISWHITE(*end))
1232 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001233 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1234 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001235 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001236 char_u *s = p;
1237
1238 // check for line starting with "au" for :autocmd or
1239 // "com" for :command, these can use a {} block
1240 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1241 || checkforcmd_noparen(&s, "command", 3);
1242 }
1243
1244 if (is_block)
1245 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001246 if (nesting == MAX_FUNC_NESTING - 1)
1247 emsg(_(e_function_nesting_too_deep));
1248 else
1249 {
1250 ++nesting;
1251 nesting_def[nesting] = TRUE;
1252 nesting_inline[nesting] = TRUE;
1253 indent += 2;
1254 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001255 }
1256 }
1257 }
1258
1259 // Check for ":append", ":change", ":insert". Not for :def.
1260 p = skip_range(p, FALSE, NULL);
1261 if (!vim9_function
1262 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1263 || (p[0] == 'c'
1264 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1265 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1266 && (STRNCMP(&p[3], "nge", 3) != 0
1267 || !ASCII_ISALPHA(p[6])))))))
1268 || (p[0] == 'i'
1269 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1270 && (!ASCII_ISALPHA(p[2])
1271 || (p[2] == 's'
1272 && (!ASCII_ISALPHA(p[3])
1273 || p[3] == 'e'))))))))
1274 skip_until = vim_strsave((char_u *)".");
1275
1276 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1277 arg = skipwhite(skiptowhite(p));
1278 if (arg[0] == '<' && arg[1] =='<'
1279 && ((p[0] == 'p' && p[1] == 'y'
1280 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1281 || ((p[2] == '3' || p[2] == 'x')
1282 && !ASCII_ISALPHA(p[3]))))
1283 || (p[0] == 'p' && p[1] == 'e'
1284 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1285 || (p[0] == 't' && p[1] == 'c'
1286 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1287 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1288 && !ASCII_ISALPHA(p[3]))
1289 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1290 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1291 || (p[0] == 'm' && p[1] == 'z'
1292 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1293 ))
1294 {
1295 // ":python <<" continues until a dot, like ":append"
1296 p = skipwhite(arg + 2);
1297 if (STRNCMP(p, "trim", 4) == 0)
1298 {
1299 // Ignore leading white space.
1300 p = skipwhite(p + 4);
1301 heredoc_trimmed = vim_strnsave(theline,
1302 skipwhite(theline) - theline);
1303 }
1304 if (*p == NUL)
1305 skip_until = vim_strsave((char_u *)".");
1306 else
1307 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1308 getline_options = GETLINE_NONE;
1309 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001310 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001311 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001312 }
1313
Bram Moolenaard881d152022-05-13 13:50:36 +01001314 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001315 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001316 // Check for ":cmd v =<< [trim] EOF"
1317 // and ":cmd [a, b] =<< [trim] EOF"
1318 // and "lines =<< [trim] EOF" for Vim9
1319 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001320 arg = p;
1321 if (checkforcmd(&arg, "let", 2)
1322 || checkforcmd(&arg, "var", 3)
1323 || checkforcmd(&arg, "final", 5)
1324 || checkforcmd(&arg, "const", 5)
1325 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001326 {
zeertzjq9a91d2b2024-04-09 21:47:10 +02001327 int save_sc_version = current_sctx.sc_version;
1328 int var_count = 0;
1329 int semicolon = 0;
zeertzjq9a91d2b2024-04-09 21:47:10 +02001330
1331 current_sctx.sc_version
1332 = vim9_function ? SCRIPT_VERSION_VIM9 : 1;
zeertzjq1817ccd2024-04-10 17:37:47 +02001333 arg = skip_var_list(arg, TRUE, &var_count, &semicolon,
zeertzjq9a91d2b2024-04-09 21:47:10 +02001334 TRUE);
zeertzjq1817ccd2024-04-10 17:37:47 +02001335 if (arg != NULL)
1336 arg = skipwhite(arg);
zeertzjq9a91d2b2024-04-09 21:47:10 +02001337 current_sctx.sc_version = save_sc_version;
zeertzjq1817ccd2024-04-10 17:37:47 +02001338 if (arg != NULL && STRNCMP(arg, "=<<", 3) == 0)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001339 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001340 p = skipwhite(arg + 3);
1341 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001342 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001343 if (STRNCMP(p, "trim", 4) == 0)
1344 {
1345 // Ignore leading white space.
1346 p = skipwhite(p + 4);
1347 heredoc_trimmed = vim_strnsave(theline,
1348 skipwhite(theline) - theline);
1349 continue;
1350 }
1351 if (STRNCMP(p, "eval", 4) == 0)
1352 {
1353 // Ignore leading white space.
1354 p = skipwhite(p + 4);
1355 continue;
1356 }
1357 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001358 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001359 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1360 getline_options = GETLINE_NONE;
1361 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001362 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001363 }
1364 }
1365 }
1366
1367 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001368 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001369 goto theend;
1370
Bram Moolenaar20677332021-06-06 17:02:53 +02001371 if (heredoc_concat_len > 0)
1372 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001373 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001374 // to be used for the instruction later.
1375 ga_concat(&heredoc_ga, theline);
1376 ga_concat(&heredoc_ga, (char_u *)"\n");
1377 p = vim_strsave((char_u *)"");
1378 }
1379 else
1380 {
1381 // Copy the line to newly allocated memory. get_one_sourceline()
1382 // allocates 250 bytes per line, this saves 80% on average. The
1383 // cost is an extra alloc/free.
1384 p = vim_strsave(theline);
1385 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001386 if (p == NULL)
1387 goto theend;
1388 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1389
1390 // Add NULL lines for continuation lines, so that the line count is
1391 // equal to the index in the growarray.
1392 while (sourcing_lnum_off-- > 0)
1393 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1394
1395 // Check for end of eap->arg.
1396 if (line_arg != NULL && *line_arg == NUL)
1397 line_arg = NULL;
1398 }
1399
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001400 // Return OK when no error was detected.
1401 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001402 ret = OK;
1403
1404theend:
1405 vim_free(skip_until);
1406 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001407 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001408 need_wait_return |= saved_wait_return;
1409 return ret;
1410}
1411
1412/*
1413 * Handle the body of a lambda. *arg points to the "{", process statements
1414 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001415 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001416 * When successful "rettv" is set to a funcref.
1417 */
1418 static int
1419lambda_function_body(
1420 char_u **arg,
1421 typval_T *rettv,
1422 evalarg_T *evalarg,
1423 garray_T *newargs,
1424 garray_T *argtypes,
1425 int varargs,
1426 garray_T *default_args,
1427 char_u *ret_type)
1428{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001429 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001430 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001431 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001432 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001433 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001434 exarg_T eap;
1435 garray_T newlines;
1436 char_u *cmdline = NULL;
1437 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001438 partial_T *pt;
1439 char_u *name;
1440 int lnum_save = -1;
1441 linenr_T sourcing_lnum_top = SOURCING_LNUM;
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001442 char_u *line_arg = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001443
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001444 *arg = skipwhite(*arg + 1);
1445 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001446 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001447 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001448 return FAIL;
1449 }
1450
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001451 // When there is a line break use what follows for the lambda body.
1452 // Makes lambda body initializers work for object and enum member
1453 // variables.
1454 if (**arg == '\n')
1455 line_arg = *arg + 1;
1456
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001457 CLEAR_FIELD(eap);
1458 eap.cmdidx = CMD_block;
1459 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001460 eap.cmdlinep = &cmdline;
1461 eap.skip = !evaluate;
1462 if (evalarg->eval_cctx != NULL)
1463 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1464 else
1465 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001466 eap.ea_getline = evalarg->eval_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001467 eap.cookie = evalarg->eval_cookie;
1468 }
1469
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001470 ga_init2(&newlines, sizeof(char_u *), 10);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001471 if (get_function_body(&eap, &newlines, line_arg,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001472 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001473 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001474
1475 // When inside a lambda must add the function lines to evalarg.eval_ga.
1476 evalarg->eval_break_count += newlines.ga_len;
1477 if (gap->ga_itemsize > 0)
1478 {
1479 int idx;
1480 char_u *last;
1481 size_t plen;
1482 char_u *pnl;
1483
1484 for (idx = 0; idx < newlines.ga_len; ++idx)
1485 {
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001486 char_u *p = ((char_u **)newlines.ga_data)[idx];
1487 if (p == NULL)
1488 // comment line in the lambda body
1489 continue;
1490
1491 p = skipwhite(p);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001492
Bram Moolenaarecb66452021-05-18 15:09:18 +02001493 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001494 goto erret;
1495
1496 // Going to concatenate the lines after parsing. For an empty or
1497 // comment line use an empty string.
1498 // Insert NL characters at the start of each line, the string will
1499 // be split again later in .get_lambda_tv().
1500 if (*p == NUL || vim9_comment_start(p))
1501 p = (char_u *)"";
1502 plen = STRLEN(p);
1503 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1504 if (pnl != NULL)
1505 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001506 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1507 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001508 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001509 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001510 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001511 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001512 // more is following after the "}", which was skipped
1513 last = cmdline;
1514 else
1515 // nothing is following the "}"
1516 last = (char_u *)"}";
1517 plen = STRLEN(last);
1518 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1519 if (pnl != NULL)
1520 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001521 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1522 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001523 }
1524
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001525 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001526 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001527 garray_T *tfgap = &evalarg->eval_tofree_ga;
1528
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001529 // Something comes after the "}".
1530 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001531
1532 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001533 if (ga_grow(tfgap, 1) == OK)
1534 {
1535 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1536 evalarg->eval_using_cmdline = TRUE;
1537 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001538 }
1539 else
1540 *arg = (char_u *)"";
1541
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001542 if (!evaluate)
1543 {
1544 ret = OK;
1545 goto erret;
1546 }
1547
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001548 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001549 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001550 if (ufunc == NULL)
1551 goto erret;
1552 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001553 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001554 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001555 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001556 ufunc->uf_refcount = 1;
1557 ufunc->uf_args = *newargs;
1558 newargs->ga_data = NULL;
1559 ufunc->uf_def_args = *default_args;
1560 default_args->ga_data = NULL;
1561 ufunc->uf_func_type = &t_func_any;
1562
1563 // error messages are for the first function line
1564 lnum_save = SOURCING_LNUM;
1565 SOURCING_LNUM = sourcing_lnum_top;
1566
1567 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001568 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001569 {
1570 SOURCING_LNUM = lnum_save;
1571 goto erret;
1572 }
1573
1574 // parse the return type, if any
1575 if (parse_return_type(ufunc, ret_type) == FAIL)
1576 goto erret;
1577
1578 pt = ALLOC_CLEAR_ONE(partial_T);
1579 if (pt == NULL)
1580 goto erret;
1581 pt->pt_func = ufunc;
1582 pt->pt_refcount = 1;
1583
1584 ufunc->uf_lines = newlines;
1585 newlines.ga_data = NULL;
1586 if (sandbox)
1587 ufunc->uf_flags |= FC_SANDBOX;
1588 if (!ASCII_ISUPPER(*ufunc->uf_name))
1589 ufunc->uf_flags |= FC_VIM9;
1590 ufunc->uf_script_ctx = current_sctx;
1591 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1592 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1593 set_function_type(ufunc);
1594
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001595 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1596
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001597 rettv->vval.v_partial = pt;
1598 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001599 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001600 ret = OK;
1601
1602erret:
1603 if (lnum_save >= 0)
1604 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001605 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001606 if (newargs != NULL)
1607 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001608 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001609 if (ufunc != NULL)
1610 {
1611 func_clear(ufunc, TRUE);
1612 func_free(ufunc, TRUE);
1613 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001614 return ret;
1615}
1616
1617/*
1618 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001619 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001620 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001621 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1622 */
1623 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001624get_lambda_tv(
1625 char_u **arg,
1626 typval_T *rettv,
1627 int types_optional,
1628 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001629{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001630 int evaluate = evalarg != NULL
1631 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001632 garray_T newargs;
1633 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001634 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001635 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001636 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001637 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001638 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001639 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001641 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001642 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001643 char_u *s;
1644 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001645 int *old_eval_lavars = eval_lavars_used;
1646 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001647 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001648 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001649 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001650 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001651 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001652 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001653
Bram Moolenaar4525a572022-02-13 11:57:33 +00001654 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001655 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001656
1657 ga_init(&newargs);
1658 ga_init(&newlines);
1659
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001660 // First, check if this is really a lambda expression. "->" or "=>" must
1661 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001662 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001663 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001664 types_optional ? &argtypes : NULL, types_optional,
1665 types_optional ? &arg_objm : NULL, evalarg,
1666 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001667 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001668 {
1669 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001670 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001671 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001672 ga_clear(&arg_objm);
1673 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001674 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001675 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001676
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001677 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001678 if (evaluate)
1679 pnewargs = &newargs;
1680 else
1681 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001682 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001683 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001684 types_optional ? &argtypes : NULL, types_optional,
1685 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001686 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001687 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001688 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001689 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001690 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001691 {
1692 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001693 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001694 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001695 ga_clear(&arg_objm);
1696 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001697 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001698 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001699 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001700 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001701
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001702 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1703 if (ret_type != NULL)
1704 {
1705 ret_type = vim_strsave(ret_type);
1706 tofree2 = ret_type;
1707 }
1708
Bram Moolenaare38eab22019-12-05 21:50:01 +01001709 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001710 if (evaluate)
1711 eval_lavars_used = &eval_lavars;
1712
Bram Moolenaar65c44152020-12-24 15:14:01 +01001713 *arg = skipwhite_and_linebreak(*arg, evalarg);
1714
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001715 // Recognize "{" as the start of a function body.
1716 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001717 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001718 if (evalarg == NULL)
1719 // cannot happen?
1720 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001721 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001722 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1723 types_optional ? &argtypes : NULL, varargs,
1724 &default_args, ret_type) == FAIL)
1725 goto errret;
1726 goto theend;
1727 }
1728 if (default_args.ga_len > 0)
1729 {
1730 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001731 goto errret;
1732 }
1733
Bram Moolenaare38eab22019-12-05 21:50:01 +01001734 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001735 start = *arg;
1736 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001737 if (ret == FAIL)
1738 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001739
Bram Moolenaar65c44152020-12-24 15:14:01 +01001740 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001741 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001742 *arg = skipwhite_and_linebreak(*arg, evalarg);
1743 if (**arg != '}')
1744 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001745 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001746 goto errret;
1747 }
1748 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001749 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001750
1751 if (evaluate)
1752 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001753 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001754 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001755 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001756 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001757 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001758
Bram Moolenaare01e5212023-01-08 20:31:18 +00001759 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001760 if (fp == NULL)
1761 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001762 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001763 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001764 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001765 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001766
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001767 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001768 if (ga_grow(&newlines, 1) == FAIL)
1769 goto errret;
1770
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001771 // If there are line breaks, we need to split up the string.
1772 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001773 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001774 line_end = end;
1775
1776 // Add "return " before the expression (or the first line).
1777 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001778 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001779 if (p == NULL)
1780 goto errret;
1781 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1782 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001783 vim_strncpy(p + 7, start, line_end - start);
1784
1785 if (line_end != end)
1786 {
1787 // Add more lines, split by line breaks. Thus is used when a
1788 // lambda with { cmds } is encountered.
1789 while (*line_end == '\n')
1790 {
1791 if (ga_grow(&newlines, 1) == FAIL)
1792 goto errret;
1793 start = line_end + 1;
1794 line_end = vim_strchr(start, '\n');
1795 if (line_end == NULL)
1796 line_end = end;
1797 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1798 vim_strnsave(start, line_end - start);
1799 }
1800 }
1801
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001802 if (strstr((char *)p + 7, "a:") == NULL)
1803 // No a: variables are used for sure.
1804 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805
1806 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001807 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001808 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001809 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001810 if (types_optional)
1811 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001812 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001813 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001814 goto errret;
1815 if (ret_type != NULL)
1816 {
1817 fp->uf_ret_type = parse_type(&ret_type,
1818 &fp->uf_type_list, TRUE);
1819 if (fp->uf_ret_type == NULL)
1820 goto errret;
1821 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001822 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001823 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001824 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001825
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001826 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001827 if (current_funccal != NULL && eval_lavars)
1828 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001829 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001830 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001831 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001832 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001833
1834#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001835 if (prof_def_func())
1836 func_do_profile(fp);
1837#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001838 if (sandbox)
1839 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001840 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001841 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001842 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001843 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001844 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001845 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001846 // Use the line number of the arguments.
1847 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001848
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001849 function_using_block_scopes(fp, evalarg->eval_cstack);
1850
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001851 pt->pt_func = fp;
1852 pt->pt_refcount = 1;
1853 rettv->vval.v_partial = pt;
1854 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001855
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001856 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001857 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001858
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001859theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001860 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001861 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001862 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001863 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001864 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001865 ga_clear(&arg_objm);
1866 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001867
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001868 return OK;
1869
1870errret:
1871 ga_clear_strings(&newargs);
1872 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001873 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001874 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001875 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001876 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001877 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001878 if (fp != NULL)
1879 vim_free(fp->uf_arg_types);
1880 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001881 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001882 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001883 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001884 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001885 return FAIL;
1886}
1887
1888/*
1889 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1890 * name it contains, otherwise return "name".
1891 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1892 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001893 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1894 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001895 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001896 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001897 */
1898 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001899deref_func_name(
1900 char_u *name,
1901 int *lenp,
1902 partial_T **partialp,
1903 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001904 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001905 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001906 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001907{
1908 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001909 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001910 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001911 char_u *s = NULL;
1912 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001913 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001914
1915 if (partialp != NULL)
1916 *partialp = NULL;
1917
1918 cc = name[*lenp];
1919 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001920
Bram Moolenaar71f21932022-01-07 18:20:55 +00001921 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001922 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001923 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001924 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001925 tv = &v->di_tv;
1926 }
1927 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1928 {
1929 imported_T *import;
1930 char_u *p = name;
1931 int len = *lenp;
1932
1933 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001934 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001935 p = name + 2;
1936 len -= 2;
1937 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001938 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001939
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001940 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001941 if (import != NULL)
1942 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001943 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001944 if (new_function)
1945 semsg(_(e_redefining_imported_item_str), name);
1946 else
1947 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001948 name[len] = cc;
1949 *lenp = 0;
1950 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001951 }
1952 }
1953
1954 if (tv != NULL)
1955 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001956 if (found_var != NULL)
1957 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001958 if (tv->v_type == VAR_FUNC)
1959 {
1960 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001961 {
1962 *lenp = 0;
1963 return (char_u *)""; // just in case
1964 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001965 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001966 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001967 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001968
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001969 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001970 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001971 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001972
1973 if (pt == NULL)
1974 {
1975 *lenp = 0;
1976 return (char_u *)""; // just in case
1977 }
1978 if (partialp != NULL)
1979 *partialp = pt;
1980 s = partial_name(pt);
1981 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001982 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001983
1984 if (s != NULL)
1985 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001986 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001987 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001988 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001989
1990 if (sv != NULL)
1991 *type = sv->sv_type;
1992 }
1993 return s;
1994 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001995 }
1996
1997 return name;
1998}
1999
2000/*
2001 * Give an error message with a function name. Handle <SNR> things.
2002 * "ermsg" is to be passed without translation, use N_() instead of _().
2003 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002004 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002005emsg_funcname(char *ermsg, char_u *name)
2006{
Bram Moolenaar54598062022-01-13 12:05:09 +00002007 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002008
Bram Moolenaar54598062022-01-13 12:05:09 +00002009 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002010 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002011 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002012 if (p != name)
2013 vim_free(p);
2014}
2015
2016/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002017 * Get function arguments at "*arg" and advance it.
2018 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01002019 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002020 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002021 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002022get_func_arguments(
2023 char_u **arg,
2024 evalarg_T *evalarg,
2025 int partial_argc,
2026 typval_T *argvars,
Ernie Raelb077b582023-12-14 20:11:44 +01002027 int *argcount,
2028 int is_builtin)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002029{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002030 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002031 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002032 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002033 int evaluate = evalarg == NULL
2034 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002035
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002036 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002037 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02002038 // skip the '(' or ',' and possibly line breaks
2039 argp = skipwhite_and_linebreak(argp + 1, evalarg);
2040
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002041 if (*argp == ')' || *argp == ',' || *argp == NUL)
2042 break;
Ernie Raelb077b582023-12-14 20:11:44 +01002043
2044 int arg_idx = *argcount;
2045 if (eval1(&argp, &argvars[arg_idx], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002046 {
2047 ret = FAIL;
2048 break;
2049 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002050 ++*argcount;
Ernie Raelb077b582023-12-14 20:11:44 +01002051 if (!is_builtin && check_typval_is_value(&argvars[arg_idx]) == FAIL)
2052 {
2053 ret = FAIL;
2054 break;
2055 }
2056
Bram Moolenaar9d489562020-07-30 20:08:50 +02002057 // The comma should come right after the argument, but this wasn't
2058 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002059 if (vim9script)
2060 {
2061 if (*argp != ',' && *skipwhite(argp) == ',')
2062 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002063 if (evaluate)
2064 semsg(_(e_no_white_space_allowed_before_str_str),
2065 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002066 ret = FAIL;
2067 break;
2068 }
2069 }
2070 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002071 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002072 if (*argp != ',')
2073 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02002074 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002075 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002076 if (evaluate)
2077 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002078 ret = FAIL;
2079 break;
2080 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002081 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002082
Bram Moolenaare6b53242020-07-01 17:28:33 +02002083 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002084 if (*argp == ')')
2085 ++argp;
2086 else
2087 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002088 *arg = argp;
2089 return ret;
2090}
2091
2092/*
2093 * Call a function and put the result in "rettv".
2094 * Return OK or FAIL.
2095 */
2096 int
2097get_func_tv(
2098 char_u *name, // name of the function
2099 int len, // length of "name" or -1 to use strlen()
2100 typval_T *rettv,
2101 char_u **arg, // argument, pointing to the '('
2102 evalarg_T *evalarg, // for line continuation
2103 funcexe_T *funcexe) // various values
2104{
2105 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002106 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002107 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
2108 int argcount = 0; // number of arguments found
2109 int vim9script = in_vim9script();
2110 int evaluate = evalarg == NULL
2111 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
2112
2113 argp = *arg;
2114 ret = get_func_arguments(&argp, evalarg,
2115 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
Ernie Raelb077b582023-12-14 20:11:44 +01002116 argvars, &argcount, builtin_function(name, -1));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002117
2118 if (ret == OK)
2119 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002120 int i = 0;
2121 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002122
2123 if (get_vim_var_nr(VV_TESTING))
2124 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002125 // Prepare for calling test_garbagecollect_now(), need to know
2126 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002127 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002128 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002129 for (i = 0; i < argcount; ++i)
2130 if (ga_grow(&funcargs, 1) == OK)
2131 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2132 &argvars[i];
2133 }
2134
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002135 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002136 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002137 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002138 // An error in a builtin function does not return FAIL, but we do
2139 // want to abort further processing if an error was given.
2140 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002141 clear_tv(rettv);
2142 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002143
2144 funcargs.ga_len -= i;
2145 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002146 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002147 {
2148 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002149 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002150 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002151 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002152 }
2153
2154 while (--argcount >= 0)
2155 clear_tv(&argvars[argcount]);
2156
Bram Moolenaar4525a572022-02-13 11:57:33 +00002157 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002158 *arg = argp;
2159 else
2160 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002161 return ret;
2162}
2163
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002164/*
2165 * Return TRUE if "p" starts with "<SID>" or "s:".
2166 * Only works if eval_fname_script() returned non-zero for "p"!
2167 */
2168 static int
2169eval_fname_sid(char_u *p)
2170{
2171 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2172}
2173
2174/*
2175 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2176 * Change <SNR>123_name() to K_SNR 123_name().
2177 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002178 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002179 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002180 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002181fname_trans_sid(
2182 char_u *name,
2183 char_u *fname_buf,
2184 char_u **tofree,
2185 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002186{
2187 int llen;
2188 char_u *fname;
2189 int i;
2190
2191 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002192 if (llen == 0)
2193 return name; // no prefix
2194
2195 fname_buf[0] = K_SPECIAL;
2196 fname_buf[1] = KS_EXTRA;
2197 fname_buf[2] = (int)KE_SNR;
2198 i = 3;
2199 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002200 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002201 if (current_sctx.sc_sid <= 0)
2202 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002203 else
2204 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002205 sprintf((char *)fname_buf + 3, "%ld_",
2206 (long)current_sctx.sc_sid);
2207 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002208 }
2209 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002210 if (i + STRLEN(name + llen) < FLEN_FIXED)
2211 {
2212 STRCPY(fname_buf + i, name + llen);
2213 fname = fname_buf;
2214 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002215 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002216 {
2217 fname = alloc(i + STRLEN(name + llen) + 1);
2218 if (fname == NULL)
2219 *error = FCERR_OTHER;
2220 else
2221 {
2222 *tofree = fname;
2223 mch_memmove(fname, fname_buf, (size_t)i);
2224 STRCPY(fname + i, name + llen);
2225 }
2226 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002227 return fname;
2228}
2229
2230/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002231 * Concatenate the script ID and function name into "<SNR>99_name".
2232 * "buffer" must have size MAX_FUNC_NAME_LEN.
2233 */
2234 void
2235func_name_with_sid(char_u *name, int sid, char_u *buffer)
2236{
2237 // A script-local function is stored as "<SNR>99_name".
2238 buffer[0] = K_SPECIAL;
2239 buffer[1] = KS_EXTRA;
2240 buffer[2] = (int)KE_SNR;
2241 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2242 (long)sid, name);
2243}
2244
2245/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 * Find a function "name" in script "sid".
2247 */
2248 static ufunc_T *
2249find_func_with_sid(char_u *name, int sid)
2250{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002251 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002252 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253
Bram Moolenaarb8822442022-01-11 15:24:05 +00002254 if (!SCRIPT_ID_VALID(sid))
2255 return NULL; // not in a script
2256
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002257 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 hi = hash_find(&func_hashtab, buffer);
2259 if (!HASHITEM_EMPTY(hi))
2260 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002261 return NULL;
2262}
2263
2264/*
2265 * Find a function "name" in script "sid" prefixing the autoload prefix.
2266 */
2267 static ufunc_T *
2268find_func_with_prefix(char_u *name, int sid)
2269{
2270 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002271 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002272 scriptitem_T *si;
2273
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002274 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002275 return NULL; // already has the prefix
2276 if (!SCRIPT_ID_VALID(sid))
2277 return NULL; // not in a script
2278 si = SCRIPT_ITEM(sid);
2279 if (si->sn_autoload_prefix != NULL)
2280 {
2281 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2282 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002283 char_u *namep;
2284
2285 // skip a "<SNR>99_" prefix
2286 namep = untrans_function_name(name);
2287 if (namep == NULL)
2288 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002289
2290 // An exported function in an autoload script is stored as
2291 // "dir#path#name".
2292 if (len < sizeof(buffer))
2293 auto_name = buffer;
2294 else
2295 auto_name = alloc(len);
2296 if (auto_name != NULL)
2297 {
2298 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002299 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002300 hi = hash_find(&func_hashtab, auto_name);
2301 if (auto_name != buffer)
2302 vim_free(auto_name);
2303 if (!HASHITEM_EMPTY(hi))
2304 return HI2UF(hi);
2305 }
2306 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307
2308 return NULL;
2309}
2310
2311/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002312 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002313 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2314 * functions.
2315 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002316 * Return NULL for unknown function.
2317 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002318 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002319find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002320{
2321 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002324 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002326 // Find script-local function before global one.
2327 if (in_vim9script() && eval_isnamec1(*name)
2328 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002330 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2331 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002332 if (func != NULL)
2333 return func;
2334 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002335 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2336 {
2337 char_u *p = name + 5;
2338 long sid;
2339
2340 // printable "<SNR>123_Name" form
2341 sid = getdigits(&p);
2342 if (*p == '_')
2343 {
2344 func = find_func_with_sid(p + 1, (int)sid);
2345 if (func != NULL)
2346 return func;
2347 }
2348 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002350
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002351 if ((flags & FFED_NO_GLOBAL) == 0)
2352 {
2353 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002354 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002355 if (!HASHITEM_EMPTY(hi))
2356 return HI2UF(hi);
2357 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002358
Bram Moolenaarb8822442022-01-11 15:24:05 +00002359 // Find autoload function if this is an autoload script.
Yegappan Lakshmanan6289f912025-01-14 17:13:36 +01002360 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
Bram Moolenaarb8822442022-01-11 15:24:05 +00002361 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362}
2363
2364/*
2365 * Find a function by name, return pointer to it in ufuncs.
2366 * "cctx" is passed in a :def function to find imported functions.
2367 * Return NULL for unknown or dead function.
2368 */
2369 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002370find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002372 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373
2374 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2375 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002376 return NULL;
2377}
2378
2379/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002380 * Return TRUE if "ufunc" is a global function.
2381 */
2382 int
2383func_is_global(ufunc_T *ufunc)
2384{
2385 return ufunc->uf_name[0] != K_SPECIAL;
2386}
2387
2388/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002389 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2390 */
2391 int
2392func_requires_g_prefix(ufunc_T *ufunc)
2393{
2394 return ufunc->uf_name[0] != K_SPECIAL
2395 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002396 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01002397 && !SAFE_isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002398}
2399
2400/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002401 * Copy the function name of "fp" to buffer "buf".
2402 * "buf" must be able to hold the function name plus three bytes.
2403 * Takes care of script-local function names.
2404 */
2405 static void
2406cat_func_name(char_u *buf, ufunc_T *fp)
2407{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002408 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002409 {
2410 STRCPY(buf, "<SNR>");
2411 STRCAT(buf, fp->uf_name + 3);
2412 }
2413 else
2414 STRCPY(buf, fp->uf_name);
2415}
2416
2417/*
2418 * Add a number variable "name" to dict "dp" with value "nr".
2419 */
2420 static void
2421add_nr_var(
2422 dict_T *dp,
2423 dictitem_T *v,
2424 char *name,
2425 varnumber_T nr)
2426{
2427 STRCPY(v->di_key, name);
2428 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002429 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002430 v->di_tv.v_type = VAR_NUMBER;
2431 v->di_tv.v_lock = VAR_FIXED;
2432 v->di_tv.vval.v_number = nr;
2433}
2434
2435/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002436 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002437 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002438 static void
2439free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002440{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002441 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002442
Bram Moolenaarca16c602022-09-06 18:57:08 +01002443 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002444 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002445 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002446
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002447 // When garbage collecting a funccall_T may be freed before the
2448 // function that references it, clear its uf_scoped field.
2449 // The function may have been redefined and point to another
2450 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002451 if (fp != NULL && fp->uf_scoped == fc)
2452 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002453 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002454 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002455
Bram Moolenaarca16c602022-09-06 18:57:08 +01002456 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002457 vim_free(fc);
2458}
2459
2460/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002461 * Free "fc" and what it contains.
2462 * Can be called only when "fc" is kept beyond the period of it called,
2463 * i.e. after cleanup_function_call(fc).
2464 */
2465 static void
2466free_funccal_contents(funccall_T *fc)
2467{
2468 listitem_T *li;
2469
2470 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002471 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002472
2473 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002474 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002475
2476 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002477 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002478 clear_tv(&li->li_tv);
2479
2480 free_funccal(fc);
2481}
2482
2483/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002484 * Handle the last part of returning from a function: free the local hashtable.
2485 * Unless it is still in use by a closure.
2486 */
2487 static void
2488cleanup_function_call(funccall_T *fc)
2489{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002490 int may_free_fc = fc->fc_refcount <= 0;
2491 int free_fc = TRUE;
2492
Bram Moolenaarca16c602022-09-06 18:57:08 +01002493 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002494
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002495 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002496 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2497 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002498 else
2499 free_fc = FALSE;
2500
2501 // If the a:000 list and the l: and a: dicts are not referenced and
2502 // there is no closure using it, we can free the funccall_T and what's
2503 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002504 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2505 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002506 else
2507 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002508 int todo;
2509 hashitem_T *hi;
2510 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002511
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002512 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002513
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002514 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002515 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002516 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002517 {
2518 if (!HASHITEM_EMPTY(hi))
2519 {
2520 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002521 di = HI2DI(hi);
2522 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002523 }
2524 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002525 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002526
Bram Moolenaarca16c602022-09-06 18:57:08 +01002527 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2528 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002529 else
2530 {
2531 listitem_T *li;
2532
2533 free_fc = FALSE;
2534
2535 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002536 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002537 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002538 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002539
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002540 if (free_fc)
2541 free_funccal(fc);
2542 else
2543 {
2544 static int made_copy = 0;
2545
2546 // "fc" is still in use. This can happen when returning "a:000",
2547 // assigning "l:" to a global variable or defining a closure.
2548 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002549 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002550 previous_funccal = fc;
2551
2552 if (want_garbage_collect)
2553 // If garbage collector is ready, clear count.
2554 made_copy = 0;
2555 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002556 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002557 // We have made a lot of copies, worth 4 Mbyte. This can happen
2558 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002559 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002560 // too much memory.
2561 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002562 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002563 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002564 }
2565}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002566
2567/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002568 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2569 */
2570 static int
2571numbered_function(char_u *name)
2572{
Keith Thompson184f71c2024-01-04 21:19:04 +01002573 return SAFE_isdigit(*name)
2574 || (name[0] == 'g' && name[1] == ':' && SAFE_isdigit(name[2]));
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002575}
2576
2577/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002578 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002579 * 1. ordinary names, function defined with :function or :def;
2580 * can start with "<SNR>123_" literally or with K_SPECIAL.
2581 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002582 * For the first we only count the name stored in func_hashtab as a reference,
2583 * using function() does not count as a reference, because the function is
2584 * looked up by name.
2585 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002586 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002587func_name_refcount(char_u *name)
2588{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002589 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002590}
2591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592/*
2593 * Unreference "fc": decrement the reference count and free it when it
2594 * becomes zero. "fp" is detached from "fc".
2595 * When "force" is TRUE we are exiting.
2596 */
2597 static void
2598funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2599{
2600 funccall_T **pfc;
2601 int i;
2602
2603 if (fc == NULL)
2604 return;
2605
2606 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002607 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2608 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2609 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2610 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 {
2612 if (fc == *pfc)
2613 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002614 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 free_funccal_contents(fc);
2616 return;
2617 }
2618 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002619 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2620 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2621 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622}
2623
2624/*
2625 * Remove the function from the function hashtable. If the function was
2626 * deleted while it still has references this was already done.
2627 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2628 */
2629 static int
2630func_remove(ufunc_T *fp)
2631{
2632 hashitem_T *hi;
2633
2634 // Return if it was already virtually deleted.
2635 if (fp->uf_flags & FC_DEAD)
2636 return FALSE;
2637
2638 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002639 if (HASHITEM_EMPTY(hi))
2640 return FALSE;
2641
2642 // When there is a def-function index do not actually remove the
2643 // function, so we can find the index when defining the function again.
2644 // Do remove it when it's a copy.
2645 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002647 fp->uf_flags |= FC_DEAD;
2648 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002650 hash_remove(&func_hashtab, hi, "remove function");
2651 fp->uf_flags |= FC_DELETED;
2652 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653}
2654
2655 static void
2656func_clear_items(ufunc_T *fp)
2657{
2658 ga_clear_strings(&(fp->uf_args));
2659 ga_clear_strings(&(fp->uf_def_args));
2660 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002662 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002663 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002664 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002665
2666 // Increment the refcount of this function to avoid it being freed
2667 // recursively when the partial is freed.
2668 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002669 partial_unref(fp->uf_partial);
2670 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002671 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002672
2673#ifdef FEAT_LUA
2674 if (fp->uf_cb_free != NULL)
2675 {
2676 fp->uf_cb_free(fp->uf_cb_state);
2677 fp->uf_cb_free = NULL;
2678 }
2679
2680 fp->uf_cb_state = NULL;
2681 fp->uf_cb = NULL;
2682#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683#ifdef FEAT_PROFILE
2684 VIM_CLEAR(fp->uf_tml_count);
2685 VIM_CLEAR(fp->uf_tml_total);
2686 VIM_CLEAR(fp->uf_tml_self);
2687#endif
2688}
2689
2690/*
2691 * Free all things that a function contains. Does not free the function
2692 * itself, use func_free() for that.
2693 * When "force" is TRUE we are exiting.
2694 */
2695 static void
2696func_clear(ufunc_T *fp, int force)
2697{
2698 if (fp->uf_cleared)
2699 return;
2700 fp->uf_cleared = TRUE;
2701
2702 // clear this function
2703 func_clear_items(fp);
2704 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002705 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706}
2707
2708/*
2709 * Free a function and remove it from the list of functions. Does not free
2710 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002711 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002712 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002714 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002715func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716{
2717 // Only remove it when not done already, otherwise we would remove a newer
2718 // version of the function with the same name.
2719 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2720 func_remove(fp);
2721
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002722 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002723 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002724 if (fp->uf_dfunc_idx > 0)
2725 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002726 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002728 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002729 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002730 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731}
2732
2733/*
2734 * Free all things that a function contains and free the function itself.
2735 * When "force" is TRUE we are exiting.
2736 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002737 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738func_clear_free(ufunc_T *fp, int force)
2739{
2740 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002741 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2742 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002743 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002744 else
2745 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746}
2747
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002748/*
2749 * Copy already defined function "lambda" to a new function with name "global".
2750 * This is for when a compiled function defines a global function.
2751 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002752 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002753copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002754 char_u *lambda,
2755 char_u *global,
2756 loopvarinfo_T *loopvarinfo,
2757 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002758{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002759 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002760 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002761
2762 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002763 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002764 semsg(_(e_lambda_function_not_found_str), lambda);
2765 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002766 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002767
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002768 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002769 if (fp != NULL)
2770 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002771 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002772 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002773 return FAIL;
2774 }
2775
Bram Moolenaare01e5212023-01-08 20:31:18 +00002776 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002777 if (fp == NULL)
2778 return FAIL;
2779
2780 fp->uf_varargs = ufunc->uf_varargs;
2781 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2782 fp->uf_def_status = ufunc->uf_def_status;
2783 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2784 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2785 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2786 == FAIL
2787 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2788 goto failed;
2789
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002790 if (ufunc->uf_arg_types != NULL)
2791 {
2792 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2793 if (fp->uf_arg_types == NULL)
2794 goto failed;
2795 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2796 sizeof(type_T *) * fp->uf_args.ga_len);
2797 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002798 if (ufunc->uf_va_name != NULL)
2799 {
2800 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2801 if (fp->uf_va_name == NULL)
2802 goto failed;
2803 }
2804 fp->uf_ret_type = ufunc->uf_ret_type;
2805
2806 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002807
2808 fp->uf_name_exp = NULL;
2809 set_ufunc_name(fp, global);
2810
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002811 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002812
2813 // the referenced dfunc_T is now used one more time
2814 link_def_function(fp);
2815
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002816 // Create a partial to store the context of the function where it was
2817 // instantiated. Only needs to be done once. Do this on the original
2818 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002819 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2820 {
2821 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2822
2823 if (pt == NULL)
2824 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002825 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002826 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002827 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002828 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002829 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002830 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002831 }
2832
2833 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002834
2835failed:
2836 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002837 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002838}
2839
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002840static int funcdepth = 0;
2841
2842/*
2843 * Increment the function call depth count.
2844 * Return FAIL when going over 'maxfuncdepth'.
2845 * Otherwise return OK, must call funcdepth_decrement() later!
2846 */
2847 int
2848funcdepth_increment(void)
2849{
2850 if (funcdepth >= p_mfd)
2851 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002852 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002853 return FAIL;
2854 }
2855 ++funcdepth;
2856 return OK;
2857}
2858
2859 void
2860funcdepth_decrement(void)
2861{
2862 --funcdepth;
2863}
2864
2865/*
2866 * Get the current function call depth.
2867 */
2868 int
2869funcdepth_get(void)
2870{
2871 return funcdepth;
2872}
2873
2874/*
2875 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002876 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002877 * times as funcdepth_increment().
2878 */
2879 void
2880funcdepth_restore(int depth)
2881{
2882 funcdepth = depth;
2883}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002884
2885/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002886 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2887 * "rettv".
2888 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2889 * Returns NULL when allocation fails.
2890 */
2891 funccall_T *
2892create_funccal(ufunc_T *fp, typval_T *rettv)
2893{
2894 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2895
2896 if (fc == NULL)
2897 return NULL;
2898 fc->fc_caller = current_funccal;
2899 current_funccal = fc;
2900 fc->fc_func = fp;
2901 func_ptr_ref(fp);
2902 fc->fc_rettv = rettv;
2903 return fc;
2904}
2905
2906/*
2907 * To be called when returning from a compiled function; restores
2908 * current_funccal.
2909 */
2910 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002911remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002912{
2913 funccall_T *fc = current_funccal;
2914
2915 current_funccal = fc->fc_caller;
2916 free_funccal(fc);
2917}
2918
2919/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002920 * Call a user function.
2921 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002922 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002923call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002924 ufunc_T *fp, // pointer to function
2925 int argcount, // nr of args
2926 typval_T *argvars, // arguments
2927 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002928 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002929 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002930{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002931 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002932 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002933 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002934 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002935 funccall_T *fc;
2936 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002937 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002938 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002939 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002940 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002941 int i;
2942 int ai;
2943 int islambda = FALSE;
2944 char_u numbuf[NUMBUFLEN];
2945 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002946 typval_T *tv_to_free[MAX_FUNC_ARGS];
2947 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002948#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002949 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002950#endif
ichizok7e5fe382023-04-15 13:17:50 +01002951 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002952
Bram Moolenaarb2049902021-01-24 12:53:53 +01002953#ifdef FEAT_PROFILE
2954 CLEAR_FIELD(profile_info);
2955#endif
2956
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002957 // If depth of calling is getting too high, don't execute the function.
2958 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002959 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002960 rettv->v_type = VAR_NUMBER;
2961 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002962 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002963 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964
Bram Moolenaare38eab22019-12-05 21:50:01 +01002965 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002966
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002967 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002968 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002969 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002970 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002971 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002972 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2973 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002974 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002975 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002976
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002977 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002979#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002980 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002981#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002982 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002983#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002984 if (do_profiling == PROF_YES)
2985 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002986#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002987 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002988 if (call_def_function(fp, argcount, argvars, 0,
2989 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2990 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002991 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002992#ifdef FEAT_PROFILE
2993 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002994 || (caller != NULL && caller->uf_profiling)))
2995 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002996#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002997 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002998 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002999 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 }
3001
Bram Moolenaar38453522021-11-28 22:00:12 +00003002 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003003
3004 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01003005 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
3006 * variables with names up to VAR_SHORT_LEN long. This avoids having to
3007 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003008 */
3009 /*
3010 * Init l: variables.
3011 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01003012 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003013 if (selfdict != NULL)
3014 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003015 // Set l:self to "selfdict". Use "name" to avoid a warning from
3016 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003017 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003018 name = v->di_key;
3019 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01003020 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003021 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003022 v->di_tv.v_type = VAR_DICT;
3023 v->di_tv.v_lock = 0;
3024 v->di_tv.vval.v_dict = selfdict;
3025 ++selfdict->dv_refcount;
3026 }
3027
3028 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003029 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003030 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003031 * Set a:000 to a list with room for the "..." arguments.
3032 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01003033 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003034 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01003035 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003036 (varnumber_T)(argcount >= fp->uf_args.ga_len
3037 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01003038 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003039 if ((fp->uf_flags & FC_NOARGS) == 0)
3040 {
3041 // Use "name" to avoid a warning from some compiler that checks the
3042 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003043 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003044 name = v->di_key;
3045 STRCPY(name, "000");
3046 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003047 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003048 v->di_tv.v_type = VAR_LIST;
3049 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003050 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003051 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01003052 CLEAR_FIELD(fc->fc_l_varlist);
3053 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
3054 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003055
3056 /*
3057 * Set a:firstline to "firstline" and a:lastline to "lastline".
3058 * Set a:name to named arguments.
3059 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003060 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003061 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003062 if ((fp->uf_flags & FC_NOARGS) == 0)
3063 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003064 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3065 "firstline", (varnumber_T)funcexe->fe_firstline);
3066 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3067 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003068 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003069 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003070 {
3071 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003072 typval_T def_rettv;
3073 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003074
3075 ai = i - fp->uf_args.ga_len;
3076 if (ai < 0)
3077 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003078 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003079 name = FUNCARG(fp, i);
3080 if (islambda)
3081 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003082
3083 // evaluate named argument default expression
3084 isdefault = ai + fp->uf_def_args.ga_len >= 0
3085 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
3086 && argvars[i].vval.v_number == VVAL_NONE));
3087 if (isdefault)
3088 {
3089 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003090
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003091 def_rettv.v_type = VAR_NUMBER;
3092 def_rettv.vval.v_number = -1;
3093
3094 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
3095 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003096 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003097 {
3098 default_arg_err = 1;
3099 break;
3100 }
3101 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003102 }
3103 else
3104 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003105 if ((fp->uf_flags & FC_NOARGS) != 0)
3106 // Bail out if no a: arguments used (in lambda).
3107 break;
3108
Bram Moolenaare38eab22019-12-05 21:50:01 +01003109 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003110 sprintf((char *)numbuf, "%d", ai + 1);
3111 name = numbuf;
3112 }
3113 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
3114 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003115 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003116 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003117 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003118 }
3119 else
3120 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003121 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003122 if (v == NULL)
3123 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003124 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003125 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003126
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003127 // Note: the values are copied directly to avoid alloc/free.
3128 // "argvars" must have VAR_FIXED for v_lock.
3129 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003130 v->di_tv.v_lock = VAR_FIXED;
3131
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003132 if (isdefault)
3133 // Need to free this later, no matter where it's stored.
3134 tv_to_free[tv_to_free_len++] = &v->di_tv;
3135
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003136 if (addlocal)
3137 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003138 // Named arguments should be accessed without the "a:" prefix in
3139 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003140 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003141 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003142 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003143 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003144 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003145
3146 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3147 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003148 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003149
3150 li->li_tv = argvars[i];
3151 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003152 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003153 }
3154 }
3155
Bram Moolenaare38eab22019-12-05 21:50:01 +01003156 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003157 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003158
3159 if (fp->uf_flags & FC_SANDBOX)
3160 {
3161 using_sandbox = TRUE;
3162 ++sandbox;
3163 }
3164
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003165 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003166 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003167 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003168 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003169 ++no_wait_return;
3170 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003171
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003172 smsg(_("calling %s"), SOURCING_NAME);
3173 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003174 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003175 char_u buf[MSG_BUF_LEN];
3176 char_u numbuf2[NUMBUFLEN];
3177 char_u *tofree;
3178 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003179
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003180 msg_puts("(");
3181 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003182 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003183 if (i > 0)
3184 msg_puts(", ");
3185 if (argvars[i].v_type == VAR_NUMBER)
3186 msg_outnum((long)argvars[i].vval.v_number);
3187 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003188 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003189 // Do not want errors such as E724 here.
3190 ++emsg_off;
3191 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3192 --emsg_off;
3193 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003194 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003195 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003196 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003197 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3198 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003199 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003200 msg_puts((char *)s);
3201 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003202 }
3203 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003204 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003205 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003206 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003207 msg_puts("\n"); // don't overwrite this either
3208
3209 verbose_leave_scroll();
3210 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003211 }
3212#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003213 if (do_profiling == PROF_YES)
3214 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003215 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003216#endif
3217
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003218 // "legacy" does not apply to commands in the function
3219 sticky_cmdmod_flags = 0;
3220
Bram Moolenaar98aff652022-09-06 21:02:35 +01003221 // If called from a compiled :def function the execution context must be
3222 // hidden, any deferred functions need to be added to the function being
3223 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003224 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003225
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003226 save_current_sctx = current_sctx;
3227 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003228 save_did_emsg = did_emsg;
3229 did_emsg = FALSE;
3230
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003231 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003232 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003233 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003234 retval = FCERR_FAILED;
3235 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003236 else if (islambda)
3237 {
3238 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3239
3240 // A Lambda always has the command "return {expr}". It is much faster
3241 // to evaluate {expr} directly.
3242 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003243 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003244 --ex_nesting_level;
3245 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003246 else
3247 // call do_cmdline() to execute the lines
3248 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003249 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3250
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003251 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003252 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003253
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003254 if (RedrawingDisabled > 0)
3255 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003256
Bram Moolenaare38eab22019-12-05 21:50:01 +01003257 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003258 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3259 {
3260 clear_tv(rettv);
3261 rettv->v_type = VAR_NUMBER;
3262 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003263
3264 // In corner cases returning a "failed" value is not backwards
3265 // compatible. Only do this for Vim9 script.
3266 if (in_vim9script())
3267 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003268 }
3269
3270#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003271 if (do_profiling == PROF_YES)
3272 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003273 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003274
3275 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3276 profile_may_end_func(&profile_info, fp, caller);
3277 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003278#endif
3279
Bram Moolenaare38eab22019-12-05 21:50:01 +01003280 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003281 if (p_verbose >= 12)
3282 {
3283 ++no_wait_return;
3284 verbose_enter_scroll();
3285
3286 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003287 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003288 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003289 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003290 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003291 else
3292 {
3293 char_u buf[MSG_BUF_LEN];
3294 char_u numbuf2[NUMBUFLEN];
3295 char_u *tofree;
3296 char_u *s;
3297
Bram Moolenaare38eab22019-12-05 21:50:01 +01003298 // The value may be very long. Skip the middle part, so that we
3299 // have some idea how it starts and ends. smsg() would always
3300 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003301 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003302 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003303 --emsg_off;
3304 if (s != NULL)
3305 {
3306 if (vim_strsize(s) > MSG_BUF_CLEN)
3307 {
3308 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3309 s = buf;
3310 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003311 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003312 vim_free(tofree);
3313 }
3314 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003315 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003316
3317 verbose_leave_scroll();
3318 --no_wait_return;
3319 }
3320
ichizok7e5fe382023-04-15 13:17:50 +01003321 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003322 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003323 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003324 restore_current_ectx(save_current_ectx);
3325
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003326#ifdef FEAT_PROFILE
3327 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003328 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003329#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003330 if (using_sandbox)
3331 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003332 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003333
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003334 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003335 {
3336 ++no_wait_return;
3337 verbose_enter_scroll();
3338
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003339 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003340 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003341
3342 verbose_leave_scroll();
3343 --no_wait_return;
3344 }
3345
3346 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003347 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003348 for (i = 0; i < tv_to_free_len; ++i)
3349 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003350 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003351
3352 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003353}
3354
3355/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003356 * Check the argument count for user function "fp".
3357 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3358 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003359 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003360check_user_func_argcount(ufunc_T *fp, int argcount)
3361{
3362 int regular_args = fp->uf_args.ga_len;
3363
3364 if (argcount < regular_args - fp->uf_def_args.ga_len)
3365 return FCERR_TOOFEW;
3366 else if (!has_varargs(fp) && argcount > regular_args)
3367 return FCERR_TOOMANY;
3368 return FCERR_UNKNOWN;
3369}
3370
3371/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003373 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003374 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375call_user_func_check(
3376 ufunc_T *fp,
3377 int argcount,
3378 typval_T *argvars,
3379 typval_T *rettv,
3380 funcexe_T *funcexe,
3381 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003382{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003383 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003384
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003385#ifdef FEAT_LUA
3386 if (fp->uf_flags & FC_CFUNC)
3387 {
3388 cfunc_T cb = fp->uf_cb;
3389
3390 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3391 }
3392#endif
3393
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003394 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3395 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003396 error = check_user_func_argcount(fp, argcount);
3397 if (error != FCERR_UNKNOWN)
3398 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003399
Bram Moolenaar50824712020-12-20 21:10:17 +01003400 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003401 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003403 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003405 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 int did_save_redo = FALSE;
3407 save_redo_T save_redo;
3408
3409 /*
3410 * Call the user function.
3411 * Save and restore search patterns, script variables and
3412 * redo buffer.
3413 */
3414 save_search_patterns();
3415 if (!ins_compl_active())
3416 {
3417 saveRedobuff(&save_redo);
3418 did_save_redo = TRUE;
3419 }
3420 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003421 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003422 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3424 // Function was unreferenced while being used, free it now.
3425 func_clear_free(fp, FALSE);
3426 if (did_save_redo)
3427 restoreRedobuff(&save_redo);
3428 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003429 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003430
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003432}
3433
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003434static funccal_entry_T *funccal_stack = NULL;
3435
3436/*
3437 * Save the current function call pointer, and set it to NULL.
3438 * Used when executing autocommands and for ":source".
3439 */
3440 void
3441save_funccal(funccal_entry_T *entry)
3442{
3443 entry->top_funccal = current_funccal;
3444 entry->next = funccal_stack;
3445 funccal_stack = entry;
3446 current_funccal = NULL;
3447}
3448
3449 void
3450restore_funccal(void)
3451{
3452 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003453 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003454 else
3455 {
3456 current_funccal = funccal_stack->top_funccal;
3457 funccal_stack = funccal_stack->next;
3458 }
3459}
3460
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003461 funccall_T *
3462get_current_funccal(void)
3463{
3464 return current_funccal;
3465}
3466
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003467/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003468 * Return TRUE when currently at the script level:
3469 * - not in a function
3470 * - not executing an autocommand
3471 * Note that when an autocommand sources a script the result is FALSE;
3472 */
3473 int
3474at_script_level(void)
3475{
3476 return current_funccal == NULL && autocmd_match == NULL;
3477}
3478
3479/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003480 * Mark all functions of script "sid" as deleted.
3481 */
3482 void
3483delete_script_functions(int sid)
3484{
3485 hashitem_T *hi;
3486 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003487 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003488 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003489 size_t len;
3490
3491 buf[0] = K_SPECIAL;
3492 buf[1] = KS_EXTRA;
3493 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003494 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003495 len = STRLEN(buf);
3496
Bram Moolenaarce658352020-07-31 23:47:12 +02003497 while (todo > 0)
3498 {
3499 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003500 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003501 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003502 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003503 fp = HI2UF(hi);
3504 if (STRNCMP(fp->uf_name, buf, len) == 0)
3505 {
3506 int changed = func_hashtab.ht_changed;
3507
3508 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003509
3510 if (fp->uf_calls > 0)
3511 {
3512 // Function is executing, don't free it but do remove
3513 // it from the hashtable.
3514 if (func_remove(fp))
3515 fp->uf_refcount--;
3516 }
3517 else
3518 {
3519 func_clear(fp, TRUE);
3520 // When clearing a function another function can be
3521 // cleared as a side effect. When that happens start
3522 // over.
3523 if (changed != func_hashtab.ht_changed)
3524 break;
3525 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003526 }
3527 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003528 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003529 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003530}
3531
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003532#if defined(EXITFREE) || defined(PROTO)
3533 void
3534free_all_functions(void)
3535{
3536 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003537 ufunc_T *fp;
3538 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003539 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003540 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003541
Bram Moolenaare38eab22019-12-05 21:50:01 +01003542 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003543 while (current_funccal != NULL)
3544 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003545 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003546 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003547 if (current_funccal == NULL && funccal_stack != NULL)
3548 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003549 }
3550
Bram Moolenaare38eab22019-12-05 21:50:01 +01003551 // First clear what the functions contain. Since this may lower the
3552 // reference count of a function, it may also free a function and change
3553 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003554 while (todo > 0)
3555 {
3556 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003557 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003558 if (!HASHITEM_EMPTY(hi))
3559 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 // clear the def function index now
3561 fp = HI2UF(hi);
3562 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003563 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564
Bram Moolenaare38eab22019-12-05 21:50:01 +01003565 // Only free functions that are not refcounted, those are
3566 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003567 if (func_name_refcount(fp->uf_name))
3568 ++skipped;
3569 else
3570 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003571 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003572 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003573 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003574 {
3575 skipped = 0;
3576 break;
3577 }
3578 }
3579 --todo;
3580 }
3581 }
3582
Bram Moolenaare38eab22019-12-05 21:50:01 +01003583 // Now actually free the functions. Need to start all over every time,
3584 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003585 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003586 while (func_hashtab.ht_used > skipped)
3587 {
3588 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003589 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003590 if (!HASHITEM_EMPTY(hi))
3591 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003592 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003593 // Only free functions that are not refcounted, those are
3594 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003595 fp = HI2UF(hi);
3596 if (func_name_refcount(fp->uf_name))
3597 ++skipped;
3598 else
3599 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003600 if (func_free(fp, FALSE) == OK)
3601 {
3602 skipped = 0;
3603 break;
3604 }
3605 // did not actually free it
3606 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003607 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003608 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003609 }
3610 if (skipped == 0)
3611 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612
3613 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003614}
3615#endif
3616
3617/*
3618 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003619 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3620 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003621 * "len" is the length of "name", or -1 for NUL terminated.
3622 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003624builtin_function(char_u *name, int len)
3625{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003626 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003627
Bram Moolenaara26b9702020-04-18 19:53:28 +02003628 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003629 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003630 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3631 {
3632 if (name[i] == AUTOLOAD_CHAR)
3633 return FALSE;
3634 if (!eval_isnamec(name[i]))
3635 {
3636 // "name.something" is not a builtin function
3637 if (name[i] == '.')
3638 return FALSE;
3639 break;
3640 }
3641 }
3642 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003643}
3644
3645 int
3646func_call(
3647 char_u *name,
3648 typval_T *args,
3649 partial_T *partial,
3650 dict_T *selfdict,
3651 typval_T *rettv)
3652{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003653 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003654 listitem_T *item;
3655 typval_T argv[MAX_FUNC_ARGS + 1];
3656 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003657 int r = 0;
3658
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003659 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003660 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003661 {
3662 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3663 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003664 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003665 break;
3666 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003667 // Make a copy of each argument. This is needed to be able to set
3668 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003669 copy_tv(&item->li_tv, &argv[argc++]);
3670 }
3671
3672 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003673 {
3674 funcexe_T funcexe;
3675
Bram Moolenaara80faa82020-04-12 19:37:17 +02003676 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003677 funcexe.fe_firstline = curwin->w_cursor.lnum;
3678 funcexe.fe_lastline = curwin->w_cursor.lnum;
3679 funcexe.fe_evaluate = TRUE;
3680 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003681 if (partial != NULL)
3682 {
3683 funcexe.fe_object = partial->pt_obj;
3684 if (funcexe.fe_object != NULL)
3685 ++funcexe.fe_object->obj_refcount;
3686 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003687 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003688 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3689 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003690
Bram Moolenaare38eab22019-12-05 21:50:01 +01003691 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003692 while (argc > 0)
3693 clear_tv(&argv[--argc]);
3694
3695 return r;
3696}
3697
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003698static int callback_depth = 0;
3699
3700 int
3701get_callback_depth(void)
3702{
3703 return callback_depth;
3704}
3705
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003706/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003707 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003708 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003709 */
3710 int
3711call_callback(
3712 callback_T *callback,
3713 int len, // length of "name" or -1 to use strlen()
3714 typval_T *rettv, // return value goes here
3715 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003716 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003717 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003718{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003719 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003720 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003721
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003722 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3723 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003724
zeertzjqfe583b12023-12-21 16:59:26 +01003725 if (callback_depth > p_mfd)
Christian Brabandt47510f32023-10-15 09:56:16 +02003726 {
3727 emsg(_(e_command_too_recursive));
3728 return FAIL;
3729 }
3730
Bram Moolenaara80faa82020-04-12 19:37:17 +02003731 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003732 funcexe.fe_evaluate = TRUE;
3733 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003734 if (callback->cb_partial != NULL)
3735 {
3736 funcexe.fe_object = callback->cb_partial->pt_obj;
3737 if (funcexe.fe_object != NULL)
3738 ++funcexe.fe_object->obj_refcount;
3739 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003740 ++callback_depth;
3741 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3742 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003743
3744 // When a :def function was called that uses :try an error would be turned
3745 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003746 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003747 {
3748 need_rethrow = FALSE;
3749 handle_did_throw();
3750 }
3751
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003752 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003753}
3754
3755/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003756 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003757 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003758 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3759 */
3760 varnumber_T
3761call_callback_retnr(
3762 callback_T *callback,
3763 int argcount, // number of "argvars"
3764 typval_T *argvars) // vars for arguments, must have "argcount"
3765 // PLUS ONE elements!
3766{
3767 typval_T rettv;
3768 varnumber_T retval;
3769
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003770 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003771 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003772
3773 retval = tv_get_number_chk(&rettv, NULL);
3774 clear_tv(&rettv);
3775 return retval;
3776}
3777
3778/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 * Give an error message for the result of a function.
3780 * Nothing if "error" is FCERR_NONE.
3781 */
3782 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003783user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784{
3785 switch (error)
3786 {
3787 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003788 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003789 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003790 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003791 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 break;
3793 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003794 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 break;
3796 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003797 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 break;
3799 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003800 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 break;
3802 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003803 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 break;
3805 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003806 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 break;
3808 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003809 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3810 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003812 case FCERR_OTHER:
3813 case FCERR_FAILED:
3814 // assume the error message was already given
3815 break;
3816 case FCERR_NONE:
3817 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 }
3819}
3820
3821/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003822 * Check the argument types "argvars[argcount]" for "name" using the
3823 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3824 * is already included in "argvars[]".
3825 * Will do nothing if "funcexe->fe_check_type" is NULL or
3826 * "funcexe->fe_evaluate" is FALSE;
3827 * Returns an FCERR_ value.
3828 */
3829 static funcerror_T
3830may_check_argument_types(
3831 funcexe_T *funcexe,
3832 typval_T *argvars,
3833 int argcount,
3834 int base_included,
3835 char_u *name)
3836{
3837 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3838 {
3839 // Check that the argument types are OK for the types of the funcref.
3840 if (check_argument_types(funcexe->fe_check_type,
3841 argvars, argcount,
3842 base_included ? NULL : funcexe->fe_basetv,
3843 name) == FAIL)
3844 return FCERR_OTHER;
3845 }
3846 return FCERR_NONE;
3847}
3848
3849/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003850 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003851 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003852 * Return FAIL when the function can't be called, OK otherwise.
3853 * Also returns OK when an error was encountered while executing the function.
3854 */
3855 int
3856call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003857 char_u *funcname, // name of the function
3858 int len, // length of "name" or -1 to use strlen()
3859 typval_T *rettv, // return value goes here
3860 int argcount_in, // number of "argvars"
3861 typval_T *argvars_in, // vars for arguments, must have "argcount"
3862 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003863 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003864{
3865 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003866 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003867 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003868 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003869 char_u fname_buf[FLEN_FIXED + 1];
3870 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003871 char_u *fname = NULL;
3872 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003873 int argcount = argcount_in;
3874 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003875 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003876 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003877 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003878 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003879 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003880 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003881 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003882 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003883
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003884 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3885 // even when call_func() returns FAIL.
3886 rettv->v_type = VAR_UNKNOWN;
3887
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003888 if (partial != NULL)
3889 fp = partial->pt_func;
3890 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003891 fp = funcexe->fe_ufunc;
3892
3893 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003894 {
3895 // Make a copy of the name, if it comes from a funcref variable it
3896 // could be changed or deleted in the called function.
3897 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3898 if (name == NULL)
3899 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003900
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003901 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3902 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003903
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003904 if (funcexe->fe_doesrange != NULL)
3905 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003906
3907 if (partial != NULL)
3908 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003909 // When the function has a partial with a dict and there is a dict
3910 // argument, use the dict argument. That is backwards compatible.
3911 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003912 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003913 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003914 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003915 {
3916 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003917 {
3918 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3919 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003920 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003921 goto theend;
3922 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003923 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003924 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003925 for (i = 0; i < argcount_in; ++i)
3926 argv[i + argv_clear] = argvars_in[i];
3927 argvars = argv;
3928 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003929
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003930 if (funcexe->fe_check_type != NULL
3931 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003932 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003933 // Now funcexe->fe_check_type is missing the added arguments,
3934 // make a copy of the type with the correction.
3935 check_type = *funcexe->fe_check_type;
3936 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003937 check_type.tt_args = check_type_args;
3938 CLEAR_FIELD(check_type_args);
3939 for (i = 0; i < check_type.tt_argcount; ++i)
3940 check_type_args[i + partial->pt_argc] =
3941 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003942 check_type.tt_argcount += partial->pt_argc;
3943 check_type.tt_min_argcount += partial->pt_argc;
3944 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003945 }
3946 }
3947
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003948 if (error == FCERR_NONE)
3949 // check the argument types if possible
3950 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3951 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003952
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003953 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003954 {
3955 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003956 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003957
Bram Moolenaar333894b2020-08-01 18:53:07 +02003958 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003959 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003960 {
3961 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003962 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003963 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003964
Bram Moolenaare38eab22019-12-05 21:50:01 +01003965 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003966 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003967 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003968
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003969 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003970 {
3971 /*
3972 * User defined function.
3973 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003974 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003975 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003976 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003977 if (fp != NULL && !is_global && in_vim9script()
3978 && func_requires_g_prefix(fp))
3979 // In Vim9 script g: is required to find a global
3980 // non-autoload function.
3981 fp = NULL;
3982 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003983
Bram Moolenaare38eab22019-12-05 21:50:01 +01003984 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003985 if (fp == NULL
3986 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003987 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003988 && !aborting())
3989 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003990 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003991 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003992 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003993 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003994 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3995 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003996 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003997 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003998 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003999 if (fp == NULL)
4000 {
4001 char_u *p = untrans_function_name(rfname);
4002
4003 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02004004 // Don't do this if the name starts with "s:".
4005 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004006 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004007 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004008
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004009 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01004010 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004011 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004012 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004013 int need_arg_check = FALSE;
4014 if (funcexe->fe_check_type == NULL)
4015 {
4016 funcexe->fe_check_type = fp->uf_func_type;
4017 need_arg_check = TRUE;
4018 }
4019
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004020 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004021 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01004022 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004023 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004024 argv_clear, fp);
4025 need_arg_check = TRUE;
4026 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02004027
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004028 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004029 {
4030 // Method call: base->Method()
4031 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004032 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004033 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004034 argvars = argv;
4035 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004036 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004037 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004038
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004039 // Check the argument types now that the function type and all
4040 // argument values are known, if not done above.
4041 if (need_arg_check)
4042 error = may_check_argument_types(funcexe, argvars, argcount,
4043 TRUE, (name != NULL) ? name : funcname);
4044 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
4045 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004047 }
4048 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004049 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004050 {
4051 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004052 * expr->method(): Find the method name in the table, call its
4053 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02004054 */
4055 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004056 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004057 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004058 else
4059 {
4060 /*
4061 * Find the function name in the table, call its implementation.
4062 */
4063 error = call_internal_func(fname, argcount, argvars, rettv);
4064 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02004065
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004066 /*
4067 * The function call (or "FuncUndefined" autocommand sequence) might
4068 * have been aborted by an error, an interrupt, or an explicitly thrown
4069 * exception that has not been caught so far. This situation can be
4070 * tested for by calling aborting(). For an error in an internal
4071 * function or for the "E132" error in call_user_func(), however, the
4072 * throw point at which the "force_abort" flag (temporarily reset by
4073 * emsg()) is normally updated has not been reached yet. We need to
4074 * update that flag first to make aborting() reliable.
4075 */
4076 update_force_abort();
4077 }
Bram Moolenaaref140542019-12-31 21:27:13 +01004078 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004079 ret = OK;
4080
Bram Moolenaar4c054e92019-11-10 00:13:50 +01004081theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004082 /*
4083 * Report an error unless the argument evaluation or function call has been
4084 * cancelled due to an aborting error, an interrupt, or an exception.
4085 */
4086 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004087 user_func_error(error, (name != NULL) ? name : funcname,
4088 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004089
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004090 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004091 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004092 clear_tv(&argv[--argv_clear + argv_base]);
4093
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004094 vim_free(tofree);
4095 vim_free(name);
4096
4097 return ret;
4098}
4099
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004100/*
4101 * Call a function without arguments, partial or dict.
4102 * This is like call_func() when the call is only "FuncName()".
4103 * To be used by "expr" options.
4104 * Returns NOTDONE when the function could not be found.
4105 */
4106 int
4107call_simple_func(
4108 char_u *funcname, // name of the function
zeertzjqc1ed7882024-08-01 22:46:54 +02004109 size_t len, // length of "name"
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004110 typval_T *rettv) // return value goes here
4111{
4112 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00004113 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004114 char_u fname_buf[FLEN_FIXED + 1];
4115 char_u *tofree = NULL;
4116 char_u *name;
4117 char_u *fname;
4118 char_u *rfname;
4119 int is_global = FALSE;
4120 ufunc_T *fp;
4121
4122 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4123 rettv->vval.v_number = 0;
4124
4125 // Make a copy of the name, an option can be changed in the function.
4126 name = vim_strnsave(funcname, len);
4127 if (name == NULL)
4128 return ret;
4129
4130 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4131
4132 // Skip "g:" before a function name.
4133 if (fname[0] == 'g' && fname[1] == ':')
4134 {
4135 is_global = TRUE;
4136 rfname = fname + 2;
4137 }
4138 else
4139 rfname = fname;
4140 fp = find_func(rfname, is_global);
4141 if (fp != NULL && !is_global && in_vim9script()
4142 && func_requires_g_prefix(fp))
4143 // In Vim9 script g: is required to find a global non-autoload
4144 // function.
4145 fp = NULL;
4146 if (fp == NULL)
4147 ret = NOTDONE;
4148 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4149 error = FCERR_DELETED;
4150 else if (fp != NULL)
4151 {
4152 typval_T argvars[1];
4153 funcexe_T funcexe;
4154
4155 argvars[0].v_type = VAR_UNKNOWN;
4156 CLEAR_FIELD(funcexe);
4157 funcexe.fe_evaluate = TRUE;
4158
4159 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4160 if (error == FCERR_NONE)
4161 ret = OK;
4162 }
4163
4164 user_func_error(error, name, FALSE);
4165 vim_free(tofree);
4166 vim_free(name);
4167
4168 return ret;
4169}
4170
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004171 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172printable_func_name(ufunc_T *fp)
4173{
4174 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4175}
4176
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004177/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004178 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4179 * TRUE. Otherwise return FALSE.
4180 */
4181 static int
4182function_list_modified(int prev_ht_changed)
4183{
4184 if (prev_ht_changed != func_hashtab.ht_changed)
4185 {
4186 emsg(_(e_function_list_was_modified));
4187 return TRUE;
4188 }
4189 return FALSE;
4190}
4191
4192/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004193 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004194 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004195 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004196list_func_head(ufunc_T *fp, int indent)
4197{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004198 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004199 int j;
4200
4201 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004202
4203 // a timer at the more prompt may have deleted the function
4204 if (function_list_modified(prev_ht_changed))
4205 return FAIL;
4206
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004207 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004208 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004209 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004210 msg_puts("def ");
4211 else
4212 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004214 msg_putchar('(');
4215 for (j = 0; j < fp->uf_args.ga_len; ++j)
4216 {
4217 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004218 msg_puts(", ");
4219 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 if (fp->uf_arg_types != NULL)
4221 {
4222 char *tofree;
4223
4224 msg_puts(": ");
4225 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4226 vim_free(tofree);
4227 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004228 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4229 {
4230 msg_puts(" = ");
4231 msg_puts(((char **)(fp->uf_def_args.ga_data))
4232 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4233 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004234 }
4235 if (fp->uf_varargs)
4236 {
4237 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004238 msg_puts(", ");
4239 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 if (fp->uf_va_name != NULL)
4242 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004243 if (!fp->uf_varargs)
4244 {
4245 if (j)
4246 msg_puts(", ");
4247 msg_puts("...");
4248 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004250 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251 {
4252 char *tofree;
4253
4254 msg_puts(": ");
4255 msg_puts(type_name(fp->uf_va_type, &tofree));
4256 vim_free(tofree);
4257 }
4258 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004259 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004260
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004261 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004262 {
4263 if (fp->uf_ret_type != &t_void)
4264 {
4265 char *tofree;
4266
4267 msg_puts(": ");
4268 msg_puts(type_name(fp->uf_ret_type, &tofree));
4269 vim_free(tofree);
4270 }
4271 }
4272 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004273 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004274 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004275 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004276 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004277 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004278 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004279 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004280 msg_clr_eos();
4281 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004282 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004283
4284 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004285}
4286
4287/*
4288 * Get a function name, translating "<SID>" and "<SNR>".
4289 * Also handles a Funcref in a List or Dictionary.
4290 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004291 * Set "*is_global" to TRUE when the function must be global, unless
4292 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004293 * flags:
4294 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004295 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004296 * TFN_QUIET: be quiet
4297 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004298 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004299 * Advances "pp" to just after the function name (if no error).
4300 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004301 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004302trans_function_name(
4303 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004304 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004305 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004306 int flags)
4307{
4308 return trans_function_name_ext(pp, is_global, skip, flags,
4309 NULL, NULL, NULL, NULL);
4310}
4311
4312/*
4313 * trans_function_name() with extra arguments.
4314 * "fdp", "partial", "type" and "ufunc" can be NULL.
4315 */
Yegappan Lakshmanana04003a2024-10-27 21:54:11 +01004316 static char_u *
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004317trans_function_name_ext(
4318 char_u **pp,
4319 int *is_global,
4320 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004321 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004322 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004323 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004324 type_T **type, // return: type of funcref
4325 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004326{
4327 char_u *name = NULL;
4328 char_u *start;
4329 char_u *end;
4330 int lead;
4331 char_u sid_buf[20];
4332 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004334 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004335 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004336 int vim9script = in_vim9script();
4337 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004338
4339 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004340 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004341 start = *pp;
4342
Bram Moolenaare38eab22019-12-05 21:50:01 +01004343 // Check for hard coded <SNR>: already translated function ID (from a user
4344 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004345 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4346 && (*pp)[2] == (int)KE_SNR)
4347 {
4348 *pp += 3;
4349 len = get_id_len(pp) + 3;
4350 return vim_strnsave(start, len);
4351 }
4352
Bram Moolenaare38eab22019-12-05 21:50:01 +01004353 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4354 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004355 lead = eval_fname_script(start);
4356 if (lead > 2)
4357 start += lead;
4358
Bram Moolenaare38eab22019-12-05 21:50:01 +01004359 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004360 end = get_lval(start, NULL, &lv, FALSE, skip,
4361 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004362 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004363 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004364 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004365 {
4366 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004367 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004368 goto theend;
4369 }
4370 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4371 {
4372 /*
4373 * Report an invalid expression in braces, unless the expression
4374 * evaluation has been cancelled due to an aborting error, an
4375 * interrupt, or an exception.
4376 */
4377 if (!aborting())
4378 {
4379 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004380 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004381 }
4382 else
4383 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4384 goto theend;
4385 }
4386
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004387 if (lv.ll_ufunc != NULL)
4388 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004389 if (ufunc != NULL)
4390 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004391 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004392 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004393 goto theend;
4394 }
4395
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004396 if (lv.ll_tv != NULL)
4397 {
4398 if (fdp != NULL)
4399 {
4400 fdp->fd_dict = lv.ll_dict;
4401 fdp->fd_newkey = lv.ll_newkey;
4402 lv.ll_newkey = NULL;
4403 fdp->fd_di = lv.ll_di;
4404 }
4405 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4406 {
4407 name = vim_strsave(lv.ll_tv->vval.v_string);
4408 *pp = end;
4409 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004410 else if (lv.ll_tv->v_type == VAR_CLASS
4411 && lv.ll_tv->vval.v_class != NULL)
4412 {
4413 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4414 *pp = end;
4415 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004416 else if (lv.ll_tv->v_type == VAR_PARTIAL
4417 && lv.ll_tv->vval.v_partial != NULL)
4418 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004419 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004420 *pp = end;
4421 if (partial != NULL)
4422 *partial = lv.ll_tv->vval.v_partial;
4423 }
4424 else
4425 {
4426 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4427 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004428 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004429 else
4430 *pp = end;
4431 name = NULL;
4432 }
4433 goto theend;
4434 }
4435
4436 if (lv.ll_name == NULL)
4437 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004438 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004439 *pp = end;
4440 goto theend;
4441 }
4442
Bram Moolenaare38eab22019-12-05 21:50:01 +01004443 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004444 if (lv.ll_exp_name != NULL)
4445 {
4446 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004447 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004448 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004449 if (name == lv.ll_exp_name)
4450 name = NULL;
4451 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004452 else if (lv.ll_sid > 0)
4453 {
4454 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4455 int cc = *lv.ll_name_end;
4456
4457 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4458 *lv.ll_name_end = NUL;
4459 if (si->sn_autoload_prefix != NULL)
4460 {
4461 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4462 }
4463 else
4464 {
4465 sid_buf[0] = K_SPECIAL;
4466 sid_buf[1] = KS_EXTRA;
4467 sid_buf[2] = (int)KE_SNR;
4468 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004469 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004470 name = concat_str(sid_buf, lv.ll_name);
4471 }
4472 *lv.ll_name_end = cc;
4473 *pp = end;
4474 goto theend;
4475 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004476 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004477 {
4478 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004479 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004480 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004481 if (name == *pp)
4482 name = NULL;
4483 }
4484 if (name != NULL)
4485 {
4486 name = vim_strsave(name);
4487 *pp = end;
4488 if (STRNCMP(name, "<SNR>", 5) == 0)
4489 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004490 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004491 name[0] = K_SPECIAL;
4492 name[1] = KS_EXTRA;
4493 name[2] = (int)KE_SNR;
4494 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4495 }
4496 goto theend;
4497 }
4498
4499 if (lv.ll_exp_name != NULL)
4500 {
4501 len = (int)STRLEN(lv.ll_exp_name);
4502 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4503 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4504 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004505 // When there was "s:" already or the name expanded to get a
4506 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004507 lv.ll_name += 2;
4508 len -= 2;
4509 lead = 2;
4510 }
4511 }
4512 else
4513 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004514 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004515 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004516 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004517 if (lv.ll_name[0] == 'g')
4518 {
4519 if (is_global != NULL)
4520 {
4521 *is_global = TRUE;
4522 }
4523 else
4524 {
4525 // dropping "g:" without setting "is_global" won't work in
4526 // Vim9script, put it back later
4527 prefix_g = TRUE;
4528 extra = 2;
4529 }
4530 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004531 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004532 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004533 len = (int)(end - lv.ll_name);
4534 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004535 if (len <= 0)
4536 {
4537 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004538 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004539 goto theend;
4540 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004541
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004542 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004543 // starts with a lower case character: dict.func(). Or when in a class.
4544 vim9_local = ASCII_ISUPPER(*start) && vim9script
4545 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004547 /*
4548 * Copy the function name to allocated memory.
4549 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4550 * Accept <SNR>123_name() outside a script.
4551 */
4552 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004553 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004554 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004555 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004556 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004557 {
Kota Kato948a3892022-08-16 16:09:59 +01004558 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4559 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004560 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004561 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004562 goto theend;
4563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004565 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004566 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004568 || eval_fname_sid(*pp))
4569 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004571 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004572 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004573 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004574 goto theend;
4575 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004576 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004577 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 extra = 3 + (int)STRLEN(sid_buf);
4579 else
4580 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004581 }
4582 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004583 // The function name must start with an upper case letter (unless it is a
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004584 // Vim9 class new() function or a Vim9 class private method or one of the
4585 // supported Vim9 object builtin functions)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004586 else if (!(flags & TFN_INT)
4587 && (builtin_function(lv.ll_name, len)
4588 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004589 && !((flags & TFN_IN_CLASS)
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004590 && (is_valid_builtin_obj_methodname(lv.ll_name)
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004591 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004592 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004593 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004594 : e_function_name_must_start_with_capital_or_s_str),
4595 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004596 goto theend;
4597 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004598 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004599 {
4600 char_u *cp = vim_strchr(lv.ll_name, ':');
4601
4602 if (cp != NULL && cp < end)
4603 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004604 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004605 goto theend;
4606 }
4607 }
4608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004610 if (name != NULL)
4611 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004612 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004613 {
4614 name[0] = K_SPECIAL;
4615 name[1] = KS_EXTRA;
4616 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004617 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004618 STRCPY(name + 3, sid_buf);
4619 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004620 else if (prefix_g)
4621 {
4622 name[0] = 'g';
4623 name[1] = ':';
4624 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4626 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004627 }
4628 *pp = end;
4629
4630theend:
4631 clear_lval(&lv);
4632 return name;
4633}
4634
4635/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004636 * Assuming "name" is the result of trans_function_name() and it was prefixed
4637 * to use the script-local name, return the unmodified name (points into
4638 * "name"). Otherwise return NULL.
4639 * This can be used to first search for a script-local function and fall back
4640 * to the global function if not found.
4641 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004642 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004643untrans_function_name(char_u *name)
4644{
4645 char_u *p;
4646
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004647 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004648 {
4649 p = vim_strchr(name, '_');
4650 if (p != NULL)
4651 return p + 1;
4652 }
4653 return NULL;
4654}
4655
4656/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004657 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4658 * current script ID and returns the expanded function name. The caller should
4659 * free the returned name. If not called from a script context or the function
4660 * name doesn't start with these prefixes, then returns NULL.
4661 * This doesn't check whether the script-local function exists or not.
4662 */
4663 char_u *
4664get_scriptlocal_funcname(char_u *funcname)
4665{
4666 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004667 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004668 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004669 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004670
4671 if (funcname == NULL)
4672 return NULL;
4673
4674 if (STRNCMP(funcname, "s:", 2) != 0
4675 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004676 {
4677 ufunc_T *ufunc;
4678
4679 // The function name does not have a script-local prefix. Try finding
4680 // it when in a Vim9 script and there is no "g:" prefix.
4681 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4682 return NULL;
4683 ufunc = find_func(funcname, FALSE);
4684 if (ufunc == NULL || func_is_global(ufunc)
4685 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4686 return NULL;
4687 ++p;
4688 off = 0;
4689 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004690 else
4691 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004692
4693 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4694 {
4695 emsg(_(e_using_sid_not_in_script_context));
4696 return NULL;
4697 }
4698 // Expand s: prefix into <SNR>nr_<name>
4699 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4700 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004701 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004702 if (newname == NULL)
4703 return NULL;
4704 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004705 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004706
4707 return newname;
4708}
4709
4710/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004711 * Return script-local "fname" with the 3-byte sequence replaced by
4712 * printable <SNR> in allocated memory.
4713 */
4714 char_u *
4715alloc_printable_func_name(char_u *fname)
4716{
4717 char_u *n = alloc(STRLEN(fname + 3) + 6);
4718
4719 if (n != NULL)
4720 {
4721 STRCPY(n, "<SNR>");
4722 STRCPY(n + 5, fname + 3);
4723 }
4724 return n;
4725}
4726
4727/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004728 * Call trans_function_name(), except that a lambda is returned as-is.
4729 * Returns the name in allocated memory.
4730 */
4731 char_u *
4732save_function_name(
4733 char_u **name,
4734 int *is_global,
4735 int skip,
4736 int flags,
4737 funcdict_T *fudi)
4738{
4739 char_u *p = *name;
4740 char_u *saved;
4741
4742 if (STRNCMP(p, "<lambda>", 8) == 0)
4743 {
4744 p += 8;
4745 (void)getdigits(&p);
4746 saved = vim_strnsave(*name, p - *name);
4747 if (fudi != NULL)
4748 CLEAR_POINTER(fudi);
4749 }
4750 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004751 saved = trans_function_name_ext(&p, is_global, skip,
4752 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004753 *name = p;
4754 return saved;
4755}
4756
4757/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004758 * List functions. When "regmatch" is NULL all of then.
4759 * Otherwise functions matching "regmatch".
4760 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004761 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004762list_functions(regmatch_T *regmatch)
4763{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004764 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004765 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004766 hashitem_T *hi;
4767
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004768 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004769 {
4770 if (!HASHITEM_EMPTY(hi))
4771 {
4772 ufunc_T *fp = HI2UF(hi);
4773
4774 --todo;
4775 if ((fp->uf_flags & FC_DEAD) == 0
4776 && (regmatch == NULL
4777 ? !message_filtered(fp->uf_name)
4778 && !func_name_refcount(fp->uf_name)
Keith Thompson184f71c2024-01-04 21:19:04 +01004779 : !SAFE_isdigit(*fp->uf_name)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004780 && vim_regexec(regmatch, fp->uf_name, 0)))
4781 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004782 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004783 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004784 if (function_list_modified(prev_ht_changed))
4785 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004786 }
4787 }
4788 }
4789}
4790
4791/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004792 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004793 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4794 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004795 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004796 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4797 * With CF_INTERFACE the function is define inside an interface, only the
4798 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004799 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004800 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004801 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004802define_function(
4803 exarg_T *eap,
4804 char_u *name_arg,
4805 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004806 int class_flags,
4807 ocmember_T *obj_members,
4808 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004809{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004810 int j;
4811 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004812 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004813 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004814 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004815 char_u *p;
4816 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004817 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004818 char_u *line_arg = NULL;
4819 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004820 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004821 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004822 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004823 garray_T newlines;
4824 int varargs = FALSE;
4825 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004826 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004827 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004828 int fp_allocated = FALSE;
4829 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004830 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004831 dictitem_T *v;
4832 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004833 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004834 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004835 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004836 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004837 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004838 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004839
4840 /*
4841 * ":function" without argument: list functions.
4842 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004843 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004844 {
4845 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004846 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004847 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004848 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004849 }
4850
4851 /*
4852 * ":function /pat": list functions matching pattern.
4853 */
4854 if (*eap->arg == '/')
4855 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004856 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004857 if (!eap->skip)
4858 {
4859 regmatch_T regmatch;
4860
4861 c = *p;
4862 *p = NUL;
4863 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4864 *p = c;
4865 if (regmatch.regprog != NULL)
4866 {
4867 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004868 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004869 vim_regfree(regmatch.regprog);
4870 }
4871 }
4872 if (*p == '/')
4873 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004874 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004875 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004876 }
4877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 ga_init(&newargs);
4879 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004880 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 ga_init(&default_args);
4882
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004883 /*
4884 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004885 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004886 * "name" == func, "fudi.fd_dict" == NULL
4887 * dict.func new dictionary entry
4888 * "name" == NULL, "fudi.fd_dict" set,
4889 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4890 * dict.func existing dict entry with a Funcref
4891 * "name" == func, "fudi.fd_dict" set,
4892 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4893 * dict.func existing dict entry that's not a Funcref
4894 * "name" == NULL, "fudi.fd_dict" set,
4895 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4896 * s:func script-local function name
4897 * g:func global function name, same as "func"
4898 */
4899 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004900 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004901 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004902 // nested function, argument is (args).
4903 paren = TRUE;
4904 CLEAR_FIELD(fudi);
4905 }
4906 else
4907 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004908 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004909 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004910 if (p[0] == 's' && p[1] == ':')
4911 {
4912 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4913 return NULL;
4914 }
4915 p = to_name_end(p, TRUE);
4916 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4917 {
4918 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4919 eap->arg);
4920 return NULL;
4921 }
4922 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004923 }
4924
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004925 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004926 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004927 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004928 paren = (vim_strchr(p, '(') != NULL);
4929 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004930 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004931 /*
4932 * Return on an invalid expression in braces, unless the expression
4933 * evaluation has been cancelled due to an aborting error, an
4934 * interrupt, or an exception.
4935 */
4936 if (!aborting())
4937 {
4938 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004939 semsg(_(e_key_not_present_in_dictionary_str),
4940 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004941 vim_free(fudi.fd_newkey);
4942 return NULL;
4943 }
4944 else
4945 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004946 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004947
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004948 // For "export def FuncName()" in an autoload script the function name
4949 // is stored with the legacy autoload name "dir#script#FuncName" so
4950 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004951 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004952 {
4953 char_u *prefixed = may_prefix_autoload(name);
4954
4955 if (prefixed != NULL && prefixed != name)
4956 {
4957 vim_free(name);
4958 name = prefixed;
4959 }
4960 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004961 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004962 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004963 {
4964 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4965 goto ret_free;
4966 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004967 }
4968
Bram Moolenaare38eab22019-12-05 21:50:01 +01004969 // An error in a function call during evaluation of an expression in magic
4970 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004971 saved_did_emsg = did_emsg;
4972 did_emsg = FALSE;
4973
4974 /*
4975 * ":function func" with only function name: list function.
4976 */
4977 if (!paren)
4978 {
4979 if (!ends_excmd(*skipwhite(p)))
4980 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004981 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004982 goto ret_free;
4983 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004984 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004985 if (eap->nextcmd != NULL)
4986 *p = NUL;
4987 if (!eap->skip && !got_int)
4988 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004989 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004990 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4991 {
4992 char_u *up = untrans_function_name(name);
4993
4994 // With Vim9 script the name was made script-local, if not
4995 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004996 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004997 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004998 }
4999
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005000 if (fp != NULL)
5001 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00005002 // Check no function was added or removed from a timer, e.g. at
5003 // the more prompt. "fp" may then be invalid.
5004 int prev_ht_changed = func_hashtab.ht_changed;
5005
5006 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005007 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00005008 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
5009 {
5010 if (FUNCLINE(fp, j) == NULL)
5011 continue;
5012 msg_putchar('\n');
5013 msg_outnum((long)(j + 1));
5014 if (j < 9)
5015 msg_putchar(' ');
5016 if (j < 99)
5017 msg_putchar(' ');
5018 if (function_list_modified(prev_ht_changed))
5019 break;
5020 msg_prt_line(FUNCLINE(fp, j), FALSE);
5021 out_flush(); // show a line at a time
5022 ui_breakcheck();
5023 }
5024 if (!got_int)
5025 {
5026 msg_putchar('\n');
5027 if (!function_list_modified(prev_ht_changed))
5028 {
5029 if (fp->uf_def_status != UF_NOT_COMPILED)
5030 msg_puts(" enddef");
5031 else
5032 msg_puts(" endfunction");
5033 }
5034 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005035 }
5036 }
5037 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00005038 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005039 }
5040 goto ret_free;
5041 }
5042
5043 /*
5044 * ":function name(arg1, arg2)" Define function.
5045 */
5046 p = skipwhite(p);
5047 if (*p != '(')
5048 {
5049 if (!eap->skip)
5050 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005051 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005052 goto ret_free;
5053 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005054 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005055 if (vim_strchr(p, '(') != NULL)
5056 p = vim_strchr(p, '(');
5057 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005058
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005059 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
5060 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01005061 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005062 goto ret_free;
5063 }
5064
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005065 // In Vim9 script only global functions can be redefined.
5066 if (vim9script && eap->forceit && !is_global)
5067 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005068 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005069 goto ret_free;
5070 }
5071
Bram Moolenaar04935fb2022-01-08 16:19:22 +00005072 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005073
Bram Moolenaar04b12692020-05-04 23:24:44 +02005074 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005075 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005076 // Check the name of the function. Unless it's a dictionary function
5077 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005078 if (name != NULL)
5079 arg = name;
5080 else
5081 arg = fudi.fd_newkey;
5082 if (arg != NULL && (fudi.fd_di == NULL
5083 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
5084 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
5085 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00005086 char_u *name_base = arg;
5087 int i;
5088
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005089 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005090 {
5091 name_base = vim_strchr(arg, '_');
5092 if (name_base == NULL)
5093 name_base = arg + 3;
5094 else
5095 ++name_base;
5096 }
5097 for (i = 0; name_base[i] != NUL && (i == 0
5098 ? eval_isnamec1(name_base[i])
5099 : eval_isnamec(name_base[i])); ++i)
5100 ;
5101 if (name_base[i] != NUL)
zeertzjq6a04bf52024-03-16 09:39:06 +01005102 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005103 emsg_funcname(e_invalid_argument_str, arg);
zeertzjq6a04bf52024-03-16 09:39:06 +01005104 goto ret_free;
5105 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00005106
5107 // In Vim9 script a function cannot have the same name as a
5108 // variable.
5109 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005110 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
5111 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00005112 + EVAL_VAR_NO_FUNC) == OK)
5113 {
5114 semsg(_(e_redefining_script_item_str), name_base);
5115 goto ret_free;
5116 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005117 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005118 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005119 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005120 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005121 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00005122 goto ret_free;
5123 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005124 }
5125
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005126 // This may get more lines and make the pointers into the first line
5127 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005128 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005129 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005130 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005131 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005132 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005133 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005134 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005135 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005138 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005140 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005141 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005142 if (*p != ':')
5143 {
5144 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5145 p = skipwhite(p);
5146 }
5147 else if (!IS_WHITE_OR_NUL(p[1]))
5148 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005149 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005150 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005152 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005153 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005154 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005155 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005156 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005157 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005158 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005159 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005160 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005161 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005162 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005163 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005165 else
5166 // find extra arguments "range", "dict", "abort" and "closure"
5167 for (;;)
5168 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005169 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170 p = skipwhite(p);
5171 if (STRNCMP(p, "range", 5) == 0)
5172 {
5173 flags |= FC_RANGE;
5174 p += 5;
5175 }
5176 else if (STRNCMP(p, "dict", 4) == 0)
5177 {
5178 flags |= FC_DICT;
5179 p += 4;
5180 }
5181 else if (STRNCMP(p, "abort", 5) == 0)
5182 {
5183 flags |= FC_ABORT;
5184 p += 5;
5185 }
5186 else if (STRNCMP(p, "closure", 7) == 0)
5187 {
5188 flags |= FC_CLOSURE;
5189 p += 7;
5190 if (current_funccal == NULL)
5191 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005192 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193 name == NULL ? (char_u *)"" : name);
5194 goto erret;
5195 }
5196 }
5197 else
5198 break;
5199 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005200
Bram Moolenaare38eab22019-12-05 21:50:01 +01005201 // When there is a line break use what follows for the function body.
5202 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005203 if (*p == '\n')
5204 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005205 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005206 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5207 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005208 && !(VIM_ISWHITE(*whitep) && *p == '#'
5209 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005210 && !eap->skip
5211 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005212 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005213
5214 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005215 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5216 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005217 */
5218 if (KeyTyped)
5219 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005220 // Check if the function already exists, don't let the user type the
5221 // whole function before telling him it doesn't work! For a script we
5222 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005223 if (!eap->skip && !eap->forceit)
5224 {
5225 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005226 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005227 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005228 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005229 }
5230
5231 if (!eap->skip && did_emsg)
5232 goto erret;
5233
Bram Moolenaare38eab22019-12-05 21:50:01 +01005234 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005235 cmdline_row = msg_row;
5236 }
5237
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005238 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005239 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005240
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005241 // Do not define the function when getting the body fails and when
5242 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005243 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005244 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005245 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5246 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005247 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005248 goto erret;
5249
5250 /*
5251 * If there are no errors, add the function
5252 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005253 if (fudi.fd_dict != NULL)
5254 {
5255 char numbuf[20];
5256
5257 fp = NULL;
5258 if (fudi.fd_newkey == NULL && !eap->forceit)
5259 {
5260 emsg(_(e_dictionary_entry_already_exists));
5261 goto erret;
5262 }
5263 if (fudi.fd_di == NULL)
5264 {
5265 // Can't add a function to a locked dictionary
5266 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5267 goto erret;
5268 }
5269 // Can't change an existing function if it is locked
5270 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5271 goto erret;
5272
5273 // Give the function a sequential number. Can only be used with a
5274 // Funcref!
5275 vim_free(name);
5276 sprintf(numbuf, "%d", ++func_nr);
5277 name = vim_strsave((char_u *)numbuf);
5278 if (name == NULL)
5279 goto erret;
5280 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005281 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005282 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005283 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005284 char_u *find_name = name;
5285 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005286 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005287
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005288 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005289 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005290 var_conflict = TRUE;
5291
5292 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5293 {
5294 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5295
5296 if (si->sn_autoload_prefix != NULL)
5297 {
5298 if (is_export)
5299 {
5300 find_name = name + STRLEN(si->sn_autoload_prefix);
5301 v = find_var(find_name, &ht, TRUE);
5302 if (v != NULL)
5303 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005304 // Only check if the function already exists in the script,
5305 // global functions can be shadowed.
5306 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005307 }
5308 else
5309 {
5310 char_u *prefixed = may_prefix_autoload(name);
5311
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005312 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005313 {
5314 v = find_var(prefixed, &ht, TRUE);
5315 if (v != NULL)
5316 var_conflict = TRUE;
5317 vim_free(prefixed);
5318 }
5319 }
5320 }
5321 }
5322 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005323 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005324 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005325 goto erret;
5326 }
5327
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005328 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005329 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005330 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005331 char_u *uname = untrans_function_name(name);
5332
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005333 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005334 }
5335
5336 if (fp != NULL || import != NULL)
5337 {
5338 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005339
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005340 // Function can be replaced with "function!" and when sourcing the
5341 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005342 // A name that is used by an import can not be overruled.
5343 if (import != NULL
5344 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005345 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005346 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005347 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005348 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005349 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005350 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005351 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005352 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005353 goto erret;
5354 }
5355 if (fp->uf_calls > 0)
5356 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005357 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005358 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005359 goto erret;
5360 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005361 if (fp->uf_refcount > 1)
5362 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005363 // This function is referenced somewhere, don't redefine it but
5364 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005365 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005366 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005367 fp = NULL;
5368 overwrite = TRUE;
5369 }
5370 else
5371 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005372 char_u *exp_name = fp->uf_name_exp;
5373
5374 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005375 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005376 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005377 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005378 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005379 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005380#ifdef FEAT_PROFILE
5381 fp->uf_profiling = FALSE;
5382 fp->uf_prof_initialized = FALSE;
5383#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005384 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005385 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005386 }
5387 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005388
5389 if (fp == NULL)
5390 {
5391 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5392 {
5393 int slen, plen;
5394 char_u *scriptname;
5395
Bram Moolenaare38eab22019-12-05 21:50:01 +01005396 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005397 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005398 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005399 {
5400 scriptname = autoload_name(name);
5401 if (scriptname != NULL)
5402 {
5403 p = vim_strchr(scriptname, '/');
5404 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005405 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005406 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005407 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005408 j = OK;
5409 vim_free(scriptname);
5410 }
5411 }
5412 if (j == FAIL)
5413 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005414 linenr_T save_lnum = SOURCING_LNUM;
5415
5416 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005417 semsg(_(e_function_name_does_not_match_script_file_name_str),
5418 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005419 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005420 goto erret;
5421 }
5422 }
5423
Bram Moolenaare01e5212023-01-08 20:31:18 +00005424 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005425 if (fp == NULL)
5426 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005427 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005428
5429 if (fudi.fd_dict != NULL)
5430 {
5431 if (fudi.fd_di == NULL)
5432 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005433 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005434 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5435 if (fudi.fd_di == NULL)
5436 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005437 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005438 goto erret;
5439 }
5440 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5441 {
5442 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005443 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005444 goto erret;
5445 }
5446 }
5447 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005448 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005449 clear_tv(&fudi.fd_di->di_tv);
5450 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005451 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005452
Bram Moolenaare38eab22019-12-05 21:50:01 +01005453 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005454 flags |= FC_DICT;
5455 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005456 }
5457 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005458 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005460 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005461
5462 if (eap->cmdidx == CMD_def)
5463 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005464 int lnum_save = SOURCING_LNUM;
5465 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005466
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005467 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005468
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005469 // error messages are for the first function line
5470 SOURCING_LNUM = sourcing_lnum_top;
5471
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005472 // The function may use script variables from the context.
5473 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005474
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005475 // The argument types and the return type may use an imported type.
5476 // In that case, the imported file will be sourced. To avoid treating
5477 // everything in the imported file as exported, temporarily reset
5478 // is_export.
5479 int save_is_export = is_export;
5480 is_export = FALSE;
5481
h-eastb895b0f2023-09-24 15:46:31 +02005482 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5483 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005484 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005485 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005486 free_fp = fp_allocated;
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005487 is_export = save_is_export;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005488 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005489 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005490 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005491
5492 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005493 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005494 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005495 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005496 free_fp = fp_allocated;
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005497 is_export = save_is_export;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005498 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005499 }
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005500 is_export = save_is_export;
Bram Moolenaar09689a02020-05-09 22:50:08 +02005501 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005502 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005503 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005504 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005505
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005506 if (fp_allocated)
5507 {
5508 // insert the new function in the function list
5509 set_ufunc_name(fp, name);
5510 if (overwrite)
5511 {
5512 hi = hash_find(&func_hashtab, name);
5513 hi->hi_key = UF2HIKEY(fp);
5514 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005515 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005516 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005517 {
5518 free_fp = TRUE;
5519 goto erret;
5520 }
5521 fp->uf_refcount = 1;
5522 }
5523
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005524 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005525 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005526 if ((flags & FC_CLOSURE) != 0)
5527 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005528 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005529 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005530 }
5531 else
5532 fp->uf_scoped = NULL;
5533
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005534#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005535 if (prof_def_func())
5536 func_do_profile(fp);
5537#endif
5538 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005539 if (sandbox)
5540 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005541 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005542 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005543 fp->uf_flags = flags;
5544 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005545 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005546 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005547 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005548 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005549 if (is_export)
5550 {
5551 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005552 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005553 is_export = FALSE;
5554 }
5555
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005556 if (eap->cmdidx == CMD_def)
5557 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005558 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5559 // :func does not use Vim9 script syntax, even in a Vim9 script file
5560 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005561
Yegappan Lakshmanan5715a722024-05-03 18:24:07 +02005562 // If test_override('defcompile' 1) is used, then compile the function now
5563 if (eap->cmdidx == CMD_def && override_defcompile)
5564 defcompile_function(fp, NULL);
5565
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005566 goto ret_free;
5567
5568erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005569 if (fp != NULL)
5570 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005571 // these were set to "newargs" and "default_args", which are cleared
5572 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005573 ga_init(&fp->uf_args);
5574 ga_init(&fp->uf_def_args);
5575 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005576errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005577 ga_clear_strings(&newargs);
5578 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005579 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005580 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005581 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005582 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005583 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01005584 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005585 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005586 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005587 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005588ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005589 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005590 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005591 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005592 if (name != name_arg)
5593 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005594 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005595 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005596
5597 return fp;
5598}
5599
5600/*
5601 * ":function"
5602 */
5603 void
5604ex_function(exarg_T *eap)
5605{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005606 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005607
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005608 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005609 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005610 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005611}
5612
LemonBoy48b7d052024-07-09 18:24:59 +02005613 int
5614get_func_arity(char_u *name, int *required, int *optional, int *varargs)
5615{
5616 ufunc_T *ufunc = NULL;
5617 int argcount = 0;
5618 int min_argcount = 0;
5619 int idx;
5620
5621 idx = find_internal_func(name);
5622 if (idx >= 0)
5623 {
5624 internal_func_get_argcount(idx, &argcount, &min_argcount);
5625 *varargs = FALSE;
5626 }
5627 else
5628 {
5629 char_u fname_buf[FLEN_FIXED + 1];
5630 char_u *tofree = NULL;
5631 funcerror_T error = FCERR_NONE;
5632 char_u *fname;
5633
5634 // May need to translate <SNR>123_ to K_SNR.
5635 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
5636 if (error == FCERR_NONE)
5637 ufunc = find_func(fname, FALSE);
5638 vim_free(tofree);
5639
5640 if (ufunc == NULL)
5641 return FAIL;
5642
5643 argcount = ufunc->uf_args.ga_len;
5644 min_argcount = ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len;
5645 *varargs = has_varargs(ufunc);
5646 }
5647
5648 *required = min_argcount;
5649 *optional = argcount - min_argcount;
5650
5651 return OK;
5652}
5653
Bram Moolenaar822ba242020-05-24 23:00:18 +02005654/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005655 * Find a function by name, including "<lambda>123".
5656 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005657 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005658 * Return NULL if not found.
5659 */
5660 ufunc_T *
5661find_func_by_name(char_u *name, compiletype_T *compile_type)
5662{
5663 char_u *arg = name;
5664 char_u *fname;
5665 ufunc_T *ufunc;
5666 int is_global = FALSE;
5667
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005668 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5669 {
5670 *compile_type = CT_PROFILE;
5671 arg = skipwhite(arg + 7);
5672 }
5673 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5674 {
5675 *compile_type = CT_DEBUG;
5676 arg = skipwhite(arg + 5);
5677 }
5678
5679 if (STRNCMP(arg, "<lambda>", 8) == 0)
5680 {
5681 arg += 8;
5682 (void)getdigits(&arg);
5683 fname = vim_strnsave(name, arg - name);
5684 }
5685 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005686 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005687 // First try finding a method in a class, trans_function_name() will
5688 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005689 ufunc = find_class_func(&arg);
5690 if (ufunc != NULL)
5691 return ufunc;
5692
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005693 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005694 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005695 NULL, NULL, NULL, &ufunc);
5696 if (ufunc != NULL)
5697 {
5698 vim_free(fname);
5699 return ufunc;
5700 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005701 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005702 if (fname == NULL)
5703 {
5704 semsg(_(e_invalid_argument_str), name);
5705 return NULL;
5706 }
5707 if (!ends_excmd2(name, arg))
5708 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005709 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005710 emsg(ex_errmsg(e_trailing_characters_str, arg));
5711 return NULL;
5712 }
5713
5714 ufunc = find_func(fname, is_global);
5715 if (ufunc == NULL)
5716 {
5717 char_u *p = untrans_function_name(fname);
5718
5719 if (p != NULL)
5720 // Try again without making it script-local.
5721 ufunc = find_func(p, FALSE);
5722 }
5723 vim_free(fname);
5724 if (ufunc == NULL)
5725 semsg(_(e_cannot_find_function_str), name);
5726 return ufunc;
5727}
5728
5729/*
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005730 * Compile the :def function "ufunc". If "cl" is not NULL, then compile the
5731 * class or object method "ufunc" in "cl".
5732 */
5733 void
5734defcompile_function(ufunc_T *ufunc, class_T *cl)
5735{
5736 compiletype_T compile_type = CT_NONE;
5737
5738 if (func_needs_compiling(ufunc, compile_type))
5739 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5740 else
5741 smsg(_("Function %s%s%s does not need compiling"),
5742 cl != NULL ? cl->class_name : (char_u *)"",
5743 cl != NULL ? (char_u *)"." : (char_u *)"",
5744 ufunc->uf_name);
5745}
5746
5747/*
5748 * Compile all the :def functions defined in the current script
5749 */
5750 static void
5751defcompile_funcs_in_script(void)
5752{
5753 long todo = (long)func_hashtab.ht_used;
5754 int changed = func_hashtab.ht_changed;
5755 hashitem_T *hi;
5756
5757 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5758 {
5759 if (!HASHITEM_EMPTY(hi))
5760 {
5761 --todo;
5762 ufunc_T *ufunc = HI2UF(hi);
5763 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5764 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5765 && (ufunc->uf_flags & FC_DEAD) == 0)
5766 {
5767 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5768
5769 if (func_hashtab.ht_changed != changed)
5770 {
5771 // a function has been added or removed, need to start
5772 // over
5773 todo = (long)func_hashtab.ht_used;
5774 changed = func_hashtab.ht_changed;
5775 hi = func_hashtab.ht_array;
5776 --hi;
5777 }
5778 }
5779 }
5780 }
5781}
5782
5783/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005784 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005785 * be compiled or the one specified by the argument.
5786 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005787 */
5788 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005789ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005790{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005791 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005792 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005793 typval_T tv;
5794
5795 if (is_class_name(eap->arg, &tv))
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005796 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005797 class_T *cl = tv.vval.v_class;
5798
5799 if (cl != NULL)
5800 defcompile_class(cl);
5801 }
5802 else
5803 {
5804 compiletype_T compile_type = CT_NONE;
5805 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
5806 if (ufunc != NULL)
5807 defcompile_function(ufunc, NULL);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005808 }
5809 }
5810 else
5811 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005812 defcompile_funcs_in_script();
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005813
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005814 // compile all the class defined in the current script
5815 defcompile_classes_in_script();
Bram Moolenaar822ba242020-05-24 23:00:18 +02005816 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005817}
5818
5819/*
5820 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5821 * Return 2 if "p" starts with "s:".
5822 * Return 0 otherwise.
5823 */
5824 int
5825eval_fname_script(char_u *p)
5826{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005827 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5828 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005829 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5830 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5831 return 5;
5832 if (p[0] == 's' && p[1] == ':')
5833 return 2;
5834 return 0;
5835}
5836
5837 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005838translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005839{
5840 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005841 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005842 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005843}
5844
5845/*
5846 * Return TRUE when "ufunc" has old-style "..." varargs
5847 * or named varargs "...name: type".
5848 */
5849 int
5850has_varargs(ufunc_T *ufunc)
5851{
5852 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005853}
5854
5855/*
5856 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005857 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005858 */
5859 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005860function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005861{
5862 char_u *nm = name;
5863 char_u *p;
5864 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005865 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005866 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005867
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005868 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5869 if (no_deref)
5870 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005871 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005872 nm = skipwhite(nm);
5873
Bram Moolenaare38eab22019-12-05 21:50:01 +01005874 // Only accept "funcname", "funcname ", "funcname (..." and
5875 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005876 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005877 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005878 vim_free(p);
5879 return n;
5880}
5881
Bram Moolenaar113e1072019-01-20 15:30:40 +01005882#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005883 char_u *
5884get_expanded_name(char_u *name, int check)
5885{
5886 char_u *nm = name;
5887 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005888 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005889
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005890 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005891
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005892 if (p != NULL && *nm == NUL
5893 && (!check || translated_function_exists(p, is_global)))
5894 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005895
5896 vim_free(p);
5897 return NULL;
5898}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005899#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005900
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005901/*
5902 * Function given to ExpandGeneric() to obtain the list of user defined
5903 * function names.
5904 */
5905 char_u *
5906get_user_func_name(expand_T *xp, int idx)
5907{
5908 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005909 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005910 static hashitem_T *hi;
5911 ufunc_T *fp;
5912
5913 if (idx == 0)
5914 {
5915 done = 0;
5916 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005917 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005918 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005919 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005920 {
5921 if (done++ > 0)
5922 ++hi;
5923 while (HASHITEM_EMPTY(hi))
5924 ++hi;
5925 fp = HI2UF(hi);
5926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005927 // don't show dead, dict and lambda functions
5928 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005929 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005930 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005931
5932 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005933 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005934
5935 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005936 if (xp->xp_context != EXPAND_USER_FUNC
5937 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005938 {
5939 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005940 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005941 STRCAT(IObuff, ")");
5942 }
5943 return IObuff;
5944 }
5945 return NULL;
5946}
5947
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005948/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005949 * Make a copy of a function.
5950 * Intended to be used for a function defined on a base class that has a copy
5951 * on the child class.
5952 * The copy has uf_refcount set to one.
5953 * Returns NULL when out of memory.
5954 */
5955 ufunc_T *
5956copy_function(ufunc_T *fp)
5957{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005958 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005959 if (ufunc == NULL)
5960 return NULL;
5961
5962 // Most things can just be copied.
5963 *ufunc = *fp;
5964
5965 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5966 ufunc->uf_dfunc_idx = 0;
5967 ufunc->uf_class = NULL;
5968
5969 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5970 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5971
5972 if (ufunc->uf_arg_types != NULL)
5973 {
5974 // "uf_arg_types" is an allocated array, make a copy.
5975 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5976 if (at != NULL)
5977 {
5978 mch_memmove(at, ufunc->uf_arg_types,
5979 sizeof(type_T *) * ufunc->uf_args.ga_len);
5980 ufunc->uf_arg_types = at;
5981 }
5982 }
5983
5984 // TODO: how about the types themselves? they can be freed when the
5985 // original function is freed:
5986 // type_T **uf_arg_types;
5987 // type_T *uf_ret_type;
5988
Ernie Rael114ec812023-06-04 18:11:35 +01005989 // make uf_type_list empty
5990 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005991
5992 // TODO: partial_T *uf_partial;
5993
5994 if (ufunc->uf_va_name != NULL)
5995 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5996
5997 // TODO:
5998 // type_T *uf_va_type;
5999 // type_T *uf_func_type;
6000
6001 ufunc->uf_block_depth = 0;
6002 ufunc->uf_block_ids = NULL;
6003
6004 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
6005
6006 ufunc->uf_refcount = 1;
6007 ufunc->uf_name_exp = NULL;
6008 STRCPY(ufunc->uf_name, fp->uf_name);
6009
6010 return ufunc;
6011}
6012
6013/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006014 * ":delfunction {name}"
6015 */
6016 void
6017ex_delfunction(exarg_T *eap)
6018{
6019 ufunc_T *fp = NULL;
6020 char_u *p;
6021 char_u *name;
6022 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006023 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006024
6025 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00006026 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
6027 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006028 vim_free(fudi.fd_newkey);
6029 if (name == NULL)
6030 {
6031 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00006032 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006033 return;
6034 }
6035 if (!ends_excmd(*skipwhite(p)))
6036 {
6037 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00006038 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006039 return;
6040 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006041 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006042 if (eap->nextcmd != NULL)
6043 *p = NUL;
6044
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006045 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02006046 {
6047 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006048 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02006049 vim_free(name);
6050 return;
6051 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006052 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006053 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006054 vim_free(name);
6055
6056 if (!eap->skip)
6057 {
6058 if (fp == NULL)
6059 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02006060 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00006061 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006062 return;
6063 }
6064 if (fp->uf_calls > 0)
6065 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006066 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006067 return;
6068 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006069 if (fp->uf_flags & FC_VIM9)
6070 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006071 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006072 return;
6073 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006074
6075 if (fudi.fd_dict != NULL)
6076 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006077 // Delete the dict item that refers to the function, it will
6078 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00006079 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006080 }
6081 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006082 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006083 // A normal function (not a numbered function or lambda) has a
6084 // refcount of 1 for the entry in the hashtable. When deleting
6085 // it and the refcount is more than one, it should be kept.
6086 // A numbered function and lambda should be kept if the refcount is
6087 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006088 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006089 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006090 // Function is still referenced somewhere. Don't free it but
6091 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006092 if (func_remove(fp))
6093 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006094 }
6095 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006096 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006097 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006098 }
6099}
6100
6101/*
6102 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006103 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006104 */
6105 void
6106func_unref(char_u *name)
6107{
Bram Moolenaar97baee82016-07-26 20:46:08 +02006108 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006109
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006110 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006111 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006112 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006113 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006114 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006115#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006116 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006117#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01006118 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006119 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006120 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006121}
6122
6123/*
6124 * Unreference a Function: decrement the reference count and free it when it
6125 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006126 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006127 */
6128 void
6129func_ptr_unref(ufunc_T *fp)
6130{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006131 if (fp != NULL && (--fp->uf_refcount <= 0
6132 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
6133 && fp->uf_partial->pt_refcount <= 1
6134 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02006135 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006136 // Only delete it when it's not being used. Otherwise it's done
6137 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02006138 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006139 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006140 }
6141}
6142
6143/*
6144 * Count a reference to a Function.
6145 */
6146 void
6147func_ref(char_u *name)
6148{
6149 ufunc_T *fp;
6150
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006151 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006152 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006153 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006154 if (fp != NULL)
6155 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006156 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01006157 // Only give an error for a numbered function.
6158 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01006159 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006160}
6161
6162/*
6163 * Count a reference to a Function.
6164 */
6165 void
6166func_ptr_ref(ufunc_T *fp)
6167{
6168 if (fp != NULL)
6169 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006170}
6171
6172/*
6173 * Return TRUE if items in "fc" do not have "copyID". That means they are not
6174 * referenced from anywhere that is in use.
6175 */
6176 static int
6177can_free_funccal(funccall_T *fc, int copyID)
6178{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006179 return (fc->fc_l_varlist.lv_copyID != copyID
6180 && fc->fc_l_vars.dv_copyID != copyID
6181 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006182 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006183}
6184
6185/*
6186 * ":return [expr]"
6187 */
6188 void
6189ex_return(exarg_T *eap)
6190{
6191 char_u *arg = eap->arg;
6192 typval_T rettv;
6193 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006194 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006195
6196 if (current_funccal == NULL)
6197 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006198 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006199 return;
6200 }
6201
Bram Moolenaar844fb642021-10-23 13:32:30 +01006202 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006203 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
6204
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006205 if (eap->skip)
6206 ++emsg_skip;
6207
6208 eap->nextcmd = NULL;
6209 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006210 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006211 {
6212 if (!eap->skip)
6213 returning = do_return(eap, FALSE, TRUE, &rettv);
6214 else
6215 clear_tv(&rettv);
6216 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01006217 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006218 else if (!eap->skip)
6219 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006220 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006221 update_force_abort();
6222
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006223 /*
6224 * Return unless the expression evaluation has been cancelled due to an
6225 * aborting error, an interrupt, or an exception.
6226 */
6227 if (!aborting())
6228 returning = do_return(eap, FALSE, TRUE, NULL);
6229 }
6230
Bram Moolenaare38eab22019-12-05 21:50:01 +01006231 // When skipping or the return gets pending, advance to the next command
6232 // in this line (!returning). Otherwise, ignore the rest of the line.
6233 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006234 if (returning)
6235 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006236 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006237 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006238
6239 if (eap->skip)
6240 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006241 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006242}
6243
zeertzjq5299c092023-04-12 20:48:16 +01006244/*
6245 * Lower level implementation of "call". Only called when not skipping.
6246 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006247 static int
6248ex_call_inner(
6249 exarg_T *eap,
6250 char_u *name,
6251 char_u **arg,
6252 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006253 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006254 evalarg_T *evalarg)
6255{
6256 linenr_T lnum;
6257 int doesrange;
6258 typval_T rettv;
6259 int failed = FALSE;
6260
zeertzjq5299c092023-04-12 20:48:16 +01006261 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006262 for ( ; lnum <= eap->line2; ++lnum)
6263 {
6264 funcexe_T funcexe;
6265
zeertzjq5299c092023-04-12 20:48:16 +01006266 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006267 {
6268 if (lnum > curbuf->b_ml.ml_line_count)
6269 {
6270 // If the function deleted lines or switched to another buffer
6271 // the line number may become invalid.
6272 emsg(_(e_invalid_range));
6273 break;
6274 }
6275 curwin->w_cursor.lnum = lnum;
6276 curwin->w_cursor.col = 0;
6277 curwin->w_cursor.coladd = 0;
6278 }
6279 *arg = startarg;
6280
6281 funcexe = *funcexe_init;
6282 funcexe.fe_doesrange = &doesrange;
6283 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6284 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6285 {
6286 failed = TRUE;
6287 break;
6288 }
6289 if (has_watchexpr())
6290 dbg_check_breakpoint(eap);
6291
6292 // Handle a function returning a Funcref, Dictionary or List.
6293 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006294 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006295 {
6296 failed = TRUE;
6297 break;
6298 }
6299
6300 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006301 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006302 break;
6303
6304 // Stop when immediately aborting on error, or when an interrupt
6305 // occurred or an exception was thrown but not caught.
6306 // get_func_tv() returned OK, so that the check for trailing
6307 // characters below is executed.
6308 if (aborting())
6309 break;
6310 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006311 return failed;
6312}
6313
6314/*
6315 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6316 * Returns FAIL or OK.
6317 */
6318 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006319ex_defer_inner(
6320 char_u *name,
6321 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006322 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006323 partial_T *partial,
6324 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006325{
6326 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006327 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006328 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006329 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006330
6331 if (current_funccal == NULL)
6332 {
6333 semsg(_(e_str_not_inside_function), "defer");
6334 return FAIL;
6335 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006336 if (partial != NULL)
6337 {
6338 if (partial->pt_dict != NULL)
6339 {
6340 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6341 return FAIL;
6342 }
6343 if (partial->pt_argc > 0)
6344 {
6345 int i;
6346
6347 partial_argc = partial->pt_argc;
6348 for (i = 0; i < partial_argc; ++i)
6349 copy_tv(&partial->pt_argv[i], &argvars[i]);
6350 }
6351 }
Ernie Raelb077b582023-12-14 20:11:44 +01006352 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006353 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006354 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006355 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006356
6357 if (r == OK)
6358 {
6359 if (type != NULL)
6360 {
6361 // Check that the arguments are OK for the types of the funcref.
6362 r = check_argument_types(type, argvars, argcount, NULL, name);
6363 }
Ernie Raelb077b582023-12-14 20:11:44 +01006364 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006365 {
6366 int idx = find_internal_func(name);
6367
6368 if (idx < 0)
6369 {
6370 emsg_funcname(e_unknown_function_str, name);
6371 r = FAIL;
6372 }
6373 else if (check_internal_func(idx, argcount) == -1)
6374 r = FAIL;
6375 }
6376 else
6377 {
6378 ufunc_T *ufunc = find_func(name, FALSE);
6379
6380 // we tolerate an unknown function here, it might be defined later
6381 if (ufunc != NULL)
6382 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006383 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006384 if (error != FCERR_UNKNOWN)
6385 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006386 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006387 r = FAIL;
6388 }
6389 }
6390 }
6391 }
6392
Bram Moolenaar86d87252022-09-05 21:21:25 +01006393 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006394 {
6395 while (--argcount >= 0)
6396 clear_tv(&argvars[argcount]);
6397 return FAIL;
6398 }
6399 return add_defer(name, argcount, argvars);
6400}
6401
6402/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006403 * Return TRUE if currently inside a function call.
6404 * Give an error message and return FALSE when not.
6405 */
6406 int
6407can_add_defer(void)
6408{
6409 if (!in_def_function() && get_current_funccal() == NULL)
6410 {
6411 semsg(_(e_str_not_inside_function), "defer");
6412 return FALSE;
6413 }
6414 return TRUE;
6415}
6416
6417/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006418 * Add a deferred call for "name" with arguments "argvars[argcount]".
6419 * Consumes "argvars[]".
6420 * Caller must check that in_def_function() returns TRUE or current_funccal is
6421 * not NULL.
6422 * Returns OK or FAIL.
6423 */
6424 int
6425add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6426{
6427 char_u *saved_name = vim_strsave(name);
6428 int argcount = argcount_arg;
6429 defer_T *dr;
6430 int ret = FAIL;
6431
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006432 if (saved_name == NULL)
6433 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006434 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006435 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006436 if (add_defer_function(saved_name, argcount, argvars) == OK)
6437 argcount = 0;
6438 }
6439 else
6440 {
6441 if (current_funccal->fc_defer.ga_itemsize == 0)
6442 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6443 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6444 goto theend;
6445 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6446 + current_funccal->fc_defer.ga_len++;
6447 dr->dr_name = saved_name;
6448 dr->dr_argcount = argcount;
6449 while (argcount > 0)
6450 {
6451 --argcount;
6452 dr->dr_argvars[argcount] = argvars[argcount];
6453 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006454 }
6455 ret = OK;
6456
6457theend:
6458 while (--argcount >= 0)
6459 clear_tv(&argvars[argcount]);
6460 return ret;
6461}
6462
6463/*
6464 * Invoked after a functions has finished: invoke ":defer" functions.
6465 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006466 static void
6467handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006468{
6469 int idx;
6470
Bram Moolenaar58779852022-09-06 18:31:14 +01006471 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006472 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006473 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006474
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006475 if (dr->dr_name == NULL)
6476 // already being called, can happen if function does ":qa"
6477 continue;
6478
6479 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006480 CLEAR_FIELD(funcexe);
6481 funcexe.fe_evaluate = TRUE;
6482
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006483 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006484 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006485
6486 char_u *name = dr->dr_name;
6487 dr->dr_name = NULL;
6488
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006489 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006490 // first statement in the function will be executed (because of the
6491 // exception). So save and restore the try/catch/throw exception
6492 // state.
6493 exception_state_T estate;
6494 exception_state_save(&estate);
6495 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006496
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006497 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6498
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006499 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006500
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006501 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006502 vim_free(name);
6503 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006504 clear_tv(&dr->dr_argvars[i]);
6505 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006506 ga_clear(&funccal->fc_defer);
6507}
6508
zeertzjq960cf912023-04-18 21:52:54 +01006509 static void
6510invoke_funccall_defer(funccall_T *fc)
6511{
6512 if (fc->fc_ectx != NULL)
6513 {
6514 // :def function
6515 unwind_def_callstack(fc->fc_ectx);
6516 may_invoke_defer_funcs(fc->fc_ectx);
6517 }
6518 else
6519 {
6520 // legacy function
6521 handle_defer_one(fc);
6522 }
6523}
6524
Bram Moolenaar58779852022-09-06 18:31:14 +01006525/*
6526 * Called when exiting: call all defer functions.
6527 */
6528 void
6529invoke_all_defer(void)
6530{
zeertzjq1be4b812023-04-19 14:21:24 +01006531 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6532 invoke_funccall_defer(fc);
6533
zeertzjq960cf912023-04-18 21:52:54 +01006534 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6535 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6536 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006537}
6538
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006539/*
6540 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006541 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006542 */
6543 void
6544ex_call(exarg_T *eap)
6545{
6546 char_u *arg = eap->arg;
6547 char_u *startarg;
6548 char_u *name;
6549 char_u *tofree;
6550 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006551 int failed = FALSE;
6552 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006553 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006554 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006555 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006556 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006557 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006558 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006559
Bram Moolenaare6b53242020-07-01 17:28:33 +02006560 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006561 if (eap->skip)
6562 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006563 typval_T rettv;
6564
Bram Moolenaare38eab22019-12-05 21:50:01 +01006565 // trans_function_name() doesn't work well when skipping, use eval0()
6566 // instead to skip to any following command, e.g. for:
6567 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006568 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006569 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006570 clear_tv(&rettv);
6571 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006572 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006573 return;
6574 }
6575
zeertzjq5299c092023-04-12 20:48:16 +01006576 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006577 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006578 if (fudi.fd_newkey != NULL)
6579 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006580 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006581 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006582 vim_free(fudi.fd_newkey);
6583 }
6584 if (tofree == NULL)
6585 return;
6586
Bram Moolenaare38eab22019-12-05 21:50:01 +01006587 // Increase refcount on dictionary, it could get deleted when evaluating
6588 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006589 if (fudi.fd_dict != NULL)
6590 ++fudi.fd_dict->dv_refcount;
6591
Bram Moolenaare38eab22019-12-05 21:50:01 +01006592 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6593 // contents. For VAR_PARTIAL get its partial, unless we already have one
6594 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006595 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006596 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006597 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006598 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006599
Bram Moolenaare38eab22019-12-05 21:50:01 +01006600 // Skip white space to allow ":call func ()". Not good, but required for
6601 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006602 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006603 if (*startarg != '(')
6604 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006605 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006606 goto end;
6607 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006608 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006609 {
6610 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6611 goto end;
6612 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006613
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006614 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006615 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006616 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006617 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006618 }
6619 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006620 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006621 funcexe_T funcexe;
6622
Bram Moolenaara80faa82020-04-12 19:37:17 +02006623 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006624 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006625 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006626 funcexe.fe_partial = partial;
6627 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006628 funcexe.fe_firstline = eap->line1;
6629 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006630 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006631 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006632 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006633 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006634
Bram Moolenaar1d341892021-09-18 15:25:52 +02006635 // When inside :try we need to check for following "| catch" or "| endtry".
6636 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006637 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006638 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006639 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006640 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006641 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006642 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006643 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006644 {
6645 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006646 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006647 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006648 }
6649 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006650 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006651 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006652 // Must be after using "arg", it may point into memory cleared here.
6653 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006654
6655end:
6656 dict_unref(fudi.fd_dict);
6657 vim_free(tofree);
6658}
6659
6660/*
6661 * Return from a function. Possibly makes the return pending. Also called
6662 * for a pending return at the ":endtry" or after returning from an extra
6663 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6664 * when called due to a ":return" command. "rettv" may point to a typval_T
6665 * with the return rettv. Returns TRUE when the return can be carried out,
6666 * FALSE when the return gets pending.
6667 */
6668 int
6669do_return(
6670 exarg_T *eap,
6671 int reanimate,
6672 int is_cmd,
6673 void *rettv)
6674{
6675 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006676 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006677
6678 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006679 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006680 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006681
6682 /*
6683 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6684 * not in its finally clause (which then is to be executed next) is found.
6685 * In this case, make the ":return" pending for execution at the ":endtry".
6686 * Otherwise, return normally.
6687 */
6688 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6689 if (idx >= 0)
6690 {
6691 cstack->cs_pending[idx] = CSTP_RETURN;
6692
6693 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006694 // A pending return again gets pending. "rettv" points to an
6695 // allocated variable with the rettv of the original ":return"'s
6696 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006697 cstack->cs_rettv[idx] = rettv;
6698 else
6699 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006700 // When undoing a return in order to make it pending, get the stored
6701 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006702 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006703 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006704
6705 if (rettv != NULL)
6706 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006707 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006708 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6709 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6710 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006711 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006712 }
6713 else
6714 cstack->cs_rettv[idx] = NULL;
6715
6716 if (reanimate)
6717 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006718 // The pending return value could be overwritten by a ":return"
6719 // without argument in a finally clause; reset the default
6720 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006721 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6722 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006723 }
6724 }
6725 report_make_pending(CSTP_RETURN, rettv);
6726 }
6727 else
6728 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006729 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006730
Bram Moolenaare38eab22019-12-05 21:50:01 +01006731 // If the return is carried out now, store the return value. For
6732 // a return immediately after reanimation, the value is already
6733 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006734 if (!reanimate && rettv != NULL)
6735 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006736 clear_tv(current_funccal->fc_rettv);
6737 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006738 if (!is_cmd)
6739 vim_free(rettv);
6740 }
6741 }
6742
6743 return idx < 0;
6744}
6745
6746/*
6747 * Free the variable with a pending return value.
6748 */
6749 void
6750discard_pending_return(void *rettv)
6751{
6752 free_tv((typval_T *)rettv);
6753}
6754
6755/*
6756 * Generate a return command for producing the value of "rettv". The result
6757 * is an allocated string. Used by report_pending() for verbose messages.
6758 */
6759 char_u *
6760get_return_cmd(void *rettv)
6761{
6762 char_u *s = NULL;
6763 char_u *tofree = NULL;
6764 char_u numbuf[NUMBUFLEN];
6765
6766 if (rettv != NULL)
6767 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6768 if (s == NULL)
6769 s = (char_u *)"";
6770
6771 STRCPY(IObuff, ":return ");
6772 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6773 if (STRLEN(s) + 8 >= IOSIZE)
6774 STRCPY(IObuff + IOSIZE - 4, "...");
6775 vim_free(tofree);
6776 return vim_strsave(IObuff);
6777}
6778
6779/*
6780 * Get next function line.
6781 * Called by do_cmdline() to get the next line.
6782 * Returns allocated string, or NULL for end of function.
6783 */
6784 char_u *
6785get_func_line(
6786 int c UNUSED,
6787 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006788 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006789 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006790{
6791 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006792 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006793 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006794 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006795
Bram Moolenaare38eab22019-12-05 21:50:01 +01006796 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006797 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006798 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006799 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006800 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006801 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006802 }
6803#ifdef FEAT_PROFILE
6804 if (do_profiling == PROF_YES)
6805 func_line_end(cookie);
6806#endif
6807
6808 gap = &fp->uf_lines;
6809 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006810 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006811 retval = NULL;
6812 else
6813 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006814 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006815 while (fcp->fc_linenr < gap->ga_len
6816 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6817 ++fcp->fc_linenr;
6818 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006819 retval = NULL;
6820 else
6821 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006822 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6823 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006824#ifdef FEAT_PROFILE
6825 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006826 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006827#endif
6828 }
6829 }
6830
Bram Moolenaare38eab22019-12-05 21:50:01 +01006831 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006832 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006833 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006834 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006835 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006836 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006837 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006838 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006839 }
6840
6841 return retval;
6842}
6843
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006844/*
6845 * Return TRUE if the currently active function should be ended, because a
6846 * return was encountered or an error occurred. Used inside a ":while".
6847 */
6848 int
6849func_has_ended(void *cookie)
6850{
6851 funccall_T *fcp = (funccall_T *)cookie;
6852
Bram Moolenaare38eab22019-12-05 21:50:01 +01006853 // Ignore the "abort" flag if the abortion behavior has been changed due to
6854 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006855 return (((fcp->fc_func->uf_flags & FC_ABORT)
6856 && did_emsg && !aborted_in_try())
6857 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006858}
6859
6860/*
6861 * return TRUE if cookie indicates a function which "abort"s on errors.
6862 */
6863 int
6864func_has_abort(
6865 void *cookie)
6866{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006867 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006868}
6869
6870
6871/*
6872 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6873 * Don't do this when "Func" is already a partial that was bound
6874 * explicitly (pt_auto is FALSE).
6875 * Changes "rettv" in-place.
6876 * Returns the updated "selfdict_in".
6877 */
6878 dict_T *
6879make_partial(dict_T *selfdict_in, typval_T *rettv)
6880{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006881 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006882 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006883 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006884 dict_T *selfdict = selfdict_in;
6885
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006886 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6887 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006888 fp = rettv->vval.v_partial->pt_func;
6889 else
6890 {
6891 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006892 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006893 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006894 if (fname == NULL)
6895 {
6896 // There is no point binding a dict to a NULL function, just create
6897 // a function reference.
6898 rettv->v_type = VAR_FUNC;
6899 rettv->vval.v_string = NULL;
6900 }
6901 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006902 {
6903 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006904 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006905
6906 // Translate "s:func" to the stored function name.
6907 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6908 fp = find_func(fname, FALSE);
6909 vim_free(tofree);
6910 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006911 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006912
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006913 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006914 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006915 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006916
6917 if (pt != NULL)
6918 {
6919 pt->pt_refcount = 1;
6920 pt->pt_dict = selfdict;
6921 pt->pt_auto = TRUE;
6922 selfdict = NULL;
6923 if (rettv->v_type == VAR_FUNC)
6924 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006925 // Just a function: Take over the function name and use
6926 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006927 pt->pt_name = rettv->vval.v_string;
6928 }
6929 else
6930 {
6931 partial_T *ret_pt = rettv->vval.v_partial;
6932 int i;
6933
Bram Moolenaare38eab22019-12-05 21:50:01 +01006934 // Partial: copy the function name, use selfdict and copy
6935 // args. Can't take over name or args, the partial might
6936 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006937 if (ret_pt->pt_name != NULL)
6938 {
6939 pt->pt_name = vim_strsave(ret_pt->pt_name);
6940 func_ref(pt->pt_name);
6941 }
6942 else
6943 {
6944 pt->pt_func = ret_pt->pt_func;
6945 func_ptr_ref(pt->pt_func);
6946 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006947 if (ret_pt->pt_argc > 0)
6948 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006949 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006950 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006951 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006952 pt->pt_argc = 0;
6953 else
6954 {
6955 pt->pt_argc = ret_pt->pt_argc;
6956 for (i = 0; i < pt->pt_argc; i++)
6957 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6958 }
6959 }
6960 partial_unref(ret_pt);
6961 }
6962 rettv->v_type = VAR_PARTIAL;
6963 rettv->vval.v_partial = pt;
6964 }
6965 }
6966 return selfdict;
6967}
6968
6969/*
6970 * Return the name of the executed function.
6971 */
6972 char_u *
6973func_name(void *cookie)
6974{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006975 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006976}
6977
6978/*
6979 * Return the address holding the next breakpoint line for a funccall cookie.
6980 */
6981 linenr_T *
6982func_breakpoint(void *cookie)
6983{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006984 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006985}
6986
6987/*
6988 * Return the address holding the debug tick for a funccall cookie.
6989 */
6990 int *
6991func_dbg_tick(void *cookie)
6992{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006993 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006994}
6995
6996/*
6997 * Return the nesting level for a funccall cookie.
6998 */
6999 int
7000func_level(void *cookie)
7001{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007002 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007003}
7004
7005/*
7006 * Return TRUE when a function was ended by a ":return" command.
7007 */
7008 int
7009current_func_returned(void)
7010{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007011 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007012}
7013
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007014 int
7015free_unref_funccal(int copyID, int testing)
7016{
7017 int did_free = FALSE;
7018 int did_free_funccal = FALSE;
7019 funccall_T *fc, **pfc;
7020
7021 for (pfc = &previous_funccal; *pfc != NULL; )
7022 {
7023 if (can_free_funccal(*pfc, copyID))
7024 {
7025 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007026 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01007027 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007028 did_free = TRUE;
7029 did_free_funccal = TRUE;
7030 }
7031 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01007032 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007033 }
7034 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01007035 // When a funccal was freed some more items might be garbage
7036 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007037 (void)garbage_collect(testing);
7038
7039 return did_free;
7040}
7041
7042/*
Bram Moolenaarba209902016-08-24 22:06:38 +02007043 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007044 */
7045 static funccall_T *
7046get_funccal(void)
7047{
7048 int i;
7049 funccall_T *funccal;
7050 funccall_T *temp_funccal;
7051
7052 funccal = current_funccal;
7053 if (debug_backtrace_level > 0)
7054 {
7055 for (i = 0; i < debug_backtrace_level; i++)
7056 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007057 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007058 if (temp_funccal)
7059 funccal = temp_funccal;
7060 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01007061 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007062 debug_backtrace_level = i;
7063 }
7064 }
7065 return funccal;
7066}
7067
7068/*
7069 * Return the hashtable used for local variables in the current funccal.
7070 * Return NULL if there is no current funccal.
7071 */
7072 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007073get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007074{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007075 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007076 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007077 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007078}
7079
7080/*
7081 * Return the l: scope variable.
7082 * Return NULL if there is no current funccal.
7083 */
7084 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007085get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007086{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007087 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007088 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007089 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007090}
7091
7092/*
7093 * Return the hashtable used for argument in the current funccal.
7094 * Return NULL if there is no current funccal.
7095 */
7096 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007097get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007098{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007099 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007100 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007101 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007102}
7103
7104/*
7105 * Return the a: scope variable.
7106 * Return NULL if there is no current funccal.
7107 */
7108 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007109get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007110{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007111 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007112 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007113 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007114}
7115
7116/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007117 * List function variables, if there is a function.
7118 */
7119 void
7120list_func_vars(int *first)
7121{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007122 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
7123 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01007124 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007125}
7126
7127/*
7128 * If "ht" is the hashtable for local variables in the current funccal, return
7129 * the dict that contains it.
7130 * Otherwise return NULL.
7131 */
7132 dict_T *
7133get_current_funccal_dict(hashtab_T *ht)
7134{
7135 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01007136 && ht == &current_funccal->fc_l_vars.dv_hashtab)
7137 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007138 return NULL;
7139}
7140
7141/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007142 * Search hashitem in parent scope.
7143 */
7144 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007145find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007146{
7147 funccall_T *old_current_funccal = current_funccal;
7148 hashtab_T *ht;
7149 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007150 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007151
Bram Moolenaarca16c602022-09-06 18:57:08 +01007152 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007153 return NULL;
7154
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02007155 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01007156 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02007157 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007158 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007159 ht = find_var_ht(name, &varname);
7160 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02007161 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007162 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02007163 if (!HASHITEM_EMPTY(hi))
7164 {
7165 *pht = ht;
7166 break;
7167 }
7168 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007169 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02007170 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007171 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007172 }
7173 current_funccal = old_current_funccal;
7174
7175 return hi;
7176}
7177
7178/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007179 * Search variable in parent scope.
7180 */
7181 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007182find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007183{
7184 dictitem_T *v = NULL;
7185 funccall_T *old_current_funccal = current_funccal;
7186 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007187 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007188
Bram Moolenaarca16c602022-09-06 18:57:08 +01007189 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007190 return NULL;
7191
Bram Moolenaare38eab22019-12-05 21:50:01 +01007192 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01007193 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007194 while (current_funccal)
7195 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007196 ht = find_var_ht(name, &varname);
7197 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007198 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007199 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007200 if (v != NULL)
7201 break;
7202 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007203 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007204 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007205 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007206 }
7207 current_funccal = old_current_funccal;
7208
7209 return v;
7210}
7211
7212/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007213 * Set "copyID + 1" in previous_funccal and callers.
7214 */
7215 int
7216set_ref_in_previous_funccal(int copyID)
7217{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007218 funccall_T *fc;
7219
Bram Moolenaarca16c602022-09-06 18:57:08 +01007220 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007221 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007222 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007223 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
7224 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
7225 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007226 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007227 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007228 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007229}
7230
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007231 static int
7232set_ref_in_funccal(funccall_T *fc, int copyID)
7233{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007234 if (fc->fc_copyID != copyID)
7235 {
7236 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007237 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7238 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7239 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7240 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007241 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007242 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007243 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007244}
7245
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007246/*
7247 * Set "copyID" in all local vars and arguments in the call stack.
7248 */
7249 int
7250set_ref_in_call_stack(int copyID)
7251{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007252 funccall_T *fc;
7253 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007254
Bram Moolenaarca16c602022-09-06 18:57:08 +01007255 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007256 if (set_ref_in_funccal(fc, copyID))
7257 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007258
7259 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007260 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007261 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007262 if (set_ref_in_funccal(fc, copyID))
7263 return TRUE;
7264 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007265}
7266
7267/*
7268 * Set "copyID" in all functions available by name.
7269 */
7270 int
7271set_ref_in_functions(int copyID)
7272{
7273 int todo;
7274 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007275 ufunc_T *fp;
7276
7277 todo = (int)func_hashtab.ht_used;
7278 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007279 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007280 if (!HASHITEM_EMPTY(hi))
7281 {
7282 --todo;
7283 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007284 if (!func_name_refcount(fp->uf_name)
7285 && set_ref_in_func(NULL, fp, copyID))
7286 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007287 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007288 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007289 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007290}
7291
7292/*
7293 * Set "copyID" in all function arguments.
7294 */
7295 int
7296set_ref_in_func_args(int copyID)
7297{
7298 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007299
7300 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007301 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7302 copyID, NULL, NULL))
7303 return TRUE;
7304 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007305}
7306
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007307/*
7308 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007309 * Returns TRUE if setting references failed somehow.
7310 */
7311 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007312set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007313{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007314 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007315 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007316 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007317 char_u fname_buf[FLEN_FIXED + 1];
7318 char_u *tofree = NULL;
7319 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007320 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007321
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007322 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007323 return FALSE;
7324
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007325 if (fp_in == NULL)
7326 {
7327 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007328 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007329 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007330 if (fp != NULL)
7331 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007332 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007333 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007334 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007335
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007336 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007337 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007338}
7339
Bram Moolenaare38eab22019-12-05 21:50:01 +01007340#endif // FEAT_EVAL