blob: 4793ee555285c7c50c4c7a5c9d688582aaf396f8 [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 Moolenaara9b579f2016-07-17 18:29:19 +020038
39 void
40func_init()
41{
42 hash_init(&func_hashtab);
43}
44
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020045/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020046 * Return the function hash table
47 */
48 hashtab_T *
49func_tbl_get(void)
50{
51 return &func_hashtab;
52}
53
54/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020055 * Get one function argument.
56 * If "argtypes" is not NULL also get the type: "arg: type".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010057 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010058 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010059 * Return a pointer to after the type.
60 * When something is wrong return "arg".
61 */
62 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010063one_function_arg(
64 char_u *arg,
65 garray_T *newargs,
66 garray_T *argtypes,
67 int types_optional,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010068 evalarg_T *evalarg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010069 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010070{
Bram Moolenaar6e949782020-04-13 17:21:00 +020071 char_u *p = arg;
72 char_u *arg_copy = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073
74 while (ASCII_ISALNUM(*p) || *p == '_')
75 ++p;
76 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020077 || (argtypes == NULL
78 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
79 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010080 {
81 if (!skip)
82 semsg(_("E125: Illegal argument: %s"), arg);
83 return arg;
84 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010085
Bram Moolenaar057e84a2021-02-28 16:55:11 +010086 // Vim9 script: cannot use script var name for argument. In function: also
87 // check local vars and arguments.
88 if (!skip && argtypes != NULL && check_defined(arg, p - arg,
89 evalarg == NULL ? NULL : evalarg->eval_cctx, TRUE) == FAIL)
Bram Moolenaarb4893b82021-02-21 22:20:24 +010090 return arg;
Bram Moolenaarb4893b82021-02-21 22:20:24 +010091
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
93 return arg;
94 if (newargs != NULL)
95 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 int c;
97 int i;
98
99 c = *p;
100 *p = NUL;
101 arg_copy = vim_strsave(arg);
102 if (arg_copy == NULL)
103 {
104 *p = c;
105 return arg;
106 }
107
108 // Check for duplicate argument name.
109 for (i = 0; i < newargs->ga_len; ++i)
110 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
111 {
112 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
113 vim_free(arg_copy);
114 return arg;
115 }
116 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
117 newargs->ga_len++;
118
119 *p = c;
120 }
121
122 // get any type from "arg: type"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100123 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 {
125 char_u *type = NULL;
126
Bram Moolenaar6e949782020-04-13 17:21:00 +0200127 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
128 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200129 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200130 arg_copy == NULL ? arg : arg_copy);
131 p = skipwhite(p);
132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 if (*p == ':')
134 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200135 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100136 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200137 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100138 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200139 return arg;
140 }
141 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200142 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100143 if (!skip)
144 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100146 else if (*skipwhite(p) != '=' && !types_optional)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200147 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200148 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200149 arg_copy == NULL ? arg : arg_copy);
150 return arg;
151 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100152 if (!skip)
153 {
154 if (type == NULL && types_optional)
155 // lambda arguments default to "any" type
156 type = vim_strsave((char_u *)"any");
157 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159 }
160
161 return p;
162}
163
164/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200165 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100166 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100167 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200168 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100169 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200170get_function_args(
171 char_u **argp,
172 char_u endchar,
173 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100175 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100176 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200177 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200178 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200179 int skip,
180 exarg_T *eap,
181 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200182{
183 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100184 char_u *arg;
185 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200186 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200187 int any_default = FALSE;
188 char_u *expr;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100189 char_u *whitep = *argp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200190
191 if (newargs != NULL)
192 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100193 if (argtypes != NULL)
194 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200195 if (default_args != NULL)
196 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200197
198 if (varargs != NULL)
199 *varargs = FALSE;
200
201 /*
202 * Isolate the arguments: "arg1, arg2, ...)"
203 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100204 arg = skipwhite(*argp);
205 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200206 while (*p != endchar)
207 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200208 while (eap != NULL && eap->getline != NULL
209 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200210 {
211 char_u *theline;
212
213 // End of the line, get the next one.
214 theline = eap->getline(':', eap->cookie, 0, TRUE);
215 if (theline == NULL)
216 break;
217 vim_free(*line_to_free);
218 *line_to_free = theline;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200219 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200220 p = skipwhite(theline);
221 }
222
223 if (mustend && *p != endchar)
224 {
225 if (!skip)
226 semsg(_(e_invarg2), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100227 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200228 }
229 if (*p == endchar)
230 break;
231
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200232 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
233 {
234 if (varargs != NULL)
235 *varargs = TRUE;
236 p += 3;
237 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238
239 if (argtypes != NULL)
240 {
241 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200242 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100243 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100244 if (!skip)
245 emsg(_(e_missing_name_after_dots));
246 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 }
248
249 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100250 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100251 evalarg, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252 if (p == arg)
253 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100254 if (*skipwhite(p) == '=')
255 {
256 emsg(_(e_cannot_use_default_for_variable_arguments));
257 break;
258 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200260 }
261 else
262 {
263 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100264 p = one_function_arg(p, newargs, argtypes, types_optional,
265 evalarg, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200267 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200268
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200269 if (*skipwhite(p) == '=' && default_args != NULL)
270 {
271 typval_T rettv;
272
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100273 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200274 any_default = TRUE;
275 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200276 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200277 p = skipwhite(p);
278 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200279 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200280 {
281 if (ga_grow(default_args, 1) == FAIL)
282 goto err_ret;
283
284 // trim trailing whitespace
285 while (p > expr && VIM_ISWHITE(p[-1]))
286 p--;
287 c = *p;
288 *p = NUL;
289 expr = vim_strsave(expr);
290 if (expr == NULL)
291 {
292 *p = c;
293 goto err_ret;
294 }
295 ((char_u **)(default_args->ga_data))
296 [default_args->ga_len] = expr;
297 default_args->ga_len++;
298 *p = c;
299 }
300 else
301 mustend = TRUE;
302 }
303 else if (any_default)
304 {
305 emsg(_("E989: Non-default argument follows default argument"));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200306 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200307 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200308 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200309 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200310 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200311 // Don't give this error when skipping, it makes the "->" not
312 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100313 // Allow missing space after comma in legacy functions.
314 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200315 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
316 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100317 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200318 goto err_ret;
319 }
320 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200321 else
322 mustend = TRUE;
323 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200324 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200325 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200326 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200327
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200328 if (*p != endchar)
329 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100330 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200331
332 *argp = p;
333 return OK;
334
335err_ret:
336 if (newargs != NULL)
337 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200338 if (default_args != NULL)
339 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200340 return FAIL;
341}
342
343/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100344 * Parse the argument types, filling "fp->uf_arg_types".
345 * Return OK or FAIL.
346 */
347 static int
348parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
349{
350 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
351 if (argtypes->ga_len > 0)
352 {
353 // When "varargs" is set the last name/type goes into uf_va_name
354 // and uf_va_type.
355 int len = argtypes->ga_len - (varargs ? 1 : 0);
356
357 if (len > 0)
358 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
359 if (fp->uf_arg_types != NULL)
360 {
361 int i;
362 type_T *type;
363
364 for (i = 0; i < len; ++ i)
365 {
366 char_u *p = ((char_u **)argtypes->ga_data)[i];
367
368 if (p == NULL)
369 // will get the type from the default value
370 type = &t_unknown;
371 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100372 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100373 if (type == NULL)
374 return FAIL;
375 fp->uf_arg_types[i] = type;
376 }
377 }
378 if (varargs)
379 {
380 char_u *p;
381
382 // Move the last argument "...name: type" to uf_va_name and
383 // uf_va_type.
384 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
385 [fp->uf_args.ga_len - 1];
386 --fp->uf_args.ga_len;
387 p = ((char_u **)argtypes->ga_data)[len];
388 if (p == NULL)
389 // todo: get type from default value
390 fp->uf_va_type = &t_any;
391 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100392 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100393 if (fp->uf_va_type == NULL)
394 return FAIL;
395 }
396 }
397 return OK;
398}
399
400/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200401 * Register function "fp" as using "current_funccal" as its scope.
402 */
403 static int
404register_closure(ufunc_T *fp)
405{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200406 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100407 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200408 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200409 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200410 fp->uf_scoped = current_funccal;
411 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200412
Bram Moolenaar58016442016-07-31 18:30:22 +0200413 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
414 return FAIL;
415 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
416 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200417 return OK;
418}
419
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100420 static void
421set_ufunc_name(ufunc_T *fp, char_u *name)
422{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100423 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
424 // actually extends beyond the struct.
425 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100426
427 if (name[0] == K_SPECIAL)
428 {
429 fp->uf_name_exp = alloc(STRLEN(name) + 3);
430 if (fp->uf_name_exp != NULL)
431 {
432 STRCPY(fp->uf_name_exp, "<SNR>");
433 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
434 }
435 }
436}
437
Bram Moolenaar58016442016-07-31 18:30:22 +0200438/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200439 * Get a name for a lambda. Returned in static memory.
440 */
441 char_u *
442get_lambda_name(void)
443{
444 static char_u name[30];
445 static int lambda_no = 0;
446
447 sprintf((char*)name, "<lambda>%d", ++lambda_no);
448 return name;
449}
450
Bram Moolenaar801ab062020-06-25 19:27:56 +0200451#if defined(FEAT_LUA) || defined(PROTO)
452/*
453 * Registers a native C callback which can be called from Vim script.
454 * Returns the name of the Vim script function.
455 */
456 char_u *
457register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
458{
459 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200460 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200461
462 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
463 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200464 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200465
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200466 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200467 fp->uf_refcount = 1;
468 fp->uf_varargs = TRUE;
469 fp->uf_flags = FC_CFUNC;
470 fp->uf_calls = 0;
471 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200472 fp->uf_cb = cb;
473 fp->uf_cb_free = cb_free;
474 fp->uf_cb_state = state;
475
476 set_ufunc_name(fp, name);
477 hash_add(&func_hashtab, UF2HIKEY(fp));
478
479 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200480}
481#endif
482
Bram Moolenaar04b12692020-05-04 23:24:44 +0200483/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100484 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100485 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100486 * If "white_error" is not NULL check for correct use of white space and set
487 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100488 * Return NULL if no valid arrow found.
489 */
490 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100491skip_arrow(
492 char_u *start,
493 int equal_arrow,
494 char_u **ret_type,
495 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100496{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100497 char_u *s = start;
498 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100499
500 if (equal_arrow)
501 {
502 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100503 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100504 if (white_error != NULL && !VIM_ISWHITE(s[1]))
505 {
506 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100507 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100508 return NULL;
509 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100510 s = skipwhite(s + 1);
511 *ret_type = s;
512 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100513 if (s == *ret_type)
514 {
515 emsg(_(e_missing_return_type));
516 return NULL;
517 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100518 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100519 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100520 s = skipwhite(s);
521 if (*s != '=')
522 return NULL;
523 ++s;
524 }
525 if (*s != '>')
526 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100527 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
528 || !IS_WHITE_OR_NUL(s[1])))
529 {
530 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100531 semsg(_(e_white_space_required_before_and_after_str_at_str),
532 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100533 return NULL;
534 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100535 return skipwhite(s + 1);
536}
537
538/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200539 * Parse a lambda expression and get a Funcref from "*arg".
Bram Moolenaar65c44152020-12-24 15:14:01 +0100540 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100541 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200542 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
543 */
544 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100545get_lambda_tv(
546 char_u **arg,
547 typval_T *rettv,
548 int types_optional,
549 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200550{
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200551 int evaluate = evalarg != NULL
552 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200553 garray_T newargs;
554 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200555 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100556 garray_T argtypes;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200557 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100558 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200559 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100560 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200561 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200562 char_u *s;
563 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200564 int *old_eval_lavars = eval_lavars_used;
565 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100566 char_u *tofree1 = NULL;
567 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100568 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100569 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +0100570 int called_emsg_start = called_emsg;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100571
572 if (equal_arrow && !in_vim9script())
573 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200574
575 ga_init(&newargs);
576 ga_init(&newlines);
577
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100578 // First, check if this is really a lambda expression. "->" or "=>" must
579 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100580 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100581 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100582 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100583 NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100584 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100585 {
586 if (types_optional)
587 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100588 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100589 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200590
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100591 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200592 if (evaluate)
593 pnewargs = &newargs;
594 else
595 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100596 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100597 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100598 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100599 &varargs, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100600 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100601 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaare462f522020-12-27 14:43:30 +0100602 equal_arrow || in_vim9script() ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100603 {
604 if (types_optional)
605 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +0100606 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100607 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100608 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100609 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200610
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100611 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
612 if (ret_type != NULL)
613 {
614 ret_type = vim_strsave(ret_type);
615 tofree2 = ret_type;
616 }
617
Bram Moolenaare38eab22019-12-05 21:50:01 +0100618 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200619 if (evaluate)
620 eval_lavars_used = &eval_lavars;
621
Bram Moolenaar65c44152020-12-24 15:14:01 +0100622 *arg = skipwhite_and_linebreak(*arg, evalarg);
623
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100624 // Recognize "{" as the start of a function body.
625 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +0100626 {
627 // TODO: process the function body upto the "}".
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100628 // Return type is required then.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100629 emsg("Lambda function body not supported yet");
630 goto errret;
631 }
632
Bram Moolenaare38eab22019-12-05 21:50:01 +0100633 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200634 start = *arg;
635 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200636 if (ret == FAIL)
637 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200638 if (evalarg != NULL)
639 {
640 // avoid that the expression gets freed when another line break follows
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100641 tofree1 = evalarg->eval_tofree;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200642 evalarg->eval_tofree = NULL;
643 }
644
Bram Moolenaar65c44152020-12-24 15:14:01 +0100645 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +0100646 {
Bram Moolenaar65c44152020-12-24 15:14:01 +0100647 *arg = skipwhite_and_linebreak(*arg, evalarg);
648 if (**arg != '}')
649 {
650 semsg(_("E451: Expected }: %s"), *arg);
651 goto errret;
652 }
653 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100654 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200655
656 if (evaluate)
657 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200658 int len;
659 int flags = 0;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200660 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200661 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200662
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200663 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200664 if (fp == NULL)
665 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200666 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200667 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200668 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200669 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200670
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200671 ga_init2(&newlines, (int)sizeof(char_u *), 1);
672 if (ga_grow(&newlines, 1) == FAIL)
673 goto errret;
674
Bram Moolenaare38eab22019-12-05 21:50:01 +0100675 // Add "return " before the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200676 len = 7 + (int)(end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200677 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200678 if (p == NULL)
679 goto errret;
680 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
681 STRCPY(p, "return ");
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200682 vim_strncpy(p + 7, start, end - start);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200683 if (strstr((char *)p + 7, "a:") == NULL)
684 // No a: variables are used for sure.
685 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200686
687 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100688 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200689 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200690 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100691 if (types_optional)
692 {
693 if (parse_argument_types(fp, &argtypes, FALSE) == FAIL)
694 goto errret;
695 if (ret_type != NULL)
696 {
697 fp->uf_ret_type = parse_type(&ret_type,
698 &fp->uf_type_list, TRUE);
699 if (fp->uf_ret_type == NULL)
700 goto errret;
701 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +0100702 else
Bram Moolenaar328eac22021-01-07 19:23:08 +0100703 fp->uf_ret_type = &t_any;
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100704 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100705
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200706 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200707 if (current_funccal != NULL && eval_lavars)
708 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200709 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200710 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200711 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200712 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200713
714#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200715 if (prof_def_func())
716 func_do_profile(fp);
717#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200718 if (sandbox)
719 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200721 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200722 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200723 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200724 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100725 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200726
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200727 pt->pt_func = fp;
728 pt->pt_refcount = 1;
729 rettv->vval.v_partial = pt;
730 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100731
732 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200733 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200734
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200735 eval_lavars_used = old_eval_lavars;
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200736 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100737 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200738 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100739 vim_free(tofree1);
740 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100741 if (types_optional)
742 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200743 return OK;
744
745errret:
746 ga_clear_strings(&newargs);
747 ga_clear_strings(&newlines);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100748 if (types_optional)
749 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200750 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100751 vim_free(pt);
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200752 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100753 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200754 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100755 vim_free(tofree1);
756 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200757 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200758 return FAIL;
759}
760
761/*
762 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
763 * name it contains, otherwise return "name".
764 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
765 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100766 * If "type" is not NULL and a Vim9 script-local variable is found look up the
767 * type of the variable.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200768 */
769 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100770deref_func_name(
771 char_u *name,
772 int *lenp,
773 partial_T **partialp,
774 type_T **type,
775 int no_autoload)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200776{
777 dictitem_T *v;
778 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100779 char_u *s = NULL;
780 hashtab_T *ht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200781
782 if (partialp != NULL)
783 *partialp = NULL;
784
785 cc = name[*lenp];
786 name[*lenp] = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100787 v = find_var(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200788 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100789 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200790 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100791 if (v->di_tv.v_type == VAR_FUNC)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200792 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100793 if (v->di_tv.vval.v_string == NULL)
794 {
795 *lenp = 0;
796 return (char_u *)""; // just in case
797 }
798 s = v->di_tv.vval.v_string;
799 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200800 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200801
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100802 if (v->di_tv.v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200803 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100804 partial_T *pt = v->di_tv.vval.v_partial;
805
806 if (pt == NULL)
807 {
808 *lenp = 0;
809 return (char_u *)""; // just in case
810 }
811 if (partialp != NULL)
812 *partialp = pt;
813 s = partial_name(pt);
814 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200815 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100816
817 if (s != NULL)
818 {
819 if (type != NULL && ht == get_script_local_ht())
820 {
821 svar_T *sv = find_typval_in_script(&v->di_tv);
822
823 if (sv != NULL)
824 *type = sv->sv_type;
825 }
826 return s;
827 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200828 }
829
830 return name;
831}
832
833/*
834 * Give an error message with a function name. Handle <SNR> things.
835 * "ermsg" is to be passed without translation, use N_() instead of _().
836 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100837 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200838emsg_funcname(char *ermsg, char_u *name)
839{
840 char_u *p;
841
842 if (*name == K_SPECIAL)
843 p = concat_str((char_u *)"<SNR>", name + 3);
844 else
845 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100846 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200847 if (p != name)
848 vim_free(p);
849}
850
851/*
852 * Allocate a variable for the result of a function.
853 * Return OK or FAIL.
854 */
855 int
856get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200857 char_u *name, // name of the function
858 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200859 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200860 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +0200861 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200862 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200863{
864 char_u *argp;
865 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100866 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
867 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200868 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200869
870 /*
871 * Get the arguments.
872 */
873 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200874 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
875 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200876 {
Bram Moolenaare6b53242020-07-01 17:28:33 +0200877 // skip the '(' or ',' and possibly line breaks
878 argp = skipwhite_and_linebreak(argp + 1, evalarg);
879
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200880 if (*argp == ')' || *argp == ',' || *argp == NUL)
881 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +0200882 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200883 {
884 ret = FAIL;
885 break;
886 }
887 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +0200888 // The comma should come right after the argument, but this wasn't
889 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200890 if (vim9script)
891 {
892 if (*argp != ',' && *skipwhite(argp) == ',')
893 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +0100894 semsg(_(e_no_white_space_allowed_before_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200895 ret = FAIL;
896 break;
897 }
898 }
899 else
Bram Moolenaar9d489562020-07-30 20:08:50 +0200900 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200901 if (*argp != ',')
902 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200903 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
904 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100905 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200906 ret = FAIL;
907 break;
908 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200909 }
Bram Moolenaare6b53242020-07-01 17:28:33 +0200910 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200911 if (*argp == ')')
912 ++argp;
913 else
914 ret = FAIL;
915
916 if (ret == OK)
917 {
918 int i = 0;
919
920 if (get_vim_var_nr(VV_TESTING))
921 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100922 // Prepare for calling test_garbagecollect_now(), need to know
923 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200924 if (funcargs.ga_itemsize == 0)
925 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
926 for (i = 0; i < argcount; ++i)
927 if (ga_grow(&funcargs, 1) == OK)
928 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
929 &argvars[i];
930 }
931
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200932 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200933
934 funcargs.ga_len -= i;
935 }
936 else if (!aborting())
937 {
938 if (argcount == MAX_FUNC_ARGS)
939 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
940 else
941 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
942 }
943
944 while (--argcount >= 0)
945 clear_tv(&argvars[argcount]);
946
Bram Moolenaar8294d492020-08-10 22:40:56 +0200947 if (in_vim9script())
948 *arg = argp;
949 else
950 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200951 return ret;
952}
953
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200954/*
955 * Return TRUE if "p" starts with "<SID>" or "s:".
956 * Only works if eval_fname_script() returned non-zero for "p"!
957 */
958 static int
959eval_fname_sid(char_u *p)
960{
961 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
962}
963
964/*
965 * In a script change <SID>name() and s:name() to K_SNR 123_name().
966 * Change <SNR>123_name() to K_SNR 123_name().
967 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
968 * (slow).
969 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100970 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200971fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
972{
973 int llen;
974 char_u *fname;
975 int i;
976
977 llen = eval_fname_script(name);
978 if (llen > 0)
979 {
980 fname_buf[0] = K_SPECIAL;
981 fname_buf[1] = KS_EXTRA;
982 fname_buf[2] = (int)KE_SNR;
983 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100984 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200985 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200986 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100987 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200988 else
989 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200990 sprintf((char *)fname_buf + 3, "%ld_",
991 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200992 i = (int)STRLEN(fname_buf);
993 }
994 }
995 if (i + STRLEN(name + llen) < FLEN_FIXED)
996 {
997 STRCPY(fname_buf + i, name + llen);
998 fname = fname_buf;
999 }
1000 else
1001 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001002 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001003 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +01001004 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001005 else
1006 {
1007 *tofree = fname;
1008 mch_memmove(fname, fname_buf, (size_t)i);
1009 STRCPY(fname + i, name + llen);
1010 }
1011 }
1012 }
1013 else
1014 fname = name;
1015 return fname;
1016}
1017
1018/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 * Find a function "name" in script "sid".
1020 */
1021 static ufunc_T *
1022find_func_with_sid(char_u *name, int sid)
1023{
1024 hashitem_T *hi;
1025 char_u buffer[200];
1026
1027 buffer[0] = K_SPECIAL;
1028 buffer[1] = KS_EXTRA;
1029 buffer[2] = (int)KE_SNR;
1030 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
1031 (long)sid, name);
1032 hi = hash_find(&func_hashtab, buffer);
1033 if (!HASHITEM_EMPTY(hi))
1034 return HI2UF(hi);
1035
1036 return NULL;
1037}
1038
1039/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001040 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001041 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001042 * Return NULL for unknown function.
1043 */
Bram Moolenaarce658352020-07-31 23:47:12 +02001044 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001045find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001046{
1047 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048 ufunc_T *func;
1049 imported_T *imported;
1050
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001051 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 {
Bram Moolenaar333894b2020-08-01 18:53:07 +02001053 char_u *after_script = NULL;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001054 long sid = 0;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001055 int find_script_local = in_vim9script()
1056 && eval_isnamec1(*name) && name[1] != ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057
Bram Moolenaar95006e32020-08-29 17:47:08 +02001058 if (find_script_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001060 // Find script-local function before global one.
1061 func = find_func_with_sid(name, current_sctx.sc_sid);
1062 if (func != NULL)
1063 return func;
1064 }
1065
Bram Moolenaarefa94442020-08-08 22:16:00 +02001066 if (name[0] == K_SPECIAL
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001067 && name[1] == KS_EXTRA
1068 && name[2] == KE_SNR)
1069 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001070 // Caller changes s: to <SNR>99_name.
1071
1072 after_script = name + 3;
1073 sid = getdigits(&after_script);
Bram Moolenaarefa94442020-08-08 22:16:00 +02001074 if (*after_script == '_')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001075 ++after_script;
1076 else
1077 after_script = NULL;
1078 }
Bram Moolenaar95006e32020-08-29 17:47:08 +02001079 if (find_script_local || after_script != NULL)
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001080 {
1081 // Find imported function before global one.
Bram Moolenaarefa94442020-08-08 22:16:00 +02001082 if (after_script != NULL && sid != current_sctx.sc_sid)
1083 imported = find_imported_in_script(after_script, 0, sid);
1084 else
1085 imported = find_imported(after_script == NULL
1086 ? name : after_script, 0, cctx);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001087 if (imported != NULL && imported->imp_funcname != NULL)
1088 {
1089 hi = hash_find(&func_hashtab, imported->imp_funcname);
1090 if (!HASHITEM_EMPTY(hi))
1091 return HI2UF(hi);
1092 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093 }
1094 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001095
Bram Moolenaara26b9702020-04-18 19:53:28 +02001096 hi = hash_find(&func_hashtab,
1097 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001098 if (!HASHITEM_EMPTY(hi))
1099 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100
1101 return NULL;
1102}
1103
1104/*
1105 * Find a function by name, return pointer to it in ufuncs.
1106 * "cctx" is passed in a :def function to find imported functions.
1107 * Return NULL for unknown or dead function.
1108 */
1109 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001110find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001112 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113
1114 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
1115 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001116 return NULL;
1117}
1118
1119/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02001120 * Return TRUE if "ufunc" is a global function.
1121 */
1122 int
1123func_is_global(ufunc_T *ufunc)
1124{
1125 return ufunc->uf_name[0] != K_SPECIAL;
1126}
1127
1128/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001129 * Copy the function name of "fp" to buffer "buf".
1130 * "buf" must be able to hold the function name plus three bytes.
1131 * Takes care of script-local function names.
1132 */
1133 static void
1134cat_func_name(char_u *buf, ufunc_T *fp)
1135{
Bram Moolenaar0f769812020-09-12 18:32:34 +02001136 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001137 {
1138 STRCPY(buf, "<SNR>");
1139 STRCAT(buf, fp->uf_name + 3);
1140 }
1141 else
1142 STRCPY(buf, fp->uf_name);
1143}
1144
1145/*
1146 * Add a number variable "name" to dict "dp" with value "nr".
1147 */
1148 static void
1149add_nr_var(
1150 dict_T *dp,
1151 dictitem_T *v,
1152 char *name,
1153 varnumber_T nr)
1154{
1155 STRCPY(v->di_key, name);
1156 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1157 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
1158 v->di_tv.v_type = VAR_NUMBER;
1159 v->di_tv.v_lock = VAR_FIXED;
1160 v->di_tv.vval.v_number = nr;
1161}
1162
1163/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001164 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001165 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001166 static void
1167free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001168{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001169 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001170
1171 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1172 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001173 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001174
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001175 // When garbage collecting a funccall_T may be freed before the
1176 // function that references it, clear its uf_scoped field.
1177 // The function may have been redefined and point to another
1178 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001179 if (fp != NULL && fp->uf_scoped == fc)
1180 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001181 }
Bram Moolenaar58016442016-07-31 18:30:22 +02001182 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001183
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001184 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001185 vim_free(fc);
1186}
1187
1188/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001189 * Free "fc" and what it contains.
1190 * Can be called only when "fc" is kept beyond the period of it called,
1191 * i.e. after cleanup_function_call(fc).
1192 */
1193 static void
1194free_funccal_contents(funccall_T *fc)
1195{
1196 listitem_T *li;
1197
1198 // Free all l: variables.
1199 vars_clear(&fc->l_vars.dv_hashtab);
1200
1201 // Free all a: variables.
1202 vars_clear(&fc->l_avars.dv_hashtab);
1203
1204 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001205 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001206 clear_tv(&li->li_tv);
1207
1208 free_funccal(fc);
1209}
1210
1211/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02001212 * Handle the last part of returning from a function: free the local hashtable.
1213 * Unless it is still in use by a closure.
1214 */
1215 static void
1216cleanup_function_call(funccall_T *fc)
1217{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001218 int may_free_fc = fc->fc_refcount <= 0;
1219 int free_fc = TRUE;
1220
Bram Moolenaar6914c642017-04-01 21:21:30 +02001221 current_funccal = fc->caller;
1222
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001223 // Free all l: variables if not referred.
1224 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
1225 vars_clear(&fc->l_vars.dv_hashtab);
1226 else
1227 free_fc = FALSE;
1228
1229 // If the a:000 list and the l: and a: dicts are not referenced and
1230 // there is no closure using it, we can free the funccall_T and what's
1231 // in it.
1232 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
1233 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001234 else
1235 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001236 int todo;
1237 hashitem_T *hi;
1238 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001239
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001240 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001241
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001242 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001243 todo = (int)fc->l_avars.dv_hashtab.ht_used;
1244 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
1245 {
1246 if (!HASHITEM_EMPTY(hi))
1247 {
1248 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001249 di = HI2DI(hi);
1250 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001251 }
1252 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001253 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001254
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001255 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
1256 fc->l_varlist.lv_first = NULL;
1257 else
1258 {
1259 listitem_T *li;
1260
1261 free_fc = FALSE;
1262
1263 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001264 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02001265 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001266 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001267
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001268 if (free_fc)
1269 free_funccal(fc);
1270 else
1271 {
1272 static int made_copy = 0;
1273
1274 // "fc" is still in use. This can happen when returning "a:000",
1275 // assigning "l:" to a global variable or defining a closure.
1276 // Link "fc" in the list for garbage collection later.
1277 fc->caller = previous_funccal;
1278 previous_funccal = fc;
1279
1280 if (want_garbage_collect)
1281 // If garbage collector is ready, clear count.
1282 made_copy = 0;
1283 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001284 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001285 // We have made a lot of copies, worth 4 Mbyte. This can happen
1286 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001287 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001288 // too much memory.
1289 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001290 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001291 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001292 }
1293}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001294
1295/*
1296 * There are two kinds of function names:
1297 * 1. ordinary names, function defined with :function or :def
1298 * 2. numbered functions and lambdas
1299 * For the first we only count the name stored in func_hashtab as a reference,
1300 * using function() does not count as a reference, because the function is
1301 * looked up by name.
1302 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001303 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001304func_name_refcount(char_u *name)
1305{
1306 return isdigit(*name) || *name == '<';
1307}
1308
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309/*
1310 * Unreference "fc": decrement the reference count and free it when it
1311 * becomes zero. "fp" is detached from "fc".
1312 * When "force" is TRUE we are exiting.
1313 */
1314 static void
1315funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
1316{
1317 funccall_T **pfc;
1318 int i;
1319
1320 if (fc == NULL)
1321 return;
1322
1323 if (--fc->fc_refcount <= 0 && (force || (
1324 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
1325 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
1326 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
1327 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
1328 {
1329 if (fc == *pfc)
1330 {
1331 *pfc = fc->caller;
1332 free_funccal_contents(fc);
1333 return;
1334 }
1335 }
1336 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1337 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
1338 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
1339}
1340
1341/*
1342 * Remove the function from the function hashtable. If the function was
1343 * deleted while it still has references this was already done.
1344 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
1345 */
1346 static int
1347func_remove(ufunc_T *fp)
1348{
1349 hashitem_T *hi;
1350
1351 // Return if it was already virtually deleted.
1352 if (fp->uf_flags & FC_DEAD)
1353 return FALSE;
1354
1355 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1356 if (!HASHITEM_EMPTY(hi))
1357 {
1358 // When there is a def-function index do not actually remove the
1359 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001360 // Do remove it when it's a copy.
1361 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaarc970e422021-03-17 15:03:04 +01001362 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01001364 return FALSE;
1365 }
1366 hash_remove(&func_hashtab, hi);
1367 fp->uf_flags |= FC_DELETED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368 return TRUE;
1369 }
1370 return FALSE;
1371}
1372
1373 static void
1374func_clear_items(ufunc_T *fp)
1375{
1376 ga_clear_strings(&(fp->uf_args));
1377 ga_clear_strings(&(fp->uf_def_args));
1378 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001380 VIM_CLEAR(fp->uf_def_arg_idx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001381 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001382 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02001383 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001384
1385 // Increment the refcount of this function to avoid it being freed
1386 // recursively when the partial is freed.
1387 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001388 partial_unref(fp->uf_partial);
1389 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001390 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02001391
1392#ifdef FEAT_LUA
1393 if (fp->uf_cb_free != NULL)
1394 {
1395 fp->uf_cb_free(fp->uf_cb_state);
1396 fp->uf_cb_free = NULL;
1397 }
1398
1399 fp->uf_cb_state = NULL;
1400 fp->uf_cb = NULL;
1401#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402#ifdef FEAT_PROFILE
1403 VIM_CLEAR(fp->uf_tml_count);
1404 VIM_CLEAR(fp->uf_tml_total);
1405 VIM_CLEAR(fp->uf_tml_self);
1406#endif
1407}
1408
1409/*
1410 * Free all things that a function contains. Does not free the function
1411 * itself, use func_free() for that.
1412 * When "force" is TRUE we are exiting.
1413 */
1414 static void
1415func_clear(ufunc_T *fp, int force)
1416{
1417 if (fp->uf_cleared)
1418 return;
1419 fp->uf_cleared = TRUE;
1420
1421 // clear this function
1422 func_clear_items(fp);
1423 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001424 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425}
1426
1427/*
1428 * Free a function and remove it from the list of functions. Does not free
1429 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001430 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02001431 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001433 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001434func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435{
1436 // Only remove it when not done already, otherwise we would remove a newer
1437 // version of the function with the same name.
1438 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1439 func_remove(fp);
1440
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001441 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001442 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001443 if (fp->uf_dfunc_idx > 0)
1444 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001445 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02001447 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001448 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02001449 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450}
1451
1452/*
1453 * Free all things that a function contains and free the function itself.
1454 * When "force" is TRUE we are exiting.
1455 */
1456 static void
1457func_clear_free(ufunc_T *fp, int force)
1458{
1459 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001460 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
1461 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001462 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02001463 else
1464 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465}
1466
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001467/*
1468 * Copy already defined function "lambda" to a new function with name "global".
1469 * This is for when a compiled function defines a global function.
1470 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001471 int
1472copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001473{
1474 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01001475 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001476
1477 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001478 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001479 semsg(_(e_lambda_function_not_found_str), lambda);
1480 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001481 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001482
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001483 fp = find_func(global, TRUE, NULL);
1484 if (fp != NULL)
1485 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001486 // TODO: handle ! to overwrite
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001487 semsg(_(e_funcexts), global);
1488 return FAIL;
1489 }
1490
1491 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
1492 if (fp == NULL)
1493 return FAIL;
1494
1495 fp->uf_varargs = ufunc->uf_varargs;
1496 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
1497 fp->uf_def_status = ufunc->uf_def_status;
1498 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
1499 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
1500 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
1501 == FAIL
1502 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
1503 goto failed;
1504
1505 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
1506 : vim_strsave(ufunc->uf_name_exp);
1507 if (ufunc->uf_arg_types != NULL)
1508 {
1509 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
1510 if (fp->uf_arg_types == NULL)
1511 goto failed;
1512 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
1513 sizeof(type_T *) * fp->uf_args.ga_len);
1514 }
1515 if (ufunc->uf_def_arg_idx != NULL)
1516 {
1517 fp->uf_def_arg_idx = ALLOC_MULT(int, fp->uf_def_args.ga_len + 1);
1518 if (fp->uf_def_arg_idx == NULL)
1519 goto failed;
1520 mch_memmove(fp->uf_def_arg_idx, ufunc->uf_def_arg_idx,
1521 sizeof(int) * fp->uf_def_args.ga_len + 1);
1522 }
1523 if (ufunc->uf_va_name != NULL)
1524 {
1525 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
1526 if (fp->uf_va_name == NULL)
1527 goto failed;
1528 }
1529 fp->uf_ret_type = ufunc->uf_ret_type;
1530
1531 fp->uf_refcount = 1;
1532 STRCPY(fp->uf_name, global);
1533 hash_add(&func_hashtab, UF2HIKEY(fp));
1534
1535 // the referenced dfunc_T is now used one more time
1536 link_def_function(fp);
1537
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001538 // Create a partial to store the context of the function where it was
1539 // instantiated. Only needs to be done once. Do this on the original
1540 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001541 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
1542 {
1543 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
1544
1545 if (pt == NULL)
1546 goto failed;
1547 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001548 {
1549 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001550 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001551 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001552 ufunc->uf_partial = pt;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001553 --pt->pt_refcount; // not actually referenced here
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001554 }
1555
1556 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001557
1558failed:
1559 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001560 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001561}
1562
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001563static int funcdepth = 0;
1564
1565/*
1566 * Increment the function call depth count.
1567 * Return FAIL when going over 'maxfuncdepth'.
1568 * Otherwise return OK, must call funcdepth_decrement() later!
1569 */
1570 int
1571funcdepth_increment(void)
1572{
1573 if (funcdepth >= p_mfd)
1574 {
1575 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
1576 return FAIL;
1577 }
1578 ++funcdepth;
1579 return OK;
1580}
1581
1582 void
1583funcdepth_decrement(void)
1584{
1585 --funcdepth;
1586}
1587
1588/*
1589 * Get the current function call depth.
1590 */
1591 int
1592funcdepth_get(void)
1593{
1594 return funcdepth;
1595}
1596
1597/*
1598 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001599 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001600 * times as funcdepth_increment().
1601 */
1602 void
1603funcdepth_restore(int depth)
1604{
1605 funcdepth = depth;
1606}
Bram Moolenaar6914c642017-04-01 21:21:30 +02001607
1608/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001609 * Call a user function.
1610 */
1611 static void
1612call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001613 ufunc_T *fp, // pointer to function
1614 int argcount, // nr of args
1615 typval_T *argvars, // arguments
1616 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001617 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01001618 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001619{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001620 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001621 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001622 funccall_T *fc;
1623 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001624 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001625 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001626 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001627 int i;
1628 int ai;
1629 int islambda = FALSE;
1630 char_u numbuf[NUMBUFLEN];
1631 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001632#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001633 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001634#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001635 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001636
Bram Moolenaarb2049902021-01-24 12:53:53 +01001637#ifdef FEAT_PROFILE
1638 CLEAR_FIELD(profile_info);
1639#endif
1640
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001641 // If depth of calling is getting too high, don't execute the function.
1642 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001643 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001644 rettv->v_type = VAR_NUMBER;
1645 rettv->vval.v_number = -1;
1646 return;
1647 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001648
Bram Moolenaare38eab22019-12-05 21:50:01 +01001649 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001650
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001651 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001652 if (fc == NULL)
1653 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001654 fc->caller = current_funccal;
1655 current_funccal = fc;
1656 fc->func = fp;
1657 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001658 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001659 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001660 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1661 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001662 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001663 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001664 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001665
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001666 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01001668#ifdef FEAT_PROFILE
1669 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
1670#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001671 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01001672#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001673 if (do_profiling == PROF_YES)
1674 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01001675#endif
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001676 call_def_function(fp, argcount, argvars, funcexe->partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001677 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01001678#ifdef FEAT_PROFILE
1679 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01001680 || (caller != NULL && caller->uf_profiling)))
1681 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01001682#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 free_funccal(fc);
1685 return;
1686 }
1687
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001688 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1689 islambda = TRUE;
1690
1691 /*
1692 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1693 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1694 * each argument variable and saves a lot of time.
1695 */
1696 /*
1697 * Init l: variables.
1698 */
1699 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1700 if (selfdict != NULL)
1701 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001702 // Set l:self to "selfdict". Use "name" to avoid a warning from
1703 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001704 v = &fc->fixvar[fixvar_idx++].var;
1705 name = v->di_key;
1706 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001707 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001708 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1709 v->di_tv.v_type = VAR_DICT;
1710 v->di_tv.v_lock = 0;
1711 v->di_tv.vval.v_dict = selfdict;
1712 ++selfdict->dv_refcount;
1713 }
1714
1715 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001716 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001717 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001718 * Set a:000 to a list with room for the "..." arguments.
1719 */
1720 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001721 if ((fp->uf_flags & FC_NOARGS) == 0)
1722 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001723 (varnumber_T)(argcount >= fp->uf_args.ga_len
1724 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001725 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001726 if ((fp->uf_flags & FC_NOARGS) == 0)
1727 {
1728 // Use "name" to avoid a warning from some compiler that checks the
1729 // destination size.
1730 v = &fc->fixvar[fixvar_idx++].var;
1731 name = v->di_key;
1732 STRCPY(name, "000");
1733 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1734 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1735 v->di_tv.v_type = VAR_LIST;
1736 v->di_tv.v_lock = VAR_FIXED;
1737 v->di_tv.vval.v_list = &fc->l_varlist;
1738 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001739 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001740 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1741 fc->l_varlist.lv_lock = VAR_FIXED;
1742
1743 /*
1744 * Set a:firstline to "firstline" and a:lastline to "lastline".
1745 * Set a:name to named arguments.
1746 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001747 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001748 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001749 if ((fp->uf_flags & FC_NOARGS) == 0)
1750 {
1751 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001752 (varnumber_T)funcexe->firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001753 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001754 (varnumber_T)funcexe->lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001755 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001756 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001757 {
1758 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001759 typval_T def_rettv;
1760 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001761
1762 ai = i - fp->uf_args.ga_len;
1763 if (ai < 0)
1764 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001765 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001766 name = FUNCARG(fp, i);
1767 if (islambda)
1768 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001769
1770 // evaluate named argument default expression
1771 isdefault = ai + fp->uf_def_args.ga_len >= 0
1772 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1773 && argvars[i].vval.v_number == VVAL_NONE));
1774 if (isdefault)
1775 {
1776 char_u *default_expr = NULL;
1777 def_rettv.v_type = VAR_NUMBER;
1778 def_rettv.vval.v_number = -1;
1779
1780 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1781 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001782 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001783 {
1784 default_arg_err = 1;
1785 break;
1786 }
1787 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788 }
1789 else
1790 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001791 if ((fp->uf_flags & FC_NOARGS) != 0)
1792 // Bail out if no a: arguments used (in lambda).
1793 break;
1794
Bram Moolenaare38eab22019-12-05 21:50:01 +01001795 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001796 sprintf((char *)numbuf, "%d", ai + 1);
1797 name = numbuf;
1798 }
1799 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1800 {
1801 v = &fc->fixvar[fixvar_idx++].var;
1802 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001803 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001804 }
1805 else
1806 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001807 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001808 if (v == NULL)
1809 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001810 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001811 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001812
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001813 // Note: the values are copied directly to avoid alloc/free.
1814 // "argvars" must have VAR_FIXED for v_lock.
1815 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001816 v->di_tv.v_lock = VAR_FIXED;
1817
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001818 if (addlocal)
1819 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001820 // Named arguments should be accessed without the "a:" prefix in
1821 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001822 copy_tv(&v->di_tv, &v->di_tv);
1823 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001824 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001825 else
1826 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001827
1828 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1829 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001830 listitem_T *li = &fc->l_listitems[ai];
1831
1832 li->li_tv = argvars[i];
1833 li->li_tv.v_lock = VAR_FIXED;
1834 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001835 }
1836 }
1837
Bram Moolenaare38eab22019-12-05 21:50:01 +01001838 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001839 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001840
1841 if (fp->uf_flags & FC_SANDBOX)
1842 {
1843 using_sandbox = TRUE;
1844 ++sandbox;
1845 }
1846
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001847 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001848 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001849 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001850 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001851 ++no_wait_return;
1852 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001853
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001854 smsg(_("calling %s"), SOURCING_NAME);
1855 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001856 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001857 char_u buf[MSG_BUF_LEN];
1858 char_u numbuf2[NUMBUFLEN];
1859 char_u *tofree;
1860 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001861
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001862 msg_puts("(");
1863 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001864 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001865 if (i > 0)
1866 msg_puts(", ");
1867 if (argvars[i].v_type == VAR_NUMBER)
1868 msg_outnum((long)argvars[i].vval.v_number);
1869 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001870 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001871 // Do not want errors such as E724 here.
1872 ++emsg_off;
1873 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1874 --emsg_off;
1875 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001876 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001877 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001878 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001879 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1880 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001881 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001882 msg_puts((char *)s);
1883 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001884 }
1885 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001886 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001887 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001888 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001889 msg_puts("\n"); // don't overwrite this either
1890
1891 verbose_leave_scroll();
1892 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001893 }
1894#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001895 if (do_profiling == PROF_YES)
1896 profile_may_start_func(&profile_info, fp,
1897 fc->caller == NULL ? NULL : fc->caller->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001898#endif
1899
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001900 save_current_sctx = current_sctx;
1901 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001902 save_did_emsg = did_emsg;
1903 did_emsg = FALSE;
1904
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001905 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1906 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001907 else if (islambda)
1908 {
1909 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1910
1911 // A Lambda always has the command "return {expr}". It is much faster
1912 // to evaluate {expr} directly.
1913 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001914 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001915 --ex_nesting_level;
1916 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001917 else
1918 // call do_cmdline() to execute the lines
1919 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001920 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1921
1922 --RedrawingDisabled;
1923
Bram Moolenaare38eab22019-12-05 21:50:01 +01001924 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001925 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1926 {
1927 clear_tv(rettv);
1928 rettv->v_type = VAR_NUMBER;
1929 rettv->vval.v_number = -1;
1930 }
1931
1932#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001933 if (do_profiling == PROF_YES)
1934 {
1935 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
1936
1937 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
1938 profile_may_end_func(&profile_info, fp, caller);
1939 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001940#endif
1941
Bram Moolenaare38eab22019-12-05 21:50:01 +01001942 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001943 if (p_verbose >= 12)
1944 {
1945 ++no_wait_return;
1946 verbose_enter_scroll();
1947
1948 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001949 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001950 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001951 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001952 (long)fc->rettv->vval.v_number);
1953 else
1954 {
1955 char_u buf[MSG_BUF_LEN];
1956 char_u numbuf2[NUMBUFLEN];
1957 char_u *tofree;
1958 char_u *s;
1959
Bram Moolenaare38eab22019-12-05 21:50:01 +01001960 // The value may be very long. Skip the middle part, so that we
1961 // have some idea how it starts and ends. smsg() would always
1962 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001963 ++emsg_off;
1964 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1965 --emsg_off;
1966 if (s != NULL)
1967 {
1968 if (vim_strsize(s) > MSG_BUF_CLEN)
1969 {
1970 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1971 s = buf;
1972 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001973 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001974 vim_free(tofree);
1975 }
1976 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001977 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001978
1979 verbose_leave_scroll();
1980 --no_wait_return;
1981 }
1982
Bram Moolenaare31ee862020-01-07 20:59:34 +01001983 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001984 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001985 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001986#ifdef FEAT_PROFILE
1987 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001988 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001989#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001990 if (using_sandbox)
1991 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001992
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001993 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001994 {
1995 ++no_wait_return;
1996 verbose_enter_scroll();
1997
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001998 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001999 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002000
2001 verbose_leave_scroll();
2002 --no_wait_return;
2003 }
2004
2005 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002006 funcdepth_decrement();
Bram Moolenaar6914c642017-04-01 21:21:30 +02002007 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002008}
2009
2010/*
Bram Moolenaar50824712020-12-20 21:10:17 +01002011 * Check the argument count for user function "fp".
2012 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
2013 */
2014 int
2015check_user_func_argcount(ufunc_T *fp, int argcount)
2016{
2017 int regular_args = fp->uf_args.ga_len;
2018
2019 if (argcount < regular_args - fp->uf_def_args.ga_len)
2020 return FCERR_TOOFEW;
2021 else if (!has_varargs(fp) && argcount > regular_args)
2022 return FCERR_TOOMANY;
2023 return FCERR_UNKNOWN;
2024}
2025
2026/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002028 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 int
2030call_user_func_check(
2031 ufunc_T *fp,
2032 int argcount,
2033 typval_T *argvars,
2034 typval_T *rettv,
2035 funcexe_T *funcexe,
2036 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002037{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002039
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
2041 *funcexe->doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01002042 error = check_user_func_argcount(fp, argcount);
2043 if (error != FCERR_UNKNOWN)
2044 return error;
2045 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 error = FCERR_DICT;
2047 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002048 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049 int did_save_redo = FALSE;
2050 save_redo_T save_redo;
2051
2052 /*
2053 * Call the user function.
2054 * Save and restore search patterns, script variables and
2055 * redo buffer.
2056 */
2057 save_search_patterns();
2058 if (!ins_compl_active())
2059 {
2060 saveRedobuff(&save_redo);
2061 did_save_redo = TRUE;
2062 }
2063 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002064 call_user_func(fp, argcount, argvars, rettv, funcexe,
2065 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
2067 // Function was unreferenced while being used, free it now.
2068 func_clear_free(fp, FALSE);
2069 if (did_save_redo)
2070 restoreRedobuff(&save_redo);
2071 restore_search_patterns();
2072 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002075}
2076
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002077static funccal_entry_T *funccal_stack = NULL;
2078
2079/*
2080 * Save the current function call pointer, and set it to NULL.
2081 * Used when executing autocommands and for ":source".
2082 */
2083 void
2084save_funccal(funccal_entry_T *entry)
2085{
2086 entry->top_funccal = current_funccal;
2087 entry->next = funccal_stack;
2088 funccal_stack = entry;
2089 current_funccal = NULL;
2090}
2091
2092 void
2093restore_funccal(void)
2094{
2095 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002096 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002097 else
2098 {
2099 current_funccal = funccal_stack->top_funccal;
2100 funccal_stack = funccal_stack->next;
2101 }
2102}
2103
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02002104 funccall_T *
2105get_current_funccal(void)
2106{
2107 return current_funccal;
2108}
2109
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002110/*
2111 * Mark all functions of script "sid" as deleted.
2112 */
2113 void
2114delete_script_functions(int sid)
2115{
2116 hashitem_T *hi;
2117 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02002118 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002119 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002120 size_t len;
2121
2122 buf[0] = K_SPECIAL;
2123 buf[1] = KS_EXTRA;
2124 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002125 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002126 len = STRLEN(buf);
2127
Bram Moolenaarce658352020-07-31 23:47:12 +02002128 while (todo > 0)
2129 {
2130 todo = func_hashtab.ht_used;
2131 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2132 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002133 {
Bram Moolenaarce658352020-07-31 23:47:12 +02002134 fp = HI2UF(hi);
2135 if (STRNCMP(fp->uf_name, buf, len) == 0)
2136 {
2137 int changed = func_hashtab.ht_changed;
2138
2139 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002140
2141 if (fp->uf_calls > 0)
2142 {
2143 // Function is executing, don't free it but do remove
2144 // it from the hashtable.
2145 if (func_remove(fp))
2146 fp->uf_refcount--;
2147 }
2148 else
2149 {
2150 func_clear(fp, TRUE);
2151 // When clearing a function another function can be
2152 // cleared as a side effect. When that happens start
2153 // over.
2154 if (changed != func_hashtab.ht_changed)
2155 break;
2156 }
Bram Moolenaarce658352020-07-31 23:47:12 +02002157 }
2158 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002159 }
Bram Moolenaarce658352020-07-31 23:47:12 +02002160 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002161}
2162
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002163#if defined(EXITFREE) || defined(PROTO)
2164 void
2165free_all_functions(void)
2166{
2167 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002168 ufunc_T *fp;
2169 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002170 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002171 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002172
Bram Moolenaare38eab22019-12-05 21:50:01 +01002173 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002174 while (current_funccal != NULL)
2175 {
2176 clear_tv(current_funccal->rettv);
2177 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002178 if (current_funccal == NULL && funccal_stack != NULL)
2179 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02002180 }
2181
Bram Moolenaare38eab22019-12-05 21:50:01 +01002182 // First clear what the functions contain. Since this may lower the
2183 // reference count of a function, it may also free a function and change
2184 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002185 while (todo > 0)
2186 {
2187 todo = func_hashtab.ht_used;
2188 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2189 if (!HASHITEM_EMPTY(hi))
2190 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 // clear the def function index now
2192 fp = HI2UF(hi);
2193 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002194 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195
Bram Moolenaare38eab22019-12-05 21:50:01 +01002196 // Only free functions that are not refcounted, those are
2197 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002198 if (func_name_refcount(fp->uf_name))
2199 ++skipped;
2200 else
2201 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002202 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002203 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002204 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002205 {
2206 skipped = 0;
2207 break;
2208 }
2209 }
2210 --todo;
2211 }
2212 }
2213
Bram Moolenaare38eab22019-12-05 21:50:01 +01002214 // Now actually free the functions. Need to start all over every time,
2215 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002216 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002217 while (func_hashtab.ht_used > skipped)
2218 {
2219 todo = func_hashtab.ht_used;
2220 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002221 if (!HASHITEM_EMPTY(hi))
2222 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02002223 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002224 // Only free functions that are not refcounted, those are
2225 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02002226 fp = HI2UF(hi);
2227 if (func_name_refcount(fp->uf_name))
2228 ++skipped;
2229 else
2230 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02002231 if (func_free(fp, FALSE) == OK)
2232 {
2233 skipped = 0;
2234 break;
2235 }
2236 // did not actually free it
2237 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002238 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002239 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02002240 }
2241 if (skipped == 0)
2242 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243
2244 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002245}
2246#endif
2247
2248/*
2249 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02002250 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002251 * "len" is the length of "name", or -1 for NUL terminated.
2252 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002254builtin_function(char_u *name, int len)
2255{
2256 char_u *p;
2257
Bram Moolenaara26b9702020-04-18 19:53:28 +02002258 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002259 return FALSE;
2260 p = vim_strchr(name, AUTOLOAD_CHAR);
2261 return p == NULL || (len > 0 && p > name + len);
2262}
2263
2264 int
2265func_call(
2266 char_u *name,
2267 typval_T *args,
2268 partial_T *partial,
2269 dict_T *selfdict,
2270 typval_T *rettv)
2271{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002272 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002273 listitem_T *item;
2274 typval_T argv[MAX_FUNC_ARGS + 1];
2275 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002276 int r = 0;
2277
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002278 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002279 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002280 {
2281 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
2282 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002283 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002284 break;
2285 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002286 // Make a copy of each argument. This is needed to be able to set
2287 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002288 copy_tv(&item->li_tv, &argv[argc++]);
2289 }
2290
2291 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002292 {
2293 funcexe_T funcexe;
2294
Bram Moolenaara80faa82020-04-12 19:37:17 +02002295 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002296 funcexe.firstline = curwin->w_cursor.lnum;
2297 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002298 funcexe.evaluate = TRUE;
2299 funcexe.partial = partial;
2300 funcexe.selfdict = selfdict;
2301 r = call_func(name, -1, rettv, argc, argv, &funcexe);
2302 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002303
Bram Moolenaare38eab22019-12-05 21:50:01 +01002304 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002305 while (argc > 0)
2306 clear_tv(&argv[--argc]);
2307
2308 return r;
2309}
2310
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002311static int callback_depth = 0;
2312
2313 int
2314get_callback_depth(void)
2315{
2316 return callback_depth;
2317}
2318
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002319/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002320 * Invoke call_func() with a callback.
2321 */
2322 int
2323call_callback(
2324 callback_T *callback,
2325 int len, // length of "name" or -1 to use strlen()
2326 typval_T *rettv, // return value goes here
2327 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002328 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002329 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002330{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002331 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002332 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002333
Bram Moolenaara80faa82020-04-12 19:37:17 +02002334 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002335 funcexe.evaluate = TRUE;
2336 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002337 ++callback_depth;
2338 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
2339 --callback_depth;
2340 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002341}
2342
2343/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 * Give an error message for the result of a function.
2345 * Nothing if "error" is FCERR_NONE.
2346 */
2347 void
2348user_func_error(int error, char_u *name)
2349{
2350 switch (error)
2351 {
2352 case FCERR_UNKNOWN:
2353 emsg_funcname(e_unknownfunc, name);
2354 break;
2355 case FCERR_NOTMETHOD:
2356 emsg_funcname(
2357 N_("E276: Cannot use function as a method: %s"), name);
2358 break;
2359 case FCERR_DELETED:
2360 emsg_funcname(N_(e_func_deleted), name);
2361 break;
2362 case FCERR_TOOMANY:
2363 emsg_funcname((char *)e_toomanyarg, name);
2364 break;
2365 case FCERR_TOOFEW:
2366 emsg_funcname((char *)e_toofewarg, name);
2367 break;
2368 case FCERR_SCRIPT:
2369 emsg_funcname(
2370 N_("E120: Using <SID> not in a script context: %s"), name);
2371 break;
2372 case FCERR_DICT:
2373 emsg_funcname(
2374 N_("E725: Calling dict function without Dictionary: %s"),
2375 name);
2376 break;
2377 }
2378}
2379
2380/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002381 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002382 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002383 * Return FAIL when the function can't be called, OK otherwise.
2384 * Also returns OK when an error was encountered while executing the function.
2385 */
2386 int
2387call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02002388 char_u *funcname, // name of the function
2389 int len, // length of "name" or -1 to use strlen()
2390 typval_T *rettv, // return value goes here
2391 int argcount_in, // number of "argvars"
2392 typval_T *argvars_in, // vars for arguments, must have "argcount"
2393 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002394 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002395{
2396 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01002397 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002398 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002399 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002400 char_u fname_buf[FLEN_FIXED + 1];
2401 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002402 char_u *fname = NULL;
2403 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002404 int argcount = argcount_in;
2405 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002406 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002407 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
2408 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002409 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002410 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002411 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002412
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02002413 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
2414 // even when call_func() returns FAIL.
2415 rettv->v_type = VAR_UNKNOWN;
2416
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002417 if (partial != NULL)
2418 fp = partial->pt_func;
2419 if (fp == NULL)
2420 {
2421 // Make a copy of the name, if it comes from a funcref variable it
2422 // could be changed or deleted in the called function.
2423 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
2424 if (name == NULL)
2425 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002426
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002427 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
2428 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002429
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002430 if (funcexe->doesrange != NULL)
2431 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002432
2433 if (partial != NULL)
2434 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002435 // When the function has a partial with a dict and there is a dict
2436 // argument, use the dict argument. That is backwards compatible.
2437 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002438 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002439 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01002440 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002441 {
2442 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002443 {
2444 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
2445 {
Bram Moolenaaref140542019-12-31 21:27:13 +01002446 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002447 goto theend;
2448 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002449 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002450 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002451 for (i = 0; i < argcount_in; ++i)
2452 argv[i + argv_clear] = argvars_in[i];
2453 argvars = argv;
2454 argcount = partial->pt_argc + argcount_in;
2455 }
2456 }
2457
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002458 if (error == FCERR_NONE && funcexe->check_type != NULL && funcexe->evaluate)
2459 {
2460 // Check that the argument types are OK for the types of the funcref.
2461 if (check_argument_types(funcexe->check_type, argvars, argcount,
2462 name) == FAIL)
2463 error = FCERR_OTHER;
2464 }
2465
Bram Moolenaaref140542019-12-31 21:27:13 +01002466 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002467 {
2468 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002469 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002470
Bram Moolenaar333894b2020-08-01 18:53:07 +02002471 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002472 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02002473 {
2474 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002475 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002476 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002477
Bram Moolenaare38eab22019-12-05 21:50:01 +01002478 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002479 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01002480 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002481
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002482 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002483 {
2484 /*
2485 * User defined function.
2486 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002487 if (fp == NULL)
Bram Moolenaar333894b2020-08-01 18:53:07 +02002488 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002489
Bram Moolenaare38eab22019-12-05 21:50:01 +01002490 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002491 if (fp == NULL
2492 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002493 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002494 && !aborting())
2495 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002496 // executed an autocommand, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002497 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002498 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002499 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002500 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
2501 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002502 // loaded a package, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002503 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002504 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02002505 if (fp == NULL)
2506 {
2507 char_u *p = untrans_function_name(rfname);
2508
2509 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02002510 // Don't do this if the name starts with "s:".
2511 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaar333894b2020-08-01 18:53:07 +02002512 fp = find_func(p, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002513 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002514
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002515 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01002516 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002517#ifdef FEAT_LUA
2518 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
2519 {
2520 cfunc_T cb = fp->uf_cb;
2521
2522 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
2523 }
2524#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002525 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002526 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002527 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01002528 // postponed filling in the arguments, do it now
2529 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002530 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002531
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002532 if (funcexe->basetv != NULL)
2533 {
2534 // Method call: base->Method()
2535 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
2536 argv[0] = *funcexe->basetv;
2537 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002538 argvars = argv;
2539 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002540 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 error = call_user_func_check(fp, argcount, argvars, rettv,
2543 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002544 }
2545 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002546 else if (funcexe->basetv != NULL)
2547 {
2548 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002549 * expr->method(): Find the method name in the table, call its
2550 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02002551 */
2552 error = call_internal_method(fname, argcount, argvars, rettv,
2553 funcexe->basetv);
2554 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002555 else
2556 {
2557 /*
2558 * Find the function name in the table, call its implementation.
2559 */
2560 error = call_internal_func(fname, argcount, argvars, rettv);
2561 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02002562
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002563 /*
2564 * The function call (or "FuncUndefined" autocommand sequence) might
2565 * have been aborted by an error, an interrupt, or an explicitly thrown
2566 * exception that has not been caught so far. This situation can be
2567 * tested for by calling aborting(). For an error in an internal
2568 * function or for the "E132" error in call_user_func(), however, the
2569 * throw point at which the "force_abort" flag (temporarily reset by
2570 * emsg()) is normally updated has not been reached yet. We need to
2571 * update that flag first to make aborting() reliable.
2572 */
2573 update_force_abort();
2574 }
Bram Moolenaaref140542019-12-31 21:27:13 +01002575 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002576 ret = OK;
2577
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002578theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002579 /*
2580 * Report an error unless the argument evaluation or function call has been
2581 * cancelled due to an aborting error, an interrupt, or an exception.
2582 */
2583 if (!aborting())
2584 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002585 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002586 }
2587
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002588 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002589 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002590 clear_tv(&argv[--argv_clear + argv_base]);
2591
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002592 vim_free(tofree);
2593 vim_free(name);
2594
2595 return ret;
2596}
2597
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002598 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599printable_func_name(ufunc_T *fp)
2600{
2601 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
2602}
2603
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002604/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002605 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002606 */
2607 static void
2608list_func_head(ufunc_T *fp, int indent)
2609{
2610 int j;
2611
2612 msg_start();
2613 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002614 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002615 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002616 msg_puts("def ");
2617 else
2618 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002620 msg_putchar('(');
2621 for (j = 0; j < fp->uf_args.ga_len; ++j)
2622 {
2623 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002624 msg_puts(", ");
2625 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 if (fp->uf_arg_types != NULL)
2627 {
2628 char *tofree;
2629
2630 msg_puts(": ");
2631 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2632 vim_free(tofree);
2633 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002634 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2635 {
2636 msg_puts(" = ");
2637 msg_puts(((char **)(fp->uf_def_args.ga_data))
2638 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2639 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002640 }
2641 if (fp->uf_varargs)
2642 {
2643 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002644 msg_puts(", ");
2645 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002646 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647 if (fp->uf_va_name != NULL)
2648 {
2649 if (j)
2650 msg_puts(", ");
2651 msg_puts("...");
2652 msg_puts((char *)fp->uf_va_name);
2653 if (fp->uf_va_type)
2654 {
2655 char *tofree;
2656
2657 msg_puts(": ");
2658 msg_puts(type_name(fp->uf_va_type, &tofree));
2659 vim_free(tofree);
2660 }
2661 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002662 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002663
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002664 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002665 {
2666 if (fp->uf_ret_type != &t_void)
2667 {
2668 char *tofree;
2669
2670 msg_puts(": ");
2671 msg_puts(type_name(fp->uf_ret_type, &tofree));
2672 vim_free(tofree);
2673 }
2674 }
2675 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002676 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002677 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002678 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002679 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002680 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002681 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002682 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002683 msg_clr_eos();
2684 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002685 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002686}
2687
2688/*
2689 * Get a function name, translating "<SID>" and "<SNR>".
2690 * Also handles a Funcref in a List or Dictionary.
2691 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002692 * Set "*is_global" to TRUE when the function must be global, unless
2693 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002694 * flags:
2695 * TFN_INT: internal function name OK
2696 * TFN_QUIET: be quiet
2697 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002698 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002699 * Advances "pp" to just after the function name (if no error).
2700 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002701 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002702trans_function_name(
2703 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002704 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002705 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002706 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002707 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002708 partial_T **partial, // return: partial of a FuncRef
2709 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002710{
2711 char_u *name = NULL;
2712 char_u *start;
2713 char_u *end;
2714 int lead;
2715 char_u sid_buf[20];
2716 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002717 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002718 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 int vim9script;
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002720 static char *e_function_name = N_("E129: Function name required");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002721
2722 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002723 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002724 start = *pp;
2725
Bram Moolenaare38eab22019-12-05 21:50:01 +01002726 // Check for hard coded <SNR>: already translated function ID (from a user
2727 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002728 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2729 && (*pp)[2] == (int)KE_SNR)
2730 {
2731 *pp += 3;
2732 len = get_id_len(pp) + 3;
2733 return vim_strnsave(start, len);
2734 }
2735
Bram Moolenaare38eab22019-12-05 21:50:01 +01002736 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2737 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002738 lead = eval_fname_script(start);
2739 if (lead > 2)
2740 start += lead;
2741
Bram Moolenaare38eab22019-12-05 21:50:01 +01002742 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002743 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002744 lead > 2 ? 0 : FNE_CHECK_START);
2745 if (end == start)
2746 {
2747 if (!skip)
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002748 emsg(_(e_function_name));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002749 goto theend;
2750 }
2751 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2752 {
2753 /*
2754 * Report an invalid expression in braces, unless the expression
2755 * evaluation has been cancelled due to an aborting error, an
2756 * interrupt, or an exception.
2757 */
2758 if (!aborting())
2759 {
2760 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002761 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002762 }
2763 else
2764 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2765 goto theend;
2766 }
2767
2768 if (lv.ll_tv != NULL)
2769 {
2770 if (fdp != NULL)
2771 {
2772 fdp->fd_dict = lv.ll_dict;
2773 fdp->fd_newkey = lv.ll_newkey;
2774 lv.ll_newkey = NULL;
2775 fdp->fd_di = lv.ll_di;
2776 }
2777 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2778 {
2779 name = vim_strsave(lv.ll_tv->vval.v_string);
2780 *pp = end;
2781 }
2782 else if (lv.ll_tv->v_type == VAR_PARTIAL
2783 && lv.ll_tv->vval.v_partial != NULL)
2784 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002785 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002786 *pp = end;
2787 if (partial != NULL)
2788 *partial = lv.ll_tv->vval.v_partial;
2789 }
2790 else
2791 {
2792 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2793 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002794 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002795 else
2796 *pp = end;
2797 name = NULL;
2798 }
2799 goto theend;
2800 }
2801
2802 if (lv.ll_name == NULL)
2803 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002804 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002805 *pp = end;
2806 goto theend;
2807 }
2808
Bram Moolenaare38eab22019-12-05 21:50:01 +01002809 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002810 if (lv.ll_exp_name != NULL)
2811 {
2812 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002813 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002814 flags & TFN_NO_AUTOLOAD);
2815 if (name == lv.ll_exp_name)
2816 name = NULL;
2817 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002818 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002819 {
2820 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002821 name = deref_func_name(*pp, &len, partial, type,
2822 flags & TFN_NO_AUTOLOAD);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002823 if (name == *pp)
2824 name = NULL;
2825 }
2826 if (name != NULL)
2827 {
2828 name = vim_strsave(name);
2829 *pp = end;
2830 if (STRNCMP(name, "<SNR>", 5) == 0)
2831 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002832 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002833 name[0] = K_SPECIAL;
2834 name[1] = KS_EXTRA;
2835 name[2] = (int)KE_SNR;
2836 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2837 }
2838 goto theend;
2839 }
2840
2841 if (lv.ll_exp_name != NULL)
2842 {
2843 len = (int)STRLEN(lv.ll_exp_name);
2844 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2845 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2846 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002847 // When there was "s:" already or the name expanded to get a
2848 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002849 lv.ll_name += 2;
2850 len -= 2;
2851 lead = 2;
2852 }
2853 }
2854 else
2855 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002856 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002857 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002858 {
2859 if (is_global != NULL && lv.ll_name[0] == 'g')
2860 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002861 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002862 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002863 len = (int)(end - lv.ll_name);
2864 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002865 if (len <= 0)
2866 {
2867 if (!skip)
2868 emsg(_(e_function_name));
2869 goto theend;
2870 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002871
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002872 // In Vim9 script a user function is script-local by default, unless it
2873 // starts with a lower case character: dict.func().
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002874 vim9script = ASCII_ISUPPER(*start) && in_vim9script();
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002875 if (vim9script)
2876 {
2877 char_u *p;
2878
2879 // SomeScript#func() is a global function.
2880 for (p = start; *p != NUL && *p != '('; ++p)
2881 if (*p == AUTOLOAD_CHAR)
2882 vim9script = FALSE;
2883 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002885 /*
2886 * Copy the function name to allocated memory.
2887 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2888 * Accept <SNR>123_name() outside a script.
2889 */
2890 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002891 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002893 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 if (!vim9script)
2895 lead = 3;
2896 if (vim9script || (lv.ll_exp_name != NULL
2897 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002898 || eval_fname_sid(*pp))
2899 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002901 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002903 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002904 goto theend;
2905 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002906 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 if (vim9script)
2908 extra = 3 + (int)STRLEN(sid_buf);
2909 else
2910 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002911 }
2912 }
2913 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2914 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002915 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002916 start);
2917 goto theend;
2918 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002919 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002920 {
2921 char_u *cp = vim_strchr(lv.ll_name, ':');
2922
2923 if (cp != NULL && cp < end)
2924 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002925 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002926 goto theend;
2927 }
2928 }
2929
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002931 if (name != NULL)
2932 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002933 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002934 {
2935 name[0] = K_SPECIAL;
2936 name[1] = KS_EXTRA;
2937 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002939 STRCPY(name + 3, sid_buf);
2940 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2942 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002943 }
2944 *pp = end;
2945
2946theend:
2947 clear_lval(&lv);
2948 return name;
2949}
2950
2951/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002952 * Assuming "name" is the result of trans_function_name() and it was prefixed
2953 * to use the script-local name, return the unmodified name (points into
2954 * "name"). Otherwise return NULL.
2955 * This can be used to first search for a script-local function and fall back
2956 * to the global function if not found.
2957 */
2958 char_u *
2959untrans_function_name(char_u *name)
2960{
2961 char_u *p;
2962
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002963 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02002964 {
2965 p = vim_strchr(name, '_');
2966 if (p != NULL)
2967 return p + 1;
2968 }
2969 return NULL;
2970}
2971
2972/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002973 * List functions. When "regmatch" is NULL all of then.
2974 * Otherwise functions matching "regmatch".
2975 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002976 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002977list_functions(regmatch_T *regmatch)
2978{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002979 int changed = func_hashtab.ht_changed;
2980 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002981 hashitem_T *hi;
2982
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002983 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002984 {
2985 if (!HASHITEM_EMPTY(hi))
2986 {
2987 ufunc_T *fp = HI2UF(hi);
2988
2989 --todo;
2990 if ((fp->uf_flags & FC_DEAD) == 0
2991 && (regmatch == NULL
2992 ? !message_filtered(fp->uf_name)
2993 && !func_name_refcount(fp->uf_name)
2994 : !isdigit(*fp->uf_name)
2995 && vim_regexec(regmatch, fp->uf_name, 0)))
2996 {
2997 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002998 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002999 {
3000 emsg(_("E454: function list was modified"));
3001 return;
3002 }
3003 }
3004 }
3005 }
3006}
3007
3008/*
Bram Moolenaaradc8e442020-12-31 18:28:18 +01003009 * Check if "*cmd" points to a function command and if so advance "*cmd" and
3010 * return TRUE.
3011 * Otherwise return FALSE;
3012 * Do not consider "function(" to be a command.
3013 */
3014 static int
3015is_function_cmd(char_u **cmd)
3016{
3017 char_u *p = *cmd;
3018
3019 if (checkforcmd(&p, "function", 2))
3020 {
3021 if (*p == '(')
3022 return FALSE;
3023 *cmd = p;
3024 return TRUE;
3025 }
3026 return FALSE;
3027}
3028
3029/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02003030 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003031 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
3032 * the function name.
Bram Moolenaar04b12692020-05-04 23:24:44 +02003033 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003034 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02003035 ufunc_T *
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003036define_function(exarg_T *eap, char_u *name_arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003037{
3038 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02003039 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003040 int j;
3041 int c;
3042 int saved_did_emsg;
3043 int saved_wait_return = need_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003044 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003045 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003046 char_u *p;
3047 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01003048 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003049 char_u *line_arg = NULL;
3050 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003052 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003053 garray_T newlines;
3054 int varargs = FALSE;
3055 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003057 ufunc_T *fp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003058 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003059 int indent;
3060 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061#define MAX_FUNC_NESTING 50
3062 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003063 dictitem_T *v;
3064 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003065 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003066 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003067 hashitem_T *hi;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003068 getline_opt_T getline_options;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003069 linenr_T sourcing_lnum_off;
3070 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003071 int is_heredoc = FALSE;
3072 char_u *skip_until = NULL;
3073 char_u *heredoc_trimmed = NULL;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003074 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02003075 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003076
3077 /*
3078 * ":function" without argument: list functions.
3079 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003080 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003081 {
3082 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003083 list_functions(NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003084 eap->nextcmd = check_nextcmd(eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003085 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003086 }
3087
3088 /*
3089 * ":function /pat": list functions matching pattern.
3090 */
3091 if (*eap->arg == '/')
3092 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003093 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003094 if (!eap->skip)
3095 {
3096 regmatch_T regmatch;
3097
3098 c = *p;
3099 *p = NUL;
3100 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
3101 *p = c;
3102 if (regmatch.regprog != NULL)
3103 {
3104 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003105 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003106 vim_regfree(regmatch.regprog);
3107 }
3108 }
3109 if (*p == '/')
3110 ++p;
3111 eap->nextcmd = check_nextcmd(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003112 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003113 }
3114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 ga_init(&newargs);
3116 ga_init(&argtypes);
3117 ga_init(&default_args);
3118
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003119 /*
3120 * Get the function name. There are these situations:
3121 * func normal function name
3122 * "name" == func, "fudi.fd_dict" == NULL
3123 * dict.func new dictionary entry
3124 * "name" == NULL, "fudi.fd_dict" set,
3125 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
3126 * dict.func existing dict entry with a Funcref
3127 * "name" == func, "fudi.fd_dict" set,
3128 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3129 * dict.func existing dict entry that's not a Funcref
3130 * "name" == NULL, "fudi.fd_dict" set,
3131 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3132 * s:func script-local function name
3133 * g:func global function name, same as "func"
3134 */
3135 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003136 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003137 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003138 // nested function, argument is (args).
3139 paren = TRUE;
3140 CLEAR_FIELD(fudi);
3141 }
3142 else
3143 {
Bram Moolenaarb6571982021-01-08 22:24:19 +01003144 if (STRNCMP(p, "<lambda>", 8) == 0)
3145 {
3146 p += 8;
3147 (void)getdigits(&p);
3148 name = vim_strnsave(eap->arg, p - eap->arg);
3149 CLEAR_FIELD(fudi);
3150 }
3151 else
3152 name = trans_function_name(&p, &is_global, eap->skip,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003153 TFN_NO_AUTOLOAD, &fudi, NULL, NULL);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003154 paren = (vim_strchr(p, '(') != NULL);
3155 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003157 /*
3158 * Return on an invalid expression in braces, unless the expression
3159 * evaluation has been cancelled due to an aborting error, an
3160 * interrupt, or an exception.
3161 */
3162 if (!aborting())
3163 {
3164 if (!eap->skip && fudi.fd_newkey != NULL)
3165 semsg(_(e_dictkey), fudi.fd_newkey);
3166 vim_free(fudi.fd_newkey);
3167 return NULL;
3168 }
3169 else
3170 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003171 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003172 }
3173
Bram Moolenaare38eab22019-12-05 21:50:01 +01003174 // An error in a function call during evaluation of an expression in magic
3175 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003176 saved_did_emsg = did_emsg;
3177 did_emsg = FALSE;
3178
3179 /*
3180 * ":function func" with only function name: list function.
3181 */
3182 if (!paren)
3183 {
3184 if (!ends_excmd(*skipwhite(p)))
3185 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003186 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003187 goto ret_free;
3188 }
3189 eap->nextcmd = check_nextcmd(p);
3190 if (eap->nextcmd != NULL)
3191 *p = NUL;
3192 if (!eap->skip && !got_int)
3193 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003194 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003195 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
3196 {
3197 char_u *up = untrans_function_name(name);
3198
3199 // With Vim9 script the name was made script-local, if not
3200 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02003201 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003202 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003203 }
3204
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003205 if (fp != NULL)
3206 {
3207 list_func_head(fp, TRUE);
3208 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
3209 {
3210 if (FUNCLINE(fp, j) == NULL)
3211 continue;
3212 msg_putchar('\n');
3213 msg_outnum((long)(j + 1));
3214 if (j < 9)
3215 msg_putchar(' ');
3216 if (j < 99)
3217 msg_putchar(' ');
3218 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003219 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003220 ui_breakcheck();
3221 }
3222 if (!got_int)
3223 {
3224 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003225 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 msg_puts(" enddef");
3227 else
3228 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003229 }
3230 }
3231 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02003232 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003233 }
3234 goto ret_free;
3235 }
3236
3237 /*
3238 * ":function name(arg1, arg2)" Define function.
3239 */
3240 p = skipwhite(p);
3241 if (*p != '(')
3242 {
3243 if (!eap->skip)
3244 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003245 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003246 goto ret_free;
3247 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003248 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003249 if (vim_strchr(p, '(') != NULL)
3250 p = vim_strchr(p, '(');
3251 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003252
Bram Moolenaar4efd9942021-01-24 21:14:20 +01003253 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
3254 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003255 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01003256 goto ret_free;
3257 }
3258
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02003259 // In Vim9 script only global functions can be redefined.
3260 if (vim9script && eap->forceit && !is_global)
3261 {
3262 emsg(_(e_nobang));
3263 goto ret_free;
3264 }
3265
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003266 ga_init2(&newlines, (int)sizeof(char_u *), 3);
3267
Bram Moolenaar04b12692020-05-04 23:24:44 +02003268 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003269 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003270 // Check the name of the function. Unless it's a dictionary function
3271 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003272 if (name != NULL)
3273 arg = name;
3274 else
3275 arg = fudi.fd_newkey;
3276 if (arg != NULL && (fudi.fd_di == NULL
3277 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
3278 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
3279 {
3280 if (*arg == K_SPECIAL)
3281 j = 3;
3282 else
3283 j = 0;
3284 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
3285 : eval_isnamec(arg[j])))
3286 ++j;
3287 if (arg[j] != NUL)
3288 emsg_funcname((char *)e_invarg2, arg);
3289 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003290 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003291 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003292 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003293 }
3294
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003295 // This may get more lines and make the pointers into the first line
3296 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01003297 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003299 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01003300 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003301 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003302 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01003303 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003306 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 // find the return type: :def Func(): type
3308 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003309 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02003311 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003313 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003314 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01003315 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003319 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003320 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02003321 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003322 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003323 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02003324 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003325 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 else
3327 // find extra arguments "range", "dict", "abort" and "closure"
3328 for (;;)
3329 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01003330 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 p = skipwhite(p);
3332 if (STRNCMP(p, "range", 5) == 0)
3333 {
3334 flags |= FC_RANGE;
3335 p += 5;
3336 }
3337 else if (STRNCMP(p, "dict", 4) == 0)
3338 {
3339 flags |= FC_DICT;
3340 p += 4;
3341 }
3342 else if (STRNCMP(p, "abort", 5) == 0)
3343 {
3344 flags |= FC_ABORT;
3345 p += 5;
3346 }
3347 else if (STRNCMP(p, "closure", 7) == 0)
3348 {
3349 flags |= FC_CLOSURE;
3350 p += 7;
3351 if (current_funccal == NULL)
3352 {
3353 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
3354 name == NULL ? (char_u *)"" : name);
3355 goto erret;
3356 }
3357 }
3358 else
3359 break;
3360 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003361
Bram Moolenaare38eab22019-12-05 21:50:01 +01003362 // When there is a line break use what follows for the function body.
3363 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003364 if (*p == '\n')
3365 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003366 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02003367 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
3368 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01003369 && !(VIM_ISWHITE(*whitep) && *p == '#'
3370 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02003371 && !eap->skip
3372 && !did_emsg)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003373 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003374
3375 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
3377 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003378 */
3379 if (KeyTyped)
3380 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003381 // Check if the function already exists, don't let the user type the
3382 // whole function before telling him it doesn't work! For a script we
3383 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003384 if (!eap->skip && !eap->forceit)
3385 {
3386 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003387 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003388 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003389 emsg_funcname(e_funcexts, name);
3390 }
3391
3392 if (!eap->skip && did_emsg)
3393 goto erret;
3394
Bram Moolenaare38eab22019-12-05 21:50:01 +01003395 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003396 cmdline_row = msg_row;
3397 }
3398
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003399 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003400 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003401
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02003402 // Detect having skipped over comment lines to find the return
3403 // type. Add NULL lines to keep the line count correct.
3404 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
3405 if (SOURCING_LNUM < sourcing_lnum_off)
3406 {
3407 sourcing_lnum_off -= SOURCING_LNUM;
3408 if (ga_grow(&newlines, sourcing_lnum_off) == FAIL)
3409 goto erret;
3410 while (sourcing_lnum_off-- > 0)
3411 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3412 }
3413
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003414 indent = 2;
3415 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003417 getline_options = eap->cmdidx == CMD_def
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01003418 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003419 for (;;)
3420 {
3421 if (KeyTyped)
3422 {
3423 msg_scroll = TRUE;
3424 saved_wait_return = FALSE;
3425 }
3426 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003427
3428 if (line_arg != NULL)
3429 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003430 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003431 theline = line_arg;
3432 p = vim_strchr(theline, '\n');
3433 if (p == NULL)
3434 line_arg += STRLEN(line_arg);
3435 else
3436 {
3437 *p = NUL;
3438 line_arg = p + 1;
3439 }
3440 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003441 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02003442 {
3443 vim_free(line_to_free);
3444 if (eap->getline == NULL)
Bram Moolenaar66250c92020-08-20 15:02:42 +02003445 theline = getcmdline(':', 0L, indent, getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003446 else
Bram Moolenaar66250c92020-08-20 15:02:42 +02003447 theline = eap->getline(':', eap->cookie, indent,
3448 getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003449 line_to_free = theline;
3450 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003451 if (KeyTyped)
3452 lines_left = Rows - 1;
3453 if (theline == NULL)
3454 {
Bram Moolenaarb8ba9b92021-01-01 18:54:34 +01003455 // Use the start of the function for the line number.
3456 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003457 if (skip_until != NULL)
3458 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
3459 else if (eap->cmdidx == CMD_def)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003460 emsg(_(e_missing_enddef));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 else
3462 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003463 goto erret;
3464 }
3465
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003466 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003467 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003468 if (SOURCING_LNUM < sourcing_lnum_off)
3469 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003470 else
3471 sourcing_lnum_off = 0;
3472
3473 if (skip_until != NULL)
3474 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003476 // * ":append" and "."
3477 // * ":python <<EOF" and "EOF"
3478 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
3479 if (heredoc_trimmed == NULL
3480 || (is_heredoc && skipwhite(theline) == theline)
3481 || STRNCMP(theline, heredoc_trimmed,
3482 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003483 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003484 if (heredoc_trimmed == NULL)
3485 p = theline;
3486 else if (is_heredoc)
3487 p = skipwhite(theline) == theline
3488 ? theline : theline + STRLEN(heredoc_trimmed);
3489 else
3490 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02003491 if (STRCMP(p, skip_until) == 0)
3492 {
3493 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003494 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003495 getline_options = eap->cmdidx == CMD_def
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01003496 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003497 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003498 }
3499 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003500 }
3501 else
3502 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003503 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01003504 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003505 ;
3506
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 // Check for "endfunction" or "enddef".
Bram Moolenaar832ea892021-01-08 21:55:26 +01003508 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 if (checkforcmd(&p, nesting_def[nesting]
Bram Moolenaar832ea892021-01-08 21:55:26 +01003510 ? "enddef" : "endfunction", 4)
3511 && *p != ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003512 {
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003513 if (nesting-- == 0)
Bram Moolenaar53564f72017-06-24 14:48:11 +02003514 {
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003515 char_u *nextcmd = NULL;
3516
3517 if (*p == '|')
3518 nextcmd = p + 1;
3519 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
3520 nextcmd = line_arg;
3521 else if (*p != NUL && *p != '"' && p_verbose > 0)
3522 give_warning2(eap->cmdidx == CMD_def
3523 ? (char_u *)_("W1001: Text found after :enddef: %s")
3524 : (char_u *)_("W22: Text found after :endfunction: %s"),
3525 p, TRUE);
3526 if (nextcmd != NULL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02003527 {
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003528 // Another command follows. If the line came from "eap"
3529 // we can simply point into it, otherwise we need to
3530 // change "eap->cmdlinep".
3531 eap->nextcmd = nextcmd;
3532 if (line_to_free != NULL)
3533 {
3534 vim_free(*eap->cmdlinep);
3535 *eap->cmdlinep = line_to_free;
3536 line_to_free = NULL;
3537 }
Bram Moolenaar53564f72017-06-24 14:48:11 +02003538 }
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003539 break;
Bram Moolenaar53564f72017-06-24 14:48:11 +02003540 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003541 }
3542
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003543 // Check for mismatched "endfunc" or "enddef".
3544 // We don't check for "def" inside "func" thus we also can't check
3545 // for "enddef".
3546 // We continue to find the end of the function, although we might
3547 // not find it.
3548 else if (nesting_def[nesting])
3549 {
Bram Moolenaar832ea892021-01-08 21:55:26 +01003550 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003551 emsg(_(e_mismatched_endfunction));
3552 }
3553 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
3554 emsg(_(e_mismatched_enddef));
3555
Bram Moolenaare38eab22019-12-05 21:50:01 +01003556 // Increase indent inside "if", "while", "for" and "try", decrease
3557 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003559 indent -= 2;
3560 else if (STRNCMP(p, "if", 2) == 0
3561 || STRNCMP(p, "wh", 2) == 0
3562 || STRNCMP(p, "for", 3) == 0
3563 || STRNCMP(p, "try", 3) == 0)
3564 indent += 2;
3565
Bram Moolenaare38eab22019-12-05 21:50:01 +01003566 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01003567 // Only recognize "def" inside "def", not inside "function",
3568 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 c = *p;
Bram Moolenaaradc8e442020-12-31 18:28:18 +01003570 if (is_function_cmd(&p)
Bram Moolenaar673660a2020-01-26 16:50:05 +01003571 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003572 {
3573 if (*p == '!')
3574 p = skipwhite(p + 1);
3575 p += eval_fname_script(p);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003576 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
3577 NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003578 if (*skipwhite(p) == '(')
3579 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 if (nesting == MAX_FUNC_NESTING - 1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003581 emsg(_(e_function_nesting_too_deep));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 else
3583 {
3584 ++nesting;
3585 nesting_def[nesting] = (c == 'd');
3586 indent += 2;
3587 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003588 }
3589 }
3590
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003591 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003592 p = skip_range(p, FALSE, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003593 if (eap->cmdidx != CMD_def
3594 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01003595 || (p[0] == 'c'
3596 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
3597 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
3598 && (STRNCMP(&p[3], "nge", 3) != 0
3599 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003600 || (p[0] == 'i'
3601 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003602 && (!ASCII_ISALPHA(p[2])
3603 || (p[2] == 's'
3604 && (!ASCII_ISALPHA(p[3])
3605 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003606 skip_until = vim_strsave((char_u *)".");
3607
Bram Moolenaare38eab22019-12-05 21:50:01 +01003608 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003609 arg = skipwhite(skiptowhite(p));
3610 if (arg[0] == '<' && arg[1] =='<'
3611 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003612 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
3613 || ((p[2] == '3' || p[2] == 'x')
3614 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003615 || (p[0] == 'p' && p[1] == 'e'
3616 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
3617 || (p[0] == 't' && p[1] == 'c'
3618 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
3619 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
3620 && !ASCII_ISALPHA(p[3]))
3621 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
3622 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
3623 || (p[0] == 'm' && p[1] == 'z'
3624 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
3625 ))
3626 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003627 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003628 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003629 if (STRNCMP(p, "trim", 4) == 0)
3630 {
3631 // Ignore leading white space.
3632 p = skipwhite(p + 4);
3633 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003634 skipwhite(theline) - theline);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003635 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003636 if (*p == NUL)
3637 skip_until = vim_strsave((char_u *)".");
3638 else
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003639 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003640 getline_options = GETLINE_NONE;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003641 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003642 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003643
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003644 // Check for ":cmd v =<< [trim] EOF"
3645 // and ":cmd [a, b] =<< [trim] EOF"
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003646 // and "lines =<< [trim] EOF" for Vim9
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003647 // Where "cmd" can be "let", "var", "final" or "const".
Bram Moolenaar8471e572019-05-19 21:37:18 +02003648 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003649 if (*arg == '[')
3650 arg = vim_strchr(arg, ']');
3651 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003652 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003653 int found = (eap->cmdidx == CMD_def && arg[0] == '='
3654 && arg[1] == '<' && arg[2] =='<');
3655
3656 if (!found)
3657 // skip over the argument after "cmd"
3658 arg = skipwhite(skiptowhite(arg));
3659 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003660 && (checkforcmd(&p, "let", 2)
3661 || checkforcmd(&p, "var", 3)
3662 || checkforcmd(&p, "final", 5)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003663 || checkforcmd(&p, "const", 5))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02003664 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003665 p = skipwhite(arg + 3);
3666 if (STRNCMP(p, "trim", 4) == 0)
3667 {
3668 // Ignore leading white space.
3669 p = skipwhite(p + 4);
3670 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003671 skipwhite(theline) - theline);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003672 }
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003673 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003674 getline_options = GETLINE_NONE;
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003675 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003676 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003677 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003678 }
3679
Bram Moolenaare38eab22019-12-05 21:50:01 +01003680 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003681 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003682 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003683
Bram Moolenaare38eab22019-12-05 21:50:01 +01003684 // Copy the line to newly allocated memory. get_one_sourceline()
3685 // allocates 250 bytes per line, this saves 80% on average. The cost
3686 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003687 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003688 if (p == NULL)
3689 goto erret;
3690 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003691
Bram Moolenaare38eab22019-12-05 21:50:01 +01003692 // Add NULL lines for continuation lines, so that the line count is
3693 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003694 while (sourcing_lnum_off-- > 0)
3695 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3696
Bram Moolenaare38eab22019-12-05 21:50:01 +01003697 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003698 if (line_arg != NULL && *line_arg == NUL)
3699 line_arg = NULL;
3700 }
3701
Bram Moolenaare38eab22019-12-05 21:50:01 +01003702 // Don't define the function when skipping commands or when an error was
3703 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003704 if (eap->skip || did_emsg)
3705 goto erret;
3706
3707 /*
3708 * If there are no errors, add the function
3709 */
3710 if (fudi.fd_dict == NULL)
3711 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 hashtab_T *ht;
3713
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003714 v = find_var(name, &ht, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003715 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3716 {
3717 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3718 name);
3719 goto erret;
3720 }
3721
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003722 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaareef21022020-08-01 22:16:43 +02003723 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003724 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003725 char_u *uname = untrans_function_name(name);
3726
3727 import = find_imported(uname == NULL ? name : uname, 0, NULL);
3728 }
3729
3730 if (fp != NULL || import != NULL)
3731 {
3732 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003734 // Function can be replaced with "function!" and when sourcing the
3735 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02003736 // A name that is used by an import can not be overruled.
3737 if (import != NULL
3738 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003739 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02003740 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003741 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003742 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003743 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02003744 else
3745 emsg_funcname(e_funcexts, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003746 goto erret;
3747 }
3748 if (fp->uf_calls > 0)
3749 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003750 emsg_funcname(
3751 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003752 name);
3753 goto erret;
3754 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003755 if (fp->uf_refcount > 1)
3756 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003757 // This function is referenced somewhere, don't redefine it but
3758 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003759 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003760 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003761 fp = NULL;
3762 overwrite = TRUE;
3763 }
3764 else
3765 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003766 char_u *exp_name = fp->uf_name_exp;
3767
3768 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003769 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003770 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003771 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003772 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003774#ifdef FEAT_PROFILE
3775 fp->uf_profiling = FALSE;
3776 fp->uf_prof_initialized = FALSE;
3777#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003778 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003779 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003780 }
3781 }
3782 else
3783 {
3784 char numbuf[20];
3785
3786 fp = NULL;
3787 if (fudi.fd_newkey == NULL && !eap->forceit)
3788 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003789 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003790 goto erret;
3791 }
3792 if (fudi.fd_di == NULL)
3793 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003794 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02003795 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003796 goto erret;
3797 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003798 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02003799 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003800 goto erret;
3801
Bram Moolenaare38eab22019-12-05 21:50:01 +01003802 // Give the function a sequential number. Can only be used with a
3803 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003804 vim_free(name);
3805 sprintf(numbuf, "%d", ++func_nr);
3806 name = vim_strsave((char_u *)numbuf);
3807 if (name == NULL)
3808 goto erret;
3809 }
3810
3811 if (fp == NULL)
3812 {
3813 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3814 {
3815 int slen, plen;
3816 char_u *scriptname;
3817
Bram Moolenaare38eab22019-12-05 21:50:01 +01003818 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003819 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003820 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003821 {
3822 scriptname = autoload_name(name);
3823 if (scriptname != NULL)
3824 {
3825 p = vim_strchr(scriptname, '/');
3826 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003827 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003828 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003829 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003830 j = OK;
3831 vim_free(scriptname);
3832 }
3833 }
3834 if (j == FAIL)
3835 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003836 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003837 goto erret;
3838 }
3839 }
3840
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003841 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003842 if (fp == NULL)
3843 goto erret;
3844
3845 if (fudi.fd_dict != NULL)
3846 {
3847 if (fudi.fd_di == NULL)
3848 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003849 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003850 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3851 if (fudi.fd_di == NULL)
3852 {
3853 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003854 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003855 goto erret;
3856 }
3857 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3858 {
3859 vim_free(fudi.fd_di);
3860 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003861 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003862 goto erret;
3863 }
3864 }
3865 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003866 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003867 clear_tv(&fudi.fd_di->di_tv);
3868 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003869 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003870
Bram Moolenaare38eab22019-12-05 21:50:01 +01003871 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003872 flags |= FC_DICT;
3873 }
3874
Bram Moolenaare38eab22019-12-05 21:50:01 +01003875 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003876 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003877 if (overwrite)
3878 {
3879 hi = hash_find(&func_hashtab, name);
3880 hi->hi_key = UF2HIKEY(fp);
3881 }
3882 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003883 {
3884 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003885 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003886 goto erret;
3887 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003888 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003889 }
3890 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003891 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003893 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894
3895 if (eap->cmdidx == CMD_def)
3896 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003897 int lnum_save = SOURCING_LNUM;
3898 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003899
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003900 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003901
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003902 // error messages are for the first function line
3903 SOURCING_LNUM = sourcing_lnum_top;
3904
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003905 if (cstack != NULL && cstack->cs_idx >= 0)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003906 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003907 int count = cstack->cs_idx + 1;
3908 int i;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003909
3910 // The block context may be needed for script variables declared in
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003911 // a block that is visible now but not when the function is called
3912 // later.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003913 fp->uf_block_ids = ALLOC_MULT(int, count);
3914 if (fp->uf_block_ids != NULL)
3915 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003916 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003917 sizeof(int) * count);
3918 fp->uf_block_depth = count;
3919 }
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003920
3921 // Set flag in each block to indicate a function was defined. This
3922 // is used to keep the variable when leaving the block, see
3923 // hide_script_var().
3924 for (i = 0; i <= cstack->cs_idx; ++i)
3925 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003926 }
3927
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003928 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003930 SOURCING_LNUM = lnum_save;
3931 goto errret_2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003933 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934
3935 // parse the return type, if any
3936 if (ret_type == NULL)
3937 fp->uf_ret_type = &t_void;
3938 else
3939 {
3940 p = ret_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01003941 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaar648ea762021-01-15 19:04:32 +01003942 if (fp->uf_ret_type == NULL)
3943 {
3944 fp->uf_ret_type = &t_void;
3945 SOURCING_LNUM = lnum_save;
3946 goto erret;
3947 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02003949 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003951 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003952 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003954 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003955 if ((flags & FC_CLOSURE) != 0)
3956 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003957 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003958 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003959 }
3960 else
3961 fp->uf_scoped = NULL;
3962
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003963#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003964 if (prof_def_func())
3965 func_do_profile(fp);
3966#endif
3967 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003968 if (sandbox)
3969 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003970 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003971 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003972 fp->uf_flags = flags;
3973 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003974 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003975 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02003976 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003977 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 if (is_export)
3979 {
3980 fp->uf_flags |= FC_EXPORT;
3981 // let ex_export() know the export worked.
3982 is_export = FALSE;
3983 }
3984
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003985 if (eap->cmdidx == CMD_def)
3986 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02003987 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
3988 // :func does not use Vim9 script syntax, even in a Vim9 script file
3989 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003990
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003991 goto ret_free;
3992
3993erret:
3994 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003995 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01003996 if (fp != NULL)
3997 {
3998 ga_init(&fp->uf_args);
3999 ga_init(&fp->uf_def_args);
4000 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004001errret_2:
4002 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004003 if (fp != NULL)
4004 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004005ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004006 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004007 vim_free(skip_until);
Bram Moolenaarfffdf472020-12-13 21:16:55 +01004008 vim_free(heredoc_trimmed);
Bram Moolenaar53564f72017-06-24 14:48:11 +02004009 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004010 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004011 if (name != name_arg)
4012 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004013 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004014 did_emsg |= saved_did_emsg;
4015 need_wait_return |= saved_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004016
4017 return fp;
4018}
4019
4020/*
4021 * ":function"
4022 */
4023 void
4024ex_function(exarg_T *eap)
4025{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004026 (void)define_function(eap, NULL);
Bram Moolenaar822ba242020-05-24 23:00:18 +02004027}
4028
4029/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004030 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarb2049902021-01-24 12:53:53 +01004031 * be compiled. Except dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02004032 */
4033 void
4034ex_defcompile(exarg_T *eap UNUSED)
4035{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004036 long todo = (long)func_hashtab.ht_used;
4037 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004038 hashitem_T *hi;
4039 ufunc_T *ufunc;
4040
4041 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
4042 {
4043 if (!HASHITEM_EMPTY(hi))
4044 {
4045 --todo;
4046 ufunc = HI2UF(hi);
4047 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004048 && ufunc->uf_def_status == UF_TO_BE_COMPILED
4049 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004050 {
Bram Moolenaardc167462021-02-20 20:26:16 +01004051 (void)compile_def_function(ufunc, FALSE, FALSE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004052
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004053 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004054 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004055 // a function has been added or removed, need to start over
4056 todo = (long)func_hashtab.ht_used;
4057 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004058 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02004059 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004060 }
4061 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004062 }
4063 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004064}
4065
4066/*
4067 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
4068 * Return 2 if "p" starts with "s:".
4069 * Return 0 otherwise.
4070 */
4071 int
4072eval_fname_script(char_u *p)
4073{
Bram Moolenaare38eab22019-12-05 21:50:01 +01004074 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
4075 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004076 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
4077 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
4078 return 5;
4079 if (p[0] == 's' && p[1] == ':')
4080 return 2;
4081 return 0;
4082}
4083
4084 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004085translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004086{
4087 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004088 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004089 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090}
4091
4092/*
4093 * Return TRUE when "ufunc" has old-style "..." varargs
4094 * or named varargs "...name: type".
4095 */
4096 int
4097has_varargs(ufunc_T *ufunc)
4098{
4099 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004100}
4101
4102/*
4103 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004104 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004105 */
4106 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004107function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004108{
4109 char_u *nm = name;
4110 char_u *p;
4111 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004112 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004113 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004114
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004115 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
4116 if (no_deref)
4117 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004118 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004119 nm = skipwhite(nm);
4120
Bram Moolenaare38eab22019-12-05 21:50:01 +01004121 // Only accept "funcname", "funcname ", "funcname (..." and
4122 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004123 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004124 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004125 vim_free(p);
4126 return n;
4127}
4128
Bram Moolenaar113e1072019-01-20 15:30:40 +01004129#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004130 char_u *
4131get_expanded_name(char_u *name, int check)
4132{
4133 char_u *nm = name;
4134 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004135 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004136
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004137 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004138 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004139
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004140 if (p != NULL && *nm == NUL
4141 && (!check || translated_function_exists(p, is_global)))
4142 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004143
4144 vim_free(p);
4145 return NULL;
4146}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004147#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004148
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004149/*
4150 * Function given to ExpandGeneric() to obtain the list of user defined
4151 * function names.
4152 */
4153 char_u *
4154get_user_func_name(expand_T *xp, int idx)
4155{
4156 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004157 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004158 static hashitem_T *hi;
4159 ufunc_T *fp;
4160
4161 if (idx == 0)
4162 {
4163 done = 0;
4164 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004165 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004166 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004167 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004168 {
4169 if (done++ > 0)
4170 ++hi;
4171 while (HASHITEM_EMPTY(hi))
4172 ++hi;
4173 fp = HI2UF(hi);
4174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 // don't show dead, dict and lambda functions
4176 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02004177 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004179
4180 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004181 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004182
4183 cat_func_name(IObuff, fp);
4184 if (xp->xp_context != EXPAND_USER_FUNC)
4185 {
4186 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004188 STRCAT(IObuff, ")");
4189 }
4190 return IObuff;
4191 }
4192 return NULL;
4193}
4194
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004195/*
4196 * ":delfunction {name}"
4197 */
4198 void
4199ex_delfunction(exarg_T *eap)
4200{
4201 ufunc_T *fp = NULL;
4202 char_u *p;
4203 char_u *name;
4204 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004205 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004206
4207 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004208 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
4209 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004210 vim_free(fudi.fd_newkey);
4211 if (name == NULL)
4212 {
4213 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004214 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004215 return;
4216 }
4217 if (!ends_excmd(*skipwhite(p)))
4218 {
4219 vim_free(name);
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004220 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004221 return;
4222 }
4223 eap->nextcmd = check_nextcmd(p);
4224 if (eap->nextcmd != NULL)
4225 *p = NUL;
4226
4227 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004228 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004229 vim_free(name);
4230
4231 if (!eap->skip)
4232 {
4233 if (fp == NULL)
4234 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004235 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004236 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004237 return;
4238 }
4239 if (fp->uf_calls > 0)
4240 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004241 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004242 return;
4243 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004244 if (fp->uf_flags & FC_VIM9)
4245 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004246 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004247 return;
4248 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004249
4250 if (fudi.fd_dict != NULL)
4251 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004252 // Delete the dict item that refers to the function, it will
4253 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004254 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4255 }
4256 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004257 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004258 // A normal function (not a numbered function or lambda) has a
4259 // refcount of 1 for the entry in the hashtable. When deleting
4260 // it and the refcount is more than one, it should be kept.
4261 // A numbered function and lambda should be kept if the refcount is
4262 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004263 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004264 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004265 // Function is still referenced somewhere. Don't free it but
4266 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004267 if (func_remove(fp))
4268 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004269 }
4270 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004271 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004272 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004273 }
4274}
4275
4276/*
4277 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004278 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004279 */
4280 void
4281func_unref(char_u *name)
4282{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004283 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004284
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004285 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004286 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004287 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004288 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004289 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004290#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004291 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004292#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004293 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004294 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004295 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004296}
4297
4298/*
4299 * Unreference a Function: decrement the reference count and free it when it
4300 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004301 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004302 */
4303 void
4304func_ptr_unref(ufunc_T *fp)
4305{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004306 if (fp != NULL && (--fp->uf_refcount <= 0
4307 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
4308 && fp->uf_partial->pt_refcount <= 1
4309 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02004310 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004311 // Only delete it when it's not being used. Otherwise it's done
4312 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004313 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004314 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004315 }
4316}
4317
4318/*
4319 * Count a reference to a Function.
4320 */
4321 void
4322func_ref(char_u *name)
4323{
4324 ufunc_T *fp;
4325
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004326 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004327 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004328 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004329 if (fp != NULL)
4330 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004331 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01004332 // Only give an error for a numbered function.
4333 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01004334 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004335}
4336
4337/*
4338 * Count a reference to a Function.
4339 */
4340 void
4341func_ptr_ref(ufunc_T *fp)
4342{
4343 if (fp != NULL)
4344 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004345}
4346
4347/*
4348 * Return TRUE if items in "fc" do not have "copyID". That means they are not
4349 * referenced from anywhere that is in use.
4350 */
4351 static int
4352can_free_funccal(funccall_T *fc, int copyID)
4353{
4354 return (fc->l_varlist.lv_copyID != copyID
4355 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004356 && fc->l_avars.dv_copyID != copyID
4357 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004358}
4359
4360/*
4361 * ":return [expr]"
4362 */
4363 void
4364ex_return(exarg_T *eap)
4365{
4366 char_u *arg = eap->arg;
4367 typval_T rettv;
4368 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004369 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004370
4371 if (current_funccal == NULL)
4372 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004373 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004374 return;
4375 }
4376
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004377 CLEAR_FIELD(evalarg);
4378 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
4379
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004380 if (eap->skip)
4381 ++emsg_skip;
4382
4383 eap->nextcmd = NULL;
4384 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004385 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004386 {
4387 if (!eap->skip)
4388 returning = do_return(eap, FALSE, TRUE, &rettv);
4389 else
4390 clear_tv(&rettv);
4391 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004392 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004393 else if (!eap->skip)
4394 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004395 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01004396 update_force_abort();
4397
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004398 /*
4399 * Return unless the expression evaluation has been cancelled due to an
4400 * aborting error, an interrupt, or an exception.
4401 */
4402 if (!aborting())
4403 returning = do_return(eap, FALSE, TRUE, NULL);
4404 }
4405
Bram Moolenaare38eab22019-12-05 21:50:01 +01004406 // When skipping or the return gets pending, advance to the next command
4407 // in this line (!returning). Otherwise, ignore the rest of the line.
4408 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004409 if (returning)
4410 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004411 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004412 eap->nextcmd = check_nextcmd(arg);
4413
4414 if (eap->skip)
4415 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02004416 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004417}
4418
4419/*
4420 * ":1,25call func(arg1, arg2)" function call.
4421 */
4422 void
4423ex_call(exarg_T *eap)
4424{
4425 char_u *arg = eap->arg;
4426 char_u *startarg;
4427 char_u *name;
4428 char_u *tofree;
4429 int len;
4430 typval_T rettv;
4431 linenr_T lnum;
4432 int doesrange;
4433 int failed = FALSE;
4434 funcdict_T fudi;
4435 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004436 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004437 type_T *type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004438
Bram Moolenaare6b53242020-07-01 17:28:33 +02004439 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004440 if (eap->skip)
4441 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004442 // trans_function_name() doesn't work well when skipping, use eval0()
4443 // instead to skip to any following command, e.g. for:
4444 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004445 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004446 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004447 clear_tv(&rettv);
4448 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004449 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004450 return;
4451 }
4452
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004453 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
4454 &fudi, &partial, in_vim9script() ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004455 if (fudi.fd_newkey != NULL)
4456 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004457 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004458 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004459 vim_free(fudi.fd_newkey);
4460 }
4461 if (tofree == NULL)
4462 return;
4463
Bram Moolenaare38eab22019-12-05 21:50:01 +01004464 // Increase refcount on dictionary, it could get deleted when evaluating
4465 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004466 if (fudi.fd_dict != NULL)
4467 ++fudi.fd_dict->dv_refcount;
4468
Bram Moolenaare38eab22019-12-05 21:50:01 +01004469 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
4470 // contents. For VAR_PARTIAL get its partial, unless we already have one
4471 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004472 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004473 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
4474 in_vim9script() && type == NULL ? &type : NULL, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004475
Bram Moolenaare38eab22019-12-05 21:50:01 +01004476 // Skip white space to allow ":call func ()". Not good, but required for
4477 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004478 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004479 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004480
4481 if (*startarg != '(')
4482 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004484 goto end;
4485 }
4486
4487 /*
4488 * When skipping, evaluate the function once, to find the end of the
4489 * arguments.
4490 * When the function takes a range, this is discovered after the first
4491 * call, and the loop is broken.
4492 */
4493 if (eap->skip)
4494 {
4495 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004496 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004497 }
4498 else
4499 lnum = eap->line1;
4500 for ( ; lnum <= eap->line2; ++lnum)
4501 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004502 funcexe_T funcexe;
4503
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004504 if (!eap->skip && eap->addr_count > 0)
4505 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004506 if (lnum > curbuf->b_ml.ml_line_count)
4507 {
4508 // If the function deleted lines or switched to another buffer
4509 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004510 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004511 break;
4512 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004513 curwin->w_cursor.lnum = lnum;
4514 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004515 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004516 }
4517 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004518
Bram Moolenaara80faa82020-04-12 19:37:17 +02004519 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004520 funcexe.firstline = eap->line1;
4521 funcexe.lastline = eap->line2;
4522 funcexe.doesrange = &doesrange;
4523 funcexe.evaluate = !eap->skip;
4524 funcexe.partial = partial;
4525 funcexe.selfdict = fudi.fd_dict;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004526 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004527 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004528 {
4529 failed = TRUE;
4530 break;
4531 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01004532 if (has_watchexpr())
4533 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004534
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004535 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004536 if (handle_subscript(&arg, &rettv,
4537 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004538 {
4539 failed = TRUE;
4540 break;
4541 }
4542
4543 clear_tv(&rettv);
4544 if (doesrange || eap->skip)
4545 break;
4546
Bram Moolenaare38eab22019-12-05 21:50:01 +01004547 // Stop when immediately aborting on error, or when an interrupt
4548 // occurred or an exception was thrown but not caught.
4549 // get_func_tv() returned OK, so that the check for trailing
4550 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004551 if (aborting())
4552 break;
4553 }
4554 if (eap->skip)
4555 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004556 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004557
Bram Moolenaare51bb172020-02-16 19:42:23 +01004558 // When inside :try we need to check for following "| catch".
4559 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004560 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004561 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02004562 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004563 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004564 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004565 if (!failed)
4566 {
4567 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004568 semsg(_(e_trailing_arg), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004569 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004570 }
4571 else
4572 eap->nextcmd = check_nextcmd(arg);
4573 }
4574
4575end:
4576 dict_unref(fudi.fd_dict);
4577 vim_free(tofree);
4578}
4579
4580/*
4581 * Return from a function. Possibly makes the return pending. Also called
4582 * for a pending return at the ":endtry" or after returning from an extra
4583 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
4584 * when called due to a ":return" command. "rettv" may point to a typval_T
4585 * with the return rettv. Returns TRUE when the return can be carried out,
4586 * FALSE when the return gets pending.
4587 */
4588 int
4589do_return(
4590 exarg_T *eap,
4591 int reanimate,
4592 int is_cmd,
4593 void *rettv)
4594{
4595 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01004596 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004597
4598 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004599 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004600 current_funccal->returned = FALSE;
4601
4602 /*
4603 * Cleanup (and inactivate) conditionals, but stop when a try conditional
4604 * not in its finally clause (which then is to be executed next) is found.
4605 * In this case, make the ":return" pending for execution at the ":endtry".
4606 * Otherwise, return normally.
4607 */
4608 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
4609 if (idx >= 0)
4610 {
4611 cstack->cs_pending[idx] = CSTP_RETURN;
4612
4613 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004614 // A pending return again gets pending. "rettv" points to an
4615 // allocated variable with the rettv of the original ":return"'s
4616 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004617 cstack->cs_rettv[idx] = rettv;
4618 else
4619 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004620 // When undoing a return in order to make it pending, get the stored
4621 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004622 if (reanimate)
4623 rettv = current_funccal->rettv;
4624
4625 if (rettv != NULL)
4626 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004627 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004628 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
4629 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
4630 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004631 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004632 }
4633 else
4634 cstack->cs_rettv[idx] = NULL;
4635
4636 if (reanimate)
4637 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004638 // The pending return value could be overwritten by a ":return"
4639 // without argument in a finally clause; reset the default
4640 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004641 current_funccal->rettv->v_type = VAR_NUMBER;
4642 current_funccal->rettv->vval.v_number = 0;
4643 }
4644 }
4645 report_make_pending(CSTP_RETURN, rettv);
4646 }
4647 else
4648 {
4649 current_funccal->returned = TRUE;
4650
Bram Moolenaare38eab22019-12-05 21:50:01 +01004651 // If the return is carried out now, store the return value. For
4652 // a return immediately after reanimation, the value is already
4653 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004654 if (!reanimate && rettv != NULL)
4655 {
4656 clear_tv(current_funccal->rettv);
4657 *current_funccal->rettv = *(typval_T *)rettv;
4658 if (!is_cmd)
4659 vim_free(rettv);
4660 }
4661 }
4662
4663 return idx < 0;
4664}
4665
4666/*
4667 * Free the variable with a pending return value.
4668 */
4669 void
4670discard_pending_return(void *rettv)
4671{
4672 free_tv((typval_T *)rettv);
4673}
4674
4675/*
4676 * Generate a return command for producing the value of "rettv". The result
4677 * is an allocated string. Used by report_pending() for verbose messages.
4678 */
4679 char_u *
4680get_return_cmd(void *rettv)
4681{
4682 char_u *s = NULL;
4683 char_u *tofree = NULL;
4684 char_u numbuf[NUMBUFLEN];
4685
4686 if (rettv != NULL)
4687 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
4688 if (s == NULL)
4689 s = (char_u *)"";
4690
4691 STRCPY(IObuff, ":return ");
4692 STRNCPY(IObuff + 8, s, IOSIZE - 8);
4693 if (STRLEN(s) + 8 >= IOSIZE)
4694 STRCPY(IObuff + IOSIZE - 4, "...");
4695 vim_free(tofree);
4696 return vim_strsave(IObuff);
4697}
4698
4699/*
4700 * Get next function line.
4701 * Called by do_cmdline() to get the next line.
4702 * Returns allocated string, or NULL for end of function.
4703 */
4704 char_u *
4705get_func_line(
4706 int c UNUSED,
4707 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02004708 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004709 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004710{
4711 funccall_T *fcp = (funccall_T *)cookie;
4712 ufunc_T *fp = fcp->func;
4713 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004714 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004715
Bram Moolenaare38eab22019-12-05 21:50:01 +01004716 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004717 if (fcp->dbg_tick != debug_tick)
4718 {
4719 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004720 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004721 fcp->dbg_tick = debug_tick;
4722 }
4723#ifdef FEAT_PROFILE
4724 if (do_profiling == PROF_YES)
4725 func_line_end(cookie);
4726#endif
4727
4728 gap = &fp->uf_lines;
4729 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4730 || fcp->returned)
4731 retval = NULL;
4732 else
4733 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004734 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004735 while (fcp->linenr < gap->ga_len
4736 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
4737 ++fcp->linenr;
4738 if (fcp->linenr >= gap->ga_len)
4739 retval = NULL;
4740 else
4741 {
4742 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004743 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004744#ifdef FEAT_PROFILE
4745 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01004746 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004747#endif
4748 }
4749 }
4750
Bram Moolenaare38eab22019-12-05 21:50:01 +01004751 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004752 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004753 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004754 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004755 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004756 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004757 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004758 fcp->dbg_tick = debug_tick;
4759 }
4760
4761 return retval;
4762}
4763
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004764/*
4765 * Return TRUE if the currently active function should be ended, because a
4766 * return was encountered or an error occurred. Used inside a ":while".
4767 */
4768 int
4769func_has_ended(void *cookie)
4770{
4771 funccall_T *fcp = (funccall_T *)cookie;
4772
Bram Moolenaare38eab22019-12-05 21:50:01 +01004773 // Ignore the "abort" flag if the abortion behavior has been changed due to
4774 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004775 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4776 || fcp->returned);
4777}
4778
4779/*
4780 * return TRUE if cookie indicates a function which "abort"s on errors.
4781 */
4782 int
4783func_has_abort(
4784 void *cookie)
4785{
4786 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
4787}
4788
4789
4790/*
4791 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4792 * Don't do this when "Func" is already a partial that was bound
4793 * explicitly (pt_auto is FALSE).
4794 * Changes "rettv" in-place.
4795 * Returns the updated "selfdict_in".
4796 */
4797 dict_T *
4798make_partial(dict_T *selfdict_in, typval_T *rettv)
4799{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004800 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004801 char_u *tofree = NULL;
4802 ufunc_T *fp;
4803 char_u fname_buf[FLEN_FIXED + 1];
4804 int error;
4805 dict_T *selfdict = selfdict_in;
4806
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004807 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4808 fp = rettv->vval.v_partial->pt_func;
4809 else
4810 {
4811 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4812 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004813 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004814 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004815 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004816 vim_free(tofree);
4817 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004818
4819 if (fp != NULL && (fp->uf_flags & FC_DICT))
4820 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004821 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004822
4823 if (pt != NULL)
4824 {
4825 pt->pt_refcount = 1;
4826 pt->pt_dict = selfdict;
4827 pt->pt_auto = TRUE;
4828 selfdict = NULL;
4829 if (rettv->v_type == VAR_FUNC)
4830 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004831 // Just a function: Take over the function name and use
4832 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004833 pt->pt_name = rettv->vval.v_string;
4834 }
4835 else
4836 {
4837 partial_T *ret_pt = rettv->vval.v_partial;
4838 int i;
4839
Bram Moolenaare38eab22019-12-05 21:50:01 +01004840 // Partial: copy the function name, use selfdict and copy
4841 // args. Can't take over name or args, the partial might
4842 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004843 if (ret_pt->pt_name != NULL)
4844 {
4845 pt->pt_name = vim_strsave(ret_pt->pt_name);
4846 func_ref(pt->pt_name);
4847 }
4848 else
4849 {
4850 pt->pt_func = ret_pt->pt_func;
4851 func_ptr_ref(pt->pt_func);
4852 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004853 if (ret_pt->pt_argc > 0)
4854 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004855 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004856 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004857 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004858 pt->pt_argc = 0;
4859 else
4860 {
4861 pt->pt_argc = ret_pt->pt_argc;
4862 for (i = 0; i < pt->pt_argc; i++)
4863 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4864 }
4865 }
4866 partial_unref(ret_pt);
4867 }
4868 rettv->v_type = VAR_PARTIAL;
4869 rettv->vval.v_partial = pt;
4870 }
4871 }
4872 return selfdict;
4873}
4874
4875/*
4876 * Return the name of the executed function.
4877 */
4878 char_u *
4879func_name(void *cookie)
4880{
4881 return ((funccall_T *)cookie)->func->uf_name;
4882}
4883
4884/*
4885 * Return the address holding the next breakpoint line for a funccall cookie.
4886 */
4887 linenr_T *
4888func_breakpoint(void *cookie)
4889{
4890 return &((funccall_T *)cookie)->breakpoint;
4891}
4892
4893/*
4894 * Return the address holding the debug tick for a funccall cookie.
4895 */
4896 int *
4897func_dbg_tick(void *cookie)
4898{
4899 return &((funccall_T *)cookie)->dbg_tick;
4900}
4901
4902/*
4903 * Return the nesting level for a funccall cookie.
4904 */
4905 int
4906func_level(void *cookie)
4907{
4908 return ((funccall_T *)cookie)->level;
4909}
4910
4911/*
4912 * Return TRUE when a function was ended by a ":return" command.
4913 */
4914 int
4915current_func_returned(void)
4916{
4917 return current_funccal->returned;
4918}
4919
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004920 int
4921free_unref_funccal(int copyID, int testing)
4922{
4923 int did_free = FALSE;
4924 int did_free_funccal = FALSE;
4925 funccall_T *fc, **pfc;
4926
4927 for (pfc = &previous_funccal; *pfc != NULL; )
4928 {
4929 if (can_free_funccal(*pfc, copyID))
4930 {
4931 fc = *pfc;
4932 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004933 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004934 did_free = TRUE;
4935 did_free_funccal = TRUE;
4936 }
4937 else
4938 pfc = &(*pfc)->caller;
4939 }
4940 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004941 // When a funccal was freed some more items might be garbage
4942 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004943 (void)garbage_collect(testing);
4944
4945 return did_free;
4946}
4947
4948/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004949 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004950 */
4951 static funccall_T *
4952get_funccal(void)
4953{
4954 int i;
4955 funccall_T *funccal;
4956 funccall_T *temp_funccal;
4957
4958 funccal = current_funccal;
4959 if (debug_backtrace_level > 0)
4960 {
4961 for (i = 0; i < debug_backtrace_level; i++)
4962 {
4963 temp_funccal = funccal->caller;
4964 if (temp_funccal)
4965 funccal = temp_funccal;
4966 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004967 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004968 debug_backtrace_level = i;
4969 }
4970 }
4971 return funccal;
4972}
4973
4974/*
4975 * Return the hashtable used for local variables in the current funccal.
4976 * Return NULL if there is no current funccal.
4977 */
4978 hashtab_T *
4979get_funccal_local_ht()
4980{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004982 return NULL;
4983 return &get_funccal()->l_vars.dv_hashtab;
4984}
4985
4986/*
4987 * Return the l: scope variable.
4988 * Return NULL if there is no current funccal.
4989 */
4990 dictitem_T *
4991get_funccal_local_var()
4992{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004994 return NULL;
4995 return &get_funccal()->l_vars_var;
4996}
4997
4998/*
4999 * Return the hashtable used for argument in the current funccal.
5000 * Return NULL if there is no current funccal.
5001 */
5002 hashtab_T *
5003get_funccal_args_ht()
5004{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005006 return NULL;
5007 return &get_funccal()->l_avars.dv_hashtab;
5008}
5009
5010/*
5011 * Return the a: scope variable.
5012 * Return NULL if there is no current funccal.
5013 */
5014 dictitem_T *
5015get_funccal_args_var()
5016{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005017 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005018 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01005019 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005020}
5021
5022/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005023 * List function variables, if there is a function.
5024 */
5025 void
5026list_func_vars(int *first)
5027{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005029 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01005030 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005031}
5032
5033/*
5034 * If "ht" is the hashtable for local variables in the current funccal, return
5035 * the dict that contains it.
5036 * Otherwise return NULL.
5037 */
5038 dict_T *
5039get_current_funccal_dict(hashtab_T *ht)
5040{
5041 if (current_funccal != NULL
5042 && ht == &current_funccal->l_vars.dv_hashtab)
5043 return &current_funccal->l_vars;
5044 return NULL;
5045}
5046
5047/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005048 * Search hashitem in parent scope.
5049 */
5050 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005051find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005052{
5053 funccall_T *old_current_funccal = current_funccal;
5054 hashtab_T *ht;
5055 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005056 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005057
5058 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5059 return NULL;
5060
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02005061 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005062 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02005063 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005064 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005065 ht = find_var_ht(name, &varname);
5066 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02005067 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005068 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02005069 if (!HASHITEM_EMPTY(hi))
5070 {
5071 *pht = ht;
5072 break;
5073 }
5074 }
5075 if (current_funccal == current_funccal->func->uf_scoped)
5076 break;
5077 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005078 }
5079 current_funccal = old_current_funccal;
5080
5081 return hi;
5082}
5083
5084/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005085 * Search variable in parent scope.
5086 */
5087 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005088find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005089{
5090 dictitem_T *v = NULL;
5091 funccall_T *old_current_funccal = current_funccal;
5092 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005093 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005094
5095 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5096 return NULL;
5097
Bram Moolenaare38eab22019-12-05 21:50:01 +01005098 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005099 current_funccal = current_funccal->func->uf_scoped;
5100 while (current_funccal)
5101 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005102 ht = find_var_ht(name, &varname);
5103 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005104 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005105 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005106 if (v != NULL)
5107 break;
5108 }
5109 if (current_funccal == current_funccal->func->uf_scoped)
5110 break;
5111 current_funccal = current_funccal->func->uf_scoped;
5112 }
5113 current_funccal = old_current_funccal;
5114
5115 return v;
5116}
5117
5118/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005119 * Set "copyID + 1" in previous_funccal and callers.
5120 */
5121 int
5122set_ref_in_previous_funccal(int copyID)
5123{
5124 int abort = FALSE;
5125 funccall_T *fc;
5126
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005127 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005128 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005129 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005130 abort = abort
5131 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
5132 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005133 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005134 }
5135 return abort;
5136}
5137
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005138 static int
5139set_ref_in_funccal(funccall_T *fc, int copyID)
5140{
5141 int abort = FALSE;
5142
5143 if (fc->fc_copyID != copyID)
5144 {
5145 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005146 abort = abort
5147 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
5148 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005149 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005150 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005151 }
5152 return abort;
5153}
5154
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005155/*
5156 * Set "copyID" in all local vars and arguments in the call stack.
5157 */
5158 int
5159set_ref_in_call_stack(int copyID)
5160{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005161 int abort = FALSE;
5162 funccall_T *fc;
5163 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005164
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005165 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005166 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005167
5168 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005169 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
5170 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005171 abort = abort || set_ref_in_funccal(fc, copyID);
5172
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005173 return abort;
5174}
5175
5176/*
5177 * Set "copyID" in all functions available by name.
5178 */
5179 int
5180set_ref_in_functions(int copyID)
5181{
5182 int todo;
5183 hashitem_T *hi = NULL;
5184 int abort = FALSE;
5185 ufunc_T *fp;
5186
5187 todo = (int)func_hashtab.ht_used;
5188 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005189 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005190 if (!HASHITEM_EMPTY(hi))
5191 {
5192 --todo;
5193 fp = HI2UF(hi);
5194 if (!func_name_refcount(fp->uf_name))
5195 abort = abort || set_ref_in_func(NULL, fp, copyID);
5196 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005197 }
5198 return abort;
5199}
5200
5201/*
5202 * Set "copyID" in all function arguments.
5203 */
5204 int
5205set_ref_in_func_args(int copyID)
5206{
5207 int i;
5208 int abort = FALSE;
5209
5210 for (i = 0; i < funcargs.ga_len; ++i)
5211 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5212 copyID, NULL, NULL);
5213 return abort;
5214}
5215
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005216/*
5217 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005218 * Returns TRUE if setting references failed somehow.
5219 */
5220 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005221set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005222{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005223 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005224 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005225 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005226 char_u fname_buf[FLEN_FIXED + 1];
5227 char_u *tofree = NULL;
5228 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005229 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005230
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005231 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005232 return FALSE;
5233
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005234 if (fp_in == NULL)
5235 {
5236 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005237 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005238 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005239 if (fp != NULL)
5240 {
5241 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005242 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005243 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005244
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005245 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005246 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005247}
5248
Bram Moolenaare38eab22019-12-05 21:50:01 +01005249#endif // FEAT_EVAL