blob: c8d08bb45cfe1e49de853609ab2920c02006e026 [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
32static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
33static char *e_funcdict = N_("E717: Dictionary entry already exists");
34static char *e_funcref = N_("E718: Funcref required");
35static char *e_nofunc = N_("E130: Unknown function: %s");
36
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020037static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010038static void func_clear(ufunc_T *fp, int force);
39static int func_free(ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020040
41 void
42func_init()
43{
44 hash_init(&func_hashtab);
45}
46
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020047/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020048 * Return the function hash table
49 */
50 hashtab_T *
51func_tbl_get(void)
52{
53 return &func_hashtab;
54}
55
56/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020057 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020058 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010059 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010060 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010061 * Return a pointer to after the type.
62 * When something is wrong return "arg".
63 */
64 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010065one_function_arg(
66 char_u *arg,
67 garray_T *newargs,
68 garray_T *argtypes,
69 int types_optional,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010070 evalarg_T *evalarg,
Bram Moolenaar2a389082021-04-09 20:24:31 +020071 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010072 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073{
Bram Moolenaar6e949782020-04-13 17:21:00 +020074 char_u *p = arg;
75 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020076 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077
78 while (ASCII_ISALNUM(*p) || *p == '_')
79 ++p;
80 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020081 || (argtypes == NULL
82 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
83 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 {
85 if (!skip)
86 semsg(_("E125: Illegal argument: %s"), arg);
87 return arg;
88 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010089
Bram Moolenaar057e84a2021-02-28 16:55:11 +010090 // Vim9 script: cannot use script var name for argument. In function: also
91 // check local vars and arguments.
92 if (!skip && argtypes != NULL && check_defined(arg, p - arg,
93 evalarg == NULL ? NULL : evalarg->eval_cctx, TRUE) == FAIL)
Bram Moolenaarb4893b82021-02-21 22:20:24 +010094 return arg;
Bram Moolenaarb4893b82021-02-21 22:20:24 +010095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
97 return arg;
98 if (newargs != NULL)
99 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100100 int c;
101 int i;
102
103 c = *p;
104 *p = NUL;
105 arg_copy = vim_strsave(arg);
106 if (arg_copy == NULL)
107 {
108 *p = c;
109 return arg;
110 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200111 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200112 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200113 // Check for duplicate argument name.
114 for (i = 0; i < newargs->ga_len; ++i)
115 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
116 {
117 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
118 vim_free(arg_copy);
119 return arg;
120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
122 newargs->ga_len++;
123
124 *p = c;
125 }
126
127 // get any type from "arg: type"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100128 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 {
130 char_u *type = NULL;
131
Bram Moolenaar6e949782020-04-13 17:21:00 +0200132 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
133 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200134 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200135 arg_copy == NULL ? arg : arg_copy);
136 p = skipwhite(p);
137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 if (*p == ':')
139 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200140 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100141 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200142 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100143 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200144 return arg;
145 }
146 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200147 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100148 if (!skip)
149 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200151 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200152 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200153 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200154 arg_copy == NULL ? arg : arg_copy);
155 return arg;
156 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100157 if (!skip)
158 {
159 if (type == NULL && types_optional)
160 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200161 type = vim_strsave((char_u *)
162 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100163 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 }
166
167 return p;
168}
169
170/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200171 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100172 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100173 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200174 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100175 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200176get_function_args(
177 char_u **argp,
178 char_u endchar,
179 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100180 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100181 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100182 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200183 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200184 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200185 int skip,
186 exarg_T *eap,
187 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200188{
189 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100190 char_u *arg;
191 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200192 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200193 int any_default = FALSE;
194 char_u *expr;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100195 char_u *whitep = *argp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200196
197 if (newargs != NULL)
198 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100199 if (argtypes != NULL)
200 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200201 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200202 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200203
204 if (varargs != NULL)
205 *varargs = FALSE;
206
207 /*
208 * Isolate the arguments: "arg1, arg2, ...)"
209 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100210 arg = skipwhite(*argp);
211 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200212 while (*p != endchar)
213 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200214 while (eap != NULL && eap->getline != NULL
215 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200216 {
217 char_u *theline;
218
219 // End of the line, get the next one.
220 theline = eap->getline(':', eap->cookie, 0, TRUE);
221 if (theline == NULL)
222 break;
223 vim_free(*line_to_free);
224 *line_to_free = theline;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200225 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200226 p = skipwhite(theline);
227 }
228
229 if (mustend && *p != endchar)
230 {
231 if (!skip)
232 semsg(_(e_invarg2), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100233 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200234 }
235 if (*p == endchar)
236 break;
237
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200238 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
239 {
240 if (varargs != NULL)
241 *varargs = TRUE;
242 p += 3;
243 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244
245 if (argtypes != NULL)
246 {
247 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200248 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100250 if (!skip)
251 emsg(_(e_missing_name_after_dots));
252 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 }
254
255 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100256 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaar2a389082021-04-09 20:24:31 +0200257 evalarg, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258 if (p == arg)
259 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100260 if (*skipwhite(p) == '=')
261 {
262 emsg(_(e_cannot_use_default_for_variable_arguments));
263 break;
264 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200266 }
267 else
268 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200269 char_u *np;
270
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200271 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100272 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaar2a389082021-04-09 20:24:31 +0200273 evalarg, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200275 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200276
Bram Moolenaar015cf102021-06-26 21:52:02 +0200277 // Recognize " = expr" but not " == expr". A lambda can have
278 // "(a = expr" but "(a == expr" is not a lambda.
279 np = skipwhite(p);
280 if (*np == '=' && np[1] != '=' && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200281 {
282 typval_T rettv;
283
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100284 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200285 any_default = TRUE;
286 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200287 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200288 p = skipwhite(p);
289 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200290 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200291 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200292 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200293 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200294 if (ga_grow(default_args, 1) == FAIL)
295 goto err_ret;
296
297 // trim trailing whitespace
298 while (p > expr && VIM_ISWHITE(p[-1]))
299 p--;
300 c = *p;
301 *p = NUL;
302 expr = vim_strsave(expr);
303 if (expr == NULL)
304 {
305 *p = c;
306 goto err_ret;
307 }
308 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200309 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200310 default_args->ga_len++;
311 *p = c;
312 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200313 }
314 else
315 mustend = TRUE;
316 }
317 else if (any_default)
318 {
319 emsg(_("E989: Non-default argument follows default argument"));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200320 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200321 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200322
323 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
324 {
325 // Be tolerant when skipping
326 if (!skip)
327 {
328 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
329 goto err_ret;
330 }
331 p = skipwhite(p);
332 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200333 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200334 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200335 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200336 // Don't give this error when skipping, it makes the "->" not
337 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100338 // Allow missing space after comma in legacy functions.
339 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200340 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
341 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100342 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200343 goto err_ret;
344 }
345 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200346 else
347 mustend = TRUE;
348 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200349 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200350 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200351 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200352
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200353 if (*p != endchar)
354 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100355 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200356
357 *argp = p;
358 return OK;
359
360err_ret:
361 if (newargs != NULL)
362 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200363 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200364 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200365 return FAIL;
366}
367
368/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100369 * Parse the argument types, filling "fp->uf_arg_types".
370 * Return OK or FAIL.
371 */
372 static int
373parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
374{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200375 int len = 0;
376
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100377 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
378 if (argtypes->ga_len > 0)
379 {
380 // When "varargs" is set the last name/type goes into uf_va_name
381 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200382 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100383
384 if (len > 0)
385 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
386 if (fp->uf_arg_types != NULL)
387 {
388 int i;
389 type_T *type;
390
391 for (i = 0; i < len; ++ i)
392 {
393 char_u *p = ((char_u **)argtypes->ga_data)[i];
394
395 if (p == NULL)
396 // will get the type from the default value
397 type = &t_unknown;
398 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100399 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100400 if (type == NULL)
401 return FAIL;
402 fp->uf_arg_types[i] = type;
403 }
404 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100405 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200406
407 if (varargs)
408 {
409 char_u *p;
410
411 // Move the last argument "...name: type" to uf_va_name and
412 // uf_va_type.
413 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
414 [fp->uf_args.ga_len - 1];
415 --fp->uf_args.ga_len;
416 p = ((char_u **)argtypes->ga_data)[len];
417 if (p == NULL)
418 // TODO: get type from default value
419 fp->uf_va_type = &t_list_any;
420 else
421 {
422 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
423 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
424 {
425 semsg(_(e_variable_arguments_type_must_be_list_str),
426 ((char_u **)argtypes->ga_data)[len]);
427 return FAIL;
428 }
429 }
430 if (fp->uf_va_type == NULL)
431 return FAIL;
432 }
433
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100434 return OK;
435}
436
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100437 static int
438parse_return_type(ufunc_T *fp, char_u *ret_type)
439{
440 if (ret_type == NULL)
441 fp->uf_ret_type = &t_void;
442 else
443 {
444 char_u *p = ret_type;
445
446 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
447 if (fp->uf_ret_type == NULL)
448 {
449 fp->uf_ret_type = &t_void;
450 return FAIL;
451 }
452 }
453 return OK;
454}
455
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100456/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200457 * Register function "fp" as using "current_funccal" as its scope.
458 */
459 static int
460register_closure(ufunc_T *fp)
461{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200462 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100463 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200464 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200465 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200466 fp->uf_scoped = current_funccal;
467 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200468
Bram Moolenaar58016442016-07-31 18:30:22 +0200469 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
470 return FAIL;
471 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
472 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200473 return OK;
474}
475
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100476 static void
477set_ufunc_name(ufunc_T *fp, char_u *name)
478{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100479 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
480 // actually extends beyond the struct.
481 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100482
483 if (name[0] == K_SPECIAL)
484 {
485 fp->uf_name_exp = alloc(STRLEN(name) + 3);
486 if (fp->uf_name_exp != NULL)
487 {
488 STRCPY(fp->uf_name_exp, "<SNR>");
489 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
490 }
491 }
492}
493
Bram Moolenaar58016442016-07-31 18:30:22 +0200494/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200495 * Get a name for a lambda. Returned in static memory.
496 */
497 char_u *
498get_lambda_name(void)
499{
500 static char_u name[30];
501 static int lambda_no = 0;
502
503 sprintf((char*)name, "<lambda>%d", ++lambda_no);
504 return name;
505}
506
Bram Moolenaar801ab062020-06-25 19:27:56 +0200507#if defined(FEAT_LUA) || defined(PROTO)
508/*
509 * Registers a native C callback which can be called from Vim script.
510 * Returns the name of the Vim script function.
511 */
512 char_u *
513register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
514{
515 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200516 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200517
518 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
519 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200520 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200521
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200522 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200523 fp->uf_refcount = 1;
524 fp->uf_varargs = TRUE;
525 fp->uf_flags = FC_CFUNC;
526 fp->uf_calls = 0;
527 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200528 fp->uf_cb = cb;
529 fp->uf_cb_free = cb_free;
530 fp->uf_cb_state = state;
531
532 set_ufunc_name(fp, name);
533 hash_add(&func_hashtab, UF2HIKEY(fp));
534
535 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200536}
537#endif
538
Bram Moolenaar04b12692020-05-04 23:24:44 +0200539/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100540 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100541 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100542 * If "white_error" is not NULL check for correct use of white space and set
543 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100544 * Return NULL if no valid arrow found.
545 */
546 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100547skip_arrow(
548 char_u *start,
549 int equal_arrow,
550 char_u **ret_type,
551 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100552{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100553 char_u *s = start;
554 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100555
556 if (equal_arrow)
557 {
558 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100559 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100560 if (white_error != NULL && !VIM_ISWHITE(s[1]))
561 {
562 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100563 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100564 return NULL;
565 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100566 s = skipwhite(s + 1);
567 *ret_type = s;
568 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100569 if (s == *ret_type)
570 {
571 emsg(_(e_missing_return_type));
572 return NULL;
573 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100574 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100575 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100576 s = skipwhite(s);
577 if (*s != '=')
578 return NULL;
579 ++s;
580 }
581 if (*s != '>')
582 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100583 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
584 || !IS_WHITE_OR_NUL(s[1])))
585 {
586 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100587 semsg(_(e_white_space_required_before_and_after_str_at_str),
588 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100589 return NULL;
590 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100591 return skipwhite(s + 1);
592}
593
594/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100595 * Check if "*cmd" points to a function command and if so advance "*cmd" and
596 * return TRUE.
597 * Otherwise return FALSE;
598 * Do not consider "function(" to be a command.
599 */
600 static int
601is_function_cmd(char_u **cmd)
602{
603 char_u *p = *cmd;
604
605 if (checkforcmd(&p, "function", 2))
606 {
607 if (*p == '(')
608 return FALSE;
609 *cmd = p;
610 return TRUE;
611 }
612 return FALSE;
613}
614
615/*
616 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200617 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100618 * "newlines" must already have been initialized.
619 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
620 */
621 static int
622get_function_body(
623 exarg_T *eap,
624 garray_T *newlines,
625 char_u *line_arg_in,
626 char_u **line_to_free)
627{
628 linenr_T sourcing_lnum_top = SOURCING_LNUM;
629 linenr_T sourcing_lnum_off;
630 int saved_wait_return = need_wait_return;
631 char_u *line_arg = line_arg_in;
632 int vim9_function = eap->cmdidx == CMD_def
633 || eap->cmdidx == CMD_block;
634#define MAX_FUNC_NESTING 50
635 char nesting_def[MAX_FUNC_NESTING];
636 int nesting = 0;
637 getline_opt_T getline_options;
638 int indent = 2;
639 char_u *skip_until = NULL;
640 int ret = FAIL;
641 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200642 int heredoc_concat_len = 0;
643 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100644 char_u *heredoc_trimmed = NULL;
645
Bram Moolenaar20677332021-06-06 17:02:53 +0200646 ga_init2(&heredoc_ga, 1, 500);
647
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100648 // Detect having skipped over comment lines to find the return
649 // type. Add NULL lines to keep the line count correct.
650 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
651 if (SOURCING_LNUM < sourcing_lnum_off)
652 {
653 sourcing_lnum_off -= SOURCING_LNUM;
654 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
655 goto theend;
656 while (sourcing_lnum_off-- > 0)
657 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
658 }
659
660 nesting_def[nesting] = vim9_function;
661 getline_options = vim9_function
662 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
663 for (;;)
664 {
665 char_u *theline;
666 char_u *p;
667 char_u *arg;
668
669 if (KeyTyped)
670 {
671 msg_scroll = TRUE;
672 saved_wait_return = FALSE;
673 }
674 need_wait_return = FALSE;
675
676 if (line_arg != NULL)
677 {
678 // Use eap->arg, split up in parts by line breaks.
679 theline = line_arg;
680 p = vim_strchr(theline, '\n');
681 if (p == NULL)
682 line_arg += STRLEN(line_arg);
683 else
684 {
685 *p = NUL;
686 line_arg = p + 1;
687 }
688 }
689 else
690 {
691 vim_free(*line_to_free);
692 if (eap->getline == NULL)
693 theline = getcmdline(':', 0L, indent, getline_options);
694 else
695 theline = eap->getline(':', eap->cookie, indent,
696 getline_options);
697 *line_to_free = theline;
698 }
699 if (KeyTyped)
700 lines_left = Rows - 1;
701 if (theline == NULL)
702 {
703 // Use the start of the function for the line number.
704 SOURCING_LNUM = sourcing_lnum_top;
705 if (skip_until != NULL)
706 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
707 else if (eap->cmdidx == CMD_def)
708 emsg(_(e_missing_enddef));
709 else if (eap->cmdidx == CMD_block)
710 emsg(_(e_missing_end_block));
711 else
712 emsg(_("E126: Missing :endfunction"));
713 goto theend;
714 }
715
716 // Detect line continuation: SOURCING_LNUM increased more than one.
717 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
718 if (SOURCING_LNUM < sourcing_lnum_off)
719 sourcing_lnum_off -= SOURCING_LNUM;
720 else
721 sourcing_lnum_off = 0;
722
723 if (skip_until != NULL)
724 {
725 // Don't check for ":endfunc"/":enddef" between
726 // * ":append" and "."
727 // * ":python <<EOF" and "EOF"
728 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
729 if (heredoc_trimmed == NULL
730 || (is_heredoc && skipwhite(theline) == theline)
731 || STRNCMP(theline, heredoc_trimmed,
732 STRLEN(heredoc_trimmed)) == 0)
733 {
734 if (heredoc_trimmed == NULL)
735 p = theline;
736 else if (is_heredoc)
737 p = skipwhite(theline) == theline
738 ? theline : theline + STRLEN(heredoc_trimmed);
739 else
740 p = theline + STRLEN(heredoc_trimmed);
741 if (STRCMP(p, skip_until) == 0)
742 {
743 VIM_CLEAR(skip_until);
744 VIM_CLEAR(heredoc_trimmed);
745 getline_options = vim9_function
746 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
747 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200748
749 if (heredoc_concat_len > 0)
750 {
751 // Replace the starting line with all the concatenated
752 // lines.
753 ga_concat(&heredoc_ga, theline);
754 vim_free(((char_u **)(newlines->ga_data))[
755 heredoc_concat_len - 1]);
756 ((char_u **)(newlines->ga_data))[
757 heredoc_concat_len - 1] = heredoc_ga.ga_data;
758 ga_init(&heredoc_ga);
759 heredoc_concat_len = 0;
760 theline += STRLEN(theline); // skip the "EOF"
761 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100762 }
763 }
764 }
765 else
766 {
767 int c;
768
769 // skip ':' and blanks
770 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
771 ;
772
773 // Check for "endfunction", "enddef" or "}".
774 // When a ":" follows it must be a dict key; "enddef: value,"
775 if ((nesting == 0 && eap->cmdidx == CMD_block)
776 ? *p == '}'
777 : (checkforcmd(&p, nesting_def[nesting]
778 ? "enddef" : "endfunction", 4)
779 && *p != ':'))
780 {
781 if (nesting-- == 0)
782 {
783 char_u *nextcmd = NULL;
784
785 if (*p == '|' || *p == '}')
786 nextcmd = p + 1;
787 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
788 nextcmd = line_arg;
789 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100790 && (vim9_function || p_verbose > 0))
791 {
792 if (eap->cmdidx == CMD_def)
793 semsg(_(e_text_found_after_enddef_str), p);
794 else
795 give_warning2((char_u *)
796 _("W22: Text found after :endfunction: %s"),
797 p, TRUE);
798 }
799 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100800 {
801 // Another command follows. If the line came from "eap"
802 // we can simply point into it, otherwise we need to
803 // change "eap->cmdlinep".
804 eap->nextcmd = nextcmd;
805 if (*line_to_free != NULL)
806 {
807 vim_free(*eap->cmdlinep);
808 *eap->cmdlinep = *line_to_free;
809 *line_to_free = NULL;
810 }
811 }
812 break;
813 }
814 }
815
816 // Check for mismatched "endfunc" or "enddef".
817 // We don't check for "def" inside "func" thus we also can't check
818 // for "enddef".
819 // We continue to find the end of the function, although we might
820 // not find it.
821 else if (nesting_def[nesting])
822 {
823 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
824 emsg(_(e_mismatched_endfunction));
825 }
826 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
827 emsg(_(e_mismatched_enddef));
828
829 // Increase indent inside "if", "while", "for" and "try", decrease
830 // at "end".
831 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
832 indent -= 2;
833 else if (STRNCMP(p, "if", 2) == 0
834 || STRNCMP(p, "wh", 2) == 0
835 || STRNCMP(p, "for", 3) == 0
836 || STRNCMP(p, "try", 3) == 0)
837 indent += 2;
838
839 // Check for defining a function inside this function.
840 // Only recognize "def" inside "def", not inside "function",
841 // For backwards compatibility, see Test_function_python().
842 c = *p;
843 if (is_function_cmd(&p)
844 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
845 {
846 if (*p == '!')
847 p = skipwhite(p + 1);
848 p += eval_fname_script(p);
849 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
850 NULL, NULL));
851 if (*skipwhite(p) == '(')
852 {
853 if (nesting == MAX_FUNC_NESTING - 1)
854 emsg(_(e_function_nesting_too_deep));
855 else
856 {
857 ++nesting;
858 nesting_def[nesting] = (c == 'd');
859 indent += 2;
860 }
861 }
862 }
863
864 // Check for ":append", ":change", ":insert". Not for :def.
865 p = skip_range(p, FALSE, NULL);
866 if (!vim9_function
867 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
868 || (p[0] == 'c'
869 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
870 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
871 && (STRNCMP(&p[3], "nge", 3) != 0
872 || !ASCII_ISALPHA(p[6])))))))
873 || (p[0] == 'i'
874 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
875 && (!ASCII_ISALPHA(p[2])
876 || (p[2] == 's'
877 && (!ASCII_ISALPHA(p[3])
878 || p[3] == 'e'))))))))
879 skip_until = vim_strsave((char_u *)".");
880
881 // Check for ":python <<EOF", ":tcl <<EOF", etc.
882 arg = skipwhite(skiptowhite(p));
883 if (arg[0] == '<' && arg[1] =='<'
884 && ((p[0] == 'p' && p[1] == 'y'
885 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
886 || ((p[2] == '3' || p[2] == 'x')
887 && !ASCII_ISALPHA(p[3]))))
888 || (p[0] == 'p' && p[1] == 'e'
889 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
890 || (p[0] == 't' && p[1] == 'c'
891 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
892 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
893 && !ASCII_ISALPHA(p[3]))
894 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
895 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
896 || (p[0] == 'm' && p[1] == 'z'
897 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
898 ))
899 {
900 // ":python <<" continues until a dot, like ":append"
901 p = skipwhite(arg + 2);
902 if (STRNCMP(p, "trim", 4) == 0)
903 {
904 // Ignore leading white space.
905 p = skipwhite(p + 4);
906 heredoc_trimmed = vim_strnsave(theline,
907 skipwhite(theline) - theline);
908 }
909 if (*p == NUL)
910 skip_until = vim_strsave((char_u *)".");
911 else
912 skip_until = vim_strnsave(p, skiptowhite(p) - p);
913 getline_options = GETLINE_NONE;
914 is_heredoc = TRUE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200915 if (eap->cmdidx == CMD_def)
916 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100917 }
918
919 // Check for ":cmd v =<< [trim] EOF"
920 // and ":cmd [a, b] =<< [trim] EOF"
921 // and "lines =<< [trim] EOF" for Vim9
922 // Where "cmd" can be "let", "var", "final" or "const".
923 arg = skipwhite(skiptowhite(p));
924 if (*arg == '[')
925 arg = vim_strchr(arg, ']');
926 if (arg != NULL)
927 {
928 int found = (eap->cmdidx == CMD_def && arg[0] == '='
929 && arg[1] == '<' && arg[2] =='<');
930
931 if (!found)
932 // skip over the argument after "cmd"
933 arg = skipwhite(skiptowhite(arg));
934 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
935 && (checkforcmd(&p, "let", 2)
936 || checkforcmd(&p, "var", 3)
937 || checkforcmd(&p, "final", 5)
938 || checkforcmd(&p, "const", 5))))
939 {
940 p = skipwhite(arg + 3);
941 if (STRNCMP(p, "trim", 4) == 0)
942 {
943 // Ignore leading white space.
944 p = skipwhite(p + 4);
945 heredoc_trimmed = vim_strnsave(theline,
946 skipwhite(theline) - theline);
947 }
948 skip_until = vim_strnsave(p, skiptowhite(p) - p);
949 getline_options = GETLINE_NONE;
950 is_heredoc = TRUE;
951 }
952 }
953 }
954
955 // Add the line to the function.
956 if (ga_grow(newlines, 1 + sourcing_lnum_off) == FAIL)
957 goto theend;
958
Bram Moolenaar20677332021-06-06 17:02:53 +0200959 if (heredoc_concat_len > 0)
960 {
961 // For a :def function "python << EOF" concatenats all the lines,
962 // to be used for the instruction later.
963 ga_concat(&heredoc_ga, theline);
964 ga_concat(&heredoc_ga, (char_u *)"\n");
965 p = vim_strsave((char_u *)"");
966 }
967 else
968 {
969 // Copy the line to newly allocated memory. get_one_sourceline()
970 // allocates 250 bytes per line, this saves 80% on average. The
971 // cost is an extra alloc/free.
972 p = vim_strsave(theline);
973 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100974 if (p == NULL)
975 goto theend;
976 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
977
978 // Add NULL lines for continuation lines, so that the line count is
979 // equal to the index in the growarray.
980 while (sourcing_lnum_off-- > 0)
981 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
982
983 // Check for end of eap->arg.
984 if (line_arg != NULL && *line_arg == NUL)
985 line_arg = NULL;
986 }
987
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200988 // Return OK when no error was detected.
989 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100990 ret = OK;
991
992theend:
993 vim_free(skip_until);
994 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +0200995 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100996 need_wait_return |= saved_wait_return;
997 return ret;
998}
999
1000/*
1001 * Handle the body of a lambda. *arg points to the "{", process statements
1002 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001003 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001004 * When successful "rettv" is set to a funcref.
1005 */
1006 static int
1007lambda_function_body(
1008 char_u **arg,
1009 typval_T *rettv,
1010 evalarg_T *evalarg,
1011 garray_T *newargs,
1012 garray_T *argtypes,
1013 int varargs,
1014 garray_T *default_args,
1015 char_u *ret_type)
1016{
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001017 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001018 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001019 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001020 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001021 exarg_T eap;
1022 garray_T newlines;
1023 char_u *cmdline = NULL;
1024 int ret = FAIL;
1025 char_u *line_to_free = NULL;
1026 partial_T *pt;
1027 char_u *name;
1028 int lnum_save = -1;
1029 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1030
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001031 if (!ends_excmd2(*arg, skipwhite(*arg + 1)))
1032 {
1033 semsg(_(e_trailing_arg), *arg + 1);
1034 return FAIL;
1035 }
1036
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001037 CLEAR_FIELD(eap);
1038 eap.cmdidx = CMD_block;
1039 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001040 eap.cmdlinep = &cmdline;
1041 eap.skip = !evaluate;
1042 if (evalarg->eval_cctx != NULL)
1043 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1044 else
1045 {
1046 eap.getline = evalarg->eval_getline;
1047 eap.cookie = evalarg->eval_cookie;
1048 }
1049
1050 ga_init2(&newlines, (int)sizeof(char_u *), 10);
1051 if (get_function_body(&eap, &newlines, NULL, &line_to_free) == FAIL)
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001052 {
1053 vim_free(cmdline);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001054 goto erret;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001055 }
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001056
1057 // When inside a lambda must add the function lines to evalarg.eval_ga.
1058 evalarg->eval_break_count += newlines.ga_len;
1059 if (gap->ga_itemsize > 0)
1060 {
1061 int idx;
1062 char_u *last;
1063 size_t plen;
1064 char_u *pnl;
1065
1066 for (idx = 0; idx < newlines.ga_len; ++idx)
1067 {
1068 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1069
Bram Moolenaarecb66452021-05-18 15:09:18 +02001070 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001071 goto erret;
1072
1073 // Going to concatenate the lines after parsing. For an empty or
1074 // comment line use an empty string.
1075 // Insert NL characters at the start of each line, the string will
1076 // be split again later in .get_lambda_tv().
1077 if (*p == NUL || vim9_comment_start(p))
1078 p = (char_u *)"";
1079 plen = STRLEN(p);
1080 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1081 if (pnl != NULL)
1082 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001083 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1084 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001085 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001086 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001087 goto erret;
1088 if (cmdline != NULL)
1089 // more is following after the "}", which was skipped
1090 last = cmdline;
1091 else
1092 // nothing is following the "}"
1093 last = (char_u *)"}";
1094 plen = STRLEN(last);
1095 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1096 if (pnl != NULL)
1097 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001098 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1099 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001100 }
1101
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001102 if (cmdline != NULL)
1103 {
1104 // Something comes after the "}".
1105 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001106
1107 // "arg" points into cmdline, need to keep the line and free it later.
1108 vim_free(evalarg->eval_tofree_cmdline);
1109 evalarg->eval_tofree_cmdline = cmdline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001110 }
1111 else
1112 *arg = (char_u *)"";
1113
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001114 if (!evaluate)
1115 {
1116 ret = OK;
1117 goto erret;
1118 }
1119
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001120 name = get_lambda_name();
1121 ufunc = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
1122 if (ufunc == NULL)
1123 goto erret;
1124 set_ufunc_name(ufunc, name);
1125 if (hash_add(&func_hashtab, UF2HIKEY(ufunc)) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001126 goto erret;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001127 ufunc->uf_refcount = 1;
1128 ufunc->uf_args = *newargs;
1129 newargs->ga_data = NULL;
1130 ufunc->uf_def_args = *default_args;
1131 default_args->ga_data = NULL;
1132 ufunc->uf_func_type = &t_func_any;
1133
1134 // error messages are for the first function line
1135 lnum_save = SOURCING_LNUM;
1136 SOURCING_LNUM = sourcing_lnum_top;
1137
1138 // parse argument types
1139 if (parse_argument_types(ufunc, argtypes, varargs) == FAIL)
1140 {
1141 SOURCING_LNUM = lnum_save;
1142 goto erret;
1143 }
1144
1145 // parse the return type, if any
1146 if (parse_return_type(ufunc, ret_type) == FAIL)
1147 goto erret;
1148
1149 pt = ALLOC_CLEAR_ONE(partial_T);
1150 if (pt == NULL)
1151 goto erret;
1152 pt->pt_func = ufunc;
1153 pt->pt_refcount = 1;
1154
1155 ufunc->uf_lines = newlines;
1156 newlines.ga_data = NULL;
1157 if (sandbox)
1158 ufunc->uf_flags |= FC_SANDBOX;
1159 if (!ASCII_ISUPPER(*ufunc->uf_name))
1160 ufunc->uf_flags |= FC_VIM9;
1161 ufunc->uf_script_ctx = current_sctx;
1162 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1163 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1164 set_function_type(ufunc);
1165
1166 rettv->vval.v_partial = pt;
1167 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001168 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001169 ret = OK;
1170
1171erret:
1172 if (lnum_save >= 0)
1173 SOURCING_LNUM = lnum_save;
1174 vim_free(line_to_free);
1175 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001176 if (newargs != NULL)
1177 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001178 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001179 if (ufunc != NULL)
1180 {
1181 func_clear(ufunc, TRUE);
1182 func_free(ufunc, TRUE);
1183 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001184 return ret;
1185}
1186
1187/*
1188 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001189 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001190 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001191 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1192 */
1193 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001194get_lambda_tv(
1195 char_u **arg,
1196 typval_T *rettv,
1197 int types_optional,
1198 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001199{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001200 int evaluate = evalarg != NULL
1201 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001202 garray_T newargs;
1203 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001204 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001205 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001206 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001207 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001208 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001209 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001210 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001211 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001212 char_u *s;
1213 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001214 int *old_eval_lavars = eval_lavars_used;
1215 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001216 char_u *tofree1 = NULL;
1217 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001218 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001219 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001220 int called_emsg_start = called_emsg;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001221
1222 if (equal_arrow && !in_vim9script())
1223 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001224
1225 ga_init(&newargs);
1226 ga_init(&newlines);
1227
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001228 // First, check if this is really a lambda expression. "->" or "=>" must
1229 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001230 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001231 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001232 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar14ded112021-06-26 19:25:49 +02001233 NULL, &default_args, TRUE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001234 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001235 {
1236 if (types_optional)
1237 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +01001238 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001239 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001240
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001241 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001242 if (evaluate)
1243 pnewargs = &newargs;
1244 else
1245 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001246 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001247 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001248 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001249 &varargs, &default_args,
1250 FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001251 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001252 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaare462f522020-12-27 14:43:30 +01001253 equal_arrow || in_vim9script() ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001254 {
1255 if (types_optional)
1256 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001257 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001258 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001259 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001260 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001261
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001262 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1263 if (ret_type != NULL)
1264 {
1265 ret_type = vim_strsave(ret_type);
1266 tofree2 = ret_type;
1267 }
1268
Bram Moolenaare38eab22019-12-05 21:50:01 +01001269 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001270 if (evaluate)
1271 eval_lavars_used = &eval_lavars;
1272
Bram Moolenaar65c44152020-12-24 15:14:01 +01001273 *arg = skipwhite_and_linebreak(*arg, evalarg);
1274
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001275 // Recognize "{" as the start of a function body.
1276 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001277 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001278 if (evalarg == NULL)
1279 // cannot happen?
1280 goto theend;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001281 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1282 types_optional ? &argtypes : NULL, varargs,
1283 &default_args, ret_type) == FAIL)
1284 goto errret;
1285 goto theend;
1286 }
1287 if (default_args.ga_len > 0)
1288 {
1289 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001290 goto errret;
1291 }
1292
Bram Moolenaare38eab22019-12-05 21:50:01 +01001293 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001294 start = *arg;
1295 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001296 if (ret == FAIL)
1297 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001298 if (evalarg != NULL)
1299 {
1300 // avoid that the expression gets freed when another line break follows
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001301 tofree1 = evalarg->eval_tofree;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001302 evalarg->eval_tofree = NULL;
1303 }
1304
Bram Moolenaar65c44152020-12-24 15:14:01 +01001305 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001306 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001307 *arg = skipwhite_and_linebreak(*arg, evalarg);
1308 if (**arg != '}')
1309 {
1310 semsg(_("E451: Expected }: %s"), *arg);
1311 goto errret;
1312 }
1313 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001314 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001315
1316 if (evaluate)
1317 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001318 int len;
1319 int flags = 0;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001320 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001321 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001322 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001323
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001324 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001325 if (fp == NULL)
1326 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001327 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001328 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001329 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001330 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001331
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001332 ga_init2(&newlines, (int)sizeof(char_u *), 1);
1333 if (ga_grow(&newlines, 1) == FAIL)
1334 goto errret;
1335
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001336 // If there are line breaks, we need to split up the string.
1337 line_end = vim_strchr(start, '\n');
1338 if (line_end == NULL)
1339 line_end = end;
1340
1341 // Add "return " before the expression (or the first line).
1342 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001343 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001344 if (p == NULL)
1345 goto errret;
1346 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1347 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001348 vim_strncpy(p + 7, start, line_end - start);
1349
1350 if (line_end != end)
1351 {
1352 // Add more lines, split by line breaks. Thus is used when a
1353 // lambda with { cmds } is encountered.
1354 while (*line_end == '\n')
1355 {
1356 if (ga_grow(&newlines, 1) == FAIL)
1357 goto errret;
1358 start = line_end + 1;
1359 line_end = vim_strchr(start, '\n');
1360 if (line_end == NULL)
1361 line_end = end;
1362 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1363 vim_strnsave(start, line_end - start);
1364 }
1365 }
1366
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001367 if (strstr((char *)p + 7, "a:") == NULL)
1368 // No a: variables are used for sure.
1369 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001370
1371 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001372 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001373 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001374 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001375 if (types_optional)
1376 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001377 if (parse_argument_types(fp, &argtypes,
1378 in_vim9script() && varargs) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001379 goto errret;
1380 if (ret_type != NULL)
1381 {
1382 fp->uf_ret_type = parse_type(&ret_type,
1383 &fp->uf_type_list, TRUE);
1384 if (fp->uf_ret_type == NULL)
1385 goto errret;
1386 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001387 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001388 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001389 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001390
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001391 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001392 if (current_funccal != NULL && eval_lavars)
1393 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001394 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001395 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001396 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001397 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001398
1399#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001400 if (prof_def_func())
1401 func_do_profile(fp);
1402#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001403 if (sandbox)
1404 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001405 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001406 // uf_args.ga_len. In Vim9 script "...name" has to be used.
1407 fp->uf_varargs = !in_vim9script() || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001408 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001409 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001410 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001411 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001412
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001413 pt->pt_func = fp;
1414 pt->pt_refcount = 1;
1415 rettv->vval.v_partial = pt;
1416 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001417
1418 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001419 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001420
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001421theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001422 eval_lavars_used = old_eval_lavars;
Bram Moolenaarefaaaa62020-07-08 22:24:09 +02001423 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001424 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001425 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001426 vim_free(tofree1);
1427 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001428 if (types_optional)
1429 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001430 return OK;
1431
1432errret:
1433 ga_clear_strings(&newargs);
1434 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001435 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001436 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001437 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001438 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001439 if (fp != NULL)
1440 vim_free(fp->uf_arg_types);
1441 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001442 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001443 vim_free(pt);
Bram Moolenaarefaaaa62020-07-08 22:24:09 +02001444 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001445 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001446 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001447 vim_free(tofree1);
1448 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001449 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001450 return FAIL;
1451}
1452
1453/*
1454 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1455 * name it contains, otherwise return "name".
1456 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1457 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001458 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1459 * type of the variable.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001460 */
1461 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001462deref_func_name(
1463 char_u *name,
1464 int *lenp,
1465 partial_T **partialp,
1466 type_T **type,
1467 int no_autoload)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001468{
1469 dictitem_T *v;
1470 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001471 char_u *s = NULL;
1472 hashtab_T *ht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001473
1474 if (partialp != NULL)
1475 *partialp = NULL;
1476
1477 cc = name[*lenp];
1478 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001479
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001480 v = find_var(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001481 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001482 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001483 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001484 if (v->di_tv.v_type == VAR_FUNC)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001485 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001486 if (v->di_tv.vval.v_string == NULL)
1487 {
1488 *lenp = 0;
1489 return (char_u *)""; // just in case
1490 }
1491 s = v->di_tv.vval.v_string;
1492 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001493 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001494
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001495 if (v->di_tv.v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001496 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001497 partial_T *pt = v->di_tv.vval.v_partial;
1498
1499 if (pt == NULL)
1500 {
1501 *lenp = 0;
1502 return (char_u *)""; // just in case
1503 }
1504 if (partialp != NULL)
1505 *partialp = pt;
1506 s = partial_name(pt);
1507 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001508 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001509
1510 if (s != NULL)
1511 {
1512 if (type != NULL && ht == get_script_local_ht())
1513 {
1514 svar_T *sv = find_typval_in_script(&v->di_tv);
1515
1516 if (sv != NULL)
1517 *type = sv->sv_type;
1518 }
1519 return s;
1520 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001521 }
1522
1523 return name;
1524}
1525
1526/*
1527 * Give an error message with a function name. Handle <SNR> things.
1528 * "ermsg" is to be passed without translation, use N_() instead of _().
1529 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001530 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001531emsg_funcname(char *ermsg, char_u *name)
1532{
1533 char_u *p;
1534
1535 if (*name == K_SPECIAL)
1536 p = concat_str((char_u *)"<SNR>", name + 3);
1537 else
1538 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001539 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001540 if (p != name)
1541 vim_free(p);
1542}
1543
1544/*
1545 * Allocate a variable for the result of a function.
1546 * Return OK or FAIL.
1547 */
1548 int
1549get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001550 char_u *name, // name of the function
1551 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001552 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001553 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +02001554 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001555 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001556{
1557 char_u *argp;
1558 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001559 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1560 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001561 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001562
1563 /*
1564 * Get the arguments.
1565 */
1566 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001567 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
1568 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001569 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001570 // skip the '(' or ',' and possibly line breaks
1571 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1572
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001573 if (*argp == ')' || *argp == ',' || *argp == NUL)
1574 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001575 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001576 {
1577 ret = FAIL;
1578 break;
1579 }
1580 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001581 // The comma should come right after the argument, but this wasn't
1582 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001583 if (vim9script)
1584 {
1585 if (*argp != ',' && *skipwhite(argp) == ',')
1586 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001587 semsg(_(e_no_white_space_allowed_before_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001588 ret = FAIL;
1589 break;
1590 }
1591 }
1592 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001593 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001594 if (*argp != ',')
1595 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001596 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1597 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001598 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001599 ret = FAIL;
1600 break;
1601 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001602 }
Bram Moolenaare6b53242020-07-01 17:28:33 +02001603 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001604 if (*argp == ')')
1605 ++argp;
1606 else
1607 ret = FAIL;
1608
1609 if (ret == OK)
1610 {
1611 int i = 0;
1612
1613 if (get_vim_var_nr(VV_TESTING))
1614 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001615 // Prepare for calling test_garbagecollect_now(), need to know
1616 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001617 if (funcargs.ga_itemsize == 0)
1618 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
1619 for (i = 0; i < argcount; ++i)
1620 if (ga_grow(&funcargs, 1) == OK)
1621 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1622 &argvars[i];
1623 }
1624
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001625 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001626
1627 funcargs.ga_len -= i;
1628 }
1629 else if (!aborting())
1630 {
1631 if (argcount == MAX_FUNC_ARGS)
1632 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
1633 else
1634 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
1635 }
1636
1637 while (--argcount >= 0)
1638 clear_tv(&argvars[argcount]);
1639
Bram Moolenaar8294d492020-08-10 22:40:56 +02001640 if (in_vim9script())
1641 *arg = argp;
1642 else
1643 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001644 return ret;
1645}
1646
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001647/*
1648 * Return TRUE if "p" starts with "<SID>" or "s:".
1649 * Only works if eval_fname_script() returned non-zero for "p"!
1650 */
1651 static int
1652eval_fname_sid(char_u *p)
1653{
1654 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1655}
1656
1657/*
1658 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1659 * Change <SNR>123_name() to K_SNR 123_name().
1660 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
1661 * (slow).
1662 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001663 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001664fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
1665{
1666 int llen;
1667 char_u *fname;
1668 int i;
1669
1670 llen = eval_fname_script(name);
1671 if (llen > 0)
1672 {
1673 fname_buf[0] = K_SPECIAL;
1674 fname_buf[1] = KS_EXTRA;
1675 fname_buf[2] = (int)KE_SNR;
1676 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001677 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001678 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001679 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001680 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001681 else
1682 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001683 sprintf((char *)fname_buf + 3, "%ld_",
1684 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001685 i = (int)STRLEN(fname_buf);
1686 }
1687 }
1688 if (i + STRLEN(name + llen) < FLEN_FIXED)
1689 {
1690 STRCPY(fname_buf + i, name + llen);
1691 fname = fname_buf;
1692 }
1693 else
1694 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001695 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001696 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +01001697 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001698 else
1699 {
1700 *tofree = fname;
1701 mch_memmove(fname, fname_buf, (size_t)i);
1702 STRCPY(fname + i, name + llen);
1703 }
1704 }
1705 }
1706 else
1707 fname = name;
1708 return fname;
1709}
1710
1711/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 * Find a function "name" in script "sid".
1713 */
1714 static ufunc_T *
1715find_func_with_sid(char_u *name, int sid)
1716{
1717 hashitem_T *hi;
1718 char_u buffer[200];
1719
1720 buffer[0] = K_SPECIAL;
1721 buffer[1] = KS_EXTRA;
1722 buffer[2] = (int)KE_SNR;
1723 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
1724 (long)sid, name);
1725 hi = hash_find(&func_hashtab, buffer);
1726 if (!HASHITEM_EMPTY(hi))
1727 return HI2UF(hi);
1728
1729 return NULL;
1730}
1731
1732/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001733 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001734 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001735 * Return NULL for unknown function.
1736 */
Bram Moolenaarce658352020-07-31 23:47:12 +02001737 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001738find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001739{
1740 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 ufunc_T *func;
1742 imported_T *imported;
1743
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001744 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 {
Bram Moolenaar333894b2020-08-01 18:53:07 +02001746 char_u *after_script = NULL;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001747 long sid = 0;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001748 int find_script_local = in_vim9script()
1749 && eval_isnamec1(*name) && name[1] != ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750
Bram Moolenaar95006e32020-08-29 17:47:08 +02001751 if (find_script_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001753 // Find script-local function before global one.
1754 func = find_func_with_sid(name, current_sctx.sc_sid);
1755 if (func != NULL)
1756 return func;
1757 }
1758
Bram Moolenaarefa94442020-08-08 22:16:00 +02001759 if (name[0] == K_SPECIAL
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001760 && name[1] == KS_EXTRA
1761 && name[2] == KE_SNR)
1762 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001763 // Caller changes s: to <SNR>99_name.
1764
1765 after_script = name + 3;
1766 sid = getdigits(&after_script);
Bram Moolenaarefa94442020-08-08 22:16:00 +02001767 if (*after_script == '_')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001768 ++after_script;
1769 else
1770 after_script = NULL;
1771 }
Bram Moolenaar95006e32020-08-29 17:47:08 +02001772 if (find_script_local || after_script != NULL)
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001773 {
1774 // Find imported function before global one.
Bram Moolenaarefa94442020-08-08 22:16:00 +02001775 if (after_script != NULL && sid != current_sctx.sc_sid)
1776 imported = find_imported_in_script(after_script, 0, sid);
1777 else
1778 imported = find_imported(after_script == NULL
1779 ? name : after_script, 0, cctx);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001780 if (imported != NULL && imported->imp_funcname != NULL)
1781 {
1782 hi = hash_find(&func_hashtab, imported->imp_funcname);
1783 if (!HASHITEM_EMPTY(hi))
1784 return HI2UF(hi);
1785 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 }
1787 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788
Bram Moolenaara26b9702020-04-18 19:53:28 +02001789 hi = hash_find(&func_hashtab,
1790 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001791 if (!HASHITEM_EMPTY(hi))
1792 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793
1794 return NULL;
1795}
1796
1797/*
1798 * Find a function by name, return pointer to it in ufuncs.
1799 * "cctx" is passed in a :def function to find imported functions.
1800 * Return NULL for unknown or dead function.
1801 */
1802 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001803find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001805 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806
1807 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
1808 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001809 return NULL;
1810}
1811
1812/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02001813 * Return TRUE if "ufunc" is a global function.
1814 */
1815 int
1816func_is_global(ufunc_T *ufunc)
1817{
1818 return ufunc->uf_name[0] != K_SPECIAL;
1819}
1820
1821/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001822 * Copy the function name of "fp" to buffer "buf".
1823 * "buf" must be able to hold the function name plus three bytes.
1824 * Takes care of script-local function names.
1825 */
1826 static void
1827cat_func_name(char_u *buf, ufunc_T *fp)
1828{
Bram Moolenaar0f769812020-09-12 18:32:34 +02001829 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001830 {
1831 STRCPY(buf, "<SNR>");
1832 STRCAT(buf, fp->uf_name + 3);
1833 }
1834 else
1835 STRCPY(buf, fp->uf_name);
1836}
1837
1838/*
1839 * Add a number variable "name" to dict "dp" with value "nr".
1840 */
1841 static void
1842add_nr_var(
1843 dict_T *dp,
1844 dictitem_T *v,
1845 char *name,
1846 varnumber_T nr)
1847{
1848 STRCPY(v->di_key, name);
1849 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1850 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
1851 v->di_tv.v_type = VAR_NUMBER;
1852 v->di_tv.v_lock = VAR_FIXED;
1853 v->di_tv.vval.v_number = nr;
1854}
1855
1856/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001857 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001858 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001859 static void
1860free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001861{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001862 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001863
1864 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1865 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001866 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001867
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001868 // When garbage collecting a funccall_T may be freed before the
1869 // function that references it, clear its uf_scoped field.
1870 // The function may have been redefined and point to another
1871 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001872 if (fp != NULL && fp->uf_scoped == fc)
1873 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001874 }
Bram Moolenaar58016442016-07-31 18:30:22 +02001875 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001876
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001877 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001878 vim_free(fc);
1879}
1880
1881/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001882 * Free "fc" and what it contains.
1883 * Can be called only when "fc" is kept beyond the period of it called,
1884 * i.e. after cleanup_function_call(fc).
1885 */
1886 static void
1887free_funccal_contents(funccall_T *fc)
1888{
1889 listitem_T *li;
1890
1891 // Free all l: variables.
1892 vars_clear(&fc->l_vars.dv_hashtab);
1893
1894 // Free all a: variables.
1895 vars_clear(&fc->l_avars.dv_hashtab);
1896
1897 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001898 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001899 clear_tv(&li->li_tv);
1900
1901 free_funccal(fc);
1902}
1903
1904/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02001905 * Handle the last part of returning from a function: free the local hashtable.
1906 * Unless it is still in use by a closure.
1907 */
1908 static void
1909cleanup_function_call(funccall_T *fc)
1910{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001911 int may_free_fc = fc->fc_refcount <= 0;
1912 int free_fc = TRUE;
1913
Bram Moolenaar6914c642017-04-01 21:21:30 +02001914 current_funccal = fc->caller;
1915
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001916 // Free all l: variables if not referred.
1917 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
1918 vars_clear(&fc->l_vars.dv_hashtab);
1919 else
1920 free_fc = FALSE;
1921
1922 // If the a:000 list and the l: and a: dicts are not referenced and
1923 // there is no closure using it, we can free the funccall_T and what's
1924 // in it.
1925 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
1926 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001927 else
1928 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001929 int todo;
1930 hashitem_T *hi;
1931 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001932
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001933 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001934
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001935 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001936 todo = (int)fc->l_avars.dv_hashtab.ht_used;
1937 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
1938 {
1939 if (!HASHITEM_EMPTY(hi))
1940 {
1941 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001942 di = HI2DI(hi);
1943 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001944 }
1945 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001946 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001947
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001948 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
1949 fc->l_varlist.lv_first = NULL;
1950 else
1951 {
1952 listitem_T *li;
1953
1954 free_fc = FALSE;
1955
1956 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001957 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02001958 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001959 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001960
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001961 if (free_fc)
1962 free_funccal(fc);
1963 else
1964 {
1965 static int made_copy = 0;
1966
1967 // "fc" is still in use. This can happen when returning "a:000",
1968 // assigning "l:" to a global variable or defining a closure.
1969 // Link "fc" in the list for garbage collection later.
1970 fc->caller = previous_funccal;
1971 previous_funccal = fc;
1972
1973 if (want_garbage_collect)
1974 // If garbage collector is ready, clear count.
1975 made_copy = 0;
1976 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001977 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001978 // We have made a lot of copies, worth 4 Mbyte. This can happen
1979 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001980 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001981 // too much memory.
1982 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001983 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001984 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001985 }
1986}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001987
1988/*
1989 * There are two kinds of function names:
1990 * 1. ordinary names, function defined with :function or :def
1991 * 2. numbered functions and lambdas
1992 * For the first we only count the name stored in func_hashtab as a reference,
1993 * using function() does not count as a reference, because the function is
1994 * looked up by name.
1995 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001996 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001997func_name_refcount(char_u *name)
1998{
1999 return isdigit(*name) || *name == '<';
2000}
2001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002/*
2003 * Unreference "fc": decrement the reference count and free it when it
2004 * becomes zero. "fp" is detached from "fc".
2005 * When "force" is TRUE we are exiting.
2006 */
2007 static void
2008funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2009{
2010 funccall_T **pfc;
2011 int i;
2012
2013 if (fc == NULL)
2014 return;
2015
2016 if (--fc->fc_refcount <= 0 && (force || (
2017 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
2018 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
2019 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2020 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
2021 {
2022 if (fc == *pfc)
2023 {
2024 *pfc = fc->caller;
2025 free_funccal_contents(fc);
2026 return;
2027 }
2028 }
2029 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2030 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
2031 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
2032}
2033
2034/*
2035 * Remove the function from the function hashtable. If the function was
2036 * deleted while it still has references this was already done.
2037 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2038 */
2039 static int
2040func_remove(ufunc_T *fp)
2041{
2042 hashitem_T *hi;
2043
2044 // Return if it was already virtually deleted.
2045 if (fp->uf_flags & FC_DEAD)
2046 return FALSE;
2047
2048 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
2049 if (!HASHITEM_EMPTY(hi))
2050 {
2051 // When there is a def-function index do not actually remove the
2052 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002053 // Do remove it when it's a copy.
2054 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaarc970e422021-03-17 15:03:04 +01002055 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002057 return FALSE;
2058 }
2059 hash_remove(&func_hashtab, hi);
2060 fp->uf_flags |= FC_DELETED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002061 return TRUE;
2062 }
2063 return FALSE;
2064}
2065
2066 static void
2067func_clear_items(ufunc_T *fp)
2068{
2069 ga_clear_strings(&(fp->uf_args));
2070 ga_clear_strings(&(fp->uf_def_args));
2071 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002073 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002074 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002075 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002076
2077 // Increment the refcount of this function to avoid it being freed
2078 // recursively when the partial is freed.
2079 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002080 partial_unref(fp->uf_partial);
2081 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002082 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002083
2084#ifdef FEAT_LUA
2085 if (fp->uf_cb_free != NULL)
2086 {
2087 fp->uf_cb_free(fp->uf_cb_state);
2088 fp->uf_cb_free = NULL;
2089 }
2090
2091 fp->uf_cb_state = NULL;
2092 fp->uf_cb = NULL;
2093#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094#ifdef FEAT_PROFILE
2095 VIM_CLEAR(fp->uf_tml_count);
2096 VIM_CLEAR(fp->uf_tml_total);
2097 VIM_CLEAR(fp->uf_tml_self);
2098#endif
2099}
2100
2101/*
2102 * Free all things that a function contains. Does not free the function
2103 * itself, use func_free() for that.
2104 * When "force" is TRUE we are exiting.
2105 */
2106 static void
2107func_clear(ufunc_T *fp, int force)
2108{
2109 if (fp->uf_cleared)
2110 return;
2111 fp->uf_cleared = TRUE;
2112
2113 // clear this function
2114 func_clear_items(fp);
2115 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002116 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117}
2118
2119/*
2120 * Free a function and remove it from the list of functions. Does not free
2121 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002122 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002123 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002125 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002126func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127{
2128 // Only remove it when not done already, otherwise we would remove a newer
2129 // version of the function with the same name.
2130 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2131 func_remove(fp);
2132
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002133 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002134 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002135 if (fp->uf_dfunc_idx > 0)
2136 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002137 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002139 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002140 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002141 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142}
2143
2144/*
2145 * Free all things that a function contains and free the function itself.
2146 * When "force" is TRUE we are exiting.
2147 */
2148 static void
2149func_clear_free(ufunc_T *fp, int force)
2150{
2151 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002152 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2153 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002154 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002155 else
2156 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157}
2158
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002159/*
2160 * Copy already defined function "lambda" to a new function with name "global".
2161 * This is for when a compiled function defines a global function.
2162 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002163 int
2164copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002165{
2166 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002167 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002168
2169 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002170 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002171 semsg(_(e_lambda_function_not_found_str), lambda);
2172 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002173 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002174
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002175 fp = find_func(global, TRUE, NULL);
2176 if (fp != NULL)
2177 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002178 // TODO: handle ! to overwrite
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002179 semsg(_(e_funcexts), global);
2180 return FAIL;
2181 }
2182
2183 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
2184 if (fp == NULL)
2185 return FAIL;
2186
2187 fp->uf_varargs = ufunc->uf_varargs;
2188 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2189 fp->uf_def_status = ufunc->uf_def_status;
2190 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2191 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2192 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2193 == FAIL
2194 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2195 goto failed;
2196
2197 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
2198 : vim_strsave(ufunc->uf_name_exp);
2199 if (ufunc->uf_arg_types != NULL)
2200 {
2201 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2202 if (fp->uf_arg_types == NULL)
2203 goto failed;
2204 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2205 sizeof(type_T *) * fp->uf_args.ga_len);
2206 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002207 if (ufunc->uf_va_name != NULL)
2208 {
2209 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2210 if (fp->uf_va_name == NULL)
2211 goto failed;
2212 }
2213 fp->uf_ret_type = ufunc->uf_ret_type;
2214
2215 fp->uf_refcount = 1;
2216 STRCPY(fp->uf_name, global);
2217 hash_add(&func_hashtab, UF2HIKEY(fp));
2218
2219 // the referenced dfunc_T is now used one more time
2220 link_def_function(fp);
2221
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002222 // Create a partial to store the context of the function where it was
2223 // instantiated. Only needs to be done once. Do this on the original
2224 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002225 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2226 {
2227 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2228
2229 if (pt == NULL)
2230 goto failed;
2231 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002232 {
2233 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002234 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002235 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002236 ufunc->uf_partial = pt;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002237 --pt->pt_refcount; // not actually referenced here
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002238 }
2239
2240 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002241
2242failed:
2243 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002244 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002245}
2246
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002247static int funcdepth = 0;
2248
2249/*
2250 * Increment the function call depth count.
2251 * Return FAIL when going over 'maxfuncdepth'.
2252 * Otherwise return OK, must call funcdepth_decrement() later!
2253 */
2254 int
2255funcdepth_increment(void)
2256{
2257 if (funcdepth >= p_mfd)
2258 {
2259 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
2260 return FAIL;
2261 }
2262 ++funcdepth;
2263 return OK;
2264}
2265
2266 void
2267funcdepth_decrement(void)
2268{
2269 --funcdepth;
2270}
2271
2272/*
2273 * Get the current function call depth.
2274 */
2275 int
2276funcdepth_get(void)
2277{
2278 return funcdepth;
2279}
2280
2281/*
2282 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002283 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002284 * times as funcdepth_increment().
2285 */
2286 void
2287funcdepth_restore(int depth)
2288{
2289 funcdepth = depth;
2290}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002291
2292/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002293 * Call a user function.
2294 */
2295 static void
2296call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002297 ufunc_T *fp, // pointer to function
2298 int argcount, // nr of args
2299 typval_T *argvars, // arguments
2300 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002301 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002302 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002303{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002304 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002305 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002306 funccall_T *fc;
2307 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002308 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002309 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002310 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002311 int i;
2312 int ai;
2313 int islambda = FALSE;
2314 char_u numbuf[NUMBUFLEN];
2315 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002316 typval_T *tv_to_free[MAX_FUNC_ARGS];
2317 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002318#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002319 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002320#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002321 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002322
Bram Moolenaarb2049902021-01-24 12:53:53 +01002323#ifdef FEAT_PROFILE
2324 CLEAR_FIELD(profile_info);
2325#endif
2326
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002327 // If depth of calling is getting too high, don't execute the function.
2328 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002329 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002330 rettv->v_type = VAR_NUMBER;
2331 rettv->vval.v_number = -1;
2332 return;
2333 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002334
Bram Moolenaare38eab22019-12-05 21:50:01 +01002335 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002336
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002337 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002338 if (fc == NULL)
2339 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002340 fc->caller = current_funccal;
2341 current_funccal = fc;
2342 fc->func = fp;
2343 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002344 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002345 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002346 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2347 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002348 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002349 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002350 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002351
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002352 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002354#ifdef FEAT_PROFILE
2355 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2356#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002357 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002358#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002359 if (do_profiling == PROF_YES)
2360 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002361#endif
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002362 call_def_function(fp, argcount, argvars, funcexe->partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002363 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002364#ifdef FEAT_PROFILE
2365 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002366 || (caller != NULL && caller->uf_profiling)))
2367 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002368#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 free_funccal(fc);
2371 return;
2372 }
2373
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002374 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
2375 islambda = TRUE;
2376
2377 /*
2378 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
2379 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
2380 * each argument variable and saves a lot of time.
2381 */
2382 /*
2383 * Init l: variables.
2384 */
2385 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
2386 if (selfdict != NULL)
2387 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002388 // Set l:self to "selfdict". Use "name" to avoid a warning from
2389 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002390 v = &fc->fixvar[fixvar_idx++].var;
2391 name = v->di_key;
2392 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002393 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002394 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
2395 v->di_tv.v_type = VAR_DICT;
2396 v->di_tv.v_lock = 0;
2397 v->di_tv.vval.v_dict = selfdict;
2398 ++selfdict->dv_refcount;
2399 }
2400
2401 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002402 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002403 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002404 * Set a:000 to a list with room for the "..." arguments.
2405 */
2406 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002407 if ((fp->uf_flags & FC_NOARGS) == 0)
2408 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002409 (varnumber_T)(argcount >= fp->uf_args.ga_len
2410 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01002411 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002412 if ((fp->uf_flags & FC_NOARGS) == 0)
2413 {
2414 // Use "name" to avoid a warning from some compiler that checks the
2415 // destination size.
2416 v = &fc->fixvar[fixvar_idx++].var;
2417 name = v->di_key;
2418 STRCPY(name, "000");
2419 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2420 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
2421 v->di_tv.v_type = VAR_LIST;
2422 v->di_tv.v_lock = VAR_FIXED;
2423 v->di_tv.vval.v_list = &fc->l_varlist;
2424 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02002425 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002426 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2427 fc->l_varlist.lv_lock = VAR_FIXED;
2428
2429 /*
2430 * Set a:firstline to "firstline" and a:lastline to "lastline".
2431 * Set a:name to named arguments.
2432 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002433 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002434 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002435 if ((fp->uf_flags & FC_NOARGS) == 0)
2436 {
2437 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002438 (varnumber_T)funcexe->firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002439 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002440 (varnumber_T)funcexe->lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002441 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002442 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002443 {
2444 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002445 typval_T def_rettv;
2446 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002447
2448 ai = i - fp->uf_args.ga_len;
2449 if (ai < 0)
2450 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002451 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002452 name = FUNCARG(fp, i);
2453 if (islambda)
2454 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002455
2456 // evaluate named argument default expression
2457 isdefault = ai + fp->uf_def_args.ga_len >= 0
2458 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2459 && argvars[i].vval.v_number == VVAL_NONE));
2460 if (isdefault)
2461 {
2462 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002463
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002464 def_rettv.v_type = VAR_NUMBER;
2465 def_rettv.vval.v_number = -1;
2466
2467 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2468 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002469 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002470 {
2471 default_arg_err = 1;
2472 break;
2473 }
2474 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002475 }
2476 else
2477 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002478 if ((fp->uf_flags & FC_NOARGS) != 0)
2479 // Bail out if no a: arguments used (in lambda).
2480 break;
2481
Bram Moolenaare38eab22019-12-05 21:50:01 +01002482 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002483 sprintf((char *)numbuf, "%d", ai + 1);
2484 name = numbuf;
2485 }
2486 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2487 {
2488 v = &fc->fixvar[fixvar_idx++].var;
2489 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002490 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002491 }
2492 else
2493 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002494 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002495 if (v == NULL)
2496 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002497 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002498 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002499
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002500 // Note: the values are copied directly to avoid alloc/free.
2501 // "argvars" must have VAR_FIXED for v_lock.
2502 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002503 v->di_tv.v_lock = VAR_FIXED;
2504
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002505 if (isdefault)
2506 // Need to free this later, no matter where it's stored.
2507 tv_to_free[tv_to_free_len++] = &v->di_tv;
2508
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002509 if (addlocal)
2510 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002511 // Named arguments should be accessed without the "a:" prefix in
2512 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002513 copy_tv(&v->di_tv, &v->di_tv);
2514 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002515 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002516 else
2517 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002518
2519 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2520 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002521 listitem_T *li = &fc->l_listitems[ai];
2522
2523 li->li_tv = argvars[i];
2524 li->li_tv.v_lock = VAR_FIXED;
2525 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002526 }
2527 }
2528
Bram Moolenaare38eab22019-12-05 21:50:01 +01002529 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002530 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002531
2532 if (fp->uf_flags & FC_SANDBOX)
2533 {
2534 using_sandbox = TRUE;
2535 ++sandbox;
2536 }
2537
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002538 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002539 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002540 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002541 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002542 ++no_wait_return;
2543 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002544
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002545 smsg(_("calling %s"), SOURCING_NAME);
2546 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002547 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002548 char_u buf[MSG_BUF_LEN];
2549 char_u numbuf2[NUMBUFLEN];
2550 char_u *tofree;
2551 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002552
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002553 msg_puts("(");
2554 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002555 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002556 if (i > 0)
2557 msg_puts(", ");
2558 if (argvars[i].v_type == VAR_NUMBER)
2559 msg_outnum((long)argvars[i].vval.v_number);
2560 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002561 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002562 // Do not want errors such as E724 here.
2563 ++emsg_off;
2564 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2565 --emsg_off;
2566 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002567 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002568 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002569 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002570 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2571 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002572 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002573 msg_puts((char *)s);
2574 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002575 }
2576 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002577 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002578 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002579 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002580 msg_puts("\n"); // don't overwrite this either
2581
2582 verbose_leave_scroll();
2583 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002584 }
2585#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002586 if (do_profiling == PROF_YES)
2587 profile_may_start_func(&profile_info, fp,
2588 fc->caller == NULL ? NULL : fc->caller->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002589#endif
2590
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002591 save_current_sctx = current_sctx;
2592 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002593 save_did_emsg = did_emsg;
2594 did_emsg = FALSE;
2595
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002596 if (default_arg_err && (fp->uf_flags & FC_ABORT))
2597 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002598 else if (islambda)
2599 {
2600 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
2601
2602 // A Lambda always has the command "return {expr}". It is much faster
2603 // to evaluate {expr} directly.
2604 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002605 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002606 --ex_nesting_level;
2607 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002608 else
2609 // call do_cmdline() to execute the lines
2610 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002611 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
2612
2613 --RedrawingDisabled;
2614
Bram Moolenaare38eab22019-12-05 21:50:01 +01002615 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002616 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
2617 {
2618 clear_tv(rettv);
2619 rettv->v_type = VAR_NUMBER;
2620 rettv->vval.v_number = -1;
2621 }
2622
2623#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002624 if (do_profiling == PROF_YES)
2625 {
2626 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2627
2628 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
2629 profile_may_end_func(&profile_info, fp, caller);
2630 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002631#endif
2632
Bram Moolenaare38eab22019-12-05 21:50:01 +01002633 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002634 if (p_verbose >= 12)
2635 {
2636 ++no_wait_return;
2637 verbose_enter_scroll();
2638
2639 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002640 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002641 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002642 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002643 (long)fc->rettv->vval.v_number);
2644 else
2645 {
2646 char_u buf[MSG_BUF_LEN];
2647 char_u numbuf2[NUMBUFLEN];
2648 char_u *tofree;
2649 char_u *s;
2650
Bram Moolenaare38eab22019-12-05 21:50:01 +01002651 // The value may be very long. Skip the middle part, so that we
2652 // have some idea how it starts and ends. smsg() would always
2653 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002654 ++emsg_off;
2655 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
2656 --emsg_off;
2657 if (s != NULL)
2658 {
2659 if (vim_strsize(s) > MSG_BUF_CLEN)
2660 {
2661 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2662 s = buf;
2663 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002664 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002665 vim_free(tofree);
2666 }
2667 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002668 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002669
2670 verbose_leave_scroll();
2671 --no_wait_return;
2672 }
2673
Bram Moolenaare31ee862020-01-07 20:59:34 +01002674 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002675 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002676 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002677#ifdef FEAT_PROFILE
2678 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002679 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002680#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02002681 if (using_sandbox)
2682 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002683
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002684 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002685 {
2686 ++no_wait_return;
2687 verbose_enter_scroll();
2688
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002689 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002690 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002691
2692 verbose_leave_scroll();
2693 --no_wait_return;
2694 }
2695
2696 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002697 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002698 for (i = 0; i < tv_to_free_len; ++i)
2699 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002700 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002701}
2702
2703/*
Bram Moolenaar50824712020-12-20 21:10:17 +01002704 * Check the argument count for user function "fp".
2705 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
2706 */
2707 int
2708check_user_func_argcount(ufunc_T *fp, int argcount)
2709{
2710 int regular_args = fp->uf_args.ga_len;
2711
2712 if (argcount < regular_args - fp->uf_def_args.ga_len)
2713 return FCERR_TOOFEW;
2714 else if (!has_varargs(fp) && argcount > regular_args)
2715 return FCERR_TOOMANY;
2716 return FCERR_UNKNOWN;
2717}
2718
2719/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002721 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722 int
2723call_user_func_check(
2724 ufunc_T *fp,
2725 int argcount,
2726 typval_T *argvars,
2727 typval_T *rettv,
2728 funcexe_T *funcexe,
2729 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002730{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002732
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
2734 *funcexe->doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01002735 error = check_user_func_argcount(fp, argcount);
2736 if (error != FCERR_UNKNOWN)
2737 return error;
2738 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 error = FCERR_DICT;
2740 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002741 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 int did_save_redo = FALSE;
2743 save_redo_T save_redo;
2744
2745 /*
2746 * Call the user function.
2747 * Save and restore search patterns, script variables and
2748 * redo buffer.
2749 */
2750 save_search_patterns();
2751 if (!ins_compl_active())
2752 {
2753 saveRedobuff(&save_redo);
2754 did_save_redo = TRUE;
2755 }
2756 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002757 call_user_func(fp, argcount, argvars, rettv, funcexe,
2758 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
2760 // Function was unreferenced while being used, free it now.
2761 func_clear_free(fp, FALSE);
2762 if (did_save_redo)
2763 restoreRedobuff(&save_redo);
2764 restore_search_patterns();
2765 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002766 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002768}
2769
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002770static funccal_entry_T *funccal_stack = NULL;
2771
2772/*
2773 * Save the current function call pointer, and set it to NULL.
2774 * Used when executing autocommands and for ":source".
2775 */
2776 void
2777save_funccal(funccal_entry_T *entry)
2778{
2779 entry->top_funccal = current_funccal;
2780 entry->next = funccal_stack;
2781 funccal_stack = entry;
2782 current_funccal = NULL;
2783}
2784
2785 void
2786restore_funccal(void)
2787{
2788 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002789 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002790 else
2791 {
2792 current_funccal = funccal_stack->top_funccal;
2793 funccal_stack = funccal_stack->next;
2794 }
2795}
2796
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02002797 funccall_T *
2798get_current_funccal(void)
2799{
2800 return current_funccal;
2801}
2802
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002803/*
2804 * Mark all functions of script "sid" as deleted.
2805 */
2806 void
2807delete_script_functions(int sid)
2808{
2809 hashitem_T *hi;
2810 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02002811 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002812 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002813 size_t len;
2814
2815 buf[0] = K_SPECIAL;
2816 buf[1] = KS_EXTRA;
2817 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002818 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002819 len = STRLEN(buf);
2820
Bram Moolenaarce658352020-07-31 23:47:12 +02002821 while (todo > 0)
2822 {
2823 todo = func_hashtab.ht_used;
2824 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2825 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002826 {
Bram Moolenaarce658352020-07-31 23:47:12 +02002827 fp = HI2UF(hi);
2828 if (STRNCMP(fp->uf_name, buf, len) == 0)
2829 {
2830 int changed = func_hashtab.ht_changed;
2831
2832 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002833
2834 if (fp->uf_calls > 0)
2835 {
2836 // Function is executing, don't free it but do remove
2837 // it from the hashtable.
2838 if (func_remove(fp))
2839 fp->uf_refcount--;
2840 }
2841 else
2842 {
2843 func_clear(fp, TRUE);
2844 // When clearing a function another function can be
2845 // cleared as a side effect. When that happens start
2846 // over.
2847 if (changed != func_hashtab.ht_changed)
2848 break;
2849 }
Bram Moolenaarce658352020-07-31 23:47:12 +02002850 }
2851 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002852 }
Bram Moolenaarce658352020-07-31 23:47:12 +02002853 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002854}
2855
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002856#if defined(EXITFREE) || defined(PROTO)
2857 void
2858free_all_functions(void)
2859{
2860 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002861 ufunc_T *fp;
2862 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002863 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002864 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002865
Bram Moolenaare38eab22019-12-05 21:50:01 +01002866 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002867 while (current_funccal != NULL)
2868 {
2869 clear_tv(current_funccal->rettv);
2870 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002871 if (current_funccal == NULL && funccal_stack != NULL)
2872 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02002873 }
2874
Bram Moolenaare38eab22019-12-05 21:50:01 +01002875 // First clear what the functions contain. Since this may lower the
2876 // reference count of a function, it may also free a function and change
2877 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002878 while (todo > 0)
2879 {
2880 todo = func_hashtab.ht_used;
2881 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2882 if (!HASHITEM_EMPTY(hi))
2883 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 // clear the def function index now
2885 fp = HI2UF(hi);
2886 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002887 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888
Bram Moolenaare38eab22019-12-05 21:50:01 +01002889 // Only free functions that are not refcounted, those are
2890 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002891 if (func_name_refcount(fp->uf_name))
2892 ++skipped;
2893 else
2894 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002895 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002896 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002897 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002898 {
2899 skipped = 0;
2900 break;
2901 }
2902 }
2903 --todo;
2904 }
2905 }
2906
Bram Moolenaare38eab22019-12-05 21:50:01 +01002907 // Now actually free the functions. Need to start all over every time,
2908 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002909 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002910 while (func_hashtab.ht_used > skipped)
2911 {
2912 todo = func_hashtab.ht_used;
2913 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002914 if (!HASHITEM_EMPTY(hi))
2915 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02002916 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002917 // Only free functions that are not refcounted, those are
2918 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02002919 fp = HI2UF(hi);
2920 if (func_name_refcount(fp->uf_name))
2921 ++skipped;
2922 else
2923 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02002924 if (func_free(fp, FALSE) == OK)
2925 {
2926 skipped = 0;
2927 break;
2928 }
2929 // did not actually free it
2930 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002931 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002932 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02002933 }
2934 if (skipped == 0)
2935 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936
2937 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002938}
2939#endif
2940
2941/*
2942 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02002943 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002944 * "len" is the length of "name", or -1 for NUL terminated.
2945 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002947builtin_function(char_u *name, int len)
2948{
2949 char_u *p;
2950
Bram Moolenaara26b9702020-04-18 19:53:28 +02002951 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002952 return FALSE;
2953 p = vim_strchr(name, AUTOLOAD_CHAR);
2954 return p == NULL || (len > 0 && p > name + len);
2955}
2956
2957 int
2958func_call(
2959 char_u *name,
2960 typval_T *args,
2961 partial_T *partial,
2962 dict_T *selfdict,
2963 typval_T *rettv)
2964{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002965 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002966 listitem_T *item;
2967 typval_T argv[MAX_FUNC_ARGS + 1];
2968 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002969 int r = 0;
2970
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002971 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002972 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002973 {
2974 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
2975 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002976 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002977 break;
2978 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002979 // Make a copy of each argument. This is needed to be able to set
2980 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002981 copy_tv(&item->li_tv, &argv[argc++]);
2982 }
2983
2984 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002985 {
2986 funcexe_T funcexe;
2987
Bram Moolenaara80faa82020-04-12 19:37:17 +02002988 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002989 funcexe.firstline = curwin->w_cursor.lnum;
2990 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002991 funcexe.evaluate = TRUE;
2992 funcexe.partial = partial;
2993 funcexe.selfdict = selfdict;
2994 r = call_func(name, -1, rettv, argc, argv, &funcexe);
2995 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002996
Bram Moolenaare38eab22019-12-05 21:50:01 +01002997 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002998 while (argc > 0)
2999 clear_tv(&argv[--argc]);
3000
3001 return r;
3002}
3003
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003004static int callback_depth = 0;
3005
3006 int
3007get_callback_depth(void)
3008{
3009 return callback_depth;
3010}
3011
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003012/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003013 * Invoke call_func() with a callback.
3014 */
3015 int
3016call_callback(
3017 callback_T *callback,
3018 int len, // length of "name" or -1 to use strlen()
3019 typval_T *rettv, // return value goes here
3020 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003021 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003022 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003023{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003024 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003025 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003026
Bram Moolenaara80faa82020-04-12 19:37:17 +02003027 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003028 funcexe.evaluate = TRUE;
3029 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003030 ++callback_depth;
3031 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3032 --callback_depth;
3033 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003034}
3035
3036/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 * Give an error message for the result of a function.
3038 * Nothing if "error" is FCERR_NONE.
3039 */
3040 void
3041user_func_error(int error, char_u *name)
3042{
3043 switch (error)
3044 {
3045 case FCERR_UNKNOWN:
3046 emsg_funcname(e_unknownfunc, name);
3047 break;
3048 case FCERR_NOTMETHOD:
3049 emsg_funcname(
3050 N_("E276: Cannot use function as a method: %s"), name);
3051 break;
3052 case FCERR_DELETED:
3053 emsg_funcname(N_(e_func_deleted), name);
3054 break;
3055 case FCERR_TOOMANY:
3056 emsg_funcname((char *)e_toomanyarg, name);
3057 break;
3058 case FCERR_TOOFEW:
3059 emsg_funcname((char *)e_toofewarg, name);
3060 break;
3061 case FCERR_SCRIPT:
3062 emsg_funcname(
3063 N_("E120: Using <SID> not in a script context: %s"), name);
3064 break;
3065 case FCERR_DICT:
3066 emsg_funcname(
3067 N_("E725: Calling dict function without Dictionary: %s"),
3068 name);
3069 break;
3070 }
3071}
3072
3073/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003074 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003075 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003076 * Return FAIL when the function can't be called, OK otherwise.
3077 * Also returns OK when an error was encountered while executing the function.
3078 */
3079 int
3080call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003081 char_u *funcname, // name of the function
3082 int len, // length of "name" or -1 to use strlen()
3083 typval_T *rettv, // return value goes here
3084 int argcount_in, // number of "argvars"
3085 typval_T *argvars_in, // vars for arguments, must have "argcount"
3086 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003087 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003088{
3089 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01003090 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003091 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003092 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003093 char_u fname_buf[FLEN_FIXED + 1];
3094 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003095 char_u *fname = NULL;
3096 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003097 int argcount = argcount_in;
3098 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003099 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003100 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
3101 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003102 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003103 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003104 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003105
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003106 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3107 // even when call_func() returns FAIL.
3108 rettv->v_type = VAR_UNKNOWN;
3109
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003110 if (partial != NULL)
3111 fp = partial->pt_func;
3112 if (fp == NULL)
3113 {
3114 // Make a copy of the name, if it comes from a funcref variable it
3115 // could be changed or deleted in the called function.
3116 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3117 if (name == NULL)
3118 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003119
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003120 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3121 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003122
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003123 if (funcexe->doesrange != NULL)
3124 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003125
3126 if (partial != NULL)
3127 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003128 // When the function has a partial with a dict and there is a dict
3129 // argument, use the dict argument. That is backwards compatible.
3130 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003131 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003132 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003133 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003134 {
3135 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003136 {
3137 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3138 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003139 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003140 goto theend;
3141 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003142 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003143 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003144 for (i = 0; i < argcount_in; ++i)
3145 argv[i + argv_clear] = argvars_in[i];
3146 argvars = argv;
3147 argcount = partial->pt_argc + argcount_in;
3148 }
3149 }
3150
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003151 if (error == FCERR_NONE && funcexe->check_type != NULL && funcexe->evaluate)
3152 {
3153 // Check that the argument types are OK for the types of the funcref.
3154 if (check_argument_types(funcexe->check_type, argvars, argcount,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003155 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003156 error = FCERR_OTHER;
3157 }
3158
Bram Moolenaaref140542019-12-31 21:27:13 +01003159 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003160 {
3161 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003162 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003163
Bram Moolenaar333894b2020-08-01 18:53:07 +02003164 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003165 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003166 {
3167 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003168 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003169 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003170
Bram Moolenaare38eab22019-12-05 21:50:01 +01003171 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003172 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003173 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003174
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003175 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003176 {
3177 /*
3178 * User defined function.
3179 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003180 if (fp == NULL)
Bram Moolenaar333894b2020-08-01 18:53:07 +02003181 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003182
Bram Moolenaare38eab22019-12-05 21:50:01 +01003183 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003184 if (fp == NULL
3185 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003186 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003187 && !aborting())
3188 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003189 // executed an autocommand, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02003190 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003191 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003192 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003193 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3194 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003195 // loaded a package, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02003196 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003197 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003198 if (fp == NULL)
3199 {
3200 char_u *p = untrans_function_name(rfname);
3201
3202 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003203 // Don't do this if the name starts with "s:".
3204 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaar333894b2020-08-01 18:53:07 +02003205 fp = find_func(p, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003206 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003207
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003208 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003209 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02003210#ifdef FEAT_LUA
3211 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
3212 {
3213 cfunc_T cb = fp->uf_cb;
3214
3215 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3216 }
3217#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003218 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003219 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003220 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003221 // postponed filling in the arguments, do it now
3222 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003223 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003224
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003225 if (funcexe->basetv != NULL)
3226 {
3227 // Method call: base->Method()
3228 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
3229 argv[0] = *funcexe->basetv;
3230 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003231 argvars = argv;
3232 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003233 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 error = call_user_func_check(fp, argcount, argvars, rettv,
3236 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003237 }
3238 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003239 else if (funcexe->basetv != NULL)
3240 {
3241 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003242 * expr->method(): Find the method name in the table, call its
3243 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003244 */
3245 error = call_internal_method(fname, argcount, argvars, rettv,
3246 funcexe->basetv);
3247 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003248 else
3249 {
3250 /*
3251 * Find the function name in the table, call its implementation.
3252 */
3253 error = call_internal_func(fname, argcount, argvars, rettv);
3254 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003255
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003256 /*
3257 * The function call (or "FuncUndefined" autocommand sequence) might
3258 * have been aborted by an error, an interrupt, or an explicitly thrown
3259 * exception that has not been caught so far. This situation can be
3260 * tested for by calling aborting(). For an error in an internal
3261 * function or for the "E132" error in call_user_func(), however, the
3262 * throw point at which the "force_abort" flag (temporarily reset by
3263 * emsg()) is normally updated has not been reached yet. We need to
3264 * update that flag first to make aborting() reliable.
3265 */
3266 update_force_abort();
3267 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003268 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003269 ret = OK;
3270
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003271theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003272 /*
3273 * Report an error unless the argument evaluation or function call has been
3274 * cancelled due to an aborting error, an interrupt, or an exception.
3275 */
3276 if (!aborting())
3277 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003278 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003279 }
3280
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003281 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003282 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003283 clear_tv(&argv[--argv_clear + argv_base]);
3284
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003285 vim_free(tofree);
3286 vim_free(name);
3287
3288 return ret;
3289}
3290
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003291 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292printable_func_name(ufunc_T *fp)
3293{
3294 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3295}
3296
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003297/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003298 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003299 */
3300 static void
3301list_func_head(ufunc_T *fp, int indent)
3302{
3303 int j;
3304
3305 msg_start();
3306 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003307 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003308 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003309 msg_puts("def ");
3310 else
3311 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003313 msg_putchar('(');
3314 for (j = 0; j < fp->uf_args.ga_len; ++j)
3315 {
3316 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003317 msg_puts(", ");
3318 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 if (fp->uf_arg_types != NULL)
3320 {
3321 char *tofree;
3322
3323 msg_puts(": ");
3324 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3325 vim_free(tofree);
3326 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003327 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3328 {
3329 msg_puts(" = ");
3330 msg_puts(((char **)(fp->uf_def_args.ga_data))
3331 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3332 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003333 }
3334 if (fp->uf_varargs)
3335 {
3336 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003337 msg_puts(", ");
3338 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003339 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 if (fp->uf_va_name != NULL)
3341 {
3342 if (j)
3343 msg_puts(", ");
3344 msg_puts("...");
3345 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003346 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 {
3348 char *tofree;
3349
3350 msg_puts(": ");
3351 msg_puts(type_name(fp->uf_va_type, &tofree));
3352 vim_free(tofree);
3353 }
3354 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003355 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003356
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003357 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003358 {
3359 if (fp->uf_ret_type != &t_void)
3360 {
3361 char *tofree;
3362
3363 msg_puts(": ");
3364 msg_puts(type_name(fp->uf_ret_type, &tofree));
3365 vim_free(tofree);
3366 }
3367 }
3368 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003369 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003370 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003371 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003372 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003373 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003374 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003375 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003376 msg_clr_eos();
3377 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003378 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003379}
3380
3381/*
3382 * Get a function name, translating "<SID>" and "<SNR>".
3383 * Also handles a Funcref in a List or Dictionary.
3384 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003385 * Set "*is_global" to TRUE when the function must be global, unless
3386 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003387 * flags:
3388 * TFN_INT: internal function name OK
3389 * TFN_QUIET: be quiet
3390 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003391 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003392 * Advances "pp" to just after the function name (if no error).
3393 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003394 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003395trans_function_name(
3396 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003397 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003398 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003399 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003400 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003401 partial_T **partial, // return: partial of a FuncRef
3402 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003403{
3404 char_u *name = NULL;
3405 char_u *start;
3406 char_u *end;
3407 int lead;
3408 char_u sid_buf[20];
3409 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003411 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 int vim9script;
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003413 static char *e_function_name = N_("E129: Function name required");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003414
3415 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003416 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003417 start = *pp;
3418
Bram Moolenaare38eab22019-12-05 21:50:01 +01003419 // Check for hard coded <SNR>: already translated function ID (from a user
3420 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003421 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
3422 && (*pp)[2] == (int)KE_SNR)
3423 {
3424 *pp += 3;
3425 len = get_id_len(pp) + 3;
3426 return vim_strnsave(start, len);
3427 }
3428
Bram Moolenaare38eab22019-12-05 21:50:01 +01003429 // A name starting with "<SID>" or "<SNR>" is local to a script. But
3430 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003431 lead = eval_fname_script(start);
3432 if (lead > 2)
3433 start += lead;
3434
Bram Moolenaare38eab22019-12-05 21:50:01 +01003435 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01003436 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003437 lead > 2 ? 0 : FNE_CHECK_START);
3438 if (end == start)
3439 {
3440 if (!skip)
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003441 emsg(_(e_function_name));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003442 goto theend;
3443 }
3444 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
3445 {
3446 /*
3447 * Report an invalid expression in braces, unless the expression
3448 * evaluation has been cancelled due to an aborting error, an
3449 * interrupt, or an exception.
3450 */
3451 if (!aborting())
3452 {
3453 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003454 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003455 }
3456 else
3457 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
3458 goto theend;
3459 }
3460
3461 if (lv.ll_tv != NULL)
3462 {
3463 if (fdp != NULL)
3464 {
3465 fdp->fd_dict = lv.ll_dict;
3466 fdp->fd_newkey = lv.ll_newkey;
3467 lv.ll_newkey = NULL;
3468 fdp->fd_di = lv.ll_di;
3469 }
3470 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
3471 {
3472 name = vim_strsave(lv.ll_tv->vval.v_string);
3473 *pp = end;
3474 }
3475 else if (lv.ll_tv->v_type == VAR_PARTIAL
3476 && lv.ll_tv->vval.v_partial != NULL)
3477 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003478 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003479 *pp = end;
3480 if (partial != NULL)
3481 *partial = lv.ll_tv->vval.v_partial;
3482 }
3483 else
3484 {
3485 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
3486 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003487 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003488 else
3489 *pp = end;
3490 name = NULL;
3491 }
3492 goto theend;
3493 }
3494
3495 if (lv.ll_name == NULL)
3496 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003497 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003498 *pp = end;
3499 goto theend;
3500 }
3501
Bram Moolenaare38eab22019-12-05 21:50:01 +01003502 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003503 if (lv.ll_exp_name != NULL)
3504 {
3505 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003506 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003507 flags & TFN_NO_AUTOLOAD);
3508 if (name == lv.ll_exp_name)
3509 name = NULL;
3510 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003511 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003512 {
3513 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003514 name = deref_func_name(*pp, &len, partial, type,
3515 flags & TFN_NO_AUTOLOAD);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003516 if (name == *pp)
3517 name = NULL;
3518 }
3519 if (name != NULL)
3520 {
3521 name = vim_strsave(name);
3522 *pp = end;
3523 if (STRNCMP(name, "<SNR>", 5) == 0)
3524 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003525 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003526 name[0] = K_SPECIAL;
3527 name[1] = KS_EXTRA;
3528 name[2] = (int)KE_SNR;
3529 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
3530 }
3531 goto theend;
3532 }
3533
3534 if (lv.ll_exp_name != NULL)
3535 {
3536 len = (int)STRLEN(lv.ll_exp_name);
3537 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
3538 && STRNCMP(lv.ll_name, "s:", 2) == 0)
3539 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003540 // When there was "s:" already or the name expanded to get a
3541 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003542 lv.ll_name += 2;
3543 len -= 2;
3544 lead = 2;
3545 }
3546 }
3547 else
3548 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003549 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003550 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003551 {
3552 if (is_global != NULL && lv.ll_name[0] == 'g')
3553 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003554 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003555 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003556 len = (int)(end - lv.ll_name);
3557 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003558 if (len <= 0)
3559 {
3560 if (!skip)
3561 emsg(_(e_function_name));
3562 goto theend;
3563 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003564
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003565 // In Vim9 script a user function is script-local by default, unless it
3566 // starts with a lower case character: dict.func().
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003567 vim9script = ASCII_ISUPPER(*start) && in_vim9script();
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003568 if (vim9script)
3569 {
3570 char_u *p;
3571
3572 // SomeScript#func() is a global function.
3573 for (p = start; *p != NUL && *p != '('; ++p)
3574 if (*p == AUTOLOAD_CHAR)
3575 vim9script = FALSE;
3576 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003578 /*
3579 * Copy the function name to allocated memory.
3580 * Accept <SID>name() inside a script, translate into <SNR>123_name().
3581 * Accept <SNR>123_name() outside a script.
3582 */
3583 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003584 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003586 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 if (!vim9script)
3588 lead = 3;
3589 if (vim9script || (lv.ll_exp_name != NULL
3590 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003591 || eval_fname_sid(*pp))
3592 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003594 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003595 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003596 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003597 goto theend;
3598 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003599 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 if (vim9script)
3601 extra = 3 + (int)STRLEN(sid_buf);
3602 else
3603 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003604 }
3605 }
Bram Moolenaar22f17a22021-06-21 20:48:58 +02003606 else if (!(flags & TFN_INT) && (builtin_function(lv.ll_name, len)
3607 || (in_vim9script() && *lv.ll_name == '_')))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003608 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003609 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003610 start);
3611 goto theend;
3612 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003613 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003614 {
3615 char_u *cp = vim_strchr(lv.ll_name, ':');
3616
3617 if (cp != NULL && cp < end)
3618 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003619 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003620 goto theend;
3621 }
3622 }
3623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003625 if (name != NULL)
3626 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01003627 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003628 {
3629 name[0] = K_SPECIAL;
3630 name[1] = KS_EXTRA;
3631 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003633 STRCPY(name + 3, sid_buf);
3634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
3636 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003637 }
3638 *pp = end;
3639
3640theend:
3641 clear_lval(&lv);
3642 return name;
3643}
3644
3645/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02003646 * Assuming "name" is the result of trans_function_name() and it was prefixed
3647 * to use the script-local name, return the unmodified name (points into
3648 * "name"). Otherwise return NULL.
3649 * This can be used to first search for a script-local function and fall back
3650 * to the global function if not found.
3651 */
3652 char_u *
3653untrans_function_name(char_u *name)
3654{
3655 char_u *p;
3656
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003657 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02003658 {
3659 p = vim_strchr(name, '_');
3660 if (p != NULL)
3661 return p + 1;
3662 }
3663 return NULL;
3664}
3665
3666/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003667 * List functions. When "regmatch" is NULL all of then.
3668 * Otherwise functions matching "regmatch".
3669 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003670 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003671list_functions(regmatch_T *regmatch)
3672{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003673 int changed = func_hashtab.ht_changed;
3674 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003675 hashitem_T *hi;
3676
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003677 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003678 {
3679 if (!HASHITEM_EMPTY(hi))
3680 {
3681 ufunc_T *fp = HI2UF(hi);
3682
3683 --todo;
3684 if ((fp->uf_flags & FC_DEAD) == 0
3685 && (regmatch == NULL
3686 ? !message_filtered(fp->uf_name)
3687 && !func_name_refcount(fp->uf_name)
3688 : !isdigit(*fp->uf_name)
3689 && vim_regexec(regmatch, fp->uf_name, 0)))
3690 {
3691 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003692 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003693 {
3694 emsg(_("E454: function list was modified"));
3695 return;
3696 }
3697 }
3698 }
3699 }
3700}
3701
3702/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02003703 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003704 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
3705 * the function name.
Bram Moolenaar04b12692020-05-04 23:24:44 +02003706 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003707 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02003708 ufunc_T *
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003709define_function(exarg_T *eap, char_u *name_arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003710{
Bram Moolenaar53564f72017-06-24 14:48:11 +02003711 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003712 int j;
3713 int c;
3714 int saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003715 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003716 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003717 char_u *p;
3718 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01003719 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003720 char_u *line_arg = NULL;
3721 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003723 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003724 garray_T newlines;
3725 int varargs = FALSE;
3726 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003728 ufunc_T *fp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003729 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003730 dictitem_T *v;
3731 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003732 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003733 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003734 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003735 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003736 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02003737 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003738
3739 /*
3740 * ":function" without argument: list functions.
3741 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003742 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003743 {
3744 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003745 list_functions(NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003746 eap->nextcmd = check_nextcmd(eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003747 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003748 }
3749
3750 /*
3751 * ":function /pat": list functions matching pattern.
3752 */
3753 if (*eap->arg == '/')
3754 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003755 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003756 if (!eap->skip)
3757 {
3758 regmatch_T regmatch;
3759
3760 c = *p;
3761 *p = NUL;
3762 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
3763 *p = c;
3764 if (regmatch.regprog != NULL)
3765 {
3766 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003767 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003768 vim_regfree(regmatch.regprog);
3769 }
3770 }
3771 if (*p == '/')
3772 ++p;
3773 eap->nextcmd = check_nextcmd(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003774 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003775 }
3776
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 ga_init(&newargs);
3778 ga_init(&argtypes);
3779 ga_init(&default_args);
3780
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003781 /*
3782 * Get the function name. There are these situations:
3783 * func normal function name
3784 * "name" == func, "fudi.fd_dict" == NULL
3785 * dict.func new dictionary entry
3786 * "name" == NULL, "fudi.fd_dict" set,
3787 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
3788 * dict.func existing dict entry with a Funcref
3789 * "name" == func, "fudi.fd_dict" set,
3790 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3791 * dict.func existing dict entry that's not a Funcref
3792 * "name" == NULL, "fudi.fd_dict" set,
3793 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3794 * s:func script-local function name
3795 * g:func global function name, same as "func"
3796 */
3797 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003798 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003799 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003800 // nested function, argument is (args).
3801 paren = TRUE;
3802 CLEAR_FIELD(fudi);
3803 }
3804 else
3805 {
Bram Moolenaarb6571982021-01-08 22:24:19 +01003806 if (STRNCMP(p, "<lambda>", 8) == 0)
3807 {
3808 p += 8;
3809 (void)getdigits(&p);
3810 name = vim_strnsave(eap->arg, p - eap->arg);
3811 CLEAR_FIELD(fudi);
3812 }
3813 else
3814 name = trans_function_name(&p, &is_global, eap->skip,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003815 TFN_NO_AUTOLOAD, &fudi, NULL, NULL);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003816 paren = (vim_strchr(p, '(') != NULL);
3817 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003818 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003819 /*
3820 * Return on an invalid expression in braces, unless the expression
3821 * evaluation has been cancelled due to an aborting error, an
3822 * interrupt, or an exception.
3823 */
3824 if (!aborting())
3825 {
3826 if (!eap->skip && fudi.fd_newkey != NULL)
3827 semsg(_(e_dictkey), fudi.fd_newkey);
3828 vim_free(fudi.fd_newkey);
3829 return NULL;
3830 }
3831 else
3832 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003833 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003834 }
3835
Bram Moolenaare38eab22019-12-05 21:50:01 +01003836 // An error in a function call during evaluation of an expression in magic
3837 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003838 saved_did_emsg = did_emsg;
3839 did_emsg = FALSE;
3840
3841 /*
3842 * ":function func" with only function name: list function.
3843 */
3844 if (!paren)
3845 {
3846 if (!ends_excmd(*skipwhite(p)))
3847 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003848 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003849 goto ret_free;
3850 }
3851 eap->nextcmd = check_nextcmd(p);
3852 if (eap->nextcmd != NULL)
3853 *p = NUL;
3854 if (!eap->skip && !got_int)
3855 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003856 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003857 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
3858 {
3859 char_u *up = untrans_function_name(name);
3860
3861 // With Vim9 script the name was made script-local, if not
3862 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02003863 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003864 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003865 }
3866
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003867 if (fp != NULL)
3868 {
3869 list_func_head(fp, TRUE);
3870 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
3871 {
3872 if (FUNCLINE(fp, j) == NULL)
3873 continue;
3874 msg_putchar('\n');
3875 msg_outnum((long)(j + 1));
3876 if (j < 9)
3877 msg_putchar(' ');
3878 if (j < 99)
3879 msg_putchar(' ');
3880 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003881 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003882 ui_breakcheck();
3883 }
3884 if (!got_int)
3885 {
3886 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003887 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 msg_puts(" enddef");
3889 else
3890 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003891 }
3892 }
3893 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02003894 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003895 }
3896 goto ret_free;
3897 }
3898
3899 /*
3900 * ":function name(arg1, arg2)" Define function.
3901 */
3902 p = skipwhite(p);
3903 if (*p != '(')
3904 {
3905 if (!eap->skip)
3906 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003907 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003908 goto ret_free;
3909 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003910 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003911 if (vim_strchr(p, '(') != NULL)
3912 p = vim_strchr(p, '(');
3913 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003914
Bram Moolenaar4efd9942021-01-24 21:14:20 +01003915 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
3916 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003917 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01003918 goto ret_free;
3919 }
3920
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02003921 // In Vim9 script only global functions can be redefined.
3922 if (vim9script && eap->forceit && !is_global)
3923 {
3924 emsg(_(e_nobang));
3925 goto ret_free;
3926 }
3927
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003928 ga_init2(&newlines, (int)sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003929
Bram Moolenaar04b12692020-05-04 23:24:44 +02003930 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003931 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003932 // Check the name of the function. Unless it's a dictionary function
3933 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003934 if (name != NULL)
3935 arg = name;
3936 else
3937 arg = fudi.fd_newkey;
3938 if (arg != NULL && (fudi.fd_di == NULL
3939 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
3940 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
3941 {
3942 if (*arg == K_SPECIAL)
3943 j = 3;
3944 else
3945 j = 0;
3946 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
3947 : eval_isnamec(arg[j])))
3948 ++j;
3949 if (arg[j] != NUL)
3950 emsg_funcname((char *)e_invarg2, arg);
3951 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003952 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003953 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003954 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003955 }
3956
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003957 // This may get more lines and make the pointers into the first line
3958 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01003959 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003961 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01003962 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003963 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003964 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01003965 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003968 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 // find the return type: :def Func(): type
3970 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003971 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02003973 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003975 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003976 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01003977 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003981 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003982 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02003983 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003984 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003985 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02003986 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003987 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 else
3989 // find extra arguments "range", "dict", "abort" and "closure"
3990 for (;;)
3991 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01003992 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 p = skipwhite(p);
3994 if (STRNCMP(p, "range", 5) == 0)
3995 {
3996 flags |= FC_RANGE;
3997 p += 5;
3998 }
3999 else if (STRNCMP(p, "dict", 4) == 0)
4000 {
4001 flags |= FC_DICT;
4002 p += 4;
4003 }
4004 else if (STRNCMP(p, "abort", 5) == 0)
4005 {
4006 flags |= FC_ABORT;
4007 p += 5;
4008 }
4009 else if (STRNCMP(p, "closure", 7) == 0)
4010 {
4011 flags |= FC_CLOSURE;
4012 p += 7;
4013 if (current_funccal == NULL)
4014 {
4015 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
4016 name == NULL ? (char_u *)"" : name);
4017 goto erret;
4018 }
4019 }
4020 else
4021 break;
4022 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004023
Bram Moolenaare38eab22019-12-05 21:50:01 +01004024 // When there is a line break use what follows for the function body.
4025 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004026 if (*p == '\n')
4027 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004028 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004029 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4030 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004031 && !(VIM_ISWHITE(*whitep) && *p == '#'
4032 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004033 && !eap->skip
4034 && !did_emsg)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004035 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004036
4037 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4039 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004040 */
4041 if (KeyTyped)
4042 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004043 // Check if the function already exists, don't let the user type the
4044 // whole function before telling him it doesn't work! For a script we
4045 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004046 if (!eap->skip && !eap->forceit)
4047 {
4048 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004049 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004050 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004051 emsg_funcname(e_funcexts, name);
4052 }
4053
4054 if (!eap->skip && did_emsg)
4055 goto erret;
4056
Bram Moolenaare38eab22019-12-05 21:50:01 +01004057 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004058 cmdline_row = msg_row;
4059 }
4060
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004061 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004062 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004063
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004064 // Do not define the function when getting the body fails and when
4065 // skipping.
4066 if (get_function_body(eap, &newlines, line_arg, &line_to_free) == FAIL
4067 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004068 goto erret;
4069
4070 /*
4071 * If there are no errors, add the function
4072 */
4073 if (fudi.fd_dict == NULL)
4074 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 hashtab_T *ht;
4076
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004077 v = find_var(name, &ht, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004078 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
4079 {
4080 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
4081 name);
4082 goto erret;
4083 }
4084
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004085 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaareef21022020-08-01 22:16:43 +02004086 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004087 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004088 char_u *uname = untrans_function_name(name);
4089
4090 import = find_imported(uname == NULL ? name : uname, 0, NULL);
4091 }
4092
4093 if (fp != NULL || import != NULL)
4094 {
4095 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004097 // Function can be replaced with "function!" and when sourcing the
4098 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02004099 // A name that is used by an import can not be overruled.
4100 if (import != NULL
4101 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004102 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02004103 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004104 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004105 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004106 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02004107 else
4108 emsg_funcname(e_funcexts, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004109 goto erret;
4110 }
4111 if (fp->uf_calls > 0)
4112 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004113 emsg_funcname(
4114 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004115 name);
4116 goto erret;
4117 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004118 if (fp->uf_refcount > 1)
4119 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004120 // This function is referenced somewhere, don't redefine it but
4121 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004122 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004123 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004124 fp = NULL;
4125 overwrite = TRUE;
4126 }
4127 else
4128 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004129 char_u *exp_name = fp->uf_name_exp;
4130
4131 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01004132 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004133 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004134 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004135 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004137#ifdef FEAT_PROFILE
4138 fp->uf_profiling = FALSE;
4139 fp->uf_prof_initialized = FALSE;
4140#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01004141 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004142 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004143 }
4144 }
4145 else
4146 {
4147 char numbuf[20];
4148
4149 fp = NULL;
4150 if (fudi.fd_newkey == NULL && !eap->forceit)
4151 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004152 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004153 goto erret;
4154 }
4155 if (fudi.fd_di == NULL)
4156 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004157 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02004158 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004159 goto erret;
4160 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004161 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02004162 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004163 goto erret;
4164
Bram Moolenaare38eab22019-12-05 21:50:01 +01004165 // Give the function a sequential number. Can only be used with a
4166 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004167 vim_free(name);
4168 sprintf(numbuf, "%d", ++func_nr);
4169 name = vim_strsave((char_u *)numbuf);
4170 if (name == NULL)
4171 goto erret;
4172 }
4173
4174 if (fp == NULL)
4175 {
4176 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
4177 {
4178 int slen, plen;
4179 char_u *scriptname;
4180
Bram Moolenaare38eab22019-12-05 21:50:01 +01004181 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004182 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004183 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004184 {
4185 scriptname = autoload_name(name);
4186 if (scriptname != NULL)
4187 {
4188 p = vim_strchr(scriptname, '/');
4189 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004190 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004191 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004192 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004193 j = OK;
4194 vim_free(scriptname);
4195 }
4196 }
4197 if (j == FAIL)
4198 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004199 linenr_T save_lnum = SOURCING_LNUM;
4200
4201 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004202 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004203 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004204 goto erret;
4205 }
4206 }
4207
Bram Moolenaar47ed5532019-08-08 20:49:14 +02004208 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004209 if (fp == NULL)
4210 goto erret;
4211
4212 if (fudi.fd_dict != NULL)
4213 {
4214 if (fudi.fd_di == NULL)
4215 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004216 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004217 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
4218 if (fudi.fd_di == NULL)
4219 {
4220 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004221 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004222 goto erret;
4223 }
4224 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
4225 {
4226 vim_free(fudi.fd_di);
4227 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004228 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004229 goto erret;
4230 }
4231 }
4232 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004233 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004234 clear_tv(&fudi.fd_di->di_tv);
4235 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004236 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004237
Bram Moolenaare38eab22019-12-05 21:50:01 +01004238 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004239 flags |= FC_DICT;
4240 }
4241
Bram Moolenaare38eab22019-12-05 21:50:01 +01004242 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004243 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004244 if (overwrite)
4245 {
4246 hi = hash_find(&func_hashtab, name);
4247 hi->hi_key = UF2HIKEY(fp);
4248 }
4249 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004250 {
4251 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004252 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004253 goto erret;
4254 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004255 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004256 }
4257 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004258 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004260 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261
4262 if (eap->cmdidx == CMD_def)
4263 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004264 int lnum_save = SOURCING_LNUM;
4265 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004266
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004267 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004268
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004269 // error messages are for the first function line
4270 SOURCING_LNUM = sourcing_lnum_top;
4271
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004272 if (cstack != NULL && cstack->cs_idx >= 0)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004273 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004274 int count = cstack->cs_idx + 1;
4275 int i;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004276
4277 // The block context may be needed for script variables declared in
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004278 // a block that is visible now but not when the function is called
4279 // later.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004280 fp->uf_block_ids = ALLOC_MULT(int, count);
4281 if (fp->uf_block_ids != NULL)
4282 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004283 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004284 sizeof(int) * count);
4285 fp->uf_block_depth = count;
4286 }
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004287
4288 // Set flag in each block to indicate a function was defined. This
4289 // is used to keep the variable when leaving the block, see
4290 // hide_script_var().
4291 for (i = 0; i <= cstack->cs_idx; ++i)
4292 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004293 }
4294
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004295 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004297 SOURCING_LNUM = lnum_save;
4298 goto errret_2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004300 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301
4302 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004303 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004305 SOURCING_LNUM = lnum_save;
4306 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02004308 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004310 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004311 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004313 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004314 if ((flags & FC_CLOSURE) != 0)
4315 {
Bram Moolenaar58016442016-07-31 18:30:22 +02004316 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004317 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004318 }
4319 else
4320 fp->uf_scoped = NULL;
4321
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004322#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004323 if (prof_def_func())
4324 func_do_profile(fp);
4325#endif
4326 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02004327 if (sandbox)
4328 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004329 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004330 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004331 fp->uf_flags = flags;
4332 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01004333 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004334 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004335 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004336 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337 if (is_export)
4338 {
4339 fp->uf_flags |= FC_EXPORT;
4340 // let ex_export() know the export worked.
4341 is_export = FALSE;
4342 }
4343
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004344 if (eap->cmdidx == CMD_def)
4345 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02004346 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
4347 // :func does not use Vim9 script syntax, even in a Vim9 script file
4348 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004349
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004350 goto ret_free;
4351
4352erret:
4353 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004354 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004355 if (fp != NULL)
4356 {
4357 ga_init(&fp->uf_args);
4358 ga_init(&fp->uf_def_args);
4359 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004360errret_2:
4361 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004362 if (fp != NULL)
4363 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004364ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004365 ga_clear_strings(&argtypes);
Bram Moolenaar53564f72017-06-24 14:48:11 +02004366 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004367 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004368 if (name != name_arg)
4369 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004370 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004371 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004372
4373 return fp;
4374}
4375
4376/*
4377 * ":function"
4378 */
4379 void
4380ex_function(exarg_T *eap)
4381{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004382 (void)define_function(eap, NULL);
Bram Moolenaar822ba242020-05-24 23:00:18 +02004383}
4384
4385/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004386 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarb2049902021-01-24 12:53:53 +01004387 * be compiled. Except dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02004388 */
4389 void
4390ex_defcompile(exarg_T *eap UNUSED)
4391{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004392 long todo = (long)func_hashtab.ht_used;
4393 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004394 hashitem_T *hi;
4395 ufunc_T *ufunc;
4396
4397 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
4398 {
4399 if (!HASHITEM_EMPTY(hi))
4400 {
4401 --todo;
4402 ufunc = HI2UF(hi);
4403 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004404 && ufunc->uf_def_status == UF_TO_BE_COMPILED
4405 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004406 {
Bram Moolenaare99d4222021-06-13 14:01:26 +02004407 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004408
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004409 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004410 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004411 // a function has been added or removed, need to start over
4412 todo = (long)func_hashtab.ht_used;
4413 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004414 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02004415 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004416 }
4417 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004418 }
4419 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004420}
4421
4422/*
4423 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
4424 * Return 2 if "p" starts with "s:".
4425 * Return 0 otherwise.
4426 */
4427 int
4428eval_fname_script(char_u *p)
4429{
Bram Moolenaare38eab22019-12-05 21:50:01 +01004430 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
4431 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004432 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
4433 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
4434 return 5;
4435 if (p[0] == 's' && p[1] == ':')
4436 return 2;
4437 return 0;
4438}
4439
4440 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004441translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004442{
4443 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004444 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004445 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446}
4447
4448/*
4449 * Return TRUE when "ufunc" has old-style "..." varargs
4450 * or named varargs "...name: type".
4451 */
4452 int
4453has_varargs(ufunc_T *ufunc)
4454{
4455 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004456}
4457
4458/*
4459 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004460 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004461 */
4462 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004463function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004464{
4465 char_u *nm = name;
4466 char_u *p;
4467 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004468 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004469 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004470
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004471 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
4472 if (no_deref)
4473 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004474 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004475 nm = skipwhite(nm);
4476
Bram Moolenaare38eab22019-12-05 21:50:01 +01004477 // Only accept "funcname", "funcname ", "funcname (..." and
4478 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004479 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004480 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004481 vim_free(p);
4482 return n;
4483}
4484
Bram Moolenaar113e1072019-01-20 15:30:40 +01004485#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004486 char_u *
4487get_expanded_name(char_u *name, int check)
4488{
4489 char_u *nm = name;
4490 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004491 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004492
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004493 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004494 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004495
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004496 if (p != NULL && *nm == NUL
4497 && (!check || translated_function_exists(p, is_global)))
4498 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004499
4500 vim_free(p);
4501 return NULL;
4502}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004503#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004504
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004505/*
4506 * Function given to ExpandGeneric() to obtain the list of user defined
4507 * function names.
4508 */
4509 char_u *
4510get_user_func_name(expand_T *xp, int idx)
4511{
4512 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004513 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004514 static hashitem_T *hi;
4515 ufunc_T *fp;
4516
4517 if (idx == 0)
4518 {
4519 done = 0;
4520 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004521 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004522 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004523 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004524 {
4525 if (done++ > 0)
4526 ++hi;
4527 while (HASHITEM_EMPTY(hi))
4528 ++hi;
4529 fp = HI2UF(hi);
4530
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 // don't show dead, dict and lambda functions
4532 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02004533 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004535
4536 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004537 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004538
4539 cat_func_name(IObuff, fp);
4540 if (xp->xp_context != EXPAND_USER_FUNC)
4541 {
4542 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004544 STRCAT(IObuff, ")");
4545 }
4546 return IObuff;
4547 }
4548 return NULL;
4549}
4550
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004551/*
4552 * ":delfunction {name}"
4553 */
4554 void
4555ex_delfunction(exarg_T *eap)
4556{
4557 ufunc_T *fp = NULL;
4558 char_u *p;
4559 char_u *name;
4560 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004561 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004562
4563 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004564 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
4565 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004566 vim_free(fudi.fd_newkey);
4567 if (name == NULL)
4568 {
4569 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004570 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004571 return;
4572 }
4573 if (!ends_excmd(*skipwhite(p)))
4574 {
4575 vim_free(name);
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004576 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004577 return;
4578 }
4579 eap->nextcmd = check_nextcmd(p);
4580 if (eap->nextcmd != NULL)
4581 *p = NUL;
4582
4583 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004584 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004585 vim_free(name);
4586
4587 if (!eap->skip)
4588 {
4589 if (fp == NULL)
4590 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004591 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004592 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004593 return;
4594 }
4595 if (fp->uf_calls > 0)
4596 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004597 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004598 return;
4599 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004600 if (fp->uf_flags & FC_VIM9)
4601 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004602 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004603 return;
4604 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004605
4606 if (fudi.fd_dict != NULL)
4607 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004608 // Delete the dict item that refers to the function, it will
4609 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004610 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4611 }
4612 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004613 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004614 // A normal function (not a numbered function or lambda) has a
4615 // refcount of 1 for the entry in the hashtable. When deleting
4616 // it and the refcount is more than one, it should be kept.
4617 // A numbered function and lambda should be kept if the refcount is
4618 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004619 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004620 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004621 // Function is still referenced somewhere. Don't free it but
4622 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004623 if (func_remove(fp))
4624 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004625 }
4626 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004627 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004628 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004629 }
4630}
4631
4632/*
4633 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004634 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004635 */
4636 void
4637func_unref(char_u *name)
4638{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004639 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004640
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004641 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004642 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004643 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004644 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004645 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004646#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004647 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004648#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004649 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004650 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004651 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004652}
4653
4654/*
4655 * Unreference a Function: decrement the reference count and free it when it
4656 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004657 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004658 */
4659 void
4660func_ptr_unref(ufunc_T *fp)
4661{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004662 if (fp != NULL && (--fp->uf_refcount <= 0
4663 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
4664 && fp->uf_partial->pt_refcount <= 1
4665 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02004666 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004667 // Only delete it when it's not being used. Otherwise it's done
4668 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004669 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004670 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004671 }
4672}
4673
4674/*
4675 * Count a reference to a Function.
4676 */
4677 void
4678func_ref(char_u *name)
4679{
4680 ufunc_T *fp;
4681
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004682 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004683 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004684 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004685 if (fp != NULL)
4686 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004687 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01004688 // Only give an error for a numbered function.
4689 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01004690 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004691}
4692
4693/*
4694 * Count a reference to a Function.
4695 */
4696 void
4697func_ptr_ref(ufunc_T *fp)
4698{
4699 if (fp != NULL)
4700 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004701}
4702
4703/*
4704 * Return TRUE if items in "fc" do not have "copyID". That means they are not
4705 * referenced from anywhere that is in use.
4706 */
4707 static int
4708can_free_funccal(funccall_T *fc, int copyID)
4709{
4710 return (fc->l_varlist.lv_copyID != copyID
4711 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004712 && fc->l_avars.dv_copyID != copyID
4713 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004714}
4715
4716/*
4717 * ":return [expr]"
4718 */
4719 void
4720ex_return(exarg_T *eap)
4721{
4722 char_u *arg = eap->arg;
4723 typval_T rettv;
4724 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004725 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004726
4727 if (current_funccal == NULL)
4728 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004729 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004730 return;
4731 }
4732
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004733 CLEAR_FIELD(evalarg);
4734 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
4735
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004736 if (eap->skip)
4737 ++emsg_skip;
4738
4739 eap->nextcmd = NULL;
4740 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004741 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004742 {
4743 if (!eap->skip)
4744 returning = do_return(eap, FALSE, TRUE, &rettv);
4745 else
4746 clear_tv(&rettv);
4747 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004748 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004749 else if (!eap->skip)
4750 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004751 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01004752 update_force_abort();
4753
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004754 /*
4755 * Return unless the expression evaluation has been cancelled due to an
4756 * aborting error, an interrupt, or an exception.
4757 */
4758 if (!aborting())
4759 returning = do_return(eap, FALSE, TRUE, NULL);
4760 }
4761
Bram Moolenaare38eab22019-12-05 21:50:01 +01004762 // When skipping or the return gets pending, advance to the next command
4763 // in this line (!returning). Otherwise, ignore the rest of the line.
4764 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004765 if (returning)
4766 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004767 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004768 eap->nextcmd = check_nextcmd(arg);
4769
4770 if (eap->skip)
4771 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02004772 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004773}
4774
4775/*
4776 * ":1,25call func(arg1, arg2)" function call.
4777 */
4778 void
4779ex_call(exarg_T *eap)
4780{
4781 char_u *arg = eap->arg;
4782 char_u *startarg;
4783 char_u *name;
4784 char_u *tofree;
4785 int len;
4786 typval_T rettv;
4787 linenr_T lnum;
4788 int doesrange;
4789 int failed = FALSE;
4790 funcdict_T fudi;
4791 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004792 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004793 type_T *type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004794
Bram Moolenaare6b53242020-07-01 17:28:33 +02004795 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004796 if (eap->skip)
4797 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004798 // trans_function_name() doesn't work well when skipping, use eval0()
4799 // instead to skip to any following command, e.g. for:
4800 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004801 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004802 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004803 clear_tv(&rettv);
4804 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004805 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004806 return;
4807 }
4808
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004809 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
4810 &fudi, &partial, in_vim9script() ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004811 if (fudi.fd_newkey != NULL)
4812 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004813 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004814 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004815 vim_free(fudi.fd_newkey);
4816 }
4817 if (tofree == NULL)
4818 return;
4819
Bram Moolenaare38eab22019-12-05 21:50:01 +01004820 // Increase refcount on dictionary, it could get deleted when evaluating
4821 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004822 if (fudi.fd_dict != NULL)
4823 ++fudi.fd_dict->dv_refcount;
4824
Bram Moolenaare38eab22019-12-05 21:50:01 +01004825 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
4826 // contents. For VAR_PARTIAL get its partial, unless we already have one
4827 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004828 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004829 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
4830 in_vim9script() && type == NULL ? &type : NULL, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004831
Bram Moolenaare38eab22019-12-05 21:50:01 +01004832 // Skip white space to allow ":call func ()". Not good, but required for
4833 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004834 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004835 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004836
4837 if (*startarg != '(')
4838 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004840 goto end;
4841 }
4842
4843 /*
4844 * When skipping, evaluate the function once, to find the end of the
4845 * arguments.
4846 * When the function takes a range, this is discovered after the first
4847 * call, and the loop is broken.
4848 */
4849 if (eap->skip)
4850 {
4851 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004852 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004853 }
4854 else
4855 lnum = eap->line1;
4856 for ( ; lnum <= eap->line2; ++lnum)
4857 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004858 funcexe_T funcexe;
4859
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004860 if (!eap->skip && eap->addr_count > 0)
4861 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004862 if (lnum > curbuf->b_ml.ml_line_count)
4863 {
4864 // If the function deleted lines or switched to another buffer
4865 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004866 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004867 break;
4868 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004869 curwin->w_cursor.lnum = lnum;
4870 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004871 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004872 }
4873 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004874
Bram Moolenaara80faa82020-04-12 19:37:17 +02004875 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004876 funcexe.firstline = eap->line1;
4877 funcexe.lastline = eap->line2;
4878 funcexe.doesrange = &doesrange;
4879 funcexe.evaluate = !eap->skip;
4880 funcexe.partial = partial;
4881 funcexe.selfdict = fudi.fd_dict;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004882 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004883 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004884 {
4885 failed = TRUE;
4886 break;
4887 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01004888 if (has_watchexpr())
4889 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004890
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004891 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004892 if (handle_subscript(&arg, &rettv,
4893 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004894 {
4895 failed = TRUE;
4896 break;
4897 }
4898
4899 clear_tv(&rettv);
4900 if (doesrange || eap->skip)
4901 break;
4902
Bram Moolenaare38eab22019-12-05 21:50:01 +01004903 // Stop when immediately aborting on error, or when an interrupt
4904 // occurred or an exception was thrown but not caught.
4905 // get_func_tv() returned OK, so that the check for trailing
4906 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004907 if (aborting())
4908 break;
4909 }
4910 if (eap->skip)
4911 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004912 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004913
Bram Moolenaare51bb172020-02-16 19:42:23 +01004914 // When inside :try we need to check for following "| catch".
4915 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004916 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004917 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02004918 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004919 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004920 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004921 if (!failed)
4922 {
4923 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004924 semsg(_(e_trailing_arg), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004925 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004926 }
4927 else
4928 eap->nextcmd = check_nextcmd(arg);
4929 }
4930
4931end:
4932 dict_unref(fudi.fd_dict);
4933 vim_free(tofree);
4934}
4935
4936/*
4937 * Return from a function. Possibly makes the return pending. Also called
4938 * for a pending return at the ":endtry" or after returning from an extra
4939 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
4940 * when called due to a ":return" command. "rettv" may point to a typval_T
4941 * with the return rettv. Returns TRUE when the return can be carried out,
4942 * FALSE when the return gets pending.
4943 */
4944 int
4945do_return(
4946 exarg_T *eap,
4947 int reanimate,
4948 int is_cmd,
4949 void *rettv)
4950{
4951 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01004952 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004953
4954 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004955 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004956 current_funccal->returned = FALSE;
4957
4958 /*
4959 * Cleanup (and inactivate) conditionals, but stop when a try conditional
4960 * not in its finally clause (which then is to be executed next) is found.
4961 * In this case, make the ":return" pending for execution at the ":endtry".
4962 * Otherwise, return normally.
4963 */
4964 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
4965 if (idx >= 0)
4966 {
4967 cstack->cs_pending[idx] = CSTP_RETURN;
4968
4969 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004970 // A pending return again gets pending. "rettv" points to an
4971 // allocated variable with the rettv of the original ":return"'s
4972 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004973 cstack->cs_rettv[idx] = rettv;
4974 else
4975 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004976 // When undoing a return in order to make it pending, get the stored
4977 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004978 if (reanimate)
4979 rettv = current_funccal->rettv;
4980
4981 if (rettv != NULL)
4982 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004983 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004984 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
4985 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
4986 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004987 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004988 }
4989 else
4990 cstack->cs_rettv[idx] = NULL;
4991
4992 if (reanimate)
4993 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004994 // The pending return value could be overwritten by a ":return"
4995 // without argument in a finally clause; reset the default
4996 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004997 current_funccal->rettv->v_type = VAR_NUMBER;
4998 current_funccal->rettv->vval.v_number = 0;
4999 }
5000 }
5001 report_make_pending(CSTP_RETURN, rettv);
5002 }
5003 else
5004 {
5005 current_funccal->returned = TRUE;
5006
Bram Moolenaare38eab22019-12-05 21:50:01 +01005007 // If the return is carried out now, store the return value. For
5008 // a return immediately after reanimation, the value is already
5009 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005010 if (!reanimate && rettv != NULL)
5011 {
5012 clear_tv(current_funccal->rettv);
5013 *current_funccal->rettv = *(typval_T *)rettv;
5014 if (!is_cmd)
5015 vim_free(rettv);
5016 }
5017 }
5018
5019 return idx < 0;
5020}
5021
5022/*
5023 * Free the variable with a pending return value.
5024 */
5025 void
5026discard_pending_return(void *rettv)
5027{
5028 free_tv((typval_T *)rettv);
5029}
5030
5031/*
5032 * Generate a return command for producing the value of "rettv". The result
5033 * is an allocated string. Used by report_pending() for verbose messages.
5034 */
5035 char_u *
5036get_return_cmd(void *rettv)
5037{
5038 char_u *s = NULL;
5039 char_u *tofree = NULL;
5040 char_u numbuf[NUMBUFLEN];
5041
5042 if (rettv != NULL)
5043 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
5044 if (s == NULL)
5045 s = (char_u *)"";
5046
5047 STRCPY(IObuff, ":return ");
5048 STRNCPY(IObuff + 8, s, IOSIZE - 8);
5049 if (STRLEN(s) + 8 >= IOSIZE)
5050 STRCPY(IObuff + IOSIZE - 4, "...");
5051 vim_free(tofree);
5052 return vim_strsave(IObuff);
5053}
5054
5055/*
5056 * Get next function line.
5057 * Called by do_cmdline() to get the next line.
5058 * Returns allocated string, or NULL for end of function.
5059 */
5060 char_u *
5061get_func_line(
5062 int c UNUSED,
5063 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02005064 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005065 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005066{
5067 funccall_T *fcp = (funccall_T *)cookie;
5068 ufunc_T *fp = fcp->func;
5069 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005070 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005071
Bram Moolenaare38eab22019-12-05 21:50:01 +01005072 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005073 if (fcp->dbg_tick != debug_tick)
5074 {
5075 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005076 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005077 fcp->dbg_tick = debug_tick;
5078 }
5079#ifdef FEAT_PROFILE
5080 if (do_profiling == PROF_YES)
5081 func_line_end(cookie);
5082#endif
5083
5084 gap = &fp->uf_lines;
5085 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5086 || fcp->returned)
5087 retval = NULL;
5088 else
5089 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005090 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005091 while (fcp->linenr < gap->ga_len
5092 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
5093 ++fcp->linenr;
5094 if (fcp->linenr >= gap->ga_len)
5095 retval = NULL;
5096 else
5097 {
5098 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005099 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005100#ifdef FEAT_PROFILE
5101 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005102 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005103#endif
5104 }
5105 }
5106
Bram Moolenaare38eab22019-12-05 21:50:01 +01005107 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005108 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005109 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005110 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01005111 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005112 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005113 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005114 fcp->dbg_tick = debug_tick;
5115 }
5116
5117 return retval;
5118}
5119
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005120/*
5121 * Return TRUE if the currently active function should be ended, because a
5122 * return was encountered or an error occurred. Used inside a ":while".
5123 */
5124 int
5125func_has_ended(void *cookie)
5126{
5127 funccall_T *fcp = (funccall_T *)cookie;
5128
Bram Moolenaare38eab22019-12-05 21:50:01 +01005129 // Ignore the "abort" flag if the abortion behavior has been changed due to
5130 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005131 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5132 || fcp->returned);
5133}
5134
5135/*
5136 * return TRUE if cookie indicates a function which "abort"s on errors.
5137 */
5138 int
5139func_has_abort(
5140 void *cookie)
5141{
5142 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
5143}
5144
5145
5146/*
5147 * Turn "dict.Func" into a partial for "Func" bound to "dict".
5148 * Don't do this when "Func" is already a partial that was bound
5149 * explicitly (pt_auto is FALSE).
5150 * Changes "rettv" in-place.
5151 * Returns the updated "selfdict_in".
5152 */
5153 dict_T *
5154make_partial(dict_T *selfdict_in, typval_T *rettv)
5155{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005156 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005157 char_u *tofree = NULL;
5158 ufunc_T *fp;
5159 char_u fname_buf[FLEN_FIXED + 1];
5160 int error;
5161 dict_T *selfdict = selfdict_in;
5162
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005163 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
5164 fp = rettv->vval.v_partial->pt_func;
5165 else
5166 {
5167 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
5168 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005169 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005170 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005171 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005172 vim_free(tofree);
5173 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005174
5175 if (fp != NULL && (fp->uf_flags & FC_DICT))
5176 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005177 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005178
5179 if (pt != NULL)
5180 {
5181 pt->pt_refcount = 1;
5182 pt->pt_dict = selfdict;
5183 pt->pt_auto = TRUE;
5184 selfdict = NULL;
5185 if (rettv->v_type == VAR_FUNC)
5186 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005187 // Just a function: Take over the function name and use
5188 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005189 pt->pt_name = rettv->vval.v_string;
5190 }
5191 else
5192 {
5193 partial_T *ret_pt = rettv->vval.v_partial;
5194 int i;
5195
Bram Moolenaare38eab22019-12-05 21:50:01 +01005196 // Partial: copy the function name, use selfdict and copy
5197 // args. Can't take over name or args, the partial might
5198 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005199 if (ret_pt->pt_name != NULL)
5200 {
5201 pt->pt_name = vim_strsave(ret_pt->pt_name);
5202 func_ref(pt->pt_name);
5203 }
5204 else
5205 {
5206 pt->pt_func = ret_pt->pt_func;
5207 func_ptr_ref(pt->pt_func);
5208 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005209 if (ret_pt->pt_argc > 0)
5210 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005211 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005212 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005213 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005214 pt->pt_argc = 0;
5215 else
5216 {
5217 pt->pt_argc = ret_pt->pt_argc;
5218 for (i = 0; i < pt->pt_argc; i++)
5219 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
5220 }
5221 }
5222 partial_unref(ret_pt);
5223 }
5224 rettv->v_type = VAR_PARTIAL;
5225 rettv->vval.v_partial = pt;
5226 }
5227 }
5228 return selfdict;
5229}
5230
5231/*
5232 * Return the name of the executed function.
5233 */
5234 char_u *
5235func_name(void *cookie)
5236{
5237 return ((funccall_T *)cookie)->func->uf_name;
5238}
5239
5240/*
5241 * Return the address holding the next breakpoint line for a funccall cookie.
5242 */
5243 linenr_T *
5244func_breakpoint(void *cookie)
5245{
5246 return &((funccall_T *)cookie)->breakpoint;
5247}
5248
5249/*
5250 * Return the address holding the debug tick for a funccall cookie.
5251 */
5252 int *
5253func_dbg_tick(void *cookie)
5254{
5255 return &((funccall_T *)cookie)->dbg_tick;
5256}
5257
5258/*
5259 * Return the nesting level for a funccall cookie.
5260 */
5261 int
5262func_level(void *cookie)
5263{
5264 return ((funccall_T *)cookie)->level;
5265}
5266
5267/*
5268 * Return TRUE when a function was ended by a ":return" command.
5269 */
5270 int
5271current_func_returned(void)
5272{
5273 return current_funccal->returned;
5274}
5275
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005276 int
5277free_unref_funccal(int copyID, int testing)
5278{
5279 int did_free = FALSE;
5280 int did_free_funccal = FALSE;
5281 funccall_T *fc, **pfc;
5282
5283 for (pfc = &previous_funccal; *pfc != NULL; )
5284 {
5285 if (can_free_funccal(*pfc, copyID))
5286 {
5287 fc = *pfc;
5288 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01005289 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005290 did_free = TRUE;
5291 did_free_funccal = TRUE;
5292 }
5293 else
5294 pfc = &(*pfc)->caller;
5295 }
5296 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005297 // When a funccal was freed some more items might be garbage
5298 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005299 (void)garbage_collect(testing);
5300
5301 return did_free;
5302}
5303
5304/*
Bram Moolenaarba209902016-08-24 22:06:38 +02005305 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005306 */
5307 static funccall_T *
5308get_funccal(void)
5309{
5310 int i;
5311 funccall_T *funccal;
5312 funccall_T *temp_funccal;
5313
5314 funccal = current_funccal;
5315 if (debug_backtrace_level > 0)
5316 {
5317 for (i = 0; i < debug_backtrace_level; i++)
5318 {
5319 temp_funccal = funccal->caller;
5320 if (temp_funccal)
5321 funccal = temp_funccal;
5322 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005323 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005324 debug_backtrace_level = i;
5325 }
5326 }
5327 return funccal;
5328}
5329
5330/*
5331 * Return the hashtable used for local variables in the current funccal.
5332 * Return NULL if there is no current funccal.
5333 */
5334 hashtab_T *
5335get_funccal_local_ht()
5336{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005337 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005338 return NULL;
5339 return &get_funccal()->l_vars.dv_hashtab;
5340}
5341
5342/*
5343 * Return the l: scope variable.
5344 * Return NULL if there is no current funccal.
5345 */
5346 dictitem_T *
5347get_funccal_local_var()
5348{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005349 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005350 return NULL;
5351 return &get_funccal()->l_vars_var;
5352}
5353
5354/*
5355 * Return the hashtable used for argument in the current funccal.
5356 * Return NULL if there is no current funccal.
5357 */
5358 hashtab_T *
5359get_funccal_args_ht()
5360{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005362 return NULL;
5363 return &get_funccal()->l_avars.dv_hashtab;
5364}
5365
5366/*
5367 * Return the a: scope variable.
5368 * Return NULL if there is no current funccal.
5369 */
5370 dictitem_T *
5371get_funccal_args_var()
5372{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005374 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01005375 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005376}
5377
5378/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005379 * List function variables, if there is a function.
5380 */
5381 void
5382list_func_vars(int *first)
5383{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005384 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005385 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01005386 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005387}
5388
5389/*
5390 * If "ht" is the hashtable for local variables in the current funccal, return
5391 * the dict that contains it.
5392 * Otherwise return NULL.
5393 */
5394 dict_T *
5395get_current_funccal_dict(hashtab_T *ht)
5396{
5397 if (current_funccal != NULL
5398 && ht == &current_funccal->l_vars.dv_hashtab)
5399 return &current_funccal->l_vars;
5400 return NULL;
5401}
5402
5403/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005404 * Search hashitem in parent scope.
5405 */
5406 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005407find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005408{
5409 funccall_T *old_current_funccal = current_funccal;
5410 hashtab_T *ht;
5411 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005412 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005413
5414 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5415 return NULL;
5416
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02005417 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005418 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02005419 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005420 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005421 ht = find_var_ht(name, &varname);
5422 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02005423 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005424 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02005425 if (!HASHITEM_EMPTY(hi))
5426 {
5427 *pht = ht;
5428 break;
5429 }
5430 }
5431 if (current_funccal == current_funccal->func->uf_scoped)
5432 break;
5433 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005434 }
5435 current_funccal = old_current_funccal;
5436
5437 return hi;
5438}
5439
5440/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005441 * Search variable in parent scope.
5442 */
5443 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005444find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005445{
5446 dictitem_T *v = NULL;
5447 funccall_T *old_current_funccal = current_funccal;
5448 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005449 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005450
5451 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5452 return NULL;
5453
Bram Moolenaare38eab22019-12-05 21:50:01 +01005454 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005455 current_funccal = current_funccal->func->uf_scoped;
5456 while (current_funccal)
5457 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005458 ht = find_var_ht(name, &varname);
5459 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005460 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005461 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005462 if (v != NULL)
5463 break;
5464 }
5465 if (current_funccal == current_funccal->func->uf_scoped)
5466 break;
5467 current_funccal = current_funccal->func->uf_scoped;
5468 }
5469 current_funccal = old_current_funccal;
5470
5471 return v;
5472}
5473
5474/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005475 * Set "copyID + 1" in previous_funccal and callers.
5476 */
5477 int
5478set_ref_in_previous_funccal(int copyID)
5479{
5480 int abort = FALSE;
5481 funccall_T *fc;
5482
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005483 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005484 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005485 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005486 abort = abort
5487 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
5488 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005489 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005490 }
5491 return abort;
5492}
5493
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005494 static int
5495set_ref_in_funccal(funccall_T *fc, int copyID)
5496{
5497 int abort = FALSE;
5498
5499 if (fc->fc_copyID != copyID)
5500 {
5501 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005502 abort = abort
5503 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
5504 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005505 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005506 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005507 }
5508 return abort;
5509}
5510
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005511/*
5512 * Set "copyID" in all local vars and arguments in the call stack.
5513 */
5514 int
5515set_ref_in_call_stack(int copyID)
5516{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005517 int abort = FALSE;
5518 funccall_T *fc;
5519 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005520
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005521 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005522 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005523
5524 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005525 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
5526 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005527 abort = abort || set_ref_in_funccal(fc, copyID);
5528
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005529 return abort;
5530}
5531
5532/*
5533 * Set "copyID" in all functions available by name.
5534 */
5535 int
5536set_ref_in_functions(int copyID)
5537{
5538 int todo;
5539 hashitem_T *hi = NULL;
5540 int abort = FALSE;
5541 ufunc_T *fp;
5542
5543 todo = (int)func_hashtab.ht_used;
5544 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005545 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005546 if (!HASHITEM_EMPTY(hi))
5547 {
5548 --todo;
5549 fp = HI2UF(hi);
5550 if (!func_name_refcount(fp->uf_name))
5551 abort = abort || set_ref_in_func(NULL, fp, copyID);
5552 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005553 }
5554 return abort;
5555}
5556
5557/*
5558 * Set "copyID" in all function arguments.
5559 */
5560 int
5561set_ref_in_func_args(int copyID)
5562{
5563 int i;
5564 int abort = FALSE;
5565
5566 for (i = 0; i < funcargs.ga_len; ++i)
5567 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5568 copyID, NULL, NULL);
5569 return abort;
5570}
5571
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005572/*
5573 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005574 * Returns TRUE if setting references failed somehow.
5575 */
5576 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005577set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005578{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005579 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005580 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005581 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005582 char_u fname_buf[FLEN_FIXED + 1];
5583 char_u *tofree = NULL;
5584 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005585 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005586
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005587 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005588 return FALSE;
5589
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005590 if (fp_in == NULL)
5591 {
5592 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005593 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005594 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005595 if (fp != NULL)
5596 {
5597 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005598 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005599 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005600
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005601 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005602 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005603}
5604
Bram Moolenaare38eab22019-12-05 21:50:01 +01005605#endif // FEAT_EVAL