blob: 5fab412b40c7d852a2d4803a82d144dad96b40b8 [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 Moolenaar8a7d6542020-01-26 15:56:19 +010058 * Return a pointer to after the type.
59 * When something is wrong return "arg".
60 */
61 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010062one_function_arg(
63 char_u *arg,
64 garray_T *newargs,
65 garray_T *argtypes,
66 int types_optional,
67 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010068{
Bram Moolenaar6e949782020-04-13 17:21:00 +020069 char_u *p = arg;
70 char_u *arg_copy = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010071
72 while (ASCII_ISALNUM(*p) || *p == '_')
73 ++p;
74 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020075 || (argtypes == NULL
76 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
77 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010078 {
79 if (!skip)
80 semsg(_("E125: Illegal argument: %s"), arg);
81 return arg;
82 }
83 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
84 return arg;
85 if (newargs != NULL)
86 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087 int c;
88 int i;
89
90 c = *p;
91 *p = NUL;
92 arg_copy = vim_strsave(arg);
93 if (arg_copy == NULL)
94 {
95 *p = c;
96 return arg;
97 }
98
99 // Check for duplicate argument name.
100 for (i = 0; i < newargs->ga_len; ++i)
101 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
102 {
103 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
104 vim_free(arg_copy);
105 return arg;
106 }
107 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
108 newargs->ga_len++;
109
110 *p = c;
111 }
112
113 // get any type from "arg: type"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100114 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 {
116 char_u *type = NULL;
117
Bram Moolenaar6e949782020-04-13 17:21:00 +0200118 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
119 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200120 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200121 arg_copy == NULL ? arg : arg_copy);
122 p = skipwhite(p);
123 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 if (*p == ':')
125 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200126 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100127 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200128 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200129 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200130 return arg;
131 }
132 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200133 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100134 if (!skip)
135 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100136 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100137 else if (*skipwhite(p) != '=' && !types_optional)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200138 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200139 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200140 arg_copy == NULL ? arg : arg_copy);
141 return arg;
142 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100143 if (!skip)
144 {
145 if (type == NULL && types_optional)
146 // lambda arguments default to "any" type
147 type = vim_strsave((char_u *)"any");
148 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150 }
151
152 return p;
153}
154
155/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200156 * Get function arguments.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100157 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200158 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200160get_function_args(
161 char_u **argp,
162 char_u endchar,
163 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100165 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200166 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200167 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200168 int skip,
169 exarg_T *eap,
170 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200171{
172 int mustend = FALSE;
173 char_u *arg = *argp;
174 char_u *p = arg;
175 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200176 int any_default = FALSE;
177 char_u *expr;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200178 char_u *whitep = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200179
180 if (newargs != NULL)
181 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 if (argtypes != NULL)
183 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200184 if (default_args != NULL)
185 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200186
187 if (varargs != NULL)
188 *varargs = FALSE;
189
190 /*
191 * Isolate the arguments: "arg1, arg2, ...)"
192 */
193 while (*p != endchar)
194 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200195 while (eap != NULL && eap->getline != NULL
196 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200197 {
198 char_u *theline;
199
200 // End of the line, get the next one.
201 theline = eap->getline(':', eap->cookie, 0, TRUE);
202 if (theline == NULL)
203 break;
204 vim_free(*line_to_free);
205 *line_to_free = theline;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200206 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200207 p = skipwhite(theline);
208 }
209
210 if (mustend && *p != endchar)
211 {
212 if (!skip)
213 semsg(_(e_invarg2), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100214 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200215 }
216 if (*p == endchar)
217 break;
218
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200219 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
220 {
221 if (varargs != NULL)
222 *varargs = TRUE;
223 p += 3;
224 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225
226 if (argtypes != NULL)
227 {
228 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200229 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100231 if (!skip)
232 emsg(_(e_missing_name_after_dots));
233 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 }
235
236 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100237 p = one_function_arg(p, newargs, argtypes, types_optional,
238 skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 if (p == arg)
240 break;
241 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200242 }
243 else
244 {
245 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100246 p = one_function_arg(p, newargs, argtypes, types_optional, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200248 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200249
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200250 if (*skipwhite(p) == '=' && default_args != NULL)
251 {
252 typval_T rettv;
253
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100254 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200255 any_default = TRUE;
256 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200257 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200258 p = skipwhite(p);
259 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200260 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200261 {
262 if (ga_grow(default_args, 1) == FAIL)
263 goto err_ret;
264
265 // trim trailing whitespace
266 while (p > expr && VIM_ISWHITE(p[-1]))
267 p--;
268 c = *p;
269 *p = NUL;
270 expr = vim_strsave(expr);
271 if (expr == NULL)
272 {
273 *p = c;
274 goto err_ret;
275 }
276 ((char_u **)(default_args->ga_data))
277 [default_args->ga_len] = expr;
278 default_args->ga_len++;
279 *p = c;
280 }
281 else
282 mustend = TRUE;
283 }
284 else if (any_default)
285 {
286 emsg(_("E989: Non-default argument follows default argument"));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200287 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200288 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200289 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200290 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200291 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200292 // Don't give this error when skipping, it makes the "->" not
293 // found in "{k,v -> x}" and give a confusing error.
294 if (!skip && in_vim9script()
295 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
296 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200297 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200298 goto err_ret;
299 }
300 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200301 else
302 mustend = TRUE;
303 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200304 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200305 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200306 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200307
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200308 if (*p != endchar)
309 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100310 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200311
312 *argp = p;
313 return OK;
314
315err_ret:
316 if (newargs != NULL)
317 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200318 if (default_args != NULL)
319 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200320 return FAIL;
321}
322
323/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100324 * Parse the argument types, filling "fp->uf_arg_types".
325 * Return OK or FAIL.
326 */
327 static int
328parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
329{
330 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
331 if (argtypes->ga_len > 0)
332 {
333 // When "varargs" is set the last name/type goes into uf_va_name
334 // and uf_va_type.
335 int len = argtypes->ga_len - (varargs ? 1 : 0);
336
337 if (len > 0)
338 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
339 if (fp->uf_arg_types != NULL)
340 {
341 int i;
342 type_T *type;
343
344 for (i = 0; i < len; ++ i)
345 {
346 char_u *p = ((char_u **)argtypes->ga_data)[i];
347
348 if (p == NULL)
349 // will get the type from the default value
350 type = &t_unknown;
351 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100352 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100353 if (type == NULL)
354 return FAIL;
355 fp->uf_arg_types[i] = type;
356 }
357 }
358 if (varargs)
359 {
360 char_u *p;
361
362 // Move the last argument "...name: type" to uf_va_name and
363 // uf_va_type.
364 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
365 [fp->uf_args.ga_len - 1];
366 --fp->uf_args.ga_len;
367 p = ((char_u **)argtypes->ga_data)[len];
368 if (p == NULL)
369 // todo: get type from default value
370 fp->uf_va_type = &t_any;
371 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100372 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100373 if (fp->uf_va_type == NULL)
374 return FAIL;
375 }
376 }
377 return OK;
378}
379
380/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200381 * Register function "fp" as using "current_funccal" as its scope.
382 */
383 static int
384register_closure(ufunc_T *fp)
385{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200386 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100387 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200388 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200389 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200390 fp->uf_scoped = current_funccal;
391 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200392
Bram Moolenaar58016442016-07-31 18:30:22 +0200393 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
394 return FAIL;
395 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
396 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200397 return OK;
398}
399
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100400 static void
401set_ufunc_name(ufunc_T *fp, char_u *name)
402{
403 STRCPY(fp->uf_name, name);
404
405 if (name[0] == K_SPECIAL)
406 {
407 fp->uf_name_exp = alloc(STRLEN(name) + 3);
408 if (fp->uf_name_exp != NULL)
409 {
410 STRCPY(fp->uf_name_exp, "<SNR>");
411 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
412 }
413 }
414}
415
Bram Moolenaar58016442016-07-31 18:30:22 +0200416/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200417 * Get a name for a lambda. Returned in static memory.
418 */
419 char_u *
420get_lambda_name(void)
421{
422 static char_u name[30];
423 static int lambda_no = 0;
424
425 sprintf((char*)name, "<lambda>%d", ++lambda_no);
426 return name;
427}
428
Bram Moolenaar801ab062020-06-25 19:27:56 +0200429#if defined(FEAT_LUA) || defined(PROTO)
430/*
431 * Registers a native C callback which can be called from Vim script.
432 * Returns the name of the Vim script function.
433 */
434 char_u *
435register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
436{
437 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200438 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200439
440 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
441 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200442 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200443
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200444 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200445 fp->uf_refcount = 1;
446 fp->uf_varargs = TRUE;
447 fp->uf_flags = FC_CFUNC;
448 fp->uf_calls = 0;
449 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200450 fp->uf_cb = cb;
451 fp->uf_cb_free = cb_free;
452 fp->uf_cb_state = state;
453
454 set_ufunc_name(fp, name);
455 hash_add(&func_hashtab, UF2HIKEY(fp));
456
457 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200458}
459#endif
460
Bram Moolenaar04b12692020-05-04 23:24:44 +0200461/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100462 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100463 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100464 * If "white_error" is not NULL check for correct use of white space and set
465 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100466 * Return NULL if no valid arrow found.
467 */
468 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100469skip_arrow(
470 char_u *start,
471 int equal_arrow,
472 char_u **ret_type,
473 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100474{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100475 char_u *s = start;
476 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100477
478 if (equal_arrow)
479 {
480 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100481 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100482 if (white_error != NULL && !VIM_ISWHITE(s[1]))
483 {
484 *white_error = TRUE;
485 semsg(_(e_white_space_required_after_str), ":");
486 return NULL;
487 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100488 s = skipwhite(s + 1);
489 *ret_type = s;
490 s = skip_type(s, TRUE);
491 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100492 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100493 s = skipwhite(s);
494 if (*s != '=')
495 return NULL;
496 ++s;
497 }
498 if (*s != '>')
499 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100500 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
501 || !IS_WHITE_OR_NUL(s[1])))
502 {
503 *white_error = TRUE;
504 semsg(_(e_white_space_required_before_and_after_str),
505 equal_arrow ? "=>" : "->");
506 return NULL;
507 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100508 return skipwhite(s + 1);
509}
510
511/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200512 * Parse a lambda expression and get a Funcref from "*arg".
Bram Moolenaar65c44152020-12-24 15:14:01 +0100513 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100514 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200515 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
516 */
517 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100518get_lambda_tv(
519 char_u **arg,
520 typval_T *rettv,
521 int types_optional,
522 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200523{
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200524 int evaluate = evalarg != NULL
525 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200526 garray_T newargs;
527 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200528 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100529 garray_T argtypes;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200530 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100531 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200532 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100533 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200534 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200535 char_u *s;
536 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200537 int *old_eval_lavars = eval_lavars_used;
538 int eval_lavars = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200539 char_u *tofree = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100540 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100541 int white_error = FALSE;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100542
543 if (equal_arrow && !in_vim9script())
544 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200545
546 ga_init(&newargs);
547 ga_init(&newlines);
548
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100549 // First, check if this is really a lambda expression. "->" or "=>" must
550 // be found after the arguments.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200551 s = skipwhite(*arg + 1);
Bram Moolenaar65c44152020-12-24 15:14:01 +0100552 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100553 types_optional ? &argtypes : NULL, types_optional,
554 NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100555 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100556 {
557 if (types_optional)
558 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200559 return NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100560 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200561
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100562 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200563 if (evaluate)
564 pnewargs = &newargs;
565 else
566 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200567 *arg = skipwhite(*arg + 1);
Bram Moolenaar65c44152020-12-24 15:14:01 +0100568 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100569 types_optional ? &argtypes : NULL, types_optional,
570 &varargs, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100571 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100572 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaare462f522020-12-27 14:43:30 +0100573 equal_arrow || in_vim9script() ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100574 {
575 if (types_optional)
576 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +0100577 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100578 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100579 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100580 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200581
Bram Moolenaare38eab22019-12-05 21:50:01 +0100582 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200583 if (evaluate)
584 eval_lavars_used = &eval_lavars;
585
Bram Moolenaar65c44152020-12-24 15:14:01 +0100586 *arg = skipwhite_and_linebreak(*arg, evalarg);
587
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100588 // Recognize "{" as the start of a function body.
589 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +0100590 {
591 // TODO: process the function body upto the "}".
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100592 // Return type is required then.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100593 emsg("Lambda function body not supported yet");
594 goto errret;
595 }
596
Bram Moolenaare38eab22019-12-05 21:50:01 +0100597 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200598 start = *arg;
599 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200600 if (ret == FAIL)
601 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200602 if (evalarg != NULL)
603 {
604 // avoid that the expression gets freed when another line break follows
605 tofree = evalarg->eval_tofree;
606 evalarg->eval_tofree = NULL;
607 }
608
Bram Moolenaar65c44152020-12-24 15:14:01 +0100609 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +0100610 {
Bram Moolenaar65c44152020-12-24 15:14:01 +0100611 *arg = skipwhite_and_linebreak(*arg, evalarg);
612 if (**arg != '}')
613 {
614 semsg(_("E451: Expected }: %s"), *arg);
615 goto errret;
616 }
617 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100618 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200619
620 if (evaluate)
621 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200622 int len;
623 int flags = 0;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200624 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200625 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200626
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200627 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200628 if (fp == NULL)
629 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200630 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200631 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200632 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200633 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200634
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200635 ga_init2(&newlines, (int)sizeof(char_u *), 1);
636 if (ga_grow(&newlines, 1) == FAIL)
637 goto errret;
638
Bram Moolenaare38eab22019-12-05 21:50:01 +0100639 // Add "return " before the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200640 len = 7 + (int)(end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200641 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200642 if (p == NULL)
643 goto errret;
644 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
645 STRCPY(p, "return ");
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200646 vim_strncpy(p + 7, start, end - start);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200647 if (strstr((char *)p + 7, "a:") == NULL)
648 // No a: variables are used for sure.
649 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200650
651 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100652 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200653 hash_add(&func_hashtab, UF2HIKEY(fp));
654 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200655 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100656 if (types_optional)
657 {
658 if (parse_argument_types(fp, &argtypes, FALSE) == FAIL)
659 goto errret;
660 if (ret_type != NULL)
661 {
662 fp->uf_ret_type = parse_type(&ret_type,
663 &fp->uf_type_list, TRUE);
664 if (fp->uf_ret_type == NULL)
665 goto errret;
666 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +0100667 else
668 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100669 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100670
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200671 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200672 if (current_funccal != NULL && eval_lavars)
673 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200674 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200675 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200676 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200677 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200678
679#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200680 if (prof_def_func())
681 func_do_profile(fp);
682#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200683 if (sandbox)
684 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200686 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200687 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200688 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200689 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100690 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200691
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200692 pt->pt_func = fp;
693 pt->pt_refcount = 1;
694 rettv->vval.v_partial = pt;
695 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200696 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200697
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200698 eval_lavars_used = old_eval_lavars;
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200699 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200700 evalarg->eval_tofree = tofree;
701 else
702 vim_free(tofree);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100703 if (types_optional)
704 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200705 return OK;
706
707errret:
708 ga_clear_strings(&newargs);
709 ga_clear_strings(&newlines);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100710 if (types_optional)
711 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200712 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100713 vim_free(pt);
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200714 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200715 evalarg->eval_tofree = tofree;
716 else
717 vim_free(tofree);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200718 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200719 return FAIL;
720}
721
722/*
723 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
724 * name it contains, otherwise return "name".
725 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
726 * "partialp".
727 */
728 char_u *
729deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
730{
731 dictitem_T *v;
732 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200733 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200734
735 if (partialp != NULL)
736 *partialp = NULL;
737
738 cc = name[*lenp];
739 name[*lenp] = NUL;
740 v = find_var(name, NULL, no_autoload);
741 name[*lenp] = cc;
742 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
743 {
744 if (v->di_tv.vval.v_string == NULL)
745 {
746 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100747 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200748 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200749 s = v->di_tv.vval.v_string;
750 *lenp = (int)STRLEN(s);
751 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200752 }
753
754 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
755 {
756 partial_T *pt = v->di_tv.vval.v_partial;
757
758 if (pt == NULL)
759 {
760 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100761 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200762 }
763 if (partialp != NULL)
764 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200765 s = partial_name(pt);
766 *lenp = (int)STRLEN(s);
767 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200768 }
769
770 return name;
771}
772
773/*
774 * Give an error message with a function name. Handle <SNR> things.
775 * "ermsg" is to be passed without translation, use N_() instead of _().
776 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100777 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200778emsg_funcname(char *ermsg, char_u *name)
779{
780 char_u *p;
781
782 if (*name == K_SPECIAL)
783 p = concat_str((char_u *)"<SNR>", name + 3);
784 else
785 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100786 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200787 if (p != name)
788 vim_free(p);
789}
790
791/*
792 * Allocate a variable for the result of a function.
793 * Return OK or FAIL.
794 */
795 int
796get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200797 char_u *name, // name of the function
798 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200799 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200800 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +0200801 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200802 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200803{
804 char_u *argp;
805 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100806 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
807 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200808 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200809
810 /*
811 * Get the arguments.
812 */
813 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200814 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
815 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200816 {
Bram Moolenaare6b53242020-07-01 17:28:33 +0200817 // skip the '(' or ',' and possibly line breaks
818 argp = skipwhite_and_linebreak(argp + 1, evalarg);
819
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200820 if (*argp == ')' || *argp == ',' || *argp == NUL)
821 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +0200822 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200823 {
824 ret = FAIL;
825 break;
826 }
827 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +0200828 // The comma should come right after the argument, but this wasn't
829 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200830 if (vim9script)
831 {
832 if (*argp != ',' && *skipwhite(argp) == ',')
833 {
834 semsg(_(e_no_white_space_allowed_before_str), ",");
835 ret = FAIL;
836 break;
837 }
838 }
839 else
Bram Moolenaar9d489562020-07-30 20:08:50 +0200840 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200841 if (*argp != ',')
842 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200843 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
844 {
845 semsg(_(e_white_space_required_after_str), ",");
846 ret = FAIL;
847 break;
848 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200849 }
Bram Moolenaare6b53242020-07-01 17:28:33 +0200850 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200851 if (*argp == ')')
852 ++argp;
853 else
854 ret = FAIL;
855
856 if (ret == OK)
857 {
858 int i = 0;
859
860 if (get_vim_var_nr(VV_TESTING))
861 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100862 // Prepare for calling test_garbagecollect_now(), need to know
863 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200864 if (funcargs.ga_itemsize == 0)
865 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
866 for (i = 0; i < argcount; ++i)
867 if (ga_grow(&funcargs, 1) == OK)
868 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
869 &argvars[i];
870 }
871
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200872 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200873
874 funcargs.ga_len -= i;
875 }
876 else if (!aborting())
877 {
878 if (argcount == MAX_FUNC_ARGS)
879 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
880 else
881 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
882 }
883
884 while (--argcount >= 0)
885 clear_tv(&argvars[argcount]);
886
Bram Moolenaar8294d492020-08-10 22:40:56 +0200887 if (in_vim9script())
888 *arg = argp;
889 else
890 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200891 return ret;
892}
893
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200894/*
895 * Return TRUE if "p" starts with "<SID>" or "s:".
896 * Only works if eval_fname_script() returned non-zero for "p"!
897 */
898 static int
899eval_fname_sid(char_u *p)
900{
901 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
902}
903
904/*
905 * In a script change <SID>name() and s:name() to K_SNR 123_name().
906 * Change <SNR>123_name() to K_SNR 123_name().
907 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
908 * (slow).
909 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100910 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200911fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
912{
913 int llen;
914 char_u *fname;
915 int i;
916
917 llen = eval_fname_script(name);
918 if (llen > 0)
919 {
920 fname_buf[0] = K_SPECIAL;
921 fname_buf[1] = KS_EXTRA;
922 fname_buf[2] = (int)KE_SNR;
923 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100924 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200925 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200926 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100927 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200928 else
929 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200930 sprintf((char *)fname_buf + 3, "%ld_",
931 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200932 i = (int)STRLEN(fname_buf);
933 }
934 }
935 if (i + STRLEN(name + llen) < FLEN_FIXED)
936 {
937 STRCPY(fname_buf + i, name + llen);
938 fname = fname_buf;
939 }
940 else
941 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200942 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200943 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100944 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200945 else
946 {
947 *tofree = fname;
948 mch_memmove(fname, fname_buf, (size_t)i);
949 STRCPY(fname + i, name + llen);
950 }
951 }
952 }
953 else
954 fname = name;
955 return fname;
956}
957
958/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 * Find a function "name" in script "sid".
960 */
961 static ufunc_T *
962find_func_with_sid(char_u *name, int sid)
963{
964 hashitem_T *hi;
965 char_u buffer[200];
966
967 buffer[0] = K_SPECIAL;
968 buffer[1] = KS_EXTRA;
969 buffer[2] = (int)KE_SNR;
970 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
971 (long)sid, name);
972 hi = hash_find(&func_hashtab, buffer);
973 if (!HASHITEM_EMPTY(hi))
974 return HI2UF(hi);
975
976 return NULL;
977}
978
979/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200980 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200981 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200982 * Return NULL for unknown function.
983 */
Bram Moolenaarce658352020-07-31 23:47:12 +0200984 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200985find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200986{
987 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100988 ufunc_T *func;
989 imported_T *imported;
990
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200991 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100992 {
Bram Moolenaar333894b2020-08-01 18:53:07 +0200993 char_u *after_script = NULL;
Bram Moolenaarefa94442020-08-08 22:16:00 +0200994 long sid = 0;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200995 int find_script_local = in_vim9script()
996 && eval_isnamec1(*name) && name[1] != ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997
Bram Moolenaar95006e32020-08-29 17:47:08 +0200998 if (find_script_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001000 // Find script-local function before global one.
1001 func = find_func_with_sid(name, current_sctx.sc_sid);
1002 if (func != NULL)
1003 return func;
1004 }
1005
Bram Moolenaarefa94442020-08-08 22:16:00 +02001006 if (name[0] == K_SPECIAL
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001007 && name[1] == KS_EXTRA
1008 && name[2] == KE_SNR)
1009 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001010 // Caller changes s: to <SNR>99_name.
1011
1012 after_script = name + 3;
1013 sid = getdigits(&after_script);
Bram Moolenaarefa94442020-08-08 22:16:00 +02001014 if (*after_script == '_')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001015 ++after_script;
1016 else
1017 after_script = NULL;
1018 }
Bram Moolenaar95006e32020-08-29 17:47:08 +02001019 if (find_script_local || after_script != NULL)
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001020 {
1021 // Find imported function before global one.
Bram Moolenaarefa94442020-08-08 22:16:00 +02001022 if (after_script != NULL && sid != current_sctx.sc_sid)
1023 imported = find_imported_in_script(after_script, 0, sid);
1024 else
1025 imported = find_imported(after_script == NULL
1026 ? name : after_script, 0, cctx);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001027 if (imported != NULL && imported->imp_funcname != NULL)
1028 {
1029 hi = hash_find(&func_hashtab, imported->imp_funcname);
1030 if (!HASHITEM_EMPTY(hi))
1031 return HI2UF(hi);
1032 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 }
1034 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001035
Bram Moolenaara26b9702020-04-18 19:53:28 +02001036 hi = hash_find(&func_hashtab,
1037 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001038 if (!HASHITEM_EMPTY(hi))
1039 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001040
1041 return NULL;
1042}
1043
1044/*
1045 * Find a function by name, return pointer to it in ufuncs.
1046 * "cctx" is passed in a :def function to find imported functions.
1047 * Return NULL for unknown or dead function.
1048 */
1049 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001050find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001052 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053
1054 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
1055 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001056 return NULL;
1057}
1058
1059/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02001060 * Return TRUE if "ufunc" is a global function.
1061 */
1062 int
1063func_is_global(ufunc_T *ufunc)
1064{
1065 return ufunc->uf_name[0] != K_SPECIAL;
1066}
1067
1068/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001069 * Copy the function name of "fp" to buffer "buf".
1070 * "buf" must be able to hold the function name plus three bytes.
1071 * Takes care of script-local function names.
1072 */
1073 static void
1074cat_func_name(char_u *buf, ufunc_T *fp)
1075{
Bram Moolenaar0f769812020-09-12 18:32:34 +02001076 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001077 {
1078 STRCPY(buf, "<SNR>");
1079 STRCAT(buf, fp->uf_name + 3);
1080 }
1081 else
1082 STRCPY(buf, fp->uf_name);
1083}
1084
1085/*
1086 * Add a number variable "name" to dict "dp" with value "nr".
1087 */
1088 static void
1089add_nr_var(
1090 dict_T *dp,
1091 dictitem_T *v,
1092 char *name,
1093 varnumber_T nr)
1094{
1095 STRCPY(v->di_key, name);
1096 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1097 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
1098 v->di_tv.v_type = VAR_NUMBER;
1099 v->di_tv.v_lock = VAR_FIXED;
1100 v->di_tv.vval.v_number = nr;
1101}
1102
1103/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001104 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001105 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001106 static void
1107free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001108{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001109 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001110
1111 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1112 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001113 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001114
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001115 // When garbage collecting a funccall_T may be freed before the
1116 // function that references it, clear its uf_scoped field.
1117 // The function may have been redefined and point to another
1118 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001119 if (fp != NULL && fp->uf_scoped == fc)
1120 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001121 }
Bram Moolenaar58016442016-07-31 18:30:22 +02001122 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001123
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001124 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001125 vim_free(fc);
1126}
1127
1128/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001129 * Free "fc" and what it contains.
1130 * Can be called only when "fc" is kept beyond the period of it called,
1131 * i.e. after cleanup_function_call(fc).
1132 */
1133 static void
1134free_funccal_contents(funccall_T *fc)
1135{
1136 listitem_T *li;
1137
1138 // Free all l: variables.
1139 vars_clear(&fc->l_vars.dv_hashtab);
1140
1141 // Free all a: variables.
1142 vars_clear(&fc->l_avars.dv_hashtab);
1143
1144 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001145 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001146 clear_tv(&li->li_tv);
1147
1148 free_funccal(fc);
1149}
1150
1151/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02001152 * Handle the last part of returning from a function: free the local hashtable.
1153 * Unless it is still in use by a closure.
1154 */
1155 static void
1156cleanup_function_call(funccall_T *fc)
1157{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001158 int may_free_fc = fc->fc_refcount <= 0;
1159 int free_fc = TRUE;
1160
Bram Moolenaar6914c642017-04-01 21:21:30 +02001161 current_funccal = fc->caller;
1162
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001163 // Free all l: variables if not referred.
1164 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
1165 vars_clear(&fc->l_vars.dv_hashtab);
1166 else
1167 free_fc = FALSE;
1168
1169 // If the a:000 list and the l: and a: dicts are not referenced and
1170 // there is no closure using it, we can free the funccall_T and what's
1171 // in it.
1172 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
1173 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001174 else
1175 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001176 int todo;
1177 hashitem_T *hi;
1178 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001179
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001180 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001181
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001182 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001183 todo = (int)fc->l_avars.dv_hashtab.ht_used;
1184 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
1185 {
1186 if (!HASHITEM_EMPTY(hi))
1187 {
1188 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001189 di = HI2DI(hi);
1190 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001191 }
1192 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001193 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001194
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001195 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
1196 fc->l_varlist.lv_first = NULL;
1197 else
1198 {
1199 listitem_T *li;
1200
1201 free_fc = FALSE;
1202
1203 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001204 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02001205 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001206 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001207
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001208 if (free_fc)
1209 free_funccal(fc);
1210 else
1211 {
1212 static int made_copy = 0;
1213
1214 // "fc" is still in use. This can happen when returning "a:000",
1215 // assigning "l:" to a global variable or defining a closure.
1216 // Link "fc" in the list for garbage collection later.
1217 fc->caller = previous_funccal;
1218 previous_funccal = fc;
1219
1220 if (want_garbage_collect)
1221 // If garbage collector is ready, clear count.
1222 made_copy = 0;
1223 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001224 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001225 // We have made a lot of copies, worth 4 Mbyte. This can happen
1226 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001227 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001228 // too much memory.
1229 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001230 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001231 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001232 }
1233}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001234
1235/*
1236 * There are two kinds of function names:
1237 * 1. ordinary names, function defined with :function or :def
1238 * 2. numbered functions and lambdas
1239 * For the first we only count the name stored in func_hashtab as a reference,
1240 * using function() does not count as a reference, because the function is
1241 * looked up by name.
1242 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001243 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001244func_name_refcount(char_u *name)
1245{
1246 return isdigit(*name) || *name == '<';
1247}
1248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249/*
1250 * Unreference "fc": decrement the reference count and free it when it
1251 * becomes zero. "fp" is detached from "fc".
1252 * When "force" is TRUE we are exiting.
1253 */
1254 static void
1255funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
1256{
1257 funccall_T **pfc;
1258 int i;
1259
1260 if (fc == NULL)
1261 return;
1262
1263 if (--fc->fc_refcount <= 0 && (force || (
1264 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
1265 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
1266 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
1267 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
1268 {
1269 if (fc == *pfc)
1270 {
1271 *pfc = fc->caller;
1272 free_funccal_contents(fc);
1273 return;
1274 }
1275 }
1276 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1277 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
1278 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
1279}
1280
1281/*
1282 * Remove the function from the function hashtable. If the function was
1283 * deleted while it still has references this was already done.
1284 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
1285 */
1286 static int
1287func_remove(ufunc_T *fp)
1288{
1289 hashitem_T *hi;
1290
1291 // Return if it was already virtually deleted.
1292 if (fp->uf_flags & FC_DEAD)
1293 return FALSE;
1294
1295 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1296 if (!HASHITEM_EMPTY(hi))
1297 {
1298 // When there is a def-function index do not actually remove the
1299 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001300 // Do remove it when it's a copy.
1301 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001302 fp->uf_flags |= FC_DEAD;
1303 else
1304 hash_remove(&func_hashtab, hi);
1305 return TRUE;
1306 }
1307 return FALSE;
1308}
1309
1310 static void
1311func_clear_items(ufunc_T *fp)
1312{
1313 ga_clear_strings(&(fp->uf_args));
1314 ga_clear_strings(&(fp->uf_def_args));
1315 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001317 VIM_CLEAR(fp->uf_def_arg_idx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001318 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001319 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02001320 clear_type_list(&fp->uf_type_list);
Bram Moolenaarf112f302020-12-20 17:47:52 +01001321 partial_unref(fp->uf_partial);
1322 fp->uf_partial = NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +02001323
1324#ifdef FEAT_LUA
1325 if (fp->uf_cb_free != NULL)
1326 {
1327 fp->uf_cb_free(fp->uf_cb_state);
1328 fp->uf_cb_free = NULL;
1329 }
1330
1331 fp->uf_cb_state = NULL;
1332 fp->uf_cb = NULL;
1333#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334#ifdef FEAT_PROFILE
1335 VIM_CLEAR(fp->uf_tml_count);
1336 VIM_CLEAR(fp->uf_tml_total);
1337 VIM_CLEAR(fp->uf_tml_self);
1338#endif
1339}
1340
1341/*
1342 * Free all things that a function contains. Does not free the function
1343 * itself, use func_free() for that.
1344 * When "force" is TRUE we are exiting.
1345 */
1346 static void
1347func_clear(ufunc_T *fp, int force)
1348{
1349 if (fp->uf_cleared)
1350 return;
1351 fp->uf_cleared = TRUE;
1352
1353 // clear this function
1354 func_clear_items(fp);
1355 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001356 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357}
1358
1359/*
1360 * Free a function and remove it from the list of functions. Does not free
1361 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001362 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02001363 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001365 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001366func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367{
1368 // Only remove it when not done already, otherwise we would remove a newer
1369 // version of the function with the same name.
1370 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1371 func_remove(fp);
1372
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001373 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001374 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001375 if (fp->uf_dfunc_idx > 0)
1376 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001377 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02001379 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001380 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02001381 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382}
1383
1384/*
1385 * Free all things that a function contains and free the function itself.
1386 * When "force" is TRUE we are exiting.
1387 */
1388 static void
1389func_clear_free(ufunc_T *fp, int force)
1390{
1391 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001392 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
1393 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001394 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02001395 else
1396 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397}
1398
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001399/*
1400 * Copy already defined function "lambda" to a new function with name "global".
1401 * This is for when a compiled function defines a global function.
1402 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001403 int
1404copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001405{
1406 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01001407 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001408
1409 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001410 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001411 semsg(_(e_lambda_function_not_found_str), lambda);
1412 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001413 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001414
1415 // TODO: handle ! to overwrite
1416 fp = find_func(global, TRUE, NULL);
1417 if (fp != NULL)
1418 {
1419 semsg(_(e_funcexts), global);
1420 return FAIL;
1421 }
1422
1423 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
1424 if (fp == NULL)
1425 return FAIL;
1426
1427 fp->uf_varargs = ufunc->uf_varargs;
1428 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
1429 fp->uf_def_status = ufunc->uf_def_status;
1430 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
1431 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
1432 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
1433 == FAIL
1434 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
1435 goto failed;
1436
1437 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
1438 : vim_strsave(ufunc->uf_name_exp);
1439 if (ufunc->uf_arg_types != NULL)
1440 {
1441 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
1442 if (fp->uf_arg_types == NULL)
1443 goto failed;
1444 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
1445 sizeof(type_T *) * fp->uf_args.ga_len);
1446 }
1447 if (ufunc->uf_def_arg_idx != NULL)
1448 {
1449 fp->uf_def_arg_idx = ALLOC_MULT(int, fp->uf_def_args.ga_len + 1);
1450 if (fp->uf_def_arg_idx == NULL)
1451 goto failed;
1452 mch_memmove(fp->uf_def_arg_idx, ufunc->uf_def_arg_idx,
1453 sizeof(int) * fp->uf_def_args.ga_len + 1);
1454 }
1455 if (ufunc->uf_va_name != NULL)
1456 {
1457 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
1458 if (fp->uf_va_name == NULL)
1459 goto failed;
1460 }
1461 fp->uf_ret_type = ufunc->uf_ret_type;
1462
1463 fp->uf_refcount = 1;
1464 STRCPY(fp->uf_name, global);
1465 hash_add(&func_hashtab, UF2HIKEY(fp));
1466
1467 // the referenced dfunc_T is now used one more time
1468 link_def_function(fp);
1469
1470 // Create a partial to store the context of the function, if not done
1471 // already.
1472 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
1473 {
1474 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
1475
1476 if (pt == NULL)
1477 goto failed;
1478 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
1479 goto failed;
1480 ufunc->uf_partial = pt;
1481 --pt->pt_refcount; // not referenced here yet
1482 }
1483 if (ufunc->uf_partial != NULL)
1484 {
1485 fp->uf_partial = ufunc->uf_partial;
1486 ++fp->uf_partial->pt_refcount;
1487 }
1488
1489 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001490
1491failed:
1492 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001493 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001494}
1495
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001496static int funcdepth = 0;
1497
1498/*
1499 * Increment the function call depth count.
1500 * Return FAIL when going over 'maxfuncdepth'.
1501 * Otherwise return OK, must call funcdepth_decrement() later!
1502 */
1503 int
1504funcdepth_increment(void)
1505{
1506 if (funcdepth >= p_mfd)
1507 {
1508 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
1509 return FAIL;
1510 }
1511 ++funcdepth;
1512 return OK;
1513}
1514
1515 void
1516funcdepth_decrement(void)
1517{
1518 --funcdepth;
1519}
1520
1521/*
1522 * Get the current function call depth.
1523 */
1524 int
1525funcdepth_get(void)
1526{
1527 return funcdepth;
1528}
1529
1530/*
1531 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001532 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001533 * times as funcdepth_increment().
1534 */
1535 void
1536funcdepth_restore(int depth)
1537{
1538 funcdepth = depth;
1539}
Bram Moolenaar6914c642017-04-01 21:21:30 +02001540
1541/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001542 * Call a user function.
1543 */
1544 static void
1545call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001546 ufunc_T *fp, // pointer to function
1547 int argcount, // nr of args
1548 typval_T *argvars, // arguments
1549 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001550 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01001551 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001552{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001553 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001554 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001555 funccall_T *fc;
1556 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001557 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001558 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001559 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001560 int i;
1561 int ai;
1562 int islambda = FALSE;
1563 char_u numbuf[NUMBUFLEN];
1564 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001565#ifdef FEAT_PROFILE
1566 proftime_T wait_start;
1567 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001568 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001569#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001570 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001571
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001572 // If depth of calling is getting too high, don't execute the function.
1573 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001574 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001575 rettv->v_type = VAR_NUMBER;
1576 rettv->vval.v_number = -1;
1577 return;
1578 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001579
Bram Moolenaare38eab22019-12-05 21:50:01 +01001580 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001581
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001582 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001583 if (fc == NULL)
1584 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001585 fc->caller = current_funccal;
1586 current_funccal = fc;
1587 fc->func = fp;
1588 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001589 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001590 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001591 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1592 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001593 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001594 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001595 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001596
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001597 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 {
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001599 // Execute the function, possibly compiling it first.
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001600 call_def_function(fp, argcount, argvars, funcexe->partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001601 funcdepth_decrement();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 free_funccal(fc);
1604 return;
1605 }
1606
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001607 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1608 islambda = TRUE;
1609
1610 /*
1611 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1612 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1613 * each argument variable and saves a lot of time.
1614 */
1615 /*
1616 * Init l: variables.
1617 */
1618 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1619 if (selfdict != NULL)
1620 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001621 // Set l:self to "selfdict". Use "name" to avoid a warning from
1622 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001623 v = &fc->fixvar[fixvar_idx++].var;
1624 name = v->di_key;
1625 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001626 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001627 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1628 v->di_tv.v_type = VAR_DICT;
1629 v->di_tv.v_lock = 0;
1630 v->di_tv.vval.v_dict = selfdict;
1631 ++selfdict->dv_refcount;
1632 }
1633
1634 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001635 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001636 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001637 * Set a:000 to a list with room for the "..." arguments.
1638 */
1639 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001640 if ((fp->uf_flags & FC_NOARGS) == 0)
1641 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001642 (varnumber_T)(argcount >= fp->uf_args.ga_len
1643 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001644 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001645 if ((fp->uf_flags & FC_NOARGS) == 0)
1646 {
1647 // Use "name" to avoid a warning from some compiler that checks the
1648 // destination size.
1649 v = &fc->fixvar[fixvar_idx++].var;
1650 name = v->di_key;
1651 STRCPY(name, "000");
1652 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1653 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1654 v->di_tv.v_type = VAR_LIST;
1655 v->di_tv.v_lock = VAR_FIXED;
1656 v->di_tv.vval.v_list = &fc->l_varlist;
1657 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001658 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001659 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1660 fc->l_varlist.lv_lock = VAR_FIXED;
1661
1662 /*
1663 * Set a:firstline to "firstline" and a:lastline to "lastline".
1664 * Set a:name to named arguments.
1665 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001666 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001667 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001668 if ((fp->uf_flags & FC_NOARGS) == 0)
1669 {
1670 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001671 (varnumber_T)funcexe->firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001672 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001673 (varnumber_T)funcexe->lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001674 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001675 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001676 {
1677 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001678 typval_T def_rettv;
1679 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001680
1681 ai = i - fp->uf_args.ga_len;
1682 if (ai < 0)
1683 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001684 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001685 name = FUNCARG(fp, i);
1686 if (islambda)
1687 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001688
1689 // evaluate named argument default expression
1690 isdefault = ai + fp->uf_def_args.ga_len >= 0
1691 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1692 && argvars[i].vval.v_number == VVAL_NONE));
1693 if (isdefault)
1694 {
1695 char_u *default_expr = NULL;
1696 def_rettv.v_type = VAR_NUMBER;
1697 def_rettv.vval.v_number = -1;
1698
1699 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1700 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001701 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001702 {
1703 default_arg_err = 1;
1704 break;
1705 }
1706 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001707 }
1708 else
1709 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001710 if ((fp->uf_flags & FC_NOARGS) != 0)
1711 // Bail out if no a: arguments used (in lambda).
1712 break;
1713
Bram Moolenaare38eab22019-12-05 21:50:01 +01001714 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001715 sprintf((char *)numbuf, "%d", ai + 1);
1716 name = numbuf;
1717 }
1718 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1719 {
1720 v = &fc->fixvar[fixvar_idx++].var;
1721 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001722 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001723 }
1724 else
1725 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001726 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001727 if (v == NULL)
1728 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001729 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001730 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001731
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001732 // Note: the values are copied directly to avoid alloc/free.
1733 // "argvars" must have VAR_FIXED for v_lock.
1734 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001735 v->di_tv.v_lock = VAR_FIXED;
1736
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001737 if (addlocal)
1738 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001739 // Named arguments should be accessed without the "a:" prefix in
1740 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001741 copy_tv(&v->di_tv, &v->di_tv);
1742 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001743 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001744 else
1745 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001746
1747 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1748 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001749 listitem_T *li = &fc->l_listitems[ai];
1750
1751 li->li_tv = argvars[i];
1752 li->li_tv.v_lock = VAR_FIXED;
1753 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001754 }
1755 }
1756
Bram Moolenaare38eab22019-12-05 21:50:01 +01001757 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001758 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001759
1760 if (fp->uf_flags & FC_SANDBOX)
1761 {
1762 using_sandbox = TRUE;
1763 ++sandbox;
1764 }
1765
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001766 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001767 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001768 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001769 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001770 ++no_wait_return;
1771 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001772
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001773 smsg(_("calling %s"), SOURCING_NAME);
1774 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001775 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001776 char_u buf[MSG_BUF_LEN];
1777 char_u numbuf2[NUMBUFLEN];
1778 char_u *tofree;
1779 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001780
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001781 msg_puts("(");
1782 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001783 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001784 if (i > 0)
1785 msg_puts(", ");
1786 if (argvars[i].v_type == VAR_NUMBER)
1787 msg_outnum((long)argvars[i].vval.v_number);
1788 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001789 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001790 // Do not want errors such as E724 here.
1791 ++emsg_off;
1792 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1793 --emsg_off;
1794 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001795 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001796 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001797 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001798 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1799 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001800 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001801 msg_puts((char *)s);
1802 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001803 }
1804 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001806 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001807 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001808 msg_puts("\n"); // don't overwrite this either
1809
1810 verbose_leave_scroll();
1811 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001812 }
1813#ifdef FEAT_PROFILE
1814 if (do_profiling == PROF_YES)
1815 {
1816 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001817 {
1818 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001819 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001820 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001821 if (fp->uf_profiling
1822 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1823 {
1824 ++fp->uf_tm_count;
1825 profile_start(&call_start);
1826 profile_zero(&fp->uf_tm_children);
1827 }
1828 script_prof_save(&wait_start);
1829 }
1830#endif
1831
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001832 save_current_sctx = current_sctx;
1833 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001834 save_did_emsg = did_emsg;
1835 did_emsg = FALSE;
1836
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001837 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1838 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001839 else if (islambda)
1840 {
1841 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1842
1843 // A Lambda always has the command "return {expr}". It is much faster
1844 // to evaluate {expr} directly.
1845 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001846 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001847 --ex_nesting_level;
1848 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001849 else
1850 // call do_cmdline() to execute the lines
1851 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001852 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1853
1854 --RedrawingDisabled;
1855
Bram Moolenaare38eab22019-12-05 21:50:01 +01001856 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001857 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1858 {
1859 clear_tv(rettv);
1860 rettv->v_type = VAR_NUMBER;
1861 rettv->vval.v_number = -1;
1862 }
1863
1864#ifdef FEAT_PROFILE
1865 if (do_profiling == PROF_YES && (fp->uf_profiling
1866 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1867 {
1868 profile_end(&call_start);
1869 profile_sub_wait(&wait_start, &call_start);
1870 profile_add(&fp->uf_tm_total, &call_start);
1871 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1872 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1873 {
1874 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1875 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1876 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001877 if (started_profiling)
1878 // make a ":profdel func" stop profiling the function
1879 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001880 }
1881#endif
1882
Bram Moolenaare38eab22019-12-05 21:50:01 +01001883 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001884 if (p_verbose >= 12)
1885 {
1886 ++no_wait_return;
1887 verbose_enter_scroll();
1888
1889 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001890 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001891 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001892 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001893 (long)fc->rettv->vval.v_number);
1894 else
1895 {
1896 char_u buf[MSG_BUF_LEN];
1897 char_u numbuf2[NUMBUFLEN];
1898 char_u *tofree;
1899 char_u *s;
1900
Bram Moolenaare38eab22019-12-05 21:50:01 +01001901 // The value may be very long. Skip the middle part, so that we
1902 // have some idea how it starts and ends. smsg() would always
1903 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001904 ++emsg_off;
1905 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1906 --emsg_off;
1907 if (s != NULL)
1908 {
1909 if (vim_strsize(s) > MSG_BUF_CLEN)
1910 {
1911 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1912 s = buf;
1913 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001914 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001915 vim_free(tofree);
1916 }
1917 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001918 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001919
1920 verbose_leave_scroll();
1921 --no_wait_return;
1922 }
1923
Bram Moolenaare31ee862020-01-07 20:59:34 +01001924 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001925 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001926 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001927#ifdef FEAT_PROFILE
1928 if (do_profiling == PROF_YES)
1929 script_prof_restore(&wait_start);
1930#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001931 if (using_sandbox)
1932 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001933
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001934 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001935 {
1936 ++no_wait_return;
1937 verbose_enter_scroll();
1938
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001939 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001940 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001941
1942 verbose_leave_scroll();
1943 --no_wait_return;
1944 }
1945
1946 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001947 funcdepth_decrement();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001948 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001949}
1950
1951/*
Bram Moolenaar50824712020-12-20 21:10:17 +01001952 * Check the argument count for user function "fp".
1953 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
1954 */
1955 int
1956check_user_func_argcount(ufunc_T *fp, int argcount)
1957{
1958 int regular_args = fp->uf_args.ga_len;
1959
1960 if (argcount < regular_args - fp->uf_def_args.ga_len)
1961 return FCERR_TOOFEW;
1962 else if (!has_varargs(fp) && argcount > regular_args)
1963 return FCERR_TOOMANY;
1964 return FCERR_UNKNOWN;
1965}
1966
1967/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001969 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 int
1971call_user_func_check(
1972 ufunc_T *fp,
1973 int argcount,
1974 typval_T *argvars,
1975 typval_T *rettv,
1976 funcexe_T *funcexe,
1977 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001978{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1982 *funcexe->doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01001983 error = check_user_func_argcount(fp, argcount);
1984 if (error != FCERR_UNKNOWN)
1985 return error;
1986 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 error = FCERR_DICT;
1988 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001989 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 int did_save_redo = FALSE;
1991 save_redo_T save_redo;
1992
1993 /*
1994 * Call the user function.
1995 * Save and restore search patterns, script variables and
1996 * redo buffer.
1997 */
1998 save_search_patterns();
1999 if (!ins_compl_active())
2000 {
2001 saveRedobuff(&save_redo);
2002 did_save_redo = TRUE;
2003 }
2004 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002005 call_user_func(fp, argcount, argvars, rettv, funcexe,
2006 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
2008 // Function was unreferenced while being used, free it now.
2009 func_clear_free(fp, FALSE);
2010 if (did_save_redo)
2011 restoreRedobuff(&save_redo);
2012 restore_search_patterns();
2013 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002014 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002016}
2017
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002018static funccal_entry_T *funccal_stack = NULL;
2019
2020/*
2021 * Save the current function call pointer, and set it to NULL.
2022 * Used when executing autocommands and for ":source".
2023 */
2024 void
2025save_funccal(funccal_entry_T *entry)
2026{
2027 entry->top_funccal = current_funccal;
2028 entry->next = funccal_stack;
2029 funccal_stack = entry;
2030 current_funccal = NULL;
2031}
2032
2033 void
2034restore_funccal(void)
2035{
2036 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002037 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002038 else
2039 {
2040 current_funccal = funccal_stack->top_funccal;
2041 funccal_stack = funccal_stack->next;
2042 }
2043}
2044
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02002045 funccall_T *
2046get_current_funccal(void)
2047{
2048 return current_funccal;
2049}
2050
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002051/*
2052 * Mark all functions of script "sid" as deleted.
2053 */
2054 void
2055delete_script_functions(int sid)
2056{
2057 hashitem_T *hi;
2058 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02002059 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002060 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002061 size_t len;
2062
2063 buf[0] = K_SPECIAL;
2064 buf[1] = KS_EXTRA;
2065 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002066 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002067 len = STRLEN(buf);
2068
Bram Moolenaarce658352020-07-31 23:47:12 +02002069 while (todo > 0)
2070 {
2071 todo = func_hashtab.ht_used;
2072 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2073 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002074 {
Bram Moolenaarce658352020-07-31 23:47:12 +02002075 fp = HI2UF(hi);
2076 if (STRNCMP(fp->uf_name, buf, len) == 0)
2077 {
2078 int changed = func_hashtab.ht_changed;
2079
2080 fp->uf_flags |= FC_DEAD;
2081 func_clear(fp, TRUE);
2082 // When clearing a function another function can be cleared
2083 // as a side effect. When that happens start over.
2084 if (changed != func_hashtab.ht_changed)
2085 break;
2086 }
2087 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002088 }
Bram Moolenaarce658352020-07-31 23:47:12 +02002089 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002090}
2091
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002092#if defined(EXITFREE) || defined(PROTO)
2093 void
2094free_all_functions(void)
2095{
2096 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002097 ufunc_T *fp;
2098 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002099 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002100 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002101
Bram Moolenaare38eab22019-12-05 21:50:01 +01002102 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002103 while (current_funccal != NULL)
2104 {
2105 clear_tv(current_funccal->rettv);
2106 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002107 if (current_funccal == NULL && funccal_stack != NULL)
2108 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02002109 }
2110
Bram Moolenaare38eab22019-12-05 21:50:01 +01002111 // First clear what the functions contain. Since this may lower the
2112 // reference count of a function, it may also free a function and change
2113 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002114 while (todo > 0)
2115 {
2116 todo = func_hashtab.ht_used;
2117 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2118 if (!HASHITEM_EMPTY(hi))
2119 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120 // clear the def function index now
2121 fp = HI2UF(hi);
2122 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002123 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124
Bram Moolenaare38eab22019-12-05 21:50:01 +01002125 // Only free functions that are not refcounted, those are
2126 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002127 if (func_name_refcount(fp->uf_name))
2128 ++skipped;
2129 else
2130 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002131 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002132 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002133 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002134 {
2135 skipped = 0;
2136 break;
2137 }
2138 }
2139 --todo;
2140 }
2141 }
2142
Bram Moolenaare38eab22019-12-05 21:50:01 +01002143 // Now actually free the functions. Need to start all over every time,
2144 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002145 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002146 while (func_hashtab.ht_used > skipped)
2147 {
2148 todo = func_hashtab.ht_used;
2149 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002150 if (!HASHITEM_EMPTY(hi))
2151 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02002152 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002153 // Only free functions that are not refcounted, those are
2154 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02002155 fp = HI2UF(hi);
2156 if (func_name_refcount(fp->uf_name))
2157 ++skipped;
2158 else
2159 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02002160 if (func_free(fp, FALSE) == OK)
2161 {
2162 skipped = 0;
2163 break;
2164 }
2165 // did not actually free it
2166 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002167 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002168 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02002169 }
2170 if (skipped == 0)
2171 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172
2173 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002174}
2175#endif
2176
2177/*
2178 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02002179 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002180 * "len" is the length of "name", or -1 for NUL terminated.
2181 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002183builtin_function(char_u *name, int len)
2184{
2185 char_u *p;
2186
Bram Moolenaara26b9702020-04-18 19:53:28 +02002187 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002188 return FALSE;
2189 p = vim_strchr(name, AUTOLOAD_CHAR);
2190 return p == NULL || (len > 0 && p > name + len);
2191}
2192
2193 int
2194func_call(
2195 char_u *name,
2196 typval_T *args,
2197 partial_T *partial,
2198 dict_T *selfdict,
2199 typval_T *rettv)
2200{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002201 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002202 listitem_T *item;
2203 typval_T argv[MAX_FUNC_ARGS + 1];
2204 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002205 int r = 0;
2206
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002207 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002208 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002209 {
2210 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
2211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002212 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002213 break;
2214 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002215 // Make a copy of each argument. This is needed to be able to set
2216 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002217 copy_tv(&item->li_tv, &argv[argc++]);
2218 }
2219
2220 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002221 {
2222 funcexe_T funcexe;
2223
Bram Moolenaara80faa82020-04-12 19:37:17 +02002224 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002225 funcexe.firstline = curwin->w_cursor.lnum;
2226 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002227 funcexe.evaluate = TRUE;
2228 funcexe.partial = partial;
2229 funcexe.selfdict = selfdict;
2230 r = call_func(name, -1, rettv, argc, argv, &funcexe);
2231 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002232
Bram Moolenaare38eab22019-12-05 21:50:01 +01002233 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002234 while (argc > 0)
2235 clear_tv(&argv[--argc]);
2236
2237 return r;
2238}
2239
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002240static int callback_depth = 0;
2241
2242 int
2243get_callback_depth(void)
2244{
2245 return callback_depth;
2246}
2247
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002248/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002249 * Invoke call_func() with a callback.
2250 */
2251 int
2252call_callback(
2253 callback_T *callback,
2254 int len, // length of "name" or -1 to use strlen()
2255 typval_T *rettv, // return value goes here
2256 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002257 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002258 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002259{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002260 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002261 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002262
Bram Moolenaara80faa82020-04-12 19:37:17 +02002263 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002264 funcexe.evaluate = TRUE;
2265 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002266 ++callback_depth;
2267 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
2268 --callback_depth;
2269 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002270}
2271
2272/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 * Give an error message for the result of a function.
2274 * Nothing if "error" is FCERR_NONE.
2275 */
2276 void
2277user_func_error(int error, char_u *name)
2278{
2279 switch (error)
2280 {
2281 case FCERR_UNKNOWN:
2282 emsg_funcname(e_unknownfunc, name);
2283 break;
2284 case FCERR_NOTMETHOD:
2285 emsg_funcname(
2286 N_("E276: Cannot use function as a method: %s"), name);
2287 break;
2288 case FCERR_DELETED:
2289 emsg_funcname(N_(e_func_deleted), name);
2290 break;
2291 case FCERR_TOOMANY:
2292 emsg_funcname((char *)e_toomanyarg, name);
2293 break;
2294 case FCERR_TOOFEW:
2295 emsg_funcname((char *)e_toofewarg, name);
2296 break;
2297 case FCERR_SCRIPT:
2298 emsg_funcname(
2299 N_("E120: Using <SID> not in a script context: %s"), name);
2300 break;
2301 case FCERR_DICT:
2302 emsg_funcname(
2303 N_("E725: Calling dict function without Dictionary: %s"),
2304 name);
2305 break;
2306 }
2307}
2308
2309/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002310 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002311 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002312 * Return FAIL when the function can't be called, OK otherwise.
2313 * Also returns OK when an error was encountered while executing the function.
2314 */
2315 int
2316call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02002317 char_u *funcname, // name of the function
2318 int len, // length of "name" or -1 to use strlen()
2319 typval_T *rettv, // return value goes here
2320 int argcount_in, // number of "argvars"
2321 typval_T *argvars_in, // vars for arguments, must have "argcount"
2322 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002323 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002324{
2325 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01002326 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002327 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002328 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002329 char_u fname_buf[FLEN_FIXED + 1];
2330 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002331 char_u *fname = NULL;
2332 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002333 int argcount = argcount_in;
2334 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002335 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002336 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
2337 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002338 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002339 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002340 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002341
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02002342 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
2343 // even when call_func() returns FAIL.
2344 rettv->v_type = VAR_UNKNOWN;
2345
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002346 if (partial != NULL)
2347 fp = partial->pt_func;
2348 if (fp == NULL)
2349 {
2350 // Make a copy of the name, if it comes from a funcref variable it
2351 // could be changed or deleted in the called function.
2352 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
2353 if (name == NULL)
2354 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002355
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002356 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
2357 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002358
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002359 if (funcexe->doesrange != NULL)
2360 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002361
2362 if (partial != NULL)
2363 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002364 // When the function has a partial with a dict and there is a dict
2365 // argument, use the dict argument. That is backwards compatible.
2366 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002367 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002368 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01002369 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002370 {
2371 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002372 {
2373 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
2374 {
Bram Moolenaaref140542019-12-31 21:27:13 +01002375 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002376 goto theend;
2377 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002378 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002379 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002380 for (i = 0; i < argcount_in; ++i)
2381 argv[i + argv_clear] = argvars_in[i];
2382 argvars = argv;
2383 argcount = partial->pt_argc + argcount_in;
2384 }
2385 }
2386
Bram Moolenaaref140542019-12-31 21:27:13 +01002387 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002388 {
2389 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002390 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002391
Bram Moolenaar333894b2020-08-01 18:53:07 +02002392 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002393 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02002394 {
2395 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002396 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002397 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002398
Bram Moolenaare38eab22019-12-05 21:50:01 +01002399 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002400 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01002401 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002402
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002403 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002404 {
2405 /*
2406 * User defined function.
2407 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002408 if (fp == NULL)
Bram Moolenaar333894b2020-08-01 18:53:07 +02002409 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002410
Bram Moolenaare38eab22019-12-05 21:50:01 +01002411 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002412 if (fp == NULL
2413 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002414 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002415 && !aborting())
2416 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002417 // executed an autocommand, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002418 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002419 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002420 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002421 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
2422 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002423 // loaded a package, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002424 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002425 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02002426 if (fp == NULL)
2427 {
2428 char_u *p = untrans_function_name(rfname);
2429
2430 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02002431 // Don't do this if the name starts with "s:".
2432 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaar333894b2020-08-01 18:53:07 +02002433 fp = find_func(p, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002434 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002435
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002436 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01002437 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002438#ifdef FEAT_LUA
2439 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
2440 {
2441 cfunc_T cb = fp->uf_cb;
2442
2443 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
2444 }
2445#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002446 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002447 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002448 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01002449 // postponed filling in the arguments, do it now
2450 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002451 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002452
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002453 if (funcexe->basetv != NULL)
2454 {
2455 // Method call: base->Method()
2456 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
2457 argv[0] = *funcexe->basetv;
2458 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002459 argvars = argv;
2460 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002461 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002462
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 error = call_user_func_check(fp, argcount, argvars, rettv,
2464 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002465 }
2466 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002467 else if (funcexe->basetv != NULL)
2468 {
2469 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002470 * expr->method(): Find the method name in the table, call its
2471 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02002472 */
2473 error = call_internal_method(fname, argcount, argvars, rettv,
2474 funcexe->basetv);
2475 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002476 else
2477 {
2478 /*
2479 * Find the function name in the table, call its implementation.
2480 */
2481 error = call_internal_func(fname, argcount, argvars, rettv);
2482 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02002483
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002484 /*
2485 * The function call (or "FuncUndefined" autocommand sequence) might
2486 * have been aborted by an error, an interrupt, or an explicitly thrown
2487 * exception that has not been caught so far. This situation can be
2488 * tested for by calling aborting(). For an error in an internal
2489 * function or for the "E132" error in call_user_func(), however, the
2490 * throw point at which the "force_abort" flag (temporarily reset by
2491 * emsg()) is normally updated has not been reached yet. We need to
2492 * update that flag first to make aborting() reliable.
2493 */
2494 update_force_abort();
2495 }
Bram Moolenaaref140542019-12-31 21:27:13 +01002496 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002497 ret = OK;
2498
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002499theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002500 /*
2501 * Report an error unless the argument evaluation or function call has been
2502 * cancelled due to an aborting error, an interrupt, or an exception.
2503 */
2504 if (!aborting())
2505 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002506 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002507 }
2508
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002509 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002510 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002511 clear_tv(&argv[--argv_clear + argv_base]);
2512
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002513 vim_free(tofree);
2514 vim_free(name);
2515
2516 return ret;
2517}
2518
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002519 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520printable_func_name(ufunc_T *fp)
2521{
2522 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
2523}
2524
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002525/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002526 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002527 */
2528 static void
2529list_func_head(ufunc_T *fp, int indent)
2530{
2531 int j;
2532
2533 msg_start();
2534 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002535 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002536 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002537 msg_puts("def ");
2538 else
2539 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002541 msg_putchar('(');
2542 for (j = 0; j < fp->uf_args.ga_len; ++j)
2543 {
2544 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002545 msg_puts(", ");
2546 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 if (fp->uf_arg_types != NULL)
2548 {
2549 char *tofree;
2550
2551 msg_puts(": ");
2552 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2553 vim_free(tofree);
2554 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002555 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2556 {
2557 msg_puts(" = ");
2558 msg_puts(((char **)(fp->uf_def_args.ga_data))
2559 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2560 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002561 }
2562 if (fp->uf_varargs)
2563 {
2564 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002565 msg_puts(", ");
2566 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002567 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 if (fp->uf_va_name != NULL)
2569 {
2570 if (j)
2571 msg_puts(", ");
2572 msg_puts("...");
2573 msg_puts((char *)fp->uf_va_name);
2574 if (fp->uf_va_type)
2575 {
2576 char *tofree;
2577
2578 msg_puts(": ");
2579 msg_puts(type_name(fp->uf_va_type, &tofree));
2580 vim_free(tofree);
2581 }
2582 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002583 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002584
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002585 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002586 {
2587 if (fp->uf_ret_type != &t_void)
2588 {
2589 char *tofree;
2590
2591 msg_puts(": ");
2592 msg_puts(type_name(fp->uf_ret_type, &tofree));
2593 vim_free(tofree);
2594 }
2595 }
2596 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002597 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002598 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002599 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002600 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002601 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002602 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002603 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002604 msg_clr_eos();
2605 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002606 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002607}
2608
2609/*
2610 * Get a function name, translating "<SID>" and "<SNR>".
2611 * Also handles a Funcref in a List or Dictionary.
2612 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002613 * Set "*is_global" to TRUE when the function must be global, unless
2614 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002615 * flags:
2616 * TFN_INT: internal function name OK
2617 * TFN_QUIET: be quiet
2618 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002619 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002620 * Advances "pp" to just after the function name (if no error).
2621 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002622 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002623trans_function_name(
2624 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002625 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002626 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002627 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002628 funcdict_T *fdp, // return: info about dictionary used
2629 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002630{
2631 char_u *name = NULL;
2632 char_u *start;
2633 char_u *end;
2634 int lead;
2635 char_u sid_buf[20];
2636 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002638 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 int vim9script;
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002640 static char *e_function_name = N_("E129: Function name required");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002641
2642 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002643 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002644 start = *pp;
2645
Bram Moolenaare38eab22019-12-05 21:50:01 +01002646 // Check for hard coded <SNR>: already translated function ID (from a user
2647 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002648 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2649 && (*pp)[2] == (int)KE_SNR)
2650 {
2651 *pp += 3;
2652 len = get_id_len(pp) + 3;
2653 return vim_strnsave(start, len);
2654 }
2655
Bram Moolenaare38eab22019-12-05 21:50:01 +01002656 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2657 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002658 lead = eval_fname_script(start);
2659 if (lead > 2)
2660 start += lead;
2661
Bram Moolenaare38eab22019-12-05 21:50:01 +01002662 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002663 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002664 lead > 2 ? 0 : FNE_CHECK_START);
2665 if (end == start)
2666 {
2667 if (!skip)
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002668 emsg(_(e_function_name));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002669 goto theend;
2670 }
2671 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2672 {
2673 /*
2674 * Report an invalid expression in braces, unless the expression
2675 * evaluation has been cancelled due to an aborting error, an
2676 * interrupt, or an exception.
2677 */
2678 if (!aborting())
2679 {
2680 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002681 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002682 }
2683 else
2684 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2685 goto theend;
2686 }
2687
2688 if (lv.ll_tv != NULL)
2689 {
2690 if (fdp != NULL)
2691 {
2692 fdp->fd_dict = lv.ll_dict;
2693 fdp->fd_newkey = lv.ll_newkey;
2694 lv.ll_newkey = NULL;
2695 fdp->fd_di = lv.ll_di;
2696 }
2697 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2698 {
2699 name = vim_strsave(lv.ll_tv->vval.v_string);
2700 *pp = end;
2701 }
2702 else if (lv.ll_tv->v_type == VAR_PARTIAL
2703 && lv.ll_tv->vval.v_partial != NULL)
2704 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002705 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002706 *pp = end;
2707 if (partial != NULL)
2708 *partial = lv.ll_tv->vval.v_partial;
2709 }
2710 else
2711 {
2712 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2713 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002714 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002715 else
2716 *pp = end;
2717 name = NULL;
2718 }
2719 goto theend;
2720 }
2721
2722 if (lv.ll_name == NULL)
2723 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002724 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002725 *pp = end;
2726 goto theend;
2727 }
2728
Bram Moolenaare38eab22019-12-05 21:50:01 +01002729 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002730 if (lv.ll_exp_name != NULL)
2731 {
2732 len = (int)STRLEN(lv.ll_exp_name);
2733 name = deref_func_name(lv.ll_exp_name, &len, partial,
2734 flags & TFN_NO_AUTOLOAD);
2735 if (name == lv.ll_exp_name)
2736 name = NULL;
2737 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002738 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002739 {
2740 len = (int)(end - *pp);
2741 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2742 if (name == *pp)
2743 name = NULL;
2744 }
2745 if (name != NULL)
2746 {
2747 name = vim_strsave(name);
2748 *pp = end;
2749 if (STRNCMP(name, "<SNR>", 5) == 0)
2750 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002751 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002752 name[0] = K_SPECIAL;
2753 name[1] = KS_EXTRA;
2754 name[2] = (int)KE_SNR;
2755 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2756 }
2757 goto theend;
2758 }
2759
2760 if (lv.ll_exp_name != NULL)
2761 {
2762 len = (int)STRLEN(lv.ll_exp_name);
2763 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2764 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2765 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002766 // When there was "s:" already or the name expanded to get a
2767 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002768 lv.ll_name += 2;
2769 len -= 2;
2770 lead = 2;
2771 }
2772 }
2773 else
2774 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002775 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002776 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002777 {
2778 if (is_global != NULL && lv.ll_name[0] == 'g')
2779 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002780 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002781 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002782 len = (int)(end - lv.ll_name);
2783 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002784 if (len <= 0)
2785 {
2786 if (!skip)
2787 emsg(_(e_function_name));
2788 goto theend;
2789 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002790
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002791 // In Vim9 script a user function is script-local by default, unless it
2792 // starts with a lower case character: dict.func().
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002793 vim9script = ASCII_ISUPPER(*start) && in_vim9script();
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002794 if (vim9script)
2795 {
2796 char_u *p;
2797
2798 // SomeScript#func() is a global function.
2799 for (p = start; *p != NUL && *p != '('; ++p)
2800 if (*p == AUTOLOAD_CHAR)
2801 vim9script = FALSE;
2802 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002804 /*
2805 * Copy the function name to allocated memory.
2806 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2807 * Accept <SNR>123_name() outside a script.
2808 */
2809 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002810 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002812 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 if (!vim9script)
2814 lead = 3;
2815 if (vim9script || (lv.ll_exp_name != NULL
2816 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002817 || eval_fname_sid(*pp))
2818 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002820 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002822 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002823 goto theend;
2824 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002825 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 if (vim9script)
2827 extra = 3 + (int)STRLEN(sid_buf);
2828 else
2829 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002830 }
2831 }
2832 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2833 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002834 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002835 start);
2836 goto theend;
2837 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002838 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002839 {
2840 char_u *cp = vim_strchr(lv.ll_name, ':');
2841
2842 if (cp != NULL && cp < end)
2843 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002844 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002845 goto theend;
2846 }
2847 }
2848
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002850 if (name != NULL)
2851 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002852 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002853 {
2854 name[0] = K_SPECIAL;
2855 name[1] = KS_EXTRA;
2856 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002858 STRCPY(name + 3, sid_buf);
2859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2861 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002862 }
2863 *pp = end;
2864
2865theend:
2866 clear_lval(&lv);
2867 return name;
2868}
2869
2870/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002871 * Assuming "name" is the result of trans_function_name() and it was prefixed
2872 * to use the script-local name, return the unmodified name (points into
2873 * "name"). Otherwise return NULL.
2874 * This can be used to first search for a script-local function and fall back
2875 * to the global function if not found.
2876 */
2877 char_u *
2878untrans_function_name(char_u *name)
2879{
2880 char_u *p;
2881
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002882 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02002883 {
2884 p = vim_strchr(name, '_');
2885 if (p != NULL)
2886 return p + 1;
2887 }
2888 return NULL;
2889}
2890
2891/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002892 * List functions. When "regmatch" is NULL all of then.
2893 * Otherwise functions matching "regmatch".
2894 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002895 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002896list_functions(regmatch_T *regmatch)
2897{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002898 int changed = func_hashtab.ht_changed;
2899 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002900 hashitem_T *hi;
2901
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002902 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002903 {
2904 if (!HASHITEM_EMPTY(hi))
2905 {
2906 ufunc_T *fp = HI2UF(hi);
2907
2908 --todo;
2909 if ((fp->uf_flags & FC_DEAD) == 0
2910 && (regmatch == NULL
2911 ? !message_filtered(fp->uf_name)
2912 && !func_name_refcount(fp->uf_name)
2913 : !isdigit(*fp->uf_name)
2914 && vim_regexec(regmatch, fp->uf_name, 0)))
2915 {
2916 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002917 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002918 {
2919 emsg(_("E454: function list was modified"));
2920 return;
2921 }
2922 }
2923 }
2924 }
2925}
2926
2927/*
Bram Moolenaaradc8e442020-12-31 18:28:18 +01002928 * Check if "*cmd" points to a function command and if so advance "*cmd" and
2929 * return TRUE.
2930 * Otherwise return FALSE;
2931 * Do not consider "function(" to be a command.
2932 */
2933 static int
2934is_function_cmd(char_u **cmd)
2935{
2936 char_u *p = *cmd;
2937
2938 if (checkforcmd(&p, "function", 2))
2939 {
2940 if (*p == '(')
2941 return FALSE;
2942 *cmd = p;
2943 return TRUE;
2944 }
2945 return FALSE;
2946}
2947
2948/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02002949 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002950 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
2951 * the function name.
Bram Moolenaar04b12692020-05-04 23:24:44 +02002952 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002953 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02002954 ufunc_T *
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002955define_function(exarg_T *eap, char_u *name_arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002956{
2957 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002958 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002959 int j;
2960 int c;
2961 int saved_did_emsg;
2962 int saved_wait_return = need_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002963 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002964 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002965 char_u *p;
2966 char_u *arg;
2967 char_u *line_arg = NULL;
2968 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002970 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002971 garray_T newlines;
2972 int varargs = FALSE;
2973 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002975 ufunc_T *fp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002976 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002977 int indent;
2978 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979#define MAX_FUNC_NESTING 50
2980 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002981 dictitem_T *v;
2982 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002983 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002984 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002985 hashitem_T *hi;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002986 getline_opt_T getline_options;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002987 linenr_T sourcing_lnum_off;
2988 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002989 int is_heredoc = FALSE;
2990 char_u *skip_until = NULL;
2991 char_u *heredoc_trimmed = NULL;
Bram Moolenaare7e48382020-07-22 18:17:08 +02002992 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02002993 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994
2995 /*
2996 * ":function" without argument: list functions.
2997 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002998 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002999 {
3000 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003001 list_functions(NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003002 eap->nextcmd = check_nextcmd(eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003003 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003004 }
3005
3006 /*
3007 * ":function /pat": list functions matching pattern.
3008 */
3009 if (*eap->arg == '/')
3010 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003011 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003012 if (!eap->skip)
3013 {
3014 regmatch_T regmatch;
3015
3016 c = *p;
3017 *p = NUL;
3018 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
3019 *p = c;
3020 if (regmatch.regprog != NULL)
3021 {
3022 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003023 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003024 vim_regfree(regmatch.regprog);
3025 }
3026 }
3027 if (*p == '/')
3028 ++p;
3029 eap->nextcmd = check_nextcmd(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003030 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003031 }
3032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 ga_init(&newargs);
3034 ga_init(&argtypes);
3035 ga_init(&default_args);
3036
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003037 /*
3038 * Get the function name. There are these situations:
3039 * func normal function name
3040 * "name" == func, "fudi.fd_dict" == NULL
3041 * dict.func new dictionary entry
3042 * "name" == NULL, "fudi.fd_dict" set,
3043 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
3044 * dict.func existing dict entry with a Funcref
3045 * "name" == func, "fudi.fd_dict" set,
3046 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3047 * dict.func existing dict entry that's not a Funcref
3048 * "name" == NULL, "fudi.fd_dict" set,
3049 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3050 * s:func script-local function name
3051 * g:func global function name, same as "func"
3052 */
3053 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003054 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003055 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003056 // nested function, argument is (args).
3057 paren = TRUE;
3058 CLEAR_FIELD(fudi);
3059 }
3060 else
3061 {
3062 name = trans_function_name(&p, &is_global, eap->skip,
3063 TFN_NO_AUTOLOAD, &fudi, NULL);
3064 paren = (vim_strchr(p, '(') != NULL);
3065 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003066 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003067 /*
3068 * Return on an invalid expression in braces, unless the expression
3069 * evaluation has been cancelled due to an aborting error, an
3070 * interrupt, or an exception.
3071 */
3072 if (!aborting())
3073 {
3074 if (!eap->skip && fudi.fd_newkey != NULL)
3075 semsg(_(e_dictkey), fudi.fd_newkey);
3076 vim_free(fudi.fd_newkey);
3077 return NULL;
3078 }
3079 else
3080 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003081 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003082 }
3083
Bram Moolenaare38eab22019-12-05 21:50:01 +01003084 // An error in a function call during evaluation of an expression in magic
3085 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003086 saved_did_emsg = did_emsg;
3087 did_emsg = FALSE;
3088
3089 /*
3090 * ":function func" with only function name: list function.
3091 */
3092 if (!paren)
3093 {
3094 if (!ends_excmd(*skipwhite(p)))
3095 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003096 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003097 goto ret_free;
3098 }
3099 eap->nextcmd = check_nextcmd(p);
3100 if (eap->nextcmd != NULL)
3101 *p = NUL;
3102 if (!eap->skip && !got_int)
3103 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003104 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003105 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
3106 {
3107 char_u *up = untrans_function_name(name);
3108
3109 // With Vim9 script the name was made script-local, if not
3110 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02003111 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003112 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003113 }
3114
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003115 if (fp != NULL)
3116 {
3117 list_func_head(fp, TRUE);
3118 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
3119 {
3120 if (FUNCLINE(fp, j) == NULL)
3121 continue;
3122 msg_putchar('\n');
3123 msg_outnum((long)(j + 1));
3124 if (j < 9)
3125 msg_putchar(' ');
3126 if (j < 99)
3127 msg_putchar(' ');
3128 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003129 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003130 ui_breakcheck();
3131 }
3132 if (!got_int)
3133 {
3134 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003135 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 msg_puts(" enddef");
3137 else
3138 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003139 }
3140 }
3141 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02003142 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003143 }
3144 goto ret_free;
3145 }
3146
3147 /*
3148 * ":function name(arg1, arg2)" Define function.
3149 */
3150 p = skipwhite(p);
3151 if (*p != '(')
3152 {
3153 if (!eap->skip)
3154 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003155 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156 goto ret_free;
3157 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003158 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003159 if (vim_strchr(p, '(') != NULL)
3160 p = vim_strchr(p, '(');
3161 }
3162 p = skipwhite(p + 1);
3163
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02003164 // In Vim9 script only global functions can be redefined.
3165 if (vim9script && eap->forceit && !is_global)
3166 {
3167 emsg(_(e_nobang));
3168 goto ret_free;
3169 }
3170
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003171 ga_init2(&newlines, (int)sizeof(char_u *), 3);
3172
Bram Moolenaar04b12692020-05-04 23:24:44 +02003173 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003174 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003175 // Check the name of the function. Unless it's a dictionary function
3176 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003177 if (name != NULL)
3178 arg = name;
3179 else
3180 arg = fudi.fd_newkey;
3181 if (arg != NULL && (fudi.fd_di == NULL
3182 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
3183 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
3184 {
3185 if (*arg == K_SPECIAL)
3186 j = 3;
3187 else
3188 j = 0;
3189 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
3190 : eval_isnamec(arg[j])))
3191 ++j;
3192 if (arg[j] != NUL)
3193 emsg_funcname((char *)e_invarg2, arg);
3194 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003195 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003196 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003197 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003198 }
3199
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003200 // This may get more lines and make the pointers into the first line
3201 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003203 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003204 &varargs, &default_args, eap->skip,
3205 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003206 goto errret_2;
3207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003209 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 // find the return type: :def Func(): type
3211 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003212 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02003214 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003216 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003217 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003221 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003222 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02003223 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003224 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003225 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02003226 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 else
3229 // find extra arguments "range", "dict", "abort" and "closure"
3230 for (;;)
3231 {
3232 p = skipwhite(p);
3233 if (STRNCMP(p, "range", 5) == 0)
3234 {
3235 flags |= FC_RANGE;
3236 p += 5;
3237 }
3238 else if (STRNCMP(p, "dict", 4) == 0)
3239 {
3240 flags |= FC_DICT;
3241 p += 4;
3242 }
3243 else if (STRNCMP(p, "abort", 5) == 0)
3244 {
3245 flags |= FC_ABORT;
3246 p += 5;
3247 }
3248 else if (STRNCMP(p, "closure", 7) == 0)
3249 {
3250 flags |= FC_CLOSURE;
3251 p += 7;
3252 if (current_funccal == NULL)
3253 {
3254 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
3255 name == NULL ? (char_u *)"" : name);
3256 goto erret;
3257 }
3258 }
3259 else
3260 break;
3261 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003262
Bram Moolenaare38eab22019-12-05 21:50:01 +01003263 // When there is a line break use what follows for the function body.
3264 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003265 if (*p == '\n')
3266 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003267 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02003268 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
3269 && eap->cmdidx != CMD_def)
Bram Moolenaare7e48382020-07-22 18:17:08 +02003270 && !(*p == '#' && (vim9script || eap->cmdidx == CMD_def))
3271 && !eap->skip
3272 && !did_emsg)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003273 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003274
3275 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
3277 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003278 */
3279 if (KeyTyped)
3280 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003281 // Check if the function already exists, don't let the user type the
3282 // whole function before telling him it doesn't work! For a script we
3283 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003284 if (!eap->skip && !eap->forceit)
3285 {
3286 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003287 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003288 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003289 emsg_funcname(e_funcexts, name);
3290 }
3291
3292 if (!eap->skip && did_emsg)
3293 goto erret;
3294
Bram Moolenaare38eab22019-12-05 21:50:01 +01003295 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003296 cmdline_row = msg_row;
3297 }
3298
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003299 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003300 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003301
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02003302 // Detect having skipped over comment lines to find the return
3303 // type. Add NULL lines to keep the line count correct.
3304 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
3305 if (SOURCING_LNUM < sourcing_lnum_off)
3306 {
3307 sourcing_lnum_off -= SOURCING_LNUM;
3308 if (ga_grow(&newlines, sourcing_lnum_off) == FAIL)
3309 goto erret;
3310 while (sourcing_lnum_off-- > 0)
3311 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3312 }
3313
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003314 indent = 2;
3315 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003317 getline_options = eap->cmdidx == CMD_def
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01003318 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003319 for (;;)
3320 {
3321 if (KeyTyped)
3322 {
3323 msg_scroll = TRUE;
3324 saved_wait_return = FALSE;
3325 }
3326 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003327
3328 if (line_arg != NULL)
3329 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003330 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003331 theline = line_arg;
3332 p = vim_strchr(theline, '\n');
3333 if (p == NULL)
3334 line_arg += STRLEN(line_arg);
3335 else
3336 {
3337 *p = NUL;
3338 line_arg = p + 1;
3339 }
3340 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003341 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02003342 {
3343 vim_free(line_to_free);
3344 if (eap->getline == NULL)
Bram Moolenaar66250c92020-08-20 15:02:42 +02003345 theline = getcmdline(':', 0L, indent, getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003346 else
Bram Moolenaar66250c92020-08-20 15:02:42 +02003347 theline = eap->getline(':', eap->cookie, indent,
3348 getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003349 line_to_free = theline;
3350 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003351 if (KeyTyped)
3352 lines_left = Rows - 1;
3353 if (theline == NULL)
3354 {
Bram Moolenaarb8ba9b92021-01-01 18:54:34 +01003355 // Use the start of the function for the line number.
3356 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003357 if (skip_until != NULL)
3358 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
3359 else if (eap->cmdidx == CMD_def)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003360 emsg(_(e_missing_enddef));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 else
3362 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003363 goto erret;
3364 }
3365
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003366 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003367 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003368 if (SOURCING_LNUM < sourcing_lnum_off)
3369 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003370 else
3371 sourcing_lnum_off = 0;
3372
3373 if (skip_until != NULL)
3374 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003376 // * ":append" and "."
3377 // * ":python <<EOF" and "EOF"
3378 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
3379 if (heredoc_trimmed == NULL
3380 || (is_heredoc && skipwhite(theline) == theline)
3381 || STRNCMP(theline, heredoc_trimmed,
3382 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003383 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003384 if (heredoc_trimmed == NULL)
3385 p = theline;
3386 else if (is_heredoc)
3387 p = skipwhite(theline) == theline
3388 ? theline : theline + STRLEN(heredoc_trimmed);
3389 else
3390 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02003391 if (STRCMP(p, skip_until) == 0)
3392 {
3393 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003394 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003395 getline_options = eap->cmdidx == CMD_def
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01003396 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003397 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003398 }
3399 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003400 }
3401 else
3402 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003403 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01003404 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003405 ;
3406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 // Check for "endfunction" or "enddef".
3408 if (checkforcmd(&p, nesting_def[nesting]
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003409 ? "enddef" : "endfunction", 4))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003410 {
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003411 if (nesting-- == 0)
Bram Moolenaar53564f72017-06-24 14:48:11 +02003412 {
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003413 char_u *nextcmd = NULL;
3414
3415 if (*p == '|')
3416 nextcmd = p + 1;
3417 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
3418 nextcmd = line_arg;
3419 else if (*p != NUL && *p != '"' && p_verbose > 0)
3420 give_warning2(eap->cmdidx == CMD_def
3421 ? (char_u *)_("W1001: Text found after :enddef: %s")
3422 : (char_u *)_("W22: Text found after :endfunction: %s"),
3423 p, TRUE);
3424 if (nextcmd != NULL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02003425 {
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003426 // Another command follows. If the line came from "eap"
3427 // we can simply point into it, otherwise we need to
3428 // change "eap->cmdlinep".
3429 eap->nextcmd = nextcmd;
3430 if (line_to_free != NULL)
3431 {
3432 vim_free(*eap->cmdlinep);
3433 *eap->cmdlinep = line_to_free;
3434 line_to_free = NULL;
3435 }
Bram Moolenaar53564f72017-06-24 14:48:11 +02003436 }
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003437 break;
Bram Moolenaar53564f72017-06-24 14:48:11 +02003438 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003439 }
3440
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003441 // Check for mismatched "endfunc" or "enddef".
3442 // We don't check for "def" inside "func" thus we also can't check
3443 // for "enddef".
3444 // We continue to find the end of the function, although we might
3445 // not find it.
3446 else if (nesting_def[nesting])
3447 {
3448 if (checkforcmd(&p, "endfunction", 4))
3449 emsg(_(e_mismatched_endfunction));
3450 }
3451 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
3452 emsg(_(e_mismatched_enddef));
3453
Bram Moolenaare38eab22019-12-05 21:50:01 +01003454 // Increase indent inside "if", "while", "for" and "try", decrease
3455 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003457 indent -= 2;
3458 else if (STRNCMP(p, "if", 2) == 0
3459 || STRNCMP(p, "wh", 2) == 0
3460 || STRNCMP(p, "for", 3) == 0
3461 || STRNCMP(p, "try", 3) == 0)
3462 indent += 2;
3463
Bram Moolenaare38eab22019-12-05 21:50:01 +01003464 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01003465 // Only recognize "def" inside "def", not inside "function",
3466 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 c = *p;
Bram Moolenaaradc8e442020-12-31 18:28:18 +01003468 if (is_function_cmd(&p)
Bram Moolenaar673660a2020-01-26 16:50:05 +01003469 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003470 {
3471 if (*p == '!')
3472 p = skipwhite(p + 1);
3473 p += eval_fname_script(p);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003474 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003475 if (*skipwhite(p) == '(')
3476 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 if (nesting == MAX_FUNC_NESTING - 1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003478 emsg(_(e_function_nesting_too_deep));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 else
3480 {
3481 ++nesting;
3482 nesting_def[nesting] = (c == 'd');
3483 indent += 2;
3484 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003485 }
3486 }
3487
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003488 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003489 p = skip_range(p, FALSE, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003490 if (eap->cmdidx != CMD_def
3491 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01003492 || (p[0] == 'c'
3493 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
3494 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
3495 && (STRNCMP(&p[3], "nge", 3) != 0
3496 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003497 || (p[0] == 'i'
3498 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003499 && (!ASCII_ISALPHA(p[2])
3500 || (p[2] == 's'
3501 && (!ASCII_ISALPHA(p[3])
3502 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003503 skip_until = vim_strsave((char_u *)".");
3504
Bram Moolenaare38eab22019-12-05 21:50:01 +01003505 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003506 arg = skipwhite(skiptowhite(p));
3507 if (arg[0] == '<' && arg[1] =='<'
3508 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003509 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
3510 || ((p[2] == '3' || p[2] == 'x')
3511 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003512 || (p[0] == 'p' && p[1] == 'e'
3513 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
3514 || (p[0] == 't' && p[1] == 'c'
3515 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
3516 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
3517 && !ASCII_ISALPHA(p[3]))
3518 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
3519 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
3520 || (p[0] == 'm' && p[1] == 'z'
3521 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
3522 ))
3523 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003524 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003525 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003526 if (STRNCMP(p, "trim", 4) == 0)
3527 {
3528 // Ignore leading white space.
3529 p = skipwhite(p + 4);
3530 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003531 skipwhite(theline) - theline);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003532 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003533 if (*p == NUL)
3534 skip_until = vim_strsave((char_u *)".");
3535 else
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003536 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003537 getline_options = GETLINE_NONE;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003538 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003539 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003540
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003541 // Check for ":cmd v =<< [trim] EOF"
3542 // and ":cmd [a, b] =<< [trim] EOF"
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003543 // and "lines =<< [trim] EOF" for Vim9
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003544 // Where "cmd" can be "let", "var", "final" or "const".
Bram Moolenaar8471e572019-05-19 21:37:18 +02003545 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003546 if (*arg == '[')
3547 arg = vim_strchr(arg, ']');
3548 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003549 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003550 int found = (eap->cmdidx == CMD_def && arg[0] == '='
3551 && arg[1] == '<' && arg[2] =='<');
3552
3553 if (!found)
3554 // skip over the argument after "cmd"
3555 arg = skipwhite(skiptowhite(arg));
3556 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003557 && (checkforcmd(&p, "let", 2)
3558 || checkforcmd(&p, "var", 3)
3559 || checkforcmd(&p, "final", 5)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003560 || checkforcmd(&p, "const", 5))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02003561 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003562 p = skipwhite(arg + 3);
3563 if (STRNCMP(p, "trim", 4) == 0)
3564 {
3565 // Ignore leading white space.
3566 p = skipwhite(p + 4);
3567 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003568 skipwhite(theline) - theline);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003569 }
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003570 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003571 getline_options = GETLINE_NONE;
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003572 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003573 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003574 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003575 }
3576
Bram Moolenaare38eab22019-12-05 21:50:01 +01003577 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003578 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003579 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003580
Bram Moolenaare38eab22019-12-05 21:50:01 +01003581 // Copy the line to newly allocated memory. get_one_sourceline()
3582 // allocates 250 bytes per line, this saves 80% on average. The cost
3583 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003584 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003585 if (p == NULL)
3586 goto erret;
3587 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003588
Bram Moolenaare38eab22019-12-05 21:50:01 +01003589 // Add NULL lines for continuation lines, so that the line count is
3590 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003591 while (sourcing_lnum_off-- > 0)
3592 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3593
Bram Moolenaare38eab22019-12-05 21:50:01 +01003594 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003595 if (line_arg != NULL && *line_arg == NUL)
3596 line_arg = NULL;
3597 }
3598
Bram Moolenaare38eab22019-12-05 21:50:01 +01003599 // Don't define the function when skipping commands or when an error was
3600 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003601 if (eap->skip || did_emsg)
3602 goto erret;
3603
3604 /*
3605 * If there are no errors, add the function
3606 */
3607 if (fudi.fd_dict == NULL)
3608 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 hashtab_T *ht;
3610
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003611 v = find_var(name, &ht, FALSE);
3612 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3613 {
3614 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3615 name);
3616 goto erret;
3617 }
3618
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003619 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaareef21022020-08-01 22:16:43 +02003620 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003621 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003622 char_u *uname = untrans_function_name(name);
3623
3624 import = find_imported(uname == NULL ? name : uname, 0, NULL);
3625 }
3626
3627 if (fp != NULL || import != NULL)
3628 {
3629 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003631 // Function can be replaced with "function!" and when sourcing the
3632 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02003633 // A name that is used by an import can not be overruled.
3634 if (import != NULL
3635 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003636 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02003637 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003638 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003639 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003640 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02003641 else
3642 emsg_funcname(e_funcexts, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003643 goto erret;
3644 }
3645 if (fp->uf_calls > 0)
3646 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003647 emsg_funcname(
3648 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003649 name);
3650 goto erret;
3651 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003652 if (fp->uf_refcount > 1)
3653 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003654 // This function is referenced somewhere, don't redefine it but
3655 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003656 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003657 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003658 fp = NULL;
3659 overwrite = TRUE;
3660 }
3661 else
3662 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003663 char_u *exp_name = fp->uf_name_exp;
3664
3665 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003666 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003667 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003668 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003669 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003671#ifdef FEAT_PROFILE
3672 fp->uf_profiling = FALSE;
3673 fp->uf_prof_initialized = FALSE;
3674#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003675 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003676 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003677 }
3678 }
3679 else
3680 {
3681 char numbuf[20];
3682
3683 fp = NULL;
3684 if (fudi.fd_newkey == NULL && !eap->forceit)
3685 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003686 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003687 goto erret;
3688 }
3689 if (fudi.fd_di == NULL)
3690 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003691 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02003692 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003693 goto erret;
3694 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003695 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02003696 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003697 goto erret;
3698
Bram Moolenaare38eab22019-12-05 21:50:01 +01003699 // Give the function a sequential number. Can only be used with a
3700 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003701 vim_free(name);
3702 sprintf(numbuf, "%d", ++func_nr);
3703 name = vim_strsave((char_u *)numbuf);
3704 if (name == NULL)
3705 goto erret;
3706 }
3707
3708 if (fp == NULL)
3709 {
3710 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3711 {
3712 int slen, plen;
3713 char_u *scriptname;
3714
Bram Moolenaare38eab22019-12-05 21:50:01 +01003715 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003716 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003717 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003718 {
3719 scriptname = autoload_name(name);
3720 if (scriptname != NULL)
3721 {
3722 p = vim_strchr(scriptname, '/');
3723 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003724 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003725 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003726 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003727 j = OK;
3728 vim_free(scriptname);
3729 }
3730 }
3731 if (j == FAIL)
3732 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003733 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003734 goto erret;
3735 }
3736 }
3737
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003738 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003739 if (fp == NULL)
3740 goto erret;
3741
3742 if (fudi.fd_dict != NULL)
3743 {
3744 if (fudi.fd_di == NULL)
3745 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003746 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003747 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3748 if (fudi.fd_di == NULL)
3749 {
3750 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003751 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003752 goto erret;
3753 }
3754 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3755 {
3756 vim_free(fudi.fd_di);
3757 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003758 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003759 goto erret;
3760 }
3761 }
3762 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003763 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003764 clear_tv(&fudi.fd_di->di_tv);
3765 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003766 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003767
Bram Moolenaare38eab22019-12-05 21:50:01 +01003768 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003769 flags |= FC_DICT;
3770 }
3771
Bram Moolenaare38eab22019-12-05 21:50:01 +01003772 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003773 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003774 if (overwrite)
3775 {
3776 hi = hash_find(&func_hashtab, name);
3777 hi->hi_key = UF2HIKEY(fp);
3778 }
3779 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003780 {
3781 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003782 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003783 goto erret;
3784 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003785 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003786 }
3787 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003788 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003790 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791
3792 if (eap->cmdidx == CMD_def)
3793 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003794 int lnum_save = SOURCING_LNUM;
3795 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003796
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003797 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003798
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003799 // error messages are for the first function line
3800 SOURCING_LNUM = sourcing_lnum_top;
3801
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003802 if (cstack != NULL && cstack->cs_idx >= 0)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003803 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003804 int count = cstack->cs_idx + 1;
3805 int i;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003806
3807 // The block context may be needed for script variables declared in
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003808 // a block that is visible now but not when the function is called
3809 // later.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003810 fp->uf_block_ids = ALLOC_MULT(int, count);
3811 if (fp->uf_block_ids != NULL)
3812 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003813 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003814 sizeof(int) * count);
3815 fp->uf_block_depth = count;
3816 }
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003817
3818 // Set flag in each block to indicate a function was defined. This
3819 // is used to keep the variable when leaving the block, see
3820 // hide_script_var().
3821 for (i = 0; i <= cstack->cs_idx; ++i)
3822 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003823 }
3824
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003825 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003827 SOURCING_LNUM = lnum_save;
3828 goto errret_2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003830 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831
3832 // parse the return type, if any
3833 if (ret_type == NULL)
3834 fp->uf_ret_type = &t_void;
3835 else
3836 {
3837 p = ret_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01003838 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02003840 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003842 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003843 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003845 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003846 if ((flags & FC_CLOSURE) != 0)
3847 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003848 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003849 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003850 }
3851 else
3852 fp->uf_scoped = NULL;
3853
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003854#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003855 if (prof_def_func())
3856 func_do_profile(fp);
3857#endif
3858 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003859 if (sandbox)
3860 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003861 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003862 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003863 fp->uf_flags = flags;
3864 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003865 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003866 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02003867 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003868 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 if (is_export)
3870 {
3871 fp->uf_flags |= FC_EXPORT;
3872 // let ex_export() know the export worked.
3873 is_export = FALSE;
3874 }
3875
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003876 if (eap->cmdidx == CMD_def)
3877 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02003878 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
3879 // :func does not use Vim9 script syntax, even in a Vim9 script file
3880 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003881
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003882 goto ret_free;
3883
3884erret:
3885 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003886 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003887errret_2:
3888 ga_clear_strings(&newlines);
3889ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003890 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003891 vim_free(skip_until);
Bram Moolenaarfffdf472020-12-13 21:16:55 +01003892 vim_free(heredoc_trimmed);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003893 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003894 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003895 if (name != name_arg)
3896 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003897 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003898 did_emsg |= saved_did_emsg;
3899 need_wait_return |= saved_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003900
3901 return fp;
3902}
3903
3904/*
3905 * ":function"
3906 */
3907 void
3908ex_function(exarg_T *eap)
3909{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003910 (void)define_function(eap, NULL);
Bram Moolenaar822ba242020-05-24 23:00:18 +02003911}
3912
3913/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02003914 * :defcompile - compile all :def functions in the current script that need to
3915 * be compiled. Except dead functions.
Bram Moolenaar822ba242020-05-24 23:00:18 +02003916 */
3917 void
3918ex_defcompile(exarg_T *eap UNUSED)
3919{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003920 long todo = (long)func_hashtab.ht_used;
3921 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003922 hashitem_T *hi;
3923 ufunc_T *ufunc;
3924
3925 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
3926 {
3927 if (!HASHITEM_EMPTY(hi))
3928 {
3929 --todo;
3930 ufunc = HI2UF(hi);
3931 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02003932 && ufunc->uf_def_status == UF_TO_BE_COMPILED
3933 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003934 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02003935 compile_def_function(ufunc, FALSE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003936
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003937 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003938 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003939 // a function has been added or removed, need to start over
3940 todo = (long)func_hashtab.ht_used;
3941 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003942 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02003943 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003944 }
3945 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003946 }
3947 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003948}
3949
3950/*
3951 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3952 * Return 2 if "p" starts with "s:".
3953 * Return 0 otherwise.
3954 */
3955 int
3956eval_fname_script(char_u *p)
3957{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003958 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3959 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003960 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3961 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3962 return 5;
3963 if (p[0] == 's' && p[1] == ':')
3964 return 2;
3965 return 0;
3966}
3967
3968 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003969translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003970{
3971 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003972 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003973 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974}
3975
3976/*
3977 * Return TRUE when "ufunc" has old-style "..." varargs
3978 * or named varargs "...name: type".
3979 */
3980 int
3981has_varargs(ufunc_T *ufunc)
3982{
3983 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003984}
3985
3986/*
3987 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003988 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003989 */
3990 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003991function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003992{
3993 char_u *nm = name;
3994 char_u *p;
3995 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003996 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003997 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003998
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003999 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
4000 if (no_deref)
4001 flag |= TFN_NO_DEREF;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004002 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004003 nm = skipwhite(nm);
4004
Bram Moolenaare38eab22019-12-05 21:50:01 +01004005 // Only accept "funcname", "funcname ", "funcname (..." and
4006 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004007 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004008 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004009 vim_free(p);
4010 return n;
4011}
4012
Bram Moolenaar113e1072019-01-20 15:30:40 +01004013#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004014 char_u *
4015get_expanded_name(char_u *name, int check)
4016{
4017 char_u *nm = name;
4018 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004019 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004020
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004021 p = trans_function_name(&nm, &is_global, FALSE,
4022 TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004023
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004024 if (p != NULL && *nm == NUL
4025 && (!check || translated_function_exists(p, is_global)))
4026 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004027
4028 vim_free(p);
4029 return NULL;
4030}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004031#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004032
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004033/*
4034 * Function given to ExpandGeneric() to obtain the list of user defined
4035 * function names.
4036 */
4037 char_u *
4038get_user_func_name(expand_T *xp, int idx)
4039{
4040 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004041 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004042 static hashitem_T *hi;
4043 ufunc_T *fp;
4044
4045 if (idx == 0)
4046 {
4047 done = 0;
4048 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004049 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004050 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004051 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004052 {
4053 if (done++ > 0)
4054 ++hi;
4055 while (HASHITEM_EMPTY(hi))
4056 ++hi;
4057 fp = HI2UF(hi);
4058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 // don't show dead, dict and lambda functions
4060 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02004061 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004063
4064 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004065 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004066
4067 cat_func_name(IObuff, fp);
4068 if (xp->xp_context != EXPAND_USER_FUNC)
4069 {
4070 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004072 STRCAT(IObuff, ")");
4073 }
4074 return IObuff;
4075 }
4076 return NULL;
4077}
4078
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004079/*
4080 * ":delfunction {name}"
4081 */
4082 void
4083ex_delfunction(exarg_T *eap)
4084{
4085 ufunc_T *fp = NULL;
4086 char_u *p;
4087 char_u *name;
4088 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004089 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004090
4091 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004092 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004093 vim_free(fudi.fd_newkey);
4094 if (name == NULL)
4095 {
4096 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004097 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004098 return;
4099 }
4100 if (!ends_excmd(*skipwhite(p)))
4101 {
4102 vim_free(name);
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004103 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004104 return;
4105 }
4106 eap->nextcmd = check_nextcmd(p);
4107 if (eap->nextcmd != NULL)
4108 *p = NUL;
4109
4110 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004111 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004112 vim_free(name);
4113
4114 if (!eap->skip)
4115 {
4116 if (fp == NULL)
4117 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004118 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004119 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004120 return;
4121 }
4122 if (fp->uf_calls > 0)
4123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004124 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004125 return;
4126 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004127 if (fp->uf_flags & FC_VIM9)
4128 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004129 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004130 return;
4131 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004132
4133 if (fudi.fd_dict != NULL)
4134 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004135 // Delete the dict item that refers to the function, it will
4136 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004137 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4138 }
4139 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004140 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004141 // A normal function (not a numbered function or lambda) has a
4142 // refcount of 1 for the entry in the hashtable. When deleting
4143 // it and the refcount is more than one, it should be kept.
4144 // A numbered function and lambda should be kept if the refcount is
4145 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004146 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004147 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004148 // Function is still referenced somewhere. Don't free it but
4149 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004150 if (func_remove(fp))
4151 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004152 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004153 }
4154 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004155 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004156 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004157 }
4158}
4159
4160/*
4161 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004162 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004163 */
4164 void
4165func_unref(char_u *name)
4166{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004167 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004168
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004169 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004170 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004171 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004172 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004173 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004174#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004175 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004176#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004177 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004178 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004179 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004180 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004181 // Only delete it when it's not being used. Otherwise it's done
4182 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004183 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004184 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02004185 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004186}
4187
4188/*
4189 * Unreference a Function: decrement the reference count and free it when it
4190 * becomes zero.
4191 */
4192 void
4193func_ptr_unref(ufunc_T *fp)
4194{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004195 if (fp != NULL && --fp->uf_refcount <= 0)
4196 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004197 // Only delete it when it's not being used. Otherwise it's done
4198 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004199 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004200 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004201 }
4202}
4203
4204/*
4205 * Count a reference to a Function.
4206 */
4207 void
4208func_ref(char_u *name)
4209{
4210 ufunc_T *fp;
4211
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004212 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004213 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004214 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004215 if (fp != NULL)
4216 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004217 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01004218 // Only give an error for a numbered function.
4219 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01004220 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004221}
4222
4223/*
4224 * Count a reference to a Function.
4225 */
4226 void
4227func_ptr_ref(ufunc_T *fp)
4228{
4229 if (fp != NULL)
4230 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004231}
4232
4233/*
4234 * Return TRUE if items in "fc" do not have "copyID". That means they are not
4235 * referenced from anywhere that is in use.
4236 */
4237 static int
4238can_free_funccal(funccall_T *fc, int copyID)
4239{
4240 return (fc->l_varlist.lv_copyID != copyID
4241 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004242 && fc->l_avars.dv_copyID != copyID
4243 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004244}
4245
4246/*
4247 * ":return [expr]"
4248 */
4249 void
4250ex_return(exarg_T *eap)
4251{
4252 char_u *arg = eap->arg;
4253 typval_T rettv;
4254 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004255 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004256
4257 if (current_funccal == NULL)
4258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004259 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004260 return;
4261 }
4262
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004263 CLEAR_FIELD(evalarg);
4264 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
4265
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004266 if (eap->skip)
4267 ++emsg_skip;
4268
4269 eap->nextcmd = NULL;
4270 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004271 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004272 {
4273 if (!eap->skip)
4274 returning = do_return(eap, FALSE, TRUE, &rettv);
4275 else
4276 clear_tv(&rettv);
4277 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004278 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004279 else if (!eap->skip)
4280 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004281 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01004282 update_force_abort();
4283
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004284 /*
4285 * Return unless the expression evaluation has been cancelled due to an
4286 * aborting error, an interrupt, or an exception.
4287 */
4288 if (!aborting())
4289 returning = do_return(eap, FALSE, TRUE, NULL);
4290 }
4291
Bram Moolenaare38eab22019-12-05 21:50:01 +01004292 // When skipping or the return gets pending, advance to the next command
4293 // in this line (!returning). Otherwise, ignore the rest of the line.
4294 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004295 if (returning)
4296 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004297 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004298 eap->nextcmd = check_nextcmd(arg);
4299
4300 if (eap->skip)
4301 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02004302 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004303}
4304
4305/*
4306 * ":1,25call func(arg1, arg2)" function call.
4307 */
4308 void
4309ex_call(exarg_T *eap)
4310{
4311 char_u *arg = eap->arg;
4312 char_u *startarg;
4313 char_u *name;
4314 char_u *tofree;
4315 int len;
4316 typval_T rettv;
4317 linenr_T lnum;
4318 int doesrange;
4319 int failed = FALSE;
4320 funcdict_T fudi;
4321 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004322 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004323
Bram Moolenaare6b53242020-07-01 17:28:33 +02004324 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004325 if (eap->skip)
4326 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004327 // trans_function_name() doesn't work well when skipping, use eval0()
4328 // instead to skip to any following command, e.g. for:
4329 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004330 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004331 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004332 clear_tv(&rettv);
4333 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004334 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004335 return;
4336 }
4337
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004338 tofree = trans_function_name(&arg, NULL, eap->skip,
4339 TFN_INT, &fudi, &partial);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004340 if (fudi.fd_newkey != NULL)
4341 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004342 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004343 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004344 vim_free(fudi.fd_newkey);
4345 }
4346 if (tofree == NULL)
4347 return;
4348
Bram Moolenaare38eab22019-12-05 21:50:01 +01004349 // Increase refcount on dictionary, it could get deleted when evaluating
4350 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004351 if (fudi.fd_dict != NULL)
4352 ++fudi.fd_dict->dv_refcount;
4353
Bram Moolenaare38eab22019-12-05 21:50:01 +01004354 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
4355 // contents. For VAR_PARTIAL get its partial, unless we already have one
4356 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004357 len = (int)STRLEN(tofree);
4358 name = deref_func_name(tofree, &len,
4359 partial != NULL ? NULL : &partial, FALSE);
4360
Bram Moolenaare38eab22019-12-05 21:50:01 +01004361 // Skip white space to allow ":call func ()". Not good, but required for
4362 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004363 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004364 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004365
4366 if (*startarg != '(')
4367 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004369 goto end;
4370 }
4371
4372 /*
4373 * When skipping, evaluate the function once, to find the end of the
4374 * arguments.
4375 * When the function takes a range, this is discovered after the first
4376 * call, and the loop is broken.
4377 */
4378 if (eap->skip)
4379 {
4380 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004381 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004382 }
4383 else
4384 lnum = eap->line1;
4385 for ( ; lnum <= eap->line2; ++lnum)
4386 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004387 funcexe_T funcexe;
4388
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004389 if (!eap->skip && eap->addr_count > 0)
4390 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004391 if (lnum > curbuf->b_ml.ml_line_count)
4392 {
4393 // If the function deleted lines or switched to another buffer
4394 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004395 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004396 break;
4397 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004398 curwin->w_cursor.lnum = lnum;
4399 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004400 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004401 }
4402 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004403
Bram Moolenaara80faa82020-04-12 19:37:17 +02004404 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004405 funcexe.firstline = eap->line1;
4406 funcexe.lastline = eap->line2;
4407 funcexe.doesrange = &doesrange;
4408 funcexe.evaluate = !eap->skip;
4409 funcexe.partial = partial;
4410 funcexe.selfdict = fudi.fd_dict;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004411 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004412 {
4413 failed = TRUE;
4414 break;
4415 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01004416 if (has_watchexpr())
4417 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004418
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004419 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004420 if (handle_subscript(&arg, &rettv,
4421 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004422 {
4423 failed = TRUE;
4424 break;
4425 }
4426
4427 clear_tv(&rettv);
4428 if (doesrange || eap->skip)
4429 break;
4430
Bram Moolenaare38eab22019-12-05 21:50:01 +01004431 // Stop when immediately aborting on error, or when an interrupt
4432 // occurred or an exception was thrown but not caught.
4433 // get_func_tv() returned OK, so that the check for trailing
4434 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004435 if (aborting())
4436 break;
4437 }
4438 if (eap->skip)
4439 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004440 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004441
Bram Moolenaare51bb172020-02-16 19:42:23 +01004442 // When inside :try we need to check for following "| catch".
4443 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004444 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004445 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02004446 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004447 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004448 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004449 if (!failed)
4450 {
4451 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004452 semsg(_(e_trailing_arg), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004453 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004454 }
4455 else
4456 eap->nextcmd = check_nextcmd(arg);
4457 }
4458
4459end:
4460 dict_unref(fudi.fd_dict);
4461 vim_free(tofree);
4462}
4463
4464/*
4465 * Return from a function. Possibly makes the return pending. Also called
4466 * for a pending return at the ":endtry" or after returning from an extra
4467 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
4468 * when called due to a ":return" command. "rettv" may point to a typval_T
4469 * with the return rettv. Returns TRUE when the return can be carried out,
4470 * FALSE when the return gets pending.
4471 */
4472 int
4473do_return(
4474 exarg_T *eap,
4475 int reanimate,
4476 int is_cmd,
4477 void *rettv)
4478{
4479 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01004480 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004481
4482 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004483 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004484 current_funccal->returned = FALSE;
4485
4486 /*
4487 * Cleanup (and inactivate) conditionals, but stop when a try conditional
4488 * not in its finally clause (which then is to be executed next) is found.
4489 * In this case, make the ":return" pending for execution at the ":endtry".
4490 * Otherwise, return normally.
4491 */
4492 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
4493 if (idx >= 0)
4494 {
4495 cstack->cs_pending[idx] = CSTP_RETURN;
4496
4497 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004498 // A pending return again gets pending. "rettv" points to an
4499 // allocated variable with the rettv of the original ":return"'s
4500 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004501 cstack->cs_rettv[idx] = rettv;
4502 else
4503 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004504 // When undoing a return in order to make it pending, get the stored
4505 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004506 if (reanimate)
4507 rettv = current_funccal->rettv;
4508
4509 if (rettv != NULL)
4510 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004511 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004512 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
4513 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
4514 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004515 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004516 }
4517 else
4518 cstack->cs_rettv[idx] = NULL;
4519
4520 if (reanimate)
4521 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004522 // The pending return value could be overwritten by a ":return"
4523 // without argument in a finally clause; reset the default
4524 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004525 current_funccal->rettv->v_type = VAR_NUMBER;
4526 current_funccal->rettv->vval.v_number = 0;
4527 }
4528 }
4529 report_make_pending(CSTP_RETURN, rettv);
4530 }
4531 else
4532 {
4533 current_funccal->returned = TRUE;
4534
Bram Moolenaare38eab22019-12-05 21:50:01 +01004535 // If the return is carried out now, store the return value. For
4536 // a return immediately after reanimation, the value is already
4537 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004538 if (!reanimate && rettv != NULL)
4539 {
4540 clear_tv(current_funccal->rettv);
4541 *current_funccal->rettv = *(typval_T *)rettv;
4542 if (!is_cmd)
4543 vim_free(rettv);
4544 }
4545 }
4546
4547 return idx < 0;
4548}
4549
4550/*
4551 * Free the variable with a pending return value.
4552 */
4553 void
4554discard_pending_return(void *rettv)
4555{
4556 free_tv((typval_T *)rettv);
4557}
4558
4559/*
4560 * Generate a return command for producing the value of "rettv". The result
4561 * is an allocated string. Used by report_pending() for verbose messages.
4562 */
4563 char_u *
4564get_return_cmd(void *rettv)
4565{
4566 char_u *s = NULL;
4567 char_u *tofree = NULL;
4568 char_u numbuf[NUMBUFLEN];
4569
4570 if (rettv != NULL)
4571 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
4572 if (s == NULL)
4573 s = (char_u *)"";
4574
4575 STRCPY(IObuff, ":return ");
4576 STRNCPY(IObuff + 8, s, IOSIZE - 8);
4577 if (STRLEN(s) + 8 >= IOSIZE)
4578 STRCPY(IObuff + IOSIZE - 4, "...");
4579 vim_free(tofree);
4580 return vim_strsave(IObuff);
4581}
4582
4583/*
4584 * Get next function line.
4585 * Called by do_cmdline() to get the next line.
4586 * Returns allocated string, or NULL for end of function.
4587 */
4588 char_u *
4589get_func_line(
4590 int c UNUSED,
4591 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02004592 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004593 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004594{
4595 funccall_T *fcp = (funccall_T *)cookie;
4596 ufunc_T *fp = fcp->func;
4597 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004598 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004599
Bram Moolenaare38eab22019-12-05 21:50:01 +01004600 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004601 if (fcp->dbg_tick != debug_tick)
4602 {
4603 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004604 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004605 fcp->dbg_tick = debug_tick;
4606 }
4607#ifdef FEAT_PROFILE
4608 if (do_profiling == PROF_YES)
4609 func_line_end(cookie);
4610#endif
4611
4612 gap = &fp->uf_lines;
4613 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4614 || fcp->returned)
4615 retval = NULL;
4616 else
4617 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004618 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004619 while (fcp->linenr < gap->ga_len
4620 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
4621 ++fcp->linenr;
4622 if (fcp->linenr >= gap->ga_len)
4623 retval = NULL;
4624 else
4625 {
4626 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004627 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004628#ifdef FEAT_PROFILE
4629 if (do_profiling == PROF_YES)
4630 func_line_start(cookie);
4631#endif
4632 }
4633 }
4634
Bram Moolenaare38eab22019-12-05 21:50:01 +01004635 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004636 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004637 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004638 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004639 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004640 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004641 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004642 fcp->dbg_tick = debug_tick;
4643 }
4644
4645 return retval;
4646}
4647
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004648/*
4649 * Return TRUE if the currently active function should be ended, because a
4650 * return was encountered or an error occurred. Used inside a ":while".
4651 */
4652 int
4653func_has_ended(void *cookie)
4654{
4655 funccall_T *fcp = (funccall_T *)cookie;
4656
Bram Moolenaare38eab22019-12-05 21:50:01 +01004657 // Ignore the "abort" flag if the abortion behavior has been changed due to
4658 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004659 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4660 || fcp->returned);
4661}
4662
4663/*
4664 * return TRUE if cookie indicates a function which "abort"s on errors.
4665 */
4666 int
4667func_has_abort(
4668 void *cookie)
4669{
4670 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
4671}
4672
4673
4674/*
4675 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4676 * Don't do this when "Func" is already a partial that was bound
4677 * explicitly (pt_auto is FALSE).
4678 * Changes "rettv" in-place.
4679 * Returns the updated "selfdict_in".
4680 */
4681 dict_T *
4682make_partial(dict_T *selfdict_in, typval_T *rettv)
4683{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004684 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004685 char_u *tofree = NULL;
4686 ufunc_T *fp;
4687 char_u fname_buf[FLEN_FIXED + 1];
4688 int error;
4689 dict_T *selfdict = selfdict_in;
4690
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004691 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4692 fp = rettv->vval.v_partial->pt_func;
4693 else
4694 {
4695 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4696 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004697 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004698 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004699 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004700 vim_free(tofree);
4701 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004702
4703 if (fp != NULL && (fp->uf_flags & FC_DICT))
4704 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004705 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004706
4707 if (pt != NULL)
4708 {
4709 pt->pt_refcount = 1;
4710 pt->pt_dict = selfdict;
4711 pt->pt_auto = TRUE;
4712 selfdict = NULL;
4713 if (rettv->v_type == VAR_FUNC)
4714 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004715 // Just a function: Take over the function name and use
4716 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004717 pt->pt_name = rettv->vval.v_string;
4718 }
4719 else
4720 {
4721 partial_T *ret_pt = rettv->vval.v_partial;
4722 int i;
4723
Bram Moolenaare38eab22019-12-05 21:50:01 +01004724 // Partial: copy the function name, use selfdict and copy
4725 // args. Can't take over name or args, the partial might
4726 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004727 if (ret_pt->pt_name != NULL)
4728 {
4729 pt->pt_name = vim_strsave(ret_pt->pt_name);
4730 func_ref(pt->pt_name);
4731 }
4732 else
4733 {
4734 pt->pt_func = ret_pt->pt_func;
4735 func_ptr_ref(pt->pt_func);
4736 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004737 if (ret_pt->pt_argc > 0)
4738 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004739 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004740 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004741 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004742 pt->pt_argc = 0;
4743 else
4744 {
4745 pt->pt_argc = ret_pt->pt_argc;
4746 for (i = 0; i < pt->pt_argc; i++)
4747 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4748 }
4749 }
4750 partial_unref(ret_pt);
4751 }
4752 rettv->v_type = VAR_PARTIAL;
4753 rettv->vval.v_partial = pt;
4754 }
4755 }
4756 return selfdict;
4757}
4758
4759/*
4760 * Return the name of the executed function.
4761 */
4762 char_u *
4763func_name(void *cookie)
4764{
4765 return ((funccall_T *)cookie)->func->uf_name;
4766}
4767
4768/*
4769 * Return the address holding the next breakpoint line for a funccall cookie.
4770 */
4771 linenr_T *
4772func_breakpoint(void *cookie)
4773{
4774 return &((funccall_T *)cookie)->breakpoint;
4775}
4776
4777/*
4778 * Return the address holding the debug tick for a funccall cookie.
4779 */
4780 int *
4781func_dbg_tick(void *cookie)
4782{
4783 return &((funccall_T *)cookie)->dbg_tick;
4784}
4785
4786/*
4787 * Return the nesting level for a funccall cookie.
4788 */
4789 int
4790func_level(void *cookie)
4791{
4792 return ((funccall_T *)cookie)->level;
4793}
4794
4795/*
4796 * Return TRUE when a function was ended by a ":return" command.
4797 */
4798 int
4799current_func_returned(void)
4800{
4801 return current_funccal->returned;
4802}
4803
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004804 int
4805free_unref_funccal(int copyID, int testing)
4806{
4807 int did_free = FALSE;
4808 int did_free_funccal = FALSE;
4809 funccall_T *fc, **pfc;
4810
4811 for (pfc = &previous_funccal; *pfc != NULL; )
4812 {
4813 if (can_free_funccal(*pfc, copyID))
4814 {
4815 fc = *pfc;
4816 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004817 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004818 did_free = TRUE;
4819 did_free_funccal = TRUE;
4820 }
4821 else
4822 pfc = &(*pfc)->caller;
4823 }
4824 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004825 // When a funccal was freed some more items might be garbage
4826 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004827 (void)garbage_collect(testing);
4828
4829 return did_free;
4830}
4831
4832/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004833 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004834 */
4835 static funccall_T *
4836get_funccal(void)
4837{
4838 int i;
4839 funccall_T *funccal;
4840 funccall_T *temp_funccal;
4841
4842 funccal = current_funccal;
4843 if (debug_backtrace_level > 0)
4844 {
4845 for (i = 0; i < debug_backtrace_level; i++)
4846 {
4847 temp_funccal = funccal->caller;
4848 if (temp_funccal)
4849 funccal = temp_funccal;
4850 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004851 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004852 debug_backtrace_level = i;
4853 }
4854 }
4855 return funccal;
4856}
4857
4858/*
4859 * Return the hashtable used for local variables in the current funccal.
4860 * Return NULL if there is no current funccal.
4861 */
4862 hashtab_T *
4863get_funccal_local_ht()
4864{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004866 return NULL;
4867 return &get_funccal()->l_vars.dv_hashtab;
4868}
4869
4870/*
4871 * Return the l: scope variable.
4872 * Return NULL if there is no current funccal.
4873 */
4874 dictitem_T *
4875get_funccal_local_var()
4876{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004878 return NULL;
4879 return &get_funccal()->l_vars_var;
4880}
4881
4882/*
4883 * Return the hashtable used for argument in the current funccal.
4884 * Return NULL if there is no current funccal.
4885 */
4886 hashtab_T *
4887get_funccal_args_ht()
4888{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004890 return NULL;
4891 return &get_funccal()->l_avars.dv_hashtab;
4892}
4893
4894/*
4895 * Return the a: scope variable.
4896 * Return NULL if there is no current funccal.
4897 */
4898 dictitem_T *
4899get_funccal_args_var()
4900{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004901 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004902 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004903 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004904}
4905
4906/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004907 * List function variables, if there is a function.
4908 */
4909 void
4910list_func_vars(int *first)
4911{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004913 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004914 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004915}
4916
4917/*
4918 * If "ht" is the hashtable for local variables in the current funccal, return
4919 * the dict that contains it.
4920 * Otherwise return NULL.
4921 */
4922 dict_T *
4923get_current_funccal_dict(hashtab_T *ht)
4924{
4925 if (current_funccal != NULL
4926 && ht == &current_funccal->l_vars.dv_hashtab)
4927 return &current_funccal->l_vars;
4928 return NULL;
4929}
4930
4931/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004932 * Search hashitem in parent scope.
4933 */
4934 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004935find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004936{
4937 funccall_T *old_current_funccal = current_funccal;
4938 hashtab_T *ht;
4939 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004940 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004941
4942 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4943 return NULL;
4944
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004945 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004946 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004947 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004948 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004949 ht = find_var_ht(name, &varname);
4950 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004951 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004952 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004953 if (!HASHITEM_EMPTY(hi))
4954 {
4955 *pht = ht;
4956 break;
4957 }
4958 }
4959 if (current_funccal == current_funccal->func->uf_scoped)
4960 break;
4961 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004962 }
4963 current_funccal = old_current_funccal;
4964
4965 return hi;
4966}
4967
4968/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004969 * Search variable in parent scope.
4970 */
4971 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004972find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004973{
4974 dictitem_T *v = NULL;
4975 funccall_T *old_current_funccal = current_funccal;
4976 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004977 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004978
4979 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4980 return NULL;
4981
Bram Moolenaare38eab22019-12-05 21:50:01 +01004982 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004983 current_funccal = current_funccal->func->uf_scoped;
4984 while (current_funccal)
4985 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004986 ht = find_var_ht(name, &varname);
4987 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004988 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004989 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004990 if (v != NULL)
4991 break;
4992 }
4993 if (current_funccal == current_funccal->func->uf_scoped)
4994 break;
4995 current_funccal = current_funccal->func->uf_scoped;
4996 }
4997 current_funccal = old_current_funccal;
4998
4999 return v;
5000}
5001
5002/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005003 * Set "copyID + 1" in previous_funccal and callers.
5004 */
5005 int
5006set_ref_in_previous_funccal(int copyID)
5007{
5008 int abort = FALSE;
5009 funccall_T *fc;
5010
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005011 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005012 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005013 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005014 abort = abort
5015 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
5016 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005017 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005018 }
5019 return abort;
5020}
5021
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005022 static int
5023set_ref_in_funccal(funccall_T *fc, int copyID)
5024{
5025 int abort = FALSE;
5026
5027 if (fc->fc_copyID != copyID)
5028 {
5029 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005030 abort = abort
5031 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
5032 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005033 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005034 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005035 }
5036 return abort;
5037}
5038
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005039/*
5040 * Set "copyID" in all local vars and arguments in the call stack.
5041 */
5042 int
5043set_ref_in_call_stack(int copyID)
5044{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005045 int abort = FALSE;
5046 funccall_T *fc;
5047 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005048
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005049 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005050 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005051
5052 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005053 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
5054 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005055 abort = abort || set_ref_in_funccal(fc, copyID);
5056
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005057 return abort;
5058}
5059
5060/*
5061 * Set "copyID" in all functions available by name.
5062 */
5063 int
5064set_ref_in_functions(int copyID)
5065{
5066 int todo;
5067 hashitem_T *hi = NULL;
5068 int abort = FALSE;
5069 ufunc_T *fp;
5070
5071 todo = (int)func_hashtab.ht_used;
5072 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005073 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005074 if (!HASHITEM_EMPTY(hi))
5075 {
5076 --todo;
5077 fp = HI2UF(hi);
5078 if (!func_name_refcount(fp->uf_name))
5079 abort = abort || set_ref_in_func(NULL, fp, copyID);
5080 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005081 }
5082 return abort;
5083}
5084
5085/*
5086 * Set "copyID" in all function arguments.
5087 */
5088 int
5089set_ref_in_func_args(int copyID)
5090{
5091 int i;
5092 int abort = FALSE;
5093
5094 for (i = 0; i < funcargs.ga_len; ++i)
5095 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5096 copyID, NULL, NULL);
5097 return abort;
5098}
5099
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005100/*
5101 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005102 * Returns TRUE if setting references failed somehow.
5103 */
5104 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005105set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005106{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005107 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005108 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005109 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005110 char_u fname_buf[FLEN_FIXED + 1];
5111 char_u *tofree = NULL;
5112 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005113 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005114
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005115 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005116 return FALSE;
5117
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005118 if (fp_in == NULL)
5119 {
5120 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005121 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005122 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005123 if (fp != NULL)
5124 {
5125 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005126 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005127 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005128
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005129 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005130 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005131}
5132
Bram Moolenaare38eab22019-12-05 21:50:01 +01005133#endif // FEAT_EVAL