blob: 4dac28126f99ba3721b747fb7be154df4e137232 [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;
242 if (eval1(&p, &rettv, FALSE) != FAIL)
243 {
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 Moolenaara9b579f2016-07-17 18:29:19 +0200332 * Parse a lambda expression and get a Funcref from "*arg".
333 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
334 */
335 int
336get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
337{
338 garray_T newargs;
339 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200340 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200341 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100342 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200343 int varargs;
344 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200345 char_u *start = skipwhite(*arg + 1);
346 char_u *s, *e;
347 static int lambda_no = 0;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200348 int *old_eval_lavars = eval_lavars_used;
349 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200350
351 ga_init(&newargs);
352 ga_init(&newlines);
353
Bram Moolenaare38eab22019-12-05 21:50:01 +0100354 // First, check if this is a lambda expression. "->" must exist.
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200355 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE,
356 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200357 if (ret == FAIL || *start != '>')
358 return NOTDONE;
359
Bram Moolenaare38eab22019-12-05 21:50:01 +0100360 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200361 if (evaluate)
362 pnewargs = &newargs;
363 else
364 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200365 *arg = skipwhite(*arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366 // TODO: argument types
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200367 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE,
368 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200369 if (ret == FAIL || **arg != '>')
370 goto errret;
371
Bram Moolenaare38eab22019-12-05 21:50:01 +0100372 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200373 if (evaluate)
374 eval_lavars_used = &eval_lavars;
375
Bram Moolenaare38eab22019-12-05 21:50:01 +0100376 // Get the start and the end of the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200377 *arg = skipwhite(*arg + 1);
378 s = *arg;
379 ret = skip_expr(arg);
380 if (ret == FAIL)
381 goto errret;
382 e = *arg;
383 *arg = skipwhite(*arg);
384 if (**arg != '}')
Bram Moolenaaree619e52020-03-28 21:38:06 +0100385 {
386 semsg(_("E451: Expected }: %s"), *arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200387 goto errret;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100388 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200389 ++*arg;
390
391 if (evaluate)
392 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200393 int len, flags = 0;
394 char_u *p;
395 char_u name[20];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200396
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200397 sprintf((char*)name, "<lambda>%d", ++lambda_no);
398
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200399 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200400 if (fp == NULL)
401 goto errret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100402 fp->uf_dfunc_idx = -1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200403 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200404 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200405 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200406
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200407 ga_init2(&newlines, (int)sizeof(char_u *), 1);
408 if (ga_grow(&newlines, 1) == FAIL)
409 goto errret;
410
Bram Moolenaare38eab22019-12-05 21:50:01 +0100411 // Add "return " before the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200412 len = 7 + e - s + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200413 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200414 if (p == NULL)
415 goto errret;
416 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
417 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200418 vim_strncpy(p + 7, s, e - s);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200419 if (strstr((char *)p + 7, "a:") == NULL)
420 // No a: variables are used for sure.
421 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200422
423 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100424 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200425 hash_add(&func_hashtab, UF2HIKEY(fp));
426 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200427 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200428 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200429 if (current_funccal != NULL && eval_lavars)
430 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200431 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200432 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200433 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200434 }
435 else
436 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200437
438#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200439 if (prof_def_func())
440 func_do_profile(fp);
441#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200442 if (sandbox)
443 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100444 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200445 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200446 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200447 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200448 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100449 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200450
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200451 pt->pt_func = fp;
452 pt->pt_refcount = 1;
453 rettv->vval.v_partial = pt;
454 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200455 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200456
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200457 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200458 return OK;
459
460errret:
461 ga_clear_strings(&newargs);
462 ga_clear_strings(&newlines);
463 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100464 vim_free(pt);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200465 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200466 return FAIL;
467}
468
469/*
470 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
471 * name it contains, otherwise return "name".
472 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
473 * "partialp".
474 */
475 char_u *
476deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
477{
478 dictitem_T *v;
479 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200480 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200481
482 if (partialp != NULL)
483 *partialp = NULL;
484
485 cc = name[*lenp];
486 name[*lenp] = NUL;
487 v = find_var(name, NULL, no_autoload);
488 name[*lenp] = cc;
489 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
490 {
491 if (v->di_tv.vval.v_string == NULL)
492 {
493 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100494 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200495 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200496 s = v->di_tv.vval.v_string;
497 *lenp = (int)STRLEN(s);
498 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200499 }
500
501 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
502 {
503 partial_T *pt = v->di_tv.vval.v_partial;
504
505 if (pt == NULL)
506 {
507 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100508 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200509 }
510 if (partialp != NULL)
511 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200512 s = partial_name(pt);
513 *lenp = (int)STRLEN(s);
514 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200515 }
516
517 return name;
518}
519
520/*
521 * Give an error message with a function name. Handle <SNR> things.
522 * "ermsg" is to be passed without translation, use N_() instead of _().
523 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100524 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200525emsg_funcname(char *ermsg, char_u *name)
526{
527 char_u *p;
528
529 if (*name == K_SPECIAL)
530 p = concat_str((char_u *)"<SNR>", name + 3);
531 else
532 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100533 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200534 if (p != name)
535 vim_free(p);
536}
537
538/*
539 * Allocate a variable for the result of a function.
540 * Return OK or FAIL.
541 */
542 int
543get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200544 char_u *name, // name of the function
545 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200546 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200547 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200548 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200549{
550 char_u *argp;
551 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100552 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
553 int argcount = 0; // number of arguments found
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200554
555 /*
556 * Get the arguments.
557 */
558 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200559 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
560 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200561 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100562 argp = skipwhite(argp + 1); // skip the '(' or ','
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200563 if (*argp == ')' || *argp == ',' || *argp == NUL)
564 break;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200565 if (eval1(&argp, &argvars[argcount], funcexe->evaluate) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200566 {
567 ret = FAIL;
568 break;
569 }
570 ++argcount;
571 if (*argp != ',')
572 break;
573 }
574 if (*argp == ')')
575 ++argp;
576 else
577 ret = FAIL;
578
579 if (ret == OK)
580 {
581 int i = 0;
582
583 if (get_vim_var_nr(VV_TESTING))
584 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100585 // Prepare for calling test_garbagecollect_now(), need to know
586 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200587 if (funcargs.ga_itemsize == 0)
588 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
589 for (i = 0; i < argcount; ++i)
590 if (ga_grow(&funcargs, 1) == OK)
591 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
592 &argvars[i];
593 }
594
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200595 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200596
597 funcargs.ga_len -= i;
598 }
599 else if (!aborting())
600 {
601 if (argcount == MAX_FUNC_ARGS)
602 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
603 else
604 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
605 }
606
607 while (--argcount >= 0)
608 clear_tv(&argvars[argcount]);
609
610 *arg = skipwhite(argp);
611 return ret;
612}
613
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200614/*
615 * Return TRUE if "p" starts with "<SID>" or "s:".
616 * Only works if eval_fname_script() returned non-zero for "p"!
617 */
618 static int
619eval_fname_sid(char_u *p)
620{
621 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
622}
623
624/*
625 * In a script change <SID>name() and s:name() to K_SNR 123_name().
626 * Change <SNR>123_name() to K_SNR 123_name().
627 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
628 * (slow).
629 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100630 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200631fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
632{
633 int llen;
634 char_u *fname;
635 int i;
636
637 llen = eval_fname_script(name);
638 if (llen > 0)
639 {
640 fname_buf[0] = K_SPECIAL;
641 fname_buf[1] = KS_EXTRA;
642 fname_buf[2] = (int)KE_SNR;
643 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100644 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200645 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200646 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100647 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200648 else
649 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200650 sprintf((char *)fname_buf + 3, "%ld_",
651 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200652 i = (int)STRLEN(fname_buf);
653 }
654 }
655 if (i + STRLEN(name + llen) < FLEN_FIXED)
656 {
657 STRCPY(fname_buf + i, name + llen);
658 fname = fname_buf;
659 }
660 else
661 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200662 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200663 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100664 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200665 else
666 {
667 *tofree = fname;
668 mch_memmove(fname, fname_buf, (size_t)i);
669 STRCPY(fname + i, name + llen);
670 }
671 }
672 }
673 else
674 fname = name;
675 return fname;
676}
677
678/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 * Find a function "name" in script "sid".
680 */
681 static ufunc_T *
682find_func_with_sid(char_u *name, int sid)
683{
684 hashitem_T *hi;
685 char_u buffer[200];
686
687 buffer[0] = K_SPECIAL;
688 buffer[1] = KS_EXTRA;
689 buffer[2] = (int)KE_SNR;
690 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
691 (long)sid, name);
692 hi = hash_find(&func_hashtab, buffer);
693 if (!HASHITEM_EMPTY(hi))
694 return HI2UF(hi);
695
696 return NULL;
697}
698
699/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200700 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200701 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200702 * Return NULL for unknown function.
703 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100704 static ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200705find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200706{
707 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708 ufunc_T *func;
709 imported_T *imported;
710
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200711 if (in_vim9script() && !is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100712 {
713 // Find script-local function before global one.
714 func = find_func_with_sid(name, current_sctx.sc_sid);
715 if (func != NULL)
716 return func;
717
718 // Find imported funcion before global one.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +0100719 imported = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 if (imported != NULL && imported->imp_funcname != NULL)
721 {
722 hi = hash_find(&func_hashtab, imported->imp_funcname);
723 if (!HASHITEM_EMPTY(hi))
724 return HI2UF(hi);
725 }
726 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200727
Bram Moolenaara26b9702020-04-18 19:53:28 +0200728 hi = hash_find(&func_hashtab,
729 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200730 if (!HASHITEM_EMPTY(hi))
731 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732
733 return NULL;
734}
735
736/*
737 * Find a function by name, return pointer to it in ufuncs.
738 * "cctx" is passed in a :def function to find imported functions.
739 * Return NULL for unknown or dead function.
740 */
741 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200742find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200744 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100745
746 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
747 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200748 return NULL;
749}
750
751/*
752 * Copy the function name of "fp" to buffer "buf".
753 * "buf" must be able to hold the function name plus three bytes.
754 * Takes care of script-local function names.
755 */
756 static void
757cat_func_name(char_u *buf, ufunc_T *fp)
758{
759 if (fp->uf_name[0] == K_SPECIAL)
760 {
761 STRCPY(buf, "<SNR>");
762 STRCAT(buf, fp->uf_name + 3);
763 }
764 else
765 STRCPY(buf, fp->uf_name);
766}
767
768/*
769 * Add a number variable "name" to dict "dp" with value "nr".
770 */
771 static void
772add_nr_var(
773 dict_T *dp,
774 dictitem_T *v,
775 char *name,
776 varnumber_T nr)
777{
778 STRCPY(v->di_key, name);
779 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
780 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
781 v->di_tv.v_type = VAR_NUMBER;
782 v->di_tv.v_lock = VAR_FIXED;
783 v->di_tv.vval.v_number = nr;
784}
785
786/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100787 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200788 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100789 static void
790free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200791{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100792 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200793
794 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
795 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100796 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200797
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100798 // When garbage collecting a funccall_T may be freed before the
799 // function that references it, clear its uf_scoped field.
800 // The function may have been redefined and point to another
801 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200802 if (fp != NULL && fp->uf_scoped == fc)
803 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200804 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200805 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200806
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200807 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200808 vim_free(fc);
809}
810
811/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100812 * Free "fc" and what it contains.
813 * Can be called only when "fc" is kept beyond the period of it called,
814 * i.e. after cleanup_function_call(fc).
815 */
816 static void
817free_funccal_contents(funccall_T *fc)
818{
819 listitem_T *li;
820
821 // Free all l: variables.
822 vars_clear(&fc->l_vars.dv_hashtab);
823
824 // Free all a: variables.
825 vars_clear(&fc->l_avars.dv_hashtab);
826
827 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200828 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100829 clear_tv(&li->li_tv);
830
831 free_funccal(fc);
832}
833
834/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200835 * Handle the last part of returning from a function: free the local hashtable.
836 * Unless it is still in use by a closure.
837 */
838 static void
839cleanup_function_call(funccall_T *fc)
840{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100841 int may_free_fc = fc->fc_refcount <= 0;
842 int free_fc = TRUE;
843
Bram Moolenaar6914c642017-04-01 21:21:30 +0200844 current_funccal = fc->caller;
845
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100846 // Free all l: variables if not referred.
847 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
848 vars_clear(&fc->l_vars.dv_hashtab);
849 else
850 free_fc = FALSE;
851
852 // If the a:000 list and the l: and a: dicts are not referenced and
853 // there is no closure using it, we can free the funccall_T and what's
854 // in it.
855 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
856 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200857 else
858 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100859 int todo;
860 hashitem_T *hi;
861 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200862
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100863 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200864
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100865 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200866 todo = (int)fc->l_avars.dv_hashtab.ht_used;
867 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
868 {
869 if (!HASHITEM_EMPTY(hi))
870 {
871 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100872 di = HI2DI(hi);
873 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200874 }
875 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100876 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200877
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100878 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
879 fc->l_varlist.lv_first = NULL;
880 else
881 {
882 listitem_T *li;
883
884 free_fc = FALSE;
885
886 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200887 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +0200888 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100889 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100890
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100891 if (free_fc)
892 free_funccal(fc);
893 else
894 {
895 static int made_copy = 0;
896
897 // "fc" is still in use. This can happen when returning "a:000",
898 // assigning "l:" to a global variable or defining a closure.
899 // Link "fc" in the list for garbage collection later.
900 fc->caller = previous_funccal;
901 previous_funccal = fc;
902
903 if (want_garbage_collect)
904 // If garbage collector is ready, clear count.
905 made_copy = 0;
906 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100907 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100908 // We have made a lot of copies, worth 4 Mbyte. This can happen
909 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100910 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100911 // too much memory.
912 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100913 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100914 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200915 }
916}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917/*
918 * Unreference "fc": decrement the reference count and free it when it
919 * becomes zero. "fp" is detached from "fc".
920 * When "force" is TRUE we are exiting.
921 */
922 static void
923funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
924{
925 funccall_T **pfc;
926 int i;
927
928 if (fc == NULL)
929 return;
930
931 if (--fc->fc_refcount <= 0 && (force || (
932 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
933 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
934 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
935 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
936 {
937 if (fc == *pfc)
938 {
939 *pfc = fc->caller;
940 free_funccal_contents(fc);
941 return;
942 }
943 }
944 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
945 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
946 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
947}
948
949/*
950 * Remove the function from the function hashtable. If the function was
951 * deleted while it still has references this was already done.
952 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
953 */
954 static int
955func_remove(ufunc_T *fp)
956{
957 hashitem_T *hi;
958
959 // Return if it was already virtually deleted.
960 if (fp->uf_flags & FC_DEAD)
961 return FALSE;
962
963 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
964 if (!HASHITEM_EMPTY(hi))
965 {
966 // When there is a def-function index do not actually remove the
967 // function, so we can find the index when defining the function again.
968 if (fp->uf_dfunc_idx >= 0)
969 fp->uf_flags |= FC_DEAD;
970 else
971 hash_remove(&func_hashtab, hi);
972 return TRUE;
973 }
974 return FALSE;
975}
976
977 static void
978func_clear_items(ufunc_T *fp)
979{
980 ga_clear_strings(&(fp->uf_args));
981 ga_clear_strings(&(fp->uf_def_args));
982 ga_clear_strings(&(fp->uf_lines));
983 VIM_CLEAR(fp->uf_name_exp);
984 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100985 VIM_CLEAR(fp->uf_def_arg_idx);
986 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200987 while (fp->uf_type_list.ga_len > 0)
988 vim_free(((type_T **)fp->uf_type_list.ga_data)
989 [--fp->uf_type_list.ga_len]);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 ga_clear(&fp->uf_type_list);
991#ifdef FEAT_PROFILE
992 VIM_CLEAR(fp->uf_tml_count);
993 VIM_CLEAR(fp->uf_tml_total);
994 VIM_CLEAR(fp->uf_tml_self);
995#endif
996}
997
998/*
999 * Free all things that a function contains. Does not free the function
1000 * itself, use func_free() for that.
1001 * When "force" is TRUE we are exiting.
1002 */
1003 static void
1004func_clear(ufunc_T *fp, int force)
1005{
1006 if (fp->uf_cleared)
1007 return;
1008 fp->uf_cleared = TRUE;
1009
1010 // clear this function
1011 func_clear_items(fp);
1012 funccal_unref(fp->uf_scoped, fp, force);
1013 delete_def_function(fp);
1014}
1015
1016/*
1017 * Free a function and remove it from the list of functions. Does not free
1018 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001019 * When "force" is TRUE we are exiting.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 */
1021 static void
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001022func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023{
1024 // Only remove it when not done already, otherwise we would remove a newer
1025 // version of the function with the same name.
1026 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1027 func_remove(fp);
1028
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001029 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 vim_free(fp);
1031}
1032
1033/*
1034 * Free all things that a function contains and free the function itself.
1035 * When "force" is TRUE we are exiting.
1036 */
1037 static void
1038func_clear_free(ufunc_T *fp, int force)
1039{
1040 func_clear(fp, force);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001041 func_free(fp, force);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042}
1043
Bram Moolenaar6914c642017-04-01 21:21:30 +02001044
1045/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001046 * Call a user function.
1047 */
1048 static void
1049call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001050 ufunc_T *fp, // pointer to function
1051 int argcount, // nr of args
1052 typval_T *argvars, // arguments
1053 typval_T *rettv, // return value
1054 linenr_T firstline, // first line of range
1055 linenr_T lastline, // last line of range
1056 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001057{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001058 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001059 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001060 funccall_T *fc;
1061 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001062 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001063 static int depth = 0;
1064 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001065 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001066 int i;
1067 int ai;
1068 int islambda = FALSE;
1069 char_u numbuf[NUMBUFLEN];
1070 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001071#ifdef FEAT_PROFILE
1072 proftime_T wait_start;
1073 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001074 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001075#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001076 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001077
Bram Moolenaare38eab22019-12-05 21:50:01 +01001078 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001079 if (depth >= p_mfd)
1080 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001081 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001082 rettv->v_type = VAR_NUMBER;
1083 rettv->vval.v_number = -1;
1084 return;
1085 }
1086 ++depth;
1087
Bram Moolenaare38eab22019-12-05 21:50:01 +01001088 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001089
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001090 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001091 if (fc == NULL)
1092 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001093 fc->caller = current_funccal;
1094 current_funccal = fc;
1095 fc->func = fp;
1096 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001097 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001098 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001099 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1100 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001101 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001102 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001103 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 if (fp->uf_dfunc_idx >= 0)
1106 {
1107 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001108 save_current_sctx = current_sctx;
1109 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110
1111 // Execute the compiled function.
1112 call_def_function(fp, argcount, argvars, rettv);
1113 --depth;
1114 current_funccal = fc->caller;
1115
1116 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001117 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 free_funccal(fc);
1119 return;
1120 }
1121
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001122 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1123 islambda = TRUE;
1124
1125 /*
1126 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1127 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1128 * each argument variable and saves a lot of time.
1129 */
1130 /*
1131 * Init l: variables.
1132 */
1133 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1134 if (selfdict != NULL)
1135 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001136 // Set l:self to "selfdict". Use "name" to avoid a warning from
1137 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001138 v = &fc->fixvar[fixvar_idx++].var;
1139 name = v->di_key;
1140 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001141 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001142 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1143 v->di_tv.v_type = VAR_DICT;
1144 v->di_tv.v_lock = 0;
1145 v->di_tv.vval.v_dict = selfdict;
1146 ++selfdict->dv_refcount;
1147 }
1148
1149 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001150 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001151 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001152 * Set a:000 to a list with room for the "..." arguments.
1153 */
1154 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001155 if ((fp->uf_flags & FC_NOARGS) == 0)
1156 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001157 (varnumber_T)(argcount >= fp->uf_args.ga_len
1158 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001159 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001160 if ((fp->uf_flags & FC_NOARGS) == 0)
1161 {
1162 // Use "name" to avoid a warning from some compiler that checks the
1163 // destination size.
1164 v = &fc->fixvar[fixvar_idx++].var;
1165 name = v->di_key;
1166 STRCPY(name, "000");
1167 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1168 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1169 v->di_tv.v_type = VAR_LIST;
1170 v->di_tv.v_lock = VAR_FIXED;
1171 v->di_tv.vval.v_list = &fc->l_varlist;
1172 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001173 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001174 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1175 fc->l_varlist.lv_lock = VAR_FIXED;
1176
1177 /*
1178 * Set a:firstline to "firstline" and a:lastline to "lastline".
1179 * Set a:name to named arguments.
1180 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001181 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001182 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001183 if ((fp->uf_flags & FC_NOARGS) == 0)
1184 {
1185 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001186 (varnumber_T)firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001187 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001188 (varnumber_T)lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001189 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001190 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001191 {
1192 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001193 typval_T def_rettv;
1194 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001195
1196 ai = i - fp->uf_args.ga_len;
1197 if (ai < 0)
1198 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001199 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001200 name = FUNCARG(fp, i);
1201 if (islambda)
1202 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001203
1204 // evaluate named argument default expression
1205 isdefault = ai + fp->uf_def_args.ga_len >= 0
1206 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1207 && argvars[i].vval.v_number == VVAL_NONE));
1208 if (isdefault)
1209 {
1210 char_u *default_expr = NULL;
1211 def_rettv.v_type = VAR_NUMBER;
1212 def_rettv.vval.v_number = -1;
1213
1214 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1215 [ai + fp->uf_def_args.ga_len];
1216 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL)
1217 {
1218 default_arg_err = 1;
1219 break;
1220 }
1221 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001222 }
1223 else
1224 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001225 if ((fp->uf_flags & FC_NOARGS) != 0)
1226 // Bail out if no a: arguments used (in lambda).
1227 break;
1228
Bram Moolenaare38eab22019-12-05 21:50:01 +01001229 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001230 sprintf((char *)numbuf, "%d", ai + 1);
1231 name = numbuf;
1232 }
1233 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1234 {
1235 v = &fc->fixvar[fixvar_idx++].var;
1236 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001237 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001238 }
1239 else
1240 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001241 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001242 if (v == NULL)
1243 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001244 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001245 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001246
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001247 // Note: the values are copied directly to avoid alloc/free.
1248 // "argvars" must have VAR_FIXED for v_lock.
1249 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001250 v->di_tv.v_lock = VAR_FIXED;
1251
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001252 if (addlocal)
1253 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001254 // Named arguments should be accessed without the "a:" prefix in
1255 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001256 copy_tv(&v->di_tv, &v->di_tv);
1257 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001258 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001259 else
1260 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001261
1262 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1263 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001264 listitem_T *li = &fc->l_listitems[ai];
1265
1266 li->li_tv = argvars[i];
1267 li->li_tv.v_lock = VAR_FIXED;
1268 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001269 }
1270 }
1271
Bram Moolenaare38eab22019-12-05 21:50:01 +01001272 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001273 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001274
1275 if (fp->uf_flags & FC_SANDBOX)
1276 {
1277 using_sandbox = TRUE;
1278 ++sandbox;
1279 }
1280
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001281 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001282 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001283 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001284 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001285 ++no_wait_return;
1286 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001287
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001288 smsg(_("calling %s"), SOURCING_NAME);
1289 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001290 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001291 char_u buf[MSG_BUF_LEN];
1292 char_u numbuf2[NUMBUFLEN];
1293 char_u *tofree;
1294 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001295
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001296 msg_puts("(");
1297 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001298 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001299 if (i > 0)
1300 msg_puts(", ");
1301 if (argvars[i].v_type == VAR_NUMBER)
1302 msg_outnum((long)argvars[i].vval.v_number);
1303 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001304 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001305 // Do not want errors such as E724 here.
1306 ++emsg_off;
1307 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1308 --emsg_off;
1309 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001310 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001311 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001312 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001313 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1314 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001315 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001316 msg_puts((char *)s);
1317 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001318 }
1319 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001320 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001321 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001322 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001323 msg_puts("\n"); // don't overwrite this either
1324
1325 verbose_leave_scroll();
1326 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001327 }
1328#ifdef FEAT_PROFILE
1329 if (do_profiling == PROF_YES)
1330 {
1331 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001332 {
1333 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001334 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001335 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001336 if (fp->uf_profiling
1337 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1338 {
1339 ++fp->uf_tm_count;
1340 profile_start(&call_start);
1341 profile_zero(&fp->uf_tm_children);
1342 }
1343 script_prof_save(&wait_start);
1344 }
1345#endif
1346
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001347 save_current_sctx = current_sctx;
1348 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001349 save_did_emsg = did_emsg;
1350 did_emsg = FALSE;
1351
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001352 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1353 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001354 else if (islambda)
1355 {
1356 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1357
1358 // A Lambda always has the command "return {expr}". It is much faster
1359 // to evaluate {expr} directly.
1360 ++ex_nesting_level;
Bram Moolenaard1e9dc22020-04-03 18:13:57 +02001361 (void)eval1(&p, rettv, TRUE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001362 --ex_nesting_level;
1363 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001364 else
1365 // call do_cmdline() to execute the lines
1366 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001367 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1368
1369 --RedrawingDisabled;
1370
Bram Moolenaare38eab22019-12-05 21:50:01 +01001371 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001372 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1373 {
1374 clear_tv(rettv);
1375 rettv->v_type = VAR_NUMBER;
1376 rettv->vval.v_number = -1;
1377 }
1378
1379#ifdef FEAT_PROFILE
1380 if (do_profiling == PROF_YES && (fp->uf_profiling
1381 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1382 {
1383 profile_end(&call_start);
1384 profile_sub_wait(&wait_start, &call_start);
1385 profile_add(&fp->uf_tm_total, &call_start);
1386 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1387 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1388 {
1389 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1390 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1391 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001392 if (started_profiling)
1393 // make a ":profdel func" stop profiling the function
1394 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001395 }
1396#endif
1397
Bram Moolenaare38eab22019-12-05 21:50:01 +01001398 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001399 if (p_verbose >= 12)
1400 {
1401 ++no_wait_return;
1402 verbose_enter_scroll();
1403
1404 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001405 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001406 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001407 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001408 (long)fc->rettv->vval.v_number);
1409 else
1410 {
1411 char_u buf[MSG_BUF_LEN];
1412 char_u numbuf2[NUMBUFLEN];
1413 char_u *tofree;
1414 char_u *s;
1415
Bram Moolenaare38eab22019-12-05 21:50:01 +01001416 // The value may be very long. Skip the middle part, so that we
1417 // have some idea how it starts and ends. smsg() would always
1418 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001419 ++emsg_off;
1420 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1421 --emsg_off;
1422 if (s != NULL)
1423 {
1424 if (vim_strsize(s) > MSG_BUF_CLEN)
1425 {
1426 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1427 s = buf;
1428 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001429 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001430 vim_free(tofree);
1431 }
1432 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001433 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001434
1435 verbose_leave_scroll();
1436 --no_wait_return;
1437 }
1438
Bram Moolenaare31ee862020-01-07 20:59:34 +01001439 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001440 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001441 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001442#ifdef FEAT_PROFILE
1443 if (do_profiling == PROF_YES)
1444 script_prof_restore(&wait_start);
1445#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001446 if (using_sandbox)
1447 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001448
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001449 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001450 {
1451 ++no_wait_return;
1452 verbose_enter_scroll();
1453
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001454 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001455 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001456
1457 verbose_leave_scroll();
1458 --no_wait_return;
1459 }
1460
1461 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001462 --depth;
1463
Bram Moolenaar6914c642017-04-01 21:21:30 +02001464 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001465}
1466
1467/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001469 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 int
1471call_user_func_check(
1472 ufunc_T *fp,
1473 int argcount,
1474 typval_T *argvars,
1475 typval_T *rettv,
1476 funcexe_T *funcexe,
1477 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001478{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 int error;
1480 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1483 *funcexe->doesrange = TRUE;
1484 if (argcount < regular_args - fp->uf_def_args.ga_len)
1485 error = FCERR_TOOFEW;
1486 else if (!has_varargs(fp) && argcount > regular_args)
1487 error = FCERR_TOOMANY;
1488 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1489 error = FCERR_DICT;
1490 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001491 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 int did_save_redo = FALSE;
1493 save_redo_T save_redo;
1494
1495 /*
1496 * Call the user function.
1497 * Save and restore search patterns, script variables and
1498 * redo buffer.
1499 */
1500 save_search_patterns();
1501 if (!ins_compl_active())
1502 {
1503 saveRedobuff(&save_redo);
1504 did_save_redo = TRUE;
1505 }
1506 ++fp->uf_calls;
1507 call_user_func(fp, argcount, argvars, rettv,
1508 funcexe->firstline, funcexe->lastline,
1509 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
1510 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1511 // Function was unreferenced while being used, free it now.
1512 func_clear_free(fp, FALSE);
1513 if (did_save_redo)
1514 restoreRedobuff(&save_redo);
1515 restore_search_patterns();
1516 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001517 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001519}
1520
1521/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001522 * There are two kinds of function names:
1523 * 1. ordinary names, function defined with :function
1524 * 2. numbered functions and lambdas
1525 * For the first we only count the name stored in func_hashtab as a reference,
1526 * using function() does not count as a reference, because the function is
1527 * looked up by name.
1528 */
1529 static int
1530func_name_refcount(char_u *name)
1531{
1532 return isdigit(*name) || *name == '<';
1533}
1534
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001535static funccal_entry_T *funccal_stack = NULL;
1536
1537/*
1538 * Save the current function call pointer, and set it to NULL.
1539 * Used when executing autocommands and for ":source".
1540 */
1541 void
1542save_funccal(funccal_entry_T *entry)
1543{
1544 entry->top_funccal = current_funccal;
1545 entry->next = funccal_stack;
1546 funccal_stack = entry;
1547 current_funccal = NULL;
1548}
1549
1550 void
1551restore_funccal(void)
1552{
1553 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001554 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001555 else
1556 {
1557 current_funccal = funccal_stack->top_funccal;
1558 funccal_stack = funccal_stack->next;
1559 }
1560}
1561
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001562 funccall_T *
1563get_current_funccal(void)
1564{
1565 return current_funccal;
1566}
1567
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001568/*
1569 * Mark all functions of script "sid" as deleted.
1570 */
1571 void
1572delete_script_functions(int sid)
1573{
1574 hashitem_T *hi;
1575 ufunc_T *fp;
1576 long_u todo;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02001577 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001578 size_t len;
1579
1580 buf[0] = K_SPECIAL;
1581 buf[1] = KS_EXTRA;
1582 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02001583 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001584 len = STRLEN(buf);
1585
1586 todo = func_hashtab.ht_used;
1587 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1588 if (!HASHITEM_EMPTY(hi))
1589 {
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001590 fp = HI2UF(hi);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001591 if (STRNCMP(fp->uf_name, buf, len) == 0)
1592 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001593 fp->uf_flags |= FC_DEAD;
1594 func_clear(fp, TRUE);
1595 }
1596 --todo;
1597 }
1598}
1599
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001600#if defined(EXITFREE) || defined(PROTO)
1601 void
1602free_all_functions(void)
1603{
1604 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001605 ufunc_T *fp;
1606 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001607 long_u todo = 1;
1608 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001609
Bram Moolenaare38eab22019-12-05 21:50:01 +01001610 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001611 while (current_funccal != NULL)
1612 {
1613 clear_tv(current_funccal->rettv);
1614 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001615 if (current_funccal == NULL && funccal_stack != NULL)
1616 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001617 }
1618
Bram Moolenaare38eab22019-12-05 21:50:01 +01001619 // First clear what the functions contain. Since this may lower the
1620 // reference count of a function, it may also free a function and change
1621 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001622 while (todo > 0)
1623 {
1624 todo = func_hashtab.ht_used;
1625 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1626 if (!HASHITEM_EMPTY(hi))
1627 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 // clear the def function index now
1629 fp = HI2UF(hi);
1630 fp->uf_flags &= ~FC_DEAD;
1631 fp->uf_dfunc_idx = -1;
1632
Bram Moolenaare38eab22019-12-05 21:50:01 +01001633 // Only free functions that are not refcounted, those are
1634 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001635 if (func_name_refcount(fp->uf_name))
1636 ++skipped;
1637 else
1638 {
1639 used = func_hashtab.ht_used;
1640 func_clear(fp, TRUE);
1641 if (used != func_hashtab.ht_used)
1642 {
1643 skipped = 0;
1644 break;
1645 }
1646 }
1647 --todo;
1648 }
1649 }
1650
Bram Moolenaare38eab22019-12-05 21:50:01 +01001651 // Now actually free the functions. Need to start all over every time,
1652 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001653 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001654 while (func_hashtab.ht_used > skipped)
1655 {
1656 todo = func_hashtab.ht_used;
1657 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001658 if (!HASHITEM_EMPTY(hi))
1659 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001660 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001661 // Only free functions that are not refcounted, those are
1662 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001663 fp = HI2UF(hi);
1664 if (func_name_refcount(fp->uf_name))
1665 ++skipped;
1666 else
1667 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001668 func_free(fp, FALSE);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001669 skipped = 0;
1670 break;
1671 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001672 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001673 }
1674 if (skipped == 0)
1675 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676
1677 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001678}
1679#endif
1680
1681/*
1682 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02001683 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001684 * "len" is the length of "name", or -1 for NUL terminated.
1685 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001687builtin_function(char_u *name, int len)
1688{
1689 char_u *p;
1690
Bram Moolenaara26b9702020-04-18 19:53:28 +02001691 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001692 return FALSE;
1693 p = vim_strchr(name, AUTOLOAD_CHAR);
1694 return p == NULL || (len > 0 && p > name + len);
1695}
1696
1697 int
1698func_call(
1699 char_u *name,
1700 typval_T *args,
1701 partial_T *partial,
1702 dict_T *selfdict,
1703 typval_T *rettv)
1704{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001705 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001706 listitem_T *item;
1707 typval_T argv[MAX_FUNC_ARGS + 1];
1708 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001709 int r = 0;
1710
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001711 range_list_materialize(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001712 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001713 {
1714 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1715 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001716 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001717 break;
1718 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001719 // Make a copy of each argument. This is needed to be able to set
1720 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001721 copy_tv(&item->li_tv, &argv[argc++]);
1722 }
1723
1724 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001725 {
1726 funcexe_T funcexe;
1727
Bram Moolenaara80faa82020-04-12 19:37:17 +02001728 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001729 funcexe.firstline = curwin->w_cursor.lnum;
1730 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001731 funcexe.evaluate = TRUE;
1732 funcexe.partial = partial;
1733 funcexe.selfdict = selfdict;
1734 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1735 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001736
Bram Moolenaare38eab22019-12-05 21:50:01 +01001737 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001738 while (argc > 0)
1739 clear_tv(&argv[--argc]);
1740
1741 return r;
1742}
1743
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001744static int callback_depth = 0;
1745
1746 int
1747get_callback_depth(void)
1748{
1749 return callback_depth;
1750}
1751
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001752/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001753 * Invoke call_func() with a callback.
1754 */
1755 int
1756call_callback(
1757 callback_T *callback,
1758 int len, // length of "name" or -1 to use strlen()
1759 typval_T *rettv, // return value goes here
1760 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001761 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001762 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001763{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001764 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001765 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001766
Bram Moolenaara80faa82020-04-12 19:37:17 +02001767 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001768 funcexe.evaluate = TRUE;
1769 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001770 ++callback_depth;
1771 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1772 --callback_depth;
1773 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001774}
1775
1776/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 * Give an error message for the result of a function.
1778 * Nothing if "error" is FCERR_NONE.
1779 */
1780 void
1781user_func_error(int error, char_u *name)
1782{
1783 switch (error)
1784 {
1785 case FCERR_UNKNOWN:
1786 emsg_funcname(e_unknownfunc, name);
1787 break;
1788 case FCERR_NOTMETHOD:
1789 emsg_funcname(
1790 N_("E276: Cannot use function as a method: %s"), name);
1791 break;
1792 case FCERR_DELETED:
1793 emsg_funcname(N_(e_func_deleted), name);
1794 break;
1795 case FCERR_TOOMANY:
1796 emsg_funcname((char *)e_toomanyarg, name);
1797 break;
1798 case FCERR_TOOFEW:
1799 emsg_funcname((char *)e_toofewarg, name);
1800 break;
1801 case FCERR_SCRIPT:
1802 emsg_funcname(
1803 N_("E120: Using <SID> not in a script context: %s"), name);
1804 break;
1805 case FCERR_DICT:
1806 emsg_funcname(
1807 N_("E725: Calling dict function without Dictionary: %s"),
1808 name);
1809 break;
1810 }
1811}
1812
1813/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001814 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001815 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001816 * Return FAIL when the function can't be called, OK otherwise.
1817 * Also returns OK when an error was encountered while executing the function.
1818 */
1819 int
1820call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001821 char_u *funcname, // name of the function
1822 int len, // length of "name" or -1 to use strlen()
1823 typval_T *rettv, // return value goes here
1824 int argcount_in, // number of "argvars"
1825 typval_T *argvars_in, // vars for arguments, must have "argcount"
1826 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001827 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001828{
1829 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001830 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001831 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001832 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001833 char_u fname_buf[FLEN_FIXED + 1];
1834 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001835 char_u *fname = NULL;
1836 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001837 int argcount = argcount_in;
1838 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001839 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001840 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1841 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001842 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001843 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001844 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001845
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001846 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1847 // even when call_func() returns FAIL.
1848 rettv->v_type = VAR_UNKNOWN;
1849
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001850 if (partial != NULL)
1851 fp = partial->pt_func;
1852 if (fp == NULL)
1853 {
1854 // Make a copy of the name, if it comes from a funcref variable it
1855 // could be changed or deleted in the called function.
1856 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
1857 if (name == NULL)
1858 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001859
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001860 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1861 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001862
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001863 if (funcexe->doesrange != NULL)
1864 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001865
1866 if (partial != NULL)
1867 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001868 // When the function has a partial with a dict and there is a dict
1869 // argument, use the dict argument. That is backwards compatible.
1870 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001871 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001872 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001873 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001874 {
1875 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001876 {
1877 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1878 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001879 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001880 goto theend;
1881 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001882 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001883 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001884 for (i = 0; i < argcount_in; ++i)
1885 argv[i + argv_clear] = argvars_in[i];
1886 argvars = argv;
1887 argcount = partial->pt_argc + argcount_in;
1888 }
1889 }
1890
Bram Moolenaaref140542019-12-31 21:27:13 +01001891 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001892 {
1893 char_u *rfname = fname;
1894
Bram Moolenaare38eab22019-12-05 21:50:01 +01001895 // Ignore "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001896 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001897 rfname = fname + 2;
1898
Bram Moolenaare38eab22019-12-05 21:50:01 +01001899 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001900 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001901 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001902
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001903 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001904 {
1905 /*
1906 * User defined function.
1907 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001908 if (fp == NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001909 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001910
Bram Moolenaare38eab22019-12-05 21:50:01 +01001911 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001912 if (fp == NULL
1913 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001914 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001915 && !aborting())
1916 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001917 // executed an autocommand, search for the function again
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001918 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001919 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001920 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001921 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1922 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001923 // loaded a package, search for the function again
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001924 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001925 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02001926 if (fp == NULL)
1927 {
1928 char_u *p = untrans_function_name(rfname);
1929
1930 // If using Vim9 script try not local to the script.
1931 // TODO: should not do this if the name started with "s:".
1932 if (p != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001933 fp = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02001934 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001935
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001936 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01001937 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001938 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001939 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001940 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001941 // postponed filling in the arguments, do it now
1942 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001943 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001944
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001945 if (funcexe->basetv != NULL)
1946 {
1947 // Method call: base->Method()
1948 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1949 argv[0] = *funcexe->basetv;
1950 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001951 argvars = argv;
1952 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001953 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 error = call_user_func_check(fp, argcount, argvars, rettv,
1956 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001957 }
1958 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001959 else if (funcexe->basetv != NULL)
1960 {
1961 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001962 * expr->method(): Find the method name in the table, call its
1963 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02001964 */
1965 error = call_internal_method(fname, argcount, argvars, rettv,
1966 funcexe->basetv);
1967 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001968 else
1969 {
1970 /*
1971 * Find the function name in the table, call its implementation.
1972 */
1973 error = call_internal_func(fname, argcount, argvars, rettv);
1974 }
1975 /*
1976 * The function call (or "FuncUndefined" autocommand sequence) might
1977 * have been aborted by an error, an interrupt, or an explicitly thrown
1978 * exception that has not been caught so far. This situation can be
1979 * tested for by calling aborting(). For an error in an internal
1980 * function or for the "E132" error in call_user_func(), however, the
1981 * throw point at which the "force_abort" flag (temporarily reset by
1982 * emsg()) is normally updated has not been reached yet. We need to
1983 * update that flag first to make aborting() reliable.
1984 */
1985 update_force_abort();
1986 }
Bram Moolenaaref140542019-12-31 21:27:13 +01001987 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001988 ret = OK;
1989
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001990theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001991 /*
1992 * Report an error unless the argument evaluation or function call has been
1993 * cancelled due to an aborting error, an interrupt, or an exception.
1994 */
1995 if (!aborting())
1996 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001997 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001998 }
1999
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002000 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002001 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002002 clear_tv(&argv[--argv_clear + argv_base]);
2003
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002004 vim_free(tofree);
2005 vim_free(name);
2006
2007 return ret;
2008}
2009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 static char_u *
2011printable_func_name(ufunc_T *fp)
2012{
2013 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
2014}
2015
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002016/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002017 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002018 */
2019 static void
2020list_func_head(ufunc_T *fp, int indent)
2021{
2022 int j;
2023
2024 msg_start();
2025 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002026 msg_puts(" ");
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002027 if (fp->uf_dfunc_idx >= 0)
2028 msg_puts("def ");
2029 else
2030 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002032 msg_putchar('(');
2033 for (j = 0; j < fp->uf_args.ga_len; ++j)
2034 {
2035 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002036 msg_puts(", ");
2037 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 if (fp->uf_arg_types != NULL)
2039 {
2040 char *tofree;
2041
2042 msg_puts(": ");
2043 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2044 vim_free(tofree);
2045 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002046 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2047 {
2048 msg_puts(" = ");
2049 msg_puts(((char **)(fp->uf_def_args.ga_data))
2050 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2051 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002052 }
2053 if (fp->uf_varargs)
2054 {
2055 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002056 msg_puts(", ");
2057 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002058 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059 if (fp->uf_va_name != NULL)
2060 {
2061 if (j)
2062 msg_puts(", ");
2063 msg_puts("...");
2064 msg_puts((char *)fp->uf_va_name);
2065 if (fp->uf_va_type)
2066 {
2067 char *tofree;
2068
2069 msg_puts(": ");
2070 msg_puts(type_name(fp->uf_va_type, &tofree));
2071 vim_free(tofree);
2072 }
2073 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002074 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002075
2076 if (fp->uf_dfunc_idx >= 0)
2077 {
2078 if (fp->uf_ret_type != &t_void)
2079 {
2080 char *tofree;
2081
2082 msg_puts(": ");
2083 msg_puts(type_name(fp->uf_ret_type, &tofree));
2084 vim_free(tofree);
2085 }
2086 }
2087 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002088 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002089 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002090 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002091 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002092 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002093 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002094 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002095 msg_clr_eos();
2096 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002097 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002098}
2099
2100/*
2101 * Get a function name, translating "<SID>" and "<SNR>".
2102 * Also handles a Funcref in a List or Dictionary.
2103 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002104 * Set "*is_global" to TRUE when the function must be global, unless
2105 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002106 * flags:
2107 * TFN_INT: internal function name OK
2108 * TFN_QUIET: be quiet
2109 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002110 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002111 * Advances "pp" to just after the function name (if no error).
2112 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002113 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002114trans_function_name(
2115 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002116 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002117 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002118 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002119 funcdict_T *fdp, // return: info about dictionary used
2120 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002121{
2122 char_u *name = NULL;
2123 char_u *start;
2124 char_u *end;
2125 int lead;
2126 char_u sid_buf[20];
2127 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002129 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002130 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002131
2132 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002133 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002134 start = *pp;
2135
Bram Moolenaare38eab22019-12-05 21:50:01 +01002136 // Check for hard coded <SNR>: already translated function ID (from a user
2137 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002138 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2139 && (*pp)[2] == (int)KE_SNR)
2140 {
2141 *pp += 3;
2142 len = get_id_len(pp) + 3;
2143 return vim_strnsave(start, len);
2144 }
2145
Bram Moolenaare38eab22019-12-05 21:50:01 +01002146 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2147 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002148 lead = eval_fname_script(start);
2149 if (lead > 2)
2150 start += lead;
2151
Bram Moolenaare38eab22019-12-05 21:50:01 +01002152 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002153 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002154 lead > 2 ? 0 : FNE_CHECK_START);
2155 if (end == start)
2156 {
2157 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002158 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002159 goto theend;
2160 }
2161 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2162 {
2163 /*
2164 * Report an invalid expression in braces, unless the expression
2165 * evaluation has been cancelled due to an aborting error, an
2166 * interrupt, or an exception.
2167 */
2168 if (!aborting())
2169 {
2170 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002171 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002172 }
2173 else
2174 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2175 goto theend;
2176 }
2177
2178 if (lv.ll_tv != NULL)
2179 {
2180 if (fdp != NULL)
2181 {
2182 fdp->fd_dict = lv.ll_dict;
2183 fdp->fd_newkey = lv.ll_newkey;
2184 lv.ll_newkey = NULL;
2185 fdp->fd_di = lv.ll_di;
2186 }
2187 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2188 {
2189 name = vim_strsave(lv.ll_tv->vval.v_string);
2190 *pp = end;
2191 }
2192 else if (lv.ll_tv->v_type == VAR_PARTIAL
2193 && lv.ll_tv->vval.v_partial != NULL)
2194 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002195 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002196 *pp = end;
2197 if (partial != NULL)
2198 *partial = lv.ll_tv->vval.v_partial;
2199 }
2200 else
2201 {
2202 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2203 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002204 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002205 else
2206 *pp = end;
2207 name = NULL;
2208 }
2209 goto theend;
2210 }
2211
2212 if (lv.ll_name == NULL)
2213 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002214 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002215 *pp = end;
2216 goto theend;
2217 }
2218
Bram Moolenaare38eab22019-12-05 21:50:01 +01002219 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002220 if (lv.ll_exp_name != NULL)
2221 {
2222 len = (int)STRLEN(lv.ll_exp_name);
2223 name = deref_func_name(lv.ll_exp_name, &len, partial,
2224 flags & TFN_NO_AUTOLOAD);
2225 if (name == lv.ll_exp_name)
2226 name = NULL;
2227 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002228 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002229 {
2230 len = (int)(end - *pp);
2231 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2232 if (name == *pp)
2233 name = NULL;
2234 }
2235 if (name != NULL)
2236 {
2237 name = vim_strsave(name);
2238 *pp = end;
2239 if (STRNCMP(name, "<SNR>", 5) == 0)
2240 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002241 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002242 name[0] = K_SPECIAL;
2243 name[1] = KS_EXTRA;
2244 name[2] = (int)KE_SNR;
2245 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2246 }
2247 goto theend;
2248 }
2249
2250 if (lv.ll_exp_name != NULL)
2251 {
2252 len = (int)STRLEN(lv.ll_exp_name);
2253 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2254 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2255 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002256 // When there was "s:" already or the name expanded to get a
2257 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002258 lv.ll_name += 2;
2259 len -= 2;
2260 lead = 2;
2261 }
2262 }
2263 else
2264 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002265 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002266 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002267 {
2268 if (is_global != NULL && lv.ll_name[0] == 'g')
2269 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002270 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002271 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002272 len = (int)(end - lv.ll_name);
2273 }
2274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275 // In Vim9 script a user function is script-local by default.
2276 vim9script = ASCII_ISUPPER(*start)
2277 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2278
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002279 /*
2280 * Copy the function name to allocated memory.
2281 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2282 * Accept <SNR>123_name() outside a script.
2283 */
2284 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002285 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002287 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288 if (!vim9script)
2289 lead = 3;
2290 if (vim9script || (lv.ll_exp_name != NULL
2291 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002292 || eval_fname_sid(*pp))
2293 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002295 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002296 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002297 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002298 goto theend;
2299 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002300 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 if (vim9script)
2302 extra = 3 + (int)STRLEN(sid_buf);
2303 else
2304 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002305 }
2306 }
2307 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2308 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002309 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002310 start);
2311 goto theend;
2312 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002313 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002314 {
2315 char_u *cp = vim_strchr(lv.ll_name, ':');
2316
2317 if (cp != NULL && cp < end)
2318 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002319 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002320 goto theend;
2321 }
2322 }
2323
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002325 if (name != NULL)
2326 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002327 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002328 {
2329 name[0] = K_SPECIAL;
2330 name[1] = KS_EXTRA;
2331 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002333 STRCPY(name + 3, sid_buf);
2334 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2336 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002337 }
2338 *pp = end;
2339
2340theend:
2341 clear_lval(&lv);
2342 return name;
2343}
2344
2345/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002346 * Assuming "name" is the result of trans_function_name() and it was prefixed
2347 * to use the script-local name, return the unmodified name (points into
2348 * "name"). Otherwise return NULL.
2349 * This can be used to first search for a script-local function and fall back
2350 * to the global function if not found.
2351 */
2352 char_u *
2353untrans_function_name(char_u *name)
2354{
2355 char_u *p;
2356
2357 if (*name == K_SPECIAL && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2358 {
2359 p = vim_strchr(name, '_');
2360 if (p != NULL)
2361 return p + 1;
2362 }
2363 return NULL;
2364}
2365
2366/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002367 * ":function"
2368 */
2369 void
2370ex_function(exarg_T *eap)
2371{
2372 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002373 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002374 int j;
2375 int c;
2376 int saved_did_emsg;
2377 int saved_wait_return = need_wait_return;
2378 char_u *name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002379 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002380 char_u *p;
2381 char_u *arg;
2382 char_u *line_arg = NULL;
2383 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002385 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002386 garray_T newlines;
2387 int varargs = FALSE;
2388 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002390 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002391 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002392 int indent;
2393 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394#define MAX_FUNC_NESTING 50
2395 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002396 dictitem_T *v;
2397 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002398 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002399 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002400 int todo;
2401 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002402 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002403 linenr_T sourcing_lnum_off;
2404 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002405 int is_heredoc = FALSE;
2406 char_u *skip_until = NULL;
2407 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002408
2409 /*
2410 * ":function" without argument: list functions.
2411 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002412 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002413 {
2414 if (!eap->skip)
2415 {
2416 todo = (int)func_hashtab.ht_used;
2417 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2418 {
2419 if (!HASHITEM_EMPTY(hi))
2420 {
2421 --todo;
2422 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 if ((fp->uf_flags & FC_DEAD)
2424 || message_filtered(fp->uf_name))
Bram Moolenaarf86db782018-10-25 13:31:37 +02002425 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002426 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002427 list_func_head(fp, FALSE);
2428 }
2429 }
2430 }
2431 eap->nextcmd = check_nextcmd(eap->arg);
2432 return;
2433 }
2434
2435 /*
2436 * ":function /pat": list functions matching pattern.
2437 */
2438 if (*eap->arg == '/')
2439 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002440 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002441 if (!eap->skip)
2442 {
2443 regmatch_T regmatch;
2444
2445 c = *p;
2446 *p = NUL;
2447 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2448 *p = c;
2449 if (regmatch.regprog != NULL)
2450 {
2451 regmatch.rm_ic = p_ic;
2452
2453 todo = (int)func_hashtab.ht_used;
2454 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2455 {
2456 if (!HASHITEM_EMPTY(hi))
2457 {
2458 --todo;
2459 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460 if ((fp->uf_flags & FC_DEAD) == 0
2461 && !isdigit(*fp->uf_name)
2462 && vim_regexec(&regmatch, fp->uf_name, 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002463 list_func_head(fp, FALSE);
2464 }
2465 }
2466 vim_regfree(regmatch.regprog);
2467 }
2468 }
2469 if (*p == '/')
2470 ++p;
2471 eap->nextcmd = check_nextcmd(p);
2472 return;
2473 }
2474
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 ga_init(&newargs);
2476 ga_init(&argtypes);
2477 ga_init(&default_args);
2478
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002479 /*
2480 * Get the function name. There are these situations:
2481 * func normal function name
2482 * "name" == func, "fudi.fd_dict" == NULL
2483 * dict.func new dictionary entry
2484 * "name" == NULL, "fudi.fd_dict" set,
2485 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2486 * dict.func existing dict entry with a Funcref
2487 * "name" == func, "fudi.fd_dict" set,
2488 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2489 * dict.func existing dict entry that's not a Funcref
2490 * "name" == NULL, "fudi.fd_dict" set,
2491 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2492 * s:func script-local function name
2493 * g:func global function name, same as "func"
2494 */
2495 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002496 name = trans_function_name(&p, &is_global, eap->skip,
2497 TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002498 paren = (vim_strchr(p, '(') != NULL);
2499 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2500 {
2501 /*
2502 * Return on an invalid expression in braces, unless the expression
2503 * evaluation has been cancelled due to an aborting error, an
2504 * interrupt, or an exception.
2505 */
2506 if (!aborting())
2507 {
2508 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002509 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002510 vim_free(fudi.fd_newkey);
2511 return;
2512 }
2513 else
2514 eap->skip = TRUE;
2515 }
2516
Bram Moolenaare38eab22019-12-05 21:50:01 +01002517 // An error in a function call during evaluation of an expression in magic
2518 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002519 saved_did_emsg = did_emsg;
2520 did_emsg = FALSE;
2521
2522 /*
2523 * ":function func" with only function name: list function.
2524 */
2525 if (!paren)
2526 {
2527 if (!ends_excmd(*skipwhite(p)))
2528 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002529 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002530 goto ret_free;
2531 }
2532 eap->nextcmd = check_nextcmd(p);
2533 if (eap->nextcmd != NULL)
2534 *p = NUL;
2535 if (!eap->skip && !got_int)
2536 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002537 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002538 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
2539 {
2540 char_u *up = untrans_function_name(name);
2541
2542 // With Vim9 script the name was made script-local, if not
2543 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02002544 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002545 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002546 }
2547
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002548 if (fp != NULL)
2549 {
2550 list_func_head(fp, TRUE);
2551 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2552 {
2553 if (FUNCLINE(fp, j) == NULL)
2554 continue;
2555 msg_putchar('\n');
2556 msg_outnum((long)(j + 1));
2557 if (j < 9)
2558 msg_putchar(' ');
2559 if (j < 99)
2560 msg_putchar(' ');
2561 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002562 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002563 ui_breakcheck();
2564 }
2565 if (!got_int)
2566 {
2567 msg_putchar('\n');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 if (fp->uf_dfunc_idx >= 0)
2569 msg_puts(" enddef");
2570 else
2571 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002572 }
2573 }
2574 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02002575 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002576 }
2577 goto ret_free;
2578 }
2579
2580 /*
2581 * ":function name(arg1, arg2)" Define function.
2582 */
2583 p = skipwhite(p);
2584 if (*p != '(')
2585 {
2586 if (!eap->skip)
2587 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002588 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002589 goto ret_free;
2590 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002591 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002592 if (vim_strchr(p, '(') != NULL)
2593 p = vim_strchr(p, '(');
2594 }
2595 p = skipwhite(p + 1);
2596
2597 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2598
2599 if (!eap->skip)
2600 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002601 // Check the name of the function. Unless it's a dictionary function
2602 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002603 if (name != NULL)
2604 arg = name;
2605 else
2606 arg = fudi.fd_newkey;
2607 if (arg != NULL && (fudi.fd_di == NULL
2608 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2609 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2610 {
2611 if (*arg == K_SPECIAL)
2612 j = 3;
2613 else
2614 j = 0;
2615 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2616 : eval_isnamec(arg[j])))
2617 ++j;
2618 if (arg[j] != NUL)
2619 emsg_funcname((char *)e_invarg2, arg);
2620 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002621 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002622 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002623 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002624 }
2625
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002626 // This may get more lines and make the pointers into the first line
2627 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 if (get_function_args(&p, ')', &newargs,
2629 eap->cmdidx == CMD_def ? &argtypes : NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002630 &varargs, &default_args, eap->skip,
2631 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002632 goto errret_2;
2633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002635 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 // find the return type: :def Func(): type
2637 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002638 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 ret_type = skipwhite(p + 1);
2640 p = skip_type(ret_type);
2641 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002642 {
2643 ret_type = vim_strnsave(ret_type, (int)(p - ret_type));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002645 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002647 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02002649 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002650 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002651 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002652 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653 else
2654 // find extra arguments "range", "dict", "abort" and "closure"
2655 for (;;)
2656 {
2657 p = skipwhite(p);
2658 if (STRNCMP(p, "range", 5) == 0)
2659 {
2660 flags |= FC_RANGE;
2661 p += 5;
2662 }
2663 else if (STRNCMP(p, "dict", 4) == 0)
2664 {
2665 flags |= FC_DICT;
2666 p += 4;
2667 }
2668 else if (STRNCMP(p, "abort", 5) == 0)
2669 {
2670 flags |= FC_ABORT;
2671 p += 5;
2672 }
2673 else if (STRNCMP(p, "closure", 7) == 0)
2674 {
2675 flags |= FC_CLOSURE;
2676 p += 7;
2677 if (current_funccal == NULL)
2678 {
2679 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2680 name == NULL ? (char_u *)"" : name);
2681 goto erret;
2682 }
2683 }
2684 else
2685 break;
2686 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002687
Bram Moolenaare38eab22019-12-05 21:50:01 +01002688 // When there is a line break use what follows for the function body.
2689 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002690 if (*p == '\n')
2691 line_arg = p + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002692 else if (*p != NUL && *p != '"' && !(eap->cmdidx == CMD_def && *p == '#')
2693 && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002694 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002695
2696 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2698 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002699 */
2700 if (KeyTyped)
2701 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002702 // Check if the function already exists, don't let the user type the
2703 // whole function before telling him it doesn't work! For a script we
2704 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002705 if (!eap->skip && !eap->forceit)
2706 {
2707 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002708 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002709 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002710 emsg_funcname(e_funcexts, name);
2711 }
2712
2713 if (!eap->skip && did_emsg)
2714 goto erret;
2715
Bram Moolenaare38eab22019-12-05 21:50:01 +01002716 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002717 cmdline_row = msg_row;
2718 }
2719
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002720 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002721 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002722
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002723 indent = 2;
2724 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002726 for (;;)
2727 {
2728 if (KeyTyped)
2729 {
2730 msg_scroll = TRUE;
2731 saved_wait_return = FALSE;
2732 }
2733 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002734
2735 if (line_arg != NULL)
2736 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002737 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002738 theline = line_arg;
2739 p = vim_strchr(theline, '\n');
2740 if (p == NULL)
2741 line_arg += STRLEN(line_arg);
2742 else
2743 {
2744 *p = NUL;
2745 line_arg = p + 1;
2746 }
2747 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002748 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002749 {
2750 vim_free(line_to_free);
2751 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002752 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002753 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002754 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002755 line_to_free = theline;
2756 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002757 if (KeyTyped)
2758 lines_left = Rows - 1;
2759 if (theline == NULL)
2760 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 if (eap->cmdidx == CMD_def)
2762 emsg(_("E1057: Missing :enddef"));
2763 else
2764 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002765 goto erret;
2766 }
2767
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002768 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002769 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002770 if (SOURCING_LNUM < sourcing_lnum_off)
2771 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002772 else
2773 sourcing_lnum_off = 0;
2774
2775 if (skip_until != NULL)
2776 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002778 // * ":append" and "."
2779 // * ":python <<EOF" and "EOF"
2780 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2781 if (heredoc_trimmed == NULL
2782 || (is_heredoc && skipwhite(theline) == theline)
2783 || STRNCMP(theline, heredoc_trimmed,
2784 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002785 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002786 if (heredoc_trimmed == NULL)
2787 p = theline;
2788 else if (is_heredoc)
2789 p = skipwhite(theline) == theline
2790 ? theline : theline + STRLEN(heredoc_trimmed);
2791 else
2792 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002793 if (STRCMP(p, skip_until) == 0)
2794 {
2795 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002796 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002797 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002798 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002799 }
2800 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002801 }
2802 else
2803 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002804 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002805 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002806 ;
2807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 // Check for "endfunction" or "enddef".
2809 if (checkforcmd(&p, nesting_def[nesting]
2810 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002811 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002812 char_u *nextcmd = NULL;
2813
Bram Moolenaar663bb232017-06-22 19:12:10 +02002814 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002815 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002816 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002817 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002818 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 give_warning2(eap->cmdidx == CMD_def
2820 ? (char_u *)_("W1001: Text found after :enddef: %s")
2821 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002822 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002823 if (nextcmd != NULL)
2824 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002825 // Another command follows. If the line came from "eap" we
2826 // can simply point into it, otherwise we need to change
2827 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002828 eap->nextcmd = nextcmd;
2829 if (line_to_free != NULL)
2830 {
2831 vim_free(*eap->cmdlinep);
2832 *eap->cmdlinep = line_to_free;
2833 line_to_free = NULL;
2834 }
2835 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002836 break;
2837 }
2838
Bram Moolenaare38eab22019-12-05 21:50:01 +01002839 // Increase indent inside "if", "while", "for" and "try", decrease
2840 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002842 indent -= 2;
2843 else if (STRNCMP(p, "if", 2) == 0
2844 || STRNCMP(p, "wh", 2) == 0
2845 || STRNCMP(p, "for", 3) == 0
2846 || STRNCMP(p, "try", 3) == 0)
2847 indent += 2;
2848
Bram Moolenaare38eab22019-12-05 21:50:01 +01002849 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002850 // Only recognize "def" inside "def", not inside "function",
2851 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002853 if (checkforcmd(&p, "function", 2)
2854 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002855 {
2856 if (*p == '!')
2857 p = skipwhite(p + 1);
2858 p += eval_fname_script(p);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002859 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002860 if (*skipwhite(p) == '(')
2861 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 if (nesting == MAX_FUNC_NESTING - 1)
2863 emsg(_("E1058: function nesting too deep"));
2864 else
2865 {
2866 ++nesting;
2867 nesting_def[nesting] = (c == 'd');
2868 indent += 2;
2869 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002870 }
2871 }
2872
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002873 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002874 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002875 if (eap->cmdidx != CMD_def
2876 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002877 || (p[0] == 'c'
2878 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2879 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2880 && (STRNCMP(&p[3], "nge", 3) != 0
2881 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002882 || (p[0] == 'i'
2883 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002884 && (!ASCII_ISALPHA(p[2])
2885 || (p[2] == 's'
2886 && (!ASCII_ISALPHA(p[3])
2887 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002888 skip_until = vim_strsave((char_u *)".");
2889
Bram Moolenaare38eab22019-12-05 21:50:01 +01002890 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002891 arg = skipwhite(skiptowhite(p));
2892 if (arg[0] == '<' && arg[1] =='<'
2893 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002894 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2895 || ((p[2] == '3' || p[2] == 'x')
2896 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002897 || (p[0] == 'p' && p[1] == 'e'
2898 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2899 || (p[0] == 't' && p[1] == 'c'
2900 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2901 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2902 && !ASCII_ISALPHA(p[3]))
2903 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2904 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2905 || (p[0] == 'm' && p[1] == 'z'
2906 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2907 ))
2908 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002909 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002910 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02002911 if (STRNCMP(p, "trim", 4) == 0)
2912 {
2913 // Ignore leading white space.
2914 p = skipwhite(p + 4);
2915 heredoc_trimmed = vim_strnsave(theline,
2916 (int)(skipwhite(theline) - theline));
2917 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002918 if (*p == NUL)
2919 skip_until = vim_strsave((char_u *)".");
2920 else
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02002921 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2922 do_concat = FALSE;
2923 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002924 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002925
2926 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002927 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002928 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002929 if (*arg == '[')
2930 arg = vim_strchr(arg, ']');
2931 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002932 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002933 arg = skipwhite(skiptowhite(arg));
2934 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2935 && ((p[0] == 'l'
2936 && p[1] == 'e'
2937 && (!ASCII_ISALNUM(p[2])
2938 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02002939 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002940 p = skipwhite(arg + 3);
2941 if (STRNCMP(p, "trim", 4) == 0)
2942 {
2943 // Ignore leading white space.
2944 p = skipwhite(p + 4);
2945 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar8471e572019-05-19 21:37:18 +02002946 (int)(skipwhite(theline) - theline));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002947 }
2948 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2949 do_concat = FALSE;
2950 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002951 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002952 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002953 }
2954
Bram Moolenaare38eab22019-12-05 21:50:01 +01002955 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002956 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002957 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002958
Bram Moolenaare38eab22019-12-05 21:50:01 +01002959 // Copy the line to newly allocated memory. get_one_sourceline()
2960 // allocates 250 bytes per line, this saves 80% on average. The cost
2961 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002962 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002963 if (p == NULL)
2964 goto erret;
2965 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002966
Bram Moolenaare38eab22019-12-05 21:50:01 +01002967 // Add NULL lines for continuation lines, so that the line count is
2968 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002969 while (sourcing_lnum_off-- > 0)
2970 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2971
Bram Moolenaare38eab22019-12-05 21:50:01 +01002972 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002973 if (line_arg != NULL && *line_arg == NUL)
2974 line_arg = NULL;
2975 }
2976
Bram Moolenaare38eab22019-12-05 21:50:01 +01002977 // Don't define the function when skipping commands or when an error was
2978 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002979 if (eap->skip || did_emsg)
2980 goto erret;
2981
2982 /*
2983 * If there are no errors, add the function
2984 */
2985 if (fudi.fd_dict == NULL)
2986 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 hashtab_T *ht;
2988
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002989 v = find_var(name, &ht, FALSE);
2990 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2991 {
2992 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2993 name);
2994 goto erret;
2995 }
2996
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002997 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002998 if (fp != NULL)
2999 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 int dead = fp->uf_flags & FC_DEAD;
3001
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003002 // Function can be replaced with "function!" and when sourcing the
3003 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003005 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
3006 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003007 {
3008 emsg_funcname(e_funcexts, name);
3009 goto erret;
3010 }
3011 if (fp->uf_calls > 0)
3012 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003013 emsg_funcname(
3014 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003015 name);
3016 goto erret;
3017 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003018 if (fp->uf_refcount > 1)
3019 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003020 // This function is referenced somewhere, don't redefine it but
3021 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003022 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003023 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003024 fp = NULL;
3025 overwrite = TRUE;
3026 }
3027 else
3028 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003029 char_u *exp_name = fp->uf_name_exp;
3030
3031 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003032 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003033 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003034 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003035 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003037#ifdef FEAT_PROFILE
3038 fp->uf_profiling = FALSE;
3039 fp->uf_prof_initialized = FALSE;
3040#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003041 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003042 }
3043 }
3044 else
3045 {
3046 char numbuf[20];
3047
3048 fp = NULL;
3049 if (fudi.fd_newkey == NULL && !eap->forceit)
3050 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003051 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003052 goto erret;
3053 }
3054 if (fudi.fd_di == NULL)
3055 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003056 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003057 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003058 goto erret;
3059 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003060 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003061 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003062 goto erret;
3063
Bram Moolenaare38eab22019-12-05 21:50:01 +01003064 // Give the function a sequential number. Can only be used with a
3065 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003066 vim_free(name);
3067 sprintf(numbuf, "%d", ++func_nr);
3068 name = vim_strsave((char_u *)numbuf);
3069 if (name == NULL)
3070 goto erret;
3071 }
3072
3073 if (fp == NULL)
3074 {
3075 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3076 {
3077 int slen, plen;
3078 char_u *scriptname;
3079
Bram Moolenaare38eab22019-12-05 21:50:01 +01003080 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003081 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003082 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003083 {
3084 scriptname = autoload_name(name);
3085 if (scriptname != NULL)
3086 {
3087 p = vim_strchr(scriptname, '/');
3088 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003089 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003090 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003091 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003092 j = OK;
3093 vim_free(scriptname);
3094 }
3095 }
3096 if (j == FAIL)
3097 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003098 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003099 goto erret;
3100 }
3101 }
3102
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003103 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003104 if (fp == NULL)
3105 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 fp->uf_dfunc_idx = -1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003107
3108 if (fudi.fd_dict != NULL)
3109 {
3110 if (fudi.fd_di == NULL)
3111 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003112 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003113 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3114 if (fudi.fd_di == NULL)
3115 {
3116 vim_free(fp);
3117 goto erret;
3118 }
3119 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3120 {
3121 vim_free(fudi.fd_di);
3122 vim_free(fp);
3123 goto erret;
3124 }
3125 }
3126 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003127 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003128 clear_tv(&fudi.fd_di->di_tv);
3129 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003130 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003131
Bram Moolenaare38eab22019-12-05 21:50:01 +01003132 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003133 flags |= FC_DICT;
3134 }
3135
Bram Moolenaare38eab22019-12-05 21:50:01 +01003136 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003137 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003138 if (overwrite)
3139 {
3140 hi = hash_find(&func_hashtab, name);
3141 hi->hi_key = UF2HIKEY(fp);
3142 }
3143 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003144 {
3145 vim_free(fp);
3146 goto erret;
3147 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003148 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003149 }
3150 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003151 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003153 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154
3155 if (eap->cmdidx == CMD_def)
3156 {
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003157 int lnum_save = SOURCING_LNUM;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003158
3159 // error messages are for the first function line
3160 SOURCING_LNUM = sourcing_lnum_top;
3161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 // parse the argument types
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003163 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164
3165 if (argtypes.ga_len > 0)
3166 {
3167 // When "varargs" is set the last name/type goes into uf_va_name
3168 // and uf_va_type.
3169 int len = argtypes.ga_len - (varargs ? 1 : 0);
3170
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003171 if (len > 0)
3172 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 if (fp->uf_arg_types != NULL)
3174 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003175 int i;
3176 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177
3178 for (i = 0; i < len; ++ i)
3179 {
3180 p = ((char_u **)argtypes.ga_data)[i];
3181 if (p == NULL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003182 // will get the type from the default value
3183 type = &t_unknown;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003185 type = parse_type(&p, &fp->uf_type_list);
3186 if (type == NULL)
3187 {
3188 SOURCING_LNUM = lnum_save;
3189 goto errret_2;
3190 }
3191 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 }
3193 }
3194 if (varargs)
3195 {
3196 // Move the last argument "...name: type" to uf_va_name and
3197 // uf_va_type.
3198 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3199 [fp->uf_args.ga_len - 1];
3200 --fp->uf_args.ga_len;
3201 p = ((char_u **)argtypes.ga_data)[len];
3202 if (p == NULL)
3203 // todo: get type from default value
3204 fp->uf_va_type = &t_any;
3205 else
3206 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003207 if (fp->uf_va_type == NULL)
3208 {
3209 SOURCING_LNUM = lnum_save;
3210 goto errret_2;
3211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 }
3213 varargs = FALSE;
3214 }
3215
3216 // parse the return type, if any
3217 if (ret_type == NULL)
3218 fp->uf_ret_type = &t_void;
3219 else
3220 {
3221 p = ret_type;
3222 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3223 }
3224 }
3225
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003226 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003227 if ((flags & FC_CLOSURE) != 0)
3228 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003229 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003230 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003231 }
3232 else
3233 fp->uf_scoped = NULL;
3234
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003235#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003236 if (prof_def_func())
3237 func_do_profile(fp);
3238#endif
3239 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003240 if (sandbox)
3241 flags |= FC_SANDBOX;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003242 if (in_vim9script() && !ASCII_ISUPPER(*fp->uf_name))
3243 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003244 fp->uf_flags = flags;
3245 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003246 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003247 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003248 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 if (is_export)
3250 {
3251 fp->uf_flags |= FC_EXPORT;
3252 // let ex_export() know the export worked.
3253 is_export = FALSE;
3254 }
3255
3256 // ":def Func()" needs to be compiled
3257 if (eap->cmdidx == CMD_def)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003258 compile_def_function(fp, FALSE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003260 goto ret_free;
3261
3262erret:
3263 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003264 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003265errret_2:
3266 ga_clear_strings(&newlines);
3267ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003268 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003269 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003270 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003271 vim_free(fudi.fd_newkey);
3272 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003273 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003274 did_emsg |= saved_did_emsg;
3275 need_wait_return |= saved_wait_return;
3276}
3277
3278/*
3279 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3280 * Return 2 if "p" starts with "s:".
3281 * Return 0 otherwise.
3282 */
3283 int
3284eval_fname_script(char_u *p)
3285{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003286 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3287 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003288 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3289 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3290 return 5;
3291 if (p[0] == 's' && p[1] == ':')
3292 return 2;
3293 return 0;
3294}
3295
3296 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003297translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003298{
3299 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003300 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003301 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302}
3303
3304/*
3305 * Return TRUE when "ufunc" has old-style "..." varargs
3306 * or named varargs "...name: type".
3307 */
3308 int
3309has_varargs(ufunc_T *ufunc)
3310{
3311 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003312}
3313
3314/*
3315 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003316 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003317 */
3318 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003319function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003320{
3321 char_u *nm = name;
3322 char_u *p;
3323 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003324 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003325 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003326
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003327 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3328 if (no_deref)
3329 flag |= TFN_NO_DEREF;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003330 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003331 nm = skipwhite(nm);
3332
Bram Moolenaare38eab22019-12-05 21:50:01 +01003333 // Only accept "funcname", "funcname ", "funcname (..." and
3334 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003335 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003336 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003337 vim_free(p);
3338 return n;
3339}
3340
Bram Moolenaar113e1072019-01-20 15:30:40 +01003341#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003342 char_u *
3343get_expanded_name(char_u *name, int check)
3344{
3345 char_u *nm = name;
3346 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003347 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003348
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003349 p = trans_function_name(&nm, &is_global, FALSE,
3350 TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003351
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003352 if (p != NULL && *nm == NUL
3353 && (!check || translated_function_exists(p, is_global)))
3354 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003355
3356 vim_free(p);
3357 return NULL;
3358}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003359#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003360
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003361/*
3362 * Function given to ExpandGeneric() to obtain the list of user defined
3363 * function names.
3364 */
3365 char_u *
3366get_user_func_name(expand_T *xp, int idx)
3367{
3368 static long_u done;
3369 static hashitem_T *hi;
3370 ufunc_T *fp;
3371
3372 if (idx == 0)
3373 {
3374 done = 0;
3375 hi = func_hashtab.ht_array;
3376 }
3377 if (done < func_hashtab.ht_used)
3378 {
3379 if (done++ > 0)
3380 ++hi;
3381 while (HASHITEM_EMPTY(hi))
3382 ++hi;
3383 fp = HI2UF(hi);
3384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 // don't show dead, dict and lambda functions
3386 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003387 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003389
3390 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003391 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003392
3393 cat_func_name(IObuff, fp);
3394 if (xp->xp_context != EXPAND_USER_FUNC)
3395 {
3396 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003398 STRCAT(IObuff, ")");
3399 }
3400 return IObuff;
3401 }
3402 return NULL;
3403}
3404
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003405/*
3406 * ":delfunction {name}"
3407 */
3408 void
3409ex_delfunction(exarg_T *eap)
3410{
3411 ufunc_T *fp = NULL;
3412 char_u *p;
3413 char_u *name;
3414 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003415 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003416
3417 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003418 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003419 vim_free(fudi.fd_newkey);
3420 if (name == NULL)
3421 {
3422 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003423 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003424 return;
3425 }
3426 if (!ends_excmd(*skipwhite(p)))
3427 {
3428 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003429 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003430 return;
3431 }
3432 eap->nextcmd = check_nextcmd(p);
3433 if (eap->nextcmd != NULL)
3434 *p = NUL;
3435
3436 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003437 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003438 vim_free(name);
3439
3440 if (!eap->skip)
3441 {
3442 if (fp == NULL)
3443 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003444 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003445 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003446 return;
3447 }
3448 if (fp->uf_calls > 0)
3449 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003450 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003451 return;
3452 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003453 if (fp->uf_flags & FC_VIM9)
3454 {
3455 semsg(_("E1084: Cannot delete Vim9 script function %s"), eap->arg);
3456 return;
3457 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003458
3459 if (fudi.fd_dict != NULL)
3460 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003461 // Delete the dict item that refers to the function, it will
3462 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003463 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3464 }
3465 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003466 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003467 // A normal function (not a numbered function or lambda) has a
3468 // refcount of 1 for the entry in the hashtable. When deleting
3469 // it and the refcount is more than one, it should be kept.
3470 // A numbered function and lambda should be kept if the refcount is
3471 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003472 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003473 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003474 // Function is still referenced somewhere. Don't free it but
3475 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003476 if (func_remove(fp))
3477 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003478 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003479 }
3480 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003481 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003482 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003483 }
3484}
3485
3486/*
3487 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003488 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003489 */
3490 void
3491func_unref(char_u *name)
3492{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003493 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003494
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003495 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003496 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003497 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003498 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003499 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003500#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003501 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003502#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003503 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003504 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003505 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003506 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003507 // Only delete it when it's not being used. Otherwise it's done
3508 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003509 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003510 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003511 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003512}
3513
3514/*
3515 * Unreference a Function: decrement the reference count and free it when it
3516 * becomes zero.
3517 */
3518 void
3519func_ptr_unref(ufunc_T *fp)
3520{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003521 if (fp != NULL && --fp->uf_refcount <= 0)
3522 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003523 // Only delete it when it's not being used. Otherwise it's done
3524 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003525 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003526 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003527 }
3528}
3529
3530/*
3531 * Count a reference to a Function.
3532 */
3533 void
3534func_ref(char_u *name)
3535{
3536 ufunc_T *fp;
3537
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003538 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003539 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003540 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003541 if (fp != NULL)
3542 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003543 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003544 // Only give an error for a numbered function.
3545 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003546 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003547}
3548
3549/*
3550 * Count a reference to a Function.
3551 */
3552 void
3553func_ptr_ref(ufunc_T *fp)
3554{
3555 if (fp != NULL)
3556 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003557}
3558
3559/*
3560 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3561 * referenced from anywhere that is in use.
3562 */
3563 static int
3564can_free_funccal(funccall_T *fc, int copyID)
3565{
3566 return (fc->l_varlist.lv_copyID != copyID
3567 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003568 && fc->l_avars.dv_copyID != copyID
3569 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003570}
3571
3572/*
3573 * ":return [expr]"
3574 */
3575 void
3576ex_return(exarg_T *eap)
3577{
3578 char_u *arg = eap->arg;
3579 typval_T rettv;
3580 int returning = FALSE;
3581
3582 if (current_funccal == NULL)
3583 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003584 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003585 return;
3586 }
3587
3588 if (eap->skip)
3589 ++emsg_skip;
3590
3591 eap->nextcmd = NULL;
3592 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3593 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3594 {
3595 if (!eap->skip)
3596 returning = do_return(eap, FALSE, TRUE, &rettv);
3597 else
3598 clear_tv(&rettv);
3599 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003600 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003601 else if (!eap->skip)
3602 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003603 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003604 update_force_abort();
3605
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003606 /*
3607 * Return unless the expression evaluation has been cancelled due to an
3608 * aborting error, an interrupt, or an exception.
3609 */
3610 if (!aborting())
3611 returning = do_return(eap, FALSE, TRUE, NULL);
3612 }
3613
Bram Moolenaare38eab22019-12-05 21:50:01 +01003614 // When skipping or the return gets pending, advance to the next command
3615 // in this line (!returning). Otherwise, ignore the rest of the line.
3616 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003617 if (returning)
3618 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003619 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003620 eap->nextcmd = check_nextcmd(arg);
3621
3622 if (eap->skip)
3623 --emsg_skip;
3624}
3625
3626/*
3627 * ":1,25call func(arg1, arg2)" function call.
3628 */
3629 void
3630ex_call(exarg_T *eap)
3631{
3632 char_u *arg = eap->arg;
3633 char_u *startarg;
3634 char_u *name;
3635 char_u *tofree;
3636 int len;
3637 typval_T rettv;
3638 linenr_T lnum;
3639 int doesrange;
3640 int failed = FALSE;
3641 funcdict_T fudi;
3642 partial_T *partial = NULL;
3643
3644 if (eap->skip)
3645 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003646 // trans_function_name() doesn't work well when skipping, use eval0()
3647 // instead to skip to any following command, e.g. for:
3648 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003649 ++emsg_skip;
3650 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3651 clear_tv(&rettv);
3652 --emsg_skip;
3653 return;
3654 }
3655
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003656 tofree = trans_function_name(&arg, NULL, eap->skip,
3657 TFN_INT, &fudi, &partial);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003658 if (fudi.fd_newkey != NULL)
3659 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003660 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003661 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003662 vim_free(fudi.fd_newkey);
3663 }
3664 if (tofree == NULL)
3665 return;
3666
Bram Moolenaare38eab22019-12-05 21:50:01 +01003667 // Increase refcount on dictionary, it could get deleted when evaluating
3668 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003669 if (fudi.fd_dict != NULL)
3670 ++fudi.fd_dict->dv_refcount;
3671
Bram Moolenaare38eab22019-12-05 21:50:01 +01003672 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3673 // contents. For VAR_PARTIAL get its partial, unless we already have one
3674 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003675 len = (int)STRLEN(tofree);
3676 name = deref_func_name(tofree, &len,
3677 partial != NULL ? NULL : &partial, FALSE);
3678
Bram Moolenaare38eab22019-12-05 21:50:01 +01003679 // Skip white space to allow ":call func ()". Not good, but required for
3680 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003681 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003682 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003683
3684 if (*startarg != '(')
3685 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003687 goto end;
3688 }
3689
3690 /*
3691 * When skipping, evaluate the function once, to find the end of the
3692 * arguments.
3693 * When the function takes a range, this is discovered after the first
3694 * call, and the loop is broken.
3695 */
3696 if (eap->skip)
3697 {
3698 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003699 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003700 }
3701 else
3702 lnum = eap->line1;
3703 for ( ; lnum <= eap->line2; ++lnum)
3704 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003705 funcexe_T funcexe;
3706
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003707 if (!eap->skip && eap->addr_count > 0)
3708 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003709 if (lnum > curbuf->b_ml.ml_line_count)
3710 {
3711 // If the function deleted lines or switched to another buffer
3712 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003713 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003714 break;
3715 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003716 curwin->w_cursor.lnum = lnum;
3717 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003718 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003719 }
3720 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003721
Bram Moolenaara80faa82020-04-12 19:37:17 +02003722 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003723 funcexe.firstline = eap->line1;
3724 funcexe.lastline = eap->line2;
3725 funcexe.doesrange = &doesrange;
3726 funcexe.evaluate = !eap->skip;
3727 funcexe.partial = partial;
3728 funcexe.selfdict = fudi.fd_dict;
3729 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003730 {
3731 failed = TRUE;
3732 break;
3733 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003734 if (has_watchexpr())
3735 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003736
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003737 // Handle a function returning a Funcref, Dictionary or List.
3738 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE,
3739 name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003740 {
3741 failed = TRUE;
3742 break;
3743 }
3744
3745 clear_tv(&rettv);
3746 if (doesrange || eap->skip)
3747 break;
3748
Bram Moolenaare38eab22019-12-05 21:50:01 +01003749 // Stop when immediately aborting on error, or when an interrupt
3750 // occurred or an exception was thrown but not caught.
3751 // get_func_tv() returned OK, so that the check for trailing
3752 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003753 if (aborting())
3754 break;
3755 }
3756 if (eap->skip)
3757 --emsg_skip;
3758
Bram Moolenaare51bb172020-02-16 19:42:23 +01003759 // When inside :try we need to check for following "| catch".
3760 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003761 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003762 // Check for trailing illegal characters and a following command.
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003763 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003764 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01003765 if (!failed)
3766 {
3767 emsg_severe = TRUE;
3768 emsg(_(e_trailing));
3769 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003770 }
3771 else
3772 eap->nextcmd = check_nextcmd(arg);
3773 }
3774
3775end:
3776 dict_unref(fudi.fd_dict);
3777 vim_free(tofree);
3778}
3779
3780/*
3781 * Return from a function. Possibly makes the return pending. Also called
3782 * for a pending return at the ":endtry" or after returning from an extra
3783 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3784 * when called due to a ":return" command. "rettv" may point to a typval_T
3785 * with the return rettv. Returns TRUE when the return can be carried out,
3786 * FALSE when the return gets pending.
3787 */
3788 int
3789do_return(
3790 exarg_T *eap,
3791 int reanimate,
3792 int is_cmd,
3793 void *rettv)
3794{
3795 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003796 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003797
3798 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003799 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003800 current_funccal->returned = FALSE;
3801
3802 /*
3803 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3804 * not in its finally clause (which then is to be executed next) is found.
3805 * In this case, make the ":return" pending for execution at the ":endtry".
3806 * Otherwise, return normally.
3807 */
3808 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3809 if (idx >= 0)
3810 {
3811 cstack->cs_pending[idx] = CSTP_RETURN;
3812
3813 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003814 // A pending return again gets pending. "rettv" points to an
3815 // allocated variable with the rettv of the original ":return"'s
3816 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003817 cstack->cs_rettv[idx] = rettv;
3818 else
3819 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003820 // When undoing a return in order to make it pending, get the stored
3821 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003822 if (reanimate)
3823 rettv = current_funccal->rettv;
3824
3825 if (rettv != NULL)
3826 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003827 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003828 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3829 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3830 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003831 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003832 }
3833 else
3834 cstack->cs_rettv[idx] = NULL;
3835
3836 if (reanimate)
3837 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003838 // The pending return value could be overwritten by a ":return"
3839 // without argument in a finally clause; reset the default
3840 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003841 current_funccal->rettv->v_type = VAR_NUMBER;
3842 current_funccal->rettv->vval.v_number = 0;
3843 }
3844 }
3845 report_make_pending(CSTP_RETURN, rettv);
3846 }
3847 else
3848 {
3849 current_funccal->returned = TRUE;
3850
Bram Moolenaare38eab22019-12-05 21:50:01 +01003851 // If the return is carried out now, store the return value. For
3852 // a return immediately after reanimation, the value is already
3853 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003854 if (!reanimate && rettv != NULL)
3855 {
3856 clear_tv(current_funccal->rettv);
3857 *current_funccal->rettv = *(typval_T *)rettv;
3858 if (!is_cmd)
3859 vim_free(rettv);
3860 }
3861 }
3862
3863 return idx < 0;
3864}
3865
3866/*
3867 * Free the variable with a pending return value.
3868 */
3869 void
3870discard_pending_return(void *rettv)
3871{
3872 free_tv((typval_T *)rettv);
3873}
3874
3875/*
3876 * Generate a return command for producing the value of "rettv". The result
3877 * is an allocated string. Used by report_pending() for verbose messages.
3878 */
3879 char_u *
3880get_return_cmd(void *rettv)
3881{
3882 char_u *s = NULL;
3883 char_u *tofree = NULL;
3884 char_u numbuf[NUMBUFLEN];
3885
3886 if (rettv != NULL)
3887 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3888 if (s == NULL)
3889 s = (char_u *)"";
3890
3891 STRCPY(IObuff, ":return ");
3892 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3893 if (STRLEN(s) + 8 >= IOSIZE)
3894 STRCPY(IObuff + IOSIZE - 4, "...");
3895 vim_free(tofree);
3896 return vim_strsave(IObuff);
3897}
3898
3899/*
3900 * Get next function line.
3901 * Called by do_cmdline() to get the next line.
3902 * Returns allocated string, or NULL for end of function.
3903 */
3904 char_u *
3905get_func_line(
3906 int c UNUSED,
3907 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003908 int indent UNUSED,
3909 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003910{
3911 funccall_T *fcp = (funccall_T *)cookie;
3912 ufunc_T *fp = fcp->func;
3913 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003914 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003915
Bram Moolenaare38eab22019-12-05 21:50:01 +01003916 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003917 if (fcp->dbg_tick != debug_tick)
3918 {
3919 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003920 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003921 fcp->dbg_tick = debug_tick;
3922 }
3923#ifdef FEAT_PROFILE
3924 if (do_profiling == PROF_YES)
3925 func_line_end(cookie);
3926#endif
3927
3928 gap = &fp->uf_lines;
3929 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3930 || fcp->returned)
3931 retval = NULL;
3932 else
3933 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003934 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003935 while (fcp->linenr < gap->ga_len
3936 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3937 ++fcp->linenr;
3938 if (fcp->linenr >= gap->ga_len)
3939 retval = NULL;
3940 else
3941 {
3942 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003943 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003944#ifdef FEAT_PROFILE
3945 if (do_profiling == PROF_YES)
3946 func_line_start(cookie);
3947#endif
3948 }
3949 }
3950
Bram Moolenaare38eab22019-12-05 21:50:01 +01003951 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003952 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003953 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003954 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003955 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003956 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003957 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003958 fcp->dbg_tick = debug_tick;
3959 }
3960
3961 return retval;
3962}
3963
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003964/*
3965 * Return TRUE if the currently active function should be ended, because a
3966 * return was encountered or an error occurred. Used inside a ":while".
3967 */
3968 int
3969func_has_ended(void *cookie)
3970{
3971 funccall_T *fcp = (funccall_T *)cookie;
3972
Bram Moolenaare38eab22019-12-05 21:50:01 +01003973 // Ignore the "abort" flag if the abortion behavior has been changed due to
3974 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003975 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3976 || fcp->returned);
3977}
3978
3979/*
3980 * return TRUE if cookie indicates a function which "abort"s on errors.
3981 */
3982 int
3983func_has_abort(
3984 void *cookie)
3985{
3986 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3987}
3988
3989
3990/*
3991 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3992 * Don't do this when "Func" is already a partial that was bound
3993 * explicitly (pt_auto is FALSE).
3994 * Changes "rettv" in-place.
3995 * Returns the updated "selfdict_in".
3996 */
3997 dict_T *
3998make_partial(dict_T *selfdict_in, typval_T *rettv)
3999{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004000 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004001 char_u *tofree = NULL;
4002 ufunc_T *fp;
4003 char_u fname_buf[FLEN_FIXED + 1];
4004 int error;
4005 dict_T *selfdict = selfdict_in;
4006
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004007 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4008 fp = rettv->vval.v_partial->pt_func;
4009 else
4010 {
4011 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4012 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004013 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004014 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004015 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004016 vim_free(tofree);
4017 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004018
4019 if (fp != NULL && (fp->uf_flags & FC_DICT))
4020 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004021 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004022
4023 if (pt != NULL)
4024 {
4025 pt->pt_refcount = 1;
4026 pt->pt_dict = selfdict;
4027 pt->pt_auto = TRUE;
4028 selfdict = NULL;
4029 if (rettv->v_type == VAR_FUNC)
4030 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004031 // Just a function: Take over the function name and use
4032 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004033 pt->pt_name = rettv->vval.v_string;
4034 }
4035 else
4036 {
4037 partial_T *ret_pt = rettv->vval.v_partial;
4038 int i;
4039
Bram Moolenaare38eab22019-12-05 21:50:01 +01004040 // Partial: copy the function name, use selfdict and copy
4041 // args. Can't take over name or args, the partial might
4042 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004043 if (ret_pt->pt_name != NULL)
4044 {
4045 pt->pt_name = vim_strsave(ret_pt->pt_name);
4046 func_ref(pt->pt_name);
4047 }
4048 else
4049 {
4050 pt->pt_func = ret_pt->pt_func;
4051 func_ptr_ref(pt->pt_func);
4052 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004053 if (ret_pt->pt_argc > 0)
4054 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004055 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004056 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004057 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004058 pt->pt_argc = 0;
4059 else
4060 {
4061 pt->pt_argc = ret_pt->pt_argc;
4062 for (i = 0; i < pt->pt_argc; i++)
4063 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4064 }
4065 }
4066 partial_unref(ret_pt);
4067 }
4068 rettv->v_type = VAR_PARTIAL;
4069 rettv->vval.v_partial = pt;
4070 }
4071 }
4072 return selfdict;
4073}
4074
4075/*
4076 * Return the name of the executed function.
4077 */
4078 char_u *
4079func_name(void *cookie)
4080{
4081 return ((funccall_T *)cookie)->func->uf_name;
4082}
4083
4084/*
4085 * Return the address holding the next breakpoint line for a funccall cookie.
4086 */
4087 linenr_T *
4088func_breakpoint(void *cookie)
4089{
4090 return &((funccall_T *)cookie)->breakpoint;
4091}
4092
4093/*
4094 * Return the address holding the debug tick for a funccall cookie.
4095 */
4096 int *
4097func_dbg_tick(void *cookie)
4098{
4099 return &((funccall_T *)cookie)->dbg_tick;
4100}
4101
4102/*
4103 * Return the nesting level for a funccall cookie.
4104 */
4105 int
4106func_level(void *cookie)
4107{
4108 return ((funccall_T *)cookie)->level;
4109}
4110
4111/*
4112 * Return TRUE when a function was ended by a ":return" command.
4113 */
4114 int
4115current_func_returned(void)
4116{
4117 return current_funccal->returned;
4118}
4119
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004120 int
4121free_unref_funccal(int copyID, int testing)
4122{
4123 int did_free = FALSE;
4124 int did_free_funccal = FALSE;
4125 funccall_T *fc, **pfc;
4126
4127 for (pfc = &previous_funccal; *pfc != NULL; )
4128 {
4129 if (can_free_funccal(*pfc, copyID))
4130 {
4131 fc = *pfc;
4132 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004133 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004134 did_free = TRUE;
4135 did_free_funccal = TRUE;
4136 }
4137 else
4138 pfc = &(*pfc)->caller;
4139 }
4140 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004141 // When a funccal was freed some more items might be garbage
4142 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004143 (void)garbage_collect(testing);
4144
4145 return did_free;
4146}
4147
4148/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004149 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004150 */
4151 static funccall_T *
4152get_funccal(void)
4153{
4154 int i;
4155 funccall_T *funccal;
4156 funccall_T *temp_funccal;
4157
4158 funccal = current_funccal;
4159 if (debug_backtrace_level > 0)
4160 {
4161 for (i = 0; i < debug_backtrace_level; i++)
4162 {
4163 temp_funccal = funccal->caller;
4164 if (temp_funccal)
4165 funccal = temp_funccal;
4166 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004167 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004168 debug_backtrace_level = i;
4169 }
4170 }
4171 return funccal;
4172}
4173
4174/*
4175 * Return the hashtable used for local variables in the current funccal.
4176 * Return NULL if there is no current funccal.
4177 */
4178 hashtab_T *
4179get_funccal_local_ht()
4180{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004182 return NULL;
4183 return &get_funccal()->l_vars.dv_hashtab;
4184}
4185
4186/*
4187 * Return the l: scope variable.
4188 * Return NULL if there is no current funccal.
4189 */
4190 dictitem_T *
4191get_funccal_local_var()
4192{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004194 return NULL;
4195 return &get_funccal()->l_vars_var;
4196}
4197
4198/*
4199 * Return the hashtable used for argument in the current funccal.
4200 * Return NULL if there is no current funccal.
4201 */
4202 hashtab_T *
4203get_funccal_args_ht()
4204{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004206 return NULL;
4207 return &get_funccal()->l_avars.dv_hashtab;
4208}
4209
4210/*
4211 * Return the a: scope variable.
4212 * Return NULL if there is no current funccal.
4213 */
4214 dictitem_T *
4215get_funccal_args_var()
4216{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004218 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004219 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004220}
4221
4222/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004223 * List function variables, if there is a function.
4224 */
4225 void
4226list_func_vars(int *first)
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 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004230 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004231}
4232
4233/*
4234 * If "ht" is the hashtable for local variables in the current funccal, return
4235 * the dict that contains it.
4236 * Otherwise return NULL.
4237 */
4238 dict_T *
4239get_current_funccal_dict(hashtab_T *ht)
4240{
4241 if (current_funccal != NULL
4242 && ht == &current_funccal->l_vars.dv_hashtab)
4243 return &current_funccal->l_vars;
4244 return NULL;
4245}
4246
4247/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004248 * Search hashitem in parent scope.
4249 */
4250 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004251find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004252{
4253 funccall_T *old_current_funccal = current_funccal;
4254 hashtab_T *ht;
4255 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004256 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004257
4258 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4259 return NULL;
4260
Bram Moolenaare38eab22019-12-05 21:50:01 +01004261 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004262 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004263 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004264 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004265 ht = find_var_ht(name, &varname);
4266 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004267 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004268 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004269 if (!HASHITEM_EMPTY(hi))
4270 {
4271 *pht = ht;
4272 break;
4273 }
4274 }
4275 if (current_funccal == current_funccal->func->uf_scoped)
4276 break;
4277 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004278 }
4279 current_funccal = old_current_funccal;
4280
4281 return hi;
4282}
4283
4284/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004285 * Search variable in parent scope.
4286 */
4287 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004288find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004289{
4290 dictitem_T *v = NULL;
4291 funccall_T *old_current_funccal = current_funccal;
4292 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004293 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004294
4295 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4296 return NULL;
4297
Bram Moolenaare38eab22019-12-05 21:50:01 +01004298 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004299 current_funccal = current_funccal->func->uf_scoped;
4300 while (current_funccal)
4301 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004302 ht = find_var_ht(name, &varname);
4303 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004304 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004305 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004306 if (v != NULL)
4307 break;
4308 }
4309 if (current_funccal == current_funccal->func->uf_scoped)
4310 break;
4311 current_funccal = current_funccal->func->uf_scoped;
4312 }
4313 current_funccal = old_current_funccal;
4314
4315 return v;
4316}
4317
4318/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004319 * Set "copyID + 1" in previous_funccal and callers.
4320 */
4321 int
4322set_ref_in_previous_funccal(int copyID)
4323{
4324 int abort = FALSE;
4325 funccall_T *fc;
4326
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004327 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004328 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004329 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004330 abort = abort
4331 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4332 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004333 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004334 }
4335 return abort;
4336}
4337
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004338 static int
4339set_ref_in_funccal(funccall_T *fc, int copyID)
4340{
4341 int abort = FALSE;
4342
4343 if (fc->fc_copyID != copyID)
4344 {
4345 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004346 abort = abort
4347 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4348 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004349 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004350 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004351 }
4352 return abort;
4353}
4354
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004355/*
4356 * Set "copyID" in all local vars and arguments in the call stack.
4357 */
4358 int
4359set_ref_in_call_stack(int copyID)
4360{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004361 int abort = FALSE;
4362 funccall_T *fc;
4363 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004364
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004365 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004366 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004367
4368 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004369 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4370 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004371 abort = abort || set_ref_in_funccal(fc, copyID);
4372
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004373 return abort;
4374}
4375
4376/*
4377 * Set "copyID" in all functions available by name.
4378 */
4379 int
4380set_ref_in_functions(int copyID)
4381{
4382 int todo;
4383 hashitem_T *hi = NULL;
4384 int abort = FALSE;
4385 ufunc_T *fp;
4386
4387 todo = (int)func_hashtab.ht_used;
4388 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004389 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004390 if (!HASHITEM_EMPTY(hi))
4391 {
4392 --todo;
4393 fp = HI2UF(hi);
4394 if (!func_name_refcount(fp->uf_name))
4395 abort = abort || set_ref_in_func(NULL, fp, copyID);
4396 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004397 }
4398 return abort;
4399}
4400
4401/*
4402 * Set "copyID" in all function arguments.
4403 */
4404 int
4405set_ref_in_func_args(int copyID)
4406{
4407 int i;
4408 int abort = FALSE;
4409
4410 for (i = 0; i < funcargs.ga_len; ++i)
4411 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4412 copyID, NULL, NULL);
4413 return abort;
4414}
4415
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004416/*
4417 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004418 * Returns TRUE if setting references failed somehow.
4419 */
4420 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004421set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004422{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004423 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004424 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004425 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004426 char_u fname_buf[FLEN_FIXED + 1];
4427 char_u *tofree = NULL;
4428 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004429 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004430
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004431 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004432 return FALSE;
4433
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004434 if (fp_in == NULL)
4435 {
4436 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004437 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004438 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004439 if (fp != NULL)
4440 {
4441 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004442 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004443 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004444
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004445 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004446 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004447}
4448
Bram Moolenaare38eab22019-12-05 21:50:01 +01004449#endif // FEAT_EVAL