blob: b4ee0a2675a087f5e3d1de989e8ab30fb0ec7527 [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/*
Ernie Rael16409692024-07-31 22:18:11 +02002312 * Find a function by name, return pointer to it.
2313 * The name may be a local script variable, VAR_FUNC. or it may be a fully
2314 * qualified import name such as 'i_imp.FuncName'.
2315 *
2316 * When VAR_FUNC, the import might either direct or autoload.
2317 * When 'i_imp.FuncName' it is direct, autoload is rewritten as i_imp#FuncName
2318 * in f_call and subsequently found.
2319 */
2320 static ufunc_T *
2321find_func_imported(char_u *name, int flags)
2322{
2323 ufunc_T *func = NULL;
2324 char_u *dot = name; // Find a dot, '.', in the name
2325
2326 // Either run into '.' or the end of the string
2327 while (eval_isnamec(*dot))
2328 ++dot;
2329
2330 if (*dot == '.')
2331 {
2332 imported_T *import = find_imported(name, dot - name, FALSE);
2333 if (import != NULL)
2334 func = find_func_with_sid(dot + 1, import->imp_sid);
2335 }
2336 else if (*dot == NUL) // looking at the entire string
2337 {
2338 hashtab_T *ht = get_script_local_ht();
2339 if (ht != NULL)
2340 {
2341 hashitem_T *hi = hash_find(ht, name);
2342 if (!HASHITEM_EMPTY(hi))
2343 {
2344 dictitem_T *di = HI2DI(hi);
Ernie Raelcb90ea92024-08-19 21:45:23 +02002345 if (di->di_tv.v_type == VAR_FUNC
2346 && di->di_tv.vval.v_string != NULL)
Ernie Rael16409692024-07-31 22:18:11 +02002347 func = find_func_even_dead(di->di_tv.vval.v_string, flags);
2348 }
2349 }
2350 }
2351 return func;
2352}
2353
2354/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002355 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002356 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2357 * functions.
2358 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002359 * Return NULL for unknown function.
2360 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002361 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002362find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002363{
2364 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002366
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002367 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002369 // Find script-local function before global one.
2370 if (in_vim9script() && eval_isnamec1(*name)
2371 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002373 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2374 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002375 if (func != NULL)
2376 return func;
2377 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002378 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2379 {
2380 char_u *p = name + 5;
2381 long sid;
2382
2383 // printable "<SNR>123_Name" form
2384 sid = getdigits(&p);
2385 if (*p == '_')
2386 {
2387 func = find_func_with_sid(p + 1, (int)sid);
2388 if (func != NULL)
2389 return func;
2390 }
2391 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002393
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002394 if ((flags & FFED_NO_GLOBAL) == 0)
2395 {
2396 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002397 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002398 if (!HASHITEM_EMPTY(hi))
2399 return HI2UF(hi);
2400 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401
Bram Moolenaarb8822442022-01-11 15:24:05 +00002402 // Find autoload function if this is an autoload script.
Ernie Rael16409692024-07-31 22:18:11 +02002403 func = find_func_with_prefix(name[0] == 's' && name[1] == ':'
Bram Moolenaarb8822442022-01-11 15:24:05 +00002404 ? name + 2 : name, current_sctx.sc_sid);
Ernie Rael16409692024-07-31 22:18:11 +02002405 if (func != NULL)
2406 return func;
2407
2408 // Find a script-local "VAR_FUNC" or i_"imp.Func", so vim9script).
2409 if (in_vim9script())
2410 func = find_func_imported(name, flags);
2411 return func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412}
2413
2414/*
2415 * Find a function by name, return pointer to it in ufuncs.
2416 * "cctx" is passed in a :def function to find imported functions.
2417 * Return NULL for unknown or dead function.
2418 */
2419 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002420find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002422 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423
2424 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2425 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002426 return NULL;
2427}
2428
2429/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002430 * Return TRUE if "ufunc" is a global function.
2431 */
2432 int
2433func_is_global(ufunc_T *ufunc)
2434{
2435 return ufunc->uf_name[0] != K_SPECIAL;
2436}
2437
2438/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002439 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2440 */
2441 int
2442func_requires_g_prefix(ufunc_T *ufunc)
2443{
2444 return ufunc->uf_name[0] != K_SPECIAL
2445 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002446 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01002447 && !SAFE_isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002448}
2449
2450/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002451 * Copy the function name of "fp" to buffer "buf".
2452 * "buf" must be able to hold the function name plus three bytes.
2453 * Takes care of script-local function names.
2454 */
2455 static void
2456cat_func_name(char_u *buf, ufunc_T *fp)
2457{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002458 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002459 {
2460 STRCPY(buf, "<SNR>");
2461 STRCAT(buf, fp->uf_name + 3);
2462 }
2463 else
2464 STRCPY(buf, fp->uf_name);
2465}
2466
2467/*
2468 * Add a number variable "name" to dict "dp" with value "nr".
2469 */
2470 static void
2471add_nr_var(
2472 dict_T *dp,
2473 dictitem_T *v,
2474 char *name,
2475 varnumber_T nr)
2476{
2477 STRCPY(v->di_key, name);
2478 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002479 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002480 v->di_tv.v_type = VAR_NUMBER;
2481 v->di_tv.v_lock = VAR_FIXED;
2482 v->di_tv.vval.v_number = nr;
2483}
2484
2485/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002486 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002487 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002488 static void
2489free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002490{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002491 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002492
Bram Moolenaarca16c602022-09-06 18:57:08 +01002493 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002494 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002495 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002496
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002497 // When garbage collecting a funccall_T may be freed before the
2498 // function that references it, clear its uf_scoped field.
2499 // The function may have been redefined and point to another
2500 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002501 if (fp != NULL && fp->uf_scoped == fc)
2502 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002503 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002504 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002505
Bram Moolenaarca16c602022-09-06 18:57:08 +01002506 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002507 vim_free(fc);
2508}
2509
2510/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002511 * Free "fc" and what it contains.
2512 * Can be called only when "fc" is kept beyond the period of it called,
2513 * i.e. after cleanup_function_call(fc).
2514 */
2515 static void
2516free_funccal_contents(funccall_T *fc)
2517{
2518 listitem_T *li;
2519
2520 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002521 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002522
2523 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002524 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002525
2526 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002527 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002528 clear_tv(&li->li_tv);
2529
2530 free_funccal(fc);
2531}
2532
2533/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002534 * Handle the last part of returning from a function: free the local hashtable.
2535 * Unless it is still in use by a closure.
2536 */
2537 static void
2538cleanup_function_call(funccall_T *fc)
2539{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002540 int may_free_fc = fc->fc_refcount <= 0;
2541 int free_fc = TRUE;
2542
Bram Moolenaarca16c602022-09-06 18:57:08 +01002543 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002544
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002545 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002546 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2547 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002548 else
2549 free_fc = FALSE;
2550
2551 // If the a:000 list and the l: and a: dicts are not referenced and
2552 // there is no closure using it, we can free the funccall_T and what's
2553 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002554 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2555 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002556 else
2557 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002558 int todo;
2559 hashitem_T *hi;
2560 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002561
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002562 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002563
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002564 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002565 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002566 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002567 {
2568 if (!HASHITEM_EMPTY(hi))
2569 {
2570 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002571 di = HI2DI(hi);
2572 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002573 }
2574 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002575 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002576
Bram Moolenaarca16c602022-09-06 18:57:08 +01002577 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2578 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002579 else
2580 {
2581 listitem_T *li;
2582
2583 free_fc = FALSE;
2584
2585 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002586 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002587 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002588 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002589
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002590 if (free_fc)
2591 free_funccal(fc);
2592 else
2593 {
2594 static int made_copy = 0;
2595
2596 // "fc" is still in use. This can happen when returning "a:000",
2597 // assigning "l:" to a global variable or defining a closure.
2598 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002599 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002600 previous_funccal = fc;
2601
2602 if (want_garbage_collect)
2603 // If garbage collector is ready, clear count.
2604 made_copy = 0;
2605 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002606 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002607 // We have made a lot of copies, worth 4 Mbyte. This can happen
2608 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002609 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002610 // too much memory.
2611 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002612 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002613 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002614 }
2615}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002616
2617/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002618 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2619 */
2620 static int
2621numbered_function(char_u *name)
2622{
Keith Thompson184f71c2024-01-04 21:19:04 +01002623 return SAFE_isdigit(*name)
2624 || (name[0] == 'g' && name[1] == ':' && SAFE_isdigit(name[2]));
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002625}
2626
2627/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002628 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002629 * 1. ordinary names, function defined with :function or :def;
2630 * can start with "<SNR>123_" literally or with K_SPECIAL.
2631 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002632 * For the first we only count the name stored in func_hashtab as a reference,
2633 * using function() does not count as a reference, because the function is
2634 * looked up by name.
2635 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002636 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002637func_name_refcount(char_u *name)
2638{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002639 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002640}
2641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642/*
2643 * Unreference "fc": decrement the reference count and free it when it
2644 * becomes zero. "fp" is detached from "fc".
2645 * When "force" is TRUE we are exiting.
2646 */
2647 static void
2648funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2649{
2650 funccall_T **pfc;
2651 int i;
2652
2653 if (fc == NULL)
2654 return;
2655
2656 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002657 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2658 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2659 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2660 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 {
2662 if (fc == *pfc)
2663 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002664 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 free_funccal_contents(fc);
2666 return;
2667 }
2668 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002669 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2670 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2671 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672}
2673
2674/*
2675 * Remove the function from the function hashtable. If the function was
2676 * deleted while it still has references this was already done.
2677 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2678 */
2679 static int
2680func_remove(ufunc_T *fp)
2681{
2682 hashitem_T *hi;
2683
2684 // Return if it was already virtually deleted.
2685 if (fp->uf_flags & FC_DEAD)
2686 return FALSE;
2687
2688 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002689 if (HASHITEM_EMPTY(hi))
2690 return FALSE;
2691
2692 // When there is a def-function index do not actually remove the
2693 // function, so we can find the index when defining the function again.
2694 // Do remove it when it's a copy.
2695 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002697 fp->uf_flags |= FC_DEAD;
2698 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002700 hash_remove(&func_hashtab, hi, "remove function");
2701 fp->uf_flags |= FC_DELETED;
2702 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703}
2704
2705 static void
2706func_clear_items(ufunc_T *fp)
2707{
2708 ga_clear_strings(&(fp->uf_args));
2709 ga_clear_strings(&(fp->uf_def_args));
2710 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002712 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002713 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002714 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002715
2716 // Increment the refcount of this function to avoid it being freed
2717 // recursively when the partial is freed.
2718 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002719 partial_unref(fp->uf_partial);
2720 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002721 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002722
2723#ifdef FEAT_LUA
2724 if (fp->uf_cb_free != NULL)
2725 {
2726 fp->uf_cb_free(fp->uf_cb_state);
2727 fp->uf_cb_free = NULL;
2728 }
2729
2730 fp->uf_cb_state = NULL;
2731 fp->uf_cb = NULL;
2732#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733#ifdef FEAT_PROFILE
2734 VIM_CLEAR(fp->uf_tml_count);
2735 VIM_CLEAR(fp->uf_tml_total);
2736 VIM_CLEAR(fp->uf_tml_self);
2737#endif
2738}
2739
2740/*
2741 * Free all things that a function contains. Does not free the function
2742 * itself, use func_free() for that.
2743 * When "force" is TRUE we are exiting.
2744 */
2745 static void
2746func_clear(ufunc_T *fp, int force)
2747{
2748 if (fp->uf_cleared)
2749 return;
2750 fp->uf_cleared = TRUE;
2751
2752 // clear this function
2753 func_clear_items(fp);
2754 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002755 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756}
2757
2758/*
2759 * Free a function and remove it from the list of functions. Does not free
2760 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002761 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002762 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002764 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002765func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766{
2767 // Only remove it when not done already, otherwise we would remove a newer
2768 // version of the function with the same name.
2769 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2770 func_remove(fp);
2771
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002772 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002773 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002774 if (fp->uf_dfunc_idx > 0)
2775 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002776 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002778 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002779 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002780 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781}
2782
2783/*
2784 * Free all things that a function contains and free the function itself.
2785 * When "force" is TRUE we are exiting.
2786 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002787 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788func_clear_free(ufunc_T *fp, int force)
2789{
2790 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002791 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2792 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002793 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002794 else
2795 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796}
2797
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002798/*
2799 * Copy already defined function "lambda" to a new function with name "global".
2800 * This is for when a compiled function defines a global function.
2801 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002802 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002803copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002804 char_u *lambda,
2805 char_u *global,
2806 loopvarinfo_T *loopvarinfo,
2807 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002808{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002809 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002810 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002811
2812 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002813 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002814 semsg(_(e_lambda_function_not_found_str), lambda);
2815 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002816 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002817
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002818 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002819 if (fp != NULL)
2820 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002821 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002822 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002823 return FAIL;
2824 }
2825
Bram Moolenaare01e5212023-01-08 20:31:18 +00002826 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002827 if (fp == NULL)
2828 return FAIL;
2829
2830 fp->uf_varargs = ufunc->uf_varargs;
2831 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2832 fp->uf_def_status = ufunc->uf_def_status;
2833 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2834 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2835 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2836 == FAIL
2837 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2838 goto failed;
2839
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002840 if (ufunc->uf_arg_types != NULL)
2841 {
2842 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2843 if (fp->uf_arg_types == NULL)
2844 goto failed;
2845 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2846 sizeof(type_T *) * fp->uf_args.ga_len);
2847 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002848 if (ufunc->uf_va_name != NULL)
2849 {
2850 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2851 if (fp->uf_va_name == NULL)
2852 goto failed;
2853 }
2854 fp->uf_ret_type = ufunc->uf_ret_type;
2855
2856 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002857
2858 fp->uf_name_exp = NULL;
2859 set_ufunc_name(fp, global);
2860
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002861 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002862
2863 // the referenced dfunc_T is now used one more time
2864 link_def_function(fp);
2865
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002866 // Create a partial to store the context of the function where it was
2867 // instantiated. Only needs to be done once. Do this on the original
2868 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002869 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2870 {
2871 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2872
2873 if (pt == NULL)
2874 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002875 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002876 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002877 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002878 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002879 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002880 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002881 }
2882
2883 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002884
2885failed:
2886 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002887 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002888}
2889
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002890static int funcdepth = 0;
2891
2892/*
2893 * Increment the function call depth count.
2894 * Return FAIL when going over 'maxfuncdepth'.
2895 * Otherwise return OK, must call funcdepth_decrement() later!
2896 */
2897 int
2898funcdepth_increment(void)
2899{
2900 if (funcdepth >= p_mfd)
2901 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002902 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002903 return FAIL;
2904 }
2905 ++funcdepth;
2906 return OK;
2907}
2908
2909 void
2910funcdepth_decrement(void)
2911{
2912 --funcdepth;
2913}
2914
2915/*
2916 * Get the current function call depth.
2917 */
2918 int
2919funcdepth_get(void)
2920{
2921 return funcdepth;
2922}
2923
2924/*
2925 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002926 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002927 * times as funcdepth_increment().
2928 */
2929 void
2930funcdepth_restore(int depth)
2931{
2932 funcdepth = depth;
2933}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002934
2935/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002936 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2937 * "rettv".
2938 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2939 * Returns NULL when allocation fails.
2940 */
2941 funccall_T *
2942create_funccal(ufunc_T *fp, typval_T *rettv)
2943{
2944 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2945
2946 if (fc == NULL)
2947 return NULL;
2948 fc->fc_caller = current_funccal;
2949 current_funccal = fc;
2950 fc->fc_func = fp;
2951 func_ptr_ref(fp);
2952 fc->fc_rettv = rettv;
2953 return fc;
2954}
2955
2956/*
2957 * To be called when returning from a compiled function; restores
2958 * current_funccal.
2959 */
2960 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002961remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002962{
2963 funccall_T *fc = current_funccal;
2964
2965 current_funccal = fc->fc_caller;
2966 free_funccal(fc);
2967}
2968
2969/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002970 * Call a user function.
2971 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002972 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002973call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002974 ufunc_T *fp, // pointer to function
2975 int argcount, // nr of args
2976 typval_T *argvars, // arguments
2977 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002978 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002979 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002980{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002981 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002982 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002983 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002984 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002985 funccall_T *fc;
2986 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002987 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002988 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002989 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002990 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002991 int i;
2992 int ai;
2993 int islambda = FALSE;
2994 char_u numbuf[NUMBUFLEN];
2995 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002996 typval_T *tv_to_free[MAX_FUNC_ARGS];
2997 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002998#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002999 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003000#endif
ichizok7e5fe382023-04-15 13:17:50 +01003001 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003002
Bram Moolenaarb2049902021-01-24 12:53:53 +01003003#ifdef FEAT_PROFILE
3004 CLEAR_FIELD(profile_info);
3005#endif
3006
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003007 // If depth of calling is getting too high, don't execute the function.
3008 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003009 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003010 rettv->v_type = VAR_NUMBER;
3011 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003012 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003013 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003014
Bram Moolenaare38eab22019-12-05 21:50:01 +01003015 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003016
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01003017 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01003018 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003019 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003020 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003021 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003022 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
3023 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003024 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003025 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003026
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003027 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01003029#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01003030 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003031#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003032 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01003033#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003034 if (do_profiling == PROF_YES)
3035 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01003036#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003037 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003038 if (call_def_function(fp, argcount, argvars, 0,
3039 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
3040 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003041 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01003042#ifdef FEAT_PROFILE
3043 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01003044 || (caller != NULL && caller->uf_profiling)))
3045 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01003046#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01003047 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003048 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003049 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 }
3051
Bram Moolenaar38453522021-11-28 22:00:12 +00003052 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003053
3054 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01003055 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
3056 * variables with names up to VAR_SHORT_LEN long. This avoids having to
3057 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003058 */
3059 /*
3060 * Init l: variables.
3061 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01003062 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003063 if (selfdict != NULL)
3064 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003065 // Set l:self to "selfdict". Use "name" to avoid a warning from
3066 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003067 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003068 name = v->di_key;
3069 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01003070 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003071 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003072 v->di_tv.v_type = VAR_DICT;
3073 v->di_tv.v_lock = 0;
3074 v->di_tv.vval.v_dict = selfdict;
3075 ++selfdict->dv_refcount;
3076 }
3077
3078 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003079 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003080 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003081 * Set a:000 to a list with room for the "..." arguments.
3082 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01003083 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003084 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01003085 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003086 (varnumber_T)(argcount >= fp->uf_args.ga_len
3087 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01003088 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003089 if ((fp->uf_flags & FC_NOARGS) == 0)
3090 {
3091 // Use "name" to avoid a warning from some compiler that checks the
3092 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003093 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003094 name = v->di_key;
3095 STRCPY(name, "000");
3096 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003097 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003098 v->di_tv.v_type = VAR_LIST;
3099 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003100 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003101 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01003102 CLEAR_FIELD(fc->fc_l_varlist);
3103 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
3104 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003105
3106 /*
3107 * Set a:firstline to "firstline" and a:lastline to "lastline".
3108 * Set a:name to named arguments.
3109 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003110 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003111 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003112 if ((fp->uf_flags & FC_NOARGS) == 0)
3113 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003114 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3115 "firstline", (varnumber_T)funcexe->fe_firstline);
3116 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3117 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003118 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003119 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003120 {
3121 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003122 typval_T def_rettv;
3123 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003124
3125 ai = i - fp->uf_args.ga_len;
3126 if (ai < 0)
3127 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003128 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003129 name = FUNCARG(fp, i);
3130 if (islambda)
3131 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003132
3133 // evaluate named argument default expression
3134 isdefault = ai + fp->uf_def_args.ga_len >= 0
3135 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
3136 && argvars[i].vval.v_number == VVAL_NONE));
3137 if (isdefault)
3138 {
3139 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003140
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003141 def_rettv.v_type = VAR_NUMBER;
3142 def_rettv.vval.v_number = -1;
3143
3144 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
3145 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003146 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003147 {
3148 default_arg_err = 1;
3149 break;
3150 }
3151 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003152 }
3153 else
3154 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003155 if ((fp->uf_flags & FC_NOARGS) != 0)
3156 // Bail out if no a: arguments used (in lambda).
3157 break;
3158
Bram Moolenaare38eab22019-12-05 21:50:01 +01003159 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003160 sprintf((char *)numbuf, "%d", ai + 1);
3161 name = numbuf;
3162 }
3163 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
3164 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003165 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003166 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003167 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003168 }
3169 else
3170 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003171 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003172 if (v == NULL)
3173 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003174 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003175 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003176
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003177 // Note: the values are copied directly to avoid alloc/free.
3178 // "argvars" must have VAR_FIXED for v_lock.
3179 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003180 v->di_tv.v_lock = VAR_FIXED;
3181
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003182 if (isdefault)
3183 // Need to free this later, no matter where it's stored.
3184 tv_to_free[tv_to_free_len++] = &v->di_tv;
3185
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003186 if (addlocal)
3187 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003188 // Named arguments should be accessed without the "a:" prefix in
3189 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003190 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003191 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003192 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003193 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003194 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003195
3196 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3197 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003198 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003199
3200 li->li_tv = argvars[i];
3201 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003202 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003203 }
3204 }
3205
Bram Moolenaare38eab22019-12-05 21:50:01 +01003206 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003207 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003208
3209 if (fp->uf_flags & FC_SANDBOX)
3210 {
3211 using_sandbox = TRUE;
3212 ++sandbox;
3213 }
3214
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003215 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003216 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003217 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003218 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003219 ++no_wait_return;
3220 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003221
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003222 smsg(_("calling %s"), SOURCING_NAME);
3223 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003224 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003225 char_u buf[MSG_BUF_LEN];
3226 char_u numbuf2[NUMBUFLEN];
3227 char_u *tofree;
3228 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003229
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003230 msg_puts("(");
3231 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003232 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003233 if (i > 0)
3234 msg_puts(", ");
3235 if (argvars[i].v_type == VAR_NUMBER)
3236 msg_outnum((long)argvars[i].vval.v_number);
3237 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003238 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003239 // Do not want errors such as E724 here.
3240 ++emsg_off;
3241 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3242 --emsg_off;
3243 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003244 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003245 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003246 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003247 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3248 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003249 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003250 msg_puts((char *)s);
3251 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003252 }
3253 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003254 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003255 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003256 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003257 msg_puts("\n"); // don't overwrite this either
3258
3259 verbose_leave_scroll();
3260 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003261 }
3262#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003263 if (do_profiling == PROF_YES)
3264 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003265 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003266#endif
3267
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003268 // "legacy" does not apply to commands in the function
3269 sticky_cmdmod_flags = 0;
3270
Bram Moolenaar98aff652022-09-06 21:02:35 +01003271 // If called from a compiled :def function the execution context must be
3272 // hidden, any deferred functions need to be added to the function being
3273 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003274 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003275
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003276 save_current_sctx = current_sctx;
3277 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003278 save_did_emsg = did_emsg;
3279 did_emsg = FALSE;
3280
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003281 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003282 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003283 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003284 retval = FCERR_FAILED;
3285 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003286 else if (islambda)
3287 {
3288 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3289
3290 // A Lambda always has the command "return {expr}". It is much faster
3291 // to evaluate {expr} directly.
3292 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003293 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003294 --ex_nesting_level;
3295 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003296 else
3297 // call do_cmdline() to execute the lines
3298 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003299 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3300
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003301 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003302 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003303
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003304 if (RedrawingDisabled > 0)
3305 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003306
Bram Moolenaare38eab22019-12-05 21:50:01 +01003307 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003308 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3309 {
3310 clear_tv(rettv);
3311 rettv->v_type = VAR_NUMBER;
3312 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003313
3314 // In corner cases returning a "failed" value is not backwards
3315 // compatible. Only do this for Vim9 script.
3316 if (in_vim9script())
3317 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003318 }
3319
3320#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003321 if (do_profiling == PROF_YES)
3322 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003323 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003324
3325 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3326 profile_may_end_func(&profile_info, fp, caller);
3327 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003328#endif
3329
Bram Moolenaare38eab22019-12-05 21:50:01 +01003330 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003331 if (p_verbose >= 12)
3332 {
3333 ++no_wait_return;
3334 verbose_enter_scroll();
3335
3336 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003337 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003338 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003339 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003340 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003341 else
3342 {
3343 char_u buf[MSG_BUF_LEN];
3344 char_u numbuf2[NUMBUFLEN];
3345 char_u *tofree;
3346 char_u *s;
3347
Bram Moolenaare38eab22019-12-05 21:50:01 +01003348 // The value may be very long. Skip the middle part, so that we
3349 // have some idea how it starts and ends. smsg() would always
3350 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003351 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003352 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003353 --emsg_off;
3354 if (s != NULL)
3355 {
3356 if (vim_strsize(s) > MSG_BUF_CLEN)
3357 {
3358 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3359 s = buf;
3360 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003361 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003362 vim_free(tofree);
3363 }
3364 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003365 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003366
3367 verbose_leave_scroll();
3368 --no_wait_return;
3369 }
3370
ichizok7e5fe382023-04-15 13:17:50 +01003371 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003372 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003373 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003374 restore_current_ectx(save_current_ectx);
3375
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003376#ifdef FEAT_PROFILE
3377 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003378 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003379#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003380 if (using_sandbox)
3381 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003382 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003383
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003384 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003385 {
3386 ++no_wait_return;
3387 verbose_enter_scroll();
3388
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003389 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003390 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003391
3392 verbose_leave_scroll();
3393 --no_wait_return;
3394 }
3395
3396 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003397 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003398 for (i = 0; i < tv_to_free_len; ++i)
3399 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003400 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003401
3402 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003403}
3404
3405/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003406 * Check the argument count for user function "fp".
3407 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3408 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003409 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003410check_user_func_argcount(ufunc_T *fp, int argcount)
3411{
3412 int regular_args = fp->uf_args.ga_len;
3413
3414 if (argcount < regular_args - fp->uf_def_args.ga_len)
3415 return FCERR_TOOFEW;
3416 else if (!has_varargs(fp) && argcount > regular_args)
3417 return FCERR_TOOMANY;
3418 return FCERR_UNKNOWN;
3419}
3420
3421/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003423 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003424 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425call_user_func_check(
3426 ufunc_T *fp,
3427 int argcount,
3428 typval_T *argvars,
3429 typval_T *rettv,
3430 funcexe_T *funcexe,
3431 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003432{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003433 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003434
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003435#ifdef FEAT_LUA
3436 if (fp->uf_flags & FC_CFUNC)
3437 {
3438 cfunc_T cb = fp->uf_cb;
3439
3440 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3441 }
3442#endif
3443
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003444 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3445 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003446 error = check_user_func_argcount(fp, argcount);
3447 if (error != FCERR_UNKNOWN)
3448 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003449
Bram Moolenaar50824712020-12-20 21:10:17 +01003450 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003451 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003453 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003455 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 int did_save_redo = FALSE;
3457 save_redo_T save_redo;
3458
3459 /*
3460 * Call the user function.
3461 * Save and restore search patterns, script variables and
3462 * redo buffer.
3463 */
3464 save_search_patterns();
3465 if (!ins_compl_active())
3466 {
3467 saveRedobuff(&save_redo);
3468 did_save_redo = TRUE;
3469 }
3470 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003471 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003472 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3474 // Function was unreferenced while being used, free it now.
3475 func_clear_free(fp, FALSE);
3476 if (did_save_redo)
3477 restoreRedobuff(&save_redo);
3478 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003479 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003482}
3483
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003484static funccal_entry_T *funccal_stack = NULL;
3485
3486/*
3487 * Save the current function call pointer, and set it to NULL.
3488 * Used when executing autocommands and for ":source".
3489 */
3490 void
3491save_funccal(funccal_entry_T *entry)
3492{
3493 entry->top_funccal = current_funccal;
3494 entry->next = funccal_stack;
3495 funccal_stack = entry;
3496 current_funccal = NULL;
3497}
3498
3499 void
3500restore_funccal(void)
3501{
3502 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003503 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003504 else
3505 {
3506 current_funccal = funccal_stack->top_funccal;
3507 funccal_stack = funccal_stack->next;
3508 }
3509}
3510
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003511 funccall_T *
3512get_current_funccal(void)
3513{
3514 return current_funccal;
3515}
3516
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003517/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003518 * Return TRUE when currently at the script level:
3519 * - not in a function
3520 * - not executing an autocommand
3521 * Note that when an autocommand sources a script the result is FALSE;
3522 */
3523 int
3524at_script_level(void)
3525{
3526 return current_funccal == NULL && autocmd_match == NULL;
3527}
3528
3529/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003530 * Mark all functions of script "sid" as deleted.
3531 */
3532 void
3533delete_script_functions(int sid)
3534{
3535 hashitem_T *hi;
3536 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003537 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003538 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003539 size_t len;
3540
3541 buf[0] = K_SPECIAL;
3542 buf[1] = KS_EXTRA;
3543 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003544 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003545 len = STRLEN(buf);
3546
Bram Moolenaarce658352020-07-31 23:47:12 +02003547 while (todo > 0)
3548 {
3549 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003550 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003551 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003552 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003553 fp = HI2UF(hi);
3554 if (STRNCMP(fp->uf_name, buf, len) == 0)
3555 {
3556 int changed = func_hashtab.ht_changed;
3557
3558 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003559
3560 if (fp->uf_calls > 0)
3561 {
3562 // Function is executing, don't free it but do remove
3563 // it from the hashtable.
3564 if (func_remove(fp))
3565 fp->uf_refcount--;
3566 }
3567 else
3568 {
3569 func_clear(fp, TRUE);
3570 // When clearing a function another function can be
3571 // cleared as a side effect. When that happens start
3572 // over.
3573 if (changed != func_hashtab.ht_changed)
3574 break;
3575 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003576 }
3577 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003578 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003579 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003580}
3581
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003582#if defined(EXITFREE) || defined(PROTO)
3583 void
3584free_all_functions(void)
3585{
3586 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003587 ufunc_T *fp;
3588 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003589 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003590 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003591
Bram Moolenaare38eab22019-12-05 21:50:01 +01003592 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003593 while (current_funccal != NULL)
3594 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003595 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003596 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003597 if (current_funccal == NULL && funccal_stack != NULL)
3598 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003599 }
3600
Bram Moolenaare38eab22019-12-05 21:50:01 +01003601 // First clear what the functions contain. Since this may lower the
3602 // reference count of a function, it may also free a function and change
3603 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003604 while (todo > 0)
3605 {
3606 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003607 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003608 if (!HASHITEM_EMPTY(hi))
3609 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 // clear the def function index now
3611 fp = HI2UF(hi);
3612 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003613 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614
Bram Moolenaare38eab22019-12-05 21:50:01 +01003615 // Only free functions that are not refcounted, those are
3616 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003617 if (func_name_refcount(fp->uf_name))
3618 ++skipped;
3619 else
3620 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003621 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003622 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003623 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003624 {
3625 skipped = 0;
3626 break;
3627 }
3628 }
3629 --todo;
3630 }
3631 }
3632
Bram Moolenaare38eab22019-12-05 21:50:01 +01003633 // Now actually free the functions. Need to start all over every time,
3634 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003635 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003636 while (func_hashtab.ht_used > skipped)
3637 {
3638 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003639 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003640 if (!HASHITEM_EMPTY(hi))
3641 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003642 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003643 // Only free functions that are not refcounted, those are
3644 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003645 fp = HI2UF(hi);
3646 if (func_name_refcount(fp->uf_name))
3647 ++skipped;
3648 else
3649 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003650 if (func_free(fp, FALSE) == OK)
3651 {
3652 skipped = 0;
3653 break;
3654 }
3655 // did not actually free it
3656 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003657 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003658 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003659 }
3660 if (skipped == 0)
3661 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662
3663 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003664}
3665#endif
3666
3667/*
3668 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003669 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3670 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003671 * "len" is the length of "name", or -1 for NUL terminated.
3672 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003674builtin_function(char_u *name, int len)
3675{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003676 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003677
Bram Moolenaara26b9702020-04-18 19:53:28 +02003678 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003679 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003680 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3681 {
3682 if (name[i] == AUTOLOAD_CHAR)
3683 return FALSE;
3684 if (!eval_isnamec(name[i]))
3685 {
3686 // "name.something" is not a builtin function
3687 if (name[i] == '.')
3688 return FALSE;
3689 break;
3690 }
3691 }
3692 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003693}
3694
3695 int
3696func_call(
3697 char_u *name,
3698 typval_T *args,
3699 partial_T *partial,
3700 dict_T *selfdict,
3701 typval_T *rettv)
3702{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003703 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003704 listitem_T *item;
3705 typval_T argv[MAX_FUNC_ARGS + 1];
3706 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003707 int r = 0;
3708
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003709 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003710 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003711 {
3712 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3713 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003714 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003715 break;
3716 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003717 // Make a copy of each argument. This is needed to be able to set
3718 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003719 copy_tv(&item->li_tv, &argv[argc++]);
3720 }
3721
3722 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003723 {
3724 funcexe_T funcexe;
3725
Bram Moolenaara80faa82020-04-12 19:37:17 +02003726 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003727 funcexe.fe_firstline = curwin->w_cursor.lnum;
3728 funcexe.fe_lastline = curwin->w_cursor.lnum;
3729 funcexe.fe_evaluate = TRUE;
3730 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003731 if (partial != NULL)
3732 {
3733 funcexe.fe_object = partial->pt_obj;
3734 if (funcexe.fe_object != NULL)
3735 ++funcexe.fe_object->obj_refcount;
3736 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003737 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003738 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3739 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003740
Bram Moolenaare38eab22019-12-05 21:50:01 +01003741 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003742 while (argc > 0)
3743 clear_tv(&argv[--argc]);
3744
3745 return r;
3746}
3747
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003748static int callback_depth = 0;
3749
3750 int
3751get_callback_depth(void)
3752{
3753 return callback_depth;
3754}
3755
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003756/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003757 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003758 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003759 */
3760 int
3761call_callback(
3762 callback_T *callback,
3763 int len, // length of "name" or -1 to use strlen()
3764 typval_T *rettv, // return value goes here
3765 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003766 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003767 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003768{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003769 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003770 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003771
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003772 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3773 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003774
zeertzjqfe583b12023-12-21 16:59:26 +01003775 if (callback_depth > p_mfd)
Christian Brabandt47510f32023-10-15 09:56:16 +02003776 {
3777 emsg(_(e_command_too_recursive));
3778 return FAIL;
3779 }
3780
Bram Moolenaara80faa82020-04-12 19:37:17 +02003781 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003782 funcexe.fe_evaluate = TRUE;
3783 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003784 if (callback->cb_partial != NULL)
3785 {
3786 funcexe.fe_object = callback->cb_partial->pt_obj;
3787 if (funcexe.fe_object != NULL)
3788 ++funcexe.fe_object->obj_refcount;
3789 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003790 ++callback_depth;
3791 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3792 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003793
3794 // When a :def function was called that uses :try an error would be turned
3795 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003796 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003797 {
3798 need_rethrow = FALSE;
3799 handle_did_throw();
3800 }
3801
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003802 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003803}
3804
3805/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003806 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003807 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003808 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3809 */
3810 varnumber_T
3811call_callback_retnr(
3812 callback_T *callback,
3813 int argcount, // number of "argvars"
3814 typval_T *argvars) // vars for arguments, must have "argcount"
3815 // PLUS ONE elements!
3816{
3817 typval_T rettv;
3818 varnumber_T retval;
3819
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003820 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003821 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003822
3823 retval = tv_get_number_chk(&rettv, NULL);
3824 clear_tv(&rettv);
3825 return retval;
3826}
3827
3828/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 * Give an error message for the result of a function.
3830 * Nothing if "error" is FCERR_NONE.
3831 */
3832 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003833user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834{
3835 switch (error)
3836 {
3837 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003838 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003839 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003840 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003841 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 break;
3843 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003844 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 break;
3846 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003847 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 break;
3849 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003850 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 break;
3852 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003853 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 break;
3855 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003856 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 break;
3858 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003859 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3860 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003862 case FCERR_OTHER:
3863 case FCERR_FAILED:
3864 // assume the error message was already given
3865 break;
3866 case FCERR_NONE:
3867 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 }
3869}
3870
3871/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003872 * Check the argument types "argvars[argcount]" for "name" using the
3873 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3874 * is already included in "argvars[]".
3875 * Will do nothing if "funcexe->fe_check_type" is NULL or
3876 * "funcexe->fe_evaluate" is FALSE;
3877 * Returns an FCERR_ value.
3878 */
3879 static funcerror_T
3880may_check_argument_types(
3881 funcexe_T *funcexe,
3882 typval_T *argvars,
3883 int argcount,
3884 int base_included,
3885 char_u *name)
3886{
3887 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3888 {
3889 // Check that the argument types are OK for the types of the funcref.
3890 if (check_argument_types(funcexe->fe_check_type,
3891 argvars, argcount,
3892 base_included ? NULL : funcexe->fe_basetv,
3893 name) == FAIL)
3894 return FCERR_OTHER;
3895 }
3896 return FCERR_NONE;
3897}
3898
3899/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003900 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003901 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003902 * Return FAIL when the function can't be called, OK otherwise.
3903 * Also returns OK when an error was encountered while executing the function.
3904 */
3905 int
3906call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003907 char_u *funcname, // name of the function
3908 int len, // length of "name" or -1 to use strlen()
3909 typval_T *rettv, // return value goes here
3910 int argcount_in, // number of "argvars"
3911 typval_T *argvars_in, // vars for arguments, must have "argcount"
3912 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003913 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003914{
3915 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003916 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003917 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003918 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003919 char_u fname_buf[FLEN_FIXED + 1];
3920 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003921 char_u *fname = NULL;
3922 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003923 int argcount = argcount_in;
3924 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003925 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003926 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003927 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003928 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003929 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003930 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003931 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003932 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003933
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003934 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3935 // even when call_func() returns FAIL.
3936 rettv->v_type = VAR_UNKNOWN;
3937
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003938 if (partial != NULL)
3939 fp = partial->pt_func;
3940 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003941 fp = funcexe->fe_ufunc;
3942
3943 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003944 {
3945 // Make a copy of the name, if it comes from a funcref variable it
3946 // could be changed or deleted in the called function.
3947 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3948 if (name == NULL)
3949 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003950
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003951 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3952 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003953
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003954 if (funcexe->fe_doesrange != NULL)
3955 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003956
3957 if (partial != NULL)
3958 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003959 // When the function has a partial with a dict and there is a dict
3960 // argument, use the dict argument. That is backwards compatible.
3961 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003962 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003963 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003964 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003965 {
3966 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003967 {
3968 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3969 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003970 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003971 goto theend;
3972 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003973 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003974 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003975 for (i = 0; i < argcount_in; ++i)
3976 argv[i + argv_clear] = argvars_in[i];
3977 argvars = argv;
3978 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003979
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003980 if (funcexe->fe_check_type != NULL
3981 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003982 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003983 // Now funcexe->fe_check_type is missing the added arguments,
3984 // make a copy of the type with the correction.
3985 check_type = *funcexe->fe_check_type;
3986 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003987 check_type.tt_args = check_type_args;
3988 CLEAR_FIELD(check_type_args);
3989 for (i = 0; i < check_type.tt_argcount; ++i)
3990 check_type_args[i + partial->pt_argc] =
3991 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003992 check_type.tt_argcount += partial->pt_argc;
3993 check_type.tt_min_argcount += partial->pt_argc;
3994 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003995 }
3996 }
3997
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003998 if (error == FCERR_NONE)
3999 // check the argument types if possible
4000 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
4001 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004002
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004003 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004004 {
4005 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02004006 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004007
Bram Moolenaar333894b2020-08-01 18:53:07 +02004008 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02004009 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02004010 {
4011 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004012 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02004013 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004014
Bram Moolenaare38eab22019-12-05 21:50:01 +01004015 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004016 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01004017 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004018
Bram Moolenaarf10806b2020-04-02 18:34:35 +02004019 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004020 {
4021 /*
4022 * User defined function.
4023 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02004024 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004025 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004026 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004027 if (fp != NULL && !is_global && in_vim9script()
4028 && func_requires_g_prefix(fp))
4029 // In Vim9 script g: is required to find a global
4030 // non-autoload function.
4031 fp = NULL;
4032 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004033
Bram Moolenaare38eab22019-12-05 21:50:01 +01004034 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004035 if (fp == NULL
4036 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004037 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004038 && !aborting())
4039 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004040 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004041 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004042 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004043 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004044 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
4045 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004046 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004047 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004048 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02004049 if (fp == NULL)
4050 {
4051 char_u *p = untrans_function_name(rfname);
4052
4053 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02004054 // Don't do this if the name starts with "s:".
4055 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004056 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004057 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004058
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004059 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01004060 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004061 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004062 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004063 int need_arg_check = FALSE;
4064 if (funcexe->fe_check_type == NULL)
4065 {
4066 funcexe->fe_check_type = fp->uf_func_type;
4067 need_arg_check = TRUE;
4068 }
4069
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004070 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004071 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01004072 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004073 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004074 argv_clear, fp);
4075 need_arg_check = TRUE;
4076 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02004077
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004078 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004079 {
4080 // Method call: base->Method()
4081 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004082 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004083 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004084 argvars = argv;
4085 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004086 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004087 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004088
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004089 // Check the argument types now that the function type and all
4090 // argument values are known, if not done above.
4091 if (need_arg_check)
4092 error = may_check_argument_types(funcexe, argvars, argcount,
4093 TRUE, (name != NULL) ? name : funcname);
4094 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
4095 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004097 }
4098 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004099 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004100 {
4101 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004102 * expr->method(): Find the method name in the table, call its
4103 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02004104 */
4105 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004106 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004107 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004108 else
4109 {
4110 /*
4111 * Find the function name in the table, call its implementation.
4112 */
4113 error = call_internal_func(fname, argcount, argvars, rettv);
4114 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02004115
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004116 /*
4117 * The function call (or "FuncUndefined" autocommand sequence) might
4118 * have been aborted by an error, an interrupt, or an explicitly thrown
4119 * exception that has not been caught so far. This situation can be
4120 * tested for by calling aborting(). For an error in an internal
4121 * function or for the "E132" error in call_user_func(), however, the
4122 * throw point at which the "force_abort" flag (temporarily reset by
4123 * emsg()) is normally updated has not been reached yet. We need to
4124 * update that flag first to make aborting() reliable.
4125 */
4126 update_force_abort();
4127 }
Bram Moolenaaref140542019-12-31 21:27:13 +01004128 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004129 ret = OK;
4130
Bram Moolenaar4c054e92019-11-10 00:13:50 +01004131theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004132 /*
4133 * Report an error unless the argument evaluation or function call has been
4134 * cancelled due to an aborting error, an interrupt, or an exception.
4135 */
4136 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004137 user_func_error(error, (name != NULL) ? name : funcname,
4138 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004139
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004140 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004141 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004142 clear_tv(&argv[--argv_clear + argv_base]);
4143
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004144 vim_free(tofree);
4145 vim_free(name);
4146
4147 return ret;
4148}
4149
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004150/*
4151 * Call a function without arguments, partial or dict.
4152 * This is like call_func() when the call is only "FuncName()".
4153 * To be used by "expr" options.
4154 * Returns NOTDONE when the function could not be found.
4155 */
4156 int
4157call_simple_func(
4158 char_u *funcname, // name of the function
zeertzjqc1ed7882024-08-01 22:46:54 +02004159 size_t len, // length of "name"
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004160 typval_T *rettv) // return value goes here
4161{
4162 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00004163 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004164 char_u fname_buf[FLEN_FIXED + 1];
4165 char_u *tofree = NULL;
4166 char_u *name;
4167 char_u *fname;
4168 char_u *rfname;
4169 int is_global = FALSE;
4170 ufunc_T *fp;
4171
4172 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4173 rettv->vval.v_number = 0;
4174
4175 // Make a copy of the name, an option can be changed in the function.
4176 name = vim_strnsave(funcname, len);
4177 if (name == NULL)
4178 return ret;
4179
4180 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4181
4182 // Skip "g:" before a function name.
4183 if (fname[0] == 'g' && fname[1] == ':')
4184 {
4185 is_global = TRUE;
4186 rfname = fname + 2;
4187 }
4188 else
4189 rfname = fname;
4190 fp = find_func(rfname, is_global);
4191 if (fp != NULL && !is_global && in_vim9script()
4192 && func_requires_g_prefix(fp))
4193 // In Vim9 script g: is required to find a global non-autoload
4194 // function.
4195 fp = NULL;
4196 if (fp == NULL)
4197 ret = NOTDONE;
4198 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4199 error = FCERR_DELETED;
4200 else if (fp != NULL)
4201 {
4202 typval_T argvars[1];
4203 funcexe_T funcexe;
4204
4205 argvars[0].v_type = VAR_UNKNOWN;
4206 CLEAR_FIELD(funcexe);
4207 funcexe.fe_evaluate = TRUE;
4208
4209 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4210 if (error == FCERR_NONE)
4211 ret = OK;
4212 }
4213
4214 user_func_error(error, name, FALSE);
4215 vim_free(tofree);
4216 vim_free(name);
4217
4218 return ret;
4219}
4220
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004221 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222printable_func_name(ufunc_T *fp)
4223{
4224 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4225}
4226
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004227/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004228 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4229 * TRUE. Otherwise return FALSE.
4230 */
4231 static int
4232function_list_modified(int prev_ht_changed)
4233{
4234 if (prev_ht_changed != func_hashtab.ht_changed)
4235 {
4236 emsg(_(e_function_list_was_modified));
4237 return TRUE;
4238 }
4239 return FALSE;
4240}
4241
4242/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004243 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004244 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004245 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004246list_func_head(ufunc_T *fp, int indent)
4247{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004248 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004249 int j;
4250
4251 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004252
4253 // a timer at the more prompt may have deleted the function
4254 if (function_list_modified(prev_ht_changed))
4255 return FAIL;
4256
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004257 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004258 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004259 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004260 msg_puts("def ");
4261 else
4262 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004264 msg_putchar('(');
4265 for (j = 0; j < fp->uf_args.ga_len; ++j)
4266 {
4267 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004268 msg_puts(", ");
4269 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 if (fp->uf_arg_types != NULL)
4271 {
4272 char *tofree;
4273
4274 msg_puts(": ");
4275 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4276 vim_free(tofree);
4277 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004278 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4279 {
4280 msg_puts(" = ");
4281 msg_puts(((char **)(fp->uf_def_args.ga_data))
4282 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4283 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004284 }
4285 if (fp->uf_varargs)
4286 {
4287 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004288 msg_puts(", ");
4289 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004290 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 if (fp->uf_va_name != NULL)
4292 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004293 if (!fp->uf_varargs)
4294 {
4295 if (j)
4296 msg_puts(", ");
4297 msg_puts("...");
4298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004300 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 {
4302 char *tofree;
4303
4304 msg_puts(": ");
4305 msg_puts(type_name(fp->uf_va_type, &tofree));
4306 vim_free(tofree);
4307 }
4308 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004309 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004310
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004311 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004312 {
4313 if (fp->uf_ret_type != &t_void)
4314 {
4315 char *tofree;
4316
4317 msg_puts(": ");
4318 msg_puts(type_name(fp->uf_ret_type, &tofree));
4319 vim_free(tofree);
4320 }
4321 }
4322 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004323 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004324 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004325 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004326 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004327 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004328 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004329 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004330 msg_clr_eos();
4331 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004332 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004333
4334 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004335}
4336
4337/*
4338 * Get a function name, translating "<SID>" and "<SNR>".
4339 * Also handles a Funcref in a List or Dictionary.
4340 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004341 * Set "*is_global" to TRUE when the function must be global, unless
4342 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004343 * flags:
4344 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004345 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004346 * TFN_QUIET: be quiet
4347 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004348 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004349 * Advances "pp" to just after the function name (if no error).
4350 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004351 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004352trans_function_name(
4353 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004354 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004355 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004356 int flags)
4357{
4358 return trans_function_name_ext(pp, is_global, skip, flags,
4359 NULL, NULL, NULL, NULL);
4360}
4361
4362/*
4363 * trans_function_name() with extra arguments.
4364 * "fdp", "partial", "type" and "ufunc" can be NULL.
4365 */
Yegappan Lakshmanana04003a2024-10-27 21:54:11 +01004366 static char_u *
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004367trans_function_name_ext(
4368 char_u **pp,
4369 int *is_global,
4370 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004371 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004372 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004373 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004374 type_T **type, // return: type of funcref
4375 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004376{
4377 char_u *name = NULL;
4378 char_u *start;
4379 char_u *end;
4380 int lead;
4381 char_u sid_buf[20];
4382 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004384 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004385 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004386 int vim9script = in_vim9script();
4387 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004388
4389 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004390 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004391 start = *pp;
4392
Bram Moolenaare38eab22019-12-05 21:50:01 +01004393 // Check for hard coded <SNR>: already translated function ID (from a user
4394 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004395 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4396 && (*pp)[2] == (int)KE_SNR)
4397 {
4398 *pp += 3;
4399 len = get_id_len(pp) + 3;
4400 return vim_strnsave(start, len);
4401 }
4402
Bram Moolenaare38eab22019-12-05 21:50:01 +01004403 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4404 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004405 lead = eval_fname_script(start);
4406 if (lead > 2)
4407 start += lead;
4408
Bram Moolenaare38eab22019-12-05 21:50:01 +01004409 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004410 end = get_lval(start, NULL, &lv, FALSE, skip,
4411 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004412 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004413 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004414 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004415 {
4416 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004417 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004418 goto theend;
4419 }
4420 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4421 {
4422 /*
4423 * Report an invalid expression in braces, unless the expression
4424 * evaluation has been cancelled due to an aborting error, an
4425 * interrupt, or an exception.
4426 */
4427 if (!aborting())
4428 {
4429 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004430 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004431 }
4432 else
4433 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4434 goto theend;
4435 }
4436
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004437 if (lv.ll_ufunc != NULL)
4438 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004439 if (ufunc != NULL)
4440 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004441 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004442 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004443 goto theend;
4444 }
4445
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004446 if (lv.ll_tv != NULL)
4447 {
4448 if (fdp != NULL)
4449 {
4450 fdp->fd_dict = lv.ll_dict;
4451 fdp->fd_newkey = lv.ll_newkey;
4452 lv.ll_newkey = NULL;
4453 fdp->fd_di = lv.ll_di;
4454 }
4455 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4456 {
4457 name = vim_strsave(lv.ll_tv->vval.v_string);
4458 *pp = end;
4459 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004460 else if (lv.ll_tv->v_type == VAR_CLASS
4461 && lv.ll_tv->vval.v_class != NULL)
4462 {
4463 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4464 *pp = end;
4465 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004466 else if (lv.ll_tv->v_type == VAR_PARTIAL
4467 && lv.ll_tv->vval.v_partial != NULL)
4468 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004469 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004470 *pp = end;
4471 if (partial != NULL)
4472 *partial = lv.ll_tv->vval.v_partial;
4473 }
4474 else
4475 {
4476 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4477 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004478 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004479 else
4480 *pp = end;
4481 name = NULL;
4482 }
4483 goto theend;
4484 }
4485
4486 if (lv.ll_name == NULL)
4487 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004488 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004489 *pp = end;
4490 goto theend;
4491 }
4492
Bram Moolenaare38eab22019-12-05 21:50:01 +01004493 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004494 if (lv.ll_exp_name != NULL)
4495 {
4496 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004497 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004498 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004499 if (name == lv.ll_exp_name)
4500 name = NULL;
4501 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004502 else if (lv.ll_sid > 0)
4503 {
4504 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4505 int cc = *lv.ll_name_end;
4506
4507 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4508 *lv.ll_name_end = NUL;
4509 if (si->sn_autoload_prefix != NULL)
4510 {
4511 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4512 }
4513 else
4514 {
4515 sid_buf[0] = K_SPECIAL;
4516 sid_buf[1] = KS_EXTRA;
4517 sid_buf[2] = (int)KE_SNR;
4518 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004519 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004520 name = concat_str(sid_buf, lv.ll_name);
4521 }
4522 *lv.ll_name_end = cc;
4523 *pp = end;
4524 goto theend;
4525 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004526 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004527 {
4528 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004529 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004530 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004531 if (name == *pp)
4532 name = NULL;
4533 }
4534 if (name != NULL)
4535 {
4536 name = vim_strsave(name);
4537 *pp = end;
4538 if (STRNCMP(name, "<SNR>", 5) == 0)
4539 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004540 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004541 name[0] = K_SPECIAL;
4542 name[1] = KS_EXTRA;
4543 name[2] = (int)KE_SNR;
4544 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4545 }
4546 goto theend;
4547 }
4548
4549 if (lv.ll_exp_name != NULL)
4550 {
4551 len = (int)STRLEN(lv.ll_exp_name);
4552 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4553 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4554 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004555 // When there was "s:" already or the name expanded to get a
4556 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004557 lv.ll_name += 2;
4558 len -= 2;
4559 lead = 2;
4560 }
4561 }
4562 else
4563 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004564 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004565 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004566 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004567 if (lv.ll_name[0] == 'g')
4568 {
4569 if (is_global != NULL)
4570 {
4571 *is_global = TRUE;
4572 }
4573 else
4574 {
4575 // dropping "g:" without setting "is_global" won't work in
4576 // Vim9script, put it back later
4577 prefix_g = TRUE;
4578 extra = 2;
4579 }
4580 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004581 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004582 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004583 len = (int)(end - lv.ll_name);
4584 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004585 if (len <= 0)
4586 {
4587 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004588 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004589 goto theend;
4590 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004591
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004592 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004593 // starts with a lower case character: dict.func(). Or when in a class.
4594 vim9_local = ASCII_ISUPPER(*start) && vim9script
4595 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004597 /*
4598 * Copy the function name to allocated memory.
4599 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4600 * Accept <SNR>123_name() outside a script.
4601 */
4602 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004603 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004604 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004605 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004606 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004607 {
Kota Kato948a3892022-08-16 16:09:59 +01004608 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4609 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004610 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004611 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004612 goto theend;
4613 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004615 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004616 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004618 || eval_fname_sid(*pp))
4619 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004621 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004622 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004623 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004624 goto theend;
4625 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004626 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004627 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 extra = 3 + (int)STRLEN(sid_buf);
4629 else
4630 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004631 }
4632 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004633 // The function name must start with an upper case letter (unless it is a
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004634 // Vim9 class new() function or a Vim9 class private method or one of the
4635 // supported Vim9 object builtin functions)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004636 else if (!(flags & TFN_INT)
4637 && (builtin_function(lv.ll_name, len)
4638 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004639 && !((flags & TFN_IN_CLASS)
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004640 && (is_valid_builtin_obj_methodname(lv.ll_name)
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004641 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004642 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004643 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004644 : e_function_name_must_start_with_capital_or_s_str),
4645 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004646 goto theend;
4647 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004648 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004649 {
4650 char_u *cp = vim_strchr(lv.ll_name, ':');
4651
4652 if (cp != NULL && cp < end)
4653 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004654 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004655 goto theend;
4656 }
4657 }
4658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004660 if (name != NULL)
4661 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004662 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004663 {
4664 name[0] = K_SPECIAL;
4665 name[1] = KS_EXTRA;
4666 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004667 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004668 STRCPY(name + 3, sid_buf);
4669 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004670 else if (prefix_g)
4671 {
4672 name[0] = 'g';
4673 name[1] = ':';
4674 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4676 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004677 }
4678 *pp = end;
4679
4680theend:
4681 clear_lval(&lv);
4682 return name;
4683}
4684
4685/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004686 * Assuming "name" is the result of trans_function_name() and it was prefixed
4687 * to use the script-local name, return the unmodified name (points into
4688 * "name"). Otherwise return NULL.
4689 * This can be used to first search for a script-local function and fall back
4690 * to the global function if not found.
4691 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004692 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004693untrans_function_name(char_u *name)
4694{
4695 char_u *p;
4696
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004697 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004698 {
4699 p = vim_strchr(name, '_');
4700 if (p != NULL)
4701 return p + 1;
4702 }
4703 return NULL;
4704}
4705
4706/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004707 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4708 * current script ID and returns the expanded function name. The caller should
4709 * free the returned name. If not called from a script context or the function
4710 * name doesn't start with these prefixes, then returns NULL.
4711 * This doesn't check whether the script-local function exists or not.
4712 */
4713 char_u *
4714get_scriptlocal_funcname(char_u *funcname)
4715{
4716 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004717 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004718 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004719 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004720
4721 if (funcname == NULL)
4722 return NULL;
4723
4724 if (STRNCMP(funcname, "s:", 2) != 0
4725 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004726 {
4727 ufunc_T *ufunc;
4728
4729 // The function name does not have a script-local prefix. Try finding
4730 // it when in a Vim9 script and there is no "g:" prefix.
4731 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4732 return NULL;
4733 ufunc = find_func(funcname, FALSE);
4734 if (ufunc == NULL || func_is_global(ufunc)
4735 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4736 return NULL;
4737 ++p;
4738 off = 0;
4739 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004740 else
4741 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004742
4743 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4744 {
4745 emsg(_(e_using_sid_not_in_script_context));
4746 return NULL;
4747 }
4748 // Expand s: prefix into <SNR>nr_<name>
4749 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4750 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004751 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004752 if (newname == NULL)
4753 return NULL;
4754 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004755 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004756
4757 return newname;
4758}
4759
4760/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004761 * Return script-local "fname" with the 3-byte sequence replaced by
4762 * printable <SNR> in allocated memory.
4763 */
4764 char_u *
4765alloc_printable_func_name(char_u *fname)
4766{
4767 char_u *n = alloc(STRLEN(fname + 3) + 6);
4768
4769 if (n != NULL)
4770 {
4771 STRCPY(n, "<SNR>");
4772 STRCPY(n + 5, fname + 3);
4773 }
4774 return n;
4775}
4776
4777/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004778 * Call trans_function_name(), except that a lambda is returned as-is.
4779 * Returns the name in allocated memory.
4780 */
4781 char_u *
4782save_function_name(
4783 char_u **name,
4784 int *is_global,
4785 int skip,
4786 int flags,
4787 funcdict_T *fudi)
4788{
4789 char_u *p = *name;
4790 char_u *saved;
4791
4792 if (STRNCMP(p, "<lambda>", 8) == 0)
4793 {
4794 p += 8;
4795 (void)getdigits(&p);
4796 saved = vim_strnsave(*name, p - *name);
4797 if (fudi != NULL)
4798 CLEAR_POINTER(fudi);
4799 }
4800 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004801 saved = trans_function_name_ext(&p, is_global, skip,
4802 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004803 *name = p;
4804 return saved;
4805}
4806
4807/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004808 * List functions. When "regmatch" is NULL all of then.
4809 * Otherwise functions matching "regmatch".
4810 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004811 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004812list_functions(regmatch_T *regmatch)
4813{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004814 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004815 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004816 hashitem_T *hi;
4817
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004818 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004819 {
4820 if (!HASHITEM_EMPTY(hi))
4821 {
4822 ufunc_T *fp = HI2UF(hi);
4823
4824 --todo;
4825 if ((fp->uf_flags & FC_DEAD) == 0
4826 && (regmatch == NULL
4827 ? !message_filtered(fp->uf_name)
4828 && !func_name_refcount(fp->uf_name)
Keith Thompson184f71c2024-01-04 21:19:04 +01004829 : !SAFE_isdigit(*fp->uf_name)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004830 && vim_regexec(regmatch, fp->uf_name, 0)))
4831 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004832 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004833 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004834 if (function_list_modified(prev_ht_changed))
4835 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004836 }
4837 }
4838 }
4839}
4840
4841/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004842 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004843 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4844 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004845 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004846 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4847 * With CF_INTERFACE the function is define inside an interface, only the
4848 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004849 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004850 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004851 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004852define_function(
4853 exarg_T *eap,
4854 char_u *name_arg,
4855 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004856 int class_flags,
4857 ocmember_T *obj_members,
4858 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004859{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004860 int j;
4861 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004862 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004863 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004864 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004865 char_u *p;
4866 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004867 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004868 char_u *line_arg = NULL;
4869 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004871 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004872 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004873 garray_T newlines;
4874 int varargs = FALSE;
4875 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004877 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004878 int fp_allocated = FALSE;
4879 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004880 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004881 dictitem_T *v;
4882 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004883 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004884 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004885 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004886 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004887 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004888 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004889
4890 /*
4891 * ":function" without argument: list functions.
4892 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004893 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004894 {
4895 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004896 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004897 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004898 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004899 }
4900
4901 /*
4902 * ":function /pat": list functions matching pattern.
4903 */
4904 if (*eap->arg == '/')
4905 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004906 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004907 if (!eap->skip)
4908 {
4909 regmatch_T regmatch;
4910
4911 c = *p;
4912 *p = NUL;
4913 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4914 *p = c;
4915 if (regmatch.regprog != NULL)
4916 {
4917 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004918 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004919 vim_regfree(regmatch.regprog);
4920 }
4921 }
4922 if (*p == '/')
4923 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004924 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004925 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004926 }
4927
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 ga_init(&newargs);
4929 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004930 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 ga_init(&default_args);
4932
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004933 /*
4934 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004935 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004936 * "name" == func, "fudi.fd_dict" == NULL
4937 * dict.func new dictionary entry
4938 * "name" == NULL, "fudi.fd_dict" set,
4939 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4940 * dict.func existing dict entry with a Funcref
4941 * "name" == func, "fudi.fd_dict" set,
4942 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4943 * dict.func existing dict entry that's not a Funcref
4944 * "name" == NULL, "fudi.fd_dict" set,
4945 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4946 * s:func script-local function name
4947 * g:func global function name, same as "func"
4948 */
4949 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004950 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004951 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004952 // nested function, argument is (args).
4953 paren = TRUE;
4954 CLEAR_FIELD(fudi);
4955 }
4956 else
4957 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004958 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004959 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004960 if (p[0] == 's' && p[1] == ':')
4961 {
4962 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4963 return NULL;
4964 }
4965 p = to_name_end(p, TRUE);
4966 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4967 {
4968 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4969 eap->arg);
4970 return NULL;
4971 }
4972 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004973 }
4974
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004975 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004976 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004977 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004978 paren = (vim_strchr(p, '(') != NULL);
4979 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004980 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004981 /*
4982 * Return on an invalid expression in braces, unless the expression
4983 * evaluation has been cancelled due to an aborting error, an
4984 * interrupt, or an exception.
4985 */
4986 if (!aborting())
4987 {
4988 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004989 semsg(_(e_key_not_present_in_dictionary_str),
4990 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004991 vim_free(fudi.fd_newkey);
4992 return NULL;
4993 }
4994 else
4995 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004996 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004997
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004998 // For "export def FuncName()" in an autoload script the function name
4999 // is stored with the legacy autoload name "dir#script#FuncName" so
5000 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00005001 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005002 {
5003 char_u *prefixed = may_prefix_autoload(name);
5004
5005 if (prefixed != NULL && prefixed != name)
5006 {
5007 vim_free(name);
5008 name = prefixed;
5009 }
5010 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00005011 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00005012 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00005013 {
5014 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
5015 goto ret_free;
5016 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005017 }
5018
Bram Moolenaare38eab22019-12-05 21:50:01 +01005019 // An error in a function call during evaluation of an expression in magic
5020 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005021 saved_did_emsg = did_emsg;
5022 did_emsg = FALSE;
5023
5024 /*
5025 * ":function func" with only function name: list function.
5026 */
5027 if (!paren)
5028 {
5029 if (!ends_excmd(*skipwhite(p)))
5030 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00005031 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005032 goto ret_free;
5033 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005034 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005035 if (eap->nextcmd != NULL)
5036 *p = NUL;
5037 if (!eap->skip && !got_int)
5038 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005039 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005040 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
5041 {
5042 char_u *up = untrans_function_name(name);
5043
5044 // With Vim9 script the name was made script-local, if not
5045 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02005046 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005047 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005048 }
5049
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005050 if (fp != NULL)
5051 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00005052 // Check no function was added or removed from a timer, e.g. at
5053 // the more prompt. "fp" may then be invalid.
5054 int prev_ht_changed = func_hashtab.ht_changed;
5055
5056 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005057 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00005058 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
5059 {
5060 if (FUNCLINE(fp, j) == NULL)
5061 continue;
5062 msg_putchar('\n');
5063 msg_outnum((long)(j + 1));
5064 if (j < 9)
5065 msg_putchar(' ');
5066 if (j < 99)
5067 msg_putchar(' ');
5068 if (function_list_modified(prev_ht_changed))
5069 break;
5070 msg_prt_line(FUNCLINE(fp, j), FALSE);
5071 out_flush(); // show a line at a time
5072 ui_breakcheck();
5073 }
5074 if (!got_int)
5075 {
5076 msg_putchar('\n');
5077 if (!function_list_modified(prev_ht_changed))
5078 {
5079 if (fp->uf_def_status != UF_NOT_COMPILED)
5080 msg_puts(" enddef");
5081 else
5082 msg_puts(" endfunction");
5083 }
5084 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005085 }
5086 }
5087 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00005088 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005089 }
5090 goto ret_free;
5091 }
5092
5093 /*
5094 * ":function name(arg1, arg2)" Define function.
5095 */
5096 p = skipwhite(p);
5097 if (*p != '(')
5098 {
5099 if (!eap->skip)
5100 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005101 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005102 goto ret_free;
5103 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005104 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005105 if (vim_strchr(p, '(') != NULL)
5106 p = vim_strchr(p, '(');
5107 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005108
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005109 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
5110 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01005111 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005112 goto ret_free;
5113 }
5114
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005115 // In Vim9 script only global functions can be redefined.
5116 if (vim9script && eap->forceit && !is_global)
5117 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005118 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005119 goto ret_free;
5120 }
5121
Bram Moolenaar04935fb2022-01-08 16:19:22 +00005122 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005123
Bram Moolenaar04b12692020-05-04 23:24:44 +02005124 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005125 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005126 // Check the name of the function. Unless it's a dictionary function
5127 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005128 if (name != NULL)
5129 arg = name;
5130 else
5131 arg = fudi.fd_newkey;
5132 if (arg != NULL && (fudi.fd_di == NULL
5133 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
5134 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
5135 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00005136 char_u *name_base = arg;
5137 int i;
5138
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005139 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005140 {
5141 name_base = vim_strchr(arg, '_');
5142 if (name_base == NULL)
5143 name_base = arg + 3;
5144 else
5145 ++name_base;
5146 }
5147 for (i = 0; name_base[i] != NUL && (i == 0
5148 ? eval_isnamec1(name_base[i])
5149 : eval_isnamec(name_base[i])); ++i)
5150 ;
5151 if (name_base[i] != NUL)
zeertzjq6a04bf52024-03-16 09:39:06 +01005152 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005153 emsg_funcname(e_invalid_argument_str, arg);
zeertzjq6a04bf52024-03-16 09:39:06 +01005154 goto ret_free;
5155 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00005156
5157 // In Vim9 script a function cannot have the same name as a
5158 // variable.
5159 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005160 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
5161 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00005162 + EVAL_VAR_NO_FUNC) == OK)
5163 {
5164 semsg(_(e_redefining_script_item_str), name_base);
5165 goto ret_free;
5166 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005167 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005168 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005169 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005170 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005171 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00005172 goto ret_free;
5173 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005174 }
5175
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005176 // This may get more lines and make the pointers into the first line
5177 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005178 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005180 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005181 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005182 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005183 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005184 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005185 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005187 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005188 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005189 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005190 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005191 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005192 if (*p != ':')
5193 {
5194 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5195 p = skipwhite(p);
5196 }
5197 else if (!IS_WHITE_OR_NUL(p[1]))
5198 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005200 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005202 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005203 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005204 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005208 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005209 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005210 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005211 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005212 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005213 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005214 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005215 else
5216 // find extra arguments "range", "dict", "abort" and "closure"
5217 for (;;)
5218 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005219 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 p = skipwhite(p);
5221 if (STRNCMP(p, "range", 5) == 0)
5222 {
5223 flags |= FC_RANGE;
5224 p += 5;
5225 }
5226 else if (STRNCMP(p, "dict", 4) == 0)
5227 {
5228 flags |= FC_DICT;
5229 p += 4;
5230 }
5231 else if (STRNCMP(p, "abort", 5) == 0)
5232 {
5233 flags |= FC_ABORT;
5234 p += 5;
5235 }
5236 else if (STRNCMP(p, "closure", 7) == 0)
5237 {
5238 flags |= FC_CLOSURE;
5239 p += 7;
5240 if (current_funccal == NULL)
5241 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005242 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243 name == NULL ? (char_u *)"" : name);
5244 goto erret;
5245 }
5246 }
5247 else
5248 break;
5249 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005250
Bram Moolenaare38eab22019-12-05 21:50:01 +01005251 // When there is a line break use what follows for the function body.
5252 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005253 if (*p == '\n')
5254 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005255 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005256 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5257 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005258 && !(VIM_ISWHITE(*whitep) && *p == '#'
5259 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005260 && !eap->skip
5261 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005262 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005263
5264 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005265 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5266 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005267 */
5268 if (KeyTyped)
5269 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005270 // Check if the function already exists, don't let the user type the
5271 // whole function before telling him it doesn't work! For a script we
5272 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005273 if (!eap->skip && !eap->forceit)
5274 {
5275 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005276 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005277 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005278 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005279 }
5280
5281 if (!eap->skip && did_emsg)
5282 goto erret;
5283
Bram Moolenaare38eab22019-12-05 21:50:01 +01005284 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005285 cmdline_row = msg_row;
5286 }
5287
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005288 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005289 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005290
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005291 // Do not define the function when getting the body fails and when
5292 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005293 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005294 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005295 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5296 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005297 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005298 goto erret;
5299
5300 /*
5301 * If there are no errors, add the function
5302 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005303 if (fudi.fd_dict != NULL)
5304 {
5305 char numbuf[20];
5306
5307 fp = NULL;
5308 if (fudi.fd_newkey == NULL && !eap->forceit)
5309 {
5310 emsg(_(e_dictionary_entry_already_exists));
5311 goto erret;
5312 }
5313 if (fudi.fd_di == NULL)
5314 {
5315 // Can't add a function to a locked dictionary
5316 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5317 goto erret;
5318 }
5319 // Can't change an existing function if it is locked
5320 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5321 goto erret;
5322
5323 // Give the function a sequential number. Can only be used with a
5324 // Funcref!
5325 vim_free(name);
5326 sprintf(numbuf, "%d", ++func_nr);
5327 name = vim_strsave((char_u *)numbuf);
5328 if (name == NULL)
5329 goto erret;
5330 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005331 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005332 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005333 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005334 char_u *find_name = name;
5335 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005336 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005337
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005338 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005339 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005340 var_conflict = TRUE;
5341
5342 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5343 {
5344 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5345
5346 if (si->sn_autoload_prefix != NULL)
5347 {
5348 if (is_export)
5349 {
5350 find_name = name + STRLEN(si->sn_autoload_prefix);
5351 v = find_var(find_name, &ht, TRUE);
5352 if (v != NULL)
5353 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005354 // Only check if the function already exists in the script,
5355 // global functions can be shadowed.
5356 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005357 }
5358 else
5359 {
5360 char_u *prefixed = may_prefix_autoload(name);
5361
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005362 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005363 {
5364 v = find_var(prefixed, &ht, TRUE);
5365 if (v != NULL)
5366 var_conflict = TRUE;
5367 vim_free(prefixed);
5368 }
5369 }
5370 }
5371 }
5372 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005373 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005374 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005375 goto erret;
5376 }
5377
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005378 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005379 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005380 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005381 char_u *uname = untrans_function_name(name);
5382
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005383 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005384 }
5385
5386 if (fp != NULL || import != NULL)
5387 {
5388 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005390 // Function can be replaced with "function!" and when sourcing the
5391 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005392 // A name that is used by an import can not be overruled.
5393 if (import != NULL
5394 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005395 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005396 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005397 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005398 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005399 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005400 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005401 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005402 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005403 goto erret;
5404 }
5405 if (fp->uf_calls > 0)
5406 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005407 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005408 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005409 goto erret;
5410 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005411 if (fp->uf_refcount > 1)
5412 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005413 // This function is referenced somewhere, don't redefine it but
5414 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005415 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005416 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005417 fp = NULL;
5418 overwrite = TRUE;
5419 }
5420 else
5421 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005422 char_u *exp_name = fp->uf_name_exp;
5423
5424 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005425 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005426 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005427 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005428 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005429 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005430#ifdef FEAT_PROFILE
5431 fp->uf_profiling = FALSE;
5432 fp->uf_prof_initialized = FALSE;
5433#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005434 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005435 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005436 }
5437 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005438
5439 if (fp == NULL)
5440 {
5441 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5442 {
5443 int slen, plen;
5444 char_u *scriptname;
5445
Bram Moolenaare38eab22019-12-05 21:50:01 +01005446 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005447 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005448 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005449 {
5450 scriptname = autoload_name(name);
5451 if (scriptname != NULL)
5452 {
5453 p = vim_strchr(scriptname, '/');
5454 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005455 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005456 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005457 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005458 j = OK;
5459 vim_free(scriptname);
5460 }
5461 }
5462 if (j == FAIL)
5463 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005464 linenr_T save_lnum = SOURCING_LNUM;
5465
5466 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005467 semsg(_(e_function_name_does_not_match_script_file_name_str),
5468 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005469 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005470 goto erret;
5471 }
5472 }
5473
Bram Moolenaare01e5212023-01-08 20:31:18 +00005474 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005475 if (fp == NULL)
5476 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005477 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005478
5479 if (fudi.fd_dict != NULL)
5480 {
5481 if (fudi.fd_di == NULL)
5482 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005483 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005484 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5485 if (fudi.fd_di == NULL)
5486 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005487 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005488 goto erret;
5489 }
5490 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5491 {
5492 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005493 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005494 goto erret;
5495 }
5496 }
5497 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005498 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005499 clear_tv(&fudi.fd_di->di_tv);
5500 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005501 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005502
Bram Moolenaare38eab22019-12-05 21:50:01 +01005503 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005504 flags |= FC_DICT;
5505 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005506 }
5507 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005508 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005509 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005510 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005511
5512 if (eap->cmdidx == CMD_def)
5513 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005514 int lnum_save = SOURCING_LNUM;
5515 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005516
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005517 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005518
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005519 // error messages are for the first function line
5520 SOURCING_LNUM = sourcing_lnum_top;
5521
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005522 // The function may use script variables from the context.
5523 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005524
h-eastb895b0f2023-09-24 15:46:31 +02005525 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5526 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005527 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005528 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005529 free_fp = fp_allocated;
5530 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005531 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005532 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005533
5534 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005535 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005536 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005537 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005538 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005539 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005540 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005541 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005542 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005543 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005544 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005545
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005546 if (fp_allocated)
5547 {
5548 // insert the new function in the function list
5549 set_ufunc_name(fp, name);
5550 if (overwrite)
5551 {
5552 hi = hash_find(&func_hashtab, name);
5553 hi->hi_key = UF2HIKEY(fp);
5554 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005555 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005556 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005557 {
5558 free_fp = TRUE;
5559 goto erret;
5560 }
5561 fp->uf_refcount = 1;
5562 }
5563
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005564 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005565 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005566 if ((flags & FC_CLOSURE) != 0)
5567 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005568 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005569 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005570 }
5571 else
5572 fp->uf_scoped = NULL;
5573
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005574#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005575 if (prof_def_func())
5576 func_do_profile(fp);
5577#endif
5578 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005579 if (sandbox)
5580 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005581 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005582 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005583 fp->uf_flags = flags;
5584 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005585 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005586 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005587 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005588 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005589 if (is_export)
5590 {
5591 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005592 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005593 is_export = FALSE;
5594 }
5595
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005596 if (eap->cmdidx == CMD_def)
5597 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005598 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5599 // :func does not use Vim9 script syntax, even in a Vim9 script file
5600 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005601
Yegappan Lakshmanan5715a722024-05-03 18:24:07 +02005602 // If test_override('defcompile' 1) is used, then compile the function now
5603 if (eap->cmdidx == CMD_def && override_defcompile)
5604 defcompile_function(fp, NULL);
5605
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005606 goto ret_free;
5607
5608erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005609 if (fp != NULL)
5610 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005611 // these were set to "newargs" and "default_args", which are cleared
5612 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005613 ga_init(&fp->uf_args);
5614 ga_init(&fp->uf_def_args);
5615 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005616errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005617 ga_clear_strings(&newargs);
5618 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005619 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005620 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005621 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005622 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005623 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01005624 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005625 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005626 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005627 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005628ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005629 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005630 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005631 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005632 if (name != name_arg)
5633 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005634 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005635 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005636
5637 return fp;
5638}
5639
5640/*
5641 * ":function"
5642 */
5643 void
5644ex_function(exarg_T *eap)
5645{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005646 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005647
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005648 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005649 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005650 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005651}
5652
LemonBoy48b7d052024-07-09 18:24:59 +02005653 int
5654get_func_arity(char_u *name, int *required, int *optional, int *varargs)
5655{
5656 ufunc_T *ufunc = NULL;
5657 int argcount = 0;
5658 int min_argcount = 0;
5659 int idx;
5660
5661 idx = find_internal_func(name);
5662 if (idx >= 0)
5663 {
5664 internal_func_get_argcount(idx, &argcount, &min_argcount);
5665 *varargs = FALSE;
5666 }
5667 else
5668 {
5669 char_u fname_buf[FLEN_FIXED + 1];
5670 char_u *tofree = NULL;
5671 funcerror_T error = FCERR_NONE;
5672 char_u *fname;
5673
5674 // May need to translate <SNR>123_ to K_SNR.
5675 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
5676 if (error == FCERR_NONE)
5677 ufunc = find_func(fname, FALSE);
5678 vim_free(tofree);
5679
5680 if (ufunc == NULL)
5681 return FAIL;
5682
5683 argcount = ufunc->uf_args.ga_len;
5684 min_argcount = ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len;
5685 *varargs = has_varargs(ufunc);
5686 }
5687
5688 *required = min_argcount;
5689 *optional = argcount - min_argcount;
5690
5691 return OK;
5692}
5693
Bram Moolenaar822ba242020-05-24 23:00:18 +02005694/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005695 * Find a function by name, including "<lambda>123".
5696 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005697 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005698 * Return NULL if not found.
5699 */
5700 ufunc_T *
5701find_func_by_name(char_u *name, compiletype_T *compile_type)
5702{
5703 char_u *arg = name;
5704 char_u *fname;
5705 ufunc_T *ufunc;
5706 int is_global = FALSE;
5707
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005708 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5709 {
5710 *compile_type = CT_PROFILE;
5711 arg = skipwhite(arg + 7);
5712 }
5713 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5714 {
5715 *compile_type = CT_DEBUG;
5716 arg = skipwhite(arg + 5);
5717 }
5718
5719 if (STRNCMP(arg, "<lambda>", 8) == 0)
5720 {
5721 arg += 8;
5722 (void)getdigits(&arg);
5723 fname = vim_strnsave(name, arg - name);
5724 }
5725 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005726 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005727 // First try finding a method in a class, trans_function_name() will
5728 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005729 ufunc = find_class_func(&arg);
5730 if (ufunc != NULL)
5731 return ufunc;
5732
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005733 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005734 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005735 NULL, NULL, NULL, &ufunc);
5736 if (ufunc != NULL)
5737 {
5738 vim_free(fname);
5739 return ufunc;
5740 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005741 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005742 if (fname == NULL)
5743 {
5744 semsg(_(e_invalid_argument_str), name);
5745 return NULL;
5746 }
5747 if (!ends_excmd2(name, arg))
5748 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005749 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005750 emsg(ex_errmsg(e_trailing_characters_str, arg));
5751 return NULL;
5752 }
5753
5754 ufunc = find_func(fname, is_global);
5755 if (ufunc == NULL)
5756 {
5757 char_u *p = untrans_function_name(fname);
5758
5759 if (p != NULL)
5760 // Try again without making it script-local.
5761 ufunc = find_func(p, FALSE);
5762 }
5763 vim_free(fname);
5764 if (ufunc == NULL)
5765 semsg(_(e_cannot_find_function_str), name);
5766 return ufunc;
5767}
5768
5769/*
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005770 * Compile the :def function "ufunc". If "cl" is not NULL, then compile the
5771 * class or object method "ufunc" in "cl".
5772 */
5773 void
5774defcompile_function(ufunc_T *ufunc, class_T *cl)
5775{
5776 compiletype_T compile_type = CT_NONE;
5777
5778 if (func_needs_compiling(ufunc, compile_type))
5779 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5780 else
5781 smsg(_("Function %s%s%s does not need compiling"),
5782 cl != NULL ? cl->class_name : (char_u *)"",
5783 cl != NULL ? (char_u *)"." : (char_u *)"",
5784 ufunc->uf_name);
5785}
5786
5787/*
5788 * Compile all the :def functions defined in the current script
5789 */
5790 static void
5791defcompile_funcs_in_script(void)
5792{
5793 long todo = (long)func_hashtab.ht_used;
5794 int changed = func_hashtab.ht_changed;
5795 hashitem_T *hi;
5796
5797 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5798 {
5799 if (!HASHITEM_EMPTY(hi))
5800 {
5801 --todo;
5802 ufunc_T *ufunc = HI2UF(hi);
5803 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5804 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5805 && (ufunc->uf_flags & FC_DEAD) == 0)
5806 {
5807 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5808
5809 if (func_hashtab.ht_changed != changed)
5810 {
5811 // a function has been added or removed, need to start
5812 // over
5813 todo = (long)func_hashtab.ht_used;
5814 changed = func_hashtab.ht_changed;
5815 hi = func_hashtab.ht_array;
5816 --hi;
5817 }
5818 }
5819 }
5820 }
5821}
5822
5823/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005824 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005825 * be compiled or the one specified by the argument.
5826 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005827 */
5828 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005829ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005830{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005831 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005832 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005833 typval_T tv;
5834
5835 if (is_class_name(eap->arg, &tv))
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005836 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005837 class_T *cl = tv.vval.v_class;
5838
5839 if (cl != NULL)
5840 defcompile_class(cl);
5841 }
5842 else
5843 {
5844 compiletype_T compile_type = CT_NONE;
5845 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
5846 if (ufunc != NULL)
5847 defcompile_function(ufunc, NULL);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005848 }
5849 }
5850 else
5851 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005852 defcompile_funcs_in_script();
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005853
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005854 // compile all the class defined in the current script
5855 defcompile_classes_in_script();
Bram Moolenaar822ba242020-05-24 23:00:18 +02005856 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005857}
5858
5859/*
5860 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5861 * Return 2 if "p" starts with "s:".
5862 * Return 0 otherwise.
5863 */
5864 int
5865eval_fname_script(char_u *p)
5866{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005867 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5868 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005869 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5870 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5871 return 5;
5872 if (p[0] == 's' && p[1] == ':')
5873 return 2;
5874 return 0;
5875}
5876
5877 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005878translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005879{
5880 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005881 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005882 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005883}
5884
5885/*
5886 * Return TRUE when "ufunc" has old-style "..." varargs
5887 * or named varargs "...name: type".
5888 */
5889 int
5890has_varargs(ufunc_T *ufunc)
5891{
5892 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005893}
5894
5895/*
5896 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005897 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005898 */
5899 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005900function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005901{
5902 char_u *nm = name;
5903 char_u *p;
5904 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005905 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005906 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005907
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005908 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5909 if (no_deref)
5910 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005911 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005912 nm = skipwhite(nm);
5913
Bram Moolenaare38eab22019-12-05 21:50:01 +01005914 // Only accept "funcname", "funcname ", "funcname (..." and
5915 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005916 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005917 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005918 vim_free(p);
5919 return n;
5920}
5921
Bram Moolenaar113e1072019-01-20 15:30:40 +01005922#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005923 char_u *
5924get_expanded_name(char_u *name, int check)
5925{
5926 char_u *nm = name;
5927 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005928 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005929
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005930 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005931
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005932 if (p != NULL && *nm == NUL
5933 && (!check || translated_function_exists(p, is_global)))
5934 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005935
5936 vim_free(p);
5937 return NULL;
5938}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005939#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005940
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005941/*
5942 * Function given to ExpandGeneric() to obtain the list of user defined
5943 * function names.
5944 */
5945 char_u *
5946get_user_func_name(expand_T *xp, int idx)
5947{
5948 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005949 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005950 static hashitem_T *hi;
5951 ufunc_T *fp;
5952
5953 if (idx == 0)
5954 {
5955 done = 0;
5956 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005957 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005958 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005959 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005960 {
5961 if (done++ > 0)
5962 ++hi;
5963 while (HASHITEM_EMPTY(hi))
5964 ++hi;
5965 fp = HI2UF(hi);
5966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005967 // don't show dead, dict and lambda functions
5968 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005969 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005970 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005971
5972 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005973 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005974
5975 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005976 if (xp->xp_context != EXPAND_USER_FUNC
5977 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005978 {
5979 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005980 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005981 STRCAT(IObuff, ")");
5982 }
5983 return IObuff;
5984 }
5985 return NULL;
5986}
5987
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005988/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005989 * Make a copy of a function.
5990 * Intended to be used for a function defined on a base class that has a copy
5991 * on the child class.
5992 * The copy has uf_refcount set to one.
5993 * Returns NULL when out of memory.
5994 */
5995 ufunc_T *
5996copy_function(ufunc_T *fp)
5997{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005998 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005999 if (ufunc == NULL)
6000 return NULL;
6001
6002 // Most things can just be copied.
6003 *ufunc = *fp;
6004
6005 ufunc->uf_def_status = UF_TO_BE_COMPILED;
6006 ufunc->uf_dfunc_idx = 0;
6007 ufunc->uf_class = NULL;
6008
6009 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
6010 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
6011
6012 if (ufunc->uf_arg_types != NULL)
6013 {
6014 // "uf_arg_types" is an allocated array, make a copy.
6015 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
6016 if (at != NULL)
6017 {
6018 mch_memmove(at, ufunc->uf_arg_types,
6019 sizeof(type_T *) * ufunc->uf_args.ga_len);
6020 ufunc->uf_arg_types = at;
6021 }
6022 }
6023
6024 // TODO: how about the types themselves? they can be freed when the
6025 // original function is freed:
6026 // type_T **uf_arg_types;
6027 // type_T *uf_ret_type;
6028
Ernie Rael114ec812023-06-04 18:11:35 +01006029 // make uf_type_list empty
6030 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00006031
6032 // TODO: partial_T *uf_partial;
6033
6034 if (ufunc->uf_va_name != NULL)
6035 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
6036
6037 // TODO:
6038 // type_T *uf_va_type;
6039 // type_T *uf_func_type;
6040
6041 ufunc->uf_block_depth = 0;
6042 ufunc->uf_block_ids = NULL;
6043
6044 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
6045
6046 ufunc->uf_refcount = 1;
6047 ufunc->uf_name_exp = NULL;
6048 STRCPY(ufunc->uf_name, fp->uf_name);
6049
6050 return ufunc;
6051}
6052
6053/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006054 * ":delfunction {name}"
6055 */
6056 void
6057ex_delfunction(exarg_T *eap)
6058{
6059 ufunc_T *fp = NULL;
6060 char_u *p;
6061 char_u *name;
6062 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006063 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006064
6065 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00006066 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
6067 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006068 vim_free(fudi.fd_newkey);
6069 if (name == NULL)
6070 {
6071 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00006072 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006073 return;
6074 }
6075 if (!ends_excmd(*skipwhite(p)))
6076 {
6077 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00006078 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006079 return;
6080 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006081 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006082 if (eap->nextcmd != NULL)
6083 *p = NUL;
6084
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006085 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02006086 {
6087 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006088 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02006089 vim_free(name);
6090 return;
6091 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006092 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006093 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006094 vim_free(name);
6095
6096 if (!eap->skip)
6097 {
6098 if (fp == NULL)
6099 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02006100 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00006101 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006102 return;
6103 }
6104 if (fp->uf_calls > 0)
6105 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006106 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006107 return;
6108 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006109 if (fp->uf_flags & FC_VIM9)
6110 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006111 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006112 return;
6113 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006114
6115 if (fudi.fd_dict != NULL)
6116 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006117 // Delete the dict item that refers to the function, it will
6118 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00006119 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006120 }
6121 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006122 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006123 // A normal function (not a numbered function or lambda) has a
6124 // refcount of 1 for the entry in the hashtable. When deleting
6125 // it and the refcount is more than one, it should be kept.
6126 // A numbered function and lambda should be kept if the refcount is
6127 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006128 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006129 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006130 // Function is still referenced somewhere. Don't free it but
6131 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006132 if (func_remove(fp))
6133 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006134 }
6135 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006136 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006137 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006138 }
6139}
6140
6141/*
6142 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006143 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006144 */
6145 void
6146func_unref(char_u *name)
6147{
Bram Moolenaar97baee82016-07-26 20:46:08 +02006148 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006149
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006150 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006151 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006152 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006153 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006154 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006155#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006156 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006157#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01006158 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006159 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006160 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006161}
6162
6163/*
6164 * Unreference a Function: decrement the reference count and free it when it
6165 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006166 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006167 */
6168 void
6169func_ptr_unref(ufunc_T *fp)
6170{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006171 if (fp != NULL && (--fp->uf_refcount <= 0
6172 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
6173 && fp->uf_partial->pt_refcount <= 1
6174 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02006175 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006176 // Only delete it when it's not being used. Otherwise it's done
6177 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02006178 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006179 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006180 }
6181}
6182
6183/*
6184 * Count a reference to a Function.
6185 */
6186 void
6187func_ref(char_u *name)
6188{
6189 ufunc_T *fp;
6190
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006191 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006192 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006193 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006194 if (fp != NULL)
6195 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006196 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01006197 // Only give an error for a numbered function.
6198 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01006199 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006200}
6201
6202/*
6203 * Count a reference to a Function.
6204 */
6205 void
6206func_ptr_ref(ufunc_T *fp)
6207{
6208 if (fp != NULL)
6209 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006210}
6211
6212/*
6213 * Return TRUE if items in "fc" do not have "copyID". That means they are not
6214 * referenced from anywhere that is in use.
6215 */
6216 static int
6217can_free_funccal(funccall_T *fc, int copyID)
6218{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006219 return (fc->fc_l_varlist.lv_copyID != copyID
6220 && fc->fc_l_vars.dv_copyID != copyID
6221 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006222 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006223}
6224
6225/*
6226 * ":return [expr]"
6227 */
6228 void
6229ex_return(exarg_T *eap)
6230{
6231 char_u *arg = eap->arg;
6232 typval_T rettv;
6233 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006234 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006235
6236 if (current_funccal == NULL)
6237 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006238 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006239 return;
6240 }
6241
Bram Moolenaar844fb642021-10-23 13:32:30 +01006242 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006243 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
6244
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006245 if (eap->skip)
6246 ++emsg_skip;
6247
6248 eap->nextcmd = NULL;
6249 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006250 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006251 {
6252 if (!eap->skip)
6253 returning = do_return(eap, FALSE, TRUE, &rettv);
6254 else
6255 clear_tv(&rettv);
6256 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01006257 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006258 else if (!eap->skip)
6259 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006260 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006261 update_force_abort();
6262
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006263 /*
6264 * Return unless the expression evaluation has been cancelled due to an
6265 * aborting error, an interrupt, or an exception.
6266 */
6267 if (!aborting())
6268 returning = do_return(eap, FALSE, TRUE, NULL);
6269 }
6270
Bram Moolenaare38eab22019-12-05 21:50:01 +01006271 // When skipping or the return gets pending, advance to the next command
6272 // in this line (!returning). Otherwise, ignore the rest of the line.
6273 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006274 if (returning)
6275 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006276 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006277 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006278
6279 if (eap->skip)
6280 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006281 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006282}
6283
zeertzjq5299c092023-04-12 20:48:16 +01006284/*
6285 * Lower level implementation of "call". Only called when not skipping.
6286 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006287 static int
6288ex_call_inner(
6289 exarg_T *eap,
6290 char_u *name,
6291 char_u **arg,
6292 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006293 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006294 evalarg_T *evalarg)
6295{
6296 linenr_T lnum;
6297 int doesrange;
6298 typval_T rettv;
6299 int failed = FALSE;
6300
zeertzjq5299c092023-04-12 20:48:16 +01006301 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006302 for ( ; lnum <= eap->line2; ++lnum)
6303 {
6304 funcexe_T funcexe;
6305
zeertzjq5299c092023-04-12 20:48:16 +01006306 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006307 {
6308 if (lnum > curbuf->b_ml.ml_line_count)
6309 {
6310 // If the function deleted lines or switched to another buffer
6311 // the line number may become invalid.
6312 emsg(_(e_invalid_range));
6313 break;
6314 }
6315 curwin->w_cursor.lnum = lnum;
6316 curwin->w_cursor.col = 0;
6317 curwin->w_cursor.coladd = 0;
6318 }
6319 *arg = startarg;
6320
6321 funcexe = *funcexe_init;
6322 funcexe.fe_doesrange = &doesrange;
6323 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6324 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6325 {
6326 failed = TRUE;
6327 break;
6328 }
6329 if (has_watchexpr())
6330 dbg_check_breakpoint(eap);
6331
6332 // Handle a function returning a Funcref, Dictionary or List.
6333 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006334 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006335 {
6336 failed = TRUE;
6337 break;
6338 }
6339
6340 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006341 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006342 break;
6343
6344 // Stop when immediately aborting on error, or when an interrupt
6345 // occurred or an exception was thrown but not caught.
6346 // get_func_tv() returned OK, so that the check for trailing
6347 // characters below is executed.
6348 if (aborting())
6349 break;
6350 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006351 return failed;
6352}
6353
6354/*
6355 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6356 * Returns FAIL or OK.
6357 */
6358 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006359ex_defer_inner(
6360 char_u *name,
6361 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006362 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006363 partial_T *partial,
6364 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006365{
6366 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006367 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006368 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006369 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006370
6371 if (current_funccal == NULL)
6372 {
6373 semsg(_(e_str_not_inside_function), "defer");
6374 return FAIL;
6375 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006376 if (partial != NULL)
6377 {
6378 if (partial->pt_dict != NULL)
6379 {
6380 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6381 return FAIL;
6382 }
6383 if (partial->pt_argc > 0)
6384 {
6385 int i;
6386
6387 partial_argc = partial->pt_argc;
6388 for (i = 0; i < partial_argc; ++i)
6389 copy_tv(&partial->pt_argv[i], &argvars[i]);
6390 }
6391 }
Ernie Raelb077b582023-12-14 20:11:44 +01006392 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006393 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006394 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006395 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006396
6397 if (r == OK)
6398 {
6399 if (type != NULL)
6400 {
6401 // Check that the arguments are OK for the types of the funcref.
6402 r = check_argument_types(type, argvars, argcount, NULL, name);
6403 }
Ernie Raelb077b582023-12-14 20:11:44 +01006404 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006405 {
6406 int idx = find_internal_func(name);
6407
6408 if (idx < 0)
6409 {
6410 emsg_funcname(e_unknown_function_str, name);
6411 r = FAIL;
6412 }
6413 else if (check_internal_func(idx, argcount) == -1)
6414 r = FAIL;
6415 }
6416 else
6417 {
6418 ufunc_T *ufunc = find_func(name, FALSE);
6419
6420 // we tolerate an unknown function here, it might be defined later
6421 if (ufunc != NULL)
6422 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006423 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006424 if (error != FCERR_UNKNOWN)
6425 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006426 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006427 r = FAIL;
6428 }
6429 }
6430 }
6431 }
6432
Bram Moolenaar86d87252022-09-05 21:21:25 +01006433 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006434 {
6435 while (--argcount >= 0)
6436 clear_tv(&argvars[argcount]);
6437 return FAIL;
6438 }
6439 return add_defer(name, argcount, argvars);
6440}
6441
6442/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006443 * Return TRUE if currently inside a function call.
6444 * Give an error message and return FALSE when not.
6445 */
6446 int
6447can_add_defer(void)
6448{
6449 if (!in_def_function() && get_current_funccal() == NULL)
6450 {
6451 semsg(_(e_str_not_inside_function), "defer");
6452 return FALSE;
6453 }
6454 return TRUE;
6455}
6456
6457/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006458 * Add a deferred call for "name" with arguments "argvars[argcount]".
6459 * Consumes "argvars[]".
6460 * Caller must check that in_def_function() returns TRUE or current_funccal is
6461 * not NULL.
6462 * Returns OK or FAIL.
6463 */
6464 int
6465add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6466{
6467 char_u *saved_name = vim_strsave(name);
6468 int argcount = argcount_arg;
6469 defer_T *dr;
6470 int ret = FAIL;
6471
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006472 if (saved_name == NULL)
6473 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006474 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006475 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006476 if (add_defer_function(saved_name, argcount, argvars) == OK)
6477 argcount = 0;
6478 }
6479 else
6480 {
6481 if (current_funccal->fc_defer.ga_itemsize == 0)
6482 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6483 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6484 goto theend;
6485 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6486 + current_funccal->fc_defer.ga_len++;
6487 dr->dr_name = saved_name;
6488 dr->dr_argcount = argcount;
6489 while (argcount > 0)
6490 {
6491 --argcount;
6492 dr->dr_argvars[argcount] = argvars[argcount];
6493 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006494 }
6495 ret = OK;
6496
6497theend:
6498 while (--argcount >= 0)
6499 clear_tv(&argvars[argcount]);
6500 return ret;
6501}
6502
6503/*
6504 * Invoked after a functions has finished: invoke ":defer" functions.
6505 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006506 static void
6507handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006508{
6509 int idx;
6510
Bram Moolenaar58779852022-09-06 18:31:14 +01006511 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006512 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006513 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006514
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006515 if (dr->dr_name == NULL)
6516 // already being called, can happen if function does ":qa"
6517 continue;
6518
6519 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006520 CLEAR_FIELD(funcexe);
6521 funcexe.fe_evaluate = TRUE;
6522
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006523 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006524 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006525
6526 char_u *name = dr->dr_name;
6527 dr->dr_name = NULL;
6528
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006529 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006530 // first statement in the function will be executed (because of the
6531 // exception). So save and restore the try/catch/throw exception
6532 // state.
6533 exception_state_T estate;
6534 exception_state_save(&estate);
6535 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006536
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006537 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6538
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006539 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006540
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006541 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006542 vim_free(name);
6543 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006544 clear_tv(&dr->dr_argvars[i]);
6545 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006546 ga_clear(&funccal->fc_defer);
6547}
6548
zeertzjq960cf912023-04-18 21:52:54 +01006549 static void
6550invoke_funccall_defer(funccall_T *fc)
6551{
6552 if (fc->fc_ectx != NULL)
6553 {
6554 // :def function
6555 unwind_def_callstack(fc->fc_ectx);
6556 may_invoke_defer_funcs(fc->fc_ectx);
6557 }
6558 else
6559 {
6560 // legacy function
6561 handle_defer_one(fc);
6562 }
6563}
6564
Bram Moolenaar58779852022-09-06 18:31:14 +01006565/*
6566 * Called when exiting: call all defer functions.
6567 */
6568 void
6569invoke_all_defer(void)
6570{
zeertzjq1be4b812023-04-19 14:21:24 +01006571 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6572 invoke_funccall_defer(fc);
6573
zeertzjq960cf912023-04-18 21:52:54 +01006574 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6575 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6576 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006577}
6578
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006579/*
6580 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006581 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006582 */
6583 void
6584ex_call(exarg_T *eap)
6585{
6586 char_u *arg = eap->arg;
6587 char_u *startarg;
6588 char_u *name;
6589 char_u *tofree;
6590 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006591 int failed = FALSE;
6592 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006593 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006594 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006595 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006596 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006597 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006598 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006599
Bram Moolenaare6b53242020-07-01 17:28:33 +02006600 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006601 if (eap->skip)
6602 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006603 typval_T rettv;
6604
Bram Moolenaare38eab22019-12-05 21:50:01 +01006605 // trans_function_name() doesn't work well when skipping, use eval0()
6606 // instead to skip to any following command, e.g. for:
6607 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006608 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006609 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006610 clear_tv(&rettv);
6611 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006612 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006613 return;
6614 }
6615
zeertzjq5299c092023-04-12 20:48:16 +01006616 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006617 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006618 if (fudi.fd_newkey != NULL)
6619 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006620 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006621 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006622 vim_free(fudi.fd_newkey);
6623 }
6624 if (tofree == NULL)
6625 return;
6626
Bram Moolenaare38eab22019-12-05 21:50:01 +01006627 // Increase refcount on dictionary, it could get deleted when evaluating
6628 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006629 if (fudi.fd_dict != NULL)
6630 ++fudi.fd_dict->dv_refcount;
6631
Bram Moolenaare38eab22019-12-05 21:50:01 +01006632 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6633 // contents. For VAR_PARTIAL get its partial, unless we already have one
6634 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006635 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006636 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006637 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006638 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006639
Bram Moolenaare38eab22019-12-05 21:50:01 +01006640 // Skip white space to allow ":call func ()". Not good, but required for
6641 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006642 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006643 if (*startarg != '(')
6644 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006645 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006646 goto end;
6647 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006648 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006649 {
6650 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6651 goto end;
6652 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006653
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006654 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006655 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006656 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006657 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006658 }
6659 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006660 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006661 funcexe_T funcexe;
6662
Bram Moolenaara80faa82020-04-12 19:37:17 +02006663 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006664 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006665 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006666 funcexe.fe_partial = partial;
6667 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006668 funcexe.fe_firstline = eap->line1;
6669 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006670 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006671 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006672 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006673 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006674
Bram Moolenaar1d341892021-09-18 15:25:52 +02006675 // When inside :try we need to check for following "| catch" or "| endtry".
6676 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006677 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006678 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006679 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006680 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006681 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006682 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006683 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006684 {
6685 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006686 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006687 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006688 }
6689 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006690 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006691 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006692 // Must be after using "arg", it may point into memory cleared here.
6693 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006694
6695end:
6696 dict_unref(fudi.fd_dict);
6697 vim_free(tofree);
6698}
6699
6700/*
6701 * Return from a function. Possibly makes the return pending. Also called
6702 * for a pending return at the ":endtry" or after returning from an extra
6703 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6704 * when called due to a ":return" command. "rettv" may point to a typval_T
6705 * with the return rettv. Returns TRUE when the return can be carried out,
6706 * FALSE when the return gets pending.
6707 */
6708 int
6709do_return(
6710 exarg_T *eap,
6711 int reanimate,
6712 int is_cmd,
6713 void *rettv)
6714{
6715 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006716 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006717
6718 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006719 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006720 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006721
6722 /*
6723 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6724 * not in its finally clause (which then is to be executed next) is found.
6725 * In this case, make the ":return" pending for execution at the ":endtry".
6726 * Otherwise, return normally.
6727 */
6728 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6729 if (idx >= 0)
6730 {
6731 cstack->cs_pending[idx] = CSTP_RETURN;
6732
6733 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006734 // A pending return again gets pending. "rettv" points to an
6735 // allocated variable with the rettv of the original ":return"'s
6736 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006737 cstack->cs_rettv[idx] = rettv;
6738 else
6739 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006740 // When undoing a return in order to make it pending, get the stored
6741 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006742 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006743 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006744
6745 if (rettv != NULL)
6746 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006747 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006748 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6749 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6750 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006751 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006752 }
6753 else
6754 cstack->cs_rettv[idx] = NULL;
6755
6756 if (reanimate)
6757 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006758 // The pending return value could be overwritten by a ":return"
6759 // without argument in a finally clause; reset the default
6760 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006761 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6762 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006763 }
6764 }
6765 report_make_pending(CSTP_RETURN, rettv);
6766 }
6767 else
6768 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006769 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006770
Bram Moolenaare38eab22019-12-05 21:50:01 +01006771 // If the return is carried out now, store the return value. For
6772 // a return immediately after reanimation, the value is already
6773 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006774 if (!reanimate && rettv != NULL)
6775 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006776 clear_tv(current_funccal->fc_rettv);
6777 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006778 if (!is_cmd)
6779 vim_free(rettv);
6780 }
6781 }
6782
6783 return idx < 0;
6784}
6785
6786/*
6787 * Free the variable with a pending return value.
6788 */
6789 void
6790discard_pending_return(void *rettv)
6791{
6792 free_tv((typval_T *)rettv);
6793}
6794
6795/*
6796 * Generate a return command for producing the value of "rettv". The result
6797 * is an allocated string. Used by report_pending() for verbose messages.
6798 */
6799 char_u *
6800get_return_cmd(void *rettv)
6801{
6802 char_u *s = NULL;
6803 char_u *tofree = NULL;
6804 char_u numbuf[NUMBUFLEN];
6805
6806 if (rettv != NULL)
6807 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6808 if (s == NULL)
6809 s = (char_u *)"";
6810
6811 STRCPY(IObuff, ":return ");
6812 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6813 if (STRLEN(s) + 8 >= IOSIZE)
6814 STRCPY(IObuff + IOSIZE - 4, "...");
6815 vim_free(tofree);
6816 return vim_strsave(IObuff);
6817}
6818
6819/*
6820 * Get next function line.
6821 * Called by do_cmdline() to get the next line.
6822 * Returns allocated string, or NULL for end of function.
6823 */
6824 char_u *
6825get_func_line(
6826 int c UNUSED,
6827 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006828 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006829 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006830{
6831 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006832 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006833 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006834 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006835
Bram Moolenaare38eab22019-12-05 21:50:01 +01006836 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006837 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006838 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006839 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006840 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006841 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006842 }
6843#ifdef FEAT_PROFILE
6844 if (do_profiling == PROF_YES)
6845 func_line_end(cookie);
6846#endif
6847
6848 gap = &fp->uf_lines;
6849 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006850 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006851 retval = NULL;
6852 else
6853 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006854 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006855 while (fcp->fc_linenr < gap->ga_len
6856 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6857 ++fcp->fc_linenr;
6858 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006859 retval = NULL;
6860 else
6861 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006862 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6863 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006864#ifdef FEAT_PROFILE
6865 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006866 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006867#endif
6868 }
6869 }
6870
Bram Moolenaare38eab22019-12-05 21:50:01 +01006871 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006872 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006873 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006874 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006875 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006876 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006877 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006878 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006879 }
6880
6881 return retval;
6882}
6883
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006884/*
6885 * Return TRUE if the currently active function should be ended, because a
6886 * return was encountered or an error occurred. Used inside a ":while".
6887 */
6888 int
6889func_has_ended(void *cookie)
6890{
6891 funccall_T *fcp = (funccall_T *)cookie;
6892
Bram Moolenaare38eab22019-12-05 21:50:01 +01006893 // Ignore the "abort" flag if the abortion behavior has been changed due to
6894 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006895 return (((fcp->fc_func->uf_flags & FC_ABORT)
6896 && did_emsg && !aborted_in_try())
6897 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006898}
6899
6900/*
6901 * return TRUE if cookie indicates a function which "abort"s on errors.
6902 */
6903 int
6904func_has_abort(
6905 void *cookie)
6906{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006907 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006908}
6909
6910
6911/*
6912 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6913 * Don't do this when "Func" is already a partial that was bound
6914 * explicitly (pt_auto is FALSE).
6915 * Changes "rettv" in-place.
6916 * Returns the updated "selfdict_in".
6917 */
6918 dict_T *
6919make_partial(dict_T *selfdict_in, typval_T *rettv)
6920{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006921 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006922 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006923 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006924 dict_T *selfdict = selfdict_in;
6925
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006926 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6927 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006928 fp = rettv->vval.v_partial->pt_func;
6929 else
6930 {
6931 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006932 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006933 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006934 if (fname == NULL)
6935 {
6936 // There is no point binding a dict to a NULL function, just create
6937 // a function reference.
6938 rettv->v_type = VAR_FUNC;
6939 rettv->vval.v_string = NULL;
6940 }
6941 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006942 {
6943 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006944 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006945
6946 // Translate "s:func" to the stored function name.
6947 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6948 fp = find_func(fname, FALSE);
6949 vim_free(tofree);
6950 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006951 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006952
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006953 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006954 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006955 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006956
6957 if (pt != NULL)
6958 {
6959 pt->pt_refcount = 1;
6960 pt->pt_dict = selfdict;
6961 pt->pt_auto = TRUE;
6962 selfdict = NULL;
6963 if (rettv->v_type == VAR_FUNC)
6964 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006965 // Just a function: Take over the function name and use
6966 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006967 pt->pt_name = rettv->vval.v_string;
6968 }
6969 else
6970 {
6971 partial_T *ret_pt = rettv->vval.v_partial;
6972 int i;
6973
Bram Moolenaare38eab22019-12-05 21:50:01 +01006974 // Partial: copy the function name, use selfdict and copy
6975 // args. Can't take over name or args, the partial might
6976 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006977 if (ret_pt->pt_name != NULL)
6978 {
6979 pt->pt_name = vim_strsave(ret_pt->pt_name);
6980 func_ref(pt->pt_name);
6981 }
6982 else
6983 {
6984 pt->pt_func = ret_pt->pt_func;
6985 func_ptr_ref(pt->pt_func);
6986 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006987 if (ret_pt->pt_argc > 0)
6988 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006989 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006990 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006991 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006992 pt->pt_argc = 0;
6993 else
6994 {
6995 pt->pt_argc = ret_pt->pt_argc;
6996 for (i = 0; i < pt->pt_argc; i++)
6997 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6998 }
6999 }
7000 partial_unref(ret_pt);
7001 }
7002 rettv->v_type = VAR_PARTIAL;
7003 rettv->vval.v_partial = pt;
7004 }
7005 }
7006 return selfdict;
7007}
7008
7009/*
7010 * Return the name of the executed function.
7011 */
7012 char_u *
7013func_name(void *cookie)
7014{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007015 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007016}
7017
7018/*
7019 * Return the address holding the next breakpoint line for a funccall cookie.
7020 */
7021 linenr_T *
7022func_breakpoint(void *cookie)
7023{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007024 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007025}
7026
7027/*
7028 * Return the address holding the debug tick for a funccall cookie.
7029 */
7030 int *
7031func_dbg_tick(void *cookie)
7032{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007033 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007034}
7035
7036/*
7037 * Return the nesting level for a funccall cookie.
7038 */
7039 int
7040func_level(void *cookie)
7041{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007042 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007043}
7044
7045/*
7046 * Return TRUE when a function was ended by a ":return" command.
7047 */
7048 int
7049current_func_returned(void)
7050{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007051 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007052}
7053
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007054 int
7055free_unref_funccal(int copyID, int testing)
7056{
7057 int did_free = FALSE;
7058 int did_free_funccal = FALSE;
7059 funccall_T *fc, **pfc;
7060
7061 for (pfc = &previous_funccal; *pfc != NULL; )
7062 {
7063 if (can_free_funccal(*pfc, copyID))
7064 {
7065 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007066 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01007067 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007068 did_free = TRUE;
7069 did_free_funccal = TRUE;
7070 }
7071 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01007072 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007073 }
7074 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01007075 // When a funccal was freed some more items might be garbage
7076 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007077 (void)garbage_collect(testing);
7078
7079 return did_free;
7080}
7081
7082/*
Bram Moolenaarba209902016-08-24 22:06:38 +02007083 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007084 */
7085 static funccall_T *
7086get_funccal(void)
7087{
7088 int i;
7089 funccall_T *funccal;
7090 funccall_T *temp_funccal;
7091
7092 funccal = current_funccal;
7093 if (debug_backtrace_level > 0)
7094 {
7095 for (i = 0; i < debug_backtrace_level; i++)
7096 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007097 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007098 if (temp_funccal)
7099 funccal = temp_funccal;
7100 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01007101 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007102 debug_backtrace_level = i;
7103 }
7104 }
7105 return funccal;
7106}
7107
7108/*
7109 * Return the hashtable used for local variables in the current funccal.
7110 * Return NULL if there is no current funccal.
7111 */
7112 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007113get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007114{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007115 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007116 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007117 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007118}
7119
7120/*
7121 * Return the l: scope variable.
7122 * Return NULL if there is no current funccal.
7123 */
7124 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007125get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007126{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007127 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007128 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007129 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007130}
7131
7132/*
7133 * Return the hashtable used for argument in the current funccal.
7134 * Return NULL if there is no current funccal.
7135 */
7136 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007137get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007138{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007139 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007140 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007141 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007142}
7143
7144/*
7145 * Return the a: scope variable.
7146 * Return NULL if there is no current funccal.
7147 */
7148 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007149get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007150{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007151 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007152 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007153 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007154}
7155
7156/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007157 * List function variables, if there is a function.
7158 */
7159 void
7160list_func_vars(int *first)
7161{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007162 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
7163 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01007164 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007165}
7166
7167/*
7168 * If "ht" is the hashtable for local variables in the current funccal, return
7169 * the dict that contains it.
7170 * Otherwise return NULL.
7171 */
7172 dict_T *
7173get_current_funccal_dict(hashtab_T *ht)
7174{
7175 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01007176 && ht == &current_funccal->fc_l_vars.dv_hashtab)
7177 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007178 return NULL;
7179}
7180
7181/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007182 * Search hashitem in parent scope.
7183 */
7184 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007185find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007186{
7187 funccall_T *old_current_funccal = current_funccal;
7188 hashtab_T *ht;
7189 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007190 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007191
Bram Moolenaarca16c602022-09-06 18:57:08 +01007192 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007193 return NULL;
7194
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02007195 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01007196 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02007197 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007198 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007199 ht = find_var_ht(name, &varname);
7200 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02007201 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007202 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02007203 if (!HASHITEM_EMPTY(hi))
7204 {
7205 *pht = ht;
7206 break;
7207 }
7208 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007209 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02007210 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007211 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007212 }
7213 current_funccal = old_current_funccal;
7214
7215 return hi;
7216}
7217
7218/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007219 * Search variable in parent scope.
7220 */
7221 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007222find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007223{
7224 dictitem_T *v = NULL;
7225 funccall_T *old_current_funccal = current_funccal;
7226 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007227 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007228
Bram Moolenaarca16c602022-09-06 18:57:08 +01007229 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007230 return NULL;
7231
Bram Moolenaare38eab22019-12-05 21:50:01 +01007232 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01007233 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007234 while (current_funccal)
7235 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007236 ht = find_var_ht(name, &varname);
7237 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007238 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007239 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007240 if (v != NULL)
7241 break;
7242 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007243 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007244 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007245 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007246 }
7247 current_funccal = old_current_funccal;
7248
7249 return v;
7250}
7251
7252/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007253 * Set "copyID + 1" in previous_funccal and callers.
7254 */
7255 int
7256set_ref_in_previous_funccal(int copyID)
7257{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007258 funccall_T *fc;
7259
Bram Moolenaarca16c602022-09-06 18:57:08 +01007260 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007261 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007262 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007263 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
7264 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
7265 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007266 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007267 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007268 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007269}
7270
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007271 static int
7272set_ref_in_funccal(funccall_T *fc, int copyID)
7273{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007274 if (fc->fc_copyID != copyID)
7275 {
7276 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007277 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7278 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7279 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7280 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007281 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007282 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007283 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007284}
7285
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007286/*
7287 * Set "copyID" in all local vars and arguments in the call stack.
7288 */
7289 int
7290set_ref_in_call_stack(int copyID)
7291{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007292 funccall_T *fc;
7293 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007294
Bram Moolenaarca16c602022-09-06 18:57:08 +01007295 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007296 if (set_ref_in_funccal(fc, copyID))
7297 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007298
7299 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007300 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007301 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007302 if (set_ref_in_funccal(fc, copyID))
7303 return TRUE;
7304 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007305}
7306
7307/*
7308 * Set "copyID" in all functions available by name.
7309 */
7310 int
7311set_ref_in_functions(int copyID)
7312{
7313 int todo;
7314 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007315 ufunc_T *fp;
7316
7317 todo = (int)func_hashtab.ht_used;
7318 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007319 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007320 if (!HASHITEM_EMPTY(hi))
7321 {
7322 --todo;
7323 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007324 if (!func_name_refcount(fp->uf_name)
7325 && set_ref_in_func(NULL, fp, copyID))
7326 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007327 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007328 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007329 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007330}
7331
7332/*
7333 * Set "copyID" in all function arguments.
7334 */
7335 int
7336set_ref_in_func_args(int copyID)
7337{
7338 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007339
7340 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007341 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7342 copyID, NULL, NULL))
7343 return TRUE;
7344 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007345}
7346
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007347/*
7348 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007349 * Returns TRUE if setting references failed somehow.
7350 */
7351 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007352set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007353{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007354 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007355 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007356 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007357 char_u fname_buf[FLEN_FIXED + 1];
7358 char_u *tofree = NULL;
7359 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007360 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007361
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007362 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007363 return FALSE;
7364
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007365 if (fp_in == NULL)
7366 {
7367 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007368 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007369 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007370 if (fp != NULL)
7371 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007372 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007373 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007374 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007375
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007376 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007377 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007378}
7379
Bram Moolenaare38eab22019-12-05 21:50:01 +01007380#endif // FEAT_EVAL