blob: 95483e2b367666d147548dd0cda3f3c4920efde5 [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 Moolenaar93343722018-07-10 19:39:18 +020017// flags used in uf_flags
18#define FC_ABORT 0x01 // abort function on error
19#define FC_RANGE 0x02 // function accepts range
20#define FC_DICT 0x04 // Dict function, uses "self"
21#define FC_CLOSURE 0x08 // closure, uses outer scope variables
22#define FC_DELETED 0x10 // :delfunction used while uf_refcount > 0
23#define FC_REMOVED 0x20 // function redefined while uf_refcount > 0
24#define FC_SANDBOX 0x40 // function defined in the sandbox
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010025#define FC_DEAD 0x80 // function kept only for reference to dfunc
26#define FC_EXPORT 0x100 // "export def Func()"
Bram Moolenaarf10806b2020-04-02 18:34:35 +020027#define FC_NOARGS 0x200 // no a: variables in lambda
Bram Moolenaara9b579f2016-07-17 18:29:19 +020028
Bram Moolenaara9b579f2016-07-17 18:29:19 +020029/*
30 * All user-defined functions are found in this hashtable.
31 */
32static hashtab_T func_hashtab;
33
Bram Moolenaare38eab22019-12-05 21:50:01 +010034// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020035static garray_T funcargs = GA_EMPTY;
36
Bram Moolenaar209b8e32019-03-14 13:43:24 +010037// pointer to funccal for currently active function
38static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020039
Bram Moolenaar209b8e32019-03-14 13:43:24 +010040// Pointer to list of previously used funccal, still around because some
41// item in it is still being used.
42static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020043
44static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
45static char *e_funcdict = N_("E717: Dictionary entry already exists");
46static char *e_funcref = N_("E718: Funcref required");
47static char *e_nofunc = N_("E130: Unknown function: %s");
48
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020049static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020050
51 void
52func_init()
53{
54 hash_init(&func_hashtab);
55}
56
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020057/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020058 * Return the function hash table
59 */
60 hashtab_T *
61func_tbl_get(void)
62{
63 return &func_hashtab;
64}
65
66/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010067 * Get one function argument and an optional type: "arg: type".
68 * Return a pointer to after the type.
69 * When something is wrong return "arg".
70 */
71 static char_u *
72one_function_arg(char_u *arg, garray_T *newargs, garray_T *argtypes, int skip)
73{
74 char_u *p = arg;
75
76 while (ASCII_ISALNUM(*p) || *p == '_')
77 ++p;
78 if (arg == p || isdigit(*arg)
79 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
80 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
81 {
82 if (!skip)
83 semsg(_("E125: Illegal argument: %s"), arg);
84 return arg;
85 }
86 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
87 return arg;
88 if (newargs != NULL)
89 {
90 char_u *arg_copy;
91 int c;
92 int i;
93
94 c = *p;
95 *p = NUL;
96 arg_copy = vim_strsave(arg);
97 if (arg_copy == NULL)
98 {
99 *p = c;
100 return arg;
101 }
102
103 // Check for duplicate argument name.
104 for (i = 0; i < newargs->ga_len; ++i)
105 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
106 {
107 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
108 vim_free(arg_copy);
109 return arg;
110 }
111 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
112 newargs->ga_len++;
113
114 *p = c;
115 }
116
117 // get any type from "arg: type"
118 if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
119 {
120 char_u *type = NULL;
121
122 if (*p == ':')
123 {
124 type = skipwhite(p + 1);
125 p = skip_type(type);
126 type = vim_strnsave(type, p - type);
127 }
128 else if (*skipwhite(p) == ':')
129 emsg(_("E1059: No white space allowed before :"));
130 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
131 }
132
133 return p;
134}
135
136/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200137 * Get function arguments.
138 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200140get_function_args(
141 char_u **argp,
142 char_u endchar,
143 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144 garray_T *argtypes, // NULL unless using :def
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200145 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200146 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200147 int skip,
148 exarg_T *eap,
149 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200150{
151 int mustend = FALSE;
152 char_u *arg = *argp;
153 char_u *p = arg;
154 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200155 int any_default = FALSE;
156 char_u *expr;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200157 char_u *whitep = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200158
159 if (newargs != NULL)
160 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161 if (argtypes != NULL)
162 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200163 if (default_args != NULL)
164 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200165
166 if (varargs != NULL)
167 *varargs = FALSE;
168
169 /*
170 * Isolate the arguments: "arg1, arg2, ...)"
171 */
172 while (*p != endchar)
173 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200174 while (eap != NULL && eap->getline != NULL
175 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200176 {
177 char_u *theline;
178
179 // End of the line, get the next one.
180 theline = eap->getline(':', eap->cookie, 0, TRUE);
181 if (theline == NULL)
182 break;
183 vim_free(*line_to_free);
184 *line_to_free = theline;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200185 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200186 p = skipwhite(theline);
187 }
188
189 if (mustend && *p != endchar)
190 {
191 if (!skip)
192 semsg(_(e_invarg2), *argp);
193 break;
194 }
195 if (*p == endchar)
196 break;
197
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200198 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
199 {
200 if (varargs != NULL)
201 *varargs = TRUE;
202 p += 3;
203 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204
205 if (argtypes != NULL)
206 {
207 // ...name: list<type>
208 if (!ASCII_ISALPHA(*p))
209 {
210 emsg(_("E1055: Missing name after ..."));
211 break;
212 }
213
214 arg = p;
215 p = one_function_arg(p, newargs, argtypes, skip);
216 if (p == arg)
217 break;
218 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200219 }
220 else
221 {
222 arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100223 p = one_function_arg(p, newargs, argtypes, skip);
224 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200225 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200226
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200227 if (*skipwhite(p) == '=' && default_args != NULL)
228 {
229 typval_T rettv;
230
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100231 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200232 any_default = TRUE;
233 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200234 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200235 p = skipwhite(p);
236 expr = p;
237 if (eval1(&p, &rettv, FALSE) != FAIL)
238 {
239 if (ga_grow(default_args, 1) == FAIL)
240 goto err_ret;
241
242 // trim trailing whitespace
243 while (p > expr && VIM_ISWHITE(p[-1]))
244 p--;
245 c = *p;
246 *p = NUL;
247 expr = vim_strsave(expr);
248 if (expr == NULL)
249 {
250 *p = c;
251 goto err_ret;
252 }
253 ((char_u **)(default_args->ga_data))
254 [default_args->ga_len] = expr;
255 default_args->ga_len++;
256 *p = c;
257 }
258 else
259 mustend = TRUE;
260 }
261 else if (any_default)
262 {
263 emsg(_("E989: Non-default argument follows default argument"));
264 mustend = TRUE;
265 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200266 if (*p == ',')
267 ++p;
268 else
269 mustend = TRUE;
270 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200271 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200272 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200273 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200274
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200275 if (*p != endchar)
276 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100277 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200278
279 *argp = p;
280 return OK;
281
282err_ret:
283 if (newargs != NULL)
284 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200285 if (default_args != NULL)
286 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200287 return FAIL;
288}
289
290/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200291 * Register function "fp" as using "current_funccal" as its scope.
292 */
293 static int
294register_closure(ufunc_T *fp)
295{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200296 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100297 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200298 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200299 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200300 fp->uf_scoped = current_funccal;
301 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200302
Bram Moolenaar58016442016-07-31 18:30:22 +0200303 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
304 return FAIL;
305 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
306 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200307 return OK;
308}
309
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100310 static void
311set_ufunc_name(ufunc_T *fp, char_u *name)
312{
313 STRCPY(fp->uf_name, name);
314
315 if (name[0] == K_SPECIAL)
316 {
317 fp->uf_name_exp = alloc(STRLEN(name) + 3);
318 if (fp->uf_name_exp != NULL)
319 {
320 STRCPY(fp->uf_name_exp, "<SNR>");
321 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
322 }
323 }
324}
325
Bram Moolenaar58016442016-07-31 18:30:22 +0200326/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200327 * Parse a lambda expression and get a Funcref from "*arg".
328 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
329 */
330 int
331get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
332{
333 garray_T newargs;
334 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200335 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200336 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100337 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200338 int varargs;
339 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200340 char_u *start = skipwhite(*arg + 1);
341 char_u *s, *e;
342 static int lambda_no = 0;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200343 int *old_eval_lavars = eval_lavars_used;
344 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200345
346 ga_init(&newargs);
347 ga_init(&newlines);
348
Bram Moolenaare38eab22019-12-05 21:50:01 +0100349 // First, check if this is a lambda expression. "->" must exist.
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200350 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE,
351 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200352 if (ret == FAIL || *start != '>')
353 return NOTDONE;
354
Bram Moolenaare38eab22019-12-05 21:50:01 +0100355 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200356 if (evaluate)
357 pnewargs = &newargs;
358 else
359 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200360 *arg = skipwhite(*arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100361 // TODO: argument types
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200362 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE,
363 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200364 if (ret == FAIL || **arg != '>')
365 goto errret;
366
Bram Moolenaare38eab22019-12-05 21:50:01 +0100367 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200368 if (evaluate)
369 eval_lavars_used = &eval_lavars;
370
Bram Moolenaare38eab22019-12-05 21:50:01 +0100371 // Get the start and the end of the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200372 *arg = skipwhite(*arg + 1);
373 s = *arg;
374 ret = skip_expr(arg);
375 if (ret == FAIL)
376 goto errret;
377 e = *arg;
378 *arg = skipwhite(*arg);
379 if (**arg != '}')
Bram Moolenaaree619e52020-03-28 21:38:06 +0100380 {
381 semsg(_("E451: Expected }: %s"), *arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200382 goto errret;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100383 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200384 ++*arg;
385
386 if (evaluate)
387 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200388 int len, flags = 0;
389 char_u *p;
390 char_u name[20];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200391
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200392 sprintf((char*)name, "<lambda>%d", ++lambda_no);
393
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200394 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200395 if (fp == NULL)
396 goto errret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397 fp->uf_dfunc_idx = -1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200398 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200399 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200400 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200401
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200402 ga_init2(&newlines, (int)sizeof(char_u *), 1);
403 if (ga_grow(&newlines, 1) == FAIL)
404 goto errret;
405
Bram Moolenaare38eab22019-12-05 21:50:01 +0100406 // Add "return " before the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200407 len = 7 + e - s + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200408 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200409 if (p == NULL)
410 goto errret;
411 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
412 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200413 vim_strncpy(p + 7, s, e - s);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200414 if (strstr((char *)p + 7, "a:") == NULL)
415 // No a: variables are used for sure.
416 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200417
418 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100419 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200420 hash_add(&func_hashtab, UF2HIKEY(fp));
421 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200422 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200423 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200424 if (current_funccal != NULL && eval_lavars)
425 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200426 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200427 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200428 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200429 }
430 else
431 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200432
433#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200434 if (prof_def_func())
435 func_do_profile(fp);
436#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200437 if (sandbox)
438 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200440 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200441 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200442 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200443 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100444 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200445
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200446 pt->pt_func = fp;
447 pt->pt_refcount = 1;
448 rettv->vval.v_partial = pt;
449 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200450 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200451
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200452 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200453 return OK;
454
455errret:
456 ga_clear_strings(&newargs);
457 ga_clear_strings(&newlines);
458 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100459 vim_free(pt);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200460 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200461 return FAIL;
462}
463
464/*
465 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
466 * name it contains, otherwise return "name".
467 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
468 * "partialp".
469 */
470 char_u *
471deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
472{
473 dictitem_T *v;
474 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200475 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200476
477 if (partialp != NULL)
478 *partialp = NULL;
479
480 cc = name[*lenp];
481 name[*lenp] = NUL;
482 v = find_var(name, NULL, no_autoload);
483 name[*lenp] = cc;
484 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
485 {
486 if (v->di_tv.vval.v_string == NULL)
487 {
488 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100489 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200490 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200491 s = v->di_tv.vval.v_string;
492 *lenp = (int)STRLEN(s);
493 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200494 }
495
496 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
497 {
498 partial_T *pt = v->di_tv.vval.v_partial;
499
500 if (pt == NULL)
501 {
502 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100503 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200504 }
505 if (partialp != NULL)
506 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200507 s = partial_name(pt);
508 *lenp = (int)STRLEN(s);
509 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200510 }
511
512 return name;
513}
514
515/*
516 * Give an error message with a function name. Handle <SNR> things.
517 * "ermsg" is to be passed without translation, use N_() instead of _().
518 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100519 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200520emsg_funcname(char *ermsg, char_u *name)
521{
522 char_u *p;
523
524 if (*name == K_SPECIAL)
525 p = concat_str((char_u *)"<SNR>", name + 3);
526 else
527 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100528 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200529 if (p != name)
530 vim_free(p);
531}
532
533/*
534 * Allocate a variable for the result of a function.
535 * Return OK or FAIL.
536 */
537 int
538get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200539 char_u *name, // name of the function
540 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200541 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200542 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200543 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200544{
545 char_u *argp;
546 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100547 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
548 int argcount = 0; // number of arguments found
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200549
550 /*
551 * Get the arguments.
552 */
553 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200554 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
555 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200556 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100557 argp = skipwhite(argp + 1); // skip the '(' or ','
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200558 if (*argp == ')' || *argp == ',' || *argp == NUL)
559 break;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200560 if (eval1(&argp, &argvars[argcount], funcexe->evaluate) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200561 {
562 ret = FAIL;
563 break;
564 }
565 ++argcount;
566 if (*argp != ',')
567 break;
568 }
569 if (*argp == ')')
570 ++argp;
571 else
572 ret = FAIL;
573
574 if (ret == OK)
575 {
576 int i = 0;
577
578 if (get_vim_var_nr(VV_TESTING))
579 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100580 // Prepare for calling test_garbagecollect_now(), need to know
581 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200582 if (funcargs.ga_itemsize == 0)
583 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
584 for (i = 0; i < argcount; ++i)
585 if (ga_grow(&funcargs, 1) == OK)
586 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
587 &argvars[i];
588 }
589
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200590 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200591
592 funcargs.ga_len -= i;
593 }
594 else if (!aborting())
595 {
596 if (argcount == MAX_FUNC_ARGS)
597 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
598 else
599 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
600 }
601
602 while (--argcount >= 0)
603 clear_tv(&argvars[argcount]);
604
605 *arg = skipwhite(argp);
606 return ret;
607}
608
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200609/*
610 * Return TRUE if "p" starts with "<SID>" or "s:".
611 * Only works if eval_fname_script() returned non-zero for "p"!
612 */
613 static int
614eval_fname_sid(char_u *p)
615{
616 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
617}
618
619/*
620 * In a script change <SID>name() and s:name() to K_SNR 123_name().
621 * Change <SNR>123_name() to K_SNR 123_name().
622 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
623 * (slow).
624 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100625 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200626fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
627{
628 int llen;
629 char_u *fname;
630 int i;
631
632 llen = eval_fname_script(name);
633 if (llen > 0)
634 {
635 fname_buf[0] = K_SPECIAL;
636 fname_buf[1] = KS_EXTRA;
637 fname_buf[2] = (int)KE_SNR;
638 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100639 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200640 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200641 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100642 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200643 else
644 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200645 sprintf((char *)fname_buf + 3, "%ld_",
646 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200647 i = (int)STRLEN(fname_buf);
648 }
649 }
650 if (i + STRLEN(name + llen) < FLEN_FIXED)
651 {
652 STRCPY(fname_buf + i, name + llen);
653 fname = fname_buf;
654 }
655 else
656 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200657 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200658 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100659 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200660 else
661 {
662 *tofree = fname;
663 mch_memmove(fname, fname_buf, (size_t)i);
664 STRCPY(fname + i, name + llen);
665 }
666 }
667 }
668 else
669 fname = name;
670 return fname;
671}
672
673/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100674 * Find a function "name" in script "sid".
675 */
676 static ufunc_T *
677find_func_with_sid(char_u *name, int sid)
678{
679 hashitem_T *hi;
680 char_u buffer[200];
681
682 buffer[0] = K_SPECIAL;
683 buffer[1] = KS_EXTRA;
684 buffer[2] = (int)KE_SNR;
685 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
686 (long)sid, name);
687 hi = hash_find(&func_hashtab, buffer);
688 if (!HASHITEM_EMPTY(hi))
689 return HI2UF(hi);
690
691 return NULL;
692}
693
694/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200695 * Find a function by name, return pointer to it in ufuncs.
696 * Return NULL for unknown function.
697 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 static ufunc_T *
699find_func_even_dead(char_u *name, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200700{
701 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100702 ufunc_T *func;
703 imported_T *imported;
704
705 if (in_vim9script())
706 {
707 // Find script-local function before global one.
708 func = find_func_with_sid(name, current_sctx.sc_sid);
709 if (func != NULL)
710 return func;
711
712 // Find imported funcion before global one.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +0100713 imported = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714 if (imported != NULL && imported->imp_funcname != NULL)
715 {
716 hi = hash_find(&func_hashtab, imported->imp_funcname);
717 if (!HASHITEM_EMPTY(hi))
718 return HI2UF(hi);
719 }
720 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200721
722 hi = hash_find(&func_hashtab, name);
723 if (!HASHITEM_EMPTY(hi))
724 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725
726 return NULL;
727}
728
729/*
730 * Find a function by name, return pointer to it in ufuncs.
731 * "cctx" is passed in a :def function to find imported functions.
732 * Return NULL for unknown or dead function.
733 */
734 ufunc_T *
735find_func(char_u *name, cctx_T *cctx)
736{
737 ufunc_T *fp = find_func_even_dead(name, cctx);
738
739 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
740 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200741 return NULL;
742}
743
744/*
745 * Copy the function name of "fp" to buffer "buf".
746 * "buf" must be able to hold the function name plus three bytes.
747 * Takes care of script-local function names.
748 */
749 static void
750cat_func_name(char_u *buf, ufunc_T *fp)
751{
752 if (fp->uf_name[0] == K_SPECIAL)
753 {
754 STRCPY(buf, "<SNR>");
755 STRCAT(buf, fp->uf_name + 3);
756 }
757 else
758 STRCPY(buf, fp->uf_name);
759}
760
761/*
762 * Add a number variable "name" to dict "dp" with value "nr".
763 */
764 static void
765add_nr_var(
766 dict_T *dp,
767 dictitem_T *v,
768 char *name,
769 varnumber_T nr)
770{
771 STRCPY(v->di_key, name);
772 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
773 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
774 v->di_tv.v_type = VAR_NUMBER;
775 v->di_tv.v_lock = VAR_FIXED;
776 v->di_tv.vval.v_number = nr;
777}
778
779/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100780 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200781 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100782 static void
783free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200784{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100785 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200786
787 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
788 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100789 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200790
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100791 // When garbage collecting a funccall_T may be freed before the
792 // function that references it, clear its uf_scoped field.
793 // The function may have been redefined and point to another
794 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200795 if (fp != NULL && fp->uf_scoped == fc)
796 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200797 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200798 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200799
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200800 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200801 vim_free(fc);
802}
803
804/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100805 * Free "fc" and what it contains.
806 * Can be called only when "fc" is kept beyond the period of it called,
807 * i.e. after cleanup_function_call(fc).
808 */
809 static void
810free_funccal_contents(funccall_T *fc)
811{
812 listitem_T *li;
813
814 // Free all l: variables.
815 vars_clear(&fc->l_vars.dv_hashtab);
816
817 // Free all a: variables.
818 vars_clear(&fc->l_avars.dv_hashtab);
819
820 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200821 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100822 clear_tv(&li->li_tv);
823
824 free_funccal(fc);
825}
826
827/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200828 * Handle the last part of returning from a function: free the local hashtable.
829 * Unless it is still in use by a closure.
830 */
831 static void
832cleanup_function_call(funccall_T *fc)
833{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100834 int may_free_fc = fc->fc_refcount <= 0;
835 int free_fc = TRUE;
836
Bram Moolenaar6914c642017-04-01 21:21:30 +0200837 current_funccal = fc->caller;
838
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100839 // Free all l: variables if not referred.
840 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
841 vars_clear(&fc->l_vars.dv_hashtab);
842 else
843 free_fc = FALSE;
844
845 // If the a:000 list and the l: and a: dicts are not referenced and
846 // there is no closure using it, we can free the funccall_T and what's
847 // in it.
848 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
849 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200850 else
851 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100852 int todo;
853 hashitem_T *hi;
854 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200855
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100856 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200857
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100858 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200859 todo = (int)fc->l_avars.dv_hashtab.ht_used;
860 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
861 {
862 if (!HASHITEM_EMPTY(hi))
863 {
864 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100865 di = HI2DI(hi);
866 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200867 }
868 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100869 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200870
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100871 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
872 fc->l_varlist.lv_first = NULL;
873 else
874 {
875 listitem_T *li;
876
877 free_fc = FALSE;
878
879 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200880 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +0200881 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100882 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100883
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100884 if (free_fc)
885 free_funccal(fc);
886 else
887 {
888 static int made_copy = 0;
889
890 // "fc" is still in use. This can happen when returning "a:000",
891 // assigning "l:" to a global variable or defining a closure.
892 // Link "fc" in the list for garbage collection later.
893 fc->caller = previous_funccal;
894 previous_funccal = fc;
895
896 if (want_garbage_collect)
897 // If garbage collector is ready, clear count.
898 made_copy = 0;
899 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100900 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100901 // We have made a lot of copies, worth 4 Mbyte. This can happen
902 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100903 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100904 // too much memory.
905 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100906 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100907 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200908 }
909}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100910/*
911 * Unreference "fc": decrement the reference count and free it when it
912 * becomes zero. "fp" is detached from "fc".
913 * When "force" is TRUE we are exiting.
914 */
915 static void
916funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
917{
918 funccall_T **pfc;
919 int i;
920
921 if (fc == NULL)
922 return;
923
924 if (--fc->fc_refcount <= 0 && (force || (
925 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
926 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
927 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
928 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
929 {
930 if (fc == *pfc)
931 {
932 *pfc = fc->caller;
933 free_funccal_contents(fc);
934 return;
935 }
936 }
937 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
938 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
939 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
940}
941
942/*
943 * Remove the function from the function hashtable. If the function was
944 * deleted while it still has references this was already done.
945 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
946 */
947 static int
948func_remove(ufunc_T *fp)
949{
950 hashitem_T *hi;
951
952 // Return if it was already virtually deleted.
953 if (fp->uf_flags & FC_DEAD)
954 return FALSE;
955
956 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
957 if (!HASHITEM_EMPTY(hi))
958 {
959 // When there is a def-function index do not actually remove the
960 // function, so we can find the index when defining the function again.
961 if (fp->uf_dfunc_idx >= 0)
962 fp->uf_flags |= FC_DEAD;
963 else
964 hash_remove(&func_hashtab, hi);
965 return TRUE;
966 }
967 return FALSE;
968}
969
970 static void
971func_clear_items(ufunc_T *fp)
972{
973 ga_clear_strings(&(fp->uf_args));
974 ga_clear_strings(&(fp->uf_def_args));
975 ga_clear_strings(&(fp->uf_lines));
976 VIM_CLEAR(fp->uf_name_exp);
977 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100978 VIM_CLEAR(fp->uf_def_arg_idx);
979 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200980 while (fp->uf_type_list.ga_len > 0)
981 vim_free(((type_T **)fp->uf_type_list.ga_data)
982 [--fp->uf_type_list.ga_len]);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100983 ga_clear(&fp->uf_type_list);
984#ifdef FEAT_PROFILE
985 VIM_CLEAR(fp->uf_tml_count);
986 VIM_CLEAR(fp->uf_tml_total);
987 VIM_CLEAR(fp->uf_tml_self);
988#endif
989}
990
991/*
992 * Free all things that a function contains. Does not free the function
993 * itself, use func_free() for that.
994 * When "force" is TRUE we are exiting.
995 */
996 static void
997func_clear(ufunc_T *fp, int force)
998{
999 if (fp->uf_cleared)
1000 return;
1001 fp->uf_cleared = TRUE;
1002
1003 // clear this function
1004 func_clear_items(fp);
1005 funccal_unref(fp->uf_scoped, fp, force);
1006 delete_def_function(fp);
1007}
1008
1009/*
1010 * Free a function and remove it from the list of functions. Does not free
1011 * what a function contains, call func_clear() first.
1012 */
1013 static void
1014func_free(ufunc_T *fp)
1015{
1016 // Only remove it when not done already, otherwise we would remove a newer
1017 // version of the function with the same name.
1018 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1019 func_remove(fp);
1020
1021 if ((fp->uf_flags & FC_DEAD) == 0)
1022 vim_free(fp);
1023}
1024
1025/*
1026 * Free all things that a function contains and free the function itself.
1027 * When "force" is TRUE we are exiting.
1028 */
1029 static void
1030func_clear_free(ufunc_T *fp, int force)
1031{
1032 func_clear(fp, force);
1033 func_free(fp);
1034}
1035
Bram Moolenaar6914c642017-04-01 21:21:30 +02001036
1037/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001038 * Call a user function.
1039 */
1040 static void
1041call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001042 ufunc_T *fp, // pointer to function
1043 int argcount, // nr of args
1044 typval_T *argvars, // arguments
1045 typval_T *rettv, // return value
1046 linenr_T firstline, // first line of range
1047 linenr_T lastline, // last line of range
1048 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001049{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001050 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001051 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001052 funccall_T *fc;
1053 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001054 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001055 static int depth = 0;
1056 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001057 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001058 int i;
1059 int ai;
1060 int islambda = FALSE;
1061 char_u numbuf[NUMBUFLEN];
1062 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001063#ifdef FEAT_PROFILE
1064 proftime_T wait_start;
1065 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001066 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001067#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001068 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001069
Bram Moolenaare38eab22019-12-05 21:50:01 +01001070 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001071 if (depth >= p_mfd)
1072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001073 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001074 rettv->v_type = VAR_NUMBER;
1075 rettv->vval.v_number = -1;
1076 return;
1077 }
1078 ++depth;
1079
Bram Moolenaare38eab22019-12-05 21:50:01 +01001080 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001081
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001082 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001083 if (fc == NULL)
1084 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001085 fc->caller = current_funccal;
1086 current_funccal = fc;
1087 fc->func = fp;
1088 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001089 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001090 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001091 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1092 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001093 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001094 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001095 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001096
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 if (fp->uf_dfunc_idx >= 0)
1098 {
1099 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001100 save_current_sctx = current_sctx;
1101 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001102
1103 // Execute the compiled function.
1104 call_def_function(fp, argcount, argvars, rettv);
1105 --depth;
1106 current_funccal = fc->caller;
1107
1108 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001109 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 free_funccal(fc);
1111 return;
1112 }
1113
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001114 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1115 islambda = TRUE;
1116
1117 /*
1118 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1119 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1120 * each argument variable and saves a lot of time.
1121 */
1122 /*
1123 * Init l: variables.
1124 */
1125 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1126 if (selfdict != NULL)
1127 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001128 // Set l:self to "selfdict". Use "name" to avoid a warning from
1129 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001130 v = &fc->fixvar[fixvar_idx++].var;
1131 name = v->di_key;
1132 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001133 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001134 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1135 v->di_tv.v_type = VAR_DICT;
1136 v->di_tv.v_lock = 0;
1137 v->di_tv.vval.v_dict = selfdict;
1138 ++selfdict->dv_refcount;
1139 }
1140
1141 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001142 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001143 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001144 * Set a:000 to a list with room for the "..." arguments.
1145 */
1146 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001147 if ((fp->uf_flags & FC_NOARGS) == 0)
1148 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001149 (varnumber_T)(argcount >= fp->uf_args.ga_len
1150 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001151 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001152 if ((fp->uf_flags & FC_NOARGS) == 0)
1153 {
1154 // Use "name" to avoid a warning from some compiler that checks the
1155 // destination size.
1156 v = &fc->fixvar[fixvar_idx++].var;
1157 name = v->di_key;
1158 STRCPY(name, "000");
1159 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1160 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1161 v->di_tv.v_type = VAR_LIST;
1162 v->di_tv.v_lock = VAR_FIXED;
1163 v->di_tv.vval.v_list = &fc->l_varlist;
1164 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001165 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001166 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1167 fc->l_varlist.lv_lock = VAR_FIXED;
1168
1169 /*
1170 * Set a:firstline to "firstline" and a:lastline to "lastline".
1171 * Set a:name to named arguments.
1172 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001173 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001174 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001175 if ((fp->uf_flags & FC_NOARGS) == 0)
1176 {
1177 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001178 (varnumber_T)firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001179 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001180 (varnumber_T)lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001181 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001182 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001183 {
1184 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001185 typval_T def_rettv;
1186 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001187
1188 ai = i - fp->uf_args.ga_len;
1189 if (ai < 0)
1190 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001191 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001192 name = FUNCARG(fp, i);
1193 if (islambda)
1194 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001195
1196 // evaluate named argument default expression
1197 isdefault = ai + fp->uf_def_args.ga_len >= 0
1198 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1199 && argvars[i].vval.v_number == VVAL_NONE));
1200 if (isdefault)
1201 {
1202 char_u *default_expr = NULL;
1203 def_rettv.v_type = VAR_NUMBER;
1204 def_rettv.vval.v_number = -1;
1205
1206 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1207 [ai + fp->uf_def_args.ga_len];
1208 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL)
1209 {
1210 default_arg_err = 1;
1211 break;
1212 }
1213 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001214 }
1215 else
1216 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001217 if ((fp->uf_flags & FC_NOARGS) != 0)
1218 // Bail out if no a: arguments used (in lambda).
1219 break;
1220
Bram Moolenaare38eab22019-12-05 21:50:01 +01001221 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001222 sprintf((char *)numbuf, "%d", ai + 1);
1223 name = numbuf;
1224 }
1225 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1226 {
1227 v = &fc->fixvar[fixvar_idx++].var;
1228 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001229 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001230 }
1231 else
1232 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001233 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001234 if (v == NULL)
1235 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001236 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001237 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001238
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001239 // Note: the values are copied directly to avoid alloc/free.
1240 // "argvars" must have VAR_FIXED for v_lock.
1241 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001242 v->di_tv.v_lock = VAR_FIXED;
1243
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001244 if (addlocal)
1245 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001246 // Named arguments should be accessed without the "a:" prefix in
1247 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001248 copy_tv(&v->di_tv, &v->di_tv);
1249 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001250 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001251 else
1252 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001253
1254 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1255 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001256 listitem_T *li = &fc->l_listitems[ai];
1257
1258 li->li_tv = argvars[i];
1259 li->li_tv.v_lock = VAR_FIXED;
1260 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001261 }
1262 }
1263
Bram Moolenaare38eab22019-12-05 21:50:01 +01001264 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001265 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001266
1267 if (fp->uf_flags & FC_SANDBOX)
1268 {
1269 using_sandbox = TRUE;
1270 ++sandbox;
1271 }
1272
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001273 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001274 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001275 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001276 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001277 ++no_wait_return;
1278 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001279
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001280 smsg(_("calling %s"), SOURCING_NAME);
1281 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001282 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001283 char_u buf[MSG_BUF_LEN];
1284 char_u numbuf2[NUMBUFLEN];
1285 char_u *tofree;
1286 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001287
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001288 msg_puts("(");
1289 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001290 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001291 if (i > 0)
1292 msg_puts(", ");
1293 if (argvars[i].v_type == VAR_NUMBER)
1294 msg_outnum((long)argvars[i].vval.v_number);
1295 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001296 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001297 // Do not want errors such as E724 here.
1298 ++emsg_off;
1299 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1300 --emsg_off;
1301 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001302 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001303 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001304 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001305 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1306 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001307 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001308 msg_puts((char *)s);
1309 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001310 }
1311 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001312 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001313 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001314 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001315 msg_puts("\n"); // don't overwrite this either
1316
1317 verbose_leave_scroll();
1318 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001319 }
1320#ifdef FEAT_PROFILE
1321 if (do_profiling == PROF_YES)
1322 {
1323 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001324 {
1325 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001326 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001327 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001328 if (fp->uf_profiling
1329 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1330 {
1331 ++fp->uf_tm_count;
1332 profile_start(&call_start);
1333 profile_zero(&fp->uf_tm_children);
1334 }
1335 script_prof_save(&wait_start);
1336 }
1337#endif
1338
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001339 save_current_sctx = current_sctx;
1340 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001341 save_did_emsg = did_emsg;
1342 did_emsg = FALSE;
1343
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001344 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1345 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001346 else if (islambda)
1347 {
1348 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1349
1350 // A Lambda always has the command "return {expr}". It is much faster
1351 // to evaluate {expr} directly.
1352 ++ex_nesting_level;
Bram Moolenaard1e9dc22020-04-03 18:13:57 +02001353 (void)eval1(&p, rettv, TRUE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001354 --ex_nesting_level;
1355 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001356 else
1357 // call do_cmdline() to execute the lines
1358 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001359 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1360
1361 --RedrawingDisabled;
1362
Bram Moolenaare38eab22019-12-05 21:50:01 +01001363 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001364 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1365 {
1366 clear_tv(rettv);
1367 rettv->v_type = VAR_NUMBER;
1368 rettv->vval.v_number = -1;
1369 }
1370
1371#ifdef FEAT_PROFILE
1372 if (do_profiling == PROF_YES && (fp->uf_profiling
1373 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1374 {
1375 profile_end(&call_start);
1376 profile_sub_wait(&wait_start, &call_start);
1377 profile_add(&fp->uf_tm_total, &call_start);
1378 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1379 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1380 {
1381 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1382 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1383 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001384 if (started_profiling)
1385 // make a ":profdel func" stop profiling the function
1386 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001387 }
1388#endif
1389
Bram Moolenaare38eab22019-12-05 21:50:01 +01001390 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001391 if (p_verbose >= 12)
1392 {
1393 ++no_wait_return;
1394 verbose_enter_scroll();
1395
1396 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001397 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001398 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001399 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001400 (long)fc->rettv->vval.v_number);
1401 else
1402 {
1403 char_u buf[MSG_BUF_LEN];
1404 char_u numbuf2[NUMBUFLEN];
1405 char_u *tofree;
1406 char_u *s;
1407
Bram Moolenaare38eab22019-12-05 21:50:01 +01001408 // The value may be very long. Skip the middle part, so that we
1409 // have some idea how it starts and ends. smsg() would always
1410 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001411 ++emsg_off;
1412 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1413 --emsg_off;
1414 if (s != NULL)
1415 {
1416 if (vim_strsize(s) > MSG_BUF_CLEN)
1417 {
1418 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1419 s = buf;
1420 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001421 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001422 vim_free(tofree);
1423 }
1424 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001425 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001426
1427 verbose_leave_scroll();
1428 --no_wait_return;
1429 }
1430
Bram Moolenaare31ee862020-01-07 20:59:34 +01001431 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001432 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001433 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001434#ifdef FEAT_PROFILE
1435 if (do_profiling == PROF_YES)
1436 script_prof_restore(&wait_start);
1437#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001438 if (using_sandbox)
1439 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001440
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001441 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001442 {
1443 ++no_wait_return;
1444 verbose_enter_scroll();
1445
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001446 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001447 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001448
1449 verbose_leave_scroll();
1450 --no_wait_return;
1451 }
1452
1453 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001454 --depth;
1455
Bram Moolenaar6914c642017-04-01 21:21:30 +02001456 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001457}
1458
1459/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001461 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 int
1463call_user_func_check(
1464 ufunc_T *fp,
1465 int argcount,
1466 typval_T *argvars,
1467 typval_T *rettv,
1468 funcexe_T *funcexe,
1469 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001470{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 int error;
1472 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1475 *funcexe->doesrange = TRUE;
1476 if (argcount < regular_args - fp->uf_def_args.ga_len)
1477 error = FCERR_TOOFEW;
1478 else if (!has_varargs(fp) && argcount > regular_args)
1479 error = FCERR_TOOMANY;
1480 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1481 error = FCERR_DICT;
1482 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001483 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001484 int did_save_redo = FALSE;
1485 save_redo_T save_redo;
1486
1487 /*
1488 * Call the user function.
1489 * Save and restore search patterns, script variables and
1490 * redo buffer.
1491 */
1492 save_search_patterns();
1493 if (!ins_compl_active())
1494 {
1495 saveRedobuff(&save_redo);
1496 did_save_redo = TRUE;
1497 }
1498 ++fp->uf_calls;
1499 call_user_func(fp, argcount, argvars, rettv,
1500 funcexe->firstline, funcexe->lastline,
1501 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
1502 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1503 // Function was unreferenced while being used, free it now.
1504 func_clear_free(fp, FALSE);
1505 if (did_save_redo)
1506 restoreRedobuff(&save_redo);
1507 restore_search_patterns();
1508 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001509 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001511}
1512
1513/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001514 * There are two kinds of function names:
1515 * 1. ordinary names, function defined with :function
1516 * 2. numbered functions and lambdas
1517 * For the first we only count the name stored in func_hashtab as a reference,
1518 * using function() does not count as a reference, because the function is
1519 * looked up by name.
1520 */
1521 static int
1522func_name_refcount(char_u *name)
1523{
1524 return isdigit(*name) || *name == '<';
1525}
1526
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001527static funccal_entry_T *funccal_stack = NULL;
1528
1529/*
1530 * Save the current function call pointer, and set it to NULL.
1531 * Used when executing autocommands and for ":source".
1532 */
1533 void
1534save_funccal(funccal_entry_T *entry)
1535{
1536 entry->top_funccal = current_funccal;
1537 entry->next = funccal_stack;
1538 funccal_stack = entry;
1539 current_funccal = NULL;
1540}
1541
1542 void
1543restore_funccal(void)
1544{
1545 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001546 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001547 else
1548 {
1549 current_funccal = funccal_stack->top_funccal;
1550 funccal_stack = funccal_stack->next;
1551 }
1552}
1553
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001554 funccall_T *
1555get_current_funccal(void)
1556{
1557 return current_funccal;
1558}
1559
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001560#if defined(EXITFREE) || defined(PROTO)
1561 void
1562free_all_functions(void)
1563{
1564 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001565 ufunc_T *fp;
1566 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001567 long_u todo = 1;
1568 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001569
Bram Moolenaare38eab22019-12-05 21:50:01 +01001570 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001571 while (current_funccal != NULL)
1572 {
1573 clear_tv(current_funccal->rettv);
1574 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001575 if (current_funccal == NULL && funccal_stack != NULL)
1576 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001577 }
1578
Bram Moolenaare38eab22019-12-05 21:50:01 +01001579 // First clear what the functions contain. Since this may lower the
1580 // reference count of a function, it may also free a function and change
1581 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001582 while (todo > 0)
1583 {
1584 todo = func_hashtab.ht_used;
1585 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1586 if (!HASHITEM_EMPTY(hi))
1587 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 // clear the def function index now
1589 fp = HI2UF(hi);
1590 fp->uf_flags &= ~FC_DEAD;
1591 fp->uf_dfunc_idx = -1;
1592
Bram Moolenaare38eab22019-12-05 21:50:01 +01001593 // Only free functions that are not refcounted, those are
1594 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001595 if (func_name_refcount(fp->uf_name))
1596 ++skipped;
1597 else
1598 {
1599 used = func_hashtab.ht_used;
1600 func_clear(fp, TRUE);
1601 if (used != func_hashtab.ht_used)
1602 {
1603 skipped = 0;
1604 break;
1605 }
1606 }
1607 --todo;
1608 }
1609 }
1610
Bram Moolenaare38eab22019-12-05 21:50:01 +01001611 // Now actually free the functions. Need to start all over every time,
1612 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001613 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001614 while (func_hashtab.ht_used > skipped)
1615 {
1616 todo = func_hashtab.ht_used;
1617 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001618 if (!HASHITEM_EMPTY(hi))
1619 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001620 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001621 // Only free functions that are not refcounted, those are
1622 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001623 fp = HI2UF(hi);
1624 if (func_name_refcount(fp->uf_name))
1625 ++skipped;
1626 else
1627 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001628 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001629 skipped = 0;
1630 break;
1631 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001632 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001633 }
1634 if (skipped == 0)
1635 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636
1637 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001638}
1639#endif
1640
1641/*
1642 * Return TRUE if "name" looks like a builtin function name: starts with a
1643 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1644 * "len" is the length of "name", or -1 for NUL terminated.
1645 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001647builtin_function(char_u *name, int len)
1648{
1649 char_u *p;
1650
1651 if (!ASCII_ISLOWER(name[0]))
1652 return FALSE;
1653 p = vim_strchr(name, AUTOLOAD_CHAR);
1654 return p == NULL || (len > 0 && p > name + len);
1655}
1656
1657 int
1658func_call(
1659 char_u *name,
1660 typval_T *args,
1661 partial_T *partial,
1662 dict_T *selfdict,
1663 typval_T *rettv)
1664{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001665 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001666 listitem_T *item;
1667 typval_T argv[MAX_FUNC_ARGS + 1];
1668 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001669 int r = 0;
1670
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001671 range_list_materialize(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001672 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001673 {
1674 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1675 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001676 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001677 break;
1678 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001679 // Make a copy of each argument. This is needed to be able to set
1680 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001681 copy_tv(&item->li_tv, &argv[argc++]);
1682 }
1683
1684 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001685 {
1686 funcexe_T funcexe;
1687
Bram Moolenaara80faa82020-04-12 19:37:17 +02001688 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001689 funcexe.firstline = curwin->w_cursor.lnum;
1690 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001691 funcexe.evaluate = TRUE;
1692 funcexe.partial = partial;
1693 funcexe.selfdict = selfdict;
1694 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1695 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001696
Bram Moolenaare38eab22019-12-05 21:50:01 +01001697 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001698 while (argc > 0)
1699 clear_tv(&argv[--argc]);
1700
1701 return r;
1702}
1703
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001704static int callback_depth = 0;
1705
1706 int
1707get_callback_depth(void)
1708{
1709 return callback_depth;
1710}
1711
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001712/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001713 * Invoke call_func() with a callback.
1714 */
1715 int
1716call_callback(
1717 callback_T *callback,
1718 int len, // length of "name" or -1 to use strlen()
1719 typval_T *rettv, // return value goes here
1720 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001721 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001722 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001723{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001724 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001725 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001726
Bram Moolenaara80faa82020-04-12 19:37:17 +02001727 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001728 funcexe.evaluate = TRUE;
1729 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001730 ++callback_depth;
1731 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1732 --callback_depth;
1733 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001734}
1735
1736/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 * Give an error message for the result of a function.
1738 * Nothing if "error" is FCERR_NONE.
1739 */
1740 void
1741user_func_error(int error, char_u *name)
1742{
1743 switch (error)
1744 {
1745 case FCERR_UNKNOWN:
1746 emsg_funcname(e_unknownfunc, name);
1747 break;
1748 case FCERR_NOTMETHOD:
1749 emsg_funcname(
1750 N_("E276: Cannot use function as a method: %s"), name);
1751 break;
1752 case FCERR_DELETED:
1753 emsg_funcname(N_(e_func_deleted), name);
1754 break;
1755 case FCERR_TOOMANY:
1756 emsg_funcname((char *)e_toomanyarg, name);
1757 break;
1758 case FCERR_TOOFEW:
1759 emsg_funcname((char *)e_toofewarg, name);
1760 break;
1761 case FCERR_SCRIPT:
1762 emsg_funcname(
1763 N_("E120: Using <SID> not in a script context: %s"), name);
1764 break;
1765 case FCERR_DICT:
1766 emsg_funcname(
1767 N_("E725: Calling dict function without Dictionary: %s"),
1768 name);
1769 break;
1770 }
1771}
1772
1773/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001774 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001775 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001776 * Return FAIL when the function can't be called, OK otherwise.
1777 * Also returns OK when an error was encountered while executing the function.
1778 */
1779 int
1780call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001781 char_u *funcname, // name of the function
1782 int len, // length of "name" or -1 to use strlen()
1783 typval_T *rettv, // return value goes here
1784 int argcount_in, // number of "argvars"
1785 typval_T *argvars_in, // vars for arguments, must have "argcount"
1786 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001787 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788{
1789 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001790 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001791 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001792 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001793 char_u fname_buf[FLEN_FIXED + 1];
1794 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001795 char_u *fname = NULL;
1796 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001797 int argcount = argcount_in;
1798 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001799 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001800 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1801 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001802 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001803 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001804 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001806 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1807 // even when call_func() returns FAIL.
1808 rettv->v_type = VAR_UNKNOWN;
1809
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001810 if (partial != NULL)
1811 fp = partial->pt_func;
1812 if (fp == NULL)
1813 {
1814 // Make a copy of the name, if it comes from a funcref variable it
1815 // could be changed or deleted in the called function.
1816 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
1817 if (name == NULL)
1818 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001819
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001820 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1821 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001822
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001823 if (funcexe->doesrange != NULL)
1824 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001825
1826 if (partial != NULL)
1827 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001828 // When the function has a partial with a dict and there is a dict
1829 // argument, use the dict argument. That is backwards compatible.
1830 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001831 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001832 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001833 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001834 {
1835 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001836 {
1837 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1838 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001839 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001840 goto theend;
1841 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001842 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001843 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001844 for (i = 0; i < argcount_in; ++i)
1845 argv[i + argv_clear] = argvars_in[i];
1846 argvars = argv;
1847 argcount = partial->pt_argc + argcount_in;
1848 }
1849 }
1850
Bram Moolenaaref140542019-12-31 21:27:13 +01001851 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001852 {
1853 char_u *rfname = fname;
1854
Bram Moolenaare38eab22019-12-05 21:50:01 +01001855 // Ignore "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001856 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001857 rfname = fname + 2;
1858
Bram Moolenaare38eab22019-12-05 21:50:01 +01001859 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001860 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001861 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001862
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001863 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001864 {
1865 /*
1866 * User defined function.
1867 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001868 if (fp == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001870
Bram Moolenaare38eab22019-12-05 21:50:01 +01001871 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001872 if (fp == NULL
1873 && apply_autocmds(EVENT_FUNCUNDEFINED,
1874 rfname, rfname, TRUE, NULL)
1875 && !aborting())
1876 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001877 // executed an autocommand, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001879 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001880 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001881 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1882 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001883 // loaded a package, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001885 }
1886
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001887 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01001888 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001889 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001890 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001891 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001892 // postponed filling in the arguments, do it now
1893 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001894 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001895
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001896 if (funcexe->basetv != NULL)
1897 {
1898 // Method call: base->Method()
1899 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1900 argv[0] = *funcexe->basetv;
1901 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001902 argvars = argv;
1903 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001904 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001905
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 error = call_user_func_check(fp, argcount, argvars, rettv,
1907 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001908 }
1909 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001910 else if (funcexe->basetv != NULL)
1911 {
1912 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001913 * expr->method(): Find the method name in the table, call its
1914 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02001915 */
1916 error = call_internal_method(fname, argcount, argvars, rettv,
1917 funcexe->basetv);
1918 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001919 else
1920 {
1921 /*
1922 * Find the function name in the table, call its implementation.
1923 */
1924 error = call_internal_func(fname, argcount, argvars, rettv);
1925 }
1926 /*
1927 * The function call (or "FuncUndefined" autocommand sequence) might
1928 * have been aborted by an error, an interrupt, or an explicitly thrown
1929 * exception that has not been caught so far. This situation can be
1930 * tested for by calling aborting(). For an error in an internal
1931 * function or for the "E132" error in call_user_func(), however, the
1932 * throw point at which the "force_abort" flag (temporarily reset by
1933 * emsg()) is normally updated has not been reached yet. We need to
1934 * update that flag first to make aborting() reliable.
1935 */
1936 update_force_abort();
1937 }
Bram Moolenaaref140542019-12-31 21:27:13 +01001938 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001939 ret = OK;
1940
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001941theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001942 /*
1943 * Report an error unless the argument evaluation or function call has been
1944 * cancelled due to an aborting error, an interrupt, or an exception.
1945 */
1946 if (!aborting())
1947 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001948 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001949 }
1950
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001951 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001952 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001953 clear_tv(&argv[--argv_clear + argv_base]);
1954
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001955 vim_free(tofree);
1956 vim_free(name);
1957
1958 return ret;
1959}
1960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 static char_u *
1962printable_func_name(ufunc_T *fp)
1963{
1964 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
1965}
1966
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001967/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001968 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001969 */
1970 static void
1971list_func_head(ufunc_T *fp, int indent)
1972{
1973 int j;
1974
1975 msg_start();
1976 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001977 msg_puts(" ");
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001978 if (fp->uf_dfunc_idx >= 0)
1979 msg_puts("def ");
1980 else
1981 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001983 msg_putchar('(');
1984 for (j = 0; j < fp->uf_args.ga_len; ++j)
1985 {
1986 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001987 msg_puts(", ");
1988 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 if (fp->uf_arg_types != NULL)
1990 {
1991 char *tofree;
1992
1993 msg_puts(": ");
1994 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
1995 vim_free(tofree);
1996 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001997 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
1998 {
1999 msg_puts(" = ");
2000 msg_puts(((char **)(fp->uf_def_args.ga_data))
2001 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2002 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002003 }
2004 if (fp->uf_varargs)
2005 {
2006 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002007 msg_puts(", ");
2008 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002009 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 if (fp->uf_va_name != NULL)
2011 {
2012 if (j)
2013 msg_puts(", ");
2014 msg_puts("...");
2015 msg_puts((char *)fp->uf_va_name);
2016 if (fp->uf_va_type)
2017 {
2018 char *tofree;
2019
2020 msg_puts(": ");
2021 msg_puts(type_name(fp->uf_va_type, &tofree));
2022 vim_free(tofree);
2023 }
2024 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002025 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002026
2027 if (fp->uf_dfunc_idx >= 0)
2028 {
2029 if (fp->uf_ret_type != &t_void)
2030 {
2031 char *tofree;
2032
2033 msg_puts(": ");
2034 msg_puts(type_name(fp->uf_ret_type, &tofree));
2035 vim_free(tofree);
2036 }
2037 }
2038 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002039 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002040 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002041 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002042 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002043 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002044 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002045 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002046 msg_clr_eos();
2047 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002048 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002049}
2050
2051/*
2052 * Get a function name, translating "<SID>" and "<SNR>".
2053 * Also handles a Funcref in a List or Dictionary.
2054 * Returns the function name in allocated memory, or NULL for failure.
2055 * flags:
2056 * TFN_INT: internal function name OK
2057 * TFN_QUIET: be quiet
2058 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002059 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002060 * Advances "pp" to just after the function name (if no error).
2061 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002062 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002063trans_function_name(
2064 char_u **pp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002065 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002066 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002067 funcdict_T *fdp, // return: info about dictionary used
2068 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002069{
2070 char_u *name = NULL;
2071 char_u *start;
2072 char_u *end;
2073 int lead;
2074 char_u sid_buf[20];
2075 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002077 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002079
2080 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002081 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002082 start = *pp;
2083
Bram Moolenaare38eab22019-12-05 21:50:01 +01002084 // Check for hard coded <SNR>: already translated function ID (from a user
2085 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002086 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2087 && (*pp)[2] == (int)KE_SNR)
2088 {
2089 *pp += 3;
2090 len = get_id_len(pp) + 3;
2091 return vim_strnsave(start, len);
2092 }
2093
Bram Moolenaare38eab22019-12-05 21:50:01 +01002094 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2095 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002096 lead = eval_fname_script(start);
2097 if (lead > 2)
2098 start += lead;
2099
Bram Moolenaare38eab22019-12-05 21:50:01 +01002100 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002101 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002102 lead > 2 ? 0 : FNE_CHECK_START);
2103 if (end == start)
2104 {
2105 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002106 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002107 goto theend;
2108 }
2109 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2110 {
2111 /*
2112 * Report an invalid expression in braces, unless the expression
2113 * evaluation has been cancelled due to an aborting error, an
2114 * interrupt, or an exception.
2115 */
2116 if (!aborting())
2117 {
2118 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002119 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002120 }
2121 else
2122 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2123 goto theend;
2124 }
2125
2126 if (lv.ll_tv != NULL)
2127 {
2128 if (fdp != NULL)
2129 {
2130 fdp->fd_dict = lv.ll_dict;
2131 fdp->fd_newkey = lv.ll_newkey;
2132 lv.ll_newkey = NULL;
2133 fdp->fd_di = lv.ll_di;
2134 }
2135 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2136 {
2137 name = vim_strsave(lv.ll_tv->vval.v_string);
2138 *pp = end;
2139 }
2140 else if (lv.ll_tv->v_type == VAR_PARTIAL
2141 && lv.ll_tv->vval.v_partial != NULL)
2142 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002143 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002144 *pp = end;
2145 if (partial != NULL)
2146 *partial = lv.ll_tv->vval.v_partial;
2147 }
2148 else
2149 {
2150 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2151 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002152 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002153 else
2154 *pp = end;
2155 name = NULL;
2156 }
2157 goto theend;
2158 }
2159
2160 if (lv.ll_name == NULL)
2161 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002162 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002163 *pp = end;
2164 goto theend;
2165 }
2166
Bram Moolenaare38eab22019-12-05 21:50:01 +01002167 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002168 if (lv.ll_exp_name != NULL)
2169 {
2170 len = (int)STRLEN(lv.ll_exp_name);
2171 name = deref_func_name(lv.ll_exp_name, &len, partial,
2172 flags & TFN_NO_AUTOLOAD);
2173 if (name == lv.ll_exp_name)
2174 name = NULL;
2175 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002176 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002177 {
2178 len = (int)(end - *pp);
2179 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2180 if (name == *pp)
2181 name = NULL;
2182 }
2183 if (name != NULL)
2184 {
2185 name = vim_strsave(name);
2186 *pp = end;
2187 if (STRNCMP(name, "<SNR>", 5) == 0)
2188 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002189 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002190 name[0] = K_SPECIAL;
2191 name[1] = KS_EXTRA;
2192 name[2] = (int)KE_SNR;
2193 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2194 }
2195 goto theend;
2196 }
2197
2198 if (lv.ll_exp_name != NULL)
2199 {
2200 len = (int)STRLEN(lv.ll_exp_name);
2201 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2202 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2203 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002204 // When there was "s:" already or the name expanded to get a
2205 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002206 lv.ll_name += 2;
2207 len -= 2;
2208 lead = 2;
2209 }
2210 }
2211 else
2212 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002213 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002214 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
2215 lv.ll_name += 2;
2216 len = (int)(end - lv.ll_name);
2217 }
2218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 // In Vim9 script a user function is script-local by default.
2220 vim9script = ASCII_ISUPPER(*start)
2221 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2222
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002223 /*
2224 * Copy the function name to allocated memory.
2225 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2226 * Accept <SNR>123_name() outside a script.
2227 */
2228 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002229 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002231 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232 if (!vim9script)
2233 lead = 3;
2234 if (vim9script || (lv.ll_exp_name != NULL
2235 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002236 || eval_fname_sid(*pp))
2237 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002239 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002240 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002241 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002242 goto theend;
2243 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002244 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 if (vim9script)
2246 extra = 3 + (int)STRLEN(sid_buf);
2247 else
2248 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002249 }
2250 }
2251 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002253 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002254 start);
2255 goto theend;
2256 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002257 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002258 {
2259 char_u *cp = vim_strchr(lv.ll_name, ':');
2260
2261 if (cp != NULL && cp < end)
2262 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002263 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002264 goto theend;
2265 }
2266 }
2267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002269 if (name != NULL)
2270 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002271 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002272 {
2273 name[0] = K_SPECIAL;
2274 name[1] = KS_EXTRA;
2275 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002277 STRCPY(name + 3, sid_buf);
2278 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2280 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002281 }
2282 *pp = end;
2283
2284theend:
2285 clear_lval(&lv);
2286 return name;
2287}
2288
2289/*
2290 * ":function"
2291 */
2292 void
2293ex_function(exarg_T *eap)
2294{
2295 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002296 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002297 int j;
2298 int c;
2299 int saved_did_emsg;
2300 int saved_wait_return = need_wait_return;
2301 char_u *name = NULL;
2302 char_u *p;
2303 char_u *arg;
2304 char_u *line_arg = NULL;
2305 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002307 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002308 garray_T newlines;
2309 int varargs = FALSE;
2310 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002312 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002313 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002314 int indent;
2315 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316#define MAX_FUNC_NESTING 50
2317 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002318 dictitem_T *v;
2319 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002320 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002321 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002322 int todo;
2323 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002324 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002325 linenr_T sourcing_lnum_off;
2326 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002327 int is_heredoc = FALSE;
2328 char_u *skip_until = NULL;
2329 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002330
2331 /*
2332 * ":function" without argument: list functions.
2333 */
2334 if (ends_excmd(*eap->arg))
2335 {
2336 if (!eap->skip)
2337 {
2338 todo = (int)func_hashtab.ht_used;
2339 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2340 {
2341 if (!HASHITEM_EMPTY(hi))
2342 {
2343 --todo;
2344 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 if ((fp->uf_flags & FC_DEAD)
2346 || message_filtered(fp->uf_name))
Bram Moolenaarf86db782018-10-25 13:31:37 +02002347 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002348 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002349 list_func_head(fp, FALSE);
2350 }
2351 }
2352 }
2353 eap->nextcmd = check_nextcmd(eap->arg);
2354 return;
2355 }
2356
2357 /*
2358 * ":function /pat": list functions matching pattern.
2359 */
2360 if (*eap->arg == '/')
2361 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002362 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002363 if (!eap->skip)
2364 {
2365 regmatch_T regmatch;
2366
2367 c = *p;
2368 *p = NUL;
2369 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2370 *p = c;
2371 if (regmatch.regprog != NULL)
2372 {
2373 regmatch.rm_ic = p_ic;
2374
2375 todo = (int)func_hashtab.ht_used;
2376 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2377 {
2378 if (!HASHITEM_EMPTY(hi))
2379 {
2380 --todo;
2381 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 if ((fp->uf_flags & FC_DEAD) == 0
2383 && !isdigit(*fp->uf_name)
2384 && vim_regexec(&regmatch, fp->uf_name, 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002385 list_func_head(fp, FALSE);
2386 }
2387 }
2388 vim_regfree(regmatch.regprog);
2389 }
2390 }
2391 if (*p == '/')
2392 ++p;
2393 eap->nextcmd = check_nextcmd(p);
2394 return;
2395 }
2396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 ga_init(&newargs);
2398 ga_init(&argtypes);
2399 ga_init(&default_args);
2400
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002401 /*
2402 * Get the function name. There are these situations:
2403 * func normal function name
2404 * "name" == func, "fudi.fd_dict" == NULL
2405 * dict.func new dictionary entry
2406 * "name" == NULL, "fudi.fd_dict" set,
2407 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2408 * dict.func existing dict entry with a Funcref
2409 * "name" == func, "fudi.fd_dict" set,
2410 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2411 * dict.func existing dict entry that's not a Funcref
2412 * "name" == NULL, "fudi.fd_dict" set,
2413 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2414 * s:func script-local function name
2415 * g:func global function name, same as "func"
2416 */
2417 p = eap->arg;
Bram Moolenaar3388d332017-12-07 22:23:04 +01002418 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002419 paren = (vim_strchr(p, '(') != NULL);
2420 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2421 {
2422 /*
2423 * Return on an invalid expression in braces, unless the expression
2424 * evaluation has been cancelled due to an aborting error, an
2425 * interrupt, or an exception.
2426 */
2427 if (!aborting())
2428 {
2429 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002430 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002431 vim_free(fudi.fd_newkey);
2432 return;
2433 }
2434 else
2435 eap->skip = TRUE;
2436 }
2437
Bram Moolenaare38eab22019-12-05 21:50:01 +01002438 // An error in a function call during evaluation of an expression in magic
2439 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002440 saved_did_emsg = did_emsg;
2441 did_emsg = FALSE;
2442
2443 /*
2444 * ":function func" with only function name: list function.
2445 */
2446 if (!paren)
2447 {
2448 if (!ends_excmd(*skipwhite(p)))
2449 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002450 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002451 goto ret_free;
2452 }
2453 eap->nextcmd = check_nextcmd(p);
2454 if (eap->nextcmd != NULL)
2455 *p = NUL;
2456 if (!eap->skip && !got_int)
2457 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002459 if (fp != NULL)
2460 {
2461 list_func_head(fp, TRUE);
2462 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2463 {
2464 if (FUNCLINE(fp, j) == NULL)
2465 continue;
2466 msg_putchar('\n');
2467 msg_outnum((long)(j + 1));
2468 if (j < 9)
2469 msg_putchar(' ');
2470 if (j < 99)
2471 msg_putchar(' ');
2472 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002473 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002474 ui_breakcheck();
2475 }
2476 if (!got_int)
2477 {
2478 msg_putchar('\n');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 if (fp->uf_dfunc_idx >= 0)
2480 msg_puts(" enddef");
2481 else
2482 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002483 }
2484 }
2485 else
2486 emsg_funcname(N_("E123: Undefined function: %s"), name);
2487 }
2488 goto ret_free;
2489 }
2490
2491 /*
2492 * ":function name(arg1, arg2)" Define function.
2493 */
2494 p = skipwhite(p);
2495 if (*p != '(')
2496 {
2497 if (!eap->skip)
2498 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002499 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002500 goto ret_free;
2501 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002502 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002503 if (vim_strchr(p, '(') != NULL)
2504 p = vim_strchr(p, '(');
2505 }
2506 p = skipwhite(p + 1);
2507
2508 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2509
2510 if (!eap->skip)
2511 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002512 // Check the name of the function. Unless it's a dictionary function
2513 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002514 if (name != NULL)
2515 arg = name;
2516 else
2517 arg = fudi.fd_newkey;
2518 if (arg != NULL && (fudi.fd_di == NULL
2519 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2520 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2521 {
2522 if (*arg == K_SPECIAL)
2523 j = 3;
2524 else
2525 j = 0;
2526 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2527 : eval_isnamec(arg[j])))
2528 ++j;
2529 if (arg[j] != NUL)
2530 emsg_funcname((char *)e_invarg2, arg);
2531 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002532 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002533 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002534 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002535 }
2536
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002537 // This may get more lines and make the pointers into the first line
2538 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 if (get_function_args(&p, ')', &newargs,
2540 eap->cmdidx == CMD_def ? &argtypes : NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002541 &varargs, &default_args, eap->skip,
2542 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002543 goto errret_2;
2544
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002546 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 // find the return type: :def Func(): type
2548 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002549 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 ret_type = skipwhite(p + 1);
2551 p = skip_type(ret_type);
2552 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002553 {
2554 ret_type = vim_strnsave(ret_type, (int)(p - ret_type));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002556 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002558 {
2559 ret_type = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002561 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002562 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 else
2565 // find extra arguments "range", "dict", "abort" and "closure"
2566 for (;;)
2567 {
2568 p = skipwhite(p);
2569 if (STRNCMP(p, "range", 5) == 0)
2570 {
2571 flags |= FC_RANGE;
2572 p += 5;
2573 }
2574 else if (STRNCMP(p, "dict", 4) == 0)
2575 {
2576 flags |= FC_DICT;
2577 p += 4;
2578 }
2579 else if (STRNCMP(p, "abort", 5) == 0)
2580 {
2581 flags |= FC_ABORT;
2582 p += 5;
2583 }
2584 else if (STRNCMP(p, "closure", 7) == 0)
2585 {
2586 flags |= FC_CLOSURE;
2587 p += 7;
2588 if (current_funccal == NULL)
2589 {
2590 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2591 name == NULL ? (char_u *)"" : name);
2592 goto erret;
2593 }
2594 }
2595 else
2596 break;
2597 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002598
Bram Moolenaare38eab22019-12-05 21:50:01 +01002599 // When there is a line break use what follows for the function body.
2600 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002601 if (*p == '\n')
2602 line_arg = p + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002603 else if (*p != NUL && *p != '"' && !(eap->cmdidx == CMD_def && *p == '#')
2604 && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002605 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002606
2607 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2609 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002610 */
2611 if (KeyTyped)
2612 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002613 // Check if the function already exists, don't let the user type the
2614 // whole function before telling him it doesn't work! For a script we
2615 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002616 if (!eap->skip && !eap->forceit)
2617 {
2618 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002619 emsg(_(e_funcdict));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620 else if (name != NULL && find_func(name, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002621 emsg_funcname(e_funcexts, name);
2622 }
2623
2624 if (!eap->skip && did_emsg)
2625 goto erret;
2626
Bram Moolenaare38eab22019-12-05 21:50:01 +01002627 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002628 cmdline_row = msg_row;
2629 }
2630
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002631 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002632 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002633
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002634 indent = 2;
2635 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002637 for (;;)
2638 {
2639 if (KeyTyped)
2640 {
2641 msg_scroll = TRUE;
2642 saved_wait_return = FALSE;
2643 }
2644 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002645
2646 if (line_arg != NULL)
2647 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002648 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002649 theline = line_arg;
2650 p = vim_strchr(theline, '\n');
2651 if (p == NULL)
2652 line_arg += STRLEN(line_arg);
2653 else
2654 {
2655 *p = NUL;
2656 line_arg = p + 1;
2657 }
2658 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002659 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002660 {
2661 vim_free(line_to_free);
2662 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002663 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002664 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002665 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002666 line_to_free = theline;
2667 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002668 if (KeyTyped)
2669 lines_left = Rows - 1;
2670 if (theline == NULL)
2671 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 if (eap->cmdidx == CMD_def)
2673 emsg(_("E1057: Missing :enddef"));
2674 else
2675 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002676 goto erret;
2677 }
2678
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002679 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002680 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002681 if (SOURCING_LNUM < sourcing_lnum_off)
2682 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002683 else
2684 sourcing_lnum_off = 0;
2685
2686 if (skip_until != NULL)
2687 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002689 // * ":append" and "."
2690 // * ":python <<EOF" and "EOF"
2691 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2692 if (heredoc_trimmed == NULL
2693 || (is_heredoc && skipwhite(theline) == theline)
2694 || STRNCMP(theline, heredoc_trimmed,
2695 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002696 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002697 if (heredoc_trimmed == NULL)
2698 p = theline;
2699 else if (is_heredoc)
2700 p = skipwhite(theline) == theline
2701 ? theline : theline + STRLEN(heredoc_trimmed);
2702 else
2703 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002704 if (STRCMP(p, skip_until) == 0)
2705 {
2706 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002707 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002708 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002709 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002710 }
2711 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002712 }
2713 else
2714 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002715 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002716 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002717 ;
2718
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 // Check for "endfunction" or "enddef".
2720 if (checkforcmd(&p, nesting_def[nesting]
2721 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002722 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002723 char_u *nextcmd = NULL;
2724
Bram Moolenaar663bb232017-06-22 19:12:10 +02002725 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002726 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002727 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002728 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002729 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 give_warning2(eap->cmdidx == CMD_def
2731 ? (char_u *)_("W1001: Text found after :enddef: %s")
2732 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002733 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002734 if (nextcmd != NULL)
2735 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002736 // Another command follows. If the line came from "eap" we
2737 // can simply point into it, otherwise we need to change
2738 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002739 eap->nextcmd = nextcmd;
2740 if (line_to_free != NULL)
2741 {
2742 vim_free(*eap->cmdlinep);
2743 *eap->cmdlinep = line_to_free;
2744 line_to_free = NULL;
2745 }
2746 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002747 break;
2748 }
2749
Bram Moolenaare38eab22019-12-05 21:50:01 +01002750 // Increase indent inside "if", "while", "for" and "try", decrease
2751 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002753 indent -= 2;
2754 else if (STRNCMP(p, "if", 2) == 0
2755 || STRNCMP(p, "wh", 2) == 0
2756 || STRNCMP(p, "for", 3) == 0
2757 || STRNCMP(p, "try", 3) == 0)
2758 indent += 2;
2759
Bram Moolenaare38eab22019-12-05 21:50:01 +01002760 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002761 // Only recognize "def" inside "def", not inside "function",
2762 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002764 if (checkforcmd(&p, "function", 2)
2765 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002766 {
2767 if (*p == '!')
2768 p = skipwhite(p + 1);
2769 p += eval_fname_script(p);
2770 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2771 if (*skipwhite(p) == '(')
2772 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 if (nesting == MAX_FUNC_NESTING - 1)
2774 emsg(_("E1058: function nesting too deep"));
2775 else
2776 {
2777 ++nesting;
2778 nesting_def[nesting] = (c == 'd');
2779 indent += 2;
2780 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002781 }
2782 }
2783
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002784 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002785 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002786 if (eap->cmdidx != CMD_def
2787 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002788 || (p[0] == 'c'
2789 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2790 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2791 && (STRNCMP(&p[3], "nge", 3) != 0
2792 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002793 || (p[0] == 'i'
2794 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002795 && (!ASCII_ISALPHA(p[2])
2796 || (p[2] == 's'
2797 && (!ASCII_ISALPHA(p[3])
2798 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002799 skip_until = vim_strsave((char_u *)".");
2800
Bram Moolenaare38eab22019-12-05 21:50:01 +01002801 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002802 arg = skipwhite(skiptowhite(p));
2803 if (arg[0] == '<' && arg[1] =='<'
2804 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002805 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2806 || ((p[2] == '3' || p[2] == 'x')
2807 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002808 || (p[0] == 'p' && p[1] == 'e'
2809 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2810 || (p[0] == 't' && p[1] == 'c'
2811 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2812 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2813 && !ASCII_ISALPHA(p[3]))
2814 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2815 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2816 || (p[0] == 'm' && p[1] == 'z'
2817 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2818 ))
2819 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002820 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821 p = skipwhite(arg + 2);
2822 if (*p == NUL)
2823 skip_until = vim_strsave((char_u *)".");
2824 else
2825 skip_until = vim_strsave(p);
2826 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002827
2828 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002829 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002830 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002831 if (*arg == '[')
2832 arg = vim_strchr(arg, ']');
2833 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002834 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002835 arg = skipwhite(skiptowhite(arg));
2836 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2837 && ((p[0] == 'l'
2838 && p[1] == 'e'
2839 && (!ASCII_ISALNUM(p[2])
2840 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02002841 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002842 p = skipwhite(arg + 3);
2843 if (STRNCMP(p, "trim", 4) == 0)
2844 {
2845 // Ignore leading white space.
2846 p = skipwhite(p + 4);
2847 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar8471e572019-05-19 21:37:18 +02002848 (int)(skipwhite(theline) - theline));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002849 }
2850 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2851 do_concat = FALSE;
2852 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002853 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002854 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002855 }
2856
Bram Moolenaare38eab22019-12-05 21:50:01 +01002857 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002858 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002859 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002860
Bram Moolenaare38eab22019-12-05 21:50:01 +01002861 // Copy the line to newly allocated memory. get_one_sourceline()
2862 // allocates 250 bytes per line, this saves 80% on average. The cost
2863 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002864 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002865 if (p == NULL)
2866 goto erret;
2867 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002868
Bram Moolenaare38eab22019-12-05 21:50:01 +01002869 // Add NULL lines for continuation lines, so that the line count is
2870 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002871 while (sourcing_lnum_off-- > 0)
2872 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2873
Bram Moolenaare38eab22019-12-05 21:50:01 +01002874 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002875 if (line_arg != NULL && *line_arg == NUL)
2876 line_arg = NULL;
2877 }
2878
Bram Moolenaare38eab22019-12-05 21:50:01 +01002879 // Don't define the function when skipping commands or when an error was
2880 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002881 if (eap->skip || did_emsg)
2882 goto erret;
2883
2884 /*
2885 * If there are no errors, add the function
2886 */
2887 if (fudi.fd_dict == NULL)
2888 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 hashtab_T *ht;
2890
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002891 v = find_var(name, &ht, FALSE);
2892 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2893 {
2894 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2895 name);
2896 goto erret;
2897 }
2898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 fp = find_func_even_dead(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002900 if (fp != NULL)
2901 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 int dead = fp->uf_flags & FC_DEAD;
2903
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002904 // Function can be replaced with "function!" and when sourcing the
2905 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002907 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2908 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002909 {
2910 emsg_funcname(e_funcexts, name);
2911 goto erret;
2912 }
2913 if (fp->uf_calls > 0)
2914 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002915 emsg_funcname(
2916 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002917 name);
2918 goto erret;
2919 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002920 if (fp->uf_refcount > 1)
2921 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002922 // This function is referenced somewhere, don't redefine it but
2923 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002924 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002925 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002926 fp = NULL;
2927 overwrite = TRUE;
2928 }
2929 else
2930 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002931 char_u *exp_name = fp->uf_name_exp;
2932
2933 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01002934 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002935 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002936 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002937 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002939#ifdef FEAT_PROFILE
2940 fp->uf_profiling = FALSE;
2941 fp->uf_prof_initialized = FALSE;
2942#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002943 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002944 }
2945 }
2946 else
2947 {
2948 char numbuf[20];
2949
2950 fp = NULL;
2951 if (fudi.fd_newkey == NULL && !eap->forceit)
2952 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002953 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002954 goto erret;
2955 }
2956 if (fudi.fd_di == NULL)
2957 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002958 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002959 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002960 goto erret;
2961 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002962 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002963 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964 goto erret;
2965
Bram Moolenaare38eab22019-12-05 21:50:01 +01002966 // Give the function a sequential number. Can only be used with a
2967 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002968 vim_free(name);
2969 sprintf(numbuf, "%d", ++func_nr);
2970 name = vim_strsave((char_u *)numbuf);
2971 if (name == NULL)
2972 goto erret;
2973 }
2974
2975 if (fp == NULL)
2976 {
2977 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
2978 {
2979 int slen, plen;
2980 char_u *scriptname;
2981
Bram Moolenaare38eab22019-12-05 21:50:01 +01002982 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002984 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002985 {
2986 scriptname = autoload_name(name);
2987 if (scriptname != NULL)
2988 {
2989 p = vim_strchr(scriptname, '/');
2990 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002991 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002992 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002993 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994 j = OK;
2995 vim_free(scriptname);
2996 }
2997 }
2998 if (j == FAIL)
2999 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003000 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003001 goto erret;
3002 }
3003 }
3004
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003005 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003006 if (fp == NULL)
3007 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 fp->uf_dfunc_idx = -1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003009
3010 if (fudi.fd_dict != NULL)
3011 {
3012 if (fudi.fd_di == NULL)
3013 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003014 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003015 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3016 if (fudi.fd_di == NULL)
3017 {
3018 vim_free(fp);
3019 goto erret;
3020 }
3021 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3022 {
3023 vim_free(fudi.fd_di);
3024 vim_free(fp);
3025 goto erret;
3026 }
3027 }
3028 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003029 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003030 clear_tv(&fudi.fd_di->di_tv);
3031 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003032 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003033
Bram Moolenaare38eab22019-12-05 21:50:01 +01003034 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003035 flags |= FC_DICT;
3036 }
3037
Bram Moolenaare38eab22019-12-05 21:50:01 +01003038 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003039 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003040 if (overwrite)
3041 {
3042 hi = hash_find(&func_hashtab, name);
3043 hi->hi_key = UF2HIKEY(fp);
3044 }
3045 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003046 {
3047 vim_free(fp);
3048 goto erret;
3049 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003050 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003051 }
3052 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003053 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003055 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056
3057 if (eap->cmdidx == CMD_def)
3058 {
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003059 int lnum_save = SOURCING_LNUM;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003060
3061 // error messages are for the first function line
3062 SOURCING_LNUM = sourcing_lnum_top;
3063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 // parse the argument types
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003065 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066
3067 if (argtypes.ga_len > 0)
3068 {
3069 // When "varargs" is set the last name/type goes into uf_va_name
3070 // and uf_va_type.
3071 int len = argtypes.ga_len - (varargs ? 1 : 0);
3072
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003073 if (len > 0)
3074 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 if (fp->uf_arg_types != NULL)
3076 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003077 int i;
3078 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079
3080 for (i = 0; i < len; ++ i)
3081 {
3082 p = ((char_u **)argtypes.ga_data)[i];
3083 if (p == NULL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003084 // will get the type from the default value
3085 type = &t_unknown;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003087 type = parse_type(&p, &fp->uf_type_list);
3088 if (type == NULL)
3089 {
3090 SOURCING_LNUM = lnum_save;
3091 goto errret_2;
3092 }
3093 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 }
3095 }
3096 if (varargs)
3097 {
3098 // Move the last argument "...name: type" to uf_va_name and
3099 // uf_va_type.
3100 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3101 [fp->uf_args.ga_len - 1];
3102 --fp->uf_args.ga_len;
3103 p = ((char_u **)argtypes.ga_data)[len];
3104 if (p == NULL)
3105 // todo: get type from default value
3106 fp->uf_va_type = &t_any;
3107 else
3108 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003109 if (fp->uf_va_type == NULL)
3110 {
3111 SOURCING_LNUM = lnum_save;
3112 goto errret_2;
3113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 }
3115 varargs = FALSE;
3116 }
3117
3118 // parse the return type, if any
3119 if (ret_type == NULL)
3120 fp->uf_ret_type = &t_void;
3121 else
3122 {
3123 p = ret_type;
3124 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3125 }
3126 }
3127
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003128 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003129 if ((flags & FC_CLOSURE) != 0)
3130 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003131 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003132 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003133 }
3134 else
3135 fp->uf_scoped = NULL;
3136
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003137#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003138 if (prof_def_func())
3139 func_do_profile(fp);
3140#endif
3141 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003142 if (sandbox)
3143 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003144 fp->uf_flags = flags;
3145 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003146 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003147 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003148 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 if (is_export)
3150 {
3151 fp->uf_flags |= FC_EXPORT;
3152 // let ex_export() know the export worked.
3153 is_export = FALSE;
3154 }
3155
3156 // ":def Func()" needs to be compiled
3157 if (eap->cmdidx == CMD_def)
3158 compile_def_function(fp, FALSE);
3159
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003160 goto ret_free;
3161
3162erret:
3163 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003164 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003165errret_2:
3166 ga_clear_strings(&newlines);
3167ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003168 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003169 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003170 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003171 vim_free(fudi.fd_newkey);
3172 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003173 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003174 did_emsg |= saved_did_emsg;
3175 need_wait_return |= saved_wait_return;
3176}
3177
3178/*
3179 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3180 * Return 2 if "p" starts with "s:".
3181 * Return 0 otherwise.
3182 */
3183 int
3184eval_fname_script(char_u *p)
3185{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003186 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3187 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003188 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3189 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3190 return 5;
3191 if (p[0] == 's' && p[1] == ':')
3192 return 2;
3193 return 0;
3194}
3195
3196 int
3197translated_function_exists(char_u *name)
3198{
3199 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003200 return has_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 return find_func(name, NULL) != NULL;
3202}
3203
3204/*
3205 * Return TRUE when "ufunc" has old-style "..." varargs
3206 * or named varargs "...name: type".
3207 */
3208 int
3209has_varargs(ufunc_T *ufunc)
3210{
3211 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003212}
3213
3214/*
3215 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003216 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003217 */
3218 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003219function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003220{
3221 char_u *nm = name;
3222 char_u *p;
3223 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003224 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003225
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003226 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3227 if (no_deref)
3228 flag |= TFN_NO_DEREF;
3229 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003230 nm = skipwhite(nm);
3231
Bram Moolenaare38eab22019-12-05 21:50:01 +01003232 // Only accept "funcname", "funcname ", "funcname (..." and
3233 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003234 if (p != NULL && (*nm == NUL || *nm == '('))
3235 n = translated_function_exists(p);
3236 vim_free(p);
3237 return n;
3238}
3239
Bram Moolenaar113e1072019-01-20 15:30:40 +01003240#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003241 char_u *
3242get_expanded_name(char_u *name, int check)
3243{
3244 char_u *nm = name;
3245 char_u *p;
3246
3247 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
3248
3249 if (p != NULL && *nm == NUL)
3250 if (!check || translated_function_exists(p))
3251 return p;
3252
3253 vim_free(p);
3254 return NULL;
3255}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003256#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003257
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003258/*
3259 * Function given to ExpandGeneric() to obtain the list of user defined
3260 * function names.
3261 */
3262 char_u *
3263get_user_func_name(expand_T *xp, int idx)
3264{
3265 static long_u done;
3266 static hashitem_T *hi;
3267 ufunc_T *fp;
3268
3269 if (idx == 0)
3270 {
3271 done = 0;
3272 hi = func_hashtab.ht_array;
3273 }
3274 if (done < func_hashtab.ht_used)
3275 {
3276 if (done++ > 0)
3277 ++hi;
3278 while (HASHITEM_EMPTY(hi))
3279 ++hi;
3280 fp = HI2UF(hi);
3281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 // don't show dead, dict and lambda functions
3283 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003284 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003286
3287 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003288 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003289
3290 cat_func_name(IObuff, fp);
3291 if (xp->xp_context != EXPAND_USER_FUNC)
3292 {
3293 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003295 STRCAT(IObuff, ")");
3296 }
3297 return IObuff;
3298 }
3299 return NULL;
3300}
3301
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003302/*
3303 * ":delfunction {name}"
3304 */
3305 void
3306ex_delfunction(exarg_T *eap)
3307{
3308 ufunc_T *fp = NULL;
3309 char_u *p;
3310 char_u *name;
3311 funcdict_T fudi;
3312
3313 p = eap->arg;
3314 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
3315 vim_free(fudi.fd_newkey);
3316 if (name == NULL)
3317 {
3318 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003319 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003320 return;
3321 }
3322 if (!ends_excmd(*skipwhite(p)))
3323 {
3324 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003325 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003326 return;
3327 }
3328 eap->nextcmd = check_nextcmd(p);
3329 if (eap->nextcmd != NULL)
3330 *p = NUL;
3331
3332 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003334 vim_free(name);
3335
3336 if (!eap->skip)
3337 {
3338 if (fp == NULL)
3339 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003340 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003341 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003342 return;
3343 }
3344 if (fp->uf_calls > 0)
3345 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003346 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003347 return;
3348 }
3349
3350 if (fudi.fd_dict != NULL)
3351 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003352 // Delete the dict item that refers to the function, it will
3353 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003354 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3355 }
3356 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003357 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003358 // A normal function (not a numbered function or lambda) has a
3359 // refcount of 1 for the entry in the hashtable. When deleting
3360 // it and the refcount is more than one, it should be kept.
3361 // A numbered function and lambda should be kept if the refcount is
3362 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003363 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003364 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003365 // Function is still referenced somewhere. Don't free it but
3366 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003367 if (func_remove(fp))
3368 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003369 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003370 }
3371 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003372 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003373 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003374 }
3375}
3376
3377/*
3378 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003379 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003380 */
3381 void
3382func_unref(char_u *name)
3383{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003384 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003385
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003386 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003387 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003389 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003390 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003391#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003392 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003393#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003394 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003395 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003396 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003397 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003398 // Only delete it when it's not being used. Otherwise it's done
3399 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003400 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003401 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003402 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003403}
3404
3405/*
3406 * Unreference a Function: decrement the reference count and free it when it
3407 * becomes zero.
3408 */
3409 void
3410func_ptr_unref(ufunc_T *fp)
3411{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003412 if (fp != NULL && --fp->uf_refcount <= 0)
3413 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003414 // Only delete it when it's not being used. Otherwise it's done
3415 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003416 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003417 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003418 }
3419}
3420
3421/*
3422 * Count a reference to a Function.
3423 */
3424 void
3425func_ref(char_u *name)
3426{
3427 ufunc_T *fp;
3428
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003429 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003430 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003432 if (fp != NULL)
3433 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003434 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003435 // Only give an error for a numbered function.
3436 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003437 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003438}
3439
3440/*
3441 * Count a reference to a Function.
3442 */
3443 void
3444func_ptr_ref(ufunc_T *fp)
3445{
3446 if (fp != NULL)
3447 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003448}
3449
3450/*
3451 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3452 * referenced from anywhere that is in use.
3453 */
3454 static int
3455can_free_funccal(funccall_T *fc, int copyID)
3456{
3457 return (fc->l_varlist.lv_copyID != copyID
3458 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003459 && fc->l_avars.dv_copyID != copyID
3460 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003461}
3462
3463/*
3464 * ":return [expr]"
3465 */
3466 void
3467ex_return(exarg_T *eap)
3468{
3469 char_u *arg = eap->arg;
3470 typval_T rettv;
3471 int returning = FALSE;
3472
3473 if (current_funccal == NULL)
3474 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003475 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003476 return;
3477 }
3478
3479 if (eap->skip)
3480 ++emsg_skip;
3481
3482 eap->nextcmd = NULL;
3483 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3484 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3485 {
3486 if (!eap->skip)
3487 returning = do_return(eap, FALSE, TRUE, &rettv);
3488 else
3489 clear_tv(&rettv);
3490 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003491 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003492 else if (!eap->skip)
3493 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003494 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003495 update_force_abort();
3496
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003497 /*
3498 * Return unless the expression evaluation has been cancelled due to an
3499 * aborting error, an interrupt, or an exception.
3500 */
3501 if (!aborting())
3502 returning = do_return(eap, FALSE, TRUE, NULL);
3503 }
3504
Bram Moolenaare38eab22019-12-05 21:50:01 +01003505 // When skipping or the return gets pending, advance to the next command
3506 // in this line (!returning). Otherwise, ignore the rest of the line.
3507 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003508 if (returning)
3509 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003510 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003511 eap->nextcmd = check_nextcmd(arg);
3512
3513 if (eap->skip)
3514 --emsg_skip;
3515}
3516
3517/*
3518 * ":1,25call func(arg1, arg2)" function call.
3519 */
3520 void
3521ex_call(exarg_T *eap)
3522{
3523 char_u *arg = eap->arg;
3524 char_u *startarg;
3525 char_u *name;
3526 char_u *tofree;
3527 int len;
3528 typval_T rettv;
3529 linenr_T lnum;
3530 int doesrange;
3531 int failed = FALSE;
3532 funcdict_T fudi;
3533 partial_T *partial = NULL;
3534
3535 if (eap->skip)
3536 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003537 // trans_function_name() doesn't work well when skipping, use eval0()
3538 // instead to skip to any following command, e.g. for:
3539 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003540 ++emsg_skip;
3541 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3542 clear_tv(&rettv);
3543 --emsg_skip;
3544 return;
3545 }
3546
3547 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
3548 if (fudi.fd_newkey != NULL)
3549 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003550 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003551 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003552 vim_free(fudi.fd_newkey);
3553 }
3554 if (tofree == NULL)
3555 return;
3556
Bram Moolenaare38eab22019-12-05 21:50:01 +01003557 // Increase refcount on dictionary, it could get deleted when evaluating
3558 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003559 if (fudi.fd_dict != NULL)
3560 ++fudi.fd_dict->dv_refcount;
3561
Bram Moolenaare38eab22019-12-05 21:50:01 +01003562 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3563 // contents. For VAR_PARTIAL get its partial, unless we already have one
3564 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003565 len = (int)STRLEN(tofree);
3566 name = deref_func_name(tofree, &len,
3567 partial != NULL ? NULL : &partial, FALSE);
3568
Bram Moolenaare38eab22019-12-05 21:50:01 +01003569 // Skip white space to allow ":call func ()". Not good, but required for
3570 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003571 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003572 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003573
3574 if (*startarg != '(')
3575 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003577 goto end;
3578 }
3579
3580 /*
3581 * When skipping, evaluate the function once, to find the end of the
3582 * arguments.
3583 * When the function takes a range, this is discovered after the first
3584 * call, and the loop is broken.
3585 */
3586 if (eap->skip)
3587 {
3588 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003589 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003590 }
3591 else
3592 lnum = eap->line1;
3593 for ( ; lnum <= eap->line2; ++lnum)
3594 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003595 funcexe_T funcexe;
3596
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003597 if (!eap->skip && eap->addr_count > 0)
3598 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003599 if (lnum > curbuf->b_ml.ml_line_count)
3600 {
3601 // If the function deleted lines or switched to another buffer
3602 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003603 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003604 break;
3605 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003606 curwin->w_cursor.lnum = lnum;
3607 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003608 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003609 }
3610 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003611
Bram Moolenaara80faa82020-04-12 19:37:17 +02003612 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003613 funcexe.firstline = eap->line1;
3614 funcexe.lastline = eap->line2;
3615 funcexe.doesrange = &doesrange;
3616 funcexe.evaluate = !eap->skip;
3617 funcexe.partial = partial;
3618 funcexe.selfdict = fudi.fd_dict;
3619 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003620 {
3621 failed = TRUE;
3622 break;
3623 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003624 if (has_watchexpr())
3625 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003626
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003627 // Handle a function returning a Funcref, Dictionary or List.
3628 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE,
3629 name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003630 {
3631 failed = TRUE;
3632 break;
3633 }
3634
3635 clear_tv(&rettv);
3636 if (doesrange || eap->skip)
3637 break;
3638
Bram Moolenaare38eab22019-12-05 21:50:01 +01003639 // Stop when immediately aborting on error, or when an interrupt
3640 // occurred or an exception was thrown but not caught.
3641 // get_func_tv() returned OK, so that the check for trailing
3642 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003643 if (aborting())
3644 break;
3645 }
3646 if (eap->skip)
3647 --emsg_skip;
3648
Bram Moolenaare51bb172020-02-16 19:42:23 +01003649 // When inside :try we need to check for following "| catch".
3650 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003651 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003652 // Check for trailing illegal characters and a following command.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003653 if (!ends_excmd(*arg))
3654 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01003655 if (!failed)
3656 {
3657 emsg_severe = TRUE;
3658 emsg(_(e_trailing));
3659 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003660 }
3661 else
3662 eap->nextcmd = check_nextcmd(arg);
3663 }
3664
3665end:
3666 dict_unref(fudi.fd_dict);
3667 vim_free(tofree);
3668}
3669
3670/*
3671 * Return from a function. Possibly makes the return pending. Also called
3672 * for a pending return at the ":endtry" or after returning from an extra
3673 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3674 * when called due to a ":return" command. "rettv" may point to a typval_T
3675 * with the return rettv. Returns TRUE when the return can be carried out,
3676 * FALSE when the return gets pending.
3677 */
3678 int
3679do_return(
3680 exarg_T *eap,
3681 int reanimate,
3682 int is_cmd,
3683 void *rettv)
3684{
3685 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003686 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003687
3688 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003689 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003690 current_funccal->returned = FALSE;
3691
3692 /*
3693 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3694 * not in its finally clause (which then is to be executed next) is found.
3695 * In this case, make the ":return" pending for execution at the ":endtry".
3696 * Otherwise, return normally.
3697 */
3698 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3699 if (idx >= 0)
3700 {
3701 cstack->cs_pending[idx] = CSTP_RETURN;
3702
3703 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003704 // A pending return again gets pending. "rettv" points to an
3705 // allocated variable with the rettv of the original ":return"'s
3706 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003707 cstack->cs_rettv[idx] = rettv;
3708 else
3709 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003710 // When undoing a return in order to make it pending, get the stored
3711 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003712 if (reanimate)
3713 rettv = current_funccal->rettv;
3714
3715 if (rettv != NULL)
3716 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003717 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003718 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3719 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3720 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003721 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003722 }
3723 else
3724 cstack->cs_rettv[idx] = NULL;
3725
3726 if (reanimate)
3727 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003728 // The pending return value could be overwritten by a ":return"
3729 // without argument in a finally clause; reset the default
3730 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003731 current_funccal->rettv->v_type = VAR_NUMBER;
3732 current_funccal->rettv->vval.v_number = 0;
3733 }
3734 }
3735 report_make_pending(CSTP_RETURN, rettv);
3736 }
3737 else
3738 {
3739 current_funccal->returned = TRUE;
3740
Bram Moolenaare38eab22019-12-05 21:50:01 +01003741 // If the return is carried out now, store the return value. For
3742 // a return immediately after reanimation, the value is already
3743 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003744 if (!reanimate && rettv != NULL)
3745 {
3746 clear_tv(current_funccal->rettv);
3747 *current_funccal->rettv = *(typval_T *)rettv;
3748 if (!is_cmd)
3749 vim_free(rettv);
3750 }
3751 }
3752
3753 return idx < 0;
3754}
3755
3756/*
3757 * Free the variable with a pending return value.
3758 */
3759 void
3760discard_pending_return(void *rettv)
3761{
3762 free_tv((typval_T *)rettv);
3763}
3764
3765/*
3766 * Generate a return command for producing the value of "rettv". The result
3767 * is an allocated string. Used by report_pending() for verbose messages.
3768 */
3769 char_u *
3770get_return_cmd(void *rettv)
3771{
3772 char_u *s = NULL;
3773 char_u *tofree = NULL;
3774 char_u numbuf[NUMBUFLEN];
3775
3776 if (rettv != NULL)
3777 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3778 if (s == NULL)
3779 s = (char_u *)"";
3780
3781 STRCPY(IObuff, ":return ");
3782 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3783 if (STRLEN(s) + 8 >= IOSIZE)
3784 STRCPY(IObuff + IOSIZE - 4, "...");
3785 vim_free(tofree);
3786 return vim_strsave(IObuff);
3787}
3788
3789/*
3790 * Get next function line.
3791 * Called by do_cmdline() to get the next line.
3792 * Returns allocated string, or NULL for end of function.
3793 */
3794 char_u *
3795get_func_line(
3796 int c UNUSED,
3797 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003798 int indent UNUSED,
3799 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003800{
3801 funccall_T *fcp = (funccall_T *)cookie;
3802 ufunc_T *fp = fcp->func;
3803 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003804 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003805
Bram Moolenaare38eab22019-12-05 21:50:01 +01003806 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003807 if (fcp->dbg_tick != debug_tick)
3808 {
3809 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003810 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003811 fcp->dbg_tick = debug_tick;
3812 }
3813#ifdef FEAT_PROFILE
3814 if (do_profiling == PROF_YES)
3815 func_line_end(cookie);
3816#endif
3817
3818 gap = &fp->uf_lines;
3819 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3820 || fcp->returned)
3821 retval = NULL;
3822 else
3823 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003824 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003825 while (fcp->linenr < gap->ga_len
3826 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3827 ++fcp->linenr;
3828 if (fcp->linenr >= gap->ga_len)
3829 retval = NULL;
3830 else
3831 {
3832 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003833 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003834#ifdef FEAT_PROFILE
3835 if (do_profiling == PROF_YES)
3836 func_line_start(cookie);
3837#endif
3838 }
3839 }
3840
Bram Moolenaare38eab22019-12-05 21:50:01 +01003841 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003842 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003843 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003844 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003845 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003846 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003847 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003848 fcp->dbg_tick = debug_tick;
3849 }
3850
3851 return retval;
3852}
3853
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003854/*
3855 * Return TRUE if the currently active function should be ended, because a
3856 * return was encountered or an error occurred. Used inside a ":while".
3857 */
3858 int
3859func_has_ended(void *cookie)
3860{
3861 funccall_T *fcp = (funccall_T *)cookie;
3862
Bram Moolenaare38eab22019-12-05 21:50:01 +01003863 // Ignore the "abort" flag if the abortion behavior has been changed due to
3864 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003865 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3866 || fcp->returned);
3867}
3868
3869/*
3870 * return TRUE if cookie indicates a function which "abort"s on errors.
3871 */
3872 int
3873func_has_abort(
3874 void *cookie)
3875{
3876 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3877}
3878
3879
3880/*
3881 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3882 * Don't do this when "Func" is already a partial that was bound
3883 * explicitly (pt_auto is FALSE).
3884 * Changes "rettv" in-place.
3885 * Returns the updated "selfdict_in".
3886 */
3887 dict_T *
3888make_partial(dict_T *selfdict_in, typval_T *rettv)
3889{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003890 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003891 char_u *tofree = NULL;
3892 ufunc_T *fp;
3893 char_u fname_buf[FLEN_FIXED + 1];
3894 int error;
3895 dict_T *selfdict = selfdict_in;
3896
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003897 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3898 fp = rettv->vval.v_partial->pt_func;
3899 else
3900 {
3901 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3902 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003903 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003904 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003906 vim_free(tofree);
3907 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003908
3909 if (fp != NULL && (fp->uf_flags & FC_DICT))
3910 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003911 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003912
3913 if (pt != NULL)
3914 {
3915 pt->pt_refcount = 1;
3916 pt->pt_dict = selfdict;
3917 pt->pt_auto = TRUE;
3918 selfdict = NULL;
3919 if (rettv->v_type == VAR_FUNC)
3920 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003921 // Just a function: Take over the function name and use
3922 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003923 pt->pt_name = rettv->vval.v_string;
3924 }
3925 else
3926 {
3927 partial_T *ret_pt = rettv->vval.v_partial;
3928 int i;
3929
Bram Moolenaare38eab22019-12-05 21:50:01 +01003930 // Partial: copy the function name, use selfdict and copy
3931 // args. Can't take over name or args, the partial might
3932 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003933 if (ret_pt->pt_name != NULL)
3934 {
3935 pt->pt_name = vim_strsave(ret_pt->pt_name);
3936 func_ref(pt->pt_name);
3937 }
3938 else
3939 {
3940 pt->pt_func = ret_pt->pt_func;
3941 func_ptr_ref(pt->pt_func);
3942 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003943 if (ret_pt->pt_argc > 0)
3944 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003945 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003946 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003947 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003948 pt->pt_argc = 0;
3949 else
3950 {
3951 pt->pt_argc = ret_pt->pt_argc;
3952 for (i = 0; i < pt->pt_argc; i++)
3953 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
3954 }
3955 }
3956 partial_unref(ret_pt);
3957 }
3958 rettv->v_type = VAR_PARTIAL;
3959 rettv->vval.v_partial = pt;
3960 }
3961 }
3962 return selfdict;
3963}
3964
3965/*
3966 * Return the name of the executed function.
3967 */
3968 char_u *
3969func_name(void *cookie)
3970{
3971 return ((funccall_T *)cookie)->func->uf_name;
3972}
3973
3974/*
3975 * Return the address holding the next breakpoint line for a funccall cookie.
3976 */
3977 linenr_T *
3978func_breakpoint(void *cookie)
3979{
3980 return &((funccall_T *)cookie)->breakpoint;
3981}
3982
3983/*
3984 * Return the address holding the debug tick for a funccall cookie.
3985 */
3986 int *
3987func_dbg_tick(void *cookie)
3988{
3989 return &((funccall_T *)cookie)->dbg_tick;
3990}
3991
3992/*
3993 * Return the nesting level for a funccall cookie.
3994 */
3995 int
3996func_level(void *cookie)
3997{
3998 return ((funccall_T *)cookie)->level;
3999}
4000
4001/*
4002 * Return TRUE when a function was ended by a ":return" command.
4003 */
4004 int
4005current_func_returned(void)
4006{
4007 return current_funccal->returned;
4008}
4009
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004010 int
4011free_unref_funccal(int copyID, int testing)
4012{
4013 int did_free = FALSE;
4014 int did_free_funccal = FALSE;
4015 funccall_T *fc, **pfc;
4016
4017 for (pfc = &previous_funccal; *pfc != NULL; )
4018 {
4019 if (can_free_funccal(*pfc, copyID))
4020 {
4021 fc = *pfc;
4022 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004023 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004024 did_free = TRUE;
4025 did_free_funccal = TRUE;
4026 }
4027 else
4028 pfc = &(*pfc)->caller;
4029 }
4030 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004031 // When a funccal was freed some more items might be garbage
4032 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004033 (void)garbage_collect(testing);
4034
4035 return did_free;
4036}
4037
4038/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004039 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004040 */
4041 static funccall_T *
4042get_funccal(void)
4043{
4044 int i;
4045 funccall_T *funccal;
4046 funccall_T *temp_funccal;
4047
4048 funccal = current_funccal;
4049 if (debug_backtrace_level > 0)
4050 {
4051 for (i = 0; i < debug_backtrace_level; i++)
4052 {
4053 temp_funccal = funccal->caller;
4054 if (temp_funccal)
4055 funccal = temp_funccal;
4056 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004057 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004058 debug_backtrace_level = i;
4059 }
4060 }
4061 return funccal;
4062}
4063
4064/*
4065 * Return the hashtable used for local variables in the current funccal.
4066 * Return NULL if there is no current funccal.
4067 */
4068 hashtab_T *
4069get_funccal_local_ht()
4070{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004072 return NULL;
4073 return &get_funccal()->l_vars.dv_hashtab;
4074}
4075
4076/*
4077 * Return the l: scope variable.
4078 * Return NULL if there is no current funccal.
4079 */
4080 dictitem_T *
4081get_funccal_local_var()
4082{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004084 return NULL;
4085 return &get_funccal()->l_vars_var;
4086}
4087
4088/*
4089 * Return the hashtable used for argument in the current funccal.
4090 * Return NULL if there is no current funccal.
4091 */
4092 hashtab_T *
4093get_funccal_args_ht()
4094{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004096 return NULL;
4097 return &get_funccal()->l_avars.dv_hashtab;
4098}
4099
4100/*
4101 * Return the a: scope variable.
4102 * Return NULL if there is no current funccal.
4103 */
4104 dictitem_T *
4105get_funccal_args_var()
4106{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004108 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004109 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004110}
4111
4112/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004113 * List function variables, if there is a function.
4114 */
4115 void
4116list_func_vars(int *first)
4117{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004119 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004120 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004121}
4122
4123/*
4124 * If "ht" is the hashtable for local variables in the current funccal, return
4125 * the dict that contains it.
4126 * Otherwise return NULL.
4127 */
4128 dict_T *
4129get_current_funccal_dict(hashtab_T *ht)
4130{
4131 if (current_funccal != NULL
4132 && ht == &current_funccal->l_vars.dv_hashtab)
4133 return &current_funccal->l_vars;
4134 return NULL;
4135}
4136
4137/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004138 * Search hashitem in parent scope.
4139 */
4140 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004141find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004142{
4143 funccall_T *old_current_funccal = current_funccal;
4144 hashtab_T *ht;
4145 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004146 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004147
4148 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4149 return NULL;
4150
Bram Moolenaare38eab22019-12-05 21:50:01 +01004151 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004152 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004153 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004154 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004155 ht = find_var_ht(name, &varname);
4156 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004157 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004158 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004159 if (!HASHITEM_EMPTY(hi))
4160 {
4161 *pht = ht;
4162 break;
4163 }
4164 }
4165 if (current_funccal == current_funccal->func->uf_scoped)
4166 break;
4167 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004168 }
4169 current_funccal = old_current_funccal;
4170
4171 return hi;
4172}
4173
4174/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004175 * Search variable in parent scope.
4176 */
4177 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004178find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004179{
4180 dictitem_T *v = NULL;
4181 funccall_T *old_current_funccal = current_funccal;
4182 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004183 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004184
4185 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4186 return NULL;
4187
Bram Moolenaare38eab22019-12-05 21:50:01 +01004188 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004189 current_funccal = current_funccal->func->uf_scoped;
4190 while (current_funccal)
4191 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004192 ht = find_var_ht(name, &varname);
4193 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004194 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004195 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004196 if (v != NULL)
4197 break;
4198 }
4199 if (current_funccal == current_funccal->func->uf_scoped)
4200 break;
4201 current_funccal = current_funccal->func->uf_scoped;
4202 }
4203 current_funccal = old_current_funccal;
4204
4205 return v;
4206}
4207
4208/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004209 * Set "copyID + 1" in previous_funccal and callers.
4210 */
4211 int
4212set_ref_in_previous_funccal(int copyID)
4213{
4214 int abort = FALSE;
4215 funccall_T *fc;
4216
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004217 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004218 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004219 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004220 abort = abort
4221 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4222 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004223 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004224 }
4225 return abort;
4226}
4227
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004228 static int
4229set_ref_in_funccal(funccall_T *fc, int copyID)
4230{
4231 int abort = FALSE;
4232
4233 if (fc->fc_copyID != copyID)
4234 {
4235 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004236 abort = abort
4237 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4238 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004239 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004240 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004241 }
4242 return abort;
4243}
4244
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004245/*
4246 * Set "copyID" in all local vars and arguments in the call stack.
4247 */
4248 int
4249set_ref_in_call_stack(int copyID)
4250{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004251 int abort = FALSE;
4252 funccall_T *fc;
4253 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004254
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004255 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004256 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004257
4258 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004259 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4260 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004261 abort = abort || set_ref_in_funccal(fc, copyID);
4262
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004263 return abort;
4264}
4265
4266/*
4267 * Set "copyID" in all functions available by name.
4268 */
4269 int
4270set_ref_in_functions(int copyID)
4271{
4272 int todo;
4273 hashitem_T *hi = NULL;
4274 int abort = FALSE;
4275 ufunc_T *fp;
4276
4277 todo = (int)func_hashtab.ht_used;
4278 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004279 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004280 if (!HASHITEM_EMPTY(hi))
4281 {
4282 --todo;
4283 fp = HI2UF(hi);
4284 if (!func_name_refcount(fp->uf_name))
4285 abort = abort || set_ref_in_func(NULL, fp, copyID);
4286 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004287 }
4288 return abort;
4289}
4290
4291/*
4292 * Set "copyID" in all function arguments.
4293 */
4294 int
4295set_ref_in_func_args(int copyID)
4296{
4297 int i;
4298 int abort = FALSE;
4299
4300 for (i = 0; i < funcargs.ga_len; ++i)
4301 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4302 copyID, NULL, NULL);
4303 return abort;
4304}
4305
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004306/*
4307 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004308 * Returns TRUE if setting references failed somehow.
4309 */
4310 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004311set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004312{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004313 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004314 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004315 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004316 char_u fname_buf[FLEN_FIXED + 1];
4317 char_u *tofree = NULL;
4318 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004319 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004320
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004321 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004322 return FALSE;
4323
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004324 if (fp_in == NULL)
4325 {
4326 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004328 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004329 if (fp != NULL)
4330 {
4331 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004332 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004333 }
4334 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004335 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004336}
4337
Bram Moolenaare38eab22019-12-05 21:50:01 +01004338#endif // FEAT_EVAL