blob: b3c4f900a1c5094b12ea16f8497b50452064b0d5 [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 Moolenaar8a7d6542020-01-26 15:56:19 +010057 * Return a pointer to after the type.
58 * When something is wrong return "arg".
59 */
60 static char_u *
61one_function_arg(char_u *arg, garray_T *newargs, garray_T *argtypes, int skip)
62{
Bram Moolenaar6e949782020-04-13 17:21:00 +020063 char_u *p = arg;
64 char_u *arg_copy = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010065
66 while (ASCII_ISALNUM(*p) || *p == '_')
67 ++p;
68 if (arg == p || isdigit(*arg)
69 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
70 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
71 {
72 if (!skip)
73 semsg(_("E125: Illegal argument: %s"), arg);
74 return arg;
75 }
76 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
77 return arg;
78 if (newargs != NULL)
79 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010080 int c;
81 int i;
82
83 c = *p;
84 *p = NUL;
85 arg_copy = vim_strsave(arg);
86 if (arg_copy == NULL)
87 {
88 *p = c;
89 return arg;
90 }
91
92 // Check for duplicate argument name.
93 for (i = 0; i < newargs->ga_len; ++i)
94 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
95 {
96 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
97 vim_free(arg_copy);
98 return arg;
99 }
100 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
101 newargs->ga_len++;
102
103 *p = c;
104 }
105
106 // get any type from "arg: type"
107 if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
108 {
109 char_u *type = NULL;
110
Bram Moolenaar6e949782020-04-13 17:21:00 +0200111 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
112 {
113 semsg(_("E1059: No white space allowed before colon: %s"),
114 arg_copy == NULL ? arg : arg_copy);
115 p = skipwhite(p);
116 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100117 if (*p == ':')
118 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200119 ++p;
120 if (!VIM_ISWHITE(*p))
121 {
122 semsg(_(e_white_after), ":");
123 return arg;
124 }
125 type = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100126 p = skip_type(type);
127 type = vim_strnsave(type, p - type);
128 }
Bram Moolenaar6e949782020-04-13 17:21:00 +0200129 else if (*skipwhite(p) != '=')
130 {
131 semsg(_("E1077: Missing argument type for %s"),
132 arg_copy == NULL ? arg : arg_copy);
133 return arg;
134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
136 }
137
138 return p;
139}
140
141/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200142 * Get function arguments.
143 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200145get_function_args(
146 char_u **argp,
147 char_u endchar,
148 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149 garray_T *argtypes, // NULL unless using :def
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200150 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200151 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200152 int skip,
153 exarg_T *eap,
154 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200155{
156 int mustend = FALSE;
157 char_u *arg = *argp;
158 char_u *p = arg;
159 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200160 int any_default = FALSE;
161 char_u *expr;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200162 char_u *whitep = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200163
164 if (newargs != NULL)
165 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 if (argtypes != NULL)
167 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200168 if (default_args != NULL)
169 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200170
171 if (varargs != NULL)
172 *varargs = FALSE;
173
174 /*
175 * Isolate the arguments: "arg1, arg2, ...)"
176 */
177 while (*p != endchar)
178 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200179 while (eap != NULL && eap->getline != NULL
180 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200181 {
182 char_u *theline;
183
184 // End of the line, get the next one.
185 theline = eap->getline(':', eap->cookie, 0, TRUE);
186 if (theline == NULL)
187 break;
188 vim_free(*line_to_free);
189 *line_to_free = theline;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200190 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200191 p = skipwhite(theline);
192 }
193
194 if (mustend && *p != endchar)
195 {
196 if (!skip)
197 semsg(_(e_invarg2), *argp);
198 break;
199 }
200 if (*p == endchar)
201 break;
202
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200203 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
204 {
205 if (varargs != NULL)
206 *varargs = TRUE;
207 p += 3;
208 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100209
210 if (argtypes != NULL)
211 {
212 // ...name: list<type>
213 if (!ASCII_ISALPHA(*p))
214 {
215 emsg(_("E1055: Missing name after ..."));
216 break;
217 }
218
219 arg = p;
220 p = one_function_arg(p, newargs, argtypes, skip);
221 if (p == arg)
222 break;
223 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200224 }
225 else
226 {
227 arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 p = one_function_arg(p, newargs, argtypes, skip);
229 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200230 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200231
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200232 if (*skipwhite(p) == '=' && default_args != NULL)
233 {
234 typval_T rettv;
235
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100236 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200237 any_default = TRUE;
238 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200239 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200240 p = skipwhite(p);
241 expr = p;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200242 if (eval1(&p, &rettv, 0) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200243 {
244 if (ga_grow(default_args, 1) == FAIL)
245 goto err_ret;
246
247 // trim trailing whitespace
248 while (p > expr && VIM_ISWHITE(p[-1]))
249 p--;
250 c = *p;
251 *p = NUL;
252 expr = vim_strsave(expr);
253 if (expr == NULL)
254 {
255 *p = c;
256 goto err_ret;
257 }
258 ((char_u **)(default_args->ga_data))
259 [default_args->ga_len] = expr;
260 default_args->ga_len++;
261 *p = c;
262 }
263 else
264 mustend = TRUE;
265 }
266 else if (any_default)
267 {
268 emsg(_("E989: Non-default argument follows default argument"));
269 mustend = TRUE;
270 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200271 if (*p == ',')
272 ++p;
273 else
274 mustend = TRUE;
275 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200276 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200277 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200278 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200279
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200280 if (*p != endchar)
281 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100282 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200283
284 *argp = p;
285 return OK;
286
287err_ret:
288 if (newargs != NULL)
289 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200290 if (default_args != NULL)
291 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200292 return FAIL;
293}
294
295/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200296 * Register function "fp" as using "current_funccal" as its scope.
297 */
298 static int
299register_closure(ufunc_T *fp)
300{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200301 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100302 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200303 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200304 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200305 fp->uf_scoped = current_funccal;
306 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200307
Bram Moolenaar58016442016-07-31 18:30:22 +0200308 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
309 return FAIL;
310 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
311 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200312 return OK;
313}
314
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100315 static void
316set_ufunc_name(ufunc_T *fp, char_u *name)
317{
318 STRCPY(fp->uf_name, name);
319
320 if (name[0] == K_SPECIAL)
321 {
322 fp->uf_name_exp = alloc(STRLEN(name) + 3);
323 if (fp->uf_name_exp != NULL)
324 {
325 STRCPY(fp->uf_name_exp, "<SNR>");
326 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
327 }
328 }
329}
330
Bram Moolenaar58016442016-07-31 18:30:22 +0200331/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200332 * Get a name for a lambda. Returned in static memory.
333 */
334 char_u *
335get_lambda_name(void)
336{
337 static char_u name[30];
338 static int lambda_no = 0;
339
340 sprintf((char*)name, "<lambda>%d", ++lambda_no);
341 return name;
342}
343
344/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200345 * Parse a lambda expression and get a Funcref from "*arg".
346 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
347 */
348 int
349get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
350{
351 garray_T newargs;
352 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200353 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200354 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100355 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200356 int varargs;
357 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200358 char_u *start = skipwhite(*arg + 1);
359 char_u *s, *e;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200360 int *old_eval_lavars = eval_lavars_used;
361 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200362
363 ga_init(&newargs);
364 ga_init(&newlines);
365
Bram Moolenaare38eab22019-12-05 21:50:01 +0100366 // First, check if this is a lambda expression. "->" must exist.
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200367 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE,
368 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200369 if (ret == FAIL || *start != '>')
370 return NOTDONE;
371
Bram Moolenaare38eab22019-12-05 21:50:01 +0100372 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200373 if (evaluate)
374 pnewargs = &newargs;
375 else
376 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200377 *arg = skipwhite(*arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378 // TODO: argument types
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200379 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE,
380 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200381 if (ret == FAIL || **arg != '>')
382 goto errret;
383
Bram Moolenaare38eab22019-12-05 21:50:01 +0100384 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200385 if (evaluate)
386 eval_lavars_used = &eval_lavars;
387
Bram Moolenaare38eab22019-12-05 21:50:01 +0100388 // Get the start and the end of the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200389 *arg = skipwhite(*arg + 1);
390 s = *arg;
391 ret = skip_expr(arg);
392 if (ret == FAIL)
393 goto errret;
394 e = *arg;
395 *arg = skipwhite(*arg);
396 if (**arg != '}')
Bram Moolenaaree619e52020-03-28 21:38:06 +0100397 {
398 semsg(_("E451: Expected }: %s"), *arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200399 goto errret;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100400 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200401 ++*arg;
402
403 if (evaluate)
404 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200405 int len, flags = 0;
406 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200407 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200408
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200409 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200410 if (fp == NULL)
411 goto errret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100412 fp->uf_dfunc_idx = -1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200413 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200414 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200415 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200416
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200417 ga_init2(&newlines, (int)sizeof(char_u *), 1);
418 if (ga_grow(&newlines, 1) == FAIL)
419 goto errret;
420
Bram Moolenaare38eab22019-12-05 21:50:01 +0100421 // Add "return " before the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200422 len = 7 + e - s + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200423 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200424 if (p == NULL)
425 goto errret;
426 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
427 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200428 vim_strncpy(p + 7, s, e - s);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200429 if (strstr((char *)p + 7, "a:") == NULL)
430 // No a: variables are used for sure.
431 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200432
433 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100434 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200435 hash_add(&func_hashtab, UF2HIKEY(fp));
436 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200437 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200438 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200439 if (current_funccal != NULL && eval_lavars)
440 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200441 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200442 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200443 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200444 }
445 else
446 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200447
448#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200449 if (prof_def_func())
450 func_do_profile(fp);
451#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200452 if (sandbox)
453 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200455 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200456 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200457 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200458 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100459 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200460
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200461 pt->pt_func = fp;
462 pt->pt_refcount = 1;
463 rettv->vval.v_partial = pt;
464 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200465 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200466
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200467 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200468 return OK;
469
470errret:
471 ga_clear_strings(&newargs);
472 ga_clear_strings(&newlines);
473 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100474 vim_free(pt);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200475 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200476 return FAIL;
477}
478
479/*
480 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
481 * name it contains, otherwise return "name".
482 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
483 * "partialp".
484 */
485 char_u *
486deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
487{
488 dictitem_T *v;
489 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200490 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200491
492 if (partialp != NULL)
493 *partialp = NULL;
494
495 cc = name[*lenp];
496 name[*lenp] = NUL;
497 v = find_var(name, NULL, no_autoload);
498 name[*lenp] = cc;
499 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
500 {
501 if (v->di_tv.vval.v_string == NULL)
502 {
503 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100504 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200505 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200506 s = v->di_tv.vval.v_string;
507 *lenp = (int)STRLEN(s);
508 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200509 }
510
511 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
512 {
513 partial_T *pt = v->di_tv.vval.v_partial;
514
515 if (pt == NULL)
516 {
517 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100518 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200519 }
520 if (partialp != NULL)
521 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200522 s = partial_name(pt);
523 *lenp = (int)STRLEN(s);
524 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200525 }
526
527 return name;
528}
529
530/*
531 * Give an error message with a function name. Handle <SNR> things.
532 * "ermsg" is to be passed without translation, use N_() instead of _().
533 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100534 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200535emsg_funcname(char *ermsg, char_u *name)
536{
537 char_u *p;
538
539 if (*name == K_SPECIAL)
540 p = concat_str((char_u *)"<SNR>", name + 3);
541 else
542 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100543 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200544 if (p != name)
545 vim_free(p);
546}
547
548/*
549 * Allocate a variable for the result of a function.
550 * Return OK or FAIL.
551 */
552 int
553get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200554 char_u *name, // name of the function
555 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200556 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200557 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200558 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200559{
560 char_u *argp;
561 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100562 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
563 int argcount = 0; // number of arguments found
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200564
565 /*
566 * Get the arguments.
567 */
568 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200569 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
570 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200571 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100572 argp = skipwhite(argp + 1); // skip the '(' or ','
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200573 if (*argp == ')' || *argp == ',' || *argp == NUL)
574 break;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200575 if (eval1(&argp, &argvars[argcount],
576 funcexe->evaluate ? EVAL_EVALUATE : 0) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200577 {
578 ret = FAIL;
579 break;
580 }
581 ++argcount;
582 if (*argp != ',')
583 break;
584 }
585 if (*argp == ')')
586 ++argp;
587 else
588 ret = FAIL;
589
590 if (ret == OK)
591 {
592 int i = 0;
593
594 if (get_vim_var_nr(VV_TESTING))
595 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100596 // Prepare for calling test_garbagecollect_now(), need to know
597 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200598 if (funcargs.ga_itemsize == 0)
599 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
600 for (i = 0; i < argcount; ++i)
601 if (ga_grow(&funcargs, 1) == OK)
602 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
603 &argvars[i];
604 }
605
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200606 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200607
608 funcargs.ga_len -= i;
609 }
610 else if (!aborting())
611 {
612 if (argcount == MAX_FUNC_ARGS)
613 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
614 else
615 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
616 }
617
618 while (--argcount >= 0)
619 clear_tv(&argvars[argcount]);
620
621 *arg = skipwhite(argp);
622 return ret;
623}
624
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200625/*
626 * Return TRUE if "p" starts with "<SID>" or "s:".
627 * Only works if eval_fname_script() returned non-zero for "p"!
628 */
629 static int
630eval_fname_sid(char_u *p)
631{
632 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
633}
634
635/*
636 * In a script change <SID>name() and s:name() to K_SNR 123_name().
637 * Change <SNR>123_name() to K_SNR 123_name().
638 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
639 * (slow).
640 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100641 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200642fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
643{
644 int llen;
645 char_u *fname;
646 int i;
647
648 llen = eval_fname_script(name);
649 if (llen > 0)
650 {
651 fname_buf[0] = K_SPECIAL;
652 fname_buf[1] = KS_EXTRA;
653 fname_buf[2] = (int)KE_SNR;
654 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100655 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200656 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200657 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100658 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200659 else
660 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200661 sprintf((char *)fname_buf + 3, "%ld_",
662 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200663 i = (int)STRLEN(fname_buf);
664 }
665 }
666 if (i + STRLEN(name + llen) < FLEN_FIXED)
667 {
668 STRCPY(fname_buf + i, name + llen);
669 fname = fname_buf;
670 }
671 else
672 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200673 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200674 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100675 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200676 else
677 {
678 *tofree = fname;
679 mch_memmove(fname, fname_buf, (size_t)i);
680 STRCPY(fname + i, name + llen);
681 }
682 }
683 }
684 else
685 fname = name;
686 return fname;
687}
688
689/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100690 * Find a function "name" in script "sid".
691 */
692 static ufunc_T *
693find_func_with_sid(char_u *name, int sid)
694{
695 hashitem_T *hi;
696 char_u buffer[200];
697
698 buffer[0] = K_SPECIAL;
699 buffer[1] = KS_EXTRA;
700 buffer[2] = (int)KE_SNR;
701 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
702 (long)sid, name);
703 hi = hash_find(&func_hashtab, buffer);
704 if (!HASHITEM_EMPTY(hi))
705 return HI2UF(hi);
706
707 return NULL;
708}
709
710/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200711 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200712 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200713 * Return NULL for unknown function.
714 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 static ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200716find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200717{
718 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719 ufunc_T *func;
720 imported_T *imported;
721
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200722 if (in_vim9script() && !is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723 {
724 // Find script-local function before global one.
725 func = find_func_with_sid(name, current_sctx.sc_sid);
726 if (func != NULL)
727 return func;
728
729 // Find imported funcion before global one.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +0100730 imported = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100731 if (imported != NULL && imported->imp_funcname != NULL)
732 {
733 hi = hash_find(&func_hashtab, imported->imp_funcname);
734 if (!HASHITEM_EMPTY(hi))
735 return HI2UF(hi);
736 }
737 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200738
Bram Moolenaara26b9702020-04-18 19:53:28 +0200739 hi = hash_find(&func_hashtab,
740 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200741 if (!HASHITEM_EMPTY(hi))
742 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743
744 return NULL;
745}
746
747/*
748 * Find a function by name, return pointer to it in ufuncs.
749 * "cctx" is passed in a :def function to find imported functions.
750 * Return NULL for unknown or dead function.
751 */
752 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200753find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100754{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200755 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756
757 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
758 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200759 return NULL;
760}
761
762/*
763 * Copy the function name of "fp" to buffer "buf".
764 * "buf" must be able to hold the function name plus three bytes.
765 * Takes care of script-local function names.
766 */
767 static void
768cat_func_name(char_u *buf, ufunc_T *fp)
769{
770 if (fp->uf_name[0] == K_SPECIAL)
771 {
772 STRCPY(buf, "<SNR>");
773 STRCAT(buf, fp->uf_name + 3);
774 }
775 else
776 STRCPY(buf, fp->uf_name);
777}
778
779/*
780 * Add a number variable "name" to dict "dp" with value "nr".
781 */
782 static void
783add_nr_var(
784 dict_T *dp,
785 dictitem_T *v,
786 char *name,
787 varnumber_T nr)
788{
789 STRCPY(v->di_key, name);
790 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
791 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
792 v->di_tv.v_type = VAR_NUMBER;
793 v->di_tv.v_lock = VAR_FIXED;
794 v->di_tv.vval.v_number = nr;
795}
796
797/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100798 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200799 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100800 static void
801free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200802{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100803 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200804
805 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
806 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100807 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200808
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100809 // When garbage collecting a funccall_T may be freed before the
810 // function that references it, clear its uf_scoped field.
811 // The function may have been redefined and point to another
812 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200813 if (fp != NULL && fp->uf_scoped == fc)
814 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200815 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200816 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200817
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200818 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200819 vim_free(fc);
820}
821
822/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100823 * Free "fc" and what it contains.
824 * Can be called only when "fc" is kept beyond the period of it called,
825 * i.e. after cleanup_function_call(fc).
826 */
827 static void
828free_funccal_contents(funccall_T *fc)
829{
830 listitem_T *li;
831
832 // Free all l: variables.
833 vars_clear(&fc->l_vars.dv_hashtab);
834
835 // Free all a: variables.
836 vars_clear(&fc->l_avars.dv_hashtab);
837
838 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200839 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100840 clear_tv(&li->li_tv);
841
842 free_funccal(fc);
843}
844
845/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200846 * Handle the last part of returning from a function: free the local hashtable.
847 * Unless it is still in use by a closure.
848 */
849 static void
850cleanup_function_call(funccall_T *fc)
851{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100852 int may_free_fc = fc->fc_refcount <= 0;
853 int free_fc = TRUE;
854
Bram Moolenaar6914c642017-04-01 21:21:30 +0200855 current_funccal = fc->caller;
856
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100857 // Free all l: variables if not referred.
858 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
859 vars_clear(&fc->l_vars.dv_hashtab);
860 else
861 free_fc = FALSE;
862
863 // If the a:000 list and the l: and a: dicts are not referenced and
864 // there is no closure using it, we can free the funccall_T and what's
865 // in it.
866 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
867 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200868 else
869 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100870 int todo;
871 hashitem_T *hi;
872 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200873
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100874 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200875
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100876 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200877 todo = (int)fc->l_avars.dv_hashtab.ht_used;
878 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
879 {
880 if (!HASHITEM_EMPTY(hi))
881 {
882 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100883 di = HI2DI(hi);
884 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200885 }
886 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100887 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200888
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100889 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
890 fc->l_varlist.lv_first = NULL;
891 else
892 {
893 listitem_T *li;
894
895 free_fc = FALSE;
896
897 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200898 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +0200899 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100900 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100901
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100902 if (free_fc)
903 free_funccal(fc);
904 else
905 {
906 static int made_copy = 0;
907
908 // "fc" is still in use. This can happen when returning "a:000",
909 // assigning "l:" to a global variable or defining a closure.
910 // Link "fc" in the list for garbage collection later.
911 fc->caller = previous_funccal;
912 previous_funccal = fc;
913
914 if (want_garbage_collect)
915 // If garbage collector is ready, clear count.
916 made_copy = 0;
917 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100918 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100919 // We have made a lot of copies, worth 4 Mbyte. This can happen
920 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100921 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100922 // too much memory.
923 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100924 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100925 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200926 }
927}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928/*
929 * Unreference "fc": decrement the reference count and free it when it
930 * becomes zero. "fp" is detached from "fc".
931 * When "force" is TRUE we are exiting.
932 */
933 static void
934funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
935{
936 funccall_T **pfc;
937 int i;
938
939 if (fc == NULL)
940 return;
941
942 if (--fc->fc_refcount <= 0 && (force || (
943 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
944 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
945 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
946 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
947 {
948 if (fc == *pfc)
949 {
950 *pfc = fc->caller;
951 free_funccal_contents(fc);
952 return;
953 }
954 }
955 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
956 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
957 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
958}
959
960/*
961 * Remove the function from the function hashtable. If the function was
962 * deleted while it still has references this was already done.
963 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
964 */
965 static int
966func_remove(ufunc_T *fp)
967{
968 hashitem_T *hi;
969
970 // Return if it was already virtually deleted.
971 if (fp->uf_flags & FC_DEAD)
972 return FALSE;
973
974 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
975 if (!HASHITEM_EMPTY(hi))
976 {
977 // When there is a def-function index do not actually remove the
978 // function, so we can find the index when defining the function again.
979 if (fp->uf_dfunc_idx >= 0)
980 fp->uf_flags |= FC_DEAD;
981 else
982 hash_remove(&func_hashtab, hi);
983 return TRUE;
984 }
985 return FALSE;
986}
987
988 static void
989func_clear_items(ufunc_T *fp)
990{
991 ga_clear_strings(&(fp->uf_args));
992 ga_clear_strings(&(fp->uf_def_args));
993 ga_clear_strings(&(fp->uf_lines));
994 VIM_CLEAR(fp->uf_name_exp);
995 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100996 VIM_CLEAR(fp->uf_def_arg_idx);
997 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200998 while (fp->uf_type_list.ga_len > 0)
999 vim_free(((type_T **)fp->uf_type_list.ga_data)
1000 [--fp->uf_type_list.ga_len]);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 ga_clear(&fp->uf_type_list);
1002#ifdef FEAT_PROFILE
1003 VIM_CLEAR(fp->uf_tml_count);
1004 VIM_CLEAR(fp->uf_tml_total);
1005 VIM_CLEAR(fp->uf_tml_self);
1006#endif
1007}
1008
1009/*
1010 * Free all things that a function contains. Does not free the function
1011 * itself, use func_free() for that.
1012 * When "force" is TRUE we are exiting.
1013 */
1014 static void
1015func_clear(ufunc_T *fp, int force)
1016{
1017 if (fp->uf_cleared)
1018 return;
1019 fp->uf_cleared = TRUE;
1020
1021 // clear this function
1022 func_clear_items(fp);
1023 funccal_unref(fp->uf_scoped, fp, force);
1024 delete_def_function(fp);
1025}
1026
1027/*
1028 * Free a function and remove it from the list of functions. Does not free
1029 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001030 * When "force" is TRUE we are exiting.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031 */
1032 static void
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001033func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001034{
1035 // Only remove it when not done already, otherwise we would remove a newer
1036 // version of the function with the same name.
1037 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1038 func_remove(fp);
1039
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001040 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041 vim_free(fp);
1042}
1043
1044/*
1045 * Free all things that a function contains and free the function itself.
1046 * When "force" is TRUE we are exiting.
1047 */
1048 static void
1049func_clear_free(ufunc_T *fp, int force)
1050{
1051 func_clear(fp, force);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001052 func_free(fp, force);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053}
1054
Bram Moolenaar6914c642017-04-01 21:21:30 +02001055
1056/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001057 * Call a user function.
1058 */
1059 static void
1060call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001061 ufunc_T *fp, // pointer to function
1062 int argcount, // nr of args
1063 typval_T *argvars, // arguments
1064 typval_T *rettv, // return value
1065 linenr_T firstline, // first line of range
1066 linenr_T lastline, // last line of range
1067 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001068{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001069 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001070 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001071 funccall_T *fc;
1072 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001073 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001074 static int depth = 0;
1075 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001076 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001077 int i;
1078 int ai;
1079 int islambda = FALSE;
1080 char_u numbuf[NUMBUFLEN];
1081 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001082#ifdef FEAT_PROFILE
1083 proftime_T wait_start;
1084 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001085 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001086#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001087 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001088
Bram Moolenaare38eab22019-12-05 21:50:01 +01001089 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001090 if (depth >= p_mfd)
1091 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001092 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001093 rettv->v_type = VAR_NUMBER;
1094 rettv->vval.v_number = -1;
1095 return;
1096 }
1097 ++depth;
1098
Bram Moolenaare38eab22019-12-05 21:50:01 +01001099 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001100
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001101 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001102 if (fc == NULL)
1103 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001104 fc->caller = current_funccal;
1105 current_funccal = fc;
1106 fc->func = fp;
1107 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001108 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001109 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001110 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1111 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001112 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001113 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001114 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 if (fp->uf_dfunc_idx >= 0)
1117 {
1118 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001119 save_current_sctx = current_sctx;
1120 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121
1122 // Execute the compiled function.
1123 call_def_function(fp, argcount, argvars, rettv);
1124 --depth;
1125 current_funccal = fc->caller;
1126
1127 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001128 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 free_funccal(fc);
1130 return;
1131 }
1132
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001133 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1134 islambda = TRUE;
1135
1136 /*
1137 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1138 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1139 * each argument variable and saves a lot of time.
1140 */
1141 /*
1142 * Init l: variables.
1143 */
1144 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1145 if (selfdict != NULL)
1146 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001147 // Set l:self to "selfdict". Use "name" to avoid a warning from
1148 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001149 v = &fc->fixvar[fixvar_idx++].var;
1150 name = v->di_key;
1151 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001152 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001153 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1154 v->di_tv.v_type = VAR_DICT;
1155 v->di_tv.v_lock = 0;
1156 v->di_tv.vval.v_dict = selfdict;
1157 ++selfdict->dv_refcount;
1158 }
1159
1160 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001161 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001162 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001163 * Set a:000 to a list with room for the "..." arguments.
1164 */
1165 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001166 if ((fp->uf_flags & FC_NOARGS) == 0)
1167 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001168 (varnumber_T)(argcount >= fp->uf_args.ga_len
1169 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001170 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001171 if ((fp->uf_flags & FC_NOARGS) == 0)
1172 {
1173 // Use "name" to avoid a warning from some compiler that checks the
1174 // destination size.
1175 v = &fc->fixvar[fixvar_idx++].var;
1176 name = v->di_key;
1177 STRCPY(name, "000");
1178 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1179 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1180 v->di_tv.v_type = VAR_LIST;
1181 v->di_tv.v_lock = VAR_FIXED;
1182 v->di_tv.vval.v_list = &fc->l_varlist;
1183 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001184 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001185 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1186 fc->l_varlist.lv_lock = VAR_FIXED;
1187
1188 /*
1189 * Set a:firstline to "firstline" and a:lastline to "lastline".
1190 * Set a:name to named arguments.
1191 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001192 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001193 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001194 if ((fp->uf_flags & FC_NOARGS) == 0)
1195 {
1196 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001197 (varnumber_T)firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001198 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001199 (varnumber_T)lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001200 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001201 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001202 {
1203 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001204 typval_T def_rettv;
1205 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001206
1207 ai = i - fp->uf_args.ga_len;
1208 if (ai < 0)
1209 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001210 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001211 name = FUNCARG(fp, i);
1212 if (islambda)
1213 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001214
1215 // evaluate named argument default expression
1216 isdefault = ai + fp->uf_def_args.ga_len >= 0
1217 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1218 && argvars[i].vval.v_number == VVAL_NONE));
1219 if (isdefault)
1220 {
1221 char_u *default_expr = NULL;
1222 def_rettv.v_type = VAR_NUMBER;
1223 def_rettv.vval.v_number = -1;
1224
1225 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1226 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar32e35112020-05-14 22:41:15 +02001227 if (eval1(&default_expr, &def_rettv, EVAL_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001228 {
1229 default_arg_err = 1;
1230 break;
1231 }
1232 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001233 }
1234 else
1235 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001236 if ((fp->uf_flags & FC_NOARGS) != 0)
1237 // Bail out if no a: arguments used (in lambda).
1238 break;
1239
Bram Moolenaare38eab22019-12-05 21:50:01 +01001240 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001241 sprintf((char *)numbuf, "%d", ai + 1);
1242 name = numbuf;
1243 }
1244 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1245 {
1246 v = &fc->fixvar[fixvar_idx++].var;
1247 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001248 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001249 }
1250 else
1251 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001252 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001253 if (v == NULL)
1254 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001255 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001256 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001257
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001258 // Note: the values are copied directly to avoid alloc/free.
1259 // "argvars" must have VAR_FIXED for v_lock.
1260 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001261 v->di_tv.v_lock = VAR_FIXED;
1262
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001263 if (addlocal)
1264 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001265 // Named arguments should be accessed without the "a:" prefix in
1266 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001267 copy_tv(&v->di_tv, &v->di_tv);
1268 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001269 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001270 else
1271 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001272
1273 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1274 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001275 listitem_T *li = &fc->l_listitems[ai];
1276
1277 li->li_tv = argvars[i];
1278 li->li_tv.v_lock = VAR_FIXED;
1279 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001280 }
1281 }
1282
Bram Moolenaare38eab22019-12-05 21:50:01 +01001283 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001284 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001285
1286 if (fp->uf_flags & FC_SANDBOX)
1287 {
1288 using_sandbox = TRUE;
1289 ++sandbox;
1290 }
1291
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001292 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001293 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001294 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001295 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001296 ++no_wait_return;
1297 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001298
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001299 smsg(_("calling %s"), SOURCING_NAME);
1300 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001301 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001302 char_u buf[MSG_BUF_LEN];
1303 char_u numbuf2[NUMBUFLEN];
1304 char_u *tofree;
1305 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001306
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001307 msg_puts("(");
1308 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001309 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001310 if (i > 0)
1311 msg_puts(", ");
1312 if (argvars[i].v_type == VAR_NUMBER)
1313 msg_outnum((long)argvars[i].vval.v_number);
1314 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001315 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001316 // Do not want errors such as E724 here.
1317 ++emsg_off;
1318 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1319 --emsg_off;
1320 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001321 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001322 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001323 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001324 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1325 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001326 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001327 msg_puts((char *)s);
1328 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001329 }
1330 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001331 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001332 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001333 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001334 msg_puts("\n"); // don't overwrite this either
1335
1336 verbose_leave_scroll();
1337 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001338 }
1339#ifdef FEAT_PROFILE
1340 if (do_profiling == PROF_YES)
1341 {
1342 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001343 {
1344 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001345 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001346 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001347 if (fp->uf_profiling
1348 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1349 {
1350 ++fp->uf_tm_count;
1351 profile_start(&call_start);
1352 profile_zero(&fp->uf_tm_children);
1353 }
1354 script_prof_save(&wait_start);
1355 }
1356#endif
1357
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001358 save_current_sctx = current_sctx;
1359 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001360 save_did_emsg = did_emsg;
1361 did_emsg = FALSE;
1362
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001363 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1364 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001365 else if (islambda)
1366 {
1367 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1368
1369 // A Lambda always has the command "return {expr}". It is much faster
1370 // to evaluate {expr} directly.
1371 ++ex_nesting_level;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001372 (void)eval1(&p, rettv, EVAL_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001373 --ex_nesting_level;
1374 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001375 else
1376 // call do_cmdline() to execute the lines
1377 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001378 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1379
1380 --RedrawingDisabled;
1381
Bram Moolenaare38eab22019-12-05 21:50:01 +01001382 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001383 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1384 {
1385 clear_tv(rettv);
1386 rettv->v_type = VAR_NUMBER;
1387 rettv->vval.v_number = -1;
1388 }
1389
1390#ifdef FEAT_PROFILE
1391 if (do_profiling == PROF_YES && (fp->uf_profiling
1392 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1393 {
1394 profile_end(&call_start);
1395 profile_sub_wait(&wait_start, &call_start);
1396 profile_add(&fp->uf_tm_total, &call_start);
1397 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1398 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1399 {
1400 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1401 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1402 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001403 if (started_profiling)
1404 // make a ":profdel func" stop profiling the function
1405 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001406 }
1407#endif
1408
Bram Moolenaare38eab22019-12-05 21:50:01 +01001409 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001410 if (p_verbose >= 12)
1411 {
1412 ++no_wait_return;
1413 verbose_enter_scroll();
1414
1415 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001416 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001417 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001418 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001419 (long)fc->rettv->vval.v_number);
1420 else
1421 {
1422 char_u buf[MSG_BUF_LEN];
1423 char_u numbuf2[NUMBUFLEN];
1424 char_u *tofree;
1425 char_u *s;
1426
Bram Moolenaare38eab22019-12-05 21:50:01 +01001427 // The value may be very long. Skip the middle part, so that we
1428 // have some idea how it starts and ends. smsg() would always
1429 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001430 ++emsg_off;
1431 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1432 --emsg_off;
1433 if (s != NULL)
1434 {
1435 if (vim_strsize(s) > MSG_BUF_CLEN)
1436 {
1437 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1438 s = buf;
1439 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001440 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001441 vim_free(tofree);
1442 }
1443 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001444 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001445
1446 verbose_leave_scroll();
1447 --no_wait_return;
1448 }
1449
Bram Moolenaare31ee862020-01-07 20:59:34 +01001450 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001451 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001452 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001453#ifdef FEAT_PROFILE
1454 if (do_profiling == PROF_YES)
1455 script_prof_restore(&wait_start);
1456#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001457 if (using_sandbox)
1458 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001459
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001460 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001461 {
1462 ++no_wait_return;
1463 verbose_enter_scroll();
1464
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001465 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001466 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001467
1468 verbose_leave_scroll();
1469 --no_wait_return;
1470 }
1471
1472 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001473 --depth;
1474
Bram Moolenaar6914c642017-04-01 21:21:30 +02001475 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001476}
1477
1478/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001480 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481 int
1482call_user_func_check(
1483 ufunc_T *fp,
1484 int argcount,
1485 typval_T *argvars,
1486 typval_T *rettv,
1487 funcexe_T *funcexe,
1488 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001489{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001490 int error;
1491 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1494 *funcexe->doesrange = TRUE;
1495 if (argcount < regular_args - fp->uf_def_args.ga_len)
1496 error = FCERR_TOOFEW;
1497 else if (!has_varargs(fp) && argcount > regular_args)
1498 error = FCERR_TOOMANY;
1499 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1500 error = FCERR_DICT;
1501 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001502 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 int did_save_redo = FALSE;
1504 save_redo_T save_redo;
1505
1506 /*
1507 * Call the user function.
1508 * Save and restore search patterns, script variables and
1509 * redo buffer.
1510 */
1511 save_search_patterns();
1512 if (!ins_compl_active())
1513 {
1514 saveRedobuff(&save_redo);
1515 did_save_redo = TRUE;
1516 }
1517 ++fp->uf_calls;
1518 call_user_func(fp, argcount, argvars, rettv,
1519 funcexe->firstline, funcexe->lastline,
1520 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
1521 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1522 // Function was unreferenced while being used, free it now.
1523 func_clear_free(fp, FALSE);
1524 if (did_save_redo)
1525 restoreRedobuff(&save_redo);
1526 restore_search_patterns();
1527 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001528 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001529 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001530}
1531
1532/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001533 * There are two kinds of function names:
1534 * 1. ordinary names, function defined with :function
1535 * 2. numbered functions and lambdas
1536 * For the first we only count the name stored in func_hashtab as a reference,
1537 * using function() does not count as a reference, because the function is
1538 * looked up by name.
1539 */
1540 static int
1541func_name_refcount(char_u *name)
1542{
1543 return isdigit(*name) || *name == '<';
1544}
1545
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001546static funccal_entry_T *funccal_stack = NULL;
1547
1548/*
1549 * Save the current function call pointer, and set it to NULL.
1550 * Used when executing autocommands and for ":source".
1551 */
1552 void
1553save_funccal(funccal_entry_T *entry)
1554{
1555 entry->top_funccal = current_funccal;
1556 entry->next = funccal_stack;
1557 funccal_stack = entry;
1558 current_funccal = NULL;
1559}
1560
1561 void
1562restore_funccal(void)
1563{
1564 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001565 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001566 else
1567 {
1568 current_funccal = funccal_stack->top_funccal;
1569 funccal_stack = funccal_stack->next;
1570 }
1571}
1572
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001573 funccall_T *
1574get_current_funccal(void)
1575{
1576 return current_funccal;
1577}
1578
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001579/*
1580 * Mark all functions of script "sid" as deleted.
1581 */
1582 void
1583delete_script_functions(int sid)
1584{
1585 hashitem_T *hi;
1586 ufunc_T *fp;
1587 long_u todo;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02001588 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001589 size_t len;
1590
1591 buf[0] = K_SPECIAL;
1592 buf[1] = KS_EXTRA;
1593 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02001594 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001595 len = STRLEN(buf);
1596
1597 todo = func_hashtab.ht_used;
1598 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1599 if (!HASHITEM_EMPTY(hi))
1600 {
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001601 fp = HI2UF(hi);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001602 if (STRNCMP(fp->uf_name, buf, len) == 0)
1603 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001604 fp->uf_flags |= FC_DEAD;
1605 func_clear(fp, TRUE);
1606 }
1607 --todo;
1608 }
1609}
1610
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001611#if defined(EXITFREE) || defined(PROTO)
1612 void
1613free_all_functions(void)
1614{
1615 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001616 ufunc_T *fp;
1617 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001618 long_u todo = 1;
1619 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001620
Bram Moolenaare38eab22019-12-05 21:50:01 +01001621 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001622 while (current_funccal != NULL)
1623 {
1624 clear_tv(current_funccal->rettv);
1625 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001626 if (current_funccal == NULL && funccal_stack != NULL)
1627 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001628 }
1629
Bram Moolenaare38eab22019-12-05 21:50:01 +01001630 // First clear what the functions contain. Since this may lower the
1631 // reference count of a function, it may also free a function and change
1632 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001633 while (todo > 0)
1634 {
1635 todo = func_hashtab.ht_used;
1636 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1637 if (!HASHITEM_EMPTY(hi))
1638 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 // clear the def function index now
1640 fp = HI2UF(hi);
1641 fp->uf_flags &= ~FC_DEAD;
1642 fp->uf_dfunc_idx = -1;
1643
Bram Moolenaare38eab22019-12-05 21:50:01 +01001644 // Only free functions that are not refcounted, those are
1645 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001646 if (func_name_refcount(fp->uf_name))
1647 ++skipped;
1648 else
1649 {
1650 used = func_hashtab.ht_used;
1651 func_clear(fp, TRUE);
1652 if (used != func_hashtab.ht_used)
1653 {
1654 skipped = 0;
1655 break;
1656 }
1657 }
1658 --todo;
1659 }
1660 }
1661
Bram Moolenaare38eab22019-12-05 21:50:01 +01001662 // Now actually free the functions. Need to start all over every time,
1663 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001664 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001665 while (func_hashtab.ht_used > skipped)
1666 {
1667 todo = func_hashtab.ht_used;
1668 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001669 if (!HASHITEM_EMPTY(hi))
1670 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001671 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001672 // Only free functions that are not refcounted, those are
1673 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001674 fp = HI2UF(hi);
1675 if (func_name_refcount(fp->uf_name))
1676 ++skipped;
1677 else
1678 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001679 func_free(fp, FALSE);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001680 skipped = 0;
1681 break;
1682 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001683 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001684 }
1685 if (skipped == 0)
1686 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687
1688 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001689}
1690#endif
1691
1692/*
1693 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02001694 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001695 * "len" is the length of "name", or -1 for NUL terminated.
1696 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001698builtin_function(char_u *name, int len)
1699{
1700 char_u *p;
1701
Bram Moolenaara26b9702020-04-18 19:53:28 +02001702 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001703 return FALSE;
1704 p = vim_strchr(name, AUTOLOAD_CHAR);
1705 return p == NULL || (len > 0 && p > name + len);
1706}
1707
1708 int
1709func_call(
1710 char_u *name,
1711 typval_T *args,
1712 partial_T *partial,
1713 dict_T *selfdict,
1714 typval_T *rettv)
1715{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001716 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001717 listitem_T *item;
1718 typval_T argv[MAX_FUNC_ARGS + 1];
1719 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001720 int r = 0;
1721
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001722 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001723 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001724 {
1725 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1726 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001727 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001728 break;
1729 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001730 // Make a copy of each argument. This is needed to be able to set
1731 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001732 copy_tv(&item->li_tv, &argv[argc++]);
1733 }
1734
1735 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001736 {
1737 funcexe_T funcexe;
1738
Bram Moolenaara80faa82020-04-12 19:37:17 +02001739 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001740 funcexe.firstline = curwin->w_cursor.lnum;
1741 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001742 funcexe.evaluate = TRUE;
1743 funcexe.partial = partial;
1744 funcexe.selfdict = selfdict;
1745 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1746 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001747
Bram Moolenaare38eab22019-12-05 21:50:01 +01001748 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001749 while (argc > 0)
1750 clear_tv(&argv[--argc]);
1751
1752 return r;
1753}
1754
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001755static int callback_depth = 0;
1756
1757 int
1758get_callback_depth(void)
1759{
1760 return callback_depth;
1761}
1762
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001763/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001764 * Invoke call_func() with a callback.
1765 */
1766 int
1767call_callback(
1768 callback_T *callback,
1769 int len, // length of "name" or -1 to use strlen()
1770 typval_T *rettv, // return value goes here
1771 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001772 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001773 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001774{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001775 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001776 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001777
Bram Moolenaara80faa82020-04-12 19:37:17 +02001778 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001779 funcexe.evaluate = TRUE;
1780 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001781 ++callback_depth;
1782 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1783 --callback_depth;
1784 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001785}
1786
1787/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 * Give an error message for the result of a function.
1789 * Nothing if "error" is FCERR_NONE.
1790 */
1791 void
1792user_func_error(int error, char_u *name)
1793{
1794 switch (error)
1795 {
1796 case FCERR_UNKNOWN:
1797 emsg_funcname(e_unknownfunc, name);
1798 break;
1799 case FCERR_NOTMETHOD:
1800 emsg_funcname(
1801 N_("E276: Cannot use function as a method: %s"), name);
1802 break;
1803 case FCERR_DELETED:
1804 emsg_funcname(N_(e_func_deleted), name);
1805 break;
1806 case FCERR_TOOMANY:
1807 emsg_funcname((char *)e_toomanyarg, name);
1808 break;
1809 case FCERR_TOOFEW:
1810 emsg_funcname((char *)e_toofewarg, name);
1811 break;
1812 case FCERR_SCRIPT:
1813 emsg_funcname(
1814 N_("E120: Using <SID> not in a script context: %s"), name);
1815 break;
1816 case FCERR_DICT:
1817 emsg_funcname(
1818 N_("E725: Calling dict function without Dictionary: %s"),
1819 name);
1820 break;
1821 }
1822}
1823
1824/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001825 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001826 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001827 * Return FAIL when the function can't be called, OK otherwise.
1828 * Also returns OK when an error was encountered while executing the function.
1829 */
1830 int
1831call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001832 char_u *funcname, // name of the function
1833 int len, // length of "name" or -1 to use strlen()
1834 typval_T *rettv, // return value goes here
1835 int argcount_in, // number of "argvars"
1836 typval_T *argvars_in, // vars for arguments, must have "argcount"
1837 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001838 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001839{
1840 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001841 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001842 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001843 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001844 char_u fname_buf[FLEN_FIXED + 1];
1845 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001846 char_u *fname = NULL;
1847 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001848 int argcount = argcount_in;
1849 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001850 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001851 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1852 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001853 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001854 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001855 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001856
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001857 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1858 // even when call_func() returns FAIL.
1859 rettv->v_type = VAR_UNKNOWN;
1860
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001861 if (partial != NULL)
1862 fp = partial->pt_func;
1863 if (fp == NULL)
1864 {
1865 // Make a copy of the name, if it comes from a funcref variable it
1866 // could be changed or deleted in the called function.
1867 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
1868 if (name == NULL)
1869 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001870
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001871 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1872 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001873
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001874 if (funcexe->doesrange != NULL)
1875 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001876
1877 if (partial != NULL)
1878 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001879 // When the function has a partial with a dict and there is a dict
1880 // argument, use the dict argument. That is backwards compatible.
1881 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001882 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001883 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001884 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001885 {
1886 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001887 {
1888 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1889 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001890 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001891 goto theend;
1892 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001893 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001894 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001895 for (i = 0; i < argcount_in; ++i)
1896 argv[i + argv_clear] = argvars_in[i];
1897 argvars = argv;
1898 argcount = partial->pt_argc + argcount_in;
1899 }
1900 }
1901
Bram Moolenaaref140542019-12-31 21:27:13 +01001902 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001903 {
1904 char_u *rfname = fname;
1905
Bram Moolenaare38eab22019-12-05 21:50:01 +01001906 // Ignore "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001907 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001908 rfname = fname + 2;
1909
Bram Moolenaare38eab22019-12-05 21:50:01 +01001910 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001911 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001912 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001913
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001914 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001915 {
1916 /*
1917 * User defined function.
1918 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001919 if (fp == NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001920 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001921
Bram Moolenaare38eab22019-12-05 21:50:01 +01001922 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001923 if (fp == NULL
1924 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001925 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001926 && !aborting())
1927 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001928 // executed an autocommand, search for the function again
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001929 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001930 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001931 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001932 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1933 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001934 // loaded a package, search for the function again
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001935 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001936 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02001937 if (fp == NULL)
1938 {
1939 char_u *p = untrans_function_name(rfname);
1940
1941 // If using Vim9 script try not local to the script.
1942 // TODO: should not do this if the name started with "s:".
1943 if (p != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001944 fp = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02001945 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001946
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001947 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01001948 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001949 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001950 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001951 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001952 // postponed filling in the arguments, do it now
1953 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001954 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001955
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001956 if (funcexe->basetv != NULL)
1957 {
1958 // Method call: base->Method()
1959 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1960 argv[0] = *funcexe->basetv;
1961 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001962 argvars = argv;
1963 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001964 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 error = call_user_func_check(fp, argcount, argvars, rettv,
1967 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001968 }
1969 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001970 else if (funcexe->basetv != NULL)
1971 {
1972 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001973 * expr->method(): Find the method name in the table, call its
1974 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02001975 */
1976 error = call_internal_method(fname, argcount, argvars, rettv,
1977 funcexe->basetv);
1978 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001979 else
1980 {
1981 /*
1982 * Find the function name in the table, call its implementation.
1983 */
1984 error = call_internal_func(fname, argcount, argvars, rettv);
1985 }
1986 /*
1987 * The function call (or "FuncUndefined" autocommand sequence) might
1988 * have been aborted by an error, an interrupt, or an explicitly thrown
1989 * exception that has not been caught so far. This situation can be
1990 * tested for by calling aborting(). For an error in an internal
1991 * function or for the "E132" error in call_user_func(), however, the
1992 * throw point at which the "force_abort" flag (temporarily reset by
1993 * emsg()) is normally updated has not been reached yet. We need to
1994 * update that flag first to make aborting() reliable.
1995 */
1996 update_force_abort();
1997 }
Bram Moolenaaref140542019-12-31 21:27:13 +01001998 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001999 ret = OK;
2000
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002001theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002002 /*
2003 * Report an error unless the argument evaluation or function call has been
2004 * cancelled due to an aborting error, an interrupt, or an exception.
2005 */
2006 if (!aborting())
2007 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002008 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002009 }
2010
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002011 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002012 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002013 clear_tv(&argv[--argv_clear + argv_base]);
2014
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002015 vim_free(tofree);
2016 vim_free(name);
2017
2018 return ret;
2019}
2020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 static char_u *
2022printable_func_name(ufunc_T *fp)
2023{
2024 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
2025}
2026
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002027/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002028 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002029 */
2030 static void
2031list_func_head(ufunc_T *fp, int indent)
2032{
2033 int j;
2034
2035 msg_start();
2036 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002037 msg_puts(" ");
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002038 if (fp->uf_dfunc_idx >= 0)
2039 msg_puts("def ");
2040 else
2041 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002043 msg_putchar('(');
2044 for (j = 0; j < fp->uf_args.ga_len; ++j)
2045 {
2046 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002047 msg_puts(", ");
2048 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049 if (fp->uf_arg_types != NULL)
2050 {
2051 char *tofree;
2052
2053 msg_puts(": ");
2054 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2055 vim_free(tofree);
2056 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002057 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2058 {
2059 msg_puts(" = ");
2060 msg_puts(((char **)(fp->uf_def_args.ga_data))
2061 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2062 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002063 }
2064 if (fp->uf_varargs)
2065 {
2066 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002067 msg_puts(", ");
2068 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002069 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 if (fp->uf_va_name != NULL)
2071 {
2072 if (j)
2073 msg_puts(", ");
2074 msg_puts("...");
2075 msg_puts((char *)fp->uf_va_name);
2076 if (fp->uf_va_type)
2077 {
2078 char *tofree;
2079
2080 msg_puts(": ");
2081 msg_puts(type_name(fp->uf_va_type, &tofree));
2082 vim_free(tofree);
2083 }
2084 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002085 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002086
2087 if (fp->uf_dfunc_idx >= 0)
2088 {
2089 if (fp->uf_ret_type != &t_void)
2090 {
2091 char *tofree;
2092
2093 msg_puts(": ");
2094 msg_puts(type_name(fp->uf_ret_type, &tofree));
2095 vim_free(tofree);
2096 }
2097 }
2098 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002099 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002100 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002101 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002102 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002103 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002104 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002105 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002106 msg_clr_eos();
2107 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002108 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002109}
2110
2111/*
2112 * Get a function name, translating "<SID>" and "<SNR>".
2113 * Also handles a Funcref in a List or Dictionary.
2114 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002115 * Set "*is_global" to TRUE when the function must be global, unless
2116 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002117 * flags:
2118 * TFN_INT: internal function name OK
2119 * TFN_QUIET: be quiet
2120 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002121 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002122 * Advances "pp" to just after the function name (if no error).
2123 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002124 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002125trans_function_name(
2126 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002127 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002128 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002129 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002130 funcdict_T *fdp, // return: info about dictionary used
2131 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002132{
2133 char_u *name = NULL;
2134 char_u *start;
2135 char_u *end;
2136 int lead;
2137 char_u sid_buf[20];
2138 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002139 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002140 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002142
2143 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002144 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002145 start = *pp;
2146
Bram Moolenaare38eab22019-12-05 21:50:01 +01002147 // Check for hard coded <SNR>: already translated function ID (from a user
2148 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002149 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2150 && (*pp)[2] == (int)KE_SNR)
2151 {
2152 *pp += 3;
2153 len = get_id_len(pp) + 3;
2154 return vim_strnsave(start, len);
2155 }
2156
Bram Moolenaare38eab22019-12-05 21:50:01 +01002157 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2158 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002159 lead = eval_fname_script(start);
2160 if (lead > 2)
2161 start += lead;
2162
Bram Moolenaare38eab22019-12-05 21:50:01 +01002163 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002164 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002165 lead > 2 ? 0 : FNE_CHECK_START);
2166 if (end == start)
2167 {
2168 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002169 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002170 goto theend;
2171 }
2172 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2173 {
2174 /*
2175 * Report an invalid expression in braces, unless the expression
2176 * evaluation has been cancelled due to an aborting error, an
2177 * interrupt, or an exception.
2178 */
2179 if (!aborting())
2180 {
2181 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002182 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002183 }
2184 else
2185 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2186 goto theend;
2187 }
2188
2189 if (lv.ll_tv != NULL)
2190 {
2191 if (fdp != NULL)
2192 {
2193 fdp->fd_dict = lv.ll_dict;
2194 fdp->fd_newkey = lv.ll_newkey;
2195 lv.ll_newkey = NULL;
2196 fdp->fd_di = lv.ll_di;
2197 }
2198 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2199 {
2200 name = vim_strsave(lv.ll_tv->vval.v_string);
2201 *pp = end;
2202 }
2203 else if (lv.ll_tv->v_type == VAR_PARTIAL
2204 && lv.ll_tv->vval.v_partial != NULL)
2205 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002206 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002207 *pp = end;
2208 if (partial != NULL)
2209 *partial = lv.ll_tv->vval.v_partial;
2210 }
2211 else
2212 {
2213 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2214 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002215 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002216 else
2217 *pp = end;
2218 name = NULL;
2219 }
2220 goto theend;
2221 }
2222
2223 if (lv.ll_name == NULL)
2224 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002225 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002226 *pp = end;
2227 goto theend;
2228 }
2229
Bram Moolenaare38eab22019-12-05 21:50:01 +01002230 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002231 if (lv.ll_exp_name != NULL)
2232 {
2233 len = (int)STRLEN(lv.ll_exp_name);
2234 name = deref_func_name(lv.ll_exp_name, &len, partial,
2235 flags & TFN_NO_AUTOLOAD);
2236 if (name == lv.ll_exp_name)
2237 name = NULL;
2238 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002239 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002240 {
2241 len = (int)(end - *pp);
2242 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2243 if (name == *pp)
2244 name = NULL;
2245 }
2246 if (name != NULL)
2247 {
2248 name = vim_strsave(name);
2249 *pp = end;
2250 if (STRNCMP(name, "<SNR>", 5) == 0)
2251 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002252 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002253 name[0] = K_SPECIAL;
2254 name[1] = KS_EXTRA;
2255 name[2] = (int)KE_SNR;
2256 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2257 }
2258 goto theend;
2259 }
2260
2261 if (lv.ll_exp_name != NULL)
2262 {
2263 len = (int)STRLEN(lv.ll_exp_name);
2264 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2265 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2266 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002267 // When there was "s:" already or the name expanded to get a
2268 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002269 lv.ll_name += 2;
2270 len -= 2;
2271 lead = 2;
2272 }
2273 }
2274 else
2275 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002276 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002277 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002278 {
2279 if (is_global != NULL && lv.ll_name[0] == 'g')
2280 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002281 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002282 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002283 len = (int)(end - lv.ll_name);
2284 }
2285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286 // In Vim9 script a user function is script-local by default.
2287 vim9script = ASCII_ISUPPER(*start)
2288 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2289
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002290 /*
2291 * Copy the function name to allocated memory.
2292 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2293 * Accept <SNR>123_name() outside a script.
2294 */
2295 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002296 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002298 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 if (!vim9script)
2300 lead = 3;
2301 if (vim9script || (lv.ll_exp_name != NULL
2302 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002303 || eval_fname_sid(*pp))
2304 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002306 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002307 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002308 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002309 goto theend;
2310 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002311 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 if (vim9script)
2313 extra = 3 + (int)STRLEN(sid_buf);
2314 else
2315 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002316 }
2317 }
2318 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2319 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002320 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002321 start);
2322 goto theend;
2323 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002324 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002325 {
2326 char_u *cp = vim_strchr(lv.ll_name, ':');
2327
2328 if (cp != NULL && cp < end)
2329 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002330 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002331 goto theend;
2332 }
2333 }
2334
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002336 if (name != NULL)
2337 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002338 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002339 {
2340 name[0] = K_SPECIAL;
2341 name[1] = KS_EXTRA;
2342 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002343 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002344 STRCPY(name + 3, sid_buf);
2345 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002346 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2347 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002348 }
2349 *pp = end;
2350
2351theend:
2352 clear_lval(&lv);
2353 return name;
2354}
2355
2356/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002357 * Assuming "name" is the result of trans_function_name() and it was prefixed
2358 * to use the script-local name, return the unmodified name (points into
2359 * "name"). Otherwise return NULL.
2360 * This can be used to first search for a script-local function and fall back
2361 * to the global function if not found.
2362 */
2363 char_u *
2364untrans_function_name(char_u *name)
2365{
2366 char_u *p;
2367
2368 if (*name == K_SPECIAL && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2369 {
2370 p = vim_strchr(name, '_');
2371 if (p != NULL)
2372 return p + 1;
2373 }
2374 return NULL;
2375}
2376
2377/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02002378 * ":function" also supporting nested ":def".
2379 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002380 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02002381 ufunc_T *
Bram Moolenaar09689a02020-05-09 22:50:08 +02002382def_function(exarg_T *eap, char_u *name_arg, void *context, int compile)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002383{
2384 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002385 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002386 int j;
2387 int c;
2388 int saved_did_emsg;
2389 int saved_wait_return = need_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002390 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002391 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002392 char_u *p;
2393 char_u *arg;
2394 char_u *line_arg = NULL;
2395 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002397 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002398 garray_T newlines;
2399 int varargs = FALSE;
2400 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002402 ufunc_T *fp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002403 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002404 int indent;
2405 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406#define MAX_FUNC_NESTING 50
2407 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002408 dictitem_T *v;
2409 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002410 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002411 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002412 int todo;
2413 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002414 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002415 linenr_T sourcing_lnum_off;
2416 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002417 int is_heredoc = FALSE;
2418 char_u *skip_until = NULL;
2419 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002420
2421 /*
2422 * ":function" without argument: list functions.
2423 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002424 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002425 {
2426 if (!eap->skip)
2427 {
2428 todo = (int)func_hashtab.ht_used;
2429 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2430 {
2431 if (!HASHITEM_EMPTY(hi))
2432 {
2433 --todo;
2434 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 if ((fp->uf_flags & FC_DEAD)
2436 || message_filtered(fp->uf_name))
Bram Moolenaarf86db782018-10-25 13:31:37 +02002437 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002438 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002439 list_func_head(fp, FALSE);
2440 }
2441 }
2442 }
2443 eap->nextcmd = check_nextcmd(eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002444 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002445 }
2446
2447 /*
2448 * ":function /pat": list functions matching pattern.
2449 */
2450 if (*eap->arg == '/')
2451 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002452 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002453 if (!eap->skip)
2454 {
2455 regmatch_T regmatch;
2456
2457 c = *p;
2458 *p = NUL;
2459 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2460 *p = c;
2461 if (regmatch.regprog != NULL)
2462 {
2463 regmatch.rm_ic = p_ic;
2464
2465 todo = (int)func_hashtab.ht_used;
2466 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2467 {
2468 if (!HASHITEM_EMPTY(hi))
2469 {
2470 --todo;
2471 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 if ((fp->uf_flags & FC_DEAD) == 0
2473 && !isdigit(*fp->uf_name)
2474 && vim_regexec(&regmatch, fp->uf_name, 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002475 list_func_head(fp, FALSE);
2476 }
2477 }
2478 vim_regfree(regmatch.regprog);
2479 }
2480 }
2481 if (*p == '/')
2482 ++p;
2483 eap->nextcmd = check_nextcmd(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002484 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002485 }
2486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 ga_init(&newargs);
2488 ga_init(&argtypes);
2489 ga_init(&default_args);
2490
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002491 /*
2492 * Get the function name. There are these situations:
2493 * func normal function name
2494 * "name" == func, "fudi.fd_dict" == NULL
2495 * dict.func new dictionary entry
2496 * "name" == NULL, "fudi.fd_dict" set,
2497 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2498 * dict.func existing dict entry with a Funcref
2499 * "name" == func, "fudi.fd_dict" set,
2500 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2501 * dict.func existing dict entry that's not a Funcref
2502 * "name" == NULL, "fudi.fd_dict" set,
2503 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2504 * s:func script-local function name
2505 * g:func global function name, same as "func"
2506 */
2507 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002508 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002509 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02002510 // nested function, argument is (args).
2511 paren = TRUE;
2512 CLEAR_FIELD(fudi);
2513 }
2514 else
2515 {
2516 name = trans_function_name(&p, &is_global, eap->skip,
2517 TFN_NO_AUTOLOAD, &fudi, NULL);
2518 paren = (vim_strchr(p, '(') != NULL);
2519 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002520 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02002521 /*
2522 * Return on an invalid expression in braces, unless the expression
2523 * evaluation has been cancelled due to an aborting error, an
2524 * interrupt, or an exception.
2525 */
2526 if (!aborting())
2527 {
2528 if (!eap->skip && fudi.fd_newkey != NULL)
2529 semsg(_(e_dictkey), fudi.fd_newkey);
2530 vim_free(fudi.fd_newkey);
2531 return NULL;
2532 }
2533 else
2534 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002535 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002536 }
2537
Bram Moolenaare38eab22019-12-05 21:50:01 +01002538 // An error in a function call during evaluation of an expression in magic
2539 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002540 saved_did_emsg = did_emsg;
2541 did_emsg = FALSE;
2542
2543 /*
2544 * ":function func" with only function name: list function.
2545 */
2546 if (!paren)
2547 {
2548 if (!ends_excmd(*skipwhite(p)))
2549 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002550 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002551 goto ret_free;
2552 }
2553 eap->nextcmd = check_nextcmd(p);
2554 if (eap->nextcmd != NULL)
2555 *p = NUL;
2556 if (!eap->skip && !got_int)
2557 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002558 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002559 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
2560 {
2561 char_u *up = untrans_function_name(name);
2562
2563 // With Vim9 script the name was made script-local, if not
2564 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02002565 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002566 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002567 }
2568
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002569 if (fp != NULL)
2570 {
2571 list_func_head(fp, TRUE);
2572 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2573 {
2574 if (FUNCLINE(fp, j) == NULL)
2575 continue;
2576 msg_putchar('\n');
2577 msg_outnum((long)(j + 1));
2578 if (j < 9)
2579 msg_putchar(' ');
2580 if (j < 99)
2581 msg_putchar(' ');
2582 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002583 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002584 ui_breakcheck();
2585 }
2586 if (!got_int)
2587 {
2588 msg_putchar('\n');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 if (fp->uf_dfunc_idx >= 0)
2590 msg_puts(" enddef");
2591 else
2592 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002593 }
2594 }
2595 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02002596 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002597 }
2598 goto ret_free;
2599 }
2600
2601 /*
2602 * ":function name(arg1, arg2)" Define function.
2603 */
2604 p = skipwhite(p);
2605 if (*p != '(')
2606 {
2607 if (!eap->skip)
2608 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002609 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002610 goto ret_free;
2611 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002612 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002613 if (vim_strchr(p, '(') != NULL)
2614 p = vim_strchr(p, '(');
2615 }
2616 p = skipwhite(p + 1);
2617
2618 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2619
Bram Moolenaar04b12692020-05-04 23:24:44 +02002620 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002621 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002622 // Check the name of the function. Unless it's a dictionary function
2623 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002624 if (name != NULL)
2625 arg = name;
2626 else
2627 arg = fudi.fd_newkey;
2628 if (arg != NULL && (fudi.fd_di == NULL
2629 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2630 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2631 {
2632 if (*arg == K_SPECIAL)
2633 j = 3;
2634 else
2635 j = 0;
2636 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2637 : eval_isnamec(arg[j])))
2638 ++j;
2639 if (arg[j] != NUL)
2640 emsg_funcname((char *)e_invarg2, arg);
2641 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002642 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002643 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002644 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002645 }
2646
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002647 // This may get more lines and make the pointers into the first line
2648 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 if (get_function_args(&p, ')', &newargs,
2650 eap->cmdidx == CMD_def ? &argtypes : NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002651 &varargs, &default_args, eap->skip,
2652 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002653 goto errret_2;
2654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002656 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 // find the return type: :def Func(): type
2658 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002659 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 ret_type = skipwhite(p + 1);
2661 p = skip_type(ret_type);
2662 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002663 {
2664 ret_type = vim_strnsave(ret_type, (int)(p - ret_type));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002666 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002668 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02002670 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002671 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002672 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002673 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 else
2675 // find extra arguments "range", "dict", "abort" and "closure"
2676 for (;;)
2677 {
2678 p = skipwhite(p);
2679 if (STRNCMP(p, "range", 5) == 0)
2680 {
2681 flags |= FC_RANGE;
2682 p += 5;
2683 }
2684 else if (STRNCMP(p, "dict", 4) == 0)
2685 {
2686 flags |= FC_DICT;
2687 p += 4;
2688 }
2689 else if (STRNCMP(p, "abort", 5) == 0)
2690 {
2691 flags |= FC_ABORT;
2692 p += 5;
2693 }
2694 else if (STRNCMP(p, "closure", 7) == 0)
2695 {
2696 flags |= FC_CLOSURE;
2697 p += 7;
2698 if (current_funccal == NULL)
2699 {
2700 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2701 name == NULL ? (char_u *)"" : name);
2702 goto erret;
2703 }
2704 }
2705 else
2706 break;
2707 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002708
Bram Moolenaare38eab22019-12-05 21:50:01 +01002709 // When there is a line break use what follows for the function body.
2710 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002711 if (*p == '\n')
2712 line_arg = p + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002713 else if (*p != NUL && *p != '"' && !(eap->cmdidx == CMD_def && *p == '#')
2714 && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002715 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002716
2717 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2719 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002720 */
2721 if (KeyTyped)
2722 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002723 // Check if the function already exists, don't let the user type the
2724 // whole function before telling him it doesn't work! For a script we
2725 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002726 if (!eap->skip && !eap->forceit)
2727 {
2728 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002729 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002730 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002731 emsg_funcname(e_funcexts, name);
2732 }
2733
2734 if (!eap->skip && did_emsg)
2735 goto erret;
2736
Bram Moolenaare38eab22019-12-05 21:50:01 +01002737 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002738 cmdline_row = msg_row;
2739 }
2740
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002741 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002742 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002743
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002744 indent = 2;
2745 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002747 for (;;)
2748 {
2749 if (KeyTyped)
2750 {
2751 msg_scroll = TRUE;
2752 saved_wait_return = FALSE;
2753 }
2754 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002755
2756 if (line_arg != NULL)
2757 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002758 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002759 theline = line_arg;
2760 p = vim_strchr(theline, '\n');
2761 if (p == NULL)
2762 line_arg += STRLEN(line_arg);
2763 else
2764 {
2765 *p = NUL;
2766 line_arg = p + 1;
2767 }
2768 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002769 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002770 {
2771 vim_free(line_to_free);
2772 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002773 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002774 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002775 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002776 line_to_free = theline;
2777 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002778 if (KeyTyped)
2779 lines_left = Rows - 1;
2780 if (theline == NULL)
2781 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 if (eap->cmdidx == CMD_def)
2783 emsg(_("E1057: Missing :enddef"));
2784 else
2785 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002786 goto erret;
2787 }
2788
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002789 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002790 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002791 if (SOURCING_LNUM < sourcing_lnum_off)
2792 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002793 else
2794 sourcing_lnum_off = 0;
2795
2796 if (skip_until != NULL)
2797 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002799 // * ":append" and "."
2800 // * ":python <<EOF" and "EOF"
2801 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2802 if (heredoc_trimmed == NULL
2803 || (is_heredoc && skipwhite(theline) == theline)
2804 || STRNCMP(theline, heredoc_trimmed,
2805 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002806 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002807 if (heredoc_trimmed == NULL)
2808 p = theline;
2809 else if (is_heredoc)
2810 p = skipwhite(theline) == theline
2811 ? theline : theline + STRLEN(heredoc_trimmed);
2812 else
2813 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002814 if (STRCMP(p, skip_until) == 0)
2815 {
2816 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002817 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002818 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002819 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002820 }
2821 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002822 }
2823 else
2824 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002825 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002826 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002827 ;
2828
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 // Check for "endfunction" or "enddef".
2830 if (checkforcmd(&p, nesting_def[nesting]
2831 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002832 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002833 char_u *nextcmd = NULL;
2834
Bram Moolenaar663bb232017-06-22 19:12:10 +02002835 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002836 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002837 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002838 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002839 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 give_warning2(eap->cmdidx == CMD_def
2841 ? (char_u *)_("W1001: Text found after :enddef: %s")
2842 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002843 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002844 if (nextcmd != NULL)
2845 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002846 // Another command follows. If the line came from "eap" we
2847 // can simply point into it, otherwise we need to change
2848 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002849 eap->nextcmd = nextcmd;
2850 if (line_to_free != NULL)
2851 {
2852 vim_free(*eap->cmdlinep);
2853 *eap->cmdlinep = line_to_free;
2854 line_to_free = NULL;
2855 }
2856 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002857 break;
2858 }
2859
Bram Moolenaare38eab22019-12-05 21:50:01 +01002860 // Increase indent inside "if", "while", "for" and "try", decrease
2861 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002863 indent -= 2;
2864 else if (STRNCMP(p, "if", 2) == 0
2865 || STRNCMP(p, "wh", 2) == 0
2866 || STRNCMP(p, "for", 3) == 0
2867 || STRNCMP(p, "try", 3) == 0)
2868 indent += 2;
2869
Bram Moolenaare38eab22019-12-05 21:50:01 +01002870 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002871 // Only recognize "def" inside "def", not inside "function",
2872 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002874 if (checkforcmd(&p, "function", 2)
2875 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002876 {
2877 if (*p == '!')
2878 p = skipwhite(p + 1);
2879 p += eval_fname_script(p);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002880 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002881 if (*skipwhite(p) == '(')
2882 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 if (nesting == MAX_FUNC_NESTING - 1)
2884 emsg(_("E1058: function nesting too deep"));
2885 else
2886 {
2887 ++nesting;
2888 nesting_def[nesting] = (c == 'd');
2889 indent += 2;
2890 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002891 }
2892 }
2893
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002894 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002895 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002896 if (eap->cmdidx != CMD_def
2897 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002898 || (p[0] == 'c'
2899 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2900 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2901 && (STRNCMP(&p[3], "nge", 3) != 0
2902 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002903 || (p[0] == 'i'
2904 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002905 && (!ASCII_ISALPHA(p[2])
2906 || (p[2] == 's'
2907 && (!ASCII_ISALPHA(p[3])
2908 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002909 skip_until = vim_strsave((char_u *)".");
2910
Bram Moolenaare38eab22019-12-05 21:50:01 +01002911 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002912 arg = skipwhite(skiptowhite(p));
2913 if (arg[0] == '<' && arg[1] =='<'
2914 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002915 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2916 || ((p[2] == '3' || p[2] == 'x')
2917 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002918 || (p[0] == 'p' && p[1] == 'e'
2919 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2920 || (p[0] == 't' && p[1] == 'c'
2921 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2922 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2923 && !ASCII_ISALPHA(p[3]))
2924 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2925 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2926 || (p[0] == 'm' && p[1] == 'z'
2927 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2928 ))
2929 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002930 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002931 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02002932 if (STRNCMP(p, "trim", 4) == 0)
2933 {
2934 // Ignore leading white space.
2935 p = skipwhite(p + 4);
2936 heredoc_trimmed = vim_strnsave(theline,
2937 (int)(skipwhite(theline) - theline));
2938 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002939 if (*p == NUL)
2940 skip_until = vim_strsave((char_u *)".");
2941 else
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02002942 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2943 do_concat = FALSE;
2944 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002945 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002946
2947 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002948 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002949 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002950 if (*arg == '[')
2951 arg = vim_strchr(arg, ']');
2952 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002953 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002954 arg = skipwhite(skiptowhite(arg));
2955 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2956 && ((p[0] == 'l'
2957 && p[1] == 'e'
2958 && (!ASCII_ISALNUM(p[2])
2959 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02002960 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002961 p = skipwhite(arg + 3);
2962 if (STRNCMP(p, "trim", 4) == 0)
2963 {
2964 // Ignore leading white space.
2965 p = skipwhite(p + 4);
2966 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar8471e572019-05-19 21:37:18 +02002967 (int)(skipwhite(theline) - theline));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002968 }
2969 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2970 do_concat = FALSE;
2971 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002972 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002973 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002974 }
2975
Bram Moolenaare38eab22019-12-05 21:50:01 +01002976 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002977 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002978 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002979
Bram Moolenaare38eab22019-12-05 21:50:01 +01002980 // Copy the line to newly allocated memory. get_one_sourceline()
2981 // allocates 250 bytes per line, this saves 80% on average. The cost
2982 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002984 if (p == NULL)
2985 goto erret;
2986 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002987
Bram Moolenaare38eab22019-12-05 21:50:01 +01002988 // Add NULL lines for continuation lines, so that the line count is
2989 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002990 while (sourcing_lnum_off-- > 0)
2991 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2992
Bram Moolenaare38eab22019-12-05 21:50:01 +01002993 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994 if (line_arg != NULL && *line_arg == NUL)
2995 line_arg = NULL;
2996 }
2997
Bram Moolenaare38eab22019-12-05 21:50:01 +01002998 // Don't define the function when skipping commands or when an error was
2999 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003000 if (eap->skip || did_emsg)
3001 goto erret;
3002
3003 /*
3004 * If there are no errors, add the function
3005 */
3006 if (fudi.fd_dict == NULL)
3007 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 hashtab_T *ht;
3009
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003010 v = find_var(name, &ht, FALSE);
3011 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3012 {
3013 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3014 name);
3015 goto erret;
3016 }
3017
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003018 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003019 if (fp != NULL)
3020 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 int dead = fp->uf_flags & FC_DEAD;
3022
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003023 // Function can be replaced with "function!" and when sourcing the
3024 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003026 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
3027 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003028 {
3029 emsg_funcname(e_funcexts, name);
3030 goto erret;
3031 }
3032 if (fp->uf_calls > 0)
3033 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003034 emsg_funcname(
3035 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003036 name);
3037 goto erret;
3038 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003039 if (fp->uf_refcount > 1)
3040 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003041 // This function is referenced somewhere, don't redefine it but
3042 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003043 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003044 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003045 fp = NULL;
3046 overwrite = TRUE;
3047 }
3048 else
3049 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003050 char_u *exp_name = fp->uf_name_exp;
3051
3052 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003053 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003054 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003055 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003056 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003058#ifdef FEAT_PROFILE
3059 fp->uf_profiling = FALSE;
3060 fp->uf_prof_initialized = FALSE;
3061#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003062 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003063 }
3064 }
3065 else
3066 {
3067 char numbuf[20];
3068
3069 fp = NULL;
3070 if (fudi.fd_newkey == NULL && !eap->forceit)
3071 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003072 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003073 goto erret;
3074 }
3075 if (fudi.fd_di == NULL)
3076 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003077 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003078 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003079 goto erret;
3080 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003081 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003082 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003083 goto erret;
3084
Bram Moolenaare38eab22019-12-05 21:50:01 +01003085 // Give the function a sequential number. Can only be used with a
3086 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003087 vim_free(name);
3088 sprintf(numbuf, "%d", ++func_nr);
3089 name = vim_strsave((char_u *)numbuf);
3090 if (name == NULL)
3091 goto erret;
3092 }
3093
3094 if (fp == NULL)
3095 {
3096 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3097 {
3098 int slen, plen;
3099 char_u *scriptname;
3100
Bram Moolenaare38eab22019-12-05 21:50:01 +01003101 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003102 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003103 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003104 {
3105 scriptname = autoload_name(name);
3106 if (scriptname != NULL)
3107 {
3108 p = vim_strchr(scriptname, '/');
3109 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003110 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003111 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003112 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003113 j = OK;
3114 vim_free(scriptname);
3115 }
3116 }
3117 if (j == FAIL)
3118 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003119 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003120 goto erret;
3121 }
3122 }
3123
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003124 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003125 if (fp == NULL)
3126 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 fp->uf_dfunc_idx = -1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003128
3129 if (fudi.fd_dict != NULL)
3130 {
3131 if (fudi.fd_di == NULL)
3132 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003133 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003134 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3135 if (fudi.fd_di == NULL)
3136 {
3137 vim_free(fp);
3138 goto erret;
3139 }
3140 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3141 {
3142 vim_free(fudi.fd_di);
3143 vim_free(fp);
3144 goto erret;
3145 }
3146 }
3147 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003148 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003149 clear_tv(&fudi.fd_di->di_tv);
3150 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003151 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003152
Bram Moolenaare38eab22019-12-05 21:50:01 +01003153 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003154 flags |= FC_DICT;
3155 }
3156
Bram Moolenaare38eab22019-12-05 21:50:01 +01003157 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003158 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003159 if (overwrite)
3160 {
3161 hi = hash_find(&func_hashtab, name);
3162 hi->hi_key = UF2HIKEY(fp);
3163 }
3164 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003165 {
3166 vim_free(fp);
3167 goto erret;
3168 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003169 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003170 }
3171 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003172 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003174 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175
3176 if (eap->cmdidx == CMD_def)
3177 {
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003178 int lnum_save = SOURCING_LNUM;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003179
3180 // error messages are for the first function line
3181 SOURCING_LNUM = sourcing_lnum_top;
3182
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 // parse the argument types
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003184 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185
3186 if (argtypes.ga_len > 0)
3187 {
3188 // When "varargs" is set the last name/type goes into uf_va_name
3189 // and uf_va_type.
3190 int len = argtypes.ga_len - (varargs ? 1 : 0);
3191
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003192 if (len > 0)
3193 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 if (fp->uf_arg_types != NULL)
3195 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003196 int i;
3197 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198
3199 for (i = 0; i < len; ++ i)
3200 {
3201 p = ((char_u **)argtypes.ga_data)[i];
3202 if (p == NULL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003203 // will get the type from the default value
3204 type = &t_unknown;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003206 type = parse_type(&p, &fp->uf_type_list);
3207 if (type == NULL)
3208 {
3209 SOURCING_LNUM = lnum_save;
3210 goto errret_2;
3211 }
3212 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 }
3214 }
3215 if (varargs)
3216 {
3217 // Move the last argument "...name: type" to uf_va_name and
3218 // uf_va_type.
3219 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3220 [fp->uf_args.ga_len - 1];
3221 --fp->uf_args.ga_len;
3222 p = ((char_u **)argtypes.ga_data)[len];
3223 if (p == NULL)
3224 // todo: get type from default value
3225 fp->uf_va_type = &t_any;
3226 else
3227 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003228 if (fp->uf_va_type == NULL)
3229 {
3230 SOURCING_LNUM = lnum_save;
3231 goto errret_2;
3232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 }
3234 varargs = FALSE;
3235 }
3236
3237 // parse the return type, if any
3238 if (ret_type == NULL)
3239 fp->uf_ret_type = &t_void;
3240 else
3241 {
3242 p = ret_type;
3243 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3244 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02003245 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 }
3247
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003248 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003249 if ((flags & FC_CLOSURE) != 0)
3250 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003251 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003252 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003253 }
3254 else
3255 fp->uf_scoped = NULL;
3256
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003257#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003258 if (prof_def_func())
3259 func_do_profile(fp);
3260#endif
3261 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003262 if (sandbox)
3263 flags |= FC_SANDBOX;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003264 if (in_vim9script() && !ASCII_ISUPPER(*fp->uf_name))
3265 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003266 fp->uf_flags = flags;
3267 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003268 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003269 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003270 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 if (is_export)
3272 {
3273 fp->uf_flags |= FC_EXPORT;
3274 // let ex_export() know the export worked.
3275 is_export = FALSE;
3276 }
3277
Bram Moolenaar09689a02020-05-09 22:50:08 +02003278 // ":def Func()" may need to be compiled
3279 if (eap->cmdidx == CMD_def && compile)
Bram Moolenaar04b12692020-05-04 23:24:44 +02003280 compile_def_function(fp, FALSE, context);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003282 goto ret_free;
3283
3284erret:
3285 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003286 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003287errret_2:
3288 ga_clear_strings(&newlines);
3289ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003290 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003291 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003292 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003293 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003294 if (name != name_arg)
3295 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003296 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003297 did_emsg |= saved_did_emsg;
3298 need_wait_return |= saved_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003299
3300 return fp;
3301}
3302
3303/*
3304 * ":function"
3305 */
3306 void
3307ex_function(exarg_T *eap)
3308{
Bram Moolenaar09689a02020-05-09 22:50:08 +02003309 (void)def_function(eap, NULL, NULL, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003310}
3311
3312/*
3313 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3314 * Return 2 if "p" starts with "s:".
3315 * Return 0 otherwise.
3316 */
3317 int
3318eval_fname_script(char_u *p)
3319{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003320 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3321 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003322 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3323 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3324 return 5;
3325 if (p[0] == 's' && p[1] == ':')
3326 return 2;
3327 return 0;
3328}
3329
3330 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003331translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003332{
3333 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003334 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003335 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336}
3337
3338/*
3339 * Return TRUE when "ufunc" has old-style "..." varargs
3340 * or named varargs "...name: type".
3341 */
3342 int
3343has_varargs(ufunc_T *ufunc)
3344{
3345 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003346}
3347
3348/*
3349 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003350 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003351 */
3352 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003353function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003354{
3355 char_u *nm = name;
3356 char_u *p;
3357 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003358 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003359 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003360
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003361 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3362 if (no_deref)
3363 flag |= TFN_NO_DEREF;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003364 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003365 nm = skipwhite(nm);
3366
Bram Moolenaare38eab22019-12-05 21:50:01 +01003367 // Only accept "funcname", "funcname ", "funcname (..." and
3368 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003369 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003370 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003371 vim_free(p);
3372 return n;
3373}
3374
Bram Moolenaar113e1072019-01-20 15:30:40 +01003375#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003376 char_u *
3377get_expanded_name(char_u *name, int check)
3378{
3379 char_u *nm = name;
3380 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003381 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003382
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003383 p = trans_function_name(&nm, &is_global, FALSE,
3384 TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003385
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003386 if (p != NULL && *nm == NUL
3387 && (!check || translated_function_exists(p, is_global)))
3388 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003389
3390 vim_free(p);
3391 return NULL;
3392}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003393#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003394
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003395/*
3396 * Function given to ExpandGeneric() to obtain the list of user defined
3397 * function names.
3398 */
3399 char_u *
3400get_user_func_name(expand_T *xp, int idx)
3401{
3402 static long_u done;
3403 static hashitem_T *hi;
3404 ufunc_T *fp;
3405
3406 if (idx == 0)
3407 {
3408 done = 0;
3409 hi = func_hashtab.ht_array;
3410 }
3411 if (done < func_hashtab.ht_used)
3412 {
3413 if (done++ > 0)
3414 ++hi;
3415 while (HASHITEM_EMPTY(hi))
3416 ++hi;
3417 fp = HI2UF(hi);
3418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 // don't show dead, dict and lambda functions
3420 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003421 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003423
3424 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003425 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003426
3427 cat_func_name(IObuff, fp);
3428 if (xp->xp_context != EXPAND_USER_FUNC)
3429 {
3430 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003432 STRCAT(IObuff, ")");
3433 }
3434 return IObuff;
3435 }
3436 return NULL;
3437}
3438
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003439/*
3440 * ":delfunction {name}"
3441 */
3442 void
3443ex_delfunction(exarg_T *eap)
3444{
3445 ufunc_T *fp = NULL;
3446 char_u *p;
3447 char_u *name;
3448 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003449 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003450
3451 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003452 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003453 vim_free(fudi.fd_newkey);
3454 if (name == NULL)
3455 {
3456 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003457 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003458 return;
3459 }
3460 if (!ends_excmd(*skipwhite(p)))
3461 {
3462 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003463 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003464 return;
3465 }
3466 eap->nextcmd = check_nextcmd(p);
3467 if (eap->nextcmd != NULL)
3468 *p = NUL;
3469
3470 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003471 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003472 vim_free(name);
3473
3474 if (!eap->skip)
3475 {
3476 if (fp == NULL)
3477 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003478 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003479 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003480 return;
3481 }
3482 if (fp->uf_calls > 0)
3483 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003484 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003485 return;
3486 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003487 if (fp->uf_flags & FC_VIM9)
3488 {
3489 semsg(_("E1084: Cannot delete Vim9 script function %s"), eap->arg);
3490 return;
3491 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003492
3493 if (fudi.fd_dict != NULL)
3494 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003495 // Delete the dict item that refers to the function, it will
3496 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003497 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3498 }
3499 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003500 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003501 // A normal function (not a numbered function or lambda) has a
3502 // refcount of 1 for the entry in the hashtable. When deleting
3503 // it and the refcount is more than one, it should be kept.
3504 // A numbered function and lambda should be kept if the refcount is
3505 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003506 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003507 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003508 // Function is still referenced somewhere. Don't free it but
3509 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003510 if (func_remove(fp))
3511 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003512 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003513 }
3514 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003515 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003516 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003517 }
3518}
3519
3520/*
3521 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003522 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003523 */
3524 void
3525func_unref(char_u *name)
3526{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003527 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003528
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003529 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003530 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003531 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003532 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003533 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003534#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003535 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003536#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003537 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003538 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003539 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003540 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003541 // Only delete it when it's not being used. Otherwise it's done
3542 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003543 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003544 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003545 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003546}
3547
3548/*
3549 * Unreference a Function: decrement the reference count and free it when it
3550 * becomes zero.
3551 */
3552 void
3553func_ptr_unref(ufunc_T *fp)
3554{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003555 if (fp != NULL && --fp->uf_refcount <= 0)
3556 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003557 // Only delete it when it's not being used. Otherwise it's done
3558 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003559 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003560 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003561 }
3562}
3563
3564/*
3565 * Count a reference to a Function.
3566 */
3567 void
3568func_ref(char_u *name)
3569{
3570 ufunc_T *fp;
3571
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003572 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003573 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003574 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003575 if (fp != NULL)
3576 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003577 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003578 // Only give an error for a numbered function.
3579 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003580 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003581}
3582
3583/*
3584 * Count a reference to a Function.
3585 */
3586 void
3587func_ptr_ref(ufunc_T *fp)
3588{
3589 if (fp != NULL)
3590 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003591}
3592
3593/*
3594 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3595 * referenced from anywhere that is in use.
3596 */
3597 static int
3598can_free_funccal(funccall_T *fc, int copyID)
3599{
3600 return (fc->l_varlist.lv_copyID != copyID
3601 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003602 && fc->l_avars.dv_copyID != copyID
3603 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003604}
3605
3606/*
3607 * ":return [expr]"
3608 */
3609 void
3610ex_return(exarg_T *eap)
3611{
3612 char_u *arg = eap->arg;
3613 typval_T rettv;
3614 int returning = FALSE;
3615
3616 if (current_funccal == NULL)
3617 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003618 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003619 return;
3620 }
3621
3622 if (eap->skip)
3623 ++emsg_skip;
3624
3625 eap->nextcmd = NULL;
3626 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaar32e35112020-05-14 22:41:15 +02003627 && eval0(arg, &rettv, &eap->nextcmd, eap->skip ? 0 : EVAL_EVALUATE)
3628 != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003629 {
3630 if (!eap->skip)
3631 returning = do_return(eap, FALSE, TRUE, &rettv);
3632 else
3633 clear_tv(&rettv);
3634 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003635 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003636 else if (!eap->skip)
3637 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003638 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003639 update_force_abort();
3640
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003641 /*
3642 * Return unless the expression evaluation has been cancelled due to an
3643 * aborting error, an interrupt, or an exception.
3644 */
3645 if (!aborting())
3646 returning = do_return(eap, FALSE, TRUE, NULL);
3647 }
3648
Bram Moolenaare38eab22019-12-05 21:50:01 +01003649 // When skipping or the return gets pending, advance to the next command
3650 // in this line (!returning). Otherwise, ignore the rest of the line.
3651 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003652 if (returning)
3653 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003654 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003655 eap->nextcmd = check_nextcmd(arg);
3656
3657 if (eap->skip)
3658 --emsg_skip;
3659}
3660
3661/*
3662 * ":1,25call func(arg1, arg2)" function call.
3663 */
3664 void
3665ex_call(exarg_T *eap)
3666{
3667 char_u *arg = eap->arg;
3668 char_u *startarg;
3669 char_u *name;
3670 char_u *tofree;
3671 int len;
3672 typval_T rettv;
3673 linenr_T lnum;
3674 int doesrange;
3675 int failed = FALSE;
3676 funcdict_T fudi;
3677 partial_T *partial = NULL;
3678
3679 if (eap->skip)
3680 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003681 // trans_function_name() doesn't work well when skipping, use eval0()
3682 // instead to skip to any following command, e.g. for:
3683 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003684 ++emsg_skip;
Bram Moolenaar32e35112020-05-14 22:41:15 +02003685 if (eval0(eap->arg, &rettv, &eap->nextcmd, 0) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003686 clear_tv(&rettv);
3687 --emsg_skip;
3688 return;
3689 }
3690
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003691 tofree = trans_function_name(&arg, NULL, eap->skip,
3692 TFN_INT, &fudi, &partial);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003693 if (fudi.fd_newkey != NULL)
3694 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003695 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003696 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003697 vim_free(fudi.fd_newkey);
3698 }
3699 if (tofree == NULL)
3700 return;
3701
Bram Moolenaare38eab22019-12-05 21:50:01 +01003702 // Increase refcount on dictionary, it could get deleted when evaluating
3703 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003704 if (fudi.fd_dict != NULL)
3705 ++fudi.fd_dict->dv_refcount;
3706
Bram Moolenaare38eab22019-12-05 21:50:01 +01003707 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3708 // contents. For VAR_PARTIAL get its partial, unless we already have one
3709 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003710 len = (int)STRLEN(tofree);
3711 name = deref_func_name(tofree, &len,
3712 partial != NULL ? NULL : &partial, FALSE);
3713
Bram Moolenaare38eab22019-12-05 21:50:01 +01003714 // Skip white space to allow ":call func ()". Not good, but required for
3715 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003716 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003717 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003718
3719 if (*startarg != '(')
3720 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003722 goto end;
3723 }
3724
3725 /*
3726 * When skipping, evaluate the function once, to find the end of the
3727 * arguments.
3728 * When the function takes a range, this is discovered after the first
3729 * call, and the loop is broken.
3730 */
3731 if (eap->skip)
3732 {
3733 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003734 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003735 }
3736 else
3737 lnum = eap->line1;
3738 for ( ; lnum <= eap->line2; ++lnum)
3739 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003740 funcexe_T funcexe;
3741
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003742 if (!eap->skip && eap->addr_count > 0)
3743 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003744 if (lnum > curbuf->b_ml.ml_line_count)
3745 {
3746 // If the function deleted lines or switched to another buffer
3747 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003748 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003749 break;
3750 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003751 curwin->w_cursor.lnum = lnum;
3752 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003753 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003754 }
3755 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003756
Bram Moolenaara80faa82020-04-12 19:37:17 +02003757 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003758 funcexe.firstline = eap->line1;
3759 funcexe.lastline = eap->line2;
3760 funcexe.doesrange = &doesrange;
3761 funcexe.evaluate = !eap->skip;
3762 funcexe.partial = partial;
3763 funcexe.selfdict = fudi.fd_dict;
3764 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003765 {
3766 failed = TRUE;
3767 break;
3768 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003769 if (has_watchexpr())
3770 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003771
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003772 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaar32e35112020-05-14 22:41:15 +02003773 if (handle_subscript(&arg, &rettv, eap->skip ? 0 : EVAL_EVALUATE,
3774 TRUE, name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003775 {
3776 failed = TRUE;
3777 break;
3778 }
3779
3780 clear_tv(&rettv);
3781 if (doesrange || eap->skip)
3782 break;
3783
Bram Moolenaare38eab22019-12-05 21:50:01 +01003784 // Stop when immediately aborting on error, or when an interrupt
3785 // occurred or an exception was thrown but not caught.
3786 // get_func_tv() returned OK, so that the check for trailing
3787 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003788 if (aborting())
3789 break;
3790 }
3791 if (eap->skip)
3792 --emsg_skip;
3793
Bram Moolenaare51bb172020-02-16 19:42:23 +01003794 // When inside :try we need to check for following "| catch".
3795 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003796 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003797 // Check for trailing illegal characters and a following command.
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003798 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003799 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01003800 if (!failed)
3801 {
3802 emsg_severe = TRUE;
3803 emsg(_(e_trailing));
3804 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003805 }
3806 else
3807 eap->nextcmd = check_nextcmd(arg);
3808 }
3809
3810end:
3811 dict_unref(fudi.fd_dict);
3812 vim_free(tofree);
3813}
3814
3815/*
3816 * Return from a function. Possibly makes the return pending. Also called
3817 * for a pending return at the ":endtry" or after returning from an extra
3818 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3819 * when called due to a ":return" command. "rettv" may point to a typval_T
3820 * with the return rettv. Returns TRUE when the return can be carried out,
3821 * FALSE when the return gets pending.
3822 */
3823 int
3824do_return(
3825 exarg_T *eap,
3826 int reanimate,
3827 int is_cmd,
3828 void *rettv)
3829{
3830 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003831 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003832
3833 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003834 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003835 current_funccal->returned = FALSE;
3836
3837 /*
3838 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3839 * not in its finally clause (which then is to be executed next) is found.
3840 * In this case, make the ":return" pending for execution at the ":endtry".
3841 * Otherwise, return normally.
3842 */
3843 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3844 if (idx >= 0)
3845 {
3846 cstack->cs_pending[idx] = CSTP_RETURN;
3847
3848 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003849 // A pending return again gets pending. "rettv" points to an
3850 // allocated variable with the rettv of the original ":return"'s
3851 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003852 cstack->cs_rettv[idx] = rettv;
3853 else
3854 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003855 // When undoing a return in order to make it pending, get the stored
3856 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003857 if (reanimate)
3858 rettv = current_funccal->rettv;
3859
3860 if (rettv != NULL)
3861 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003862 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003863 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3864 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3865 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003866 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003867 }
3868 else
3869 cstack->cs_rettv[idx] = NULL;
3870
3871 if (reanimate)
3872 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003873 // The pending return value could be overwritten by a ":return"
3874 // without argument in a finally clause; reset the default
3875 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003876 current_funccal->rettv->v_type = VAR_NUMBER;
3877 current_funccal->rettv->vval.v_number = 0;
3878 }
3879 }
3880 report_make_pending(CSTP_RETURN, rettv);
3881 }
3882 else
3883 {
3884 current_funccal->returned = TRUE;
3885
Bram Moolenaare38eab22019-12-05 21:50:01 +01003886 // If the return is carried out now, store the return value. For
3887 // a return immediately after reanimation, the value is already
3888 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003889 if (!reanimate && rettv != NULL)
3890 {
3891 clear_tv(current_funccal->rettv);
3892 *current_funccal->rettv = *(typval_T *)rettv;
3893 if (!is_cmd)
3894 vim_free(rettv);
3895 }
3896 }
3897
3898 return idx < 0;
3899}
3900
3901/*
3902 * Free the variable with a pending return value.
3903 */
3904 void
3905discard_pending_return(void *rettv)
3906{
3907 free_tv((typval_T *)rettv);
3908}
3909
3910/*
3911 * Generate a return command for producing the value of "rettv". The result
3912 * is an allocated string. Used by report_pending() for verbose messages.
3913 */
3914 char_u *
3915get_return_cmd(void *rettv)
3916{
3917 char_u *s = NULL;
3918 char_u *tofree = NULL;
3919 char_u numbuf[NUMBUFLEN];
3920
3921 if (rettv != NULL)
3922 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3923 if (s == NULL)
3924 s = (char_u *)"";
3925
3926 STRCPY(IObuff, ":return ");
3927 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3928 if (STRLEN(s) + 8 >= IOSIZE)
3929 STRCPY(IObuff + IOSIZE - 4, "...");
3930 vim_free(tofree);
3931 return vim_strsave(IObuff);
3932}
3933
3934/*
3935 * Get next function line.
3936 * Called by do_cmdline() to get the next line.
3937 * Returns allocated string, or NULL for end of function.
3938 */
3939 char_u *
3940get_func_line(
3941 int c UNUSED,
3942 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003943 int indent UNUSED,
3944 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003945{
3946 funccall_T *fcp = (funccall_T *)cookie;
3947 ufunc_T *fp = fcp->func;
3948 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003949 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003950
Bram Moolenaare38eab22019-12-05 21:50:01 +01003951 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003952 if (fcp->dbg_tick != debug_tick)
3953 {
3954 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003955 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003956 fcp->dbg_tick = debug_tick;
3957 }
3958#ifdef FEAT_PROFILE
3959 if (do_profiling == PROF_YES)
3960 func_line_end(cookie);
3961#endif
3962
3963 gap = &fp->uf_lines;
3964 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3965 || fcp->returned)
3966 retval = NULL;
3967 else
3968 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003969 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003970 while (fcp->linenr < gap->ga_len
3971 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3972 ++fcp->linenr;
3973 if (fcp->linenr >= gap->ga_len)
3974 retval = NULL;
3975 else
3976 {
3977 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003978 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003979#ifdef FEAT_PROFILE
3980 if (do_profiling == PROF_YES)
3981 func_line_start(cookie);
3982#endif
3983 }
3984 }
3985
Bram Moolenaare38eab22019-12-05 21:50:01 +01003986 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003987 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003988 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003989 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003990 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003991 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003992 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003993 fcp->dbg_tick = debug_tick;
3994 }
3995
3996 return retval;
3997}
3998
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003999/*
4000 * Return TRUE if the currently active function should be ended, because a
4001 * return was encountered or an error occurred. Used inside a ":while".
4002 */
4003 int
4004func_has_ended(void *cookie)
4005{
4006 funccall_T *fcp = (funccall_T *)cookie;
4007
Bram Moolenaare38eab22019-12-05 21:50:01 +01004008 // Ignore the "abort" flag if the abortion behavior has been changed due to
4009 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004010 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4011 || fcp->returned);
4012}
4013
4014/*
4015 * return TRUE if cookie indicates a function which "abort"s on errors.
4016 */
4017 int
4018func_has_abort(
4019 void *cookie)
4020{
4021 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
4022}
4023
4024
4025/*
4026 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4027 * Don't do this when "Func" is already a partial that was bound
4028 * explicitly (pt_auto is FALSE).
4029 * Changes "rettv" in-place.
4030 * Returns the updated "selfdict_in".
4031 */
4032 dict_T *
4033make_partial(dict_T *selfdict_in, typval_T *rettv)
4034{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004035 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004036 char_u *tofree = NULL;
4037 ufunc_T *fp;
4038 char_u fname_buf[FLEN_FIXED + 1];
4039 int error;
4040 dict_T *selfdict = selfdict_in;
4041
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004042 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4043 fp = rettv->vval.v_partial->pt_func;
4044 else
4045 {
4046 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4047 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004048 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004049 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004050 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004051 vim_free(tofree);
4052 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004053
4054 if (fp != NULL && (fp->uf_flags & FC_DICT))
4055 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004056 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004057
4058 if (pt != NULL)
4059 {
4060 pt->pt_refcount = 1;
4061 pt->pt_dict = selfdict;
4062 pt->pt_auto = TRUE;
4063 selfdict = NULL;
4064 if (rettv->v_type == VAR_FUNC)
4065 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004066 // Just a function: Take over the function name and use
4067 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004068 pt->pt_name = rettv->vval.v_string;
4069 }
4070 else
4071 {
4072 partial_T *ret_pt = rettv->vval.v_partial;
4073 int i;
4074
Bram Moolenaare38eab22019-12-05 21:50:01 +01004075 // Partial: copy the function name, use selfdict and copy
4076 // args. Can't take over name or args, the partial might
4077 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004078 if (ret_pt->pt_name != NULL)
4079 {
4080 pt->pt_name = vim_strsave(ret_pt->pt_name);
4081 func_ref(pt->pt_name);
4082 }
4083 else
4084 {
4085 pt->pt_func = ret_pt->pt_func;
4086 func_ptr_ref(pt->pt_func);
4087 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004088 if (ret_pt->pt_argc > 0)
4089 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004090 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004091 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004092 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004093 pt->pt_argc = 0;
4094 else
4095 {
4096 pt->pt_argc = ret_pt->pt_argc;
4097 for (i = 0; i < pt->pt_argc; i++)
4098 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4099 }
4100 }
4101 partial_unref(ret_pt);
4102 }
4103 rettv->v_type = VAR_PARTIAL;
4104 rettv->vval.v_partial = pt;
4105 }
4106 }
4107 return selfdict;
4108}
4109
4110/*
4111 * Return the name of the executed function.
4112 */
4113 char_u *
4114func_name(void *cookie)
4115{
4116 return ((funccall_T *)cookie)->func->uf_name;
4117}
4118
4119/*
4120 * Return the address holding the next breakpoint line for a funccall cookie.
4121 */
4122 linenr_T *
4123func_breakpoint(void *cookie)
4124{
4125 return &((funccall_T *)cookie)->breakpoint;
4126}
4127
4128/*
4129 * Return the address holding the debug tick for a funccall cookie.
4130 */
4131 int *
4132func_dbg_tick(void *cookie)
4133{
4134 return &((funccall_T *)cookie)->dbg_tick;
4135}
4136
4137/*
4138 * Return the nesting level for a funccall cookie.
4139 */
4140 int
4141func_level(void *cookie)
4142{
4143 return ((funccall_T *)cookie)->level;
4144}
4145
4146/*
4147 * Return TRUE when a function was ended by a ":return" command.
4148 */
4149 int
4150current_func_returned(void)
4151{
4152 return current_funccal->returned;
4153}
4154
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004155 int
4156free_unref_funccal(int copyID, int testing)
4157{
4158 int did_free = FALSE;
4159 int did_free_funccal = FALSE;
4160 funccall_T *fc, **pfc;
4161
4162 for (pfc = &previous_funccal; *pfc != NULL; )
4163 {
4164 if (can_free_funccal(*pfc, copyID))
4165 {
4166 fc = *pfc;
4167 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004168 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004169 did_free = TRUE;
4170 did_free_funccal = TRUE;
4171 }
4172 else
4173 pfc = &(*pfc)->caller;
4174 }
4175 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004176 // When a funccal was freed some more items might be garbage
4177 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004178 (void)garbage_collect(testing);
4179
4180 return did_free;
4181}
4182
4183/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004184 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004185 */
4186 static funccall_T *
4187get_funccal(void)
4188{
4189 int i;
4190 funccall_T *funccal;
4191 funccall_T *temp_funccal;
4192
4193 funccal = current_funccal;
4194 if (debug_backtrace_level > 0)
4195 {
4196 for (i = 0; i < debug_backtrace_level; i++)
4197 {
4198 temp_funccal = funccal->caller;
4199 if (temp_funccal)
4200 funccal = temp_funccal;
4201 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004202 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004203 debug_backtrace_level = i;
4204 }
4205 }
4206 return funccal;
4207}
4208
4209/*
4210 * Return the hashtable used for local variables in the current funccal.
4211 * Return NULL if there is no current funccal.
4212 */
4213 hashtab_T *
4214get_funccal_local_ht()
4215{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004217 return NULL;
4218 return &get_funccal()->l_vars.dv_hashtab;
4219}
4220
4221/*
4222 * Return the l: scope variable.
4223 * Return NULL if there is no current funccal.
4224 */
4225 dictitem_T *
4226get_funccal_local_var()
4227{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004229 return NULL;
4230 return &get_funccal()->l_vars_var;
4231}
4232
4233/*
4234 * Return the hashtable used for argument in the current funccal.
4235 * Return NULL if there is no current funccal.
4236 */
4237 hashtab_T *
4238get_funccal_args_ht()
4239{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004241 return NULL;
4242 return &get_funccal()->l_avars.dv_hashtab;
4243}
4244
4245/*
4246 * Return the a: scope variable.
4247 * Return NULL if there is no current funccal.
4248 */
4249 dictitem_T *
4250get_funccal_args_var()
4251{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004253 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004254 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004255}
4256
4257/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004258 * List function variables, if there is a function.
4259 */
4260 void
4261list_func_vars(int *first)
4262{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004264 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004265 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004266}
4267
4268/*
4269 * If "ht" is the hashtable for local variables in the current funccal, return
4270 * the dict that contains it.
4271 * Otherwise return NULL.
4272 */
4273 dict_T *
4274get_current_funccal_dict(hashtab_T *ht)
4275{
4276 if (current_funccal != NULL
4277 && ht == &current_funccal->l_vars.dv_hashtab)
4278 return &current_funccal->l_vars;
4279 return NULL;
4280}
4281
4282/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004283 * Search hashitem in parent scope.
4284 */
4285 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004286find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004287{
4288 funccall_T *old_current_funccal = current_funccal;
4289 hashtab_T *ht;
4290 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004291 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004292
4293 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4294 return NULL;
4295
Bram Moolenaare38eab22019-12-05 21:50:01 +01004296 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004297 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004298 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004299 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004300 ht = find_var_ht(name, &varname);
4301 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004302 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004303 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004304 if (!HASHITEM_EMPTY(hi))
4305 {
4306 *pht = ht;
4307 break;
4308 }
4309 }
4310 if (current_funccal == current_funccal->func->uf_scoped)
4311 break;
4312 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004313 }
4314 current_funccal = old_current_funccal;
4315
4316 return hi;
4317}
4318
4319/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004320 * Search variable in parent scope.
4321 */
4322 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004323find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004324{
4325 dictitem_T *v = NULL;
4326 funccall_T *old_current_funccal = current_funccal;
4327 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004328 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004329
4330 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4331 return NULL;
4332
Bram Moolenaare38eab22019-12-05 21:50:01 +01004333 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004334 current_funccal = current_funccal->func->uf_scoped;
4335 while (current_funccal)
4336 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004337 ht = find_var_ht(name, &varname);
4338 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004339 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004340 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004341 if (v != NULL)
4342 break;
4343 }
4344 if (current_funccal == current_funccal->func->uf_scoped)
4345 break;
4346 current_funccal = current_funccal->func->uf_scoped;
4347 }
4348 current_funccal = old_current_funccal;
4349
4350 return v;
4351}
4352
4353/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004354 * Set "copyID + 1" in previous_funccal and callers.
4355 */
4356 int
4357set_ref_in_previous_funccal(int copyID)
4358{
4359 int abort = FALSE;
4360 funccall_T *fc;
4361
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004362 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004363 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004364 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004365 abort = abort
4366 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4367 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004368 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004369 }
4370 return abort;
4371}
4372
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004373 static int
4374set_ref_in_funccal(funccall_T *fc, int copyID)
4375{
4376 int abort = FALSE;
4377
4378 if (fc->fc_copyID != copyID)
4379 {
4380 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004381 abort = abort
4382 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4383 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004384 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004385 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004386 }
4387 return abort;
4388}
4389
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004390/*
4391 * Set "copyID" in all local vars and arguments in the call stack.
4392 */
4393 int
4394set_ref_in_call_stack(int copyID)
4395{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004396 int abort = FALSE;
4397 funccall_T *fc;
4398 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004399
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004400 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004401 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004402
4403 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004404 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4405 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004406 abort = abort || set_ref_in_funccal(fc, copyID);
4407
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004408 return abort;
4409}
4410
4411/*
4412 * Set "copyID" in all functions available by name.
4413 */
4414 int
4415set_ref_in_functions(int copyID)
4416{
4417 int todo;
4418 hashitem_T *hi = NULL;
4419 int abort = FALSE;
4420 ufunc_T *fp;
4421
4422 todo = (int)func_hashtab.ht_used;
4423 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004424 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004425 if (!HASHITEM_EMPTY(hi))
4426 {
4427 --todo;
4428 fp = HI2UF(hi);
4429 if (!func_name_refcount(fp->uf_name))
4430 abort = abort || set_ref_in_func(NULL, fp, copyID);
4431 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004432 }
4433 return abort;
4434}
4435
4436/*
4437 * Set "copyID" in all function arguments.
4438 */
4439 int
4440set_ref_in_func_args(int copyID)
4441{
4442 int i;
4443 int abort = FALSE;
4444
4445 for (i = 0; i < funcargs.ga_len; ++i)
4446 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4447 copyID, NULL, NULL);
4448 return abort;
4449}
4450
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004451/*
4452 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004453 * Returns TRUE if setting references failed somehow.
4454 */
4455 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004456set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004457{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004458 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004459 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004460 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004461 char_u fname_buf[FLEN_FIXED + 1];
4462 char_u *tofree = NULL;
4463 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004464 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004465
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004466 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004467 return FALSE;
4468
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004469 if (fp_in == NULL)
4470 {
4471 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004472 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004473 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004474 if (fp != NULL)
4475 {
4476 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004477 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004478 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004479
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004480 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004481 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004482}
4483
Bram Moolenaare38eab22019-12-05 21:50:01 +01004484#endif // FEAT_EVAL